Ejemplo n.º 1
0
 /**
  * @brief 在路由之前执行,这个钩子里,可以做url重写等功能
  * @param \Yaf\Request_Abstract $request
  * @param \Yaf\Response_Abstract $response
  */
 public function routerStartup(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $response)
 {
     $referer = $request->getServer('HTTP_REFERER');
     $referHost = parse_url($referer, PHP_URL_HOST);
     $host = $request->getServer('HTTP_HOST');
     $referDomain = implode('.', array_slice(explode('.', $referHost), -2));
     $domain = implode('.', array_slice(explode('.', $host), -2));
 }
Ejemplo n.º 2
0
 public function routerStartup(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $reponse)
 {
     // 增加url伪静态 .html
     $uri = str_replace('.html', '', $_SERVER['REQUEST_URI']);
     // 增加路由支持 - 分割
     $uri = str_replace('-', '/', $uri);
     // 注册路由
     $request->setRequestUri($uri);
 }
Ejemplo n.º 3
0
 /**
  * @brief 路由完成后,在这个钩子里,可以做登陆检测,权限控制等功能
  * @param \Yaf\Request_Abstract $request
  * @param \Yaf\Response_Abstract $response
  */
 public function routerShutdown(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $response)
 {
     $objConfig = \Yaf\Application::app()->getConfig();
     $arrPath = explode('/', trim($objConfig->application->baseUri, '/'));
     $strController = array_pop($arrPath);
     if (!empty($strController)) {
         $request->setControllerName($strController);
     }
 }
Ejemplo n.º 4
0
 public function routerShutdown(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $response)
 {
     //PRETREATMENT REQUEST START -->
     if ($request->controller == 'Api') {
         \Core\KEY::set('_REQUEST', \Process\RequestModel::get($request->method, $request->getParams()));
         \Core\KEY::set('_IS_AUTHORIZED', FALSE);
     }
     //PRETREATMENT REQUEST END <--
 }
Ejemplo n.º 5
0
 public function routerStartup(\Yaf\Request_Abstract $request, \Yaf\Response_Abstract $response)
 {
     $request_url = $request->getRequestUri();
     // 增加url伪静态 .html
     $uri = str_replace('.html', '', $request_url);
     // 增加路由支持 - 分割
     $uri = str_replace('-', '/', $uri);
     // 注册路由
     $request->setRequestUri($uri);
 }
Ejemplo n.º 6
0
 function preDispatch(Request_Abstract $request, Response_Abstract $response)
 {
     $controllerName = strtolower($request->getControllerName());
     $exclude = array('index', 'error', 'login');
     if (!in_array($controllerName, $exclude)) {
         $model = LoginModel::getInstance();
         if (false == $model->checkLogin()) {
             $request->setControllerName('Login')->setActionName('index');
         }
     }
 }
Ejemplo n.º 7
0
 /**
  * Matches a user submitted path with a previously defined route.
  * Assigns and returns an array of defaults on a successful match.
  *
  * @param  string $path Path used to match against this routing map
  * @return array|false  An array of assigned values or a false on a mismatch
  */
 public function route(\Yaf\Request_Abstract $request)
 {
     $requestUri = $request->getRequestUri();
     $baseuri = $request->getBaseUri();
     if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) !== false) {
         $path = substr($requestUri, strlen($baseuri));
     } else {
         $path = $requestUri;
     }
     $path = urldecode($path);
     $res = preg_match($this->_route, $path, $values);
     if ($res === 0) {
         return false;
     }
     $values = $this->_getMappedValues($values);
     if (isset($this->_default['module'])) {
         $request->setModuleName($this->_default['module']);
     }
     if (isset($this->_default['controller'])) {
         $request->setControllerName($this->_default['controller']);
     }
     if (isset($this->_default['action'])) {
         $request->setActionName($this->_default['action']);
     }
     $request->setParam($values);
     return true;
 }
Ejemplo n.º 8
0
 /**
  * Matches a user submitted path with a previously defined route.
  * Assigns and returns an array of defaults on a successful match.
  *
  * @param  string $path Path used to match against this routing map
  *
  * @return array|false  An array of assigned values or a false on a mismatch
  */
 public function route(\Yaf\Request_Abstract $request)
 {
     $requestUri = $request->getRequestUri();
     $baseuri = $request->getBaseUri();
     if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) !== false) {
         $path = substr($requestUri, strlen($baseuri));
     } else {
         $path = $requestUri;
     }
     $path = trim(urldecode($path), \Yaf\Router::URI_DELIMITER);
     $values = $this->_match($path);
     if ($values == null) {
         return false;
     }
     if (isset($this->_default['module'])) {
         $request->setModuleName($this->_default['module']);
     }
     if (isset($this->_default['controller'])) {
         $request->setControllerName($this->_default['controller']);
     }
     if (isset($this->_default['action'])) {
         $request->setActionName($this->_default['action']);
     }
     $request->setParam($values);
     return true;
 }
