/** * 遞歸過濾給定的值 * @param mixed $value 鍵值 * @param mixed $key 鍵名 * @param array $filters 過濾方法+默認值 * @return mixed */ private function filterValue(&$value, $key, $filters) {// 遞歸過濾給定的值 filterValue 傳引用 $key 貌似沒有用 過濾器 $default = array_pop($filters);// 首先 拉取 默認的 過濾器 foreach ($filters as $filter) {// 遍歷循環 過濾器 if (is_callable($filter)) {// 如果是可以調用的 // 調用函數或者方法過濾 $value = call_user_func($filter, $value);// 執行函數調用 } elseif (is_scalar($value)) {// 是否是標量 標準 的變量 if (strpos($filter, '/')) {// 如果 過濾器 含有 / 的方式 當作正則表達式 // 正則過濾 if (!preg_match($filter, $value)) {// 正則過濾 // 匹配不成功返回默認值 $value = $default; break; } } elseif (!empty($filter)) { // filter函數不存在時, 則使用filter_var進行過濾 // filter爲非整形值時, 調用filter_id取得過濾id $value = filter_var($value, is_int($filter) ? $filter : filter_id($filter));// 利用過濾器的形式過濾 // 厲害了我的哥,牛叉 if (false === $value) {// 如果 爲空 $value = $default;// 默認值 break; } } } } return $this->filterExp($value);// 執行全部檢查 返回後的結果 // 然後執行 表達式 過濾 } /** * 過濾表單中的表達式 * @param string $value * @return void */ public function filterExp(&$value) { // 過濾查詢特殊字符 if (is_string($value) && preg_match('/^(EXP|NEQ|GT|EGT|LT|ELT|OR|XOR|LIKE|NOTLIKE|NOT BETWEEN|NOTBETWEEN|BETWEEN|NOTIN|NOT IN|IN)$/i', $value)) { $value .= ' '; } // TODO 其他安全過濾 }// 過濾表單中的表達式 /** * 強制類型轉換 * @param string $data * @param string $type * @return mixed */ private function typeCast(&$data, $type) {// 強制類型轉換 switch (strtolower($type)) { // 數組 case 'a':// 執行數組 $data = (array) $data; break; // 數字 case 'd':// 數字強轉 $data = (int) $data; break; // 浮點 case 'f':// 浮點強轉換 $data = (float) $data; break; // 布爾 case 'b':// 布爾強轉 $data = (boolean) $data; break; // 字符串 case 's':// 默認是字符串 default: if (is_scalar($data)) {// 標量 $data = (string) $data;// 數據強制類型轉換 這個挺好的 } else { throw new \InvalidArgumentException('variable type error:' . gettype($data)); } } } /** * 是否存在某個請求參數 * @access public * @param string $name 變量名 * @param string $type 變量類型 * @param bool $checkEmpty 是否檢測空值 * @return mixed */ public function has($name, $type = 'param', $checkEmpty = false) {// 是否存在請求的參數 if (empty($this->$type)) {// 如果爲空 $param = $this->$type(); // 獲取參數的 } else { $param = $this->$type;// 參數 獲取 } // 按.拆分成多維數組進行判斷 foreach (explode('.', $name) as $val) {// 拆分多維數組 if (isset($param[$val])) {// 如果當前參數設置 $param = $param[$val];// 參數固定 } else { return false; } } return ($checkEmpty && '' === $param) ? false : true;// 返回數據 } /** * 獲取指定的參數 * @access public * @param string|array $name 變量名 * @param string $type 變量類型 * @return mixed */ public function only($name, $type = 'param')// 獲取指定的參數 { $param = $this->$type();// 如果參數 爲類型的 很多東西 if (is_string($name)) {// 如果是 字符串 $name = explode(',', $name);// 打散 } $item = [];// 隊列 foreach ($name as $key) {// 遍歷循環 if (isset($param[$key])) {// 如果設置了 參數 $item[$key] = $param[$key];// 參數 獲取 } } return $item;// 返回數據 } /** * 排除指定參數獲取 * @access public * @param string|array $name 變量名 * @param string $type 變量類型 * @return mixed */ public function except($name, $type = 'param') {// 排除 指定參數 $param = $this->$type();// param 參數 if (is_string($name)) {// 如果是字符串 $name = explode(',', $name);// 轉數組 } foreach ($name as $key) {// 變量數組 if (isset($param[$key])) { unset($param[$key]); } } return $param;// 返回剔除之後的數據 } /** * 當前是否ssl * @access public * @return bool */ public function isSsl() {// 是否 ssl 模式 $server = array_merge($_SERVER, $this->server);// 拼合全部相關數據 if (isset($server['HTTPS']) && ('1' == $server['HTTPS'] || 'on' == strtolower($server['HTTPS']))) { return true;// 如果 服務器 } elseif (isset($server['REQUEST_SCHEME']) && 'https' == $server['REQUEST_SCHEME']) { return true;// 如果 請求 } elseif (isset($server['SERVER_PORT']) && ('443' == $server['SERVER_PORT'])) { return true;// 如果 port 端口 } elseif (isset($server['HTTP_X_FORWARDED_PROTO']) && 'https' == $server['HTTP_X_FORWARDED_PROTO']) { return true;// 如果 https } return false; } /** * 當前是否Ajax請求 * @access public * @return bool */ public function isAjax() {// 當前 是否 ajax 請求 $value = $this->server('HTTP_X_REQUESTED_WITH'); return (!is_null($value) && strtolower($value) == 'xmlhttprequest') ? true : false; } /** * 當前是否Pjax請求 * @access public * @return bool */ public function isPjax() { return !is_null($this->server('HTTP_X_PJAX')) ? true : false; }// 神奇的 Pjax 方式 /** * 獲取客戶端IP地址 * @param integer $type 返回類型 0 返回IP地址 1 返回IPV4地址數字 * @param boolean $adv 是否進行高級模式獲取(有可能被僞裝) * @return mixed */ public function ip($type = 0, $adv = false) { $type = $type ? 1 : 0; static $ip = null; if (null !== $ip) { return $ip[$type]; } if ($adv) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown', $arr); if (false !== $pos) { unset($arr[$pos]); } $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // IP地址合法驗證 $long = sprintf("%u", ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type]; } /** * 檢測是否使用手機訪問 * @access public * @return bool */ public function isMobile() { if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) { return true; } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) { return true; } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) { return true; } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) { return true; } else { return false; } }// 如果是 手機端 // 根據訪問來路進行確認 /** * 當前URL地址中的scheme參數 * @access public * @return string */ public function scheme() { return $this->isSsl() ? 'https' : 'http'; }// 如果 https 或者 http /** * 當前請求URL地址中的query參數 * @access public * @return string */ public function query() { return $this->server('QUERY_STRING'); }// 當前請求 URL 地址中的 query 參數 /** * 當前請求的host * @access public * @return string */ public function host() { return $this->server('HTTP_HOST'); }// 當前請求的 host /** * 當前請求URL地址中的port參數 * @access public * @return integer */ public function port() { return $this->server('SERVER_PORT'); }// 端口 /** * 當前請求 SERVER_PROTOCOL * @access public * @return integer */ public function protocol() { return $this->server('SERVER_PROTOCOL'); }// server_protocol /** * 當前請求 REMOTE_PORT * @access public * @return integer */ public function remotePort() { return $this->server('REMOTE_PORT'); }// remote_port /** * 獲取當前請求的路由信息 * @access public * @param array $route 路由名稱 * @return array */ public function routeInfo($route = []) { if (!empty($route)) {// 如果爲空 $this->routeInfo = $route; } else { return $this->routeInfo; } }// 獲取路由信息 好神奇的方式 /** * 設置或者獲取當前請求的調度信息 * @access public * @param array $dispatch 調度信息 * @return array */ public function dispatch($dispatch = null) { if (!is_null($dispatch)) { $this->dispatch = $dispatch; } return $this->dispatch; }// 調度信息 踢出 /** * 設置或者獲取當前的模塊名 * @access public * @param string $module 模塊名 * @return string|$this */ public function module($module = null) { if (!is_null($module)) { $this->module = $module; return $this; } else { return $this->module ?: ''; } }// 一樣的模塊名稱 /** * 設置或者獲取當前的控制器名 * @access public * @param string $controller 控制器名 * @return string|$this */ public function controller($controller = null) { if (!is_null($controller)) { $this->controller = $controller; return $this; } else { return $this->controller ?: ''; } }// 控制器 名 /** * 設置或者獲取當前的操作名 * @access public * @param string $action 操作名 * @return string */ public function action($action = null) { if (!is_null($action)) { $this->action = $action; return $this; } else { return $this->action ?: ''; } }// 操作名 /** * 設置或者獲取當前的語言 * @access public * @param string $lang 語言名 * @return string */ public function langset($lang = null) { if (!is_null($lang)) { $this->langset = $lang; return $this; } else { return $this->langset ?: ''; } }// 語言設置 /** * 設置或者獲取當前請求的content * @access public * @return string */ public function getContent() { if (is_null($this->content)) { $this->content = file_get_contents('php://input'); } return $this->content; }// 獲取請求內容 /** * 生成請求令牌 * @access public * @param string $name 令牌名稱 * @param mixed $type 令牌生成方法 * @return string */ public function token($name = '__token__', $type = 'md5') { $type = is_callable($type) ? $type : 'md5'; $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']); if ($this->isAjax()) { header($name . ': ' . $token); } Session::set($name, $token); return $token; }// 生成控制令牌 /** * 設置當前請求綁定的對象實例 * @access public * @param string $name 綁定的對象標識 * @param mixed $obj 綁定的對象實例 * @return mixed */ public function bind($name, $obj = null) { if (is_array($name)) { $this->bind = array_merge($this->bind, $name); } else { $this->bind[$name] = $obj; } }//參數綁定 public function __set($name, $value) { $this->bind[$name] = $value; }// 魔法設置 public function __get($name) { return isset($this->bind[$name]) ? $this->bind[$name] : null; }// 魔法獲取 }
[李景山php]每天TP5-20170203|thinkphp5-Request.php-6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.