dever.php 30 KB

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