Ejemplo n.º 9
0
 /**
  * Processes a request and sets its controller and action.  If
  * no route was possible, default route is set.
  *
  * @param  Yaf_Request_Abstract
  *
  * @return Yaf_Request_Abstract|boolean
  */
 public function route(\Yaf\Request_Abstract $request)
 {
     $requestUri = $request->getRequestUri();
     $baseuri = $request->getBaseUri();
     if ($requestUri != '' && $baseuri != '' && stripos($requestUri, $baseuri) === 0) {
         $path = substr($requestUri, strlen($baseuri));
     } else {
         $path = $requestUri;
     }
     $path = trim(urldecode($path), \Yaf\Router::URI_DELIMITER);
     $rest = '';
     if (is_string($this->_delimiter) && $this->_delimiter != '') {
         if (($queryStringPos = strpos($path, $this->_delimiter)) !== false) {
             $rest = substr($path, $queryStringPos + strlen($this->_delimiter), strlen($path) - 1);
             $path = substr($path, 0, $queryStringPos);
         }
     }
     $route = '';
     if ($path != '' && $path != '/') {
         $route = str_replace(\Yaf\Router::URI_DELIMITER, '_', trim($path, \Yaf\Router::URI_DELIMITER));
     }
     if ($route != '') {
         if ($this->_ctlPrefer == true) {
             $request->setControllerName($route);
         } else {
             $request->setActionName($route);
         }
     }
     $params = array();
     if ($rest != null && trim($rest) != '') {
         $path = explode(\Yaf\Router::URI_DELIMITER, trim($rest, \Yaf\Router::URI_DELIMITER));
         if (($numSegs = count($path)) != 0) {
             for ($i = 0; $i < $numSegs; $i = $i + 2) {
                 $key = urldecode($path[$i]);
                 $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
                 $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val;
             }
         }
         $request->setParam($params);
     }
     return true;
 }
Ejemplo n.º 10
0
 /**
  * Processes a request and sets its controller and action.  If
  * no route was possible, default route is set.
  *
  * @param  Yaf_Request_Abstract
  *
  * @return Yaf_Request_Abstract|boolean
  */
 public function route(\Yaf\Request_Abstract $request)
 {
     $module = isset($_GET[$this->_module]) ? $_GET[$this->_module] : null;
     $controller = isset($_GET[$this->_controller]) ? $_GET[$this->_controller] : null;
     $action = isset($_GET[$this->_action]) ? $_GET[$this->_action] : null;
     if ($module == null && $controller == null && $action == null) {
         return false;
     } else {
         if ($module != null) {
             $request->setModuleName($module);
         }
         if ($controller != null) {
             $request->setControllerName($controller);
         }
         if ($action != null) {
             $request->setActionName($action);
         }
     }
     return true;
 }
Ejemplo n.º 11
0
 protected function checkAuth(Request_Abstract $request, Response_Abstract $response)
 {
     $config = ['allow' => ['api-user-create' => 1, 'api-user-login' => 1, 'api-user-recommend' => 1, 'api-user-info' => 1, 'api-group-recommend' => 1]];
     $id = strtolower($request->getModuleName() . '-' . $request->getControllerName() . '-' . $request->getActionName());
     if (!isset($config['allow'][$id])) {
         $user = new User();
         $user->id = ApiRequest::getParam('uid');
         $token = ApiRequest::getParam('token');
         if (!$user->id || TextUtil::isEmptyString($token)) {
             return new ApiResponse(Code::FAIL_PARAMETER_MISSING, 'uid or token is missing');
         }
         if ($resp = UserManager::getInstance()->getUser($user)) {
             return $resp;
         }
         if ($user->token != $token) {
             return new ApiResponse(Code::FAIL_USER_TOKEN_EXPIRE, null);
         }
         UserManager::getInstance()->setAuthorizedUser($user);
     }
     return null;
 }
