123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- namespace Mall\Framework\Core;
- class Request
- {
-
- protected $params;
- static protected $instance;
- private function __construct()
- {
- }
- static public function getInstance()
- {
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
- public static function params($returnRawJson = false)
- {
- if (self::isCLI()) {
- return self::cliParams();
- } else if( strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE || strpos($_SERVER['CONTENT_TYPE'], 'text/plain') !== FALSE || strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== FALSE){
- $request_body = file_get_contents("php://input");
- if(!empty($request_body)){
- $request_body = json_decode($request_body, true);
- if(!$request_body){
- throw new \Exception("参数格式异常");
- }
- if($returnRawJson){
- return array_merge($_REQUEST, ['rawjson'=> $request_body]);
- }
- return array_merge($_REQUEST, $request_body);
- }
- return $_REQUEST;
- } else{
- return $_REQUEST;
- }
- }
-
- private static function cliParams()
- {
- $result = array();
- $params = $GLOBALS['argv'];
- array_shift($params);
- do {
- $tmpEachResult = array_shift($params);
- if (!$tmpEachResult) {
- break;
- }
- $p = $tmpEachResult;
- if ($p[0] == '-') {
- $pname = substr($p, 1);
- $value = false;
- if ($pname[0] == '-') {
- $pname = substr($pname, 1);
- if (strpos($p, '=') !== false) {
-
- list($pname, $value) = explode('=', substr($p, 2), 2);
- }
- } else {
- if (strpos($p, '=') !== false) {
-
- list($pname, $value) = explode('=', substr($p, 1), 2);
- } else if (strlen($p) > 1) {
- $pname = substr($p, 1, 1);
- $value = substr($p, 2);
- }
- }
-
- $nextparm = current($params);
- if ($value === false
- && $nextparm !== false
- && $nextparm[0] != '-'
- ) {
- $value = array_shift($params);
- }
- $result[$pname] = (string)$value;
- } else {
-
-
- }
- } while (true);
- return $result;
- }
-
- public function dispatch($enableNginxRwite = true, $enableModule = false)
- {
-
- if( !$enableNginxRwite && !self::isCLI()){
- $requestUrl = $_SERVER['REQUEST_URI'];
- if(strpos($requestUrl, '?') !== false){
- $requestUrl = substr($requestUrl, 0, strpos($requestUrl, '?'));
- }
- if(!empty($requestUrl)){
- $requestUrl = explode('/', $requestUrl);
- if(!empty($requestUrl)){
- if($enableModule){
- $_REQUEST['m'] = $requestUrl[1];
- $_REQUEST['c'] = $requestUrl[2];
- $_REQUEST['a'] = $requestUrl[3];
- if(isset($requestUrl[4])){
- $_REQUEST['request_id'] = intval($requestUrl[4]);
- }
- }else{
- $_REQUEST['c'] = $requestUrl[1];
- $_REQUEST['a'] = $requestUrl[2];
- if(isset($requestUrl[3])){
- $_REQUEST['request_id'] = intval($requestUrl[3]);
- }
- }
- }
- }
- }
- self::$instance->params = self::params();
- $controllerName = self::getController($enableModule);
- if (!class_exists($controllerName)) {
- throw new \Exception("controller: {$controllerName} not exists!");
- }
- $action = self::getAction();
- $controller = new $controllerName();
- $controller->$action();
- }
- public function getModule()
- {
- $m = self::param('m') ?: DEFAULT_CLASS_MODULE_NAME;
- return ucfirst(trim($m, '_'));
- }
- public function getModuleType()
- {
- $t = self::param('t') ?: '';
- if ($t) {
- return ucfirst(trim($t, '_'));
- }
- }
- public function getController($enableModule)
- {
- $c = self::param('c');
- $c = trim($c, '_');
-
- if (!$c) {
- $c = DEFAULT_CLASS_CONTROLLER_NAME;
- }
- $arr_class_path = array_map(function ($tmpV) {
- return ucfirst($tmpV);
- }, explode('_', $c));
- $c = join('\\', $arr_class_path);
- $NS = Config::getInstance()->get('app_name');
- $controller = "{$NS}\\Controller\\";
- if ($t = self::getModuleType()) {
- $controller .= $t . "\\";
- }
- if (($m = self::getModule()) && $enableModule) {
- $controller .= $m . "\\";
- }
- $controller .= "{$c}";
- return $controller;
- }
- public function getAction()
- {
- $action = self::param('a');
-
- if (!$action) {
- $action = DEFAULT_CLASS_ACTION_NAME;
- }
- return $action;
- }
-
- public function param($name)
- {
- if(empty($this->params)){
- $this->params = self::params();
- }
- if (isset($this->params[$name])) {
- return self::paramFilter($this->params[$name]);
- }
- return false;
- }
-
- public function getRawJson()
- {
- if(empty($this->params['rawjson'])){
- $this->params = self::params(true);
- }
- if (isset($this->params['rawjson'])) {
- return self::paramFilter($this->params['rawjson']);
- }
- return false;
- }
-
- static public function isPost()
- {
- return $_SERVER['REQUEST_METHOD'] == 'POST' ? true : false;
- }
-
- static public function isGet()
- {
- return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
- }
-
- static public function isCLI()
- {
- if (php_sapi_name() == "cli" || empty($_SERVER['PHP_SELF'])) {
- return true;
- } else {
- return false;
- }
- }
-
- static public function get_onlineip()
- {
- $onlineip = '';
- if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
- $onlineip = getenv('HTTP_CLIENT_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
- $onlineip = getenv('HTTP_X_FORWARDED_FOR');
- } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
- $onlineip = getenv('REMOTE_ADDR');
- } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
- $onlineip = $_SERVER['REMOTE_ADDR'];
- }
- return $onlineip;
- }
-
- function getServerParam($param)
- {
- if (isset($_SERVER[$param])) {
- return self::paramFilter($_SERVER[$param]);
- }
- return '';
- }
-
- private function paramFilter($param)
- {
- if (is_array($param)) {
- foreach ($param as $key => $value) {
- if(is_array($value)){
- self::paramFilter($value);
- continue;
- }
- if(!is_numeric($value)){
- $param[$key] = htmlspecialchars(trim($value), ENT_QUOTES, 'UTF-8');
- }
- }
- } else if (!empty($param) && !is_numeric($param)) {
- $param = htmlspecialchars(trim($param), ENT_QUOTES, 'UTF-8');
- }
- return $param;
- }
-
- public function file($name = '')
- {
- $files = isset($_FILES) ? $_FILES : [];
- if (is_array($name)) {
- return $files = array_merge($files, $name);
- }
- if (!empty($files)) {
-
- $array = [];
- foreach ($files as $key => $file) {
- if (is_array($file['name'])) {
- $item = [];
- $keys = array_keys($file);
- $count = count($file['name']);
- for ($i = 0; $i < $count; $i++) {
- if (empty($file['tmp_name'][$i]) || !is_file($file['tmp_name'][$i])) {
- continue;
- }
- $temp['key'] = $key;
- foreach ($keys as $_key) {
- $temp[$_key] = $file[$_key][$i];
- }
- $item[] = (new File($temp['tmp_name']))->setUploadInfo($temp);
- }
- $array[$key] = $item;
- } else {
- if ($file instanceof File) {
- $array[$key] = $file;
- } else {
- if (empty($file['tmp_name']) || !is_file($file['tmp_name'])) {
- continue;
- }
- $array[$key] = (new File($file['tmp_name']))->setUploadInfo($file);
- }
- }
- }
- if (strpos($name, '.')) {
- list($name, $sub) = explode('.', $name);
- }
- if ('' === $name) {
-
- return $array;
- } elseif (isset($sub) && isset($array[$name][$sub])) {
- return $array[$name][$sub];
- } elseif (isset($array[$name])) {
- return $array[$name];
- }
- }
- return;
- }
- }
|