123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
- class Maze extends Maze\Config\Load
- {
- /**
- * 注册maze类库里常用的方法,用法为Maze:path('');目前仅支持三个参数以下的方法
- */
- static private $register = array
- (
- 'path' => array('File\\Path','create'),
- 'page' => array('Page\\Main','getPage'),
- 'total' => array('Page\\Main','getTotal'),
- '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'),
- 'load' => array('Routing\\Load','get'),
- 'object' => array('Routing\\Load','object'),
- 'config' => array('Config\\Load','get'),
- 'project' => array('Config\\Project','load'),
- 'debug' => array('Debug\\Process','wait'),
- 'view' => array('Template\\View','get'),
- );
- /**
- * 设置循环里每行不同的值
- * @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;
- }
- }
- /**
- * 获取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 $thead
- * @param string $tbody
- * @param string $class
- *
- * @return array
- *
- static public function table($thead, $tbody, $class = '')
- {
- $result = '<table class='.$class.'><tr>';
- foreach($thead as $k => $v)
- {
- $result .= '<td style=\'width:50%\'>' . $v . '</td>';
- }
- $result .= '</tr>';
- foreach($tbody as $k => $v)
- {
- $result .= '<tr><td>' . $k . '</td><td>' . $v . '</td></tr>';
- }
- $result .= '</table>';
- return $result;
- }
-
- /**
- * 生成table
- * @param string $data
- * @param string $class
- *
- * @return array
- */
- static public function table($data, $class = '')
- {
- if($class)
- {
- $style = 'class='.$class.'';
- }
- else
- {
- $style = 'border=1 width=100% height=100%';
- }
-
- $html = '<table '.$style.'>';
-
- foreach($data as $k => $v)
- {
- if(is_array($v))
- {
- $v = self::table($v, $class);
- //$v = var_export($v, true);
- }
- $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>';
- }
-
- $html .= '</table>';
- return $html;
- }
- /**
- * 获取pic
- * @param string $value
- * @param string $index
- *
- * @return array
- */
- static public function pic($pic, $name)
- {
- if(strpos($pic, '_t') !== false)
- {
- $temp = explode('_', $pic);
- $pic = $temp[0] . '_' . $name . '.jpg';
- }
- return $pic;
- }
-
- /**
- * 获取_param
- * @param string $value
- * @param string $index
- *
- * @return array
- */
- static public function param($name)
- {
- if(isset(Maze::$global['base']['_param']))
- {
- if(isset(Maze::$global['base']['_param']['add_' . $name]))
- {
- return Maze::$global['base']['_param']['add_' . $name];
- }
-
- if(isset(Maze::$global['base']['_param']['set_' . $name]))
- {
- return Maze::$global['base']['_param']['set_' . $name];
- }
- }
- return false;
- }
- /**
- * 获取ip
- *
- * @return string
- */
- static public function 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'];
- }
- /**
- * 运维程序
- *
- * @return string
- */
- static public function daemon($interface, $project = false)
- {
- $project = $project ? $project : MAZE_PROJECT_NAME;
- $path = self::load('manage/project.path', $project);
- if(strpos($path, 'http://'))
- {
- //self::curl($path . $interface);
- system('curl ' . $path . $interface . ' > /dev/null &');
-
- }
- else
- {
- # ?和&无法解析
- $interface = str_replace(array('?', '&'), array('__', '^'), $interface);
- system('php ' . $path . 'index.php -send ' . $interface . ' > /dev/null &');
- }
- }
- static public function mdate($num)
- {
- if($num <= 0)
- {
- return '0S';
- }
- $num = time() - $num;
- //$num = 2*3600*24 + 1000;
-
- if($num <= 60)
- {
- return $num . 'S';
- }
- $config = array
- (
- //array(3600*24, 'D'),
- array(3600, 'H'),
- array(60, 'M'),
- array(1, 'S')
- );
- $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 = Maze::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'];
- Maze::load('manage/cron-update', $update);
- }
- }
- else
- {
- $update['add_name'] = $name;
- $update['add_ldate'] = $ldate;
- $update['add_interface'] = $interface;
- $update['add_time'] = $time;
- $update['add_project'] = $project;
- Maze::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;
- }
- /**
- * 执行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];
- }
- }
- /**
- * 注册maze类库里的常用类,方便使用
- * @param string $name
- * @param array $param
- *
- * @return mixed
- */
- static public function __callStatic($name, $param = array())
- {
- if(isset(self::$register[$name]))
- {
- $class = 'Maze\\' . self::$register[$name][0];
- $method = self::$register[$name][1];
- if(isset($param[2]))
- {
- return $class::$method($param[0], $param[1], $param[2]);
- }
- elseif(isset($param[1]))
- {
- return $class::$method($param[0], $param[1]);
- }
- elseif(isset($param[0]))
- {
- return $class::$method($param[0]);
- }
- else
- {
- return $class::$method();
- }
- }
- }
-
- static public function curl($url, $param = false, $type = '')
- {
- //初始化
- $ch = curl_init();
- //设置选项,包括URL
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- 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;
- }
- }
|