/** * Constructor that called the parent \Exception constructor * * @param string $message Error message * @param int $code Error level * @param \Exception|null $previous Previous \Exception or \Error */ public function __construct(string $message, int $code = 0, $previous = null) { parent::__construct($message, $code, $previous); Ini::setIniFileName('conf.ini'); $this->logger = new Logger(Ini::getParam('Exception', 'implementedLogger')); $this->logger->log($code, $message, parent::getTrace()); }
/** * Routing function * * @param string $route The given route to map */ function route($route) { $path = explode('/', $route); $method = array_pop($path); $controller = ucfirst(array_pop($path)) . 'Controller'; $deep = count($path); $currentDeep = 0; $route = __DIR__ . DIRECTORY_SEPARATOR . 'controllers'; while ($currentDeep < $deep) { $route .= DIRECTORY_SEPARATOR . $path[$currentDeep++]; if (!is_dir($route)) { header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request', true, 400); die; } } $route .= DIRECTORY_SEPARATOR . $controller . '.php'; if (stream_resolve_include_path($route) === false) { header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404); die(file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'static' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . '404NotFound.html')); } include_once $route; Ini::setIniFileName(Ini::INI_CONF_FILE); // If the print SQL debug mode is on start a buffer if (Ini::getParam('Console', 'printSql')) { ob_start(); } $controllerPath = 'controllers\\' . $controller; $controllerInstance = new $controllerPath(); $controllerInstance->{$method}(); }
/** * Constructor that take the file path as a first parameter, if omitted it loads the file path defined in the ini * * @param string $filePath OPTIONAL the file path */ public function __construct(string $filePath = null) { if ($filePath !== null && is_string($filePath)) { $this->filePath = $filePath; } else { $this->filePath = Ini::getParam('FileLogger', 'filePath'); } }
/** * Get the email template by its name * * @param string $templateName The email template name * * @throws \Exception if the template is not found * * @return string The email template */ public static function getEmailTemplate(string $templateName) : string { $templatePath = dirname(__FILE__, 3) . DIRECTORY_SEPARATOR . Ini::getParam('Web', 'emailsPath') . DIRECTORY_SEPARATOR . $templateName . '.php'; if (stream_resolve_include_path($templatePath) === false) { throw new \Exception('"' . $templatePath . '" not find in email templates path'); } return file_get_contents($templatePath); }
/** * Constructor that loads chat parameters */ public function __construct() { Ini::setIniFileName(Ini::INI_CONF_FILE); $conf = Ini::getSectionParams('Room service'); $this->serviceName = $conf['serviceName']; $this->chatServiceName = Ini::getParam('Chat service', 'serviceName'); $this->logger = new Logger([Logger::CONSOLE]); }
/** * Constructor that loads chat parameters */ public function __construct() { Ini::setIniFileName(Ini::INI_CONF_FILE); $this->logger = new Logger([Logger::CONSOLE]); $this->esIndex = Ini::getParam('ElasticSearch', 'index'); $conf = Ini::getSectionParams('Chat service'); $this->serviceName = $conf['serviceName']; $this->historicStep = $conf['historicStep']; }
/** * Output a JSON response from a data array passed in parameter * * @param array $data The data to output */ public function JsonResponse(array $data) { Ini::setIniFileName(Ini::INI_CONF_FILE); // If the print SQL debug mode is on clean the buffer before output if (Ini::getParam('Console', 'printSql')) { ob_end_clean(); } echo json_encode($data, true); }
/** * Get the user latitude and longitude as array ['lat' => latitude, 'lon' => longitude] or empty array on error * using MaxMind database */ public function getLocation() { try { $record = (new Reader(Ini::getParam('GeoIp', 'databasePath')))->city($_SERVER['REMOTE_ADDR']); $location = ['lat' => $record->location->latitude, 'lon' => $record->location->longitude]; } catch (\Exception $e) { $location = []; } finally { $this->JsonResponse($location); } }
/** * Echo shortcut but with a encoding conversion before output * * @param string $output The string to output in the console * @param string $prefix A prefix to add to teh output DEFAULT '' * * @static */ public static function out(string $output, string $prefix = '') { Ini::setIniFileName(Ini::INI_CONF_FILE); $environment = Ini::getParam('Environment', 'environment'); if (!isset($_SERVER['REQUEST_URI'])) { $environment = 'console'; } switch ($environment) { case 'console': echo mb_convert_encoding($prefix . $output, static::$echoEncoding); break; case 'web': foreach (preg_split('/' . PHP_EOL . '/', ConsoleColors::unsetColor($output)) as $line) { // Console::log($line); echo $line; } break; default: echo $prefix . $output; break; } }
/** * Constructor that loads connection parameters * * @param string[] $parameters OPTIONAL connection parameters * @param bool $verbose OPTIONAL true if output should be print, false if not and null will load the * ini value */ public function __construct(array $parameters = null, bool $verbose = null) { $this->params = $parameters !== null ? $parameters : Ini::getSectionParams('Deployment'); $this->verbose = (int) Ini::getParam('Deployment', 'verbose'); $this->resource = null; }
/** * Constructor that instantiates a ConsoleColors */ public function __construct() { static::$echoEncoding = Ini::getParam('Console', 'encoding'); }
/** * Utility method to reuse the same PDO instance at each call (work like a Singleton pattern) * * @static */ private static function initialize() { Ini::setIniFileName(Ini::INI_CONF_FILE); if (static::$printSQL === null) { // Load default printSQL value static::$printSQL = Ini::getParam('Console', 'printSql'); } try { if (static::$PDO === null) { if (static::$username !== '' && static::$password !== '') { if (count(static::$options) > 0) { static::$PDO = new \PDO(static::$dsn, static::$username, static::$password, static::$options); } else { static::$PDO = new \PDO(static::$dsn, static::$username, static::$password); } } elseif (static::$dsn !== '') { static::$PDO = new \PDO(static::$dsn); } else { // Load default database parameters $param = Ini::getSectionParams('Database'); static::$PDO = new \PDO($param['dsn'], $param['username'], $param['password'], $param['options']); } // Load default PDO parameters $params = Ini::getSectionParams('PDO'); foreach ($params as $paramName => $paramValue) { if (!is_numeric($paramValue)) { $paramValue = constant('\\PDO::' . $paramValue); } static::$PDO->setAttribute(constant('\\PDO::' . $paramName), $paramValue); } static::setPDOStatement(); } } catch (\Exception $e) { throw new Exception($e->getMessage(), Exception::$CRITICAL); } }
/** * Check and sanitize the input field before setting the value and keep errors trace * * @param string $columnName The column name * @param string $value The new column value * * @return string The sanitized value * * @todo Global validateField in EntityManager with generic checks (size) and custom to define in entity ? */ private function validateField(string $columnName, string $value) : string { if ($columnName !== 'password') { $value = trim($value); } $this->errors[$columnName] = array(); $length = strlen($value); $maxLength = $this->entity->getColumnMaxSize($columnName); $name = _(strtolower(preg_replace('/([A-Z])/', ' $0', $columnName))); if (in_array($columnName, User::$mustDefinedFields) && $length === 0) { $this->errors[$columnName][] = _('The ' . $name . ' can\'t be empty'); } elseif ($length > $maxLength) { $this->errors[$columnName][] = _('The ' . $name . ' size can\'t exceed ' . $maxLength . ' characters'); } if ($this->entity->checkUniqueField($columnName, $value)) { $this->errors[$columnName][] = _('This ' . $name . ' is already used'); } switch ($columnName) { case 'lastName': $value = ucwords(strtolower($value)); $value = preg_replace('/ ( )*/', ' ', $value); break; case 'firstName': $value = ucfirst(strtolower($value)); $value = preg_replace('/ ( )*(.)?/', '-' . strtoupper('$2'), $value); break; case 'pseudonym': if (in_array(strtolower($value), User::$pseudoBlackList)) { $this->errors[$columnName][] = _('The pseudonym "' . $value . '" is not accepted'); } foreach (User::$forbiddenPseudoCharacters as $forbiddenPseudoCharacter) { if (strpos($value, $forbiddenPseudoCharacter) !== false) { $this->errors[$columnName][] = _('The character "' . $forbiddenPseudoCharacter . '" is not accepted in pseudonyms'); } } if ($value === '') { $value = null; } break; case 'email': if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) { $this->errors[$columnName][] = _('This is not a valid email address'); } break; case 'password': Ini::setIniFileName(Ini::INI_CONF_FILE); $minPasswordLength = Ini::getParam('User', 'minPasswordLength'); if ($length < $minPasswordLength) { $this->errors[$columnName][] = _('The password length must be at least ' . $minPasswordLength); } $value = crypt($value, Ini::getParam('User', 'passwordCryptSalt')); break; } if (count($this->errors[$columnName]) === 0) { unset($this->errors[$columnName]); } return $value; }
/** * Insert users in database */ private function insertUserData() { $users = new UserCollection(); $userManager = new UserManager(null, $users); $password = crypt('123', Ini::getParam('User', 'passwordCryptSalt')); static::out('Create user data' . PHP_EOL); // Create an admin with password = 123 $admin = new User(['id' => 1, 'email' => '*****@*****.**', 'firstName' => 'Admin', 'lastName' => 'God', 'pseudonym' => 'admin', 'password' => $password]); $admin->setRight(new UserRight(['idUser' => 1, 'webSocket' => 1, 'chatAdmin' => 1, 'kibana' => 1])); $users->add($admin); // Create some normal users with password = 123 for ($i = 1; $i < 11; $i++) { $user = new User(['id' => $i + 1, 'email' => 'user_' . $i . '@websocket.com', 'firstName' => 'User ' . $i, 'lastName' => 'Normal', 'pseudonym' => 'User ' . $i, 'password' => $password]); $users->add($user); } if ($userManager->saveUserCollection()) { static::ok(sprintf('The followings users are inserted %s' . PHP_EOL, $users)); } else { static::fail('An error occurred on user collection save' . PHP_EOL); } }
/** * Constructor that instantiate a new logger */ public function __construct() { Ini::setIniFileName('conf.ini'); $this->logger = new Logger(Ini::getParam('Exception', 'implementedLogger')); }
============================--> <?php WebContentInclude::includeDirectoryFiles(Ini::getParam('Web', 'pagesPath')); ?> <!--==== End of Pages ====--> <!--============================ = Modals = =============================--> <?php WebContentInclude::includeDirectoryFiles(Ini::getParam('Web', 'modalsPath')); ?> <!--==== End of Modals ====--> <!--============================ = Alerts = =============================--> <?php WebContentInclude::includeDirectoryFiles(Ini::getParam('Web', 'alertsPath')); ?> <!--==== End of Alerts ====--> </body> </html>
/** * Constructor */ public function __construct() { static::$echoEncoding = Ini::getParam('Console', 'encoding'); $this->maxLength = Ini::getParam('Console', 'maxLength'); }