dever.php 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. <?php
  2. class Dever extends Dever\Config\Load
  3. {
  4. static private $register = array
  5. (
  6. 'path' => array('File\\Path','create'),
  7. 'page' => array('Page\\Main','getPage'),
  8. 'total' => array('Page\\Main','getTotal'),
  9. 'html' => array('Page\\Main','getHtml'),
  10. 'url' => array('Http\\Url','get'),
  11. 'location' => array('Http\\Url','location'),
  12. 'input' => array('Http\\Input','get'),
  13. 'preInput' => array('Http\\Input','prefix'),
  14. 'setInput' => array('Http\\Input','set'),
  15. 'abert' => array('Http\\Output','abert'),
  16. 'out' => array('Http\\Output','result'),
  17. 'export' => array('Http\\Output','export'),
  18. 'pageInfo' => array('Http\\Output','page'),
  19. 'load' => array('Routing\\Load','get'),
  20. 'object' => array('Routing\\Load','object'),
  21. 'setStep' => array('Routing\\Step','set'),
  22. 'getStep' => array('Routing\\Step','get'),
  23. 'config' => array('Config\\Load','get'),
  24. 'project' => array('Config\\Project','load'),
  25. 'debug' => array('Debug\\Process','wait'),
  26. 'view' => array('Template\\View','get'),
  27. 'render' => array('Template\\View','html'),
  28. 'token' => array('Security\\Api','get'),
  29. 'login' => array('Security\\Api','login'),
  30. 'rule' => array('Security\\String','rule'),
  31. 'idtostr' => array('Security\\String','idtostr'),
  32. 'strtoid' => array('Security\\String','strtoid'),
  33. 'tcp' => array('Server\\Swoole','getInstance'),
  34. );
  35. static public $markdown;
  36. static public $save;
  37. /**
  38. * 设置循环里每行不同的值
  39. * @param string $value
  40. * @param string $index
  41. *
  42. * @return array
  43. */
  44. static public function lace($value, $index, $key = 2, $num = 0)
  45. {
  46. $value = explode(',', $value);
  47. if(!isset($value[1]))
  48. {
  49. $value[1] = '';
  50. }
  51. if($index > 0 && $index%$key == $num)
  52. {
  53. return $value[0];
  54. }
  55. else
  56. {
  57. return $value[1];
  58. }
  59. }
  60. /**
  61. * 设置循环里最新一条数据的值
  62. * @param string $value
  63. * @param string $index
  64. *
  65. * @return array
  66. */
  67. static public function first($value, $index)
  68. {
  69. if($index == 0)
  70. {
  71. return $value;
  72. }
  73. }
  74. /**
  75. * 设置循环里最后一条数据的值
  76. * @param string $value
  77. * @param string $index
  78. * @param string $total
  79. *
  80. * @return array
  81. */
  82. static public function last($value, $index, $total)
  83. {
  84. if($index == $total)
  85. {
  86. return $value;
  87. }
  88. }
  89. /**
  90. * 除了最后一条
  91. * @param string $value
  92. * @param string $index
  93. * @param string $total
  94. *
  95. * @return array
  96. */
  97. static public function other($value, $index, $total)
  98. {
  99. if($index != $total)
  100. {
  101. return $value;
  102. }
  103. }
  104. /**
  105. * 对多维数组进行排序
  106. * @param string $array
  107. * @param string $field
  108. * @param string $desc
  109. *
  110. * @return array
  111. */
  112. private function sort(&$array, $field, $desc = false)
  113. {
  114. $fieldArr = array();
  115. foreach ($array as $k => $v)
  116. {
  117. $fieldArr[$k] = $v[$field];
  118. }
  119. $sort = $desc == false ? SORT_ASC : SORT_DESC;
  120. array_multisort($fieldArr, $sort, $array);
  121. }
  122. /**
  123. * 生成html a标记 以后统一修改
  124. * @param string $name
  125. * @param string $link
  126. * @param string $target
  127. *
  128. * @return array
  129. */
  130. private function a($name, $link, $target = '_self')
  131. {
  132. return '<a href="'.Dever::url($link).'" target="'.$target.'">'.$name.'</a>';
  133. }
  134. /**
  135. * 生成html img标记 以后统一修改
  136. * @param string $link
  137. * @param string $target
  138. *
  139. * @return array
  140. */
  141. private function img($link, $target = '_self')
  142. {
  143. return Dever::a('<img src="'.$link.'" />', $link, $target);
  144. }
  145. /**
  146. * 获取assets的文件网络路径
  147. * @param string $value
  148. * @param string $type
  149. *
  150. * @return array
  151. */
  152. static public function assets($value, $type = 'css')
  153. {
  154. return parent::$global['host'][$type] . $value;
  155. }
  156. /**
  157. * 生成table
  158. * @param string $data
  159. * @param string $class
  160. *
  161. * @return array
  162. */
  163. static public function table($data, $class = '', $num = 1)
  164. {
  165. if($class)
  166. {
  167. $style = 'class='.$class.'';
  168. }
  169. else
  170. {
  171. $style = 'border=1 width=100% height=100%';
  172. }
  173. $html = '<table '.$style.'>';
  174. foreach($data as $k => $v)
  175. {
  176. if(is_array($v) && $num == 2)
  177. {
  178. $tbody = array($k);
  179. foreach($v as $j)
  180. {
  181. array_push($tbody, $j);
  182. }
  183. $html .= self::tbody($tbody);
  184. }
  185. else
  186. {
  187. if(is_array($v))
  188. {
  189. $v = self::table($v, $class);
  190. //$v = var_export($v, true);
  191. }
  192. if(is_numeric($k))
  193. {
  194. $html .= self::tbody(array($v));
  195. }
  196. else
  197. {
  198. $html .= self::tbody(array($k, $v));
  199. }
  200. }
  201. }
  202. $html .= '</table>';
  203. return $html;
  204. }
  205. /**
  206. * 生成tbody
  207. * @param string $data
  208. * @param string $class
  209. *
  210. * @return array
  211. */
  212. static public function tbody($data)
  213. {
  214. $html = '<tr>';
  215. foreach($data as $k => $v)
  216. {
  217. if($k == 0)
  218. {
  219. $html .= '<td style=width:30%;line-height:1.75;font-weight:bold>'.$v.'</td>';
  220. }
  221. elseif($k == 1)
  222. {
  223. $html .= '<td style=width:30%;word-break:break-all;word-wrap:break-word;>'.$v.'</td>';
  224. }
  225. else
  226. {
  227. $html .= '<td style=word-break:break-all;word-wrap:break-word;>'.$v.'</td>';
  228. }
  229. }
  230. $html .= '</tr>';
  231. return $html;
  232. }
  233. /**
  234. * 判断是否包含
  235. * @param string $value
  236. * @param string $source
  237. *
  238. * @return array
  239. */
  240. static public function in($value, $index)
  241. {
  242. $key = $value . '_' . $index;
  243. if(isset(self::$global['in'][$key]))
  244. {
  245. return self::$global['in'][$key];
  246. }
  247. if(is_array($index))
  248. {
  249. $index = implode(',', $index);
  250. }
  251. $index .= ',';
  252. if(strpos($value, ','))
  253. {
  254. $temp = explode(',', $value);
  255. foreach($temp as $k => $v)
  256. {
  257. $state = self::in($v, $index);
  258. if($state == true)
  259. {
  260. break;
  261. }
  262. }
  263. return self::$global['in'][$key] = $state;
  264. }
  265. else
  266. {
  267. if(strpos($index, $value . ',') !== false)
  268. {
  269. return self::$global['in'][$key] = true;
  270. }
  271. return self::$global['in'][$key] = false;
  272. }
  273. }
  274. /**
  275. * pic
  276. * @param string $file
  277. * @param string $name
  278. *
  279. * @return array
  280. */
  281. static public function pic($file, $name = false)
  282. {
  283. if(strpos($file, ',') !== false)
  284. {
  285. $temp = explode(',', $file);
  286. $file = array();
  287. foreach($temp as $k => $v)
  288. {
  289. $file[$k] = self::pic($v, $name);
  290. }
  291. return implode(',', $file);
  292. }
  293. if($name && strpos($file, '_') !== false)
  294. {
  295. $temp = explode('_', $file);
  296. $file = $temp[0] . '_' . $name;
  297. }
  298. # 替换host
  299. if($file && strpos($file, 'http://') === false)
  300. {
  301. $host = Dever::$global['host']['image'];
  302. if(is_array(Dever::$global['host']['image']))
  303. {
  304. $index = array_rand(Dever::$global['host']['image']);
  305. $host = Dever::$global['host']['image'][$index];
  306. }
  307. $file = $host . $file;
  308. }
  309. return $file;
  310. }
  311. /**
  312. * 获取链接
  313. * @param string $url
  314. * @param array $param
  315. *
  316. * @return string
  317. */
  318. static public function link($url, $param = array(), $project = 'main')
  319. {
  320. if($param)
  321. {
  322. $send = array();
  323. foreach($param as $k => $v)
  324. {
  325. if(is_string($k) && $v)
  326. {
  327. $send[] = $k.'=' . $v;
  328. }
  329. elseif($v && $value = Dever::input($v))
  330. {
  331. $send[] = $v.'=' . $value;
  332. }
  333. }
  334. if($send)
  335. {
  336. $url .= '?' . implode('&', $send);
  337. }
  338. }
  339. return Dever::url($url, $project);
  340. }
  341. /**
  342. * str_explode
  343. * @param string $value
  344. * @param string $index
  345. *
  346. * @return array
  347. */
  348. static function str_explode($value, $num = 2)
  349. {
  350. $len = strlen($value);
  351. $result = array();
  352. for($i = 0; $i < $len; $i=$i+$num)
  353. {
  354. $result[$i/$num] = substr($value, $i, $num);
  355. }
  356. return $result;
  357. }
  358. /**
  359. * 获取_param
  360. * @param string $value
  361. * @param string $index
  362. *
  363. * @return array
  364. */
  365. static public function param($name)
  366. {
  367. if(isset(Dever::$global['base']['_param']))
  368. {
  369. if(isset(Dever::$global['base']['_param']['add_' . $name]))
  370. {
  371. return Dever::$global['base']['_param']['add_' . $name];
  372. }
  373. if(isset(Dever::$global['base']['_param']['set_' . $name]))
  374. {
  375. return Dever::$global['base']['_param']['set_' . $name];
  376. }
  377. }
  378. return false;
  379. }
  380. /**
  381. * 获取ip
  382. *
  383. * @return string
  384. */
  385. static public function ip()
  386. {
  387. $ip = '';
  388. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
  389. {
  390. $ip = getenv('HTTP_CLIENT_IP');
  391. }
  392. elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
  393. {
  394. $ip = getenv('HTTP_X_FORWARDED_FOR');
  395. }
  396. elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
  397. {
  398. $ip = getenv('REMOTE_ADDR');
  399. }
  400. elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
  401. {
  402. $ip = $_SERVER['REMOTE_ADDR'];
  403. }
  404. return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
  405. }
  406. /**
  407. * 获取ua
  408. *
  409. * @return string
  410. */
  411. static public function ua()
  412. {
  413. return $_SERVER['HTTP_USER_AGENT'];
  414. }
  415. /**
  416. * kill
  417. *
  418. * @return string
  419. */
  420. static public function kill($commend)
  421. {
  422. $shell = "ps -ef | grep ".$commend." | grep -v grep | awk '{print $2}' | xargs kill -9";
  423. self::run($shell, '');
  424. }
  425. /**
  426. * 运维程序
  427. *
  428. * @return string
  429. */
  430. static public function daemon($interface, $project = false, $file = 'index.php', $state = false)
  431. {
  432. if(strpos($interface, 'http://') !== false)
  433. {
  434. return self::run('curl "' . $interface . '"');
  435. }
  436. $project = $project ? $project : DEVER_APP_NAME;
  437. $path = self::load('manage/project.setup', $project);
  438. if(strpos($path, 'http://') !== false)
  439. {
  440. //self::curl($path . $interface);
  441. return self::run('curl "' . $path . $interface . '"');
  442. }
  443. else
  444. {
  445. # ?和&无法解析
  446. $interface = str_replace(array('?', '&'), array('__', '^'), $interface);
  447. return self::run('php ' . $path . $file . ' -send ' . $interface, $state);
  448. }
  449. }
  450. /**
  451. * shell
  452. *
  453. * @return string
  454. */
  455. static public function run($shell, $state = false, $method = 'system')
  456. {
  457. if($state === false)
  458. {
  459. $state = ' > /dev/null &';
  460. }
  461. $method($shell . $state, $result);
  462. return $result;
  463. }
  464. /**
  465. * process
  466. *
  467. * @return string
  468. */
  469. static public function process($commend, $count = false)
  470. {
  471. $shell = "ps -ef | grep ".$commend." | grep -v grep";
  472. if($count)
  473. {
  474. $shell .= ' | grep "master" | wc -l';
  475. }
  476. $result = self::run($shell, '', 'system');
  477. if($result != 1)
  478. {
  479. return $result;
  480. }
  481. return false;
  482. }
  483. static public function mdate($num, $type = 1)
  484. {
  485. $num = time() - $num;
  486. if($num <= 0)
  487. {
  488. if($type == 2)
  489. {
  490. return '1秒前';
  491. }
  492. else
  493. {
  494. return '1S';
  495. }
  496. }
  497. $config = array
  498. (
  499. array(31536000, 'Y', '年'),
  500. array(2592000, 'T', '个月'),
  501. array(604800, 'W', '星期'),
  502. array(86400, 'D', '天'),
  503. array(3600, 'H', '小时'),
  504. array(60, 'M', '分钟'),
  505. array(1, 'S', '秒')
  506. );
  507. if($type == 2)
  508. {
  509. foreach($config as $k => $v)
  510. {
  511. $value = intval($num/$v[0]);
  512. if($value != 0)
  513. {
  514. return $value . $v[2] . '前';
  515. }
  516. }
  517. return '';
  518. }
  519. else
  520. {
  521. $result = '';
  522. foreach($config as $k => $v)
  523. {
  524. if($num > $v[0])
  525. {
  526. $value = intval($num/$v[0]);
  527. $num = $num - $v[0]*$value;
  528. $result .= $value . $v[1] . ' ';
  529. }
  530. }
  531. return $result;
  532. }
  533. }
  534. /**
  535. * 加入到cron中
  536. *
  537. * @return string
  538. */
  539. static public function cron($name, $ldate, $interface, $time = 0, $project = false, $update = true)
  540. {
  541. if($ldate > 0)
  542. {
  543. $ldate = date('Y-m-d H:i:s', $ldate);
  544. }
  545. $info = Dever::load('manage/cron-getOne', array('where_project' => $project, 'where_interface' => $interface));
  546. if($info)
  547. {
  548. if($update)
  549. {
  550. $update = array();
  551. $update['set_name'] = $name;
  552. $update['set_ldate'] = $ldate;
  553. $update['set_interface'] = $interface;
  554. $update['set_time'] = $time;
  555. $update['set_project'] = $project;
  556. $update['set_state'] = 1;
  557. $update['where_id'] = $info['id'];
  558. Dever::load('manage/cron-update', $update);
  559. }
  560. }
  561. else
  562. {
  563. $update = array();
  564. $update['add_name'] = $name;
  565. $update['add_ldate'] = $ldate;
  566. $update['add_interface'] = $interface;
  567. $update['add_time'] = $time;
  568. $update['add_project'] = $project;
  569. Dever::load('manage/cron-insert', $update);
  570. }
  571. }
  572. /**
  573. * 字符串截取
  574. * @param string $string
  575. * @param string $length
  576. * @param string $etc
  577. *
  578. * @return array
  579. */
  580. static public function cut($string, $length = 80, $etc = '...')
  581. {
  582. $result = '';
  583. $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8');
  584. for($i = 0, $j = 0; $i < strlen($string); $i++)
  585. {
  586. if($j >= $length)
  587. {
  588. for($x = 0, $y = 0; $x < strlen($etc); $x++)
  589. {
  590. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  591. {
  592. $x += $number - 1;
  593. $y++;
  594. }
  595. else
  596. {
  597. $y += 0.5;
  598. }
  599. }
  600. $length -= $y;
  601. break;
  602. }
  603. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  604. {
  605. $i += $number - 1;
  606. $j++;
  607. }
  608. else
  609. {
  610. $j += 0.5;
  611. }
  612. }
  613. for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++)
  614. {
  615. if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
  616. {
  617. if($length < 1.0)
  618. {
  619. break;
  620. }
  621. $result .= substr($string, $i, $number);
  622. $length -= 1.0;
  623. $i += $number - 1;
  624. }
  625. else
  626. {
  627. $result .= substr($string, $i, 1);
  628. $length -= 0.5;
  629. }
  630. }
  631. $result = htmlentities($result, ENT_QUOTES, 'utf-8');
  632. if($i < strlen($string))
  633. {
  634. $result .= $etc;
  635. }
  636. return $result;
  637. }
  638. /**
  639. * strlen
  640. * @param string $string
  641. *
  642. * @return array
  643. */
  644. static public function strlen($string)
  645. {
  646. preg_match_all("/./us", $string, $match);
  647. return count($match[0]);
  648. }
  649. /**
  650. * 执行hack的值
  651. * @param string $value
  652. * @param string $index
  653. * @param string $total
  654. *
  655. * @return array
  656. */
  657. static public function hack($key)
  658. {
  659. if(isset(parent::$global['hack'][$key]))
  660. {
  661. return parent::$global['hack'][$key];
  662. }
  663. }
  664. /**
  665. * 注册dever类库里的常用类,方便使用
  666. * @param string $name
  667. * @param array $param
  668. *
  669. * @return mixed
  670. */
  671. static public function __callStatic($name, $param = array())
  672. {
  673. if(isset(self::$register[$name]))
  674. {
  675. $class = 'Dever\\' . self::$register[$name][0];
  676. $method = self::$register[$name][1];
  677. return call_user_func_array(array($class, $method), $param);
  678. }
  679. }
  680. static public function curl($url, $param = false, $type = '', $ip = '', $refer = '')
  681. {
  682. //初始化
  683. $ch = curl_init();
  684. //设置选项,包括URL
  685. curl_setopt($ch, CURLOPT_URL, $url);
  686. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  687. if($ip)
  688. {
  689. $headers['CLIENT-IP'] = $ip;
  690. $headers['X-FORWARDED-FOR'] = $ip;
  691. $headerArr = array();
  692. foreach($headers as $n => $v)
  693. {
  694. $headerArr[] = $n .':' . $v;
  695. }
  696. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  697. }
  698. if($refer)
  699. {
  700. curl_setopt($ch, CURLOPT_REFERER, $refer);
  701. }
  702. if($ip || $refer)
  703. {
  704. curl_setopt($ch, CURLOPT_HEADER, 1);
  705. }
  706. else
  707. {
  708. curl_setopt($ch, CURLOPT_HEADER, 0);
  709. }
  710. if($param)
  711. {
  712. // post数据
  713. curl_setopt($ch, CURLOPT_POST, 1);
  714. if($type == 'json')
  715. {
  716. # 中文不用unicode编码
  717. $string = str_replace("\\/", "/", json_encode($param));
  718. $search = "#\\\u([0-9a-f]+)#ie";
  719. if(strpos(strtoupper(PHP_OS), 'WIN') === false)
  720. {
  721. $replace = "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))";//LINUX
  722. }
  723. else
  724. {
  725. $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";//WINDOWS
  726. }
  727. $param = preg_replace($search, $replace, $string);
  728. //$param = json_encode($param, JSON_UNESCAPED_UNICODE);
  729. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  730. 'Content-Type: application/json',
  731. 'Content-Length: ' . strlen($param))
  732. );
  733. }
  734. //print_r($param);die;
  735. // post的变量
  736. @curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
  737. }
  738. //执行并获取HTML文档内容
  739. $output = curl_exec($ch);
  740. //释放curl句柄
  741. curl_close($ch);
  742. //打印获得的数据
  743. return $output;
  744. }
  745. static public function weixin()
  746. {
  747. if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false)
  748. {
  749. return true;
  750. }
  751. return false;
  752. }
  753. static public function mobile()
  754. {
  755. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  756. $mobile_browser = '0';
  757. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT'])))
  758. {
  759. $mobile_browser++;
  760. }
  761. if((isset($_SERVER['HTTP_ACCEPT'])) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
  762. {
  763. $mobile_browser++;
  764. }
  765. if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
  766. {
  767. $mobile_browser++;
  768. }
  769. if(isset($_SERVER['HTTP_PROFILE']))
  770. {
  771. $mobile_browser++;
  772. }
  773. $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
  774. $mobile_agents = array(
  775. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  776. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  777. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  778. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  779. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  780. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  781. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  782. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  783. 'wapr','webc','winw','winw','xda','xda-'
  784. );
  785. if(in_array($mobile_ua, $mobile_agents))
  786. {
  787. $mobile_browser++;
  788. }
  789. if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
  790. {
  791. $mobile_browser++;
  792. }
  793. // Pre-final check to reset everything if the user is on Windows
  794. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
  795. {
  796. $mobile_browser=0;
  797. }
  798. // But WP7 is also Windows, with a slightly different characteristic
  799. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
  800. {
  801. $mobile_browser++;
  802. }
  803. if($mobile_browser>0)
  804. {
  805. return true;
  806. }
  807. else
  808. {
  809. return false;
  810. }
  811. }
  812. static public function maketime($v)
  813. {
  814. if(!$v) return '';
  815. if(is_numeric($v)) return $v;
  816. if(is_array($v))
  817. {
  818. $v = $v[1];
  819. }
  820. if(strstr($v, ' '))
  821. {
  822. $t = explode(' ', $v);
  823. $v = $t[0];
  824. $s = explode(':', $t[1]);
  825. }
  826. else
  827. {
  828. $s = array(0,0,0);
  829. }
  830. if(strstr($v, '-'))
  831. {
  832. $t = explode('-', $v);
  833. }
  834. elseif(strstr($v, '/'))
  835. {
  836. $u = explode('/', $v);
  837. $t[0] = $u[2];
  838. $t[1] = $u[0];
  839. $t[2] = $u[1];
  840. }
  841. if(!isset($t))
  842. {
  843. $t = array(0,0,0);
  844. }
  845. $v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]);
  846. return $v;
  847. }
  848. /**
  849. * 生成随机码
  850. */
  851. static function code($num = 4)
  852. {
  853. $codes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  854. $code="";
  855. for($i=1;$i<=$num;$i++)
  856. {
  857. $code.=$codes{rand(0,61)};
  858. }
  859. return $code;
  860. }
  861. static function proxy($method = false, $param = false)
  862. {
  863. if($method)
  864. {
  865. if(isset(Dever::$global['host']['proxy']) && Dever::$global['host']['proxy'])
  866. {
  867. $method = urlencode($method);
  868. return Dever::url(Dever::$global['host']['proxy'] . 'proxy_method=' . $method . '&' . $param);
  869. }
  870. return Dever::url($method . '?' . $param);
  871. }
  872. $data = Dever::input();
  873. foreach($data as $k => $v)
  874. {
  875. Dever::setInput($k, $v);
  876. }
  877. return Dever::load(urldecode($data['proxy_method']), array('CHECK' => true));
  878. }
  879. static public function script()
  880. {
  881. $script = 'var config={};config.host="'.Dever::url('').'";config.type="'.Dever\Routing\Uri::$type.'";config.current="'.Dever::url().'";config.upload="'.Dever::$global['host']['upload'].'";config.assets="'.Dever::$global['host']['core'].'";';
  882. if(isset(Dever::$global['host']['proxy']))
  883. {
  884. $script .= 'config.proxy="'.Dever::$global['host']['proxy'].'";';
  885. }
  886. if(isset(Dever::$global['host']['manage']))
  887. {
  888. $script .= 'config.manage_assets="'.Dever::$global['host']['manage'].'";';
  889. }
  890. if(isset(Dever::$global['host']['workspace']))
  891. {
  892. $script .= 'config.workspace="'.Dever::$global['host']['workspace'].'";';
  893. }
  894. if(isset(Dever::$global['base']['layout']))
  895. {
  896. $script .= 'config.layout = "'.Dever::$global['base']['layout'].'";$(document).ready(function()
  897. {
  898. $(document).pjax("a", "'.Dever::$global['base']['layout'].'");
  899. $(document).on("submit", "#form1", function (event) {event.preventDefault();$.pjax.submit(event, "'.Dever::$global['base']['layout'].'", {"push": true, "replace": false, timeout:6000, "scrollTo": 0, maxCacheLength: 0});});
  900. $(document).on("pjax:start", function()
  901. {
  902. NProgress.start();
  903. });
  904. $(document).on("pjax:end", function()
  905. {
  906. NProgress.done();
  907. });
  908. });';
  909. }
  910. return $script;
  911. }
  912. /**
  913. * 获取文件后缀
  914. * @param string $file
  915. *
  916. * @return string
  917. */
  918. static public function ext($file)
  919. {
  920. return pathinfo($file, PATHINFO_EXTENSION);
  921. }
  922. /**
  923. * markdown解析
  924. * @param string $value
  925. *
  926. * @return string
  927. */
  928. static public function markdown($value)
  929. {
  930. self::$markdown = self::$markdown ? self::$markdown : new \Parsedown();
  931. # 一个回车就换行
  932. self::$markdown->setBreaksEnabled(true);
  933. # 过滤掉所有特殊代码
  934. return self::$markdown->text($value);
  935. }
  936. /**
  937. * setup 安装引导程序
  938. * @param string $value
  939. *
  940. * @return string
  941. */
  942. static public function setup()
  943. {
  944. $host = DEVER_APP_HOST;
  945. $path = DEVER_APP_PATH . '../';
  946. $dir = scandir($path);
  947. foreach($dir as $k => $v)
  948. {
  949. if($v != '.' && $v != '..' && is_dir($path . $v))
  950. {
  951. $url = str_replace(array('www', 'main'), $v, $host);
  952. Dever::curl($url);
  953. echo $url . ' 安装成功!<br />' . "\r\n";
  954. }
  955. }
  956. return '感谢您使用Dever!';
  957. }
  958. /**
  959. * cache 缓存功能
  960. * @param string $key
  961. * @param array $data
  962. * @param int $expire
  963. * @param string $type
  964. *
  965. * @return mixed
  966. */
  967. static public function cache($key = false, $data = false, $expire = 0, $type = 'data')
  968. {
  969. $cache = self::$global['cache'];
  970. if(empty($cache[$type]))
  971. {
  972. return;
  973. }
  974. //$handle =& \Dever\Cache\Handle::getInstance($type, $cache[$type]);
  975. $handle = \Dever\Cache\Handle::getInstance($type, $cache[$type]);
  976. if(is_string($data) && $data == 'delete')
  977. {
  978. return $handle->delete($key);
  979. }
  980. elseif($data)
  981. {
  982. return $handle->set($key, $data, $expire);
  983. }
  984. return $handle->get($key);
  985. }
  986. /**
  987. * dyna 动态解析功能
  988. * @param string $key
  989. * @param array $data
  990. *
  991. * @return mixed
  992. */
  993. static public function odyna($key = false, $data = array())
  994. {
  995. return (object)self::dyna($key, $data);
  996. }
  997. /**
  998. * dyna 动态解析功能
  999. * @param string $key
  1000. * @param array $data
  1001. *
  1002. * @return mixed
  1003. */
  1004. static public function dyna($key = false, $data = array())
  1005. {
  1006. if(isset(self::$global['dyna_data'][$key]))
  1007. {
  1008. return self::$global['dyna_data'][$key];
  1009. }
  1010. $config = self::get('dyna');
  1011. if(empty($config[$key]))
  1012. {
  1013. return $key;
  1014. }
  1015. if(!$data)
  1016. {
  1017. return $config[$key];
  1018. }
  1019. if(is_array($config[$key]))
  1020. {
  1021. foreach($config[$key] as $k => $v)
  1022. {
  1023. $v = '$value = "'.$v.'";';
  1024. eval($v);
  1025. if(isset($value))
  1026. {
  1027. $config[$key][$k] = $value;
  1028. }
  1029. }
  1030. }
  1031. else
  1032. {
  1033. $v = '$value = "'.$config[$key].'";';
  1034. eval($v);
  1035. if(isset($value))
  1036. {
  1037. $config[$key] = $value;
  1038. }
  1039. }
  1040. self::$global['dyna_data'][$key] = $config[$key];
  1041. return $config[$key];
  1042. }
  1043. /**
  1044. * submit 处理重复提交功能 此处暂时有问题
  1045. * @param string $type
  1046. * @param int $value
  1047. *
  1048. * @return mixed
  1049. */
  1050. static public function submit($type = 'update', $value = 1)
  1051. {
  1052. if(empty(self::$save))
  1053. {
  1054. self::$save = new \Dever\Session\Save(DEVER_APP_NAME, 'cookie');
  1055. }
  1056. if($type == 'check')
  1057. {
  1058. $submit = self::$save->get('submit');
  1059. if($submit == 2)
  1060. {
  1061. Dever::abert('请不要重复提交');
  1062. }
  1063. }
  1064. if($value)
  1065. {
  1066. self::$save->add('submit', $value);
  1067. }
  1068. }
  1069. }