public static function resolve($params = array())
 {
     // Set error handler
     @set_error_handler('Ai1wm_Handler::error');
     // Set params
     if (empty($params)) {
         $params = ai1wm_urldecode($_REQUEST);
     }
     // Set secret key
     $secret_key = null;
     if (isset($params['secret_key'])) {
         $secret_key = $params['secret_key'];
     }
     // Verify secret key by using the value in the database, not in cache
     if ($secret_key !== get_option(AI1WM_SECRET_KEY)) {
         Ai1wm_Status::error(sprintf(__('Unable to authenticate your request with secret_key = "%s"', AI1WM_PLUGIN_NAME), $secret_key), __('Unable to resolve', AI1WM_PLUGIN_NAME));
         exit;
     }
     // Set IP address
     if (isset($params['url_ip']) && ($ip = $params['url_ip'])) {
         update_option(AI1WM_URL_IP, $ip);
     }
     // Set adapter
     if (isset($params['url_adapter']) && ($adapter = $params['url_adapter'])) {
         if ($adapter === 'curl') {
             update_option(AI1WM_URL_ADAPTER, 'curl');
         } else {
             update_option(AI1WM_URL_ADAPTER, 'stream');
         }
     }
 }
 public static function resolve($args = array())
 {
     // Set error handler
     @set_error_handler('Ai1wm_Log::error_handler');
     // Set arguments
     if (empty($args)) {
         $args = ai1wm_urldecode($_REQUEST);
     }
     // Set secret key
     $secret_key = null;
     if (isset($args['secret_key'])) {
         $secret_key = $args['secret_key'];
     }
     // Verify secret key by using the value in the database, not in cache
     if ($secret_key !== get_site_option(AI1WM_SECRET_KEY, false, false)) {
         Ai1wm_Status::set(array('type' => 'error', 'title' => __("Unable to resolve", AI1WM_PLUGIN_NAME), 'message' => __("Unable to authenticate your request with secret_key = \"{$secret_key}\"", AI1WM_PLUGIN_NAME)));
         exit;
     }
     // Set IP address
     if (isset($args['url_ip']) && ($ip = $args['url_ip'])) {
         update_site_option(AI1WM_URL_IP, $ip);
     }
     // Set transport layer
     if (isset($args['url_transport']) && ($transport = $args['url_transport'])) {
         if ($transport === 'curl') {
             update_site_option(AI1WM_URL_TRANSPORT, array('curl', 'ai1wm'));
         } else {
             update_site_option(AI1WM_URL_TRANSPORT, array('ai1wm', 'curl'));
         }
     }
 }
 public static function import($params = array())
 {
     global $wp_filter;
     // Set error handler
     @set_error_handler('Ai1wm_Handler::error');
     // Set params
     if (empty($params)) {
         $params = ai1wm_urldecode($_REQUEST);
     }
     // Set priority
     $priority = 10;
     if (isset($params['priority'])) {
         $priority = (int) $params['priority'];
     }
     // Set secret key
     $secret_key = null;
     if (isset($params['secret_key'])) {
         $secret_key = $params['secret_key'];
     }
     // Verify secret key by using the value in the database, not in cache
     if ($secret_key !== get_option(AI1WM_SECRET_KEY)) {
         Ai1wm_Status::error(sprintf(__('Unable to authenticate your request with secret_key = "%s"', AI1WM_PLUGIN_NAME), $secret_key), __('Unable to import', AI1WM_PLUGIN_NAME));
         exit;
     }
     // Get hook
     if (isset($wp_filter['ai1wm_import']) && ($filters = $wp_filter['ai1wm_import']) && ksort($filters)) {
         while ($hooks = current($filters)) {
             if ($priority == key($filters)) {
                 foreach ($hooks as $hook) {
                     try {
                         $params = call_user_func_array($hook['function'], array($params));
                     } catch (Ai1wm_Import_Retry_Exception $exception) {
                         status_header($exception->getCode());
                         echo json_encode(array('message' => $exception->getMessage()));
                         exit;
                     } catch (Exception $e) {
                         Ai1wm_Status::error($e->getMessage(), __('Unable to import', AI1WM_PLUGIN_NAME));
                         exit;
                     }
                 }
                 // Set completed
                 $completed = true;
                 if (isset($params['completed'])) {
                     $completed = (bool) $params['completed'];
                 }
                 // Log request
                 if (empty($params['priority']) || is_file(ai1wm_import_path($params))) {
                     Ai1wm_Log::import($params);
                 }
                 // Do request
                 if ($completed === false || ($next = next($filters)) && ($params['priority'] = key($filters))) {
                     return Ai1wm_Http::get(admin_url('admin-ajax.php?action=ai1wm_import'), $params);
                 }
             }
             next($filters);
         }
     }
 }
 public static function import($args = array())
 {
     // Set error handler
     @set_error_handler('Ai1wm_Log::error_handler');
     try {
         // Set arguments
         if (empty($args)) {
             $args = ai1wm_urldecode($_REQUEST);
         }
         // Set storage path
         if (empty($args['storage'])) {
             $args['storage'] = uniqid();
         }
         // Set secret key
         $secret_key = null;
         if (isset($args['secret_key'])) {
             $secret_key = $args['secret_key'];
         }
         // Verify secret key by using the value in the database, not in cache
         if ($secret_key !== get_site_option(AI1WM_SECRET_KEY, false, false)) {
             throw new Ai1wm_Import_Exception(sprintf(__('Unable to authenticate your request with secret_key = <strong>"%s"</strong>', AI1WM_PLUGIN_NAME), $secret_key));
         }
         // Set provider
         $provider = null;
         if (isset($args['provider'])) {
             $provider = $args['provider'];
         }
         $class = "Ai1wm_Import_{$provider}";
         if (!class_exists($class)) {
             throw new Ai1wm_Import_Exception(sprintf(__('Unknown provider: <strong>"%s"</strong>', AI1WM_PLUGIN_NAME), $class));
         }
         // Set method
         $method = null;
         if (isset($args['method'])) {
             $method = $args['method'];
         }
         // Initialize provider
         $provider = new $class($args);
         if (!method_exists($provider, $method)) {
             throw new Ai1wm_Import_Exception(sprintf(__('Unknown method: <strong>"%s"</strong>', AI1WM_PLUGIN_NAME), $method));
         }
         // Invoke method
         echo json_encode($provider->{$method}());
         exit;
     } catch (Exception $e) {
         // Log the error
         Ai1wm_Log::error('Exception while importing: ' . $e->getMessage());
         // Set the status to failed
         Ai1wm_Status::set(array('type' => 'error', 'title' => __('Unable to import', AI1WM_PLUGIN_NAME), 'message' => $e->getMessage()));
         // End the process
         wp_die('Exception while importing: ' . $e->getMessage());
     }
 }