Beispiel #1
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);
 }
Beispiel #2
0
 public function display()
 {
     $tplPath = ZPHP\Core\Config::getField('project', 'tpl_path', 'template' . DS . 'template');
     $fileName = ZPHP\ZPHP::getRootPath() . DS . $tplPath . DS . $this->tplFile;
     if (!\is_file($fileName)) {
         throw new \Exception("no file {$fileName}");
     }
     if (!empty($this->model)) {
         \extract($this->model);
     }
     include "{$fileName}";
 }
Beispiel #3
0
 public function onSend($fd, $data)
 {
     $pathInfo = $_SERVER['PATH_INFO'];
     $filePath = \ZPHP\ZPHP::getRootPath() . DS . ZConfig::getField('project', 'webroot', 'webroot') . $pathInfo;
     $mime = 'text/html';
     if (is_file($filePath)) {
         $result = file_get_contents($filePath);
         $mime = $this->getMime($filePath);
     } else {
         $result = $this->route($data, $fd);
     }
     $this->sendTo($fd, $result, $mime);
 }
Beispiel #4
0
 public static function info($type, $params = array())
 {
     $t = \date("Ymd");
     $logPath = Config::getField('project', 'log_path', '');
     if (empty($logPath)) {
         $dir = ZPHP::getRootPath() . DS . 'log' . DS . $t;
     } else {
         $dir = $logPath . DS . $t;
     }
     Dir::make($dir);
     $str = \date('Y-m-d H:i:s', Config::get('now_time', time())) . self::SEPARATOR . \implode(self::SEPARATOR, array_map('ZPHP\\Common\\Log::myJson', $params));
     $logFile = $dir . \DS . $type . '.log';
     \file_put_contents($logFile, $str . "\n", FILE_APPEND | LOCK_EX);
 }
Beispiel #5
0
 public static function info($type, $params = array())
 {
     $t = \date("Ymd");
     $logPath = Config::get('log_path', '');
     if (empty($logPath)) {
         $dir = ZPHP::getRootPath() . DS . 'log' . DS . $t;
     } else {
         $dir = $logPath . DS . $t;
     }
     Dir::make($dir);
     $str = \date('Y-m-d H:i:s', Config::get('now_time', time())) . self::SEPARATOR . \implode(self::SEPARATOR, array_map('json_encode', $params));
     $logFile = $dir . \DS . $type . '.log';
     \error_log($str . "\n", 3, $logFile);
 }
Beispiel #6
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;
 }
Beispiel #7
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 (Config::get('server_mode') == 'Http') {
         include "{$fileName}";
     } else {
         \ob_start();
         include "{$fileName}";
         $content = ob_get_contents();
         \ob_end_clean();
         return $content;
     }
 }
Beispiel #8
0
<?php

