Пример #1
0
 /**
  * 直接 parse $_REQUEST
  * @param $_data
  * @return bool
  */
 public function parse($data)
 {
     $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $methodName = Config::getField('project', 'default_method_name', 'main');
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($data[$apn])) {
         $ctrlName = \str_replace('/', '\\', $data[$apn]);
     }
     if (isset($data[$mpn])) {
         $methodName = $data[$mpn];
     }
     if (!empty($_SERVER['PATH_INFO'])) {
         //swoole_http模式 需要在onRequest里,设置一下 $_SERVER['PATH_INFO'] = $request->server['path_info']
         $routeMap = ZRoute::match(Config::get('route', false), $_SERVER['PATH_INFO']);
         if (is_array($routeMap)) {
             $ctrlName = \str_replace('/', '\\', $routeMap[0]);
             $methodName = $routeMap[1];
             if (!empty($routeMap[2]) && is_array($routeMap[2])) {
                 //参数优先
                 $data = $data + $routeMap[2];
             }
         }
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Php'));
     return true;
 }
Пример #2
0
 public static function display($model)
 {
     if (null === $model || false === $model) {
         return $model;
     }
     if (is_array($model) && !empty($model['_view_mode'])) {
         $viewMode = $model['_view_mode'];
         unset($model['_view_mode']);
     } else {
         $viewMode = Request::getViewMode();
         if (empty($viewMode)) {
             if (Request::isAjax()) {
                 $viewMode = 'Json';
             } else {
                 $viewMode = 'Php';
             }
         }
     }
     $view = ZView::getInstance($viewMode);
     if ('Php' === $viewMode) {
         $_tpl_file = Request::getTplFile();
         if (is_array($model) && !empty($model['_tpl_file'])) {
             $_tpl_file = $model['_tpl_file'];
             unset($model['_tpl_file']);
         }
         if (empty($_tpl_file)) {
             throw new \Exception("tpl file empty");
         }
         $view->setTpl($_tpl_file);
     }
     $view->setModel($model);
     return $view->display();
 }
Пример #3
0
 public function onRequest($request, $response)
 {
     $filename = ZPHP::getRootPath() . DS . 'webroot' . $request->server['path_info'];
     if (is_file($filename)) {
         //解析静态文件
         $response->header("Content-Type", $this->getMime($filename) . '; charset=utf-8');
         $response->end(file_get_contents($filename));
         return;
     }
     $param = [];
     if (!empty($request->get)) {
         $param = $request->get;
     }
     if (!empty($request->post)) {
         $param += $request->post;
     }
     $_SERVER['HTTP_USER_AGENT'] = $request->header['user-agent'];
     Request::parse($param);
     Request::setViewMode('Php');
     Request::setHttpServer(1);
     Response::setResponse($response);
     try {
         $result = ZRoute::route();
     } catch (\Exception $e) {
         $model = Formater::exception($e);
         $model['_view_mode'] = 'Json';
         $result = Response::display($model);
     }
     $response->end($result);
     Request::setViewMode(ZConfig::getField('project', 'view_mode', 'Json'));
     Request::setHttpServer(0);
 }
Пример #4
0
 public function run()
 {
     $server = Protocol\Factory::getInstance('Cli');
     Protocol\Request::setServer($server);
     Protocol\Request::parse($_SERVER['argv']);
     return Core\Route::route();
 }
Пример #5
0
 /**
  * client包格式: writeString(json_encode(array("a"='main/main',"m"=>'main', 'k1'=>'v1')));
  * server包格式:包总长+数据(json_encode)
  * @param $_data
  * @return bool
  */
 public function parse($_data)
 {
     $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $methodName = Config::getField('project', 'default_method_name', 'main');
     $fd = Request::getFd();
     if (!empty($this->_buffer[$fd])) {
         $_data = $this->_buffer . $_data;
     }
     $packData = new MessagePacker($_data);
     $packLen = $packData->readInt();
     $dataLen = \strlen($_data);
     if ($packLen > $dataLen) {
         $this->_buffer[$fd] = $_data;
         return false;
     } elseif ($packLen < $dataLen) {
         $this->_buffer[$fd] = \substr($_data, $packLen, $dataLen - $packLen);
     } else {
         if (!empty($this->_buffer[$fd])) {
             unset($this->_buffer[$fd]);
         }
     }
     $packData->resetOffset();
     $params = $packData->readString();
     $data = \json_decode($params, true);
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($params[$apn])) {
         $ctrlName = \str_replace('/', '\\', $params[$apn]);
     }
     if (isset($params[$mpn])) {
         $methodName = $params[$mpn];
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Zpack'));
     return true;
 }
