array('File\\Path','create'), 'page' => array('Page\\Main','getPage'), 'total' => array('Page\\Main','getTotal'), 'html' => array('Page\\Main','getHtml'), 'url' => array('Http\\Url','get'), 'location' => array('Http\\Url','location'), 'input' => array('Http\\Input','get'), 'preInput' => array('Http\\Input','prefix'), 'setInput' => array('Http\\Input','set'), 'abert' => array('Http\\Output','abert'), 'out' => array('Http\\Output','result'), 'export' => array('Http\\Output','export'), 'pageInfo' => array('Http\\Output','page'), 'load' => array('Routing\\Load','get'), 'object' => array('Routing\\Load','object'), 'setStep' => array('Routing\\Step','set'), 'getStep' => array('Routing\\Step','get'), 'config' => array('Config\\Load','get'), 'project' => array('Config\\Project','load'), 'debug' => array('Debug\\Process','wait'), 'view' => array('Template\\View','get'), 'render' => array('Template\\View','html'), 'token' => array('Security\\Api','get'), 'login' => array('Security\\Api','login'), 'rule' => array('Security\\String','rule'), 'idtostr' => array('Security\\String','idtostr'), 'strtoid' => array('Security\\String','strtoid'), 'tcp' => array('Server\\Swoole','getInstance'), ); static public $markdown; static public $save; /** * 设置循环里每行不同的值 * @param string $value * @param string $index * * @return array */ static public function lace($value, $index, $key = 2, $num = 0) { $value = explode(',', $value); if(!isset($value[1])) { $value[1] = ''; } if($index > 0 && $index%$key == $num) { return $value[0]; } else { return $value[1]; } } /** * 设置循环里最新一条数据的值 * @param string $value * @param string $index * * @return array */ static public function first($value, $index) { if($index == 0) { return $value; } } /** * 设置循环里最后一条数据的值 * @param string $value * @param string $index * @param string $total * * @return array */ static public function last($value, $index, $total) { if($index == $total) { return $value; } } /** * 除了最后一条 * @param string $value * @param string $index * @param string $total * * @return array */ static public function other($value, $index, $total) { if($index != $total) { return $value; } } /** * 对多维数组进行排序 * @param string $array * @param string $field * @param string $desc * * @return array */ private function sort(&$array, $field, $desc = false) { $fieldArr = array(); foreach ($array as $k => $v) { $fieldArr[$k] = $v[$field]; } $sort = $desc == false ? SORT_ASC : SORT_DESC; array_multisort($fieldArr, $sort, $array); } /** * 生成html a标记 以后统一修改 * @param string $name * @param string $link * @param string $target * * @return array */ private function a($name, $link, $target = '_self') { return ''.$name.''; } /** * 生成html img标记 以后统一修改 * @param string $link * @param string $target * * @return array */ private function img($link, $target = '_self') { return Dever::a('', $link, $target); } /** * 获取assets的文件网络路径 * @param string $value * @param string $type * * @return array */ static public function assets($value, $type = 'css') { return parent::$global['host'][$type] . $value; } /** * 生成table * @param string $data * @param string $class * * @return array */ static public function table($data, $class = '', $num = 1) { if($class) { $style = 'class='.$class.''; } else { $style = 'border=1 width=100% height=100%'; } $html = ''; foreach($data as $k => $v) { if(is_array($v) && $num == 2) { $tbody = array($k); foreach($v as $j) { array_push($tbody, $j); } $html .= self::tbody($tbody); } else { if(is_array($v)) { $v = self::table($v, $class); //$v = var_export($v, true); } if(is_numeric($k)) { $html .= self::tbody(array($v)); } else { $html .= self::tbody(array($k, $v)); } } } $html .= '
'; return $html; } /** * 生成tbody * @param string $data * @param string $class * * @return array */ static public function tbody($data) { $html = ''; foreach($data as $k => $v) { if($k == 0) { $html .= ''.$v.''; } elseif($k == 1) { $html .= ''.$v.''; } else { $html .= ''.$v.''; } } $html .= ''; return $html; } /** * 判断是否包含 * @param string $value * @param string $source * * @return array */ static public function in($value, $index) { $key = $value . '_' . $index; if(isset(self::$global['in'][$key])) { return self::$global['in'][$key]; } if(is_array($index)) { $index = implode(',', $index); } $index .= ','; if(strpos($value, ',')) { $temp = explode(',', $value); foreach($temp as $k => $v) { $state = self::in($v, $index); if($state == true) { break; } } return self::$global['in'][$key] = $state; } else { if(strpos($index, $value . ',') !== false) { return self::$global['in'][$key] = true; } return self::$global['in'][$key] = false; } } /** * pic * @param string $file * @param string $name * * @return array */ static public function pic($file, $name = false) { if(strpos($file, ',') !== false) { $temp = explode(',', $file); $file = array(); foreach($temp as $k => $v) { $file[$k] = self::pic($v, $name); } return implode(',', $file); } if($name && strpos($file, '_') !== false) { $temp = explode('_', $file); $file = $temp[0] . '_' . $name; } # 替换host if($file && strpos($file, 'http://') === false) { $host = Dever::$global['host']['image']; if(is_array(Dever::$global['host']['image'])) { $index = array_rand(Dever::$global['host']['image']); $host = Dever::$global['host']['image'][$index]; } $file = $host . $file; } return $file; } /** * 获取链接 * @param string $url * @param array $param * * @return string */ static public function link($url, $param = array(), $project = 'main') { if($param) { $send = array(); foreach($param as $k => $v) { if(is_string($k) && $v) { $send[] = $k.'=' . $v; } elseif($v && $value = Dever::input($v)) { $send[] = $v.'=' . $value; } } if($send) { $url .= '?' . implode('&', $send); } } return Dever::url($url, $project); } /** * str_explode * @param string $value * @param string $index * * @return array */ static function str_explode($value, $num = 2) { $len = strlen($value); $result = array(); for($i = 0; $i < $len; $i=$i+$num) { $result[$i/$num] = substr($value, $i, $num); } return $result; } /** * 获取_param * @param string $value * @param string $index * * @return array */ static public function param($name) { if(isset(Dever::$global['base']['_param'])) { if(isset(Dever::$global['base']['_param']['add_' . $name])) { return Dever::$global['base']['_param']['add_' . $name]; } if(isset(Dever::$global['base']['_param']['set_' . $name])) { return Dever::$global['base']['_param']['set_' . $name]; } } return false; } /** * 获取ip * * @return string */ static public function ip() { $ip = ''; if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; } /** * 获取ua * * @return string */ static public function ua() { return $_SERVER['HTTP_USER_AGENT']; } /** * kill * * @return string */ static public function kill($commend) { $shell = "ps -ef | grep ".$commend." | grep -v grep | awk '{print $2}' | xargs kill -9"; self::run($shell, ''); } /** * 运维程序 * * @return string */ static public function daemon($interface, $project = false, $file = 'index.php', $state = false) { if(strpos($interface, 'http://') !== false) { return self::run('curl "' . $interface . '"'); } $project = $project ? $project : DEVER_APP_NAME; $path = self::load('manage/project.setup', $project); if(strpos($path, 'http://') !== false) { //self::curl($path . $interface); return self::run('curl "' . $path . $interface . '"'); } else { # ?和&无法解析 $interface = str_replace(array('?', '&'), array('__', '^'), $interface); return self::run('php ' . $path . $file . ' -send ' . $interface, $state); } } /** * shell * * @return string */ static public function run($shell, $state = false, $method = 'system') { if($state === false) { $state = ' > /dev/null &'; } $method($shell . $state, $result); return $result; } /** * process * * @return string */ static public function process($commend, $count = false) { $shell = "ps -ef | grep ".$commend." | grep -v grep"; if($count) { $shell .= ' | grep "master" | wc -l'; } $result = self::run($shell, '', 'system'); if($result != 1) { return $result; } return false; } static public function mdate($num, $type = 1) { $num = time() - $num; if($num <= 0) { if($type == 2) { return '1秒前'; } else { return '1S'; } } $config = array ( array(31536000, 'Y', '年'), array(2592000, 'T', '个月'), array(604800, 'W', '星期'), array(86400, 'D', '天'), array(3600, 'H', '小时'), array(60, 'M', '分钟'), array(1, 'S', '秒') ); if($type == 2) { foreach($config as $k => $v) { $value = intval($num/$v[0]); if($value != 0) { return $value . $v[2] . '前'; } } return ''; } else { $result = ''; foreach($config as $k => $v) { if($num > $v[0]) { $value = intval($num/$v[0]); $num = $num - $v[0]*$value; $result .= $value . $v[1] . ' '; } } return $result; } } /** * 加入到cron中 * * @return string */ static public function cron($name, $ldate, $interface, $time = 0, $project = false, $update = true) { if($ldate > 0) { $ldate = date('Y-m-d H:i:s', $ldate); } $info = Dever::load('manage/cron-getOne', array('where_project' => $project, 'where_interface' => $interface)); if($info) { if($update) { $update = array(); $update['set_name'] = $name; $update['set_ldate'] = $ldate; $update['set_interface'] = $interface; $update['set_time'] = $time; $update['set_project'] = $project; $update['set_state'] = 1; $update['where_id'] = $info['id']; Dever::load('manage/cron-update', $update); } } else { $update = array(); $update['add_name'] = $name; $update['add_ldate'] = $ldate; $update['add_interface'] = $interface; $update['add_time'] = $time; $update['add_project'] = $project; Dever::load('manage/cron-insert', $update); } } /** * 字符串截取 * @param string $string * @param string $length * @param string $etc * * @return array */ static public function cut($string, $length = 80, $etc = '...') { $result = ''; $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'utf-8'); for($i = 0, $j = 0; $i < strlen($string); $i++) { if($j >= $length) { for($x = 0, $y = 0; $x < strlen($etc); $x++) { if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) { $x += $number - 1; $y++; } else { $y += 0.5; } } $length -= $y; break; } if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) { $i += $number - 1; $j++; } else { $j += 0.5; } } for($i = 0; (($i < strlen($string)) && ($length > 0)); $i++) { if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0')) { if($length < 1.0) { break; } $result .= substr($string, $i, $number); $length -= 1.0; $i += $number - 1; } else { $result .= substr($string, $i, 1); $length -= 0.5; } } $result = htmlentities($result, ENT_QUOTES, 'utf-8'); if($i < strlen($string)) { $result .= $etc; } return $result; } /** * strlen * @param string $string * * @return array */ static public function strlen($string) { preg_match_all("/./us", $string, $match); return count($match[0]); } /** * 执行hack的值 * @param string $value * @param string $index * @param string $total * * @return array */ static public function hack($key) { if(isset(parent::$global['hack'][$key])) { return parent::$global['hack'][$key]; } } /** * 注册dever类库里的常用类,方便使用 * @param string $name * @param array $param * * @return mixed */ static public function __callStatic($name, $param = array()) { if(isset(self::$register[$name])) { $class = 'Dever\\' . self::$register[$name][0]; $method = self::$register[$name][1]; return call_user_func_array(array($class, $method), $param); } } static public function curl($url, $param = false, $type = '', $ip = '', $refer = '') { //初始化 $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($ip) { $headers['CLIENT-IP'] = $ip; $headers['X-FORWARDED-FOR'] = $ip; $headerArr = array(); foreach($headers as $n => $v) { $headerArr[] = $n .':' . $v; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr); } if($refer) { curl_setopt($ch, CURLOPT_REFERER, $refer); } if($ip || $refer) { curl_setopt($ch, CURLOPT_HEADER, 1); } else { curl_setopt($ch, CURLOPT_HEADER, 0); } if($param) { // post数据 curl_setopt($ch, CURLOPT_POST, 1); if($type == 'json') { # 中文不用unicode编码 $string = str_replace("\\/", "/", json_encode($param)); $search = "#\\\u([0-9a-f]+)#ie"; if(strpos(strtoupper(PHP_OS), 'WIN') === false) { $replace = "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))";//LINUX } else { $replace = "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))";//WINDOWS } $param = preg_replace($search, $replace, $string); //$param = json_encode($param, JSON_UNESCAPED_UNICODE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($param)) ); } //print_r($param);die; // post的变量 @curl_setopt($ch, CURLOPT_POSTFIELDS, $param); } //执行并获取HTML文档内容 $output = curl_exec($ch); //释放curl句柄 curl_close($ch); //打印获得的数据 return $output; } static public function weixin() { if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { return true; } return false; } static public function mobile() { $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : ''; $mobile_browser = '0'; if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { $mobile_browser++; } if((isset($_SERVER['HTTP_ACCEPT'])) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)) { $mobile_browser++; } if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) { $mobile_browser++; } if(isset($_SERVER['HTTP_PROFILE'])) { $mobile_browser++; } $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)); $mobile_agents = array( 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac', 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno', 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-', 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-', 'newt','noki','oper','palm','pana','pant','phil','play','port','prox', 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar', 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-', 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp', 'wapr','webc','winw','winw','xda','xda-' ); if(in_array($mobile_ua, $mobile_agents)) { $mobile_browser++; } if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) { $mobile_browser++; } // Pre-final check to reset everything if the user is on Windows if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false) { $mobile_browser=0; } // But WP7 is also Windows, with a slightly different characteristic if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) { $mobile_browser++; } if($mobile_browser>0) { return true; } else { return false; } } static public function maketime($v) { if(!$v) return ''; if(is_numeric($v)) return $v; if(is_array($v)) { $v = $v[1]; } if(strstr($v, ' ')) { $t = explode(' ', $v); $v = $t[0]; $s = explode(':', $t[1]); } else { $s = array(0,0,0); } if(strstr($v, '-')) { $t = explode('-', $v); } elseif(strstr($v, '/')) { $u = explode('/', $v); $t[0] = $u[2]; $t[1] = $u[0]; $t[2] = $u[1]; } if(!isset($t)) { $t = array(0,0,0); } $v = mktime($s[0], $s[1], $s[2], $t[1], $t[2], $t[0]); return $v; } /** * 生成随机码 */ static function code($num = 4) { $codes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $code=""; for($i=1;$i<=$num;$i++) { $code.=$codes{rand(0,61)}; } return $code; } static function proxy($method = false, $param = false) { if($method) { if(isset(Dever::$global['host']['proxy']) && Dever::$global['host']['proxy']) { $method = urlencode($method); return Dever::url(Dever::$global['host']['proxy'] . 'proxy_method=' . $method . '&' . $param); } return Dever::url($method . '?' . $param); } $data = Dever::input(); foreach($data as $k => $v) { Dever::setInput($k, $v); } return Dever::load(urldecode($data['proxy_method']), array('CHECK' => true)); } static public function script() { $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'].'";'; if(isset(Dever::$global['host']['proxy'])) { $script .= 'config.proxy="'.Dever::$global['host']['proxy'].'";'; } if(isset(Dever::$global['host']['manage'])) { $script .= 'config.manage_assets="'.Dever::$global['host']['manage'].'";'; } if(isset(Dever::$global['host']['workspace'])) { $script .= 'config.workspace="'.Dever::$global['host']['workspace'].'";'; } if(isset(Dever::$global['base']['layout'])) { $script .= 'config.layout = "'.Dever::$global['base']['layout'].'";$(document).ready(function() { $(document).pjax("a", "'.Dever::$global['base']['layout'].'"); $(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});}); $(document).on("pjax:start", function() { NProgress.start(); }); $(document).on("pjax:end", function() { NProgress.done(); }); });'; } return $script; } /** * 获取文件后缀 * @param string $file * * @return string */ static public function ext($file) { return pathinfo($file, PATHINFO_EXTENSION); } /** * markdown解析 * @param string $value * * @return string */ static public function markdown($value) { self::$markdown = self::$markdown ? self::$markdown : new \Parsedown(); # 一个回车就换行 self::$markdown->setBreaksEnabled(true); # 过滤掉所有特殊代码 return self::$markdown->text($value); } /** * setup 安装引导程序 * @param string $value * * @return string */ static public function setup() { $host = DEVER_APP_HOST; $path = DEVER_APP_PATH . '../'; $dir = scandir($path); foreach($dir as $k => $v) { if($v != '.' && $v != '..' && is_dir($path . $v)) { $url = str_replace(array('www', 'main'), $v, $host); Dever::curl($url); echo $url . ' 安装成功!
' . "\r\n"; } } return '感谢您使用Dever!'; } /** * cache 缓存功能 * @param string $key * @param array $data * @param int $expire * @param string $type * * @return mixed */ static public function cache($key = false, $data = false, $expire = 0, $type = 'data') { $cache = self::$global['cache']; if(empty($cache[$type])) { return; } //$handle =& \Dever\Cache\Handle::getInstance($type, $cache[$type]); $handle = \Dever\Cache\Handle::getInstance($type, $cache[$type]); if(is_string($data) && $data == 'delete') { return $handle->delete($key); } elseif($data) { return $handle->set($key, $data, $expire); } return $handle->get($key); } /** * dyna 动态解析功能 * @param string $key * @param array $data * * @return mixed */ static public function odyna($key = false, $data = array()) { return (object)self::dyna($key, $data); } /** * dyna 动态解析功能 * @param string $key * @param array $data * * @return mixed */ static public function dyna($key = false, $data = array()) { if(isset(self::$global['dyna_data'][$key])) { return self::$global['dyna_data'][$key]; } $config = self::get('dyna'); if(empty($config[$key])) { return $key; } if(!$data) { return $config[$key]; } if(is_array($config[$key])) { foreach($config[$key] as $k => $v) { $v = '$value = "'.$v.'";'; eval($v); if(isset($value)) { $config[$key][$k] = $value; } } } else { $v = '$value = "'.$config[$key].'";'; eval($v); if(isset($value)) { $config[$key] = $value; } } self::$global['dyna_data'][$key] = $config[$key]; return $config[$key]; } /** * submit 处理重复提交功能 此处暂时有问题 * @param string $type * @param int $value * * @return mixed */ static public function submit($type = 'update', $value = 1) { if(empty(self::$save)) { self::$save = new \Dever\Session\Save(DEVER_APP_NAME, 'cookie'); } if($type == 'check') { $submit = self::$save->get('submit'); if($submit == 2) { Dever::abert('请不要重复提交'); } } if($value) { self::$save->add('submit', $value); } } }