Example #1
0
 public static function processRequest()
 {
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     $data = array();
     switch ($request_method) {
         case 'get':
             $data = $_GET;
             break;
         case 'post':
             $data = $_POST;
             break;
         case 'put':
             // read a string from PHP's special input location, and then parse it out into an array via parse_str... per the PHP docs:
             // parses str as if it were the query string passed via a URL and sets variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
     }
     $return_obj->setMethod($request_method);
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         $return_obj->setData(json_decode($data['data']));
     }
     return $return_obj;
 }
 public static function processRequest()
 {
     // get the request method
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // store the request data
     $data = array();
     switch ($request_method) {
         // put data
         case 'put':
             $data = $_GET;
             break;
             // get data
         // get data
         case 'get':
             $data = $_GET;
             break;
             // post request
         // post request
         case 'post':
             $data = $_POST;
             break;
     }
     // store the method
     $return_obj->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         // translate the JSON to an Object for use however you want
         $return_obj->setData($json->decode($data['data']));
     }
     return $return_obj;
 }
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // we'll store our data here
     $data = array();
     switch ($request_method) {
         case 'get':
             $data = $_GET;
             break;
         case 'post':
             $data = $_POST;
             break;
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
     }
     // store the method
     $return_obj->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         // translate the JSON to an Object for use however you want
         $return_obj->setData(json_decode($data['data']));
     }
     return $return_obj;
 }
Example #4
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // we'll store our data here
     $data = array();
     switch ($request_method) {
         // gets are easy...
         case 'get':
             $data = $_GET;
             break;
             // so are posts
         // so are posts
         case 'post':
             $data = $_POST;
             break;
             // here's the tricky bit...
         // here's the tricky bit...
         case 'put':
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
     }
     // store the method
     $return_obj->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         // translate the JSON to an Object for use however you want
         $return_obj->setData(json_decode($data['data']));
     }
     return $return_obj;
 }
Example #5
0
 public static function initProcess()
 {
     $requestMethod = strtolower($_SERVER['REQUEST_METHOD']);
     $obj = new RestRequest();
     $data = array();
     $files = array();
     $_PUT_VARS = null;
     switch ($requestMethod) {
         case 'get':
             $data = $_GET;
             break;
         case 'post':
             $data = $_POST;
             if (isset($_FILES) && !empty($_FILES)) {
                 $files = $_FILES;
             }
             break;
         case 'put':
             parse_str(file_get_contents('php://input'), $_PUT_VARS);
             $data = $_PUT_VARS;
             break;
     }
     $obj->setMethod($requestMethod);
     $obj->setRequestVars($data);
     if (isset($data['data'])) {
         $obj->setData(json_decode($data['data']));
     }
     if (isset($files)) {
         $obj->setFiles($files);
     }
     return $obj;
 }
Example #6
0
 public static function processRequest()
 {
     // cogemos el metodo
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // aqui almacenaremos los datos$data = array();
     switch ($request_method) {
         // para get y post esto sera sencillo
         case 'get':
             $data = $_GET;
             break;
         case 'post':
             $data = $_POST;
             break;
             // Aqui esta lo "complicado"
         // Aqui esta lo "complicado"
         case 'put':
             //para put y delete usamos una cadena desde una localizacion de inputespecial de PHP y la parseamos con parse_str.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
         case 'delete':
             $data = '';
             //delete no acepta parametros
             break;
     }
     // almacenamos el metodo
     $return_obj->setMethod($request_method);
     // Almacenamos los parametros pasados en crudo, por si necesitamo sacceder a ellos
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         // asumimos que los datos estan pasados en forjato JSON
         $return_obj->setData(json_decode($data['data']));
     } else {
         if ($request_method == "get") {
             $return_obj->setData($data);
         }
     }
     return $return_obj;
 }
 public static function processRequest()
 {
     include 'config.php';
     $auth_username = $user;
     $auth_pass = $password;
     // figure out if we need to challenge the user
     if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
         header('HTTP/1.1 401 Unauthorized');
         header('WWW-Authenticate: Digest realm="' . AUTH_REALM . '",qop="auth",nonce="' . uniqid() . '",opaque="' . md5(AUTH_REALM) . '"');
         // show the error if they hit cancel
         die(RestUtils::sendResponse(401));
     }
     // now, analayze the PHP_AUTH_DIGEST var
     if (!($data = self::http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) || $auth_username != $data['username']) {
         // show the error due to bad auth
         die(RestUtils::sendResponse(401));
     }
     // so far, everything's good, let's now check the response a bit more...
     $A1 = md5($data['username'] . ':' . AUTH_REALM . ':' . $auth_pass);
     $A2 = md5($_SERVER['REQUEST_METHOD'] . ':' . $data['uri']);
     $valid_response = md5($A1 . ':' . $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . $A2);
     // last check..
     if ($data['response'] != $valid_response) {
         die(RestUtils::sendResponse(401));
     }
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // we'll store our data here
     $data = array();
     if (strcmp($request_method, 'post') == 0) {
         $data = $_POST;
         // store the method
         $return_obj->setMethod($request_method);
         // set the raw data, so we can access it if needed (there may be
         // other pieces to your requests)
         $return_obj->setRequestVars($data);
         if (isset($data['data'])) {
             // translate the JSON to an Object for use however you want
             $return_obj->setData(json_decode($data['data']));
         }
         return $return_obj;
     }
 }