Пример #6
0
 public static function route()
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . Request::getCtrl();
     $view = null;
     try {
         $class = Factory::getInstance($action);
         if (!$class instanceof IController) {
             throw new \Exception("ctrl error");
         } else {
             $class->_before();
             $method = Request::getMethod();
             if (!method_exists($class, $method)) {
                 throw new \Exception("method error");
             }
             $view = $class->{$method}();
             $class->_after();
             if (null === $view) {
                 return null;
             }
             return Response::display($view);
         }
     } catch (\Exception $e) {
         if (Request::isLongServer()) {
             return \call_user_func(Config::getField('project', 'exception_handler', 'ZPHP\\ZPHP::exceptionHandler'), $e);
         }
         throw $e;
     }
 }
Пример #7
0
 public function onReceive()
 {
     list($serv, $fd, $fromId, $data) = func_get_args();
     if (empty($data)) {
         return;
     }
     Request::parse($data);
     $result = ZRoute::route();
     $serv->send($fd, $result);
 }
Пример #8
0
 public static function getViewMode()
 {
     if (Request::isLongServer()) {
         return ZConfig::getField('project', 'view_mode', 'Json');
     }
     if (\ZPHP\Common\Utils::isAjax()) {
         return 'Json';
     }
     return 'Php';
 }
Пример #9
0
 public static function checkTime()
 {
     if (Request::isLongServer()) {
         if (self::$nextCheckTime < time()) {
             if (self::$lastModifyTime < \filectime(self::$configPath)) {
                 self::load(self::$configPath);
             }
         }
     }
     return;
 }
Пример #10
0
 public function onTask($server, $taskId, $fromId, $data)
 {
     Request::parse($data);
     Request::addParams('taskId', $taskId . '_' . $fromId);
     try {
         ZRoute::route();
     } catch (\Exception $e) {
         $model = Formater::exception($e);
         ZLog::info('exception', $model);
     }
 }
Пример #11
0
 public function main()
 {
     $project = Config::getField('project', 'name', 'zphp');
     $data = $project . " runing!\n";
     $params = Request::getParams();
     if (!empty($params)) {
         foreach ($params as $key => $val) {
             $data .= "key:{$key}=>{$val}\n";
         }
     }
     return ['data' => ['data' => ['zphp' => $data]]];
 }
Пример #12
0
 public function display()
 {
     if (Request::isHttp()) {
         Response::header("Content-Type", "text/xml; charset=utf-8");
     }
     $data = $this->xmlEncode();
     if (Request::isLongServer()) {
         return $data;
     }
     echo $data;
     return null;
 }
Пример #13
0
 public function display()
 {
     if (Request::isHttp()) {
         Response::header('Content-Type', 'application/amf; charset=utf-8');
     }
     $data = \amf3_encode($this->model);
     if (Request::isLongServer()) {
         return $data;
     }
     echo $data;
     return null;
 }
Пример #14
0
 public function display()
 {
     $pack = new MessagePacker();
     $pack->writeString(json_encode($this->model));
     if (Request::isHttp()) {
         Response::header("Content-Type", "application/zpack; charset=utf-8");
     }
     if (Request::isLongServer()) {
         return array($this->model, $pack->getData());
     }
     echo $pack->getData();
     return null;
 }
