Esempio n. 1
0
 /**
  * checks whether an api function is bound to the current requests. If so, so the api function will be executed.
  */
 public static function handleCall()
 {
     if (static::hasFactoryClass()) {
         return static::callFactoryClass(__FUNCTION__, func_get_args());
     }
     $apiFunc = self::factory();
     if ($apiFunc != null) {
         if ($apiFunc->published !== true) {
             if (rex::isBackend() !== true) {
                 throw new rex_http_exception(new rex_api_exception('the api function ' . get_class($apiFunc) . ' is not published, therefore can only be called from the backend!'), rex_response::HTTP_FORBIDDEN);
             }
             if (!rex::getUser()) {
                 throw new rex_http_exception(new rex_api_exception('missing backend session to call api function ' . get_class($apiFunc) . '!'), rex_response::HTTP_UNAUTHORIZED);
             }
         }
         $urlResult = rex_get(self::REQ_RESULT_PARAM, 'string');
         if ($urlResult) {
             // take over result from url and do not execute the apiFunc
             $result = rex_api_result::fromJSON($urlResult);
             $apiFunc->result = $result;
         } else {
             try {
                 $result = $apiFunc->execute();
                 if (!$result instanceof rex_api_result) {
                     throw new rex_exception('Illegal result returned from api-function ' . rex_get(self::REQ_CALL_PARAM) . '. Expected a instance of rex_api_result but got "' . (is_object($result) ? get_class($result) : gettype($result)) . '".');
                 }
                 $apiFunc->result = $result;
                 if ($result->requiresReboot()) {
                     $context = rex_context::fromGet();
                     // add api call result to url
                     $context->setParam(self::REQ_RESULT_PARAM, $result->toJSON());
                     // and redirect to SELF for reboot
                     rex_response::sendRedirect($context->getUrl([], false));
                 }
             } catch (rex_api_exception $e) {
                 $message = $e->getMessage();
                 $result = new rex_api_result(false, $message);
                 $apiFunc->result = $result;
             }
         }
     }
 }