Example #1
0
 /**
  *
  */
 protected function dispatch()
 {
     if ($this->dispatcher_is_breaked) {
         return;
     }
     $module = $this->request->getRoute('module');
     $controller = $this->request->getRoute('controller');
     $controller = Core_String::toClass($module) . '_' . Core_String::toClass($controller) . 'Controller';
     if (class_exists($controller, true)) {
         $controller_instance = new $controller();
         if (method_exists($controller_instance, 'init')) {
             $controller_instance->init();
         }
         if ($this->dispatcher_is_breaked) {
             return;
         }
         $action = $this->request->getRoute('action');
         $action = Core_String::toFunction($action) . 'Action';
         if (method_exists($controller_instance, $action)) {
             $controller_instance->{$action}();
             return;
         }
     }
     p404('Missing: ' . $controller . (!empty($action) ? '::' . $action : null));
 }
Example #2
0
 /**
  * @param       $address
  * @param bool  $lang
  * @param array $options
  * @return mixed|string
  */
 public function getAddress($address, array $options = array())
 {
     if (substr($address, 0, 7) == 'http://' || substr($address, 0, 8) == 'https://') {
         return $address;
     }
     if (!isset($address)) {
         $tmp_parts = Core_Request_Routes::getInstance()->getRoutes();
         unset($tmp_parts['module']);
         $address = implode('/', $tmp_parts);
         unset($tmp_parts);
     }
     // Create query
     $query = array();
     if (!empty($options[self::ADD_CURRENT_QUERY])) {
         $query = array_merge($query, $this->request->getAllQueries());
     }
     if (strstr($address, '?')) {
         parse_str(preg_replace('[.*\\?]', null, $address), $query_tmp);
         $query = array_merge($query, $query_tmp);
         $address = preg_replace('[\\?.*]', null, $address);
     }
     if (isset($options[self::ADD_QUERY])) {
         $query = array_merge($query, $options[self::ADD_QUERY]);
     }
     // Create $address
     $module = isset($options[self::SET_MODULE]) ? $options[self::SET_MODULE] : $this->request->getRoute('module');
     if ($module == 'default') {
         $module = null;
     }
     $address = '/' . $module . '/' . $address;
     $address_parts = array();
     foreach (explode('/', $address) as $part) {
         if ($part) {
             if (substr($part, 0, 1) == ':') {
                 $part = $this->request->getRoute(substr($part, 1));
             }
             // skip displaying index files
             if ($part == 'index') {
                 continue;
             }
             $address_parts[] = $part;
         }
     }
     $address = cfg()->game_address . '/' . implode('/', $address_parts);
     // Prepare to return
     if ($query) {
         $address .= '&' . http_build_query($query);
     }
     if (!empty($options[self::STRICT_LINK])) {
         $address = str_replace('&', '&', $address);
     }
     return $address;
 }
Example #3
0
 /**
  * @throws Exception
  */
 public function __construct()
 {
     parent::__construct();
     //		if (PHP_SAPI == 'cli') {
     //			throw new Exception('You can`t create session in console');
     //		}
     $this->request = Core_Request::getInstance();
     // Start session
     $session_id = $this->request->{self::NAME};
     if ($session_id) {
         session_id($session_id);
     }
     if ($this->request->getCookie(self::NAME_PERSISTENT)) {
         session_set_cookie_params(self::PERSISTENT_TIME, $this->request->getHomeDir());
     } else {
         session_set_cookie_params(0, $this->request->getHomeDir());
     }
     session_name(self::NAME);
     // Try session start many times
     for ($i = 1; $i <= 5; $i++) {
         if (session_start()) {
             break;
         }
         usleep(100000);
     }
 }
Example #4
0
 /**
  *
  */
 public static function checkPermissions()
 {
     $request = Core_Request::getInstance();
     // permissions structure
     $data = array('module' => $request->getRoute('module'), 'controller' => $request->getRoute('controller'), 'action' => $request->getRoute('action'));
     $model = Admin_PermissionsModel::getInstance();
     $flag = $model->getFlag($data);
     // $flag = 0 - is a free acces of the page
     if ($flag === 0) {
         return true;
     }
     if (!$flag) {
         // we need to check that method exist
         $model->add($data);
     } else {
         if (!s()->user->id) {
             // @todo
             Core_View::getInstance()->addFlashMessage(__('Please Login'), 'danger');
             Core_Response::getInstance()->setStatus(1)->redirect('admin')->toJson();
         }
         if (!Core_Bit::check(s()->user->access['permissions'], $flag)) {
             // well an owner has ALL access
             $role_rs = Admin_RolesModel::get(array('id' => s()->user->role_id));
             if ($role_rs->is_owner === 1 || s()->user->is_developer === 1) {
                 return true;
             }
             Core_View::getInstance()->addFlashMessage(__('You Don\'t have permission to access this page'), 'danger');
             Core_Response::getInstance()->setStatus(1)->redirect('admin')->toJson();
         }
     }
 }
