標籤:static 匯入 turn sql new let version eric add
<?php/***公用資源類****/class Common { /** * 通用的json返回資料 * $code int 返回的錯誤碼 * $msg string 返回的錯誤資訊 * $data array 返回的數組 */ public static function EchoResult($code = 0 , $msg = ‘‘ , $data = array() ){ $result = array( ‘code‘ => $code , ‘msg‘ => $msg , ‘data‘ => $data ); echo json_encode($result , JSON_UNESCAPED_UNICODE); exit; } /** * 處理form 提交的參數過濾 * $string string 需要處理的字串或者數組 * @return string 返回處理之後的字串或者數組 */ public static function Dtrim($string){ if(is_array($string)) { $keys = array_keys($string); foreach($keys as $key) { $val = $string[$key]; unset($string[$key]); $string[trim($key)] = self::Dtrim($val); } } else { $string = trim($string); } return $string; } /** * 處理form 提交的參數過濾 * $string string 需要處理的字串或者數組 * $force boolean 強制進行處理 * @return string 返回處理之後的字串或者數組 */ public static function Daddslashes($string, $force = 1) { if(is_array($string)) { $keys = array_keys($string); foreach($keys as $key) { $val = $string[$key]; unset($string[$key]); $string[addslashes($key)] = self::daddslashes($val, $force); } } else { $string = addslashes($string); } return $string; } //請參考php內建的方法 htmlspecialchars public static function Dhtmlspecialchars($string, $flags = ENT_COMPAT) { if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = self::dhtmlspecialchars($val, $flags); } } else { if($flags === null) { $string = str_replace(array(‘&‘, ‘"‘, ‘<‘, ‘>‘), array(‘&‘, ‘"‘, ‘<‘, ‘>‘), $string); if(strpos($string, ‘&#‘) !== false) { $string = preg_replace(‘/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/‘, ‘&\\1‘, $string); } } else { if(PHP_VERSION < ‘5.4.0‘) { $string = htmlspecialchars($string, $flags); } else { $charset = ‘UTF-8‘; $string = htmlspecialchars($string, $flags, $charset); } } } return $string; } //請參考 php 內建方法 stripslashes public static function Dstripslashes($string) { if(empty($string)) return $string; if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = self::dstripslashes($val); } } else { $string = stripslashes($string); } return $string; } /* * 函數名稱:VerifyId() * 函數作用:校正提交的ID類值是否合法 * 參 數:$id: 提交的ID值 * 返 回 值:返回處理後的ID * */ public static function VerifyId($id=null) { if (!$id) { return 0; } // 是否為空白判斷 elseif (self::inject_check($id)) { return 0; } // 注射判斷 elseif (!is_numeric($id)) { return 0 ; } // 數字判斷 $id = intval($id); // 整型化 return $id; } /* *檢測提交的值是不是含有SQL注射的字元,防止注射,保護伺服器安全 *參 數:$sql_str: 提交的變數 *返 回 值:返回檢測結果,ture or false */ public static function InjectCheck($sql_str) { return @eregi(‘select|insert|and|or|update|delete|\‘|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile‘, $sql_str); // 進行過濾 } /** * 匯入檔案並且進行執行個體化 ,預設匯入的是business這個檔案夾下面的檔案 * $filename string $layer 模型層名稱 * return object */ public static function ImportBusiness($name = null ){ $layer = "business"; $class=$name.ucfirst($layer); static $_obj=array(); if(isset($_obj[$name])){ return $_obj[$name]; } if(!file_exists(APP_PATH."/application/{$layer}/{$class}.php")){ exit("file {$class}.php is not exists "); } Yaf_loader::import(APP_PATH."/application/business/{$class}.php");//匯入類庫(商務邏輯類庫之類的) $_obj[$name] = new $class(); return $_obj[$name]; } /** * 匯入檔案並且進行執行個體化 ,預設匯入的是business這個檔案夾下面的檔案 * $filename string $layer 模型層名稱 * return object */ public static function ImportmodulesBusiness($name = null,$modules = null ){ $layer = "business"; $class=$name.ucfirst($layer); static $_obj=array(); if(isset($_obj[$name])){ return $_obj[$name]; } if(!file_exists(APP_PATH."/application/modules/{$modules}/{$layer}/{$class}.php")){ exit("file {$class}.php is not exists "); } Yaf_loader::import(APP_PATH."/application/modules/{$modules}/business/{$class}.php");//匯入類庫(商務邏輯類庫之類的) $_obj[$name] = new $class(); return $_obj[$name]; } }
公用資源類的封裝