SinglePHP.class.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /**
  4. * 获取和设置配置参数 支持批量定义
  5. * 如果$key是关联型数组,则会按K-V的形式写入配置
  6. * 如果$key是数字索引数组,则返回对应的配置数组
  7. * @param string|array $key 配置变量
  8. * @param array|null $value 配置值
  9. * @return array|null
  10. */
  11. function C($key,$value=null){
  12. static $_config = array();
  13. $args = func_num_args();
  14. if($args == 1){
  15. if(is_string($key)){ //如果传入的key是字符串
  16. return isset($_config[$key])?$_config[$key]:null;
  17. }
  18. if(is_array($key)){
  19. if(array_keys($key) !== range(0, count($key) - 1)){ //如果传入的key是关联数组
  20. $_config = array_merge($_config, $key);
  21. }else{
  22. $ret = array();
  23. foreach ($key as $k) {
  24. $ret[$k] = isset($_config[$k])?$_config[$k]:null;
  25. }
  26. return $ret;
  27. }
  28. }
  29. }else{
  30. if(is_string($key)){
  31. $_config[$key] = $value;
  32. }else{
  33. halt('传入参数不正确');
  34. }
  35. }
  36. return null;
  37. }
  38. /**
  39. * 调用Widget
  40. * @param string $name widget名
  41. * @param array $data 传递给widget的变量列表,key为变量名,value为变量值
  42. * @return void
  43. */
  44. function W($name, $data = array()){
  45. $fullName = $name.'Widget';
  46. if(!class_exists($fullName)){
  47. halt('Widget '.$name.'不存在');
  48. }
  49. $widget = new $fullName();
  50. $widget->invoke($data);
  51. }
  52. /**
  53. * 获取输入参数 支持过滤和默认值
  54. * 使用方法:
  55. * <code>
  56. * I('id',0); 获取id参数 自动判断get或者post
  57. * I('post.name','','htmlspecialchars'); 获取$_POST['name']
  58. * I('get.'); 获取$_GET
  59. * </code>
  60. * @param string $name 变量的名称 支持指定类型
  61. * @param mixed $default 不存在的时候默认值
  62. * @param mixed $filter 参数过滤方法
  63. * @return mixed
  64. */
  65. function I($name,$default='',$filter=null) {
  66. if(strpos($name,'.')) { // 指定参数来源
  67. //判断参数$name中是否包括.号
  68. list($method,$name) = explode('.',$name,2);
  69. //如果包括.号将.号前后分隔,并且分别赋值给$method以及$name
  70. }else{ // 默认为自动判断
  71. //如果没有.号
  72. $method = 'param';
  73. }
  74. switch(strtolower($method)) {//将$method转换为小写
  75. //如果$method为get,则$input为$_GET
  76. case 'get' : $input =& $_GET;break;
  77. //如果$method为get,则$input为$_POST
  78. case 'post' : $input =& $_POST;break;
  79. //如果为put,则将post的原始数据转参数给$input
  80. case 'put' : parse_str(file_get_contents('php://input'), $input);break;
  81. //如果是param
  82. case 'param' :
  83. //判断$_SERVER['REQUEST_METHOD']
  84. switch($_SERVER['REQUEST_METHOD']) {
  85. //如果为post,则$input的内容为$_POST的内容
  86. case 'POST':
  87. $input = $_POST;
  88. break;
  89. //如果为PUT.则input的内容为PUT的内容
  90. case 'PUT':
  91. parse_str(file_get_contents('php://input'), $input);
  92. break;
  93. //默认为$_GET的内容
  94. default:
  95. $input = $_GET;
  96. }
  97. break;
  98. //如果$method为request,则$input为$_REQUEST
  99. case 'request' : $input =& $_REQUEST; break;
  100. //如果$method为session,则$input为$_SESSION
  101. case 'session' : $input =& $_SESSION; break;
  102. //如果$method为cookie,则$input为$_COOKIE
  103. case 'cookie' : $input =& $_COOKIE; break;
  104. //如果$method为server,则$input为$_SERVER
  105. case 'server' : $input =& $_SERVER; break;
  106. //如果$method为globals,则$input为$GLOBALS
  107. case 'globals' : $input =& $GLOBALS; break;
  108. //默认返回空
  109. default:
  110. return NULL;
  111. }
  112. /**
  113. * 到此为止,已经根据传入的参数的需要(第一个参数.号前面的),把所有的变量都取到了。下面就是返回变量的内容了。
  114. **/
  115. //如果$name为空,也就是I()第一个参数的.号后面为空的时候
  116. if(empty($name)) { // 获取全部变量
  117. //获取到的变量$input全部复制给$data
  118. $data = $input;
  119. //array_walk_recursive — 对数组中的每个成员递归地应用用户函数
  120. //将$data的键值作为filter_exp函数的第一个参数,键名作为第二个参数
  121. //如果$data的键值中含有or或者exp这两个字符,自动在后面加一个空格
  122. array_walk_recursive($data,'filter_exp');
  123. //判断过滤参数是否有,如果有的话,就直接使用过滤方法,如果没有的话,就使用配置中的过滤方法
  124. $filters = isset($filter)?$filter:C('DEFAULT_FILTER');
  125. if($filters) {
  126. $filters = explode(',',$filters);
  127. //将过滤参数中的每个方法都应用到$data中
  128. foreach($filters as $filter){
  129. //将$data的每个值使用$filters过滤
  130. $data = array_map_recursive($filter,$data); // 参数过滤
  131. }
  132. }
  133. }elseif(isset($input[$name])) { // 取值操作
  134. $data = $input[$name];
  135. is_array($data) && array_walk_recursive($data,'filter_exp');
  136. $filters = isset($filter)?$filter:C('DEFAULT_FILTER');
  137. if($filters) {
  138. $filters = explode(',',$filters);
  139. foreach($filters as $filter){
  140. if(function_exists($filter)) {
  141. $data = is_array($data)?array_map_recursive($filter,$data):$filter($data); // 参数过滤
  142. }else{
  143. $data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
  144. if(false === $data) {
  145. return isset($default)?$default:NULL;
  146. }
  147. }
  148. }
  149. }
  150. }else{ // 变量默认值
  151. $data = isset($default)?$default:NULL;
  152. }
  153. return $data;
  154. }
  155. /**
  156. * 终止程序运行
  157. * @param string $str 终止原因
  158. * @param bool $display 是否显示调用栈,默认不显示
  159. * @return void
  160. */
  161. function halt($str, $display=false){
  162. Log::fatal($str.' debug_backtrace:'.var_export(debug_backtrace(), true));
  163. header("Content-Type:text/html; charset=utf-8");
  164. if($display){
  165. echo "<pre>";
  166. debug_print_backtrace();
  167. echo "</pre>";
  168. }
  169. echo $str;
  170. exit;
  171. }
  172. /**
  173. * 获取数据库实例
  174. * @return DB
  175. */
  176. function M(){
  177. $dbConf = C(array('DB_HOST','DB_PORT','DB_USER','DB_PWD','DB_NAME','DB_CHARSET'));
  178. return DB::getInstance($dbConf);
  179. }
  180. /**
  181. * 如果文件存在就include进来
  182. * @param string $path 文件路径
  183. * @return void
  184. */
  185. function includeIfExist($path){
  186. if(file_exists($path)){
  187. include $path;
  188. }
  189. }
  190. /**
  191. * 总控类
  192. */
  193. class SinglePHP {
  194. /**
  195. * 控制器
  196. * @var string
  197. */
  198. private $c;
  199. /**
  200. * Action
  201. * @var string
  202. */
  203. private $a;
  204. /**
  205. * 单例
  206. * @var SinglePHP
  207. */
  208. private static $_instance;
  209. /**
  210. * 构造函数,初始化配置
  211. * @param array $conf
  212. */
  213. private function __construct($conf){
  214. C($conf);
  215. }
  216. private function __clone(){}
  217. /**
  218. * 获取单例
  219. * @param array $conf
  220. * @return SinglePHP
  221. */
  222. public static function getInstance($conf){
  223. if(!(self::$_instance instanceof self)){
  224. self::$_instance = new self($conf);
  225. }
  226. return self::$_instance;
  227. }
  228. /**
  229. * 运行应用实例
  230. * @access public
  231. * @return void
  232. */
  233. public function run(){
  234. if(C('USE_SESSION') == true){
  235. session_start();
  236. }
  237. C('APP_FULL_PATH', getcwd().'/'.C('APP_PATH').'/');
  238. $pathMod = C('PATH_MOD');
  239. $pathMod = empty($pathMod)?'NORMAL':$pathMod;
  240. spl_autoload_register(array('SinglePHP', 'autoload'));
  241. if(strcmp(strtoupper($pathMod),'NORMAL') === 0 || !isset($_SERVER['PATH_INFO'])){
  242. $this->c = isset($_GET['c'])?$_GET['c']:'Index';
  243. $this->a = isset($_GET['a'])?$_GET['a']:'Index';
  244. }else{
  245. $pathInfo = isset($_SERVER['PATH_INFO'])?$_SERVER['PATH_INFO']:'';
  246. $pathInfoArr = explode('/',trim($pathInfo,'/'));
  247. if(isset($pathInfoArr[0]) && $pathInfoArr[0] !== ''){
  248. $this->c = $pathInfoArr[0];
  249. }else{
  250. $this->c = 'Index';
  251. }
  252. if(isset($pathInfoArr[1])){
  253. $this->a = $pathInfoArr[1];
  254. }else{
  255. $this->a = 'Index';
  256. }
  257. }
  258. $controllerClass = $this->c.'Controller';
  259. $controller = new $controllerClass();
  260. if(!class_exists($this->c.'Controller')){
  261. //halt('控制器'.$this->c.'不存在');
  262. //halt('jsonp1({"error":"控制器'.$this->c.'不存在"})');
  263. halt('jsonp1({"error":"非法访问"})');
  264. }
  265. if($this->c == "index" || $this->c == "Index" ||$this->c == "api" || $this->c == "Api" || $this->c == "other" || $this->c == "Other"){
  266. call_user_func(array($controller,'BaseAction'),$this->a);
  267. }else{
  268. call_user_func(array($controller,$this->a.'Action'));
  269. }
  270. }
  271. /**
  272. * 自动加载函数
  273. * @param string $class 类名
  274. */
  275. public static function autoload($class){
  276. if(substr($class,-10)=='Controller'){
  277. includeIfExist(C('APP_FULL_PATH').'/Controller/'.$class.'.class.php');
  278. }elseif(substr($class,-6)=='Widget'){
  279. includeIfExist(C('APP_FULL_PATH').'/Widget/'.$class.'.class.php');
  280. }else{
  281. includeIfExist(C('APP_FULL_PATH').'/Lib/'.$class.'.class.php');
  282. }
  283. }
  284. }
  285. /**
  286. * 控制器类
  287. */
  288. class Controller {
  289. /**
  290. * 视图实例
  291. * @var View
  292. */
  293. private $_view;
  294. /**
  295. * 构造函数,初始化视图实例,调用hook
  296. */
  297. public function __construct(){
  298. $this->_view = new View();
  299. $this->_init();
  300. }
  301. /**
  302. * 前置hook
  303. */
  304. protected function _init(){}
  305. /**
  306. * 渲染模板并输出
  307. * @param null|string $tpl 模板文件路径
  308. * 参数为相对于App/View/文件的相对路径,不包含后缀名,例如index/index
  309. * 如果参数为空,则默认使用$controller/$action.php
  310. * 如果参数不包含"/",则默认使用$controller/$tpl
  311. * @return void
  312. */
  313. protected function display($tpl=''){
  314. if($tpl === ''){
  315. $trace = debug_backtrace();
  316. $controller = substr($trace[1]['class'], 0, -10);
  317. $action = substr($trace[1]['function'], 0 , -6);
  318. $tpl = $controller . '/' . $action;
  319. }elseif(strpos($tpl, '/') === false){
  320. $trace = debug_backtrace();
  321. $controller = substr($trace[1]['class'], 0, -10);
  322. $tpl = $controller . '/' . $tpl;
  323. }
  324. $this->_view->display($tpl);
  325. }
  326. /**
  327. * 为视图引擎设置一个模板变量
  328. * @param string $name 要在模板中使用的变量名
  329. * @param mixed $value 模板中该变量名对应的值
  330. * @return void
  331. */
  332. protected function assign($name,$value){
  333. $this->_view->assign($name,$value);
  334. }
  335. /**
  336. * 将数据用json格式输出至浏览器,并停止执行代码
  337. * @param array $data 要输出的数据
  338. */
  339. protected function ajaxReturn($data){
  340. echo json_encode($data);
  341. exit;
  342. }
  343. /**
  344. * 重定向至指定url
  345. * @param string $url 要跳转的url
  346. * @param void
  347. */
  348. protected function redirect($url){
  349. header("Location: $url");
  350. exit;
  351. }
  352. }
  353. /**
  354. * 视图类
  355. */
  356. class View {
  357. /**
  358. * 视图文件目录
  359. * @var string
  360. */
  361. private $_tplDir;
  362. /**
  363. * 视图文件路径
  364. * @var string
  365. */
  366. private $_viewPath;
  367. /**
  368. * 视图变量列表
  369. * @var array
  370. */
  371. private $_data = array();
  372. /**
  373. * 给tplInclude用的变量列表
  374. * @var array
  375. */
  376. private static $tmpData;
  377. /**
  378. * @param string $tplDir
  379. */
  380. public function __construct($tplDir=''){
  381. if($tplDir == ''){
  382. $this->_tplDir = './'.C('APP_PATH').'/View/';
  383. }else{
  384. $this->_tplDir = $tplDir;
  385. }
  386. }
  387. /**
  388. * 为视图引擎设置一个模板变量
  389. * @param string $key 要在模板中使用的变量名
  390. * @param mixed $value 模板中该变量名对应的值
  391. * @return void
  392. */
  393. public function assign($key, $value) {
  394. $this->_data[$key] = $value;
  395. }
  396. /**
  397. * 渲染模板并输出
  398. * @param null|string $tplFile 模板文件路径,相对于App/View/文件的相对路径,不包含后缀名,例如index/index
  399. * @return void
  400. */
  401. public function display($tplFile) {
  402. $this->_viewPath = $this->_tplDir . $tplFile . '.php';
  403. unset($tplFile);
  404. extract($this->_data);
  405. include $this->_viewPath;
  406. }
  407. /**
  408. * 用于在模板文件中包含其他模板
  409. * @param string $path 相对于View目录的路径
  410. * @param array $data 传递给子模板的变量列表,key为变量名,value为变量值
  411. * @return void
  412. */
  413. public static function tplInclude($path, $data=array()){
  414. self::$tmpData = array(
  415. 'path' => C('APP_FULL_PATH') . '/View/' . $path . '.php',
  416. 'data' => $data,
  417. );
  418. unset($path);
  419. unset($data);
  420. extract(self::$tmpData['data']);
  421. include self::$tmpData['path'];
  422. }
  423. }
  424. /**
  425. * Widget类
  426. * 使用时需继承此类,重写invoke方法,并在invoke方法中调用display
  427. */
  428. class Widget {
  429. /**
  430. * 视图实例
  431. * @var View
  432. */
  433. protected $_view;
  434. /**
  435. * Widget名
  436. * @var string
  437. */
  438. protected $_widgetName;
  439. /**
  440. * 构造函数,初始化视图实例
  441. */
  442. public function __construct(){
  443. $this->_widgetName = get_class($this);
  444. $dir = C('APP_FULL_PATH') . '/Widget/Tpl/';
  445. $this->_view = new View($dir);
  446. }
  447. /**
  448. * 处理逻辑
  449. * @param mixed $data 参数
  450. */
  451. public function invoke($data){}
  452. /**
  453. * 渲染模板
  454. * @param string $tpl 模板路径,如果为空则用类名作为模板名
  455. */
  456. protected function display($tpl=''){
  457. if($tpl == ''){
  458. $tpl = $this->_widgetName;
  459. }
  460. $this->_view->display($tpl);
  461. }
  462. /**
  463. * 为视图引擎设置一个模板变量
  464. * @param string $name 要在模板中使用的变量名
  465. * @param mixed $value 模板中该变量名对应的值
  466. * @return void
  467. */
  468. protected function assign($name,$value){
  469. $this->_view->assign($name,$value);
  470. }
  471. }
  472. /**
  473. * 数据库操作类
  474. * 使用方法:
  475. * DB::getInstance($conf)->query('select * from table');
  476. * 其中$conf是一个关联数组,需要包含以下key:
  477. * DB_HOST DB_USER DB_PWD DB_NAME
  478. * 可以用DB_PORT和DB_CHARSET来指定端口和编码,默认3306和utf8
  479. */
  480. class DB {
  481. /**
  482. * 数据库链接
  483. * @var resource
  484. */
  485. private $_db;
  486. /**
  487. * 保存最后一条sql
  488. * @var string
  489. */
  490. private $_lastSql;
  491. /**
  492. * 上次sql语句影响的行数
  493. * @var int
  494. */
  495. private $_rows;
  496. /**
  497. * 上次sql执行的错误
  498. * @var string
  499. */
  500. private $_error;
  501. /**
  502. * 实例数组
  503. * @var array
  504. */
  505. private static $_instance = array();
  506. /**
  507. * 构造函数
  508. * @param array $dbConf 配置数组
  509. */
  510. private function __construct($dbConf){
  511. if(!isset($dbConf['DB_CHARSET'])){
  512. $dbConf['DB_CHARSET'] = 'utf8';
  513. }
  514. $this->_db = mysql_connect($dbConf['DB_HOST'].':'.$dbConf['DB_PORT'],$dbConf['DB_USER'],$dbConf['DB_PWD']);
  515. if($this->_db === false){
  516. halt(mysql_error());
  517. }
  518. $selectDb = mysql_select_db($dbConf['DB_NAME'],$this->_db);
  519. if($selectDb === false){
  520. halt(mysql_error());
  521. }
  522. mysql_set_charset($dbConf['DB_CHARSET']);
  523. }
  524. private function __clone(){}
  525. /**
  526. * 获取DB类
  527. * @param array $dbConf 配置数组
  528. * @return DB
  529. */
  530. static public function getInstance($dbConf){
  531. if(!isset($dbConf['DB_PORT'])){
  532. $dbConf['DB_PORT'] = '3306';
  533. }
  534. $key = $dbConf['DB_HOST'].':'.$dbConf['DB_PORT'];
  535. if(!isset(self::$_instance[$key]) || !(self::$_instance[$key] instanceof self)){
  536. self::$_instance[$key] = new self($dbConf);
  537. }
  538. return self::$_instance[$key];
  539. }
  540. /**
  541. * 转义字符串
  542. * @param string $str 要转义的字符串
  543. * @return string 转义后的字符串
  544. */
  545. public function escape($str){
  546. return mysql_real_escape_string($str, $this->_db);
  547. }
  548. /**
  549. * 查询,用于select语句
  550. * @param string $sql 要查询的sql
  551. * @return bool|array 查询成功返回对应数组,失败返回false
  552. */
  553. public function query($sql){
  554. $this->_rows = 0;
  555. $this->_error = '';
  556. $this->_lastSql = $sql;
  557. $this->logSql();
  558. $res = mysql_query($sql,$this->_db);
  559. if($res === false){
  560. $this->_error = mysql_error($this->_db);
  561. $this->logError();
  562. return false;
  563. }else{
  564. $this->_rows = mysql_num_rows($res);
  565. $result = array();
  566. if($this->_rows >0) {
  567. while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
  568. $result[] = $row;
  569. }
  570. mysql_data_seek($res,0);
  571. }
  572. return $result;
  573. }
  574. }
  575. /**
  576. * 查询,用于insert/update/delete语句
  577. * @param string $sql 要查询的sql
  578. * @return bool|int 查询成功返回影响的记录数量,失败返回false
  579. */
  580. public function execute($sql) {
  581. $this->_rows = 0;
  582. $this->_error = '';
  583. $this->_lastSql = $sql;
  584. $this->logSql();
  585. $result = mysql_query($sql, $this->_db) ;
  586. if ( false === $result) {
  587. $this->_error = mysql_error($this->_db);
  588. $this->logError();
  589. return false;
  590. } else {
  591. $this->_rows = mysql_affected_rows($this->_db);
  592. return $this->_rows;
  593. }
  594. }
  595. /**
  596. * 查询,用于insert/update/delete语句
  597. * @param string $sql 要查询的sql
  598. * @return bool|int 查询成功返回影响的记录数量,失败返回false
  599. */
  600. public function executeinsert($sql) {
  601. $this->_rows = 0;
  602. $this->_error = '';
  603. $this->_lastSql = $sql;
  604. $this->logSql();
  605. $result = mysql_query($sql, $this->_db) ;
  606. if ( false === $result) {
  607. $this->_error = mysql_error($this->_db);
  608. $this->logError();
  609. return false;
  610. } else {
  611. $this->_rows = mysql_insert_id($this->_db);
  612. return $this->_rows;
  613. }
  614. }
  615. /**
  616. * 获取上一次查询影响的记录数量
  617. * @return int 影响的记录数量
  618. */
  619. public function getRows(){
  620. return $this->_rows;
  621. }
  622. /**
  623. * 获取上一次insert后生成的自增id
  624. * @return int 自增ID
  625. */
  626. public function getInsertId() {
  627. return mysql_insert_id($this->_db);
  628. }
  629. /**
  630. * 获取上一次查询的sql
  631. * @return string sql
  632. */
  633. public function getLastSql(){
  634. return $this->_lastSql;
  635. }
  636. /**
  637. * 获取上一次查询的错误信息
  638. * @return string 错误信息
  639. */
  640. public function getError(){
  641. return $this->_error;
  642. }
  643. /**
  644. * 记录sql到文件
  645. */
  646. private function logSql(){
  647. Log::sql($this->_lastSql);
  648. }
  649. /**
  650. * 记录错误日志到文件
  651. */
  652. private function logError(){
  653. $str = '[SQL ERR]'.$this->_error.' SQL:'.$this->_lastSql;
  654. Log::warn($str);
  655. }
  656. }
  657. /**
  658. * 日志类
  659. * 使用方法:Log::fatal('error msg');
  660. * 保存路径为 App/Log,按天存放
  661. * fatal和warning会记录在.log.wf文件中
  662. */
  663. class Log{
  664. /**
  665. * 打日志,支持SAE环境
  666. * @param string $msg 日志内容
  667. * @param string $level 日志等级
  668. * @param bool $wf 是否为错误日志
  669. */
  670. public static function write($msg, $level='DEBUG', $wf=false,$file){
  671. if(function_exists('sae_debug')){ //如果是SAE,则使用sae_debug函数打日志
  672. $msg = "[{$level}]".$msg;
  673. sae_set_display_errors(false);
  674. sae_debug(trim($msg));
  675. sae_set_display_errors(true);
  676. }else{
  677. $msg = date('[ Y-m-d H:i:s ]')."[{$level}]".$msg."\r\n";
  678. $logPath = C('APP_FULL_PATH').'/Log/'.date('Ymd').$file.'.log';
  679. if($wf){
  680. $logPath .= '.wf';
  681. }
  682. file_put_contents($logPath, $msg, FILE_APPEND);
  683. }
  684. }
  685. /**
  686. * 打印fatal日志
  687. * @param string $msg 日志信息
  688. */
  689. public static function fatal($msg){
  690. $msg = iconv("utf-8","gb2312",$msg);
  691. self::write($msg, 'FATAL', true);
  692. }
  693. /**
  694. * 打印warning日志
  695. * @param string $msg 日志信息
  696. */
  697. public static function warn($msg){
  698. $msg = iconv("utf-8","gb2312",$msg);
  699. self::write($msg, 'WARN', true);
  700. }
  701. /**
  702. * 打印notice日志
  703. * @param string $msg 日志信息
  704. */
  705. public static function notice($msg){
  706. $msg = iconv("utf-8","gb2312",$msg);
  707. self::write($msg, 'NOTICE');
  708. }
  709. /**
  710. * 打印gethis日志
  711. * @param string $msg 日志信息
  712. */
  713. public static function gethis($msg){
  714. $msg = iconv("utf-8","gb2312",$msg);
  715. self::write($msg, 'GETHIS');
  716. }
  717. /**
  718. * 打印gethis日志
  719. * @param string $msg 日志信息
  720. */
  721. public static function posthis($msg){
  722. $msg = iconv("utf-8","gb2312",$msg);
  723. self::write($msg, 'POSTHIS');
  724. }
  725. /**
  726. * 打印gethis日志
  727. * @param string $msg 日志信息
  728. */
  729. public static function soaphis($msg,$file){
  730. //$msg = iconv("utf-8","GB2312//IGNORE",$msg);
  731. //$msg = mb_convert_encoding($msg, "GB2312","UTF-8");
  732. $msg = iconv("utf-8","gb2312",$msg);
  733. self::write($msg, 'SOAPHIS',false,$file);
  734. }
  735. /**
  736. * 打印xmlerr日志
  737. * @param string $msg 日志信息
  738. */
  739. public static function xmlerr($msg){
  740. self::write($msg, 'XMLERR');
  741. }
  742. /**
  743. * 打印xmltc日志
  744. * @param string $msg 日志信息
  745. */
  746. public static function xmltc($msg){
  747. self::write($msg, 'XMLTC');
  748. }
  749. /**
  750. * 打印syncdberr日志
  751. * @param string $msg 日志信息
  752. */
  753. public static function syncdberr($msg){
  754. self::write($msg, 'SYNCDBERR');
  755. }
  756. /**
  757. * 打印cur_err日志
  758. * @param string $msg 日志信息
  759. */
  760. public static function cur_err($msg){
  761. self::write($msg, 'CURERR');
  762. }
  763. /**
  764. * 打印返回前台JSON日志
  765. * @param string $msg 日志信息
  766. */
  767. public static function json($msg){
  768. $msg = iconv("utf-8","gb2312",$msg);
  769. self::write($msg, 'JSON');
  770. }
  771. /**
  772. * 打印debug日志
  773. * @param string $msg 日志信息
  774. */
  775. public static function debug($msg){
  776. $msg = iconv("utf-8","gb2312",$msg);
  777. self::write($msg, 'DEBUG');
  778. }
  779. /**
  780. * 打印sql日志
  781. * @param string $msg 日志信息
  782. */
  783. public static function sql($msg){
  784. $msg = iconv("utf-8","gb2312",$msg);
  785. self::write($msg, 'SQL');
  786. }
  787. /**
  788. * 打印getpost日志
  789. * @param string $msg 日志信息
  790. */
  791. public static function getpost($msg){
  792. self::write($msg, 'GETPOST');
  793. }
  794. }
  795. /**
  796. * ExtException类,记录额外的异常信息
  797. */
  798. class ExtException extends Exception{
  799. /**
  800. * @var array
  801. */
  802. protected $extra;
  803. /**
  804. * @param string $message
  805. * @param array $extra
  806. * @param int $code
  807. * @param null $previous
  808. */
  809. public function __construct($message = "", $extra = array(), $code = 0, $previous = null){
  810. $this->extra = $extra;
  811. parent::__construct($message, $code, $previous);
  812. }
  813. /**
  814. * 获取额外的异常信息
  815. * @return array
  816. */
  817. public function getExtra(){
  818. return $this->extra;
  819. }
  820. }