Esempio n. 1
0
 public function send()
 {
     if ($this->data['status'] == self::FORBIDDEN) {
         header("HTTP/1.0 403 Forbidden");
         exit('Forbidden');
     }
     if (Kernel::env() == 'dev') {
         header('Access-Control-Allow-Origin: *');
     } else {
         header('Access-Control-Allow-Origin: ' . Config::get('security.domain'));
     }
     header('Cache-Control: no-cache, must-revalidate');
     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
     if ($this->html === false) {
         header('Content-type: application/json');
         $json = json_encode($this->data);
         if (Kernel::env() == 'dev') {
             echo $this->json_format($json);
         } else {
             echo $json;
         }
         return;
     }
     echo $this->data['data'];
 }
Esempio n. 2
0
 private function __construct()
 {
     if (!is_dir(__DIR__ . '/../../logs')) {
         if (!mkdir(__DIR__ . '/../../logs')) {
             throw new LoggerException('The log directory is not writable.');
         }
     }
     $this->log = new MonoLogger('MineREST');
     $this->log->pushHandler(new StreamHandler(__DIR__ . '/../../logs/' . strtolower(Kernel::env()) . '.log'));
 }
Esempio n. 3
0
 public static function run()
 {
     // first we get the requested url and method
     $requestMethod = strtolower($_SERVER['REQUEST_METHOD']);
     $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
     // we need the cached routes
     if (!file_exists(__DIR__ . '/../../../cache/routes.php')) {
         throw new RouterException();
     }
     $routes = (require __DIR__ . '/../../../cache/routes.php');
     $plugin = null;
     $data = array();
     foreach ($routes as $url => $route) {
         if (preg_match('#' . $url . '#', $requestUrl, $matches) && isset($route[strtoupper($requestMethod)])) {
             if (count($matches) > 1) {
                 for ($i = 0; $i < count($route[strtoupper($requestMethod)][2]); $i++) {
                     $data[$route[strtoupper($requestMethod)][2][$i]] = $matches[$i + 1];
                 }
             }
             $plugin = new $route[strtoupper($requestMethod)][0]();
             $plugin->setRequestMethod($requestMethod, $data);
             $plugin->setScript(Config::get('server.script', '/etc/init.d/minecraft'));
             $method = $route[strtoupper($requestMethod)][1];
             break;
         }
     }
     // error 404
     if ($plugin == null) {
         throw new RouterException("Error 404");
     }
     $response = $plugin->{$method}();
     if (!$response instanceof Response && Kernel::env() == 'prod') {
         $response = new Response(Response::ERROR);
     }
     $response->send();
 }