Пример #15
0
 public static function start($sessionType, $config)
 {
     if (null !== self::$_sid) {
         return;
     }
     //判断参数里是否有sessid
     if (empty($config)) {
         $config = ZConfig::get('session');
     }
     if (!empty($config['adapter'])) {
         $sessionType = $config['adapter'];
     }
     self::$_config = $config;
     self::$_sessionType = $sessionType;
     $request = Request::getRequest();
     $sessionName = empty($config['session_name']) ? 'ZPHPSESSID' : $config['session_name'];
     $sid = null;
     if (!empty($request->cookie[$sessionName])) {
         $sid = $request->cookie[$sessionName];
     }
     if (!$sid && !empty($request->get[$sessionName])) {
         $sid = $request->get[$sessionName];
     }
     if (!$sid && !empty($request->post[$sessionName])) {
         $sid = $request->post[$sessionName];
     }
     if ($sid) {
         $handler = Factory::getInstance($sessionType, $config);
         $data = $handler->read($sid);
         if (!empty($data)) {
             $_SESSION = unserialize($data);
         } else {
             $_SESSION = array();
         }
     } else {
         $sid = sha1($request->header['user-agent'] . $request->server['remote_addr'] . uniqid(Request::getSocket()->worker_pid . '_'));
         $path = empty($config['path']) ? '/' : $config['path'];
         $domain = empty($config['domain']) ? '' : $config['domain'];
         $secure = empty($config['secure']) ? false : $config['secure'];
         $httponly = !isset($config['httponly']) ? true : $config['httponly'];
         $lifetime = 0;
         if (!empty($config['cache_expire'])) {
             $lifetime = time() + $config['cache_expire'] * 60;
         }
         Response::getResponse()->cookie($sessionName, $sid, $lifetime, $path, $domain, $secure, $httponly);
         $_SESSION = array();
     }
     self::$_sid = $sid;
 }
Пример #16
0
 /**
  * 直接 parse $_REQUEST
  * @param $data
  * @return bool
  */
 public function parse($data)
 {
     $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $methodName = Config::getField('project', 'default_method_name', 'main');
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($data[$apn])) {
         $ctrlName = \str_replace('/', '\\', $data[$apn]);
     }
     if (isset($data[$mpn])) {
         $methodName = $data[$mpn];
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Rpc'));
     return true;
 }
Пример #17
0
 public function display()
 {
     if (Request::isHttp()) {
         Response::header("Content-Type", "text/plain; charset=utf-8");
     }
     if (\is_string($this->model)) {
         $data = $this->model;
     } else {
         $data = json_encode($this->model);
     }
     if (Request::isLongServer()) {
         return $data;
     }
     echo $data;
     return null;
 }
Пример #18
0
 public function run()
 {
     $config = Config::get('socket');
     if (empty($config)) {
         throw new \Exception("socket config empty");
     }
     $socket = SFactory::getInstance($config['adapter'], $config);
     if (method_exists($socket, 'setClient')) {
         $client = CFactory::getInstance($config['client_class']);
         $socket->setClient($client);
     }
     Request::setServer(ZProtocol::getInstance(Config::getField('socket', 'protocol')));
     Request::setLongServer();
     Request::setHttpServer(0);
     $socket->run();
 }
Пример #19
0
 public function msg($toId, $msg)
 {
     $fd = Request::getFd();
     $uid = $this->getConn()->getUid($fd);
     if (empty($toId)) {
         //公共聊天
         $this->sendToChannel(common\Cmd::CHAT, array($uid, $msg, $toId));
     } else {
         //私聊
         $toInfo = $this->getConn()->get($toId);
         if (!empty($toInfo)) {
             $this->sendOne($toInfo['fd'], common\Cmd::CHAT, array($uid, $msg, $toId));
             $this->sendOne($fd, common\Cmd::CHAT, array($uid, $msg, $toId));
         }
     }
 }
