12f45cf6282df0403adaca64b0cc44a09c746e8c.svn-base 14 KB

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