/** * Hide the real message from being displayed when not in debug mode * @return string */ public function _getMessage() { if (Aelix::isDebug()) { $e = $this->getPrevious() ?: $this; return $e->getMessage(); } return 'You\'ve encountered an error. Please send the displayed ID to the site admin.'; }
/** * initiate template engine. * @param array $directories search directories for template files. * @param bool|true $caching */ public function __construct(array $directories, $caching = true) { $this->directories = $directories; $this->caching = $caching; $this->twigLoader = new \Twig_Loader_Filesystem($directories); $options = ['charset' => 'utf-8', 'cache' => $caching ? DIR_CACHE . 'template' . DS : false, 'auto_reload' => Aelix::isDebug()]; $this->twigEnv = new \Twig_Environment($this->twigLoader, $options); }
/** * register module namespaces to autoloader */ public function registerNamespaces() { foreach ($this->modules as $module) { chdir($this->modulesDir . $module->getName()); foreach ($module->getNamespaces() as $namespace => $dir) { if (is_array($dir)) { foreach ($dir as $curDir) { Aelix::autoloader()->addNamespace($namespace, realpath($curDir)); } } elseif (is_string($dir)) { Aelix::autoloader()->addNamespace($namespace, realpath($dir)); } } } chdir(DIR_START); }
/** * Print this exception * @return void */ public function show() { // try to log this shit $id = $this->logError(); // try to get the site title $title = ''; // TODO: read from site configuration // print HTML @header('HTTP/1.1 503 Service Unavailable'); $e = $this->getPrevious() ?: $this; echo '<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Fatal Error ' . $title . '</title> <style type="text/css"> html, body { font: normal normal normal 13px Helvetica, "Droid Sans", "Segoe UI", Arial, Verdana, sans-serif; margin: 0; padding: 0; color: #555; } h1, h2, h3, p, pre { margin: 0; padding: 5px 10px; color: #2e2e2e; } h1 { background: #ffd5d5; font-size: 20px; font-weight: bold; } h2 { background: #eee; font-size: 16px; font-weight: bold; } h3 { background: #f6f6f6; margin-top: 20px; } table { padding: 5px 10px; border-spacing: 0; border: none; width: 100%; } td { padding: 2px; } td:first-child { font-weight: bold; color: #2e2e2e; width: 100px; } td:last-child { font-family: monospace; } tr:nth-child(2n) { background: #f6f6f6; } </style> </head> <body> <h1>Fatal Error</h1>'; if (Aelix::isDebug()) { echo ' <h2>' . UString::encodeHTML($this->_getMessage()) . '</h2> <p>' . UString::encodeHTML($this->getDescription()) . '</p> <h3>Information</h3> <p>' . $this->information . '</p> <table> <tr> <td>ID</td> <td>' . $id . '</td> </tr> <tr> <td>Error message</td> <td>' . UString::encodeHTML($this->_getMessage()) . '</td> </tr> <tr> <td>Error code</td> <td>' . (int) $e->getCode() . '</td> </tr> <tr> <td>File</td> <td>' . UString::encodeHTML($e->getFile() . ':' . $e->getLine()) . '</td> </tr> <tr> <td>Time</td> <td>' . gmdate('r') . '</td> </tr> <tr> <td>Request</td> <td>' . (isset($_SERVER['REQUEST_URI']) ? UString::encodeHTML($_SERVER['REQUEST_URI']) : '') . '</td> </tr> <tr> <td>Referer</td> <td>' . (isset($_SERVER['HTTP_REFERER']) ? UString::encodeHTML($_SERVER['HTTP_REFERER']) : '') . '</td> </tr> </table> <h3>Stacktrace</h3> <pre>' . UString::encodeHTML($this->__getTraceAsString()) . '</pre>'; } else { echo ' <table> <tr> <td>ID</td> <td>' . $id . '</td> </tr> </table> <p>Send this ID to the administrator of this website to report this issue.</p>'; } echo ' </body> </html>'; }
/** * aelix constructor. * @param bool|false $initOnly only initialize basic functions, don't output anything (e.g. for migrations) * @throws CoreException */ public final function __construct($initOnly = false) { // register error and exception handler set_exception_handler(['\\aelix\\framework\\Aelix', 'handleException']); set_error_handler(['\\aelix\\framework\\Aelix', 'handleError'], E_ALL); // init autoloader require_once DIR_SRC . 'Autoload.php'; self::$autoloader = new Autoloader(); self::$autoloader->addNamespace('aelix\\framework', DIR_SRC); self::$autoloader->register(); // init event handling self::$eventHandler = new EventHandler(); // also use composer autoloader if necessary if (is_dir(DIR_ROOT . 'vendor') && is_file(DIR_ROOT . 'vendor' . DS . 'autoload.php')) { require_once DIR_ROOT . 'vendor' . DS . 'autoload.php'; } // module loader self::$moduleLoader = new ModuleLoader(DIR_ROOT . 'modules' . DS); self::$moduleLoader->registerNamespaces(); self::$moduleLoader->load(); Aelix::event()->dispatch('aelix.modules.load'); // load database config if (!is_file(DIR_ROOT . 'config.php')) { throw new CoreException('Could not find file ' . DIR_ROOT . 'config.php!', 0, 'Unable to find or open the config file for aelix: ' . DIR_ROOT . 'config.php'); } $config = (require_once DIR_ROOT . 'config.php'); // init DB self::$db = DatabaseFactory::initDatabase($config['database.driver'], $config['database.host'], $config['database.user'], $config['database.password'], $config['database.database'], $config['database.port']); // unset $config for security reasons unset($config); Aelix::event()->dispatch('aelix.database.init'); if ($initOnly) { // basic init is done, abort return; } // boot up configs self::$config = new Config('config'); Aelix::event()->dispatch('aelix.config.init'); // launch routes self::$router = new Router(); // make basepath configurable Aelix::event()->dispatch('aelix.router.register'); // init session self::$session = new Session(new DatabaseSessionHandler(self::$db, 'session'), self::config()->get('core.session.max_lifetime'), self::config()->get('core.session.gc_probability'), self::config()->get('core.session.cookie_lifetime'), self::config()->get('core.session.cookie_name')); Aelix::event()->dispatch('aelix.session.init'); // who's there? self::$user = self::$session->getUser(); Aelix::event()->dispatch('aelix.user.init'); // fire router $match = self::$router->matchCurrentRequest(); if ($match === false) { Aelix::event()->dispatch('aelix.router.no_route'); } else { $match->dispatch(); } Aelix::event()->dispatch('aelix.router.dispatch'); }
/** * @param string $username * @param string $email * @param string $fullname * @param string $password clear text * @param int $hashCost * @return User */ public static function create($username, $email, $fullname, $password, $hashCost = USecurity::HASHING_COST) { $passwordHash = USecurity::encryptPassword($password, $hashCost); Aelix::db()->prepare('INSERT INTO `user` SET `username` = :username, `email` = :email, `passwordHash` = :passwordHash, `fullname` = :fullname')->execute([':username' => $username, ':email' => $email, ':passwordHash' => $passwordHash, ':fullname' => $fullname]); $userID = Aelix::db()->getPDO()->lastInsertId('user'); $user = new User(['id' => $userID, 'username' => $username, 'email' => $email, 'passwordHash' => $passwordHash, 'fullname' => $fullname]); self::$userByID[$userID] = $user; return $user; }
/** * @param $name * @param $value * @return ConfigNode * @throws ConfigNodeAlreadyExistsException */ public function add($name, $value) { if (isset($this->nodes[$name])) { throw new ConfigNodeAlreadyExistsException('Config node ' . $name . ' already exists'); } $stmt = Aelix::db()->prepare('INSERT INTO `' . $this->tableName . '` SET `name` = :name, `value` = :value'); $stmt->execute([':name' => $name, ':value' => serialize($value)]); $node = new ConfigNode($this, Aelix::db()->getPDO()->lastInsertId($this->tableName), $name, $value); $this->nodes[$name] = $node; return $node; }
<?php /** * @author aelix framework <info@aelix framework.org> * @copyright Copyright (c) 2015 aelix framework * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 */ /* * init aelix */ define('DS', DIRECTORY_SEPARATOR); define('DIR_START', dirname(__FILE__) . DS); // from where we started the execution // only init aelix (until database) // we need the DB for phinx define('AELIX_ONLY_INIT', true); require 'src' . DS . 'bootstrap.php'; /* * aelix done, generate phinx config */ use aelix\framework\Aelix; // return phinx config array return ['paths' => ['migrations' => DIR_ROOT . 'migrations'], 'environments' => ['default_migration_table' => 'phinxlog', 'default_database' => 'aelix', 'aelix' => ['name' => Aelix::db()->getDatabaseName(), 'connection' => Aelix::db()->getPDO()]]];
/** * creates a new field. if already exists, returns the existing * @param string $fieldName * @return UserDataField */ public static function createField($fieldName) { // is already cached? if (isset(self::$fields[$fieldName]) && self::$fields[$fieldName] instanceof UserDataField) { return self::$fields[$fieldName]; } // find in DB $rowCount = Aelix::db()->prepare('SELECT * FROM `user_data_field` WHERE `fieldName` = :fieldName')->execute([':fieldName' => $fieldName])->rowCount(); // field exists, return it if ($rowCount > 0) { return self::getField($fieldName); } // create a new one Aelix::db()->prepare('INSERT INTO `user_data_field` SET `fieldName` = :fieldName')->execute([':fieldName' => $fieldName]); $fieldID = Aelix::db()->getPDO()->lastInsertId('user_data_field'); $obj = new UserDataField($fieldID, $fieldName); self::$fields[$fieldName] = $obj; return $obj; }
/** * @param mixed $value */ public function setValue($value) { $this->value = $value; $stmt = Aelix::db()->prepare('UPDATE `' . $this->config->getTableName() . '` SET `value` = :value WHERE `id` = :id'); $stmt->execute([':id' => $this->id, ':value' => serialize($this->value)]); }
/** * @param UserDataField $field * @param User $user * @param mixed $value * @return UserData * @throws \InvalidArgumentException */ public static function create(UserDataField $field, User $user, $value) { // check if field is already set for user if ($user->getData($field->getName()) !== null) { throw new \InvalidArgumentException('User\'s data field ' . $field->getName() . ' already set.'); } $value = serialize($value); Aelix::db()->prepare('INSERT INTO `user_data` SET `userID` = :userID, `fieldID` = :fieldID, `value` = :value')->execute([':userID' => $user->getID(), ':fieldID' => $field->getID(), ':value' => $value]); $dataID = Aelix::db()->getPDO()->lastInsertId('user_data'); return new UserData($user, $field, $dataID, $value); }