Ejemplo n.º 12
0
 function dispatchLoopStartup(Request_Abstract $request, Response_Abstract $response)
 {
     $dispatcher = Dispatcher::getInstance();
     $controllerName = strtolower($request->getControllerName());
     $isApi = 0 === strpos($controllerName, 'api');
     $isCli = 0 === strpos($controllerName, 'cli_');
     // db
     \Zend_Db_Table::setDefaultAdapter(AbstractModel::getInstance()->getMultiDb()->getDefaultDb());
     // view
     if ($isApi) {
         $dispatcher->disableView();
     } else {
         if ($response instanceof Http) {
             $response->setHeader('Content-Type', 'text/html; charset=UTF-8');
         }
         $dispatcher->setView(SmartyView::getInstance());
     }
     // cli
     if ($isCli && !$request->isCli()) {
         $request->setControllerName('Error')->setActionName('error403');
     }
 }
Ejemplo n.º 13
0
 /**
  * 常量注册
  * @param \Yaf\Request_Abstract $request 请求对象
  * @param \Yaf\Response_Abstract $response 响应对象
  * @return void
  */
 public function preDispatch(Request_Abstract $request, Response_Abstract $response)
 {
     // 请求方式定义
     define('IS_AJAX', $request->isXmlHttpRequest());
     define('IS_GET', $request->isGet());
     define('IS_POST', $request->isPost());
     define('IS_PUT', $request->isPut());
     define('IS_DELETE', $request->getServer('REQUEST_METHOD') == 'DELETE');
     // 模块信息常量定义
     define('CONTROLLER_NAME', $request->getControllerName());
     define('ACTION_NAME', $request->getActionName());
     define('MODULE_NAME', $request->getModuleName());
     define('MODULE_PATH', sprintf("%smodules%s%s%s", APP_PATH, DS, $request->getModuleName(), DS));
     define('COMMON_VIEW_PATH', sprintf('%sviews%s', APP_PATH, DS));
     define('MODULE_VIEW_PATH', sprintf("%sviews%s", MODULE_PATH, DS));
     // 自定义常量定义
     foreach (new Ini(sprintf("%sconsts.ini", CONF_PATH), \YAF\ENVIRON) as $key => $value) {
         if (is_string($key) && is_string($value)) {
             define(strtoupper($key), $value);
         }
     }
 }
Ejemplo n.º 14
0
 /**
  * 构造方法
  *
  * @access public
  * @param Exception $e
  * @param \Yaf\Request_Abstract $request
  */
 public function __construct(Exception $e, Request_Abstract $request)
 {
     $this->host = @$_SERVER['SERVER_NAME'];
     $this->uri = @$_SERVER['REQUEST_URI'];
     $this->query = @$_SERVER['QUERY_STRING'];
     $this->module = $request->getModuleName();
     $this->controller = $request->getControllerName();
     $this->action = $request->getActionName();
     $this->params = '';
     if ($request->isPost()) {
         $this->params = '$_POST => ' . var_export($request->getPost(), true) . "\r\n";
     }
     if (strtolower($request->getMethod()) == 'cli' || strtolower($request->getMethod()) == 'api') {
         $this->params .= 'CLI_PARAMS => ' . var_export($request->getParams(), true);
     }
     $this->code = $e->getCode();
     $this->message = $e->getMessage();
     $this->file = $e->getFile();
     $this->line = $e->getLine();
     $this->exception = get_class($e);
     $this->datetime = date('Y-m-d H:i:s');
     $this->timestamp = time();
 }
Ejemplo n.º 15
0
 /**
  * 路由调度
  * @param \Yaf\Request_Abstract $request http请求对象
  * @return boolean TRUE表示和其他路由协议共存
  */
 public function route($request)
 {
     // 解析url信息
     $uri = $request->getRequestUri();
     $uri = explode('/', trim($request->getRequestUri(), '/'));
     $module = strtolower($uri[0]);
     // 分析url信息
     if (in_array($module, $this->modules)) {
         $this->route['module'] = $module;
         array_splice($uri, 0, 1);
     }
     if (isset($uri[0])) {
         $this->route['controller'] = $uri[0];
     }
     if (isset($uri[1])) {
         $this->route['action'] = $uri[1];
     }
     // 修改调度信息
     $request->setModuleName($this->route['module']);
     $request->setControllerName($this->route['controller']);
     $request->setActionName($this->route['action']);
     return TRUE;
 }
