b251c67e3ed0d233e61a2a35a7757fda0307d99c.svn-base 14 KB

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