Beispiel #1
0
 public function execute()
 {
     $service = func_get_arg(0);
     $config = $this->api->getConfig();
     $logger = $this->api->getLogger();
     // Stop if blocks is not configured
     if (!isset($config['block'])) {
         return;
     }
     $blocked = explode(',', $config['block']);
     if (in_array($_SERVER['REMOTE_ADDR'], $blocked)) {
         $logger->log("Request blocked for this IP by BlockIp Hook.");
         $response = new Api_Response();
         $response->setCode(406);
         $response->addError("Stop it you Spammer!");
         $this->api->send($response);
     }
 }
Beispiel #2
0
 /**
  * Handle Request
  * 
  * @param Api_Request
  * @return void
  */
 public final function handle($request, $config = array())
 {
     // Set request object as local
     $this->request = $request;
     // Get params
     $params = $this->request->getParams();
     // Merge config arrays
     if (isset($config[$params['version']]) && is_array($config[$params['version']])) {
         $this->config = array_merge($this->config, $config[$params['version']]);
     } else {
         throw new Api_Error(Api_Error::VERSION_NOT_FOUND, $params['version']);
     }
     // Set response type
     $this->setType(ucfirst(strtolower($params['api_response_type'])));
     // Do config modify hooks
     $this->executeHooks(Api_Hook_IHook::HOOK_CONFIG_LOADED);
     // Prepare variables
     $service = $this->getServiceClassName($params['service']);
     $service_file_path = $this->getServicePath($service);
     $service_file_name = $this->getServiceFileName($params['service']);
     $service_file_fullpath = $service_file_path . "/" . $service_file_name;
     // Try to include
     if (file_exists($service_file_fullpath)) {
         require_once $service_file_fullpath;
     }
     // Check if the service is loaded
     if (!class_exists($service)) {
         throw new Api_Error(Api_Error::SERVICE_NOT_FOUND, $params['service']);
     }
     // Create service object
     $ServiceObject = new $service($this);
     // Do before hooks
     $this->executeHooks(Api_Hook_IHook::HOOK_BEFORE_SERVICE_EXECUTE, array($ServiceObject));
     // Set response data
     $response = new Api_Response();
     if (isset($params['method']) && !empty($params['method'])) {
         $method = $this->prepareMethodName($params['method']);
         if (is_callable(array($ServiceObject, $method))) {
             $response->setData($ServiceObject->{$method}($params, $this->config));
         } else {
             throw new Api_Error(Api_Error::ACTION_NOT_FOUND, array($method, $params['service']));
         }
     } else {
         $response->setData($ServiceObject->execute($params, $this->config));
     }
     $response->addError($ServiceObject->errors);
     $response->setCode($ServiceObject->code);
     $this->send($response);
 }