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']; }
protected function db() { if ($this->database === null) { $host = Config::get('database.host'); $port = Config::get('database.port'); $username = Config::get('database.username'); $password = Config::get('database.password'); $base = Config::get('database.base'); try { $this->database = new \PDO("mysql:host={$host}:{$port};dbname={$base}", $username, $password); } catch (\Exception $e) { $this->database = false; Logger::addError('The API could not connect to the database, check the config.'); } } return $this->database; }
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(); }
private static function run($config, $env) { $envs = array("prod", "dev"); if (!in_array($env, $envs)) { $env = "dev"; } self::$env = $env; Config::set($config); if (Config::get('security.https', false) === true) { if (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] != "on") { throw new ForbiddenAccessException('SSL is mandatory.'); } } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $IP = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $IP = $_SERVER['HTTP_CLIENT_IP']; } else { $IP = $_SERVER['REMOTE_ADDR']; } if (is_array(Config::get('security.ip'))) { if (!in_array($IP, Config::get('security.ip'))) { throw new ForbiddenAccessException(); } } elseif (Config::get('security.ip') != $IP) { throw new ForbiddenAccessException(); } if (!file_exists(__DIR__ . "/../../cache/routes.php") || self::$env == 'dev') { $pluginsDir = opendir(__DIR__ . "/Plugin"); $out = "<?php\n"; while ($entry = @readdir($pluginsDir)) { if (!is_dir($entry)) { $className = preg_replace("#\\.php#", '', $entry); $reflection = new \ReflectionClass('MineREST\\Plugin\\' . $className); foreach ($reflection->getMethods() as $method) { if ($method->class == 'MineREST\\Plugin\\' . $className && !preg_match('#__#', $method->name)) { $ref = new ReflectionAnnotatedMethod('MineREST\\Plugin\\' . $className, $method->name); $params = $ref->getAnnotation('Params'); if ($params !== false) { $params = $params->value; for ($i = 0; $i < count($params); $i++) { $params[$i] = "'" . $params[$i] . "'"; } $params = implode(',', $params); $out .= "\$routes['/{$className}" . $ref->getAnnotation('Route')->value . "']['" . strtoupper($ref->getAnnotation('Method')->value) . "'] = array('MineREST\\Plugin\\" . $className . '\', \'' . $method->name . "', array(" . $params . "));\n"; } else { $out .= "\$routes['/{$className}" . $ref->getAnnotation('Route')->value . "']['" . strtoupper($ref->getAnnotation('Method')->value) . "'] = array('MineREST\\Plugin\\" . $className . '\', \'' . $method->name . "');\n"; } } } } } $out .= "return \$routes;"; closedir($pluginsDir); @mkdir(__DIR__ . "/../../cache/"); if (file_put_contents(__DIR__ . "/../../cache/routes.php", $out) === false) { throw new KernelException("Unable to write in cache directory."); } } return Router::run(); }