/**
  * Process requested endpoint 
  * @return mixed 	Endpoint result 
  */
 public function endpoint_process()
 {
     $og_endpoint = $this->server->original_endpoint;
     // Check if endpoint exists in dictionary
     $og_exists = Dictionary::exists($og_endpoint);
     if ($og_exists) {
         try {
             // Get endpoint query
             $query = Dictionary::get_query($og_exists);
             // Before callback function call
             if ($before = Dictionary::get_before($og_exists)) {
                 if (is_callable($before)) {
                     $query['q']['q'] = call_user_func($before, $query['q']['q']);
                 }
             }
             // Check is endpoint is cacheable
             $cacheable = Dictionary::is_cacheable($og_exists);
             if (!$query) {
                 throw new APIexception('Endpoint not found', 6, 404);
             } elseif ($query['method'] !== $this->server->method) {
                 throw new APIexception('Method mismatch. You should use ' . $query['method'], 11, 400);
             }
             // Request security for endpoints with security enabled
             if ($query['signed']) {
                 if (SECURE_TYPE === "oauth1.0a") {
                     if (OAuth1\OAuthRequestVerifier::requestIsSigned()) {
                         try {
                             $req = new OAuth1\OAuthRequestVerifier();
                             if (!$req->verify()) {
                                 throw new APIexception('Unauthorized request.', 15, 401);
                             }
                         } catch (OAuth1\OAuthException2 $e) {
                             throw new APIexception('OAuth error: ' . $e->getMessage(), 15, 401);
                         }
                     } else {
                         throw new APIexception('Unauthorized request.', 15, 401);
                     }
                 }
             }
             // Retrieves endpoint cache if cache enabled
             if (CACHE && $cacheable) {
                 $cached_content = Cache::search($this->server);
                 if ($cached_content) {
                     // After callback function call
                     if ($after = Dictionary::get_after($og_exists)) {
                         if (is_callable($after)) {
                             $cached_content = call_user_func($after, $cached_content);
                         }
                     }
                     return Output::encode(json_decode($cached_content), $this->server->output, TRUE);
                 }
             }
             // If no cached content, create file if cache enabled
             if (empty($cached_content)) {
                 $data = $this->server->data;
                 $filters = $this->server->args;
                 $res = Query::execute($query["q"], TRUE, $data, $filters);
                 if ($query['q']['join']) {
                     foreach ($query['q']['join'] as $item => $join_query) {
                         foreach ($res as $num => $response) {
                             $key = Dictionary::get_col_prefix($og_exists) . $item;
                             if (array_key_exists($key, $response)) {
                                 $value = sprintf($join_query, $response[$key]);
                                 if ($joinq = Query::execute(array("q" => $value), TRUE)) {
                                     if (count($joinq) > 1) {
                                         $res[$num][$key] = $joinq;
                                     } else {
                                         $res[$num][$key] = $joinq[0];
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (CACHE && $cacheable) {
                     $res = Cache::write($res);
                 }
                 // After callback function call
                 if ($after = Dictionary::get_after($og_exists)) {
                     if (is_callable($after)) {
                         $res = call_user_func($after, $res);
                     }
                 }
                 return Output::encode($res, $this->server->output);
             }
         } catch (APIexception $e) {
             die($e->output());
         }
     }
 }
示例#2
0
 /**
  * Creates endpoint
  * @param  array $endpoint  Endpoint to create
  */
 private function create_endpoint($endpoint)
 {
     // Test if method set
     if (!isset($endpoint['method'])) {
         throw new APIexception("Unexpected Header", 2, 400);
     }
     $ep = explode("/", $endpoint['endpoint']);
     // Test if name set
     if (!isset($ep[0])) {
         throw new APIexception("No endpoint", 1, 404);
     }
     // Test if verb
     if (isset($ep[1])) {
         if (preg_match('/^\\:(\\w+)/', $ep[1])) {
             $verb = NULL;
         } else {
             $verb = $ep[1];
         }
     } else {
         $verb = NULL;
     }
     // Test for filters in endpoint name
     foreach ($ep as $filter) {
         if (preg_match('/^\\:(\\w+)/', $filter, $result)) {
             $endpoint['params']['filters'][] = $result[1];
         }
     }
     // Sets endpoint name
     $ep = $ep[0];
     // Test for custom query
     if (isset($endpoint['params']['query'])) {
         $query = $endpoint['params']['query'];
         $action = strtolower(preg_replace("/^(\\w+) .*/", "\$1", $query));
     } else {
         // Check if columns from table have prefix
         $this->col_prefix = $endpoint['params']['col_prefix'] = isset($endpoint['params']['col_prefix']) ? $endpoint['params']['col_prefix'] : Dictionary::get_col_prefix($ep);
         // Creates query
         $q = new Query($endpoint['method'], $ep, $verb, $endpoint['params']);
         $query = $q->get_query();
         $action = $q->get_action();
     }
     $join_queries = array();
     if (isset($endpoint['params']['join'])) {
         foreach ($endpoint['params']['join'] as $k => $v) {
             $cols = explode("|", $v);
             $params = array();
             if (isset($cols[2])) {
                 $params['show'] = array_map(function ($val) {
                     if ($val !== "id" && $val !== "updated") {
                         return $this->col_prefix . $val;
                     }
                     return $val;
                 }, explode(",", $cols[2]));
             }
             $params['filters'][] = $cols[1];
             $join_queries[$cols[0]] = Query::construct_query("select", $k, $params);
         }
     }
     // Sets endpoint object
     $endpoint['query'] = array("q" => $query, "action" => $action, "columns" => !empty($endpoint['params']['columns']) ? $endpoint['params']['columns'] : "", "filters" => !empty($endpoint['params']['filters']) ? $endpoint['params']['filters'] : "", "limiter" => !empty($endpoint['params']['limit']) ? $endpoint['params']['limit'] : "", "join" => $join_queries);
     $endpoint['params']['cacheable'] = isset($endpoint['params']['cacheable']) ? $endpoint['params']['cacheable'] : FALSE;
     // Registers endpoint on dictionary
     Dictionary::register($endpoint);
 }