array('POST'), 'createuser' => array('POST'), 'userlogin' => array('POST'), 'updateprofile' => array('POST'), 'updsprofile' => array('POST'), 'newlundrypickup' => array('POST'), 'newdrycleanpickup' => array('POST'), 'confirmlundrypickup' => array('POST'), 'savecardpayment' => array('POST'), 'getlundrylocation' => array('POST'), 'getcardpaymentlist' => array('POST'), 'getmyservicelist' => array('POST'), 'getoneserviceitem' => array('POST'), 'loadprofile' => array('POST'), 'deletecard' => array('POST') ); /* header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"); header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS"); //header("Access-Control-Expose-Headers: Access-Control-Allow-Origin"); header('Content-type: application/json'); if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) { die(); } */ header("Access-Control-Allow-Origin: *"); header("Access-Control-Expose-Headers: Access-Control-Allow-Origin"); header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"); header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS"); header('Content-type: application/json'); if ( "OPTIONS" === $_SERVER['REQUEST_METHOD'] ) { exit(); } $endpoint = strtolower(str_replace('/user/', '', strtok($_SERVER['REQUEST_URI'],'?'))); $id = 0; // update, get & delete actions require ID if (substr($endpoint,0,19)=='gettransportrequest' || substr($endpoint,0,13)=='updateprofile') { $endpoint = strtok($endpoint,'/'); $id = strtok('/'); } if (!isset($endpoints[$endpoint])) { header('HTTP/1.1 400 Bad Request'); header('Status: 400 Bad Request'); echo "{\"status\":\"Invalid endpoint url\"}"; exit(); } $methods = $endpoints[$endpoint]; if (array_search($_SERVER['REQUEST_METHOD'], $methods)===false) { header('HTTP/1.1 405 Method Not Allowed'); header('Status: 405 Method Not Allowed'); echo "{\"status\":\"Invalid request method\"}"; exit(); } include '../rest_api.php'; if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($endpoint == "uploadfile") { upload_file_call(); exit(); } else { $in = flatten(json_decode(file_get_contents('php://input'), true)); } } if ($_SERVER["REQUEST_METHOD"] == "PUT") { parse_str(file_get_contents('php://input'), $in); } $in["loc"] = $_SERVER["REMOTE_ADDR"]; // get who is connecting IP $in["pid"] = 100; switch ($endpoint) { case 'getdrycleanservicelist': $in["action"] = MOBIDELIV_USER_DRYCLIST; break; case 'createuser': $in["action"] = MOBIDELIV_USER_CREATE; $in["street1"] = $in["streetaddress"]; $in["zipcode"] = $in["zip"]; $in["country"] = "US"; $in["loc"] = $_SERVER["REMOTE_ADDR"]; break; case 'userlogin': $in["action"] = MOBIDELIV_USER_LOGIN; break; case 'updateprofile': $in["action"] = MOBIDELIV_USER_PROFILE; $in["street1"] = $in["streetaddress"]; $in["zipcode"] = $in["zip"]; $in["country"] = "US"; $in["loc"] = $_SERVER["REMOTE_ADDR"]; break; case 'updsprofile': $in["action"] = MOBIDELIV_USER_COMPLETEPROFILE; break; case 'getcardpaymentlist': $in["action"] = MOBIDELIV_USER_GETCCLIST; break; case 'newlundrypickup': $in["action"] = MOBIDELIV_USER_NEWLUNDRYPICK; $in["service_type"] = 1; $in["service_date"] = $in["pickupdate"] . " " . $in["pickuptime"]; break; case 'newdrycleanpickup': $in["action"] = MOBIDELIV_USER_NEWLUNDRYPICK; $in["service_type"] = 2; $in["service_date"] = $in["pickupdate"] . " " . $in["pickuptime"]; break; case 'confirmlundrypickup': $in["action"] = MOBIDELIV_USER_CONFIRMPICKUP; break; case 'savecardpayment': $in["action"] = MOBIDELIV_USER_SAVECARDPAYMENT; break; case 'getlundrylocation': $in["action"] = MOBIDELIV_USER_LUNDRYLOCATION; $in["limit"] = 100; break; case 'getmyservicelist': $in["action"] = MOBIDELIV_USER_GETSERVICELIST; break; case 'getoneserviceitem': $in["action"] = MOBIDELIV_USER_GETSERVICEITEM; break; case 'loadprofile': $in["action"] = MOBIDELIV_USER_PROFILE; break; case 'deletecard': $in["action"] = MOBIDELIV_USER_DELETECARD; break; } $in["pid"] = 100; //file_put_contents("in_debug.log", $in); // DEBUG $out = array(); external_internal_call($in, $out); header("HTTP/1.1 200 OK"); header("Status: 200 OK"); //$out = array_merge($in, $out); // DEBUG echo json_encode(processOutJson($in, $out)); exit(); function flatten($data, $parentkey="") { $result = array(); foreach ($data as $key=>$val) { if (is_array($val)) { $result = array_merge($result, flatten($val, $parentkey.$key."_")); } else { $result[$parentkey.$key] = $val; } } return $result; } function upload_file_call() { global $target_url; $data = $_POST; $url = $target_url."/../internal_upload.php"; $uploaddir = realpath('./') . '/files/'; $uploadfile = $uploaddir . basename($_FILES['file_contents']['name']); if (!move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) { $in["uploadfile"] = $uploadfile; header('HTTP/1.1 400 Bad Request'); header('Status: 400 Bad Request'); echo "{\"status\":\"Failed to upload file\"}"; exit(); } //----------------------------------------------------------- $file_name_with_full_path = realpath($uploadfile); /* curl will accept an array here too. * Many examples I found showed a url-encoded string instead. * Take note that the 'key' in the array will be the key that shows up in the * $_FILES array of the accept script. and the at sign '@' is required before the * file name. */ $data['file_contents'] = '@'.$file_name_with_full_path; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_POST,1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); $json_response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ( $status != 200 ) { header('HTTP/1.1 400 Bad Request'); header('Status: 400 Bad Request'); echo "{\"status\":\"Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)."\"}"; } curl_close($curl); unlink($file_name_with_full_path); //$response = json_decode($json_response, true); header("HTTP/1.1 200 OK"); header("Status: 200 OK"); echo $json_response; } // vi:ts=2