/** * Clear Assets */ public function clearAction() { // get Asset manager $assets = Safan::handler()->getObjectManager()->get('assets'); // get assets path $path = $assets->getCompressor()->getAssetsPath(); if (is_dir($path)) { shell_exec('rm -rf ' . $path . DS . '*'); } else { return CliManager::getErrorMessage('Assets path is not exist'); } }
/** * Return all Model Class names * * @return array */ private function getAllModels() { $modules = Safan::handler()->getModules(); $modelClasses = []; foreach ($modules as $moduleName => $modulePath) { $modelsPath = APP_BASE_PATH . DS . $modulePath . DS . 'Models'; $modelFiles = []; if (is_dir($modelsPath)) { $modelFiles = scandir($modelsPath); } foreach ($modelFiles as $modelFile) { if ($modelFile != '.' && $modelFile != '..' && is_dir($modelsPath . DS . $modelFile)) { $subModelFiles = scandir($modelsPath . DS . $modelFile); foreach ($subModelFiles as $subModelFile) { if ($subModelFile != '.' && $subModelFile != '..' && is_file($modelsPath . DS . $modelFile . DS . $subModelFile)) { $subModelName = substr($subModelFile, 0, -4); $modelClasses[] = ['name' => $subModelName, 'namespace' => '\\' . $moduleName . '\\Models\\' . $modelFile . '\\' . $subModelName, 'file' => $modelsPath . DS . $modelFile . DS . $subModelFile]; } } } elseif ($modelFile != '.' && $modelFile != '..' && is_file($modelsPath . DS . $modelFile)) { $modelName = substr($modelFile, 0, -4); $modelClasses[] = ['name' => $modelName, 'namespace' => '\\' . $moduleName . '\\Models\\' . $modelName, 'file' => $modelsPath . DS . $modelFile]; } } } return $modelClasses; }
/** * @param $widgetName * @param array $params * @throws \Safan\GlobalExceptions\ParamsNotFoundException * @throws \Safan\GlobalExceptions\FileNotFoundException */ public function begin($widgetName, $params = array()) { if (!isset($this->config[$widgetName])) { throw new FileNotFoundException($widgetName . ' Widget is not exist'); } $widget = $this->config[$widgetName]; if (!isset($widget['module']) || !isset($widget['controller']) || !isset($widget['action'])) { throw new ParamsNotFoundException($widgetName . ' Params is not exist'); } // get dispatcher $dispatcher = Safan::handler()->getObjectManager()->get('dispatcher'); // load widget $dispatcher->loadModule($widget['module'], $widget['controller'], $widget['action'], $params); }
/** * @var $command array */ private function dispatchCommand() { Safan::handler()->getObjectManager()->get('router')->checkCliCommand($this->command); if (Get::exists('module')) { $module = Get::str('module'); } else { return $this->getErrorMessage('Module Global Variable is not exists'); } if (Get::exists('controller')) { $controller = Get::str('controller'); } else { return $this->getErrorMessage('Controller Global Variable is not exists'); } if (Get::exists('action')) { $action = Get::str('action'); } else { return $this->getErrorMessage('Action Global Variable is not exists'); } // get all modules $modules = Safan::handler()->getModules(); if (isset($modules[$module]) && is_dir(APP_BASE_PATH . DS . $modules[$module])) { $nameSpace = '\\' . $module; $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[$module]; } elseif (isset($modules[ucfirst(strtolower($module))]) && is_dir(APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))])) { // check case sensitivity $nameSpace = '\\' . ucfirst(strtolower($module)); $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))]; } else { return $this->getErrorMessage($module . ' module or path are not exist'); } // Controller Class Name $moduleController = ucfirst(strtolower($controller)) . 'Controller'; $controllerFile = $modulePath . DS . 'Commands' . DS . $moduleController . '.php'; $this->currentController = $controller; if (!file_exists($controllerFile)) { return $this->getErrorMessage($modulePath . DS . 'Commands' . DS . $moduleController . ' controller file is not exist'); } include $controllerFile; // controller class $controllerClass = $nameSpace . '\\Commands\\' . $moduleController; if (!class_exists($controllerClass)) { return $this->getErrorMessage($controllerClass . ' Controller Class is not exist'); } $moduleControllerObject = new $controllerClass(); $actionMethod = strtolower($action) . 'Action'; if (!method_exists($moduleControllerObject, $actionMethod)) { return $this->getErrorMessage($actionMethod . ' Action Method is not exist in Controller Class'); } return $moduleControllerObject->{$actionMethod}(); }
/** * Connect to driver * * @param $dbConfig * @throws Exceptions\DriverNotFound */ public function init($dbConfig) { $profiler = new Profiler(); if (isset($this->drivers[$dbConfig['driver']])) { $driverClass = $this->drivers[$dbConfig['driver']]; self::$driverInstance = $driverClass::getInstance(); $profiler->getTimer()->start(); self::$driverInstance->setup($dbConfig); $profilerOptions = ['type' => 'connection', 'time' => $profiler->getTimer()->calculate()]; $profiler->setOptions('dbConnection', $profilerOptions); Safan::handler()->getObjectManager()->setObject('gapOrmProfiler', $profiler); } else { throw new DriverNotFound($dbConfig['driver']); } }
/** * Initialize Authentication * * @param $authParams * @throws FileNotFoundException */ public function init($authParams) { // build config parameters $config = new Configuration(); $config->buildConfig($authParams); // check driver if (!isset($this->drivers[$authParams['driver']])) { throw new FileNotFoundException($authParams['driver']); } $driverClass = $this->drivers[$authParams['driver']]; $auth = new $driverClass($config); $auth->checkStatus(); // set to object manager $om = Safan::handler()->getObjectManager(); $om->setObject('authentication', $auth); }
/** * Check cli routes for module */ public function checkCliRoutes() { // get modules $modules = Safan::handler()->getModules(); $modules = array_reverse($modules); // get all routes foreach ($modules as $moduleName => $modulePath) { $routerFile = APP_BASE_PATH . DS . $modulePath . DS . 'Resources' . DS . 'config' . DS . 'cli.router.config.php'; // register module namespaces $loader = new SplClassLoader($moduleName, $modulePath . DS . '..' . DS); $loader->register(); if (file_exists($routerFile)) { $route = (include $routerFile); $this->cliRoutes = array_merge($this->cliRoutes, $route); } } }
/** * Load module * * @param $module * @param $controller * @param $action * @param array $params * @return mixed */ public function loadModule($module, $controller, $action, $params = array()) { // get all modules $modules = Safan::handler()->getModules(); if (isset($modules[$module]) && is_dir(APP_BASE_PATH . DS . $modules[$module])) { $nameSpace = '\\' . $module; $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[$module]; } elseif (isset($modules[ucfirst(strtolower($module))]) && is_dir(APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))])) { // check case sensitivity $nameSpace = '\\' . ucfirst(strtolower($module)); $this->currentModulePath = $modulePath = APP_BASE_PATH . DS . $modules[ucfirst(strtolower($module))]; } else { return $this->dispatchToError(404, $module . ' module or path are not exist'); } // Controller Class Name $moduleController = ucfirst(strtolower($controller)) . 'Controller'; $controllerFile = $modulePath . DS . 'Controllers' . DS . $moduleController . '.php'; $this->currentController = $controller; if (!file_exists($controllerFile)) { return $this->dispatchToError(404, $modulePath . DS . 'Controllers' . DS . $moduleController . ' controller file is not exist'); } // controller class $controllerClass = $nameSpace . '\\Controllers\\' . $moduleController; // Check for widgets if (!class_exists($controllerClass)) { include $controllerFile; } if (!class_exists($controllerClass)) { return $this->dispatchToError(404, $controllerClass . ' Controller Class is not exists'); } $moduleControllerObject = new $controllerClass(); $actionMethod = strtolower($action) . 'Action'; if (!method_exists($moduleControllerObject, $actionMethod)) { return $this->dispatchToError(404, $actionMethod . ' Action Method is not exists in Controller Class'); } if (!empty($params)) { return $moduleControllerObject->{$actionMethod}($params); } else { return $moduleControllerObject->{$actionMethod}(); } }
/** * */ public function runEvent($eventKey) { if (!isset($this->events[$eventKey])) { return false; } $eventStr = $this->events[$eventKey]; $event = explode(':', $eventStr); if (sizeof($event) != 2) { throw new ParamsNotFoundException('Event params is not correct - ' . $eventStr); } $moduleName = $event[0]; $eventClass = $event[1]; // get all modules $allModules = Safan::handler()->getModules(); // check Module if (!isset($allModules[$moduleName])) { throw new FileNotFoundException($event[0] . ' Module is not defined'); } $eventFile = $allModules[$moduleName] . DS . 'Events' . DS . $eventClass . '.php'; $eventClass = $moduleName . '\\Events\\' . $eventClass; // check Evenet file if (!file_exists($eventFile)) { throw new FileNotFoundException($eventFile . ' event file is not exist'); } // check event class for double call if (!class_exists($eventClass)) { include $eventFile; } // check event class if (!class_exists($eventClass)) { throw new ObjectDoesntExistsException($eventClass . ' is not defined'); } $eventObj = new $eventClass(); // check init method if (!method_exists($eventObj, 'init')) { throw new ObjectDoesntExistsException($eventClass . ' have not method init()'); } return $eventObj->init(); }
/** * @return string */ public function getUri() { $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $uri = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $uriRequest = strpos($uri, Safan::handler()->baseUrl); if ($uriRequest !== false) { $uri = substr($uri, strlen(Safan::handler()->baseUrl) - $uriRequest); } //Get Variables if (strpos($uri, '?') !== false) { $uriVars = parse_str(substr(strstr($uri, '?'), 1), $outPutVars); //Generate Get variables foreach ($outPutVars as $key => $value) { if ($key != 'module' && $key != 'controller' && $key != 'action') { Get::setParams($key, $value); } } //Generate main uri $uri = strstr($uri, '?', true); } return $uri; }
/** * */ public function __construct() { if (is_null($this->sessionObject)) { $this->sessionObject = Safan::handler()->getObjectManager()->get('session'); } }
/** * @param $files * @return bool */ public function checkCustomAssets($files) { if (empty($files)) { return false; } $fileName = md5(implode('_', $files)) . '.js'; $jsFile = APP_BASE_PATH . DS . 'resource' . $this->cacheDir . DS . $fileName; if (!file_exists($jsFile)) { $this->compressFiles($files); } $cacheFileUri = Safan::handler()->baseUrl . $this->cacheDir . '/' . $fileName; return $this->getCacheFile($cacheFileUri); }
/** * Translate path * * @param $files * @return array * @throws \Safan\GlobalExceptions\FileNotFoundException */ private function translator($files) { // empty array for return $fileArray = []; // get modules $modules = Safan::handler()->getModules(); foreach ($files as $filePath) { $asset = explode(':', $filePath); if (sizeof($asset) !== 2) { throw new FileNotFoundException('Css asset name is not correct'); } $moduleName = $asset[0]; $filePath = $asset[1]; if (!isset($modules[$moduleName])) { throw new FileNotFoundException('Asset module is not define'); } $fullPath = APP_BASE_PATH . DS . $modules[$moduleName] . DS . 'Resources' . DS . 'public' . DS . $filePath; if (!file_exists($fullPath)) { throw new FileNotFoundException('Asset file is not define'); } $fileArray[] = $fullPath; } return $fileArray; }
/** * @param $profilerType * @param $result */ private function endProfiling($profilerType, $result) { $profiler = Safan::handler()->getObjectManager()->get('gapOrmProfiler'); $profileOptions = ['type' => 'query', 'time' => $profiler->getTimer()->calculate(), 'query' => $this->getQuery(), 'queryType' => $profilerType, 'queryParams' => $this->getParams(), 'result' => $result]; $profiler->setOptions($profilerType . '_' . time(), $profileOptions); }
/** * @param null $config */ public function __construct($config = null) { Safan::setHandler($this); $this->runApplication(); }
/** * Render captcha * * @return mixed */ public function render() { $dispatcher = Safan::handler()->getObjectManager()->get('dispatcher'); return $dispatcher->loadModule('Google', 'captcha', 'set', $this->config); }
/** * Update hashes * * @param $userData * @throws \Authentication\Exceptions\ClientDataException */ private function updateHash($userData) { // get client data $ip = $this->getClientIp(); $browser = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : false; // check client data if ($ip <= 0 || !$browser) { throw new ClientDataException(); } // generate cookie cache $cHash = hash('sha256', $userData->email . $this->hashKey . $userData->password . time()); // generate hash for db and memcache $oHash = hash('sha256', $ip . $browser . $cHash . $userData->id); // update db hash $userModel = UserBase::instance(); $userData->hash = $oHash; $userData->hashCreationDate = new \DateTime(); $userModel->save($userData); // update memcache hash $memcacheObj = Safan::handler()->getObjectManager()->get('memcache'); $memcacheKey = $this->getMemcacheKey($userData->id); $memcacheData = array('id' => $userData->id, $this->cookieHashPrefix => $oHash); $memcacheObj->set($memcacheKey, $memcacheData, self::MEMCACHE_CODE_TIMEOUT); // update cookie hash $cookieObj = Safan::handler()->getObjectManager()->get('cookie'); if ($this->isRemember) { $cookieDate = time() + self::COOKIE_LONG_DATE; } else { $cookieDate = time() + self::COOKIE_SHORT_DATE; } $cookieObj->set($this->cookieUserIDPrefix, $userData->id, $cookieDate, '/', null, null, true); $cookieObj->set($this->cookieHashPrefix, $cHash, $cookieDate, '/', null, null, true); }
/** * Redirect * * @param string $url * @param bool $globalUrl */ public function redirect($url = '', $globalUrl = false) { if ($globalUrl) { header('location: ' . $url); exit; } if (!$url) { header('location: ' . Safan::handler()->baseUrl); } else { header('location: ' . Safan::handler()->baseUrl . $url); } exit; }