/**
  * Execute a request on this resource.
  * @param Request request
  * @return Response
  */
 function exec($request)
 {
     $response = new FormattedResponse($request);
     # good for debugging, remove this at some point
     $response->addHeader('X-Resource', get_class($this));
     $data = $request->parseData();
     $logger = null;
     if ($data !== null) {
         if (isset($data->username) && !empty($data->username) && isset($data->local_user) && !empty($data->local_user)) {
             $logger = new Logger($request->uri, $request->method, sprintf("%s -> %s", $data->local_user, $data->username));
         } else {
             if (isset($data->username) && !empty($data->username)) {
                 $logger = new Logger($request->uri, $request->method, sprintf("Anonymous -> %s", $data->username));
             } else {
                 $logger = new Logger($request->uri, $request->method, "Anonymous");
             }
         }
     } else {
         $logger = new Logger($request->uri, $request->method, "Anonymous");
     }
     if (!empty($data) && $data instanceof StdClass && isset($data->password)) {
         $data->password = "******";
     }
     $logger->setInput(json_encode($data));
     if (method_exists($this, $request->method)) {
         $parameters = $this->parameters;
         array_unshift($parameters, $request);
         try {
             $response = call_user_func_array(array($this, $request->method), $parameters);
         } catch (Exception $e) {
             $response->code = Response::INTERNALSERVERERROR;
             $response->error = $e;
             $response->error_detail = "INTERNAL_SERVER_ERROR";
             $logger->writeLog($response->error, $response->code);
             return $response;
         }
     } else {
         // send 405 method not allowed
         $response->code = Response::METHODNOTALLOWED;
         $response->error = sprintf('The HTTP method "%s" used for the request is not allowed for the resource "%s".', $request->method, $request->uri);
         $response->error_detail = "METHOD_NOT_ALLOWED";
         $logger->writeLog($response->error, $resonse->code);
         return $response;
     }
     $logger->setOutput(json_encode($response->body));
     if (!empty($response->error)) {
         $logger->writeLog($response->error, $response->code);
     } else {
         if (!empty($response->log_message)) {
             $logger->writeLog($response->log_message, $response->code);
         } else {
             $logger->writeLog("Action completed", $response->code);
         }
     }
     return $response;
 }
Exemple #2
0
 /**
  * Execute a request on this resource.
  * @param Request request
  * @return Response
  */
 function exec($request)
 {
     $response = new FormattedResponse($request);
     # good for debugging, remove this at some point
     $response->addHeader('X-Resource', get_class($this));
     $logger = new Logger($request->uri, $request->method, "Anonymous");
     $data = $request->parseData();
     if (!empty($data) && $data instanceof StdClass && isset($data->password)) {
         $data->password = "******";
     }
     $logger->setInput(json_encode($data));
     if (!isset($request->requestToken) || empty($request->requestToken)) {
         $response->code = Response::UNAUTHORIZED;
         $response->error = "Authorization required";
         $response->error_detail = "UNAUTHORIZED";
         $response->addHeader('X-Debug', "No token supplied");
         $logger->writeLog($response->error, $response->code);
         return $response;
     }
     $backend = null;
     try {
         switch (PowerDnsConfig::TOKEN_BACKEND) {
             case "PDO":
                 $backend = new PDOTokenBackend();
                 break;
             default:
                 $backend = new SqliteTokenBackend();
                 break;
         }
     } catch (Exception $e) {
         $response->code = Response::INTERNALSERVERERROR;
         $response->error = $e->getMessage();
         $response->error_detail = "INTERNAL_SERVER_ERROR";
         $logger->writeLog($response->error, $response->code);
         return $response;
     }
     $token = $backend->retrieveToken($request->requestToken);
     if ($token == null) {
         $response->code = Response::FORBIDDEN;
         $response->error = "Authentication failed";
         $response->error_detail = "AUTHENTICATION_FAILED";
         $response->addHeader('X-Debug', "Token is null");
         $logger->writeLog($response->error, $response->code);
         return $response;
     }
     $logger->setUser($token->username);
     if ($backend->validateToken($token) === false) {
         $response->code = Response::FORBIDDEN;
         $response->error = "Authentication failed";
         $response->error_detail = "AUTHENTICATION_FAILED";
         $response->addHeader('X-Debug', "Token is invalid");
         $logger->writeLog($response->error, $response->code);
         return $response;
     }
     $backend->refreshToken($token->hash);
     if (method_exists($this, $request->method)) {
         $parameters = $this->parameters;
         array_unshift($parameters, $request);
         try {
             $response = call_user_func_array(array($this, $request->method), $parameters);
         } catch (Exception $e) {
             $response->code = Response::INTERNALSERVERERROR;
             $response->error = $e;
             $response->error_detail = "INTERNAL_SERVER_ERROR";
             $logger->writeLog($response->error, $response->code);
             return $response;
         }
     } else {
         // send 405 method not allowed
         $response->code = Response::METHODNOTALLOWED;
         $response->error = sprintf('The HTTP method "%s" used for the request is not allowed for the resource "%s".', $request->method, $request->uri);
         $response->error_detail = "METHOD_NOT_ALLOWED";
         $logger->writeLog($response->error, $response->code);
         return $response;
     }
     $logger->setOutput(json_encode($response->body));
     if (!empty($response->error)) {
         $logger->writeLog($response->error, $response->code);
     } else {
         if (!empty($response->log_message)) {
             $logger->writeLog($response->log_message, $response->code);
         } else {
             $logger->writeLog("Action completed", $response->code);
         }
     }
     return $response;
 }