Ejemplo n.º 16
0
 /**
  * Processes a request and sets its controller and action based on a
  * supervar value.
  *
  * @param  Yaf_Request_Abstract
  * @return Yaf_Request_Abstract|boolean
  */
 public function route(\Yaf\Request_Abstract $request)
 {
     $requestUri = $request->getQuery($this->_varName);
     if ($requestUri == null || $requestUri == '') {
         return false;
     }
     $module = null;
     $controller = null;
     $action = null;
     $rest = null;
     $path = trim($requestUri, \Yaf\Router::URI_DELIMITER);
     if ($path != '' && $path != '/') {
         $path = explode(\Yaf\Router::URI_DELIMITER, $path);
         if (\Yaf\Application::isModuleName($path[0])) {
             $module = $path[0];
             array_shift($path);
         }
         if (count($path) && !empty($path[0])) {
             $controller = $path[0];
             array_shift($path);
         }
         if (count($path) && !empty($path[0])) {
             $action = $path[0];
             array_shift($path);
         }
         $rest = implode(\Yaf\Router::URI_DELIMITER, $path);
         $actionPrefer = \Yaf\G::iniGet('yaf.action_prefer');
         if ($module == null && $controller == null && $action == null) {
             if ($actionPrefer == true) {
                 $action = $rest;
             } else {
                 $controller = $rest;
             }
             $rest = null;
         } elseif ($module == null && $action == null && $rest == null) {
             if ($actionPrefer == true) {
                 $action = $controller;
                 $controller = null;
             }
         } elseif ($controller == null && $action == null && $rest != null) {
             $controller = $module;
             $action = $rest;
             $module = null;
             $rest = null;
         } elseif ($action == null && $rest == null) {
             $action = $controller;
             $controller = $module;
             $module = null;
         } elseif ($controller == null && $action == null) {
             $controller = $module;
             $action = $rest;
             $module = null;
             $rest = null;
         } elseif ($action == null) {
             $action = $rest;
             $rest = null;
         }
         if ($module != null) {
             $request->setModuleName($module);
         }
         if ($controller != null) {
             $request->setControllerName($controller);
         }
         if ($action != null) {
             $request->setActionName($action);
         }
         $params = array();
         if ($rest != null && trim($rest) != '') {
             $path = explode(\Yaf\Router::URI_DELIMITER, $rest);
             if (($numSegs = count($path)) != 0) {
                 for ($i = 0; $i < $numSegs; $i = $i + 2) {
                     $key = urldecode($path[$i]);
                     $val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
                     $params[$key] = isset($params[$key]) ? array_merge((array) $params[$key], array($val)) : $val;
                 }
             }
             $request->setParam($params);
         }
     }
     return true;
 }
Ejemplo n.º 17
0
 /**
  * <p><b>\Yaf\Route_Interface::route()</b> is the only method that a custom route should implement.</p><br/>
  * <p>if this method return TRUE, then the route process will be end. otherwise,\Yaf\Router will call next route in the route stack to route request.</p><br/>
  * <p>This method would set the route result to the parameter request, by calling \Yaf\Request_Abstract::setControllerName(), \Yaf\Request_Abstract::setActionName() and \Yaf\Request_Abstract::setModuleName().</p><br/>
  * <p>This method should also call \Yaf\Request_Abstract::setRouted() to make the request routed at last.</p>
  *
  * @link http://www.php.net/manual/en/yaf-route-interface.route.php
  *
  * @param \Yaf\Request_Abstract $request
  * @desc 默认module和controller均写死为Index了
  * @return bool
  */
 public function route($request)
 {
     $uris = explode('/', str_replace('-', '', $request->getRequestUri()));
     $request->setModuleName('Index');
     if (!empty($uris[1])) {
         $request->setControllerName($uris[1]);
     } else {
         $request->setControllerName('Index');
     }
     $params = array();
     $param_pairs = array_chunk(array_slice($uris, 2), 2);
     foreach ($param_pairs as $param) {
         $params[$param[0]] = empty($param[1]) ? '' : $param[1];
     }
     $request->setParam($params);
     $request->setParam($_REQUEST);
     $request->setActionName(strtolower($request->getMethod()));
     $method = $request->getParam('_method');
     if (!empty($method)) {
         $request->setActionName(strtolower($method));
     }
     //如果不return true 会继续执行static 路由规则
     return true;
 }
Ejemplo n.º 18
0
 /**
  * [logsMessageAction ]
  * @param  Request_Abstract  $request  [description]
  * @param  Response_Abstract $response [description]
  * @return [type]                      [description]
  */
 private function logsMessageAction(Request_Abstract $request, Response_Abstract $response)
 {
     $permission = Application::app()->getConfig()->get("log")->get("permission");
     if ($permission and $permission == false) {
         return;
     }
     SystemLogger::info('  Controller: ' . $request->getControllerName() . ' Action:' . $request->getActionName() . ' is called');
 }