/** * Constructor that takes the entity name as first parameter to call the parent constructor * * @param string $entityName The entity name */ public function __construct(string $entityName) { Ini::setIniFileName(static::ENTITIES_CONF_PATH . $entityName . '.ini'); $this->conf = Ini::getAllParams(); $this->entityName = $entityName; $this->parseConf(); }
/** * 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 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()); }
/** * 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]); }
/** * 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); }
/** * 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']; }
/** * 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; } }
/** * 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; }
/** * Constructor that instantiate a new logger */ public function __construct() { Ini::setIniFileName('conf.ini'); $this->logger = new Logger(Ini::getParam('Exception', 'implementedLogger')); }
/** * Constructor that loads chat parameters */ public function __construct() { Ini::setIniFileName(Ini::INI_CONF_FILE); $conf = Ini::getSectionParams('Client service'); $this->serviceName = $conf['serviceName']; }
/** * Process the command called on the entities * * @param string $command The command passed with its arguments */ private function entitiesProcess(string $command) { $args = $this->getArgs($command); if (isset($args['table'])) { $tables = [['ENTITY', 'TABLE']]; foreach (DB::getAllEntities() as $entityName) { /** @var Entity $entity */ $entityClassNamespace = Ini::getParam('Entities', 'entitiesClassNamespace') . '\\' . $entityName; $entity = new $entityClassNamespace(); $tables[] = [$entityName, $entity->getTableName()]; Ini::setIniFileName(Ini::INI_CONF_FILE); } static::out(static::prettyTwoDimensionalArray($tables) . PHP_EOL); } elseif (isset($args['info'])) { foreach (DB::getAllEntities() as $entityName) { /** @var Entity $entity */ $entityClassNamespace = Ini::getParam('Entities', 'entitiesClassNamespace') . '\\' . $entityName; $entity = new $entityClassNamespace(); static::out($entity->__toInfo() . PHP_EOL); } } elseif (isset($args['create'])) { $this->createAllTables(); } elseif (isset($args['data'])) { $this->insertUserData(); $this->insertRoomData(); } elseif (isset($args['drop'])) { if ($this->confirmAction('DROP the all entities table ? (Y/N)') === 'Y') { foreach (DB::getAllEntities() as $entityName) { /** @var Entity $entity */ $entityClassNamespace = Ini::getParam('Entities', 'entitiesClassNamespace') . '\\' . $entityName; $entity = new $entityClassNamespace(); Ini::setIniFileName(Ini::INI_CONF_FILE); if (DB::dropTable($entity->getTableName())) { static::ok(static::ACTION_DONE . PHP_EOL); } else { static::fail(static::ACTION_FAIL . PHP_EOL . static::tablePrettyPrint(DB::errorInfo()) . PHP_EOL); } } } else { static::out(static::ACTION_CANCEL . PHP_EOL); } } }