/** * Check if group is granted for the given permission but with read-only access. * @param string $permission Name of the permission to check. * @return boolean 'true' if group is granted else 'false'. * @throw \InvalidArgumentException Thrown when permission is null or empty. */ public function isReadOnly($permission) { $isReadOnly = false; // Check if permission is valid (not null or empty) if (StringUtils::emptyOrSpaces(strval($permission))) { throw new \InvalidArgumentException("Invalid permission (null or empty) !"); } // Get group permission $permission = $this->has_many_through('\\BenGee\\Slim\\Auth\\Permission', '\\BenGee\\Slim\\Auth\\GroupPermission')->where('label', $permission)->find_one(); if (boolval($permission)) { $groupPermission = $this->has_many('\\BenGee\\Slim\\Auth\\GroupPermission')->where('permission_id', $permission->id)->find_one(); if (boolval($groupPermission)) { $isReadOnly = boolval($groupPermission->read_only); } } return $isReadOnly; }
/** * Get a fully qualified path to a given template. * @param string $file Filename of the template to find in referenced templates directories. * @param string $namespace Namespace of a specific set of templates directories. * @return string The full pathname to the template or 'false' if not found. */ public function getTemplatePathname($file, $namespace = false) { $pathname = false; if ($this->_loader->exists($file)) { $paths = StringUtils::emptyOrSpaces($namespace) ? $this->_loader->getPaths() : $this->_loader->getPaths(trim($namespace)); foreach ($paths as $path) { $tmp_pathname = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($file, DIRECTORY_SEPARATOR); if (file_exists($tmp_pathname)) { $pathname = $tmp_pathname; break; } } } return $pathname; }
/** * Connect a user identified by its username and password. * When connected, user can be retrieved using the user() method. * If there are no password encryption algo configured then given password * is used as real password (without encryption). * @param string $username Username. * @param string $password Password. * @return bool true if connected else false. * @throw \RuntimeException Thrown if database adapter is not valid (null) or is not based on SlimORM. * @throw \RuntimeException Thrown if user model classname doesn't exist or is not based on \BenGee\Slim\Auth\User class. * @throw \ErrorException Thrown if session cannot be opened. */ public function connect($username, $password) { $connected = false; // Get reference to the parent Slim app and ORM $app = $this->app(); $db = $app->db; if (!$db || !$db instanceof \BenGee\Slim\Db\SlimORM) { throw new \RuntimeException("Database adapter referenced in parent Slim app must be valid (not null) and based on SlimORM !"); } // Get the configured named SlimORM connection $cnxName = $app->config(self::KEY_CONFIG_CNX_NAME); $cnxName = StringUtils::emptyOrSpaces($cnxName) ? SlimORM::DEFAULT_CNX : $cnxName; // Get an instance of the user model class $modelClassname = $this->getUserModel(); if (StringUtils::emptyOrSpaces($modelClassname)) { $modelClassname = '\\BenGee\\Slim\\Auth\\User'; } else { if (!class_exists($modelClassname)) { throw new \RuntimeException("The user defined user model class doesn't exist !"); } } $model = $db->model($modelClassname, $cnxName); // Load the user $username = strval($username); $user = $model->where('username', $username)->find_one(); // Check user validity if ($user) { $connected = $user->username == $username && (StringUtils::emptyOrSpaces($this->getPwdCypher()) && $password == $user->password || !StringUtils::emptyOrSpaces($this->getPwdCypher()) && password_verify($password, $user->password)); if ($connected) { if (session_start()) { $_SESSION[self::KEY_SESSION_USER] = $user; session_write_close(); } else { throw new \ErrorException("Cannot open the session !"); } } } // Return connection status return $connected; }
/** * Add a new connection to existing ones in Idiorm & Paris. * Every connection is identified by a name given in parameters to the * method. If no name is given then it configures the default connection * without deleting those already existing. * @param string $dsn Connection string to the data source. * @param string $usr Username to use to connect to the data source or false (default) if there is no identification needed. * @param string $pwd Password to use to connect to the data source or false (default) if there is no identification needed. * @param string $opt Additional connection options or null (default) if none. * @param string $name Name of the connection or false (default) to configure the default one. * @throw \ErrorException If DSN is empty or not a string. */ public function addConnection($dsn, $usr = false, $pwd = false, $opt = null, $name = false) { $dsn = is_string($dsn) ? trim($dsn) : false; $name = is_string($name) && !ctype_space($name) ? trim($name) : self::DEFAULT_CNX; if (!StringUtils::emptyOrSpaces($dsn)) { self::configure(self::DSN, $dsn, $name); $usr = !StringUtils::emptyOrSpaces($usr) ? trim($usr) : false; if ($usr !== false) { self::configure(self::USR, $usr, $name); } $pwd = !StringUtils::emptyOrSpaces($pwd) ? $pwd : false; if ($pwd !== false) { self::configure(self::PWD, $pwd, $name); } $opt = !StringUtils::emptyOrSpaces($opt) ? trim($opt) : false; if (!empty($opt)) { self::configure(self::OPT, $opt, $name); } } else { throw new ErrorException("Connection string cannot be empty and must be a string !"); } }
/** * Override this method to register routes handled by the module. * By default, handled routes follow the given pattern : * 'GET /module_name[/module_ctrl[/action[/param1/param2/...]]]' * If module is set as the default one, it also manages '/' request and its * route is named 'home' otherwise it is named with module's name. */ protected function registerRoutes() { $app = $this->app(); $module = $this; $route = $this->_name . '(/:ctrl(/:action(/:params+)))'; $route = '/' . ($this->isDefault() ? '(' . $route . ')' : $route); $app->get($route, function ($ctrl = 'index', $action = 'index', $params = null) use($app, $module) { // Set templates directory to the module's one if current view renderer is not Twig. if (!$app->view instanceof \BenGee\Slim\Twig\TwigView) { $app->view->setTemplatesDirectory($this->getDirectory() . DIRECTORY_SEPARATOR . 'views', $this->name()); } // Load controller class file. $ctrl_name = (!empty($ctrl) && trim($ctrl) != '' ? \BenGee\Slim\Utils\StringUtils::camelize($ctrl, true) : 'Index') . 'Controller'; require_once $this->getDirectory() . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR . $ctrl_name . '.php'; // Create an instance of the controller. $ctrl_instance = new $ctrl_name($module); // Call required action. $action_name = ($action != null && trim($action) != '' ? \BenGee\Slim\Utils\StringUtils::camelize($action) : 'index') . 'Action'; echo $params != null ? $ctrl_instance->{$action_name}($params) : $ctrl_instance->{$action_name}(); })->name($this->isDefault() ? 'home' : $this->name()); }
$result = StringUtils::endsWith('test string\\r\\n', 'string'); echo $result ? 'true (FAILED)' : 'false (SUCCESS)'; ?> </li> <li> <pre>StringUtils::endsWith('test string\r\n', 'string\r\n')</pre> Result : <?php $result = StringUtils::endsWith('test string\\r\\n', 'string\\r\\n'); echo $result ? 'true (SUCCESS)' : 'false (FAILED)'; ?> </li> <li> <pre>StringUtils::endsWith('test string\r\n', '\n')</pre> Result : <?php $result = StringUtils::endsWith('test string\\r\\n', '\\n'); echo $result ? 'true (SUCCESS)' : 'false (FAILED)'; ?> </li> <li> <pre>StringUtils::endsWith('test string\r\n', '\r')</pre> Result : <?php $result = StringUtils::endsWith('test string\\r\\n', '\\r'); echo $result ? 'true (FAILED)' : 'false (SUCCESS)'; ?> </li> </ul> </li> </ul> </body> </html>
/** * Constructor. * First, get application root directory. * Second, get user defined modules storage directory or use a default * 'modules' one inside application root directory. * Third, get user defined default templates directory or use a default * 'templates' one inside application root directory. * Fourth, get array of modules fully qualified classnames to load. * @param array $userSettings Associative array of application settings. * @throw \ErrorException If module class cannot be loaded or an instance is already loaded. */ public function __construct(array $userSettings = array()) { parent::__construct($userSettings); // Get application root directory path $slimRootDir = $this->config('slim.dir.root'); if (!StringUtils::emptyOrSpaces($slimRootDir) && !StringUtils::endsWith($slimRootDir, '/') && !StringUtils::endsWith($slimRootDir, '\\')) { $slimRootDir .= DIRECTORY_SEPARATOR; } // Get application modules directory path $slimModulesDir = $this->config('slim.dir.modules'); if (!StringUtils::emptyOrSpaces($slimModulesDir)) { if (!StringUtils::endsWith($slimModulesDir, '/') && !StringUtils::endsWith($slimModulesDir, '\\')) { $slimModulesDir .= DIRECTORY_SEPARATOR; } } else { $slimModulesDir = $slimRootDir . 'modules' . DIRECTORY_SEPARATOR; } // Set application default templates directory where to look first $slimTemplatesDir = $this->config('slim.dir.templates'); if (!StringUtils::emptyOrSpaces($slimTemplatesDir)) { if (!StringUtils::endsWith($slimTemplatesDir, '/') && !StringUtils::endsWith($slimTemplatesDir, '\\')) { $slimTemplatesDir .= DIRECTORY_SEPARATOR; } } else { $slimTemplatesDir = $slimRootDir . 'templates' . DIRECTORY_SEPARATOR; } if ($this->view instanceof \BenGee\Slim\TwigView) { $this->view->addTemplatesDirectory($slimTemplatesDir); } else { $this->view->setTemplatesDirectory($slimTemplatesDir); } // Load list of modules composing main application. $slimModules = $this->config('slim.modules'); // Create a default modules list if not defined. if (!is_array($slimModules)) { $slimModules = array(); } // Add the default module to the list if not already present. if (array_search('default', $slimModules) === false) { $slimModules['default'] = 'DefaultModule'; } // Update the modules list in the application settings $this->config('slim.modules', $slimModules); // Inject a single instance of each defined module into the main application under $app->modules->module_name $this->modules = new \Slim\Helper\Set(); foreach ($slimModules as $moduleName => $moduleClass) { // If the class is not already loaded because not available as a package downloaded via Composer ... if (!class_exists($moduleClass)) { // ... then try loading from the local modules folder $moduleFile = $slimModulesDir . $moduleName . DIRECTORY_SEPARATOR . $moduleClass . '.php'; if (file_exists($moduleFile)) { require_once $moduleFile; } else { throw new \ErrorException("Cannot load module definition file : " . $moduleFile); } } // Reference module inside the application in the 'modules' named array under the module's name as key. $module = new $moduleClass($this, $moduleName, trim($moduleName) == 'default' ? true : false); $oldModule = $this->modules[$moduleName]; if (!empty($oldModule) || $oldModule instanceof $moduleClass) { throw new \ErrorException("An instance of the module is already loaded !"); } $this->modules->singleton($moduleName, function () use($module) { return $module; }); } }