d409b1522d6be05bcf8a5b141efa7b0a62cf0cce.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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. $project = $project ? $project : MAZE_PROJECT_NAME;
  220. $path = self::load('manage/project.path', $project);
  221. if(strpos($path, 'http://'))
  222. {
  223. //self::curl($path . $interface);
  224. system('curl ' . $path . $interface . ' > /dev/null &');
  225. }
  226. else
  227. {
  228. # ?和&无法解析
  229. $interface = str_replace(array('?', '&'), array('__', '^'), $interface);
  230. system('php ' . $path . 'index.php -send ' . $interface . ' > /dev/null &');
  231. }
  232. }
  233. static public function mdate($num)
  234. {
  235. if($num <= 0)
  236. {
  237. return '0S';
  238. }
  239. $num = time() - $num;
  240. //$num = 2*3600*24 + 1000;
  241. if($num <= 60)
  242. {
  243. return $num . 'S';
  244. }
  245. $config = array
  246. (
  247. //array(3600*24, 'D'),
  248. array(3600, 'H'),
  249. array(60, 'M'),
  250. array(1, 'S')
  251. );
  252. $result = '';
  253. foreach($config as $k => $v)
  254. {
  255. if($num > $v[0])
  256. {
  257. $value = intval($num/$v[0]);
  258. $num = $num - $v[0]*$value;
  259. $result .= $value . $v[1] . ' ';
  260. }
  261. }
  262. return $result;
  263. }
  264. /**
  265. * 加入到cron中
  266. *
  267. * @return string
  268. */
  269. static public function cron($name, $ldate, $interface, $time = 0, $project = false, $update = true)
  270. {
  271. if($ldate > 0)
  272. {
  273. $ldate = date('Y-m-d H:i:s', $ldate);
  274. }
  275. $info = Maze::load('manage/cron-getOne', array('where_project' => $project, 'where_interface' => $interface));
  276. if($info)
  277. {
  278. if($update)
  279. {
  280. $update = array();
  281. $update['set_name'] = $name;
  282. $update['set_ldate'] = $ldate;
  283. $update['set_interface'] = $interface;
  284. $update['set_time'] = $time;
  285. $update['set_project'] = $project;
  286. $update['set_state'] = 1;
  287. $update['where_id'] = $info['id'];
  288. Maze::load('manage/cron-update', $update);
  289. }
  290. }
  291. else
  292. {
  293. $update['add_name'] = $name;
  294. $update['add_ldate'] = $ldate;
  295. $update['add_interface'] = $interface;
  296. $update['add_time'] = $time;
  297. $update['add_project'] = $project;
  298. Maze::load('manage/cron-insert', $update);
  299. }
  300. }
  301. /**
  302. * 字符串截取
  303. * @param string $string
  304. * @param string $length
  305. * @param string $etc
  306. *
  307. * @return array
  308. */
  309. static public function cut($string, $length = 80, $etc = '...')
  310. {
  311. $result = '';
  312. $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
  313. for($i = 0, $j = 0; $i < strlen($string); $i++)
  314. {
  315. if($j >= $length)
  316. {
  317. for($x = 0, $y = 0; $x < strlen($etc); $x++)
  318. {
  319. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  320. {
  321. $x += $number - 1;
  322. $y++;
  323. }
  324. else
  325. {
  326. $y += 0.5;
  327. }
  328. }
  329. $length -= $y;
  330. break;
  331. }
  332. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  333. {
  334. $i += $number - 1;
  335. $j++;
  336. }
  337. else
  338. {
  339. $j += 0.5;
  340. }
  341. }
  342. for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
  343. {
  344. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  345. {
  346. if($length < 1.0)
  347. {
  348. break;
  349. }
  350. $result .= substr($string, $i, $number);
  351. $length -= 1.0;
  352. $i += $number - 1;
  353. }
  354. else
  355. {
  356. $result .= substr($string, $i, 1);
  357. $length -= 0.5;
  358. }
  359. }
  360. $result = htmlentities($result, ENT_QUOTES, 'utf-8');
  361. if($i < strlen($string))
  362. {
  363. $result .= $etc;
  364. }
  365. return $result;
  366. }
  367. /**
  368. * 执行hack的值
  369. * @param string $value
  370. * @param string $index
  371. * @param string $total
  372. *
  373. * @return array
  374. */
  375. static public function hack($key)
  376. {
  377. if(isset(parent::$global['hack'][$key]))
  378. {
  379. return parent::$global['hack'][$key];
  380. }
  381. }
  382. /**
  383. * 注册maze类库里的常用类,方便使用
  384. * @param string $name
  385. * @param array $param
  386. *
  387. * @return mixed
  388. */
  389. static public function __callStatic($name, $param = array())
  390. {
  391. if(isset(self::$register[$name]))
  392. {
  393. $class = 'Maze\\' . self::$register[$name][0];
  394. $method = self::$register[$name][1];
  395. if(isset($param[2]))
  396. {
  397. return $class::$method($param[0], $param[1], $param[2]);
  398. }
  399. elseif(isset($param[1]))
  400. {
  401. return $class::$method($param[0], $param[1]);
  402. }
  403. elseif(isset($param[0]))
  404. {
  405. return $class::$method($param[0]);
  406. }
  407. else
  408. {
  409. return $class::$method();
  410. }
  411. }
  412. }
  413. static public function curl($url, $param = false, $type = '')
  414. {
  415. //初始化
  416. $ch = curl_init();
  417. //设置选项,包括URL
  418. curl_setopt($ch, CURLOPT_URL, $url);
  419. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  420. curl_setopt($ch, CURLOPT_HEADER, 0);
  421. if($param)
  422. {
  423. // post数据
  424. curl_setopt($ch, CURLOPT_POST, 1);
  425. if($type == 'json')
  426. {
  427. # 中文不用unicode编码
  428. $string = str_replace("\\/", "/", json_encode($param));
  429. $search = "#\\\u([0-9a-f]+)#ie";
  430. if(strpos(strtoupper(PHP_OS), 'WIN') === false)
  431. {
  432. $replace = "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))";//LINUX
  433. }
  434. else
  435. {
  436. $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";//WINDOWS
  437. }
  438. $param = preg_replace($search, $replace, $string);
  439. //$param = json_encode($param, JSON_UNESCAPED_UNICODE);
  440. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  441. 'Content-Type: application/json',
  442. 'Content-Length: ' . strlen($param))
  443. );
  444. }
  445. //print_r($param);die;
  446. // post的变量
  447. @curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  448. }
  449. //执行并获取HTML文档内容
  450. $output = curl_exec($ch);
  451. //释放curl句柄
  452. curl_close($ch);
  453. //打印获得的数据
  454. return $output;
  455. }
  456. static public function weixin()
  457. {
  458. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  459. {
  460. return true;
  461. }
  462. return false;
  463. }
  464. static public function mobile()
  465. {
  466. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  467. $mobile_browser = '0';
  468. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
  469. {
  470. $mobile_browser++;
  471. }
  472. if((isset($_SERVER['HTTP_ACCEPT'])) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
  473. {
  474. $mobile_browser++;
  475. }
  476. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
  477. {
  478. $mobile_browser++;
  479. }
  480. if(isset($_SERVER['HTTP_PROFILE']))
  481. {
  482. $mobile_browser++;
  483. }
  484. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  485. $mobile_agents = array(
  486. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  487. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  488. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  489. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  490. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  491. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  492. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  493. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  494. 'wapr','webc','winw','winw','xda','xda-'
  495. );
  496. if(in_array($mobile_ua, $mobile_agents))
  497. {
  498. $mobile_browser++;
  499. }
  500. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
  501. {
  502. $mobile_browser++;
  503. }
  504. // Pre-final check to reset everything if the user is on Windows
  505. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
  506. {
  507. $mobile_browser=0;
  508. }
  509. // But WP7 is also Windows, with a slightly different characteristic
  510. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
  511. {
  512. $mobile_browser++;
  513. }
  514. if($mobile_browser>0)
  515. {
  516. return true;
  517. }
  518. else
  519. {
  520. return false;
  521. }
  522. }
  523. static public function maketime($v)
  524. {
  525. if(!$v) return '';
  526. if(is_numeric($v)) return $v;
  527. if(is_array($v))
  528. {
  529. $v = $v[1];
  530. }
  531. if(strstr($v, ' '))
  532. {
  533. $t = explode(' ', $v);
  534. $v = $t[0];
  535. $s = explode(':', $t[1]);
  536. }
  537. else
  538. {
  539. $s = array(0,0,0);
  540. }
  541. if(strstr($v, '-'))
  542. {
  543. $t = explode('-', $v);
  544. }
  545. elseif(strstr($v, '/'))
  546. {
  547. $u = explode('/', $v);
  548. $t[0] = $u[2];
  549. $t[1] = $u[0];
  550. $t[2] = $u[1];
  551. }
  552. if(!isset($t))
  553. {
  554. $t = array(0,0,0);
  555. }
  556. $v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
  557. return $v;
  558. }
  559. /**
  560. * 生成随机码
  561. */
  562. static function code($num = 4)
  563. {
  564. $codes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  565. $code="";
  566. for($i=1;$i<=$num;$i++)
  567. {
  568. $code.=$codes{rand(0,61)};
  569. }
  570. return $code;
  571. }
  572. }