Example #8
0
 public static function processRequest()
 {
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // store our data here
     $data = array();
     switch ($request_method) {
         case 'get':
             $data = $_GET;
             break;
         case 'post':
             $data = $_POST;
             break;
     }
     $return_obj->setMethod($request_method);
     $return_obj->setRequestVars($data);
     if (isset($data['data'])) {
         // translate the JSON to an Object for use however we want
         $return_obj->setData(json_decode($data['data'], true));
     }
     return $return_obj;
 }
Example #9
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     // we'll store our query here
     $query = array();
     switch ($request_method) {
         // gets are easy...
         case 'get':
             $query = $_GET;
             break;
             // so are posts
         // so are posts
         case 'post':
             $query = $_POST;
             break;
             // here's the tricky bit...
         // here's the tricky bit...
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $query = $put_vars;
             break;
     }
     // store the method
     $return_obj->setMethod($request_method);
     $request_vars = explode('/', $_GET['url']);
     $return_obj->setRequestVars($request_vars);
     $data = $query;
     unset($data['url']);
     $return_obj->setData($data);
     return $return_obj;
 }
Example #10
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $element = $_GET['REST_element'];
     $id = null;
     $operation = null;
     if (isset($_GET['REST_id'])) {
         $id = $_GET['REST_id'];
     }
     if (isset($_GET['REST_operation'])) {
         $operation = $_GET['REST_operation'];
     }
     $return_obj = new RestRequest();
     // we'll store our data here
     $data = array();
     //TO recode
     switch ($request_method) {
         // gets are easy...
         //already got element and id
         case 'get':
             $data = $_GET;
             unset($data['REST_element']);
             unset($data['REST_id']);
             unset($data['REST_operation']);
             unset($data['REST_format']);
             break;
             // so are posts
             /*case 'post':
             		$data = $_POST;
             		break;*/
             // here's the tricky bit...
         // so are posts
         /*case 'post':
         		$data = $_POST;
         		break;*/
         // here's the tricky bit...
         case 'put':
         case 'post':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             $contents = file_get_contents('php://input');
             //die('-'.$contents.'-');
             $data = RestUtils::data_decode($contents, $_SERVER['CONTENT_TYPE']);
             //$data = $contents;
             break;
     }
     // store the data
     $return_obj->setData($data);
     // store the method
     $return_obj->setMethod($request_method);
     // store the method
     $return_obj->setOperation($operation);
     // store the element
     $return_obj->setElement($element);
     // store the id
     $return_obj->setID($id);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $return_obj->setRequestVars($data);
     return $return_obj;
 }
Example #11
0
 public static function processRequest()
 {
     //Get our request method and data storage var
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $return_obj = new RestRequest();
     $data = array();
     //Verify request method
     switch ($request_method) {
         //GET
         case 'get':
             $data = $_GET;
             break;
             //POST
         //POST
         case 'post':
             $data = $_POST;
             break;
             //PUT
         //PUT
         case 'put':
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
             //DELETE
         //DELETE
         case 'delete':
             $data = $_GET;
             break;
             //OTHERS
         //OTHERS
         default:
             $data = $_GET;
             break;
     }
     //Store the method
     if (isset($_SERVER['HTTP_METHOD'])) {
         $request_method = strtolower($_SERVER['HTTP_METHOD']);
     } elseif (isset($data['method'])) {
         $request_method = strtolower($data['method']);
     }
     $return_obj->setMethod($request_method);
     //Store the data
     $return_obj->setRequestVars($data);
     //Store the JSON object
     if (isset($data['data'])) {
         $return_obj->setData(json_decode($data['data']));
     } else {
         $return_obj->setData($data);
     }
     //Return
     return $return_obj;
 }
Example #12
0
 public static function processRequest()
 {
     // What is the method used ?
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $request = new RestRequest();
     // Storing the requested data
     $data = array();
     switch ($request_method) {
         /* *** GET *** */
         case 'get':
             // Because of the htaccess ...
             // Extra parameters in the request URI
             $params = array();
             $parts = preg_split("~=|&~", $_SERVER['REQUEST_URI'], -1, PREG_SPLIT_NO_EMPTY);
             // Skip through the segments by 2
             for ($i = 0; $i < count($parts); $i = $i + 2) {
                 // First segment needs a lift because it contains the script name (method actually)
                 if ($i == 0) {
                     $parts[$i] = preg_replace("/(\\/[^\\?]*\\?)(.*)/", "\$2", $parts[$i]);
                 }
                 if (isset($parts[$i]) && isset($parts[$i + 1])) {
                     // First segment is the param name, second is the value
                     $params[$parts[$i]] = $parts[$i + 1];
                 }
             }
             $data = $params;
             $request->validateRequest();
             break;
             /* *** POST *** */
         /* *** POST *** */
         case 'post':
             $data = $_POST;
             $request->invalidateRequest();
             break;
             /* *** PUT *** */
         /* *** PUT *** */
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             $request->invalidateRequest();
             break;
     }
     // Storing the data into data (redundant for GET, but well)
     $request->setData((object) $data);
     // Storing the method used
     $request->setMethod($request_method);
     // Set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $request->setRequestVars($data);
     // If we have data (as per in a post request), we store it
     if (isset($data['data'])) {
         // translate the JSON to an Object for use however you want
         $request->setData(json_decode($data['data']));
     }
     // If we force JSON or XML instead of ACCEPT
     if (isset($data['alt']) && ($data['alt'] == 'json' || $data['alt'] == 'xml')) {
         $request->setHttpAccept($data['alt']);
     }
     return $request;
 }