Example #5
0
 /**
  *
  */
 public function indexAction()
 {
     die('todo');
     $overwrite = !is_null(Core_Request::getInstance()->getArgv(4)) ? Core_Request::getInstance()->getArgv(4) : false;
     foreach (dibi::getDatabaseInfo()->getTables() as $table_data) {
         if ($table_data->getName() == Migration_MigrationModel::getTableName()) {
             continue;
         }
         $ddl_data = dibi::query('SHOW CREATE TABLE ' . $table_data->getName())->fetch()->toArray();
         $ddl_query = $ddl_data['Create Table'];
         $migration_time = time();
         $migration_name = 'Create' . ucfirst($table_data->getName());
         $migration_name = str_replace(' ', '', $migration_name);
         $migration_name = str_replace('-', '', $migration_name);
         $migration_name = str_replace('_', '', $migration_name);
         $filename = cfg()->migration_path . $migration_name . '.php';
         if (Core_Files::fileSize($filename) && !$overwrite) {
             echo PHP_EOL . 'Migration "Create ' . ucfirst($table_data->getName()) . '" Exists' . PHP_EOL;
             continue;
         }
         $template_data = Core_Files::getContent(cfg()->migration_path . Migration_FilesHelper::TEMPLATE_FILE_NAME);
         $template_data = str_replace('Template', $migration_name, $template_data);
         $template_data = str_replace('__NAME__', $migration_name, $template_data);
         $template_data = str_replace('__CREATED_AT__', $migration_time, $template_data);
         $template_data = str_replace('__CREATED_CFG__', Core_Request::getInstance()->getArgv(1), $template_data);
         $template_data = preg_replace('#//__UP_ACTION__#', $ddl_query, $template_data);
         $down_query = 'DROP TABLE IF EXISTS `' . $table_data->getName() . '`';
         $template_data = preg_replace('#//__DOWN_ACTION__#', $down_query, $template_data);
         Core_Files::putContent($filename, $template_data);
         echo PHP_EOL . 'Migration ' . $filename . ' created' . PHP_EOL;
     }
 }
