Пример #1
0
 private static function exit_handle_request($app_id_or_slug, $service_slug, $action, $id = 0)
 {
     global $wp_query;
     self::log($_SERVER['REQUEST_METHOD'] . ' : ' . $action . ' : ' . print_r($_REQUEST, true));
     //Set AJAX WP context :
     define('DOING_AJAX', true);
     if (self::cache_on()) {
         //TODO_WPAK
         /* $cached_webservice = WpakCache::get_cached_web_service(
         	  self::get_web_service_cache_id($service),
         	  isset($_GET['force_reload']) && is_numeric($_GET['force_reload']) && $_GET['force_reload'] == 1,
         	  isset($_GET['last_update']) && is_numeric($_GET['last_update']) ? $_GET['last_update'] : 0
         	  );
         	  if( !empty($cached_webservice) ){
         	  self::exit_sending_web_service_content($cached_webservice);
         	  } */
     }
     $app = WpakApps::get_app($app_id_or_slug);
     //Check that the asked app exists :
     if (empty($app)) {
         header("HTTP/1.0 404 Not Found");
         _e('App not found', WpAppKit::i18n_domain) . ' : [' . $app_id_or_slug . ']';
         exit;
     }
     $app_id = $app->ID;
     $app_slug = $app->post_name;
     WpakWebServiceContext::$current_app_id = $app_id;
     WpakWebServiceContext::$current_app_slug = $app_slug;
     //Some browsers or viewports on mobile devices cache HTTP resquests, we don't want this!
     header("Cache-Control: no-cache, must-revalidate");
     // HTTP/1.1
     header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
     // Some time in the past
     if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
         header('Allow: GET, PUT, DELETE, POST');
         header('Access-Control-Allow-Origin: *');
         header('Access-Control-Allow-Methods: GET, PUT, DELETE, POST');
         header('Access-Control-Allow-Headers: origin, content-type, accept, x-http-method-override');
         header('Access-Control-Allow-Credentials: true');
         exit;
     }
     //If the app current theme has some PHP (hooks!) to be executed before the web
     //service process, include it here :
     WpakThemes::include_app_theme_php($app_id);
     //Include PHP files required by addons activated for this app :
     WpakAddons::require_app_addons_php_files($app_id);
     $service_answer = null;
     switch ($action) {
         case 'list':
             if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                 $headers = function_exists('apache_request_headers') ? apache_request_headers() : array();
                 $is_url_encoded = !empty($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/x-www-form-urlencoded') !== false || !empty($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false;
                 if ($is_url_encoded) {
                     if (isset($_POST['model'])) {
                         //Specific to backbone's "emulateJSON"
                         $json = stripslashes($_POST['model']);
                         $sent = json_decode($json);
                     } else {
                         $sent = $_POST;
                     }
                 } else {
                     $json = file_get_contents("php://input");
                     $sent = json_decode($json);
                 }
                 $service_answer = WpakWebServiceCrud::create($app_id, $service_slug, $sent);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
                 $service_answer = WpakWebServiceCrud::read($app_id, $service_slug, $wp_query->query_vars);
             }
             break;
         case 'one':
             if ($_SERVER['REQUEST_METHOD'] == 'GET') {
                 $service_answer = WpakWebServiceCrud::read_one($app_id, $service_slug, $id);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'PUT') {
                 $json = file_get_contents("php://input");
                 $new = json_decode($json);
                 $service_answer = WpakWebServiceCrud::update($app_id, $service_slug, $new);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
                 $service_answer = WpakWebServiceCrud::delete($app_id, $service_slug, $id);
             } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
                 $http_method_override_method = '';
                 $headers = function_exists('apache_request_headers') ? apache_request_headers() : array();
                 if (!empty($headers['X-HTTP-Method-Override'])) {
                     $http_method_override_method = $headers['X-HTTP-Method-Override'];
                 } elseif (!empty($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
                     $http_method_override_method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
                 }
                 $is_url_encoded = !empty($headers['Content-Type']) && strpos($headers['Content-Type'], 'application/x-www-form-urlencoded') !== false || !empty($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false;
                 //self::log('$_SERVER : '. print_r($_SERVER,true));
                 self::log('X-HTTP-Method-Override : ' . $http_method_override_method);
                 if (!empty($http_method_override_method)) {
                     if ($http_method_override_method == 'PUT') {
                         if ($is_url_encoded) {
                             if (isset($_POST['model'])) {
                                 //Specific to backbone's "emulateJSON"
                                 $json = stripslashes($_POST['model']);
                                 $sent = json_decode($json);
                             } else {
                                 $sent = $_POST;
                             }
                             self::log('PUT one (X-HTTP-Method-Override + emulateJSON) : ' . $id . ' - json :' . $json . ' - _POST : ' . print_r($_POST, true));
                         } else {
                             $data = file_get_contents("php://input");
                             $new = json_decode($data);
                             self::log('PUT one (X-HTTP-Method-Override) : ' . $id . ' : ' . $data);
                         }
                         if ($new !== null) {
                             $service_answer = WpakWebServiceCrud::update($app_id, $service_slug, $new);
                         }
                     } elseif ($http_method_override_method == 'DELETE') {
                         self::log('DELETE one (X-HTTP-Method-Override) : ' . $id);
                         $service_answer = WpakWebServiceCrud::delete($app_id, $service_slug, $id);
                     }
                 }
             }
             break;
     }
     //Simulate delay : TODO : make this configurable in WP BO :
     //time_nanosleep(rand(0,1), (floatval(rand(20,100))/100) * 1000000000);
     //sleep(2);
     if ($service_answer !== null) {
         self::exit_sending_answer($service_answer, $app_id, $service_slug);
     }
     exit(__('Error : Web service not recognised', WpAppKit::i18n_domain));
 }