/** * Construct the URL Writer helper * * Extract string from server configuration */ private function __construct() { // Set server address $this->server_app_root = Request::server()['SERVER_NAME']; $ar_domain = explode('.', Request::server()['SERVER_NAME']); if (count($ar_domain) >= 3) { $start = strlen($ar_domain[0]) + 1; $this->server_main_root = substr(Request::server()['SERVER_NAME'], $start); } else { $this->server_main_root = Request::server()['SERVER_NAME']; } if (!Request::is_https() && Request::get_request_port() != 80 || Request::is_https() && Request::get_request_port() != 443) { $this->server_app_root .= ":" . Request::get_request_port(); $this->server_main_root .= ":" . Request::get_request_port(); } if (isset(Request::request()['request'])) { $ar_path = explode('/', Request::request()['request']); array_shift($ar_path); $this->session_path = implode('/', $ar_path); } else { $this->session_path = ''; } $webroot = Application::get_instance()->get_webroot(); if (!is_null($webroot) && !empty($webroot)) { $this->server_app_root .= "/" . $webroot; $this->server_main_root .= "/" . $webroot; } }
/** * HTTP GET action * * @see MVC\APIActionsInterface * @return MVC\JSONView */ public function get($params) { $version = Application::get_instance()->get_version(); $array['application'] = array('version' => $version->application()); $array['apine_framework'] = array('version' => $version->framework()); $this->_view->set_json_file($array); $this->_view->set_response_code(200); return $this->_view; }
/** * Construct the session handler * Fetch data from request headers and authenticate the user */ public function __construct() { $config = Application::get_instance()->get_config(); if (!is_null($config->get('runtime', 'token_lifespan'))) { $this->token_lifespan = (int) $config->get('runtime', 'token_lifespan'); } $request = Request::get_instance(); if (isset($request->get_request_headers()['Authorization'])) { $authorization_string = $request->get_request_headers()['Authorization']; $authorization_array = explode(':', $authorization_string); $name = $authorization_array[0]; $token = $authorization_array[1]; $referer = isset($request->server()['REMOTE_ADDR']) ? $request->server()['REMOTE_ADDR'] : ''; $agent = isset($request->server()['HTTP_USER_AGENT']) ? $request->server()['HTTP_USER_AGENT'] : ''; $token_id = Apine\User\Factory\UserTokenFactory::authentication($name, $token, $this->token_lifespan); $token = Apine\User\Factory\UserTokenFactory::create_by_id($token_id); if ($token_id && $token->get_origin() == $referer . $agent) { $this->logged_in = true; $this->token = $token; $this->session_type = $this->token->get_user()->get_type(); $this->token->set_last_access_date(date('d M Y H:i:s', time() + $this->token_lifespan)); $this->token->save(); } } else { if (isset($_COOKIE['apine_session'])) { $session = new WebSession(); $data = $session->data(); if ($data != null) { $user_id = $data->get_var('apine_user_id'); if ($user_id != null) { $user = UserFactory::create_by_id($user_id); $token = new UserToken(); $token->set_user($user); $this->logged_in = true; $this->token = $token; $this->session_type = $data->get_var('apine_user_type'); $this->token->set_last_access_date(date('d M Y H:i:s', time() + $this->token_lifespan)); } } } } }
/** * Database class' constructor * * @param string $db_type * @param string $db_host * @param string $db_name * @param string $db_user * @param string $db_password * @param string $db_charset * @throws DatabaseException If cannot connect to database server */ public function __construct($db_type = null, $db_host = null, $db_name = null, $db_user = '******', $db_password = '', $db_charset = 'utf8') { try { if (!is_null($db_type) && !is_null($db_host) && !is_null($db_name) || !isset(self::$apine_instance)) { $config = Application::get_instance()->get_config(); $db_port = '3306'; if (!(!is_null($db_type) && !is_null($db_host) && !is_null($db_name))) { $db_host = $config->get('database', 'host'); } // Split Host string to extract the port $port_pos = strrpos($db_host, ':'); if ($port_pos) { $str_port = substr($db_host, $port_pos + 1); if (is_numeric($str_port)) { $db_port = (int) $str_port; } } if (!is_null($db_type) && !is_null($db_host) && !is_null($db_name)) { $db_dns = $db_type . ':host=' . $db_host . ';dbname=' . $db_name . ';port=' . $db_port . ';charset=' . $db_charset; } else { $db_dns = $config->get('database', 'type') . ':host=' . $db_host . ';dbname=' . $config->get('database', 'dbname') . ';port=' . $db_port . ';charset=' . $config->get('database', 'charset'); $db_user = $config->get('database', 'username'); $db_password = $config->get('database', 'password'); } $this->instance = new \PDO($db_dns, $db_user, $db_password); $this->instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->instance->exec('SET time_zone = "+00:00";'); if (!(!is_null($db_type) && !is_null($db_host) && !is_null($db_name)) && !isset(self::$apine_instance)) { self::$apine_instance = $this->instance; } } else { $this->instance =& self::$apine_instance; } } catch (\PDOException $e) { throw new DatabaseException($e->getMessage(), $e->getCode(), $e); } }
/** * Write the default routes file * * @throws GenericException */ private function import_routes() { try { if (Application::get_instance()->get_routes_type() == APINE_ROUTES_XML) { if (file_exists($this->project . '/routes.xml')) { return; } $routes = fopen($this->project . '/routes.xml', 'x+'); $path = $this->project . '/routes.xml'; $content = file_get_contents($this->parent . '/Installation/routes.xml'); } else { if (file_exists($this->project . '/routes.json')) { return; } $routes = fopen($this->project . '/routes.json', 'x+'); $path = $this->project . '/routes.json'; $content = file_get_contents($this->parent . '/Installation/routes.json'); } $result = fwrite($routes, $content); fclose($routes); if ($result === false) { throw new Exception('Cannot write routes file'); } chmod($path, 0777); } catch (\Exception $e) { throw new GenericException($e->getMessage(), $e->getCode(), $e); } }
/** * Return the content of the view * * @return string */ public function content() { $config = Application\Application::get_instance()->get_config(); if (!is_null($config)) { if (SessionManager::is_logged_in()) { $user_array = array(); $apine_user = SessionManager::get_user(); $user_array['id'] = $apine_user->get_id(); $user_array['username'] = $apine_user->get_username(); $user_array['password'] = $apine_user->get_password(); $user_array['type'] = $apine_user->get_type(); $user_array['email'] = $apine_user->get_email_address(); $user_array['register_date'] = $apine_user->get_register_date(); $user_array['groups'] = array(); $properties = $apine_user->get_property_all(); if (is_array($properties)) { foreach ($properties as $name => $value) { $user_array["property_" . $name] = $value->get_value(); } } foreach ($apine_user->get_group() as $group) { $user_array['group_' . $group->get_id()] = true; $user_array['groups'][$group->get_id()] = $group->get_id(); } } else { $user_array = false; } } else { $user_array = false; } Engine::instance()->add_data(array('apine_user' => $user_array, 'apine_application_https' => Application\Application::get_instance()->get_use_https(), 'apine_application_mode' => Application\Application::get_instance()->get_mode(), 'apine_application_secure' => Application\Application::get_instance()->get_secure_session(), 'apine_view_metatags' => $this->_metatags, 'apine_view_scripts' => $this->_scripts, 'apine_view_stylesheets' => $this->_styles, "apine_view_title" => $this->_title)); Engine::instance()->add_data($this->_params->get_all()); $this->content = Engine::instance()->process($this->_view, $this->_layout); return $this->content; }
/** * Return the content of the view * * @return string */ public function content() { $this->data = array('apine_application_https' => Application\Application::get_instance()->get_use_https(), 'apine_application_mode' => Application\Application::get_instance()->get_mode(), 'apine_application_secure' => Application\Application::get_instance()->get_secure_session()); $this->data = array_merge($this->data, $this->_params->get_all()); $this->data = array_merge($this->data, array("apine_view_title" => $this->_title)); $content = $this->process($this->_view); $this->data[] = $content; $this->content = $this->process($this->_layout); return $this->content; }
/** * Set path to view file * * @param string $a_view */ public function set_view($a_view) { if ($a_view != "") { $location = Application\Application::get_instance()->framework_location(); // Verify if the view file exists if (file_exists("views/{$a_view}.php")) { $this->_view = "views/{$a_view}"; } else { if (file_exists($location . "/Views/{$a_view}.php")) { $this->_view = "{$location}/Views/{$a_view}"; } else { if (file_exists("{$a_view}.php")) { $this->_view = $a_view; } else { $this->_view = $location . '/Views/default_view'; } } } } }
/** * Error view generation * * @param string|integer $a_code * @param string $a_message * @param Exception $a_exception * @return MVC\View */ public function custom($a_code, $a_message, Exception $a_exception = null) { $this->_view->set_param('code', $a_code); $this->_view->set_param('message', $a_message); if (Core\Request::is_api_call() || Core\Request::is_ajax()) { $this->_view->set_param('request', Core\Request::get()['request']); } else { $this->_view->set_title($a_message); $this->_view->set_view('error'); } if ($a_exception !== null && !is_array($a_exception)) { $this->_view->set_param('file', $a_exception->getFile()); $this->_view->set_param('line', $a_exception->getLine()); if (Application\Application::get_instance()->get_mode() === APINE_MODE_DEVELOPMENT) { $this->_view->set_param('trace', $a_exception->getTraceAsString()); } } if ($this->is_http_code($a_code)) { $this->_view->set_response_code($a_code); } else { $this->_view->set_response_code(500); } return $this->_view; }
/** * Construct the session handler * Fetch data from PHP structures and start the PHP session */ public function __construct() { $config = Application::get_instance()->get_config(); if ($config->get('runtime', 'user_class')) { $user_class = $config->get('runtime', 'user_class'); $pos_slash = strpos($user_class, '/'); $module = substr($user_class, 0, $pos_slash); $class = substr($user_class, $pos_slash + 1); apine_load_module($module); if (is_a($class, 'Apine\\User\\User')) { $this->user_class_name = $class; } else { $this->user_class_name = 'Apine\\User\\User'; } } else { $this->user_class_name = "Apine\\User\\User"; } if (!is_null($config->get('runtime', 'session_lifespan'))) { $this->session_lifespan = (int) $config->get('runtime', 'session_lifespan'); } if (!is_null($config->get('runtime', 'session_permanent_lifespan'))) { $this->session_lifespan = (int) $config->get('runtime', 'session_permanent_lifespan'); } if (isset($_COOKIE['apine_session'])) { $token = $_COOKIE['apine_session']; } else { $token = Encryption::token(); } $this->session = new SessionData($token); $this->php_session_id = $token; $delay = $this->session_lifespan; $this->logged_in = false; if ($this->session->get_var('apine_user_id') != null) { if ($this->session->get_var('apine_session_permanent') != null) { $delay = $this->session_permanent_lifespan; } if ($this->session->is_valid($delay) && UserFactory::is_id_exist($this->session->get_var('apine_user_id'))) { $this->logged_in = true; $this->user_id = $this->session->get_var('apine_user_id'); $this->session_type = $this->session->get_var('apine_user_type'); } else { $this->session->reset(); } } $this->current_session_lifespan = $delay; setcookie('apine_session', $this->php_session_id, time() + $delay, '/'); }