Пример #20
0
 /**
  * 包格式: 包总长+命令id+请求id+数据
  * 
  * @param $_data
  * @return bool
  */
 public function parse($_data)
 {
     $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $methodName = Config::getField('project', 'default_method_name', 'main');
     if (empty($this->_cache)) {
         $this->_cache = ZCache::getInstance('Php');
     }
     $fd = Request::getFd();
     $cacheData = $this->_cache->get($fd);
     if (!empty($cacheData)) {
         $_data = $cacheData . $_data;
         $this->_cache->delete($fd);
     }
     if (empty($_data)) {
         return false;
     }
     $packData = new MessagePacker($_data);
     $packLen = $packData->readInt();
     $dataLen = \strlen($_data);
     if ($packLen > $dataLen) {
         $this->_cache->set($fd, $_data);
         return false;
     } elseif ($packLen < $dataLen) {
         $this->_cache->set($fd, \substr($_data, $packLen, $dataLen - $packLen));
     }
     $packData->resetOffset(4);
     $data = [];
     $data['_cmd'] = $packData->readInt();
     $pathinfo = Config::getField('cmdlist', $data['_cmd']);
     $data['_rid'] = $packData->readInt();
     $params = $packData->readString();
     $unpackData = \json_decode(gzdecode($params), true);
     if (!empty($unpackData) && \is_array($unpackData)) {
         $data += $unpackData;
     }
     $routeMap = ZRoute::match(Config::get('route', false), $pathinfo);
     if (is_array($routeMap)) {
         $ctrlName = $routeMap[0];
         $methodName = $routeMap[1];
         if (!empty($routeMap[2]) && is_array($routeMap[2])) {
             //参数优先
             $data = $data + $routeMap[2];
         }
     }
     Request::init($ctrlName, $methodName, $data);
     return true;
 }
Пример #21
0
 public static function end($key = 'ALL', $logName = 'debug')
 {
     $endTime = self::getMicroTime();
     $run_id = 0;
     if (self::$xhprof) {
         $xhprof_data = \xhprof_disable();
         $xhprof_runs = new \XHProfRuns_Default();
         $run_id = $xhprof_runs->save_run($xhprof_data, 'random');
     }
     $times = $endTime - self::$records[$key]['start_time'];
     $mem_use = memory_get_usage() - self::$records[$key]['memory_use'];
     unset(self::$records[$key]);
     if (empty($_SERVER['HTTP_HOST'])) {
         $_SERVER['HTTP_HOST'] = '';
     }
     Log::info($logName, array($times, self::convert($mem_use), $run_id, $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], Request::getParams()));
 }
Пример #22
0
 public function display()
 {
     if (Request::isHttp()) {
         $params = Request::getParams();
         if (isset($params['jsoncallback'])) {
             Response::header("Content-Type", 'application/x-javascript; charset=utf-8');
         } else {
             Response::header("Content-Type", "application/json; charset=utf-8");
         }
     }
     $data = \json_encode($this->model);
     if (Request::isLongServer()) {
         return $data;
     }
     echo $data;
     return null;
 }
Пример #23
0
 public function onRequest($request, $response)
 {
     $param = [];
     if (!empty($request->get)) {
         $param = $request->get;
     }
     if (!empty($request->post)) {
         $param += $request->post;
     }
     Request::parse($param);
     try {
         $result = ZRoute::route();
     } catch (\Exception $e) {
         $model = Formater::exception($e);
         $model['_view_mode'] = 'Json';
         $result = Response::display($model);
     }
     $response->end($result);
 }
Пример #24
0
 public function display()
 {
     $jsonData = \json_encode($this->model);
     $data = gzencode($jsonData);
     $pack = new MessagePacker();
     $len = strlen($data);
     $pack->writeInt($len + 16);
     $pack->writeInt($this->model['cmd']);
     $pack->writeInt($this->model['rid']);
     $pack->writeString($data, $len);
     if (Request::isHttp()) {
         Response::header("Content-Type", "application/zrpack; charset=utf-8");
     }
     if (Request::isLongServer()) {
         return array($jsonData, $pack->getData());
     }
     echo $pack->getData();
     return null;
 }
Пример #25
0
 public function onMessage($server, $frame)
 {
     if (empty($frame->finish)) {
         //数据未完
         if (empty($this->buff[$frame->fd])) {
             $this->buff[$frame->fd] = $frame->data;
         } else {
             $this->buff[$frame->fd] .= $frame->data;
         }
     } else {
         if (!empty($this->buff[$frame->fd])) {
             $frame->data = $this->buff[$frame->fd] . $frame->data;
             unset($this->buff[$frame->fd]);
         }
     }
     Request::parse($frame->data);
     $result = ZRoute::route();
     $server->push($frame->fd, $result);
 }
