1c566b3fab4cb97d513c7b649717c1c445b15fda.svn-base 14 KB

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