use ZPHP\ZPHP;
$config = array('server_mode' => 'Server\\WebSocket', 'project_name' => 'zwebsocket', 'app_path' => 'apps', 'ctrl_path' => 'ctrl', 'lib_path' => ZPHP::getRootPath() . DS . '..' . DS . 'lib', 'project' => array('default_ctrl_name' => 'main', 'log_path' => 'socket', 'static_url' => 'http://hs.static.45117.com/', 'app_host' => $_SERVER['HTTP_HOST']), 'socket' => array('host' => '0.0.0.0', 'port' => 8993, 'adapter' => 'Swoole', 'adapter' => 'Swoole', 'socket_type' => '\\ZPHP', 'work_mode' => 3, 'worker_num' => 1, 'client_class' => 'socket\\WebSocket', 'parse_class' => 'WebSocketChatParse', 'protocol' => 'Rpc', 'call_mode' => 'ZPHP', 'max_request' => 10000, 'dispatch_mode' => 2, 'heartbeat_idle_time' => 600, 'heartbeat_check_interval' => 610));
$publicConfig = array('connection.php', 'cache.php');
foreach ($publicConfig as $file) {
    $file = ZPHP::getRootPath() . DS . 'config' . DS . 'public' . DS . $file;
    $config += (include "{$file}");
}
return $config;
Beispiel #9
0
 public function request(array $url, $stdin = false)
 {
     $params = array('GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'REQUEST_METHOD' => 'GET', 'SCRIPT_FILENAME' => isset($url['SCRIPT_NAME']) ? $url['SCRIPT_NAME'] : ZPHP::getRootPath() . DS . 'webroot' . DS . 'main.php', 'SCRIPT_NAME' => isset($url['SCRIPT_NAME']) ? $url['SCRIPT_NAME'] : DS . 'main.php', 'DOCUMENT_URI' => isset($url['DOCUMENT_URI']) ? $url['DOCUMENT_URI'] : DS . 'main.php', 'HTTP_HOST' => isset($url['HTTP_HOST']) ? $url['HTTP_HOSTI'] : 'default', 'QUERY_STRING' => $url['query'], 'REQUEST_URI' => DS . $url['query'], 'SERVER_SOFTWARE' => 'zphp', 'REMOTE_ADDR' => '127.0.0.1', 'REMOTE_PORT' => '9985', 'SERVER_ADDR' => '127.0.0.1', 'SERVER_PORT' => '80', 'SERVER_NAME' => php_uname('n'), 'CONTENT_TYPE' => '', 'CONTENT_LENGTH' => 0, 'REQUEST_TIME' => time());
     $response = '';
     $this->connect();
     $request = $this->buildPacket(self::BEGIN_REQUEST, chr(0) . chr(self::RESPONDER) . chr((int) $this->_keepAlive) . str_repeat(chr(0), 5));
     $paramsRequest = '';
     foreach ($params as $key => $value) {
         $paramsRequest .= $this->buildNvpair($key, $value);
     }
     if ($paramsRequest) {
         $request .= $this->buildPacket(self::PARAMS, $paramsRequest);
     }
     $request .= $this->buildPacket(self::PARAMS, '');
     if ($stdin) {
         $request .= $this->buildPacket(self::STDIN, $stdin);
     }
     $request .= $this->buildPacket(self::STDIN, '');
     fwrite($this->_sock, $request);
     do {
         $resp = $this->readPacket();
         if ($resp['type'] == self::STDOUT || $resp['type'] == self::STDERR) {
             $response .= $resp['content'];
         }
     } while ($resp && $resp['type'] != self::END_REQUEST);
     if (!is_array($resp)) {
         throw new \Exception('Bad request');
     }
     switch (ord($resp['content'][4])) {
         case self::CANT_MPX_CONN:
             throw new \Exception('This app can\'t multiplex [CANT_MPX_CONN]');
             break;
         case self::OVERLOADED:
             throw new \Exception('New request rejected; too busy [OVERLOADED]');
             break;
         case self::UNKNOWN_ROLE:
             throw new \Exception('Role value not known [UNKNOWN_ROLE]');
             break;
         case self::REQUEST_COMPLETE:
             list($header, $content) = explode("\r\n\r\n", $response, 2);
             return array('header' => $header, 'content' => $content);
     }
 }
Beispiel #10
0
 private static function getTrace()
 {
     $traces = debug_backtrace();
     // only display 2 to 6 backtrace
     for ($i = 2, $n = count($traces); $i < $n && $i < 7; $i++) {
         //for ($i = 3, $n = count($traces); $i < $n; $i++){
         $trace = $traces[$i];
         if (isset($trace['type'])) {
             $callInfo = $trace['class'] . $trace['type'] . $trace['function'] . '()';
         } else {
             $callInfo = 'internal:' . $trace['function'] . '()';
         }
         if (isset($trace['file'])) {
             $fileInfo = str_replace(ZPHP::getRootPath() . '/', '', $trace['file']) . ':' . $trace['line'];
         } else {
             $fileInfo = '';
         }
         //$traces_data[] = $fileInfo . " " . $callInfo;
         $traces_data[] = $callInfo . " " . $fileInfo;
     }
     return $traces_data;
 }
Beispiel #11
0
<?php

use ZPHP\ZPHP;
use ZPHP\Socket\Adapter\Swoole;
define('TPL_PATH', ZPHP::getRootPath() . DS . 'template' . DS . 'zchat' . DS);
define('STATIC_URL', '/static/');
return array('server_mode' => 'Server\\WebSocket', 'project_name' => 'zwebsocket', 'app_path' => 'apps', 'ctrl_path' => 'ctrl', 'lib_path' => ZPHP::getRootPath() . DS . '..' . DS . 'lib', 'project' => array('default_ctrl_name' => 'main', 'log_path' => 'socket', 'static_url' => STATIC_URL, 'tpl_path' => TPL_PATH), 'socket' => array('host' => '0.0.0.0', 'port' => 8992, 'adapter' => 'Swoole', 'server_type' => Swoole::TYPE_WEBSOCKET, 'protocol' => 'Json', 'daemonize' => 1, 'client_class' => 'socket\\WebSocket', 'work_mode' => 3, 'worker_num' => 4, 'task_worker_num' => 2, 'max_request' => 0, 'debug_mode' => 0), 'pdo' => ['dsn' => 'mysql:host=localhost;port=3306', 'name' => 'cd', 'user' => 'laya_log', 'pass' => 'NXdfHu3aWfXmGMjE', 'dbname' => 'laya_log', 'charset' => 'UTF8', 'pconnect' => true, 'ping' => 1], 'route' => ['static' => ['/' => ['main', 'main']], 'dynamic' => ['/^\\/(.+)\\/(.+)$/iU' => ['{1}', '{2}', [], '/{a}/{m}']]]);
Beispiel #12
0
<?php

/**
 * Created by PhpStorm.
 * User: 王晶
 * Date: 2015/8/27
 * Time: 20:09
 */
use ZPHP\ZPHP;
use ZPHP\Socket\Adapter\Swoole;
//定义当前请求时间
define('NOW_TIME', isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time());
//定义项目根目录
if (!defined('ROOT_PATH')) {
    define('ROOT_PATH', ZPHP::getRootPath());
}
//定义项目名称
define('PROJECT_NAME', 'zmail');
//定义模版目录
define('TPL_PATH', ROOT_PATH . DS . 'template' . DS . PROJECT_NAME . DS);
//定义静态地址目录
define('STATIC_URL', '/static/');
//定义上传目录
define('UPLOAD_PATH', ROOT_PATH . DS . 'webroot' . DS . 'upload' . DS);
//项目配置
$config = ['server_mode' => 'Socket', 'project_name' => PROJECT_NAME, 'app_path' => 'apps', 'ctrl_path' => 'ctrl', 'lib_path' => ROOT_PATH . DS . '..' . DS . 'lib', 'project' => ['default_ctrl_name' => 'mail', 'default_method_name' => 'send', 'log_path' => ROOT_PATH . DS . 'log' . DS . PROJECT_NAME, 'static_url' => STATIC_URL, 'tpl_path' => TPL_PATH, 'view_mode' => 'Json', 'app_host' => empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'], 'exception_handler' => 'common\\MyException::exceptionHandler', 'fatal_handler' => 'common\\MyException::fatalHandler', 'debug_mode' => 0, 'ctrl_name' => '_act', 'method_name' => '_mod'], 'socket' => array('host' => '0.0.0.0', 'port' => 8996, 'adapter' => 'Swoole', 'server_type' => Swoole::TYPE_TCP, 'protocol' => 'Json', 'daemonize' => 1, 'client_class' => 'socket\\Tcp', 'work_mode' => 3, 'worker_num' => 4, 'task_worker_num' => 16, 'max_request' => 0, 'debug_mode' => 0, 'open_length_check' => true, 'package_length_type' => 'N', 'package_length_offset' => 0, 'package_body_offset' => 4, 'package_max_length' => 2000000), 'mail' => ['smtp_host' => 'smtp.163.com', 'smtp_port' => 25, 'username' => '*****@*****.**', 'password' => '123456', 'sendname' => 'zmail.server'], 'route' => ['static' => ['/' => ['main', 'main']], 'dynamic' => ['/^\\/(.+)\\/(.+)$/iU' => ['{1}', '{2}', [], '/{a}/{m}']]]];
return $config;
Beispiel #13
0
<?php

use ZPHP\ZPHP;
define('START_SERV_TIME', time());
$config = array('server_mode' => 'Socket', 'project_name' => 'gambleapp', 'app_path' => 'app', 'ctrl_path' => 'control', 'project' => array('language' => 'zh-cn', 'debug_mode' => 1, 'log_path' => 'log'), 'socket' => array('host' => '0.0.0.0', 'port' => 8991, 'adapter' => 'Swoole', 'daemonize' => 0, 'times' => array('rungame' => 1000, 'online' => 10000), 'work_mode' => 3, 'worker_num' => 4, 'task_worker_num' => 2, 'client_class' => 'socket\\Swoole', 'protocol' => 'Json', 'max_request' => 10000, 'heartbeat_idle_time' => 21600, 'heartbeat_check_interval' => 30, 'log_file' => dirname(dirname(__DIR__)) . '/log/swoole.log', 'dispatch_mode' => 2, 'data_eof' => "~~code~~"));
$publicConfig = array('pdo.php', 'connection.php', 'cache.php');
foreach ($publicConfig as $file) {
    $file = ZPHP::getRootPath() . DS . 'config' . DS . 'public' . DS . $file;
    $config += (include "{$file}");
}
//load exception language
if (isset($config['project']['language'])) {
    $langFile = ZPHP::getRootPath() . DS . "{$config['app_path']}" . DS . "language" . DS . "{$config['project']['language']}" . DS . "ui.php";
    $config += (include "{$langFile}");
}
//load game config
$gameConfig = array('init.php', 'item.php', 'map.php', 'reward.php', 'itemrate.php');
foreach ($gameConfig as $file) {
    $file = ZPHP::getRootPath() . DS . 'config' . DS . 'game' . DS . $file;
    $config += (include "{$file}");
}
return $config;
Beispiel #14
0
 private function getPath()
 {
     return isset($this->config['save_path']) ? $this->config['save_path'] : ZPHP::getRootPath() . DS . 'session_tmp';
 }