.

--

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

 * 
 * Generic Table Editor
 * 
 * Inspired by http://extjs.com/deploy/dev/examples/writer/writer.html
 *
 * @author Bernhard Woehrlin
 * @copyright (c) 2008-2009, IT.CappuccinoNet.com
 * @date 2009-09-01 16:27:36
 * @version 1.3
 * @class Request
 */
class Request {
    public $restful, $method, $controller, $action, $id, $params;

    public function __construct($params) {
    	
        $this->restful = (isset($params["restful"])) ? $params["restful"]  : false;
				$this->method = $_SERVER["REQUEST_METHOD"];
        $this->parseRequest();
				
				#echo 'restful: '.$this->restful.', ';
        #echo 'method: '.$this->method.', ';
        #echo 'controller: '.$this->controller.', ';
        #echo 'action: '.$this->action.', ';
        #echo 'id: '.$this->id ;
    }
    public function isRestful() {
        return $this->restful;
    }
    protected function parseRequest() {
    	
        if ($this->method == 'PUT') {   // <-- Have to jump through hoops to get PUT data
            $raw  = '';
            $httpContent = fopen('php://input', 'r');
						while ($kb = fread($httpContent, 1024)) {
                $raw .= $kb;
            }
            fclose($httpContent);
						
            $params = json_decode($raw, true);
						
						$this->id = (isset($params['id'])) ? $params['id'] : null;
            $this->params = (isset($params['data'])) ? $params['data'] : null;
						
        } else {
            // grab JSON data if there...
						
						#if ( isset($_GET['data'])== true) { echo "Ja"; }else { echo "Nein";}
						$this->params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data']), true) : null;
            $this->id = (isset($_REQUEST['id'])) ? json_decode(stripslashes($_REQUEST['id']), true) : null;
						if ($this->id == null) $this->id = stripslashes($_REQUEST['data']);
        }
				$this->params['start'] = $_REQUEST['start'];
        $this->params['limit'] = $_REQUEST['limit'];
      
				
				// Quickndirty PATH_INFO parser
        if (isset($_SERVER["PATH_INFO"])){
            $cai = '/^\/([a-z]+\w)\/([a-z]+\w)\/([0-9]+)$/';  // /controller/action/id
            $ca =  '/^\/([a-z]+\w)\/([a-z]+)$/';              // /controller/action
            $ci = '/^\/([a-z]+\w)\/([0-9]+)$/';               // /controller/id
            $c =  '/^\/([a-z]+\w)$/';                             // /controller
            $i =  '/^\/([0-9]+)$/';                             // /id
            $matches = array();
            if (preg_match($cai, $_SERVER["PATH_INFO"], $matches)) {
                $this->controller = $matches[1];
                $this->action = $matches[2];
                $this->id = $matches[3];
            } else if (preg_match($ca, $_SERVER["PATH_INFO"], $matches)) {
                $this->controller = $matches[1];
                $this->action = $matches[2];
            } else if (preg_match($ci, $_SERVER["PATH_INFO"], $matches)) {
                $this->controller = $matches[1];
                $this->id = $matches[2];
            } else if (preg_match($c, $_SERVER["PATH_INFO"], $matches)) {
                $this->controller = $matches[1];
            } else if (preg_match($i, $_SERVER["PATH_INFO"], $matches)) {
                $this->id = $matches[1];
            }
        }
    }
}