function __construct() { parent::__construct(); $this->load->database(); $this->load->library('datamapper'); if (!$this->db->conn_id) { $this->error(500, 'Database connection failed. Make sure the database server is running and the information in storage / configuration / database.php is still correct.', true); } if (strlen($this->config->item('encryption_key')) !== 32) { $key = md5($_SERVER['HTTP_HOST'] . uniqid('', true)); $this->config->set_item('encryption_key', $key); Shutter::write_encryption_key($key); } if (isset($_SERVER['HTTP_X_KOKEN_AUTH']) && $_SERVER['HTTP_X_KOKEN_AUTH'] === 'cookie') { $this->load->library('session'); } $this->check_for_rewrite(); $uri_parts = $this->uri->ruri_to_assoc(1); $action = array_shift($uri_parts); $this->cache_path = 'api' . $this->uri->uri_string(); if ($this->input->is_cli_request()) { $this->method = 'get'; } else { $this->method = strtolower($_SERVER['REQUEST_METHOD']); } if ($this->auto_authenticate && is_array($this->auto_authenticate)) { if (array_key_exists('exclude', $this->auto_authenticate)) { if (in_array($action, $this->auto_authenticate['exclude'])) { $this->auto_authenticate = false; } } } if ($this->auto_authenticate) { $auth = $this->authenticate(); if ($auth) { $this->auth = true; list($this->auth_user_id, $this->auth_token, $this->auth_role) = $auth; if (strpos($this->cache_path, '/token:') === false) { $this->cache_path .= '/token:' . $this->auth_token; } } } $this->caching = !array_key_exists('cache:false', $uri_parts) && ($this->caching === true || is_array($this->caching) && in_array($action, $this->caching)); $content_type = 'application/json'; $user_setup = FCPATH . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'configuration' . DIRECTORY_SEPARATOR . 'user_setup.php'; if ($this->uri->uri_string() === '/system' && file_exists($user_setup)) { $this->cache_path .= '/' . filemtime($user_setup); } else { if (preg_match('~/(js|css)$~', $this->uri->uri_string(), $content_type_match)) { if ($content_type_match[1] === 'js') { $content_type = 'text/javascript'; } else { $content_type = 'text/css'; } } } if ($this->method === 'get' && $this->caching) { $cache = Shutter::get_cache($this->cache_path, getenv('HTTP_IF_MODIFIED_SINCE')); if ($cache !== false) { if ($cache['status'] === 304) { set_status_header('304'); exit; } if ($content_type !== 'application/json' || !empty($cache['data']) && json_decode($cache['data'])) { header('Content-type: ' . $content_type); header('Cache-control: must-revalidate'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $cache['modified']) . ' GMT'); header('X-Koken-Cache: hit'); die($cache['data']); } } } else { if ($this->method !== 'get') { if ($this->auto_authenticate && (!$this->auth || $this->auth_role == 'read')) { $this->error('401', 'Not authorized to perform this action.', true); } if ($this->purges_cache && ENVIRONMENT === 'production') { Shutter::clear_cache(array('api', 'site')); } if (isset($_POST) && isset($_POST['_method'])) { $this->method = strtolower($_POST['_method']); if (isset($_POST['model'])) { $_POST = json_decode($_POST['model']); } } } } // Force MySQL to UTC $this->db->simple_query("SET time_zone = '+00:00'"); }