6c038b9a7ecba42f0388001e0c6f719cf1c288b9.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. class Maze extends Maze\Config\Load
  3. {
  4. /**
  5. * 注册maze类库里常用的方法,用法为Maze:path('');目前仅支持三个参数以下的方法
  6. */
  7. static private $register = array
  8. (
  9. 'path' => array('File\\Path','create'),
  10. 'page' => array('Page\\Main','getPage'),
  11. 'total' => array('Page\\Main','getTotal'),
  12. 'url' => array('Http\\Url','get'),
  13. 'location' => array('Http\\Url','location'),
  14. 'input' => array('Http\\Input','get'),
  15. 'preInput' => array('Http\\Input','prefix'),
  16. 'setInput' => array('Http\\Input','set'),
  17. 'abert' => array('Http\\Output','abert'),
  18. 'out' => array('Http\\Output','result'),
  19. 'export' => array('Http\\Output','export'),
  20. 'load' => array('Routing\\Load','get'),
  21. 'object' => array('Routing\\Load','object'),
  22. 'config' => array('Config\\Load','get'),
  23. 'project' => array('Config\\Project','load'),
  24. 'debug' => array('Debug\\Process','wait'),
  25. 'view' => array('Template\\View','get'),
  26. );
  27. /**
  28. * 设置循环里每行不同的值
  29. * @param string $value
  30. * @param string $index
  31. *
  32. * @return array
  33. */
  34. static public function lace($value, $index, $key = 2, $num = 0)
  35. {
  36. $value = explode(',', $value);
  37. if(!isset($value[1]))
  38. {
  39. $value[1] = '';
  40. }
  41. if($index > 0 && $index%$key == $num)
  42. {
  43. return $value[0];
  44. }
  45. else
  46. {
  47. return $value[1];
  48. }
  49. }
  50. /**
  51. * 设置循环里最新一条数据的值
  52. * @param string $value
  53. * @param string $index
  54. *
  55. * @return array
  56. */
  57. static public function first($value, $index)
  58. {
  59. if($index == 0)
  60. {
  61. return $value;
  62. }
  63. }
  64. /**
  65. * 设置循环里最后一条数据的值
  66. * @param string $value
  67. * @param string $index
  68. * @param string $total
  69. *
  70. * @return array
  71. */
  72. static public function last($value, $index, $total)
  73. {
  74. if($index == $total)
  75. {
  76. return $value;
  77. }
  78. }
  79. /**
  80. * 获取assets的文件网络路径
  81. * @param string $value
  82. * @param string $type
  83. *
  84. * @return array
  85. */
  86. static public function assets($value, $type = 'css')
  87. {
  88. return parent::$global['host'][$type] . $value;
  89. }
  90. /**
  91. * 生成table
  92. * @param string $thead
  93. * @param string $tbody
  94. * @param string $class
  95. *
  96. * @return array
  97. *
  98. static public function table($thead, $tbody, $class = '')
  99. {
  100. $result = '<table class='.$class.'><tr>';
  101. foreach($thead as $k => $v)
  102. {
  103. $result .= '<td style=\'width:50%\'>' . $v . '</td>';
  104. }
  105. $result .= '</tr>';
  106. foreach($tbody as $k => $v)
  107. {
  108. $result .= '<tr><td>' . $k . '</td><td>' . $v . '</td></tr>';
  109. }
  110. $result .= '</table>';
  111. return $result;
  112. }
  113. /**
  114. * 生成table
  115. * @param string $data
  116. * @param string $class
  117. *
  118. * @return array
  119. */
  120. static public function table($data, $class = '')
  121. {
  122. if($class)
  123. {
  124. $style = 'class='.$class.'';
  125. }
  126. else
  127. {
  128. $style = 'border=1 width=100% height=100%';
  129. }
  130. $html = '<table '.$style.'>';
  131. foreach($data as $k => $v)
  132. {
  133. if(is_array($v))
  134. {
  135. $v = self::table($v, $class);
  136. //$v = var_export($v, true);
  137. }
  138. $html .= '<tr><td style=width:30%;line-height:1.75;>'.$k.'</td><td style=word-break:break-all;word-wrap:break-word;>'.$v.'</td></tr>';
  139. }
  140. $html .= '</table>';
  141. return $html;
  142. }
  143. /**
  144. * 获取pic
  145. * @param string $value
  146. * @param string $index
  147. *
  148. * @return array
  149. */
  150. static public function pic($pic, $name)
  151. {
  152. if(strpos($pic, '_t') !== false)
  153. {
  154. $temp = explode('_', $pic);
  155. $pic = $temp[0] . '_' . $name . '.jpg';
  156. }
  157. return $pic;
  158. }
  159. /**
  160. * 获取_param
  161. * @param string $value
  162. * @param string $index
  163. *
  164. * @return array
  165. */
  166. static public function param($name)
  167. {
  168. if(isset(Maze::$global['base']['_param']))
  169. {
  170. if(isset(Maze::$global['base']['_param']['add_' . $name]))
  171. {
  172. return Maze::$global['base']['_param']['add_' . $name];
  173. }
  174. if(isset(Maze::$global['base']['_param']['set_' . $name]))
  175. {
  176. return Maze::$global['base']['_param']['set_' . $name];
  177. }
  178. }
  179. return false;
  180. }
  181. /**
  182. * 获取ip
  183. *
  184. * @return string
  185. */
  186. static public function ip()
  187. {
  188. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
  189. {
  190. $ip = getenv('HTTP_CLIENT_IP');
  191. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
  192. {
  193. $ip = getenv('HTTP_X_FORWARDED_FOR');
  194. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
  195. {
  196. $ip = getenv('REMOTE_ADDR');
  197. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
  198. {
  199. $ip = $_SERVER['REMOTE_ADDR'];
  200. }
  201. return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
  202. }
  203. /**
  204. * 获取ua
  205. *
  206. * @return string
  207. */
  208. static public function ua()
  209. {
  210. return $_SERVER['HTTP_USER_AGENT'];
  211. }
  212. /**
  213. * 运维程序
  214. *
  215. * @return string
  216. */
  217. static public function daemon($interface, $project = false)
  218. {
  219. if(strpos($interface, 'http://') !== false)
  220. {
  221. system('curl "' . $interface . '" > /dev/null &');
  222. return;
  223. }
  224. $project = $project ? $project : MAZE_PROJECT_NAME;
  225. $path = self::load('manage/project.path', $project);
  226. if(strpos($path, 'http://') !== false)
  227. {
  228. //self::curl($path . $interface);
  229. system('curl "' . $path . $interface . '" > /dev/null &');
  230. }
  231. else
  232. {
  233. # ?和&无法解析
  234. $interface = str_replace(array('?', '&'), array('__', '^'), $interface);
  235. system('php ' . $path . 'index.php -send ' . $interface . ' > /dev/null &');
  236. }
  237. }
  238. static public function mdate($num)
  239. {
  240. if($num <= 0)
  241. {
  242. return '0S';
  243. }
  244. $num = time() - $num;
  245. //$num = 2*3600*24 + 1000;
  246. if($num <= 60)
  247. {
  248. return $num . 'S';
  249. }
  250. $config = array
  251. (
  252. //array(3600*24, 'D'),
  253. array(3600, 'H'),
  254. array(60, 'M'),
  255. array(1, 'S')
  256. );
  257. $result = '';
  258. foreach($config as $k => $v)
  259. {
  260. if($num > $v[0])
  261. {
  262. $value = intval($num/$v[0]);
  263. $num = $num - $v[0]*$value;
  264. $result .= $value . $v[1] . ' ';
  265. }
  266. }
  267. return $result;
  268. }
  269. /**
  270. * 加入到cron中
  271. *
  272. * @return string
  273. */
  274. static public function cron($name, $ldate, $interface, $time = 0, $project = false, $update = true)
  275. {
  276. if($ldate > 0)
  277. {
  278. $ldate = date('Y-m-d H:i:s', $ldate);
  279. }
  280. $info = Maze::load('manage/cron-getOne', array('where_project' => $project, 'where_interface' => $interface));
  281. if($info)
  282. {
  283. if($update)
  284. {
  285. $update = array();
  286. $update['set_name'] = $name;
  287. $update['set_ldate'] = $ldate;
  288. $update['set_interface'] = $interface;
  289. $update['set_time'] = $time;
  290. $update['set_project'] = $project;
  291. $update['set_state'] = 1;
  292. $update['where_id'] = $info['id'];
  293. Maze::load('manage/cron-update', $update);
  294. }
  295. }
  296. else
  297. {
  298. $update['add_name'] = $name;
  299. $update['add_ldate'] = $ldate;
  300. $update['add_interface'] = $interface;
  301. $update['add_time'] = $time;
  302. $update['add_project'] = $project;
  303. Maze::load('manage/cron-insert', $update);
  304. }
  305. }
  306. /**
  307. * 字符串截取
  308. * @param string $string
  309. * @param string $length
  310. * @param string $etc
  311. *
  312. * @return array
  313. */
  314. static public function cut($string, $length = 80, $etc = '...')
  315. {
  316. $result = '';
  317. $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
  318. for($i = 0, $j = 0; $i < strlen($string); $i++)
  319. {
  320. if($j >= $length)
  321. {
  322. for($x = 0, $y = 0; $x < strlen($etc); $x++)
  323. {
  324. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  325. {
  326. $x += $number - 1;
  327. $y++;
  328. }
  329. else
  330. {
  331. $y += 0.5;
  332. }
  333. }
  334. $length -= $y;
  335. break;
  336. }
  337. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  338. {
  339. $i += $number - 1;
  340. $j++;
  341. }
  342. else
  343. {
  344. $j += 0.5;
  345. }
  346. }
  347. for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
  348. {
  349. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  350. {
  351. if($length < 1.0)
  352. {
  353. break;
  354. }
  355. $result .= substr($string, $i, $number);
  356. $length -= 1.0;
  357. $i += $number - 1;
  358. }
  359. else
  360. {
  361. $result .= substr($string, $i, 1);
  362. $length -= 0.5;
  363. }
  364. }
  365. $result = htmlentities($result, ENT_QUOTES, 'utf-8');
  366. if($i < strlen($string))
  367. {
  368. $result .= $etc;
  369. }
  370. return $result;
  371. }
  372. /**
  373. * 执行hack的值
  374. * @param string $value
  375. * @param string $index
  376. * @param string $total
  377. *
  378. * @return array
  379. */
  380. static public function hack($key)
  381. {
  382. if(isset(parent::$global['hack'][$key]))
  383. {
  384. return parent::$global['hack'][$key];
  385. }
  386. }
  387. /**
  388. * 注册maze类库里的常用类,方便使用
  389. * @param string $name
  390. * @param array $param
  391. *
  392. * @return mixed
  393. */
  394. static public function __callStatic($name, $param = array())
  395. {
  396. if(isset(self::$register[$name]))
  397. {
  398. $class = 'Maze\\' . self::$register[$name][0];
  399. $method = self::$register[$name][1];
  400. if(isset($param[2]))
  401. {
  402. return $class::$method($param[0], $param[1], $param[2]);
  403. }
  404. elseif(isset($param[1]))
  405. {
  406. return $class::$method($param[0], $param[1]);
  407. }
  408. elseif(isset($param[0]))
  409. {
  410. return $class::$method($param[0]);
  411. }
  412. else
  413. {
  414. return $class::$method();
  415. }
  416. }
  417. }
  418. static public function curl($url, $param = false, $type = '')
  419. {
  420. //初始化
  421. $ch = curl_init();
  422. //设置选项,包括URL
  423. curl_setopt($ch, CURLOPT_URL, $url);
  424. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  425. curl_setopt($ch, CURLOPT_HEADER, 0);
  426. if($param)
  427. {
  428. // post数据
  429. curl_setopt($ch, CURLOPT_POST, 1);
  430. if($type == 'json')
  431. {
  432. # 中文不用unicode编码
  433. $string = str_replace("\\/", "/", json_encode($param));
  434. $search = "#\\\u([0-9a-f]+)#ie";
  435. if(strpos(strtoupper(PHP_OS), 'WIN') === false)
  436. {
  437. $replace = "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))";//LINUX
  438. }
  439. else
  440. {
  441. $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";//WINDOWS
  442. }
  443. $param = preg_replace($search, $replace, $string);
  444. //$param = json_encode($param, JSON_UNESCAPED_UNICODE);
  445. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  446. 'Content-Type: application/json',
  447. 'Content-Length: ' . strlen($param))
  448. );
  449. }
  450. //print_r($param);die;
  451. // post的变量
  452. @curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  453. }
  454. //执行并获取HTML文档内容
  455. $output = curl_exec($ch);
  456. //释放curl句柄
  457. curl_close($ch);
  458. //打印获得的数据
  459. return $output;
  460. }
  461. static public function weixin()
  462. {
  463. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  464. {
  465. return true;
  466. }
  467. return false;
  468. }
  469. static public function mobile()
  470. {
  471. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  472. $mobile_browser = '0';
  473. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
  474. {
  475. $mobile_browser++;
  476. }
  477. if((isset($_SERVER['HTTP_ACCEPT'])) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
  478. {
  479. $mobile_browser++;
  480. }
  481. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
  482. {
  483. $mobile_browser++;
  484. }
  485. if(isset($_SERVER['HTTP_PROFILE']))
  486. {
  487. $mobile_browser++;
  488. }
  489. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  490. $mobile_agents = array(
  491. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  492. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  493. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  494. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  495. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  496. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  497. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  498. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  499. 'wapr','webc','winw','winw','xda','xda-'
  500. );
  501. if(in_array($mobile_ua, $mobile_agents))
  502. {
  503. $mobile_browser++;
  504. }
  505. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
  506. {
  507. $mobile_browser++;
  508. }
  509. // Pre-final check to reset everything if the user is on Windows
  510. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
  511. {
  512. $mobile_browser=0;
  513. }
  514. // But WP7 is also Windows, with a slightly different characteristic
  515. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
  516. {
  517. $mobile_browser++;
  518. }
  519. if($mobile_browser>0)
  520. {
  521. return true;
  522. }
  523. else
  524. {
  525. return false;
  526. }
  527. }
  528. static public function maketime($v)
  529. {
  530. if(!$v) return '';
  531. if(is_numeric($v)) return $v;
  532. if(is_array($v))
  533. {
  534. $v = $v[1];
  535. }
  536. if(strstr($v, ' '))
  537. {
  538. $t = explode(' ', $v);
  539. $v = $t[0];
  540. $s = explode(':', $t[1]);
  541. }
  542. else
  543. {
  544. $s = array(0,0,0);
  545. }
  546. if(strstr($v, '-'))
  547. {
  548. $t = explode('-', $v);
  549. }
  550. elseif(strstr($v, '/'))
  551. {
  552. $u = explode('/', $v);
  553. $t[0] = $u[2];
  554. $t[1] = $u[0];
  555. $t[2] = $u[1];
  556. }
  557. if(!isset($t))
  558. {
  559. $t = array(0,0,0);
  560. }
  561. $v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
  562. return $v;
  563. }
  564. /**
  565. * 生成随机码
  566. */
  567. static function code($num = 4)
  568. {
  569. $codes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  570. $code="";
  571. for($i=1;$i<=$num;$i++)
  572. {
  573. $code.=$codes{rand(0,61)};
  574. }
  575. return $code;
  576. }
  577. }