Пример #26
0
 public function display()
 {
     $data = \json_encode($this->model, JSON_UNESCAPED_UNICODE);
     if (Request::isHttp()) {
         $params = Request::getParams();
         $key = Config::getField('project', 'jsonp', 'jsoncallback');
         if (isset($params[$key])) {
             Response::header("Content-Type", 'application/x-javascript; charset=utf-8');
             $data = $params[$key] . '(' . $data . ')';
         } else {
             Response::header("Content-Type", "application/json; charset=utf-8");
         }
     }
     if (Request::isLongServer()) {
         return $data;
     }
     echo $data;
     return null;
 }
Пример #27
0
 public function display()
 {
     $tplPath = ZPHP\Core\Config::getField('project', 'tpl_path', ZPHP\ZPHP::getRootPath() . DS . 'template' . DS . 'default' . DS);
     $fileName = $tplPath . $this->tplFile;
     if (!\is_file($fileName)) {
         throw new \Exception("no file {$fileName}");
     }
     if (!empty($this->model)) {
         \extract($this->model);
     }
     if (ZPHP\Protocol\Request::isLongServer()) {
         \ob_start();
         include "{$fileName}";
         $content = ob_get_contents();
         \ob_end_clean();
         return $content;
     }
     include "{$fileName}";
     return null;
 }
Пример #28
0
 public function onRequest($request, $reponse)
 {
     $content = "";
     do {
         $path_info = explode("/", $request->server['path_info']);
         $ctrl = $path_info[1];
         $method = $path_info[2];
         if (isset($request->post)) {
             Protocol\Request::parse($request->post);
         } else {
             Protocol\Request::parse($request->rawContent());
         }
         Protocol\Request::setCtrl($ctrl);
         Protocol\Request::setMethod($method);
         Protocol\Request::setViewMode('Json');
         Protocol\Request::setSocket($this->serv);
         //\ob_start();
         $content = Core\Route::route();
         //$content = \ob_get_contents();
         //\ob_end_clean();
     } while (0);
     $reponse->end($content);
 }
Пример #29
0
 public function parse($_data)
 {
     $ctrlName = Config::getField('project', 'default_ctrl_name', 'main\\main');
     $methodName = Config::getField('project', 'default_method_name', 'main');
     $data = [];
     if (!empty($_data)) {
         if (is_array($_data)) {
             $data = $_data;
         } else {
             $data = \json_decode($_data, true);
         }
     }
     $apn = Config::getField('project', 'ctrl_name', 'a');
     $mpn = Config::getField('project', 'method_name', 'm');
     if (isset($data[$apn])) {
         $ctrlName = \str_replace('/', '\\', $data[$apn]);
     }
     if (isset($data[$mpn])) {
         $methodName = $data[$mpn];
     }
     Request::init($ctrlName, $methodName, $data, Config::getField('project', 'view_mode', 'Json'));
     return true;
 }
Пример #30
0
 public static function start($sessionType = '', $config = '')
 {
     if (Request::isLongServer()) {
         return Swoole::start($sessionType, $config);
     }
     if (false === self::$isStart) {
         if (empty($config)) {
             $config = ZConfig::get('session');
         }
         if (!empty($config['adapter'])) {
             $sessionType = $config['adapter'];
         }
         $lifetime = 0;
         if (!empty($config['cache_expire'])) {
             \session_cache_expire($config['cache_expire']);
             $lifetime = $config['cache_expire'] * 60;
         }
         $path = empty($config['path']) ? '/' : $config['path'];
         $domain = empty($config['domain']) ? '' : $config['domain'];
         $secure = empty($config['secure']) ? false : $config['secure'];
         $httponly = !isset($config['httponly']) ? true : $config['httponly'];
         \session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
         $sessionName = empty($config['session_name']) ? 'ZPHPSESSID' : $config['session_name'];
         \session_name($sessionName);
         if (!empty($_GET[$sessionName])) {
             \session_id($_GET[$sessionName]);
         } elseif (!empty($_SERVER[$sessionName])) {
             \session_id($_SERVER[$sessionName]);
         }
         if (!empty($sessionType)) {
             $handler = self::getInstance($sessionType, $config);
             \session_set_save_handler(array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc'));
         }
         \session_start();
         self::$isStart = true;
     }
 }