Example #6
0
 public static function getInstance()
 {
     if (null == self::$_instance) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Example #7
0
 /**
  * @param        $host
  * @param        $user
  * @param        $pass
  * @param        $name
  * @param string $driver
  */
 public function __construct($host, $user, $pass, $name, $driver = 'pgsql')
 {
     try {
         parent::__construct($driver . ':dbname=' . $name . ';host=' . $host, $user, $pass);
     } catch (Exception $e) {
         if (!cfg()->dev_mode && Core_Request::getInstance()->getRoute('module') == 'default') {
             $view = Core_View::getInstance();
             $view->setLayoutFile('$maintenance/db_connect.phtml');
             $view->displayLayout();
             die;
         } else {
             print get_class($e) . ': ' . $e->getMessage() . PHP_EOL;
             die;
         }
     }
     if (cfg()->dev_mode) {
         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } else {
         $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
     }
     $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Core_Db_Statement', array($this)));
     if (cfg()->dev_mode) {
         //			Core_Debug::getInstance()->initPdoDebug($this);
     }
 }
Example #8
0
 public function __get($var)
 {
     switch ($var) {
         case '_request':
             return $this->_request = Core_Request::getInstance();
             break;
     }
 }
Example #9
0
/**
 * @param null $address
 */
function rr($address = null)
{
    $request = Core_Request::getInstance();
    if ($request->getServer('HTTP_REFERER')) {
        if (strstr($request->getServer('HTTP_REFERER'), $request->getServer('REQUEST_URI'))) {
            r('/');
        } else {
            r($request->getServer('HTTP_REFERER'), null, array(Core_Url::DIRECT_REDIRECT => true));
        }
    } else {
        r($address);
    }
}
Example #10
0
 public static function login($user_id, $persistent = false)
 {
     s()->flush();
     if ($persistent) {
         s()->setPersistent();
     }
     // Load user data
     s()->user->id = $user_id;
     // Set user session
     Admin_UsersModel::set(array('session_id' => s()->getId()), array('id' => s()->user->id));
     Admin_LogsModel::insert(array('user_id' => $user_id, 'ip' => Core_Request::getInstance()->getServer('REMOTE_ADDR')), Admin_LogsModel::USER_LOGIN);
     Cms_UserData::refresh(array(Cms_UserData::ATTR_ALL));
     return s()->user->id ? true : false;
 }
Example #11
0
 public function createPaging($total_results, $per_page, array $parameters = array())
 {
     $id = !empty($parameters[self::PARAM_ID]) ? $parameters[self::PARAM_ID] : self::Admin_ID;
     self::$store[$id] = $parameters;
     self::$store[$id][self::PARAM_TOTAL_RESULTS] = $total_results;
     self::$store[$id][self::PARAM_PER_PAGE] = $per_page;
     self::$store[$id][self::PARAM_TOTAL_PAGES] = ceil($total_results / $per_page);
     if (empty(self::$store[$id][self::PARAM_CURRENT_PAGE])) {
         $request = Core_Request::getInstance();
         self::$store[$id][self::PARAM_CURRENT_PAGE] = $request->{$id . 'p'} ? $request->{$id . 'p'} : 1;
     }
     if (empty(self::$store[$id][self::PARAM_MAX_PAGES])) {
         self::$store[$id][self::PARAM_MAX_PAGES] = self::MAX_PAGES;
     }
     if (empty(self::$store[$id][self::PARAM_SUFIX])) {
         self::$store[$id][self::PARAM_SUFIX] = '';
     }
 }
Example #12
0
 /**
  * @throws Exception
  */
 public function indexAction()
 {
     $migration_time = time();
     $migration_name = time();
     if (!$migration_name) {
         throw new Exception('No migration name given');
     }
     $filename = cfg()->migration_path . $migration_name . '.php';
     if (Core_Files::fileSize($filename)) {
         throw new Exception('Migration with that name exists');
     }
     $template_data = Core_Files::getContent(cfg()->migration_path . Const_Migrations::TEMPLATE_FILE_NAME);
     $template_data = str_replace('Template', $migration_name, $template_data);
     $template_data = str_replace('__NAME__', $migration_name, $template_data);
     $template_data = str_replace('__CREATED_AT__', $migration_time, $template_data);
     $template_data = str_replace('__CREATED_CFG__', Core_Request::getInstance()->getArgv(1), $template_data);
     Core_Files::putContent($filename, $template_data);
     echo PHP_EOL . 'Migration ' . $filename . ' created' . PHP_EOL . PHP_EOL;
 }
Example #13
0
 /**
  * @return Core_Request
  */
 public final function getRequest()
 {
     return Core_Request::getInstance();
 }
Example #14
0
File: Boot.php Project: eryx/labs
    array_walk_recursive($_COOKIE, 'array_stripslashes');
    array_walk_recursive($_REQUEST, 'array_stripslashes');
    array_walk_recursive($_FILES, 'array_stripslashes_files');
}
require_once "Core/Common.php";
function __autoload($class)
{
    $class = str_replace('_', '/', $class);
    if (preg_match("#^(.*)/Model/(.*)#i", $class, $regs)) {
        $class = strtolower($regs[1]) . '/models/' . $regs[2];
    }
    require_once $class . ".php";
}
//spl_autoload_register('__autoload');
try {
    $reqs = new Core_Request();
    if (isset($config['routes'])) {
        $reqs->router($config['routes']);
    }
    $ctr = str_replace(array('-', '.'), ' ', $reqs->ctr);
    $ctr = str_replace(' ', '', ucwords($ctr)) . 'Controller';
    $pat = SYS_ROOT . "app/{$reqs->mod}/controllers/";
    if (file_exists($pat . "{$ctr}.php")) {
        require_once $pat . "{$ctr}.php";
    } else {
        if (file_exists($pat . "IndexController.php")) {
            require_once $pat . "IndexController.php";
            $ctr = "IndexController";
        } else {
            throw new Exception('Invalid Request');
        }
Example #15
0
 public function dispatch()
 {
     $controller = $this->_dispatchInfo['controller'];
     $action = $this->_dispatchInfo['action'];
     $params = $this->_dispatchInfo['params'];
     // 储存URL参数
     if ($params) {
         Core_Request::getInstance()->setParams($params);
     }
     // 如果抛出异常,则由异常处理器处理
     if ($controller == 'Core_Exception_Error') {
         $className = $controller;
         $classPath = SYS_PATH . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $controller) . '.php';
     } else {
         $className = "Controller" . "_" . ucfirst($controller);
         $classPath = APP_PATH . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
     }
     // 先检测文件是否查找
     if (!file_exists($classPath)) {
         throw new Core_Exception_Fatal('Unable To find file - ' . $classPath);
     }
     // 然后再包含文件
     require $classPath;
     if (!class_exists($className)) {
         throw new Core_Exception_Fatal('Unable to find controller - ' . $classPath);
     }
     $controllerObj = new $className();
     $actionMethod = $action . 'Action';
     // 方法不存在
     if (!method_exists($controllerObj, $actionMethod)) {
         throw new Core_Exception_Fatal('Unable to find action - ' . $className . '::' . $actionMethod, 404);
     }
     $result = call_user_func(array($controllerObj, $actionMethod));
     if (isset($controllerObj->autoRender) && $controllerObj->autoRender) {
         if (null === $result || $result !== false) {
             $tpl = $controllerObj->getTpl() ?: strtolower($controller) . DS . strtolower($action);
             $tplFilePath = template($tpl);
             if (!is_file($tplFilePath)) {
                 throw new Core_Exception_Fatal('Unable to find template - ' . $tplFilePath);
             }
             Core_View::getInstance()->display($tpl);
         }
     }
 }
Example #16
0
 /**
  * @throws Exception
  */
 public function __construct()
 {
     parent::__construct();
     $this->request = Core_Request::getInstance();
 }