PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/wusheng/functions.php

https://bitbucket.org/wusheng/trackstar
PHP | 406 lines | 317 code | 45 blank | 44 comment | 77 complexity | 172e319276ef266ae4e2a735c8c1450d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /********* funcitons for debug **************/
  3. //short var_dump
  4. function dp()
  5. {
  6. $arg_list = func_get_args();
  7. echo '<pre>';
  8. foreach ($arg_list as $a)
  9. {
  10. var_dump($a);
  11. echo '<br>';
  12. }
  13. echo '</pre>';
  14. }
  15. //short var_dump with exit
  16. function dpe()
  17. {
  18. $arg_list = func_get_args();
  19. echo '<pre>';
  20. foreach ($arg_list as $a)
  21. {
  22. var_dump($a);
  23. echo '<br>';
  24. }
  25. echo '</pre>';
  26. exit;
  27. }
  28. //short var_export
  29. function ep()
  30. {
  31. $arg_list = func_get_args();
  32. echo '<pre>';
  33. foreach ($arg_list as $a)
  34. {
  35. var_export($a);
  36. echo '<br>';
  37. }
  38. echo '</pre>';
  39. }
  40. //short var_export with exit
  41. function epe()
  42. {
  43. $arg_list = func_get_args();
  44. echo '<pre>';
  45. foreach ($arg_list as $a)
  46. {
  47. var_export($a);
  48. echo '<br>';
  49. }
  50. echo '</pre>';
  51. exit;
  52. }
  53. //debug_backtrace short cut function
  54. function bt()
  55. {
  56. dpe(debug_backtrace ());
  57. }
  58. /************** shortcut functions **************/
  59. function g( $str )
  60. {
  61. return isset( $GLOBALS[$str] ) ? $GLOBALS[$str] : false;
  62. }
  63. function t( $str )
  64. {
  65. return trim($str);
  66. }
  67. function z( $str )
  68. {
  69. return strip_tags( $str );
  70. }
  71. function c( $str )
  72. {
  73. if(!empty($_SERVER[$str]))
  74. return $_SERVER[$str];
  75. return isset( $GLOBALS['config'][$str] ) ? $GLOBALS['config'][$str] : false;
  76. }
  77. function v($key){
  78. $value = isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : '');
  79. if(is_array($value) && sizeof($value) > 0)
  80. {
  81. for ($i = 0 ; $i < sizeof($value); $i++)
  82. $value[$i] = trim($value[$i]);
  83. }else
  84. $value = trim($value);
  85. return $value;
  86. }
  87. function vs($arr){
  88. $rst = array();
  89. if(is_array($arr) && sizeof($arr) > 0)
  90. {
  91. foreach (array_keys($arr) as $key)
  92. $rst[$key] = v($key);
  93. }
  94. return $rst;
  95. }
  96. /**
  97. * normalize the unit to M, return the data part
  98. */
  99. function unit_to_m($v)
  100. {
  101. preg_match('/\s*([0-9.]+)\s*([a-zA-Z]+)/', $v, $mat);
  102. $ret = 0;
  103. if(isset($mat[1]) && isset($mat[2]))
  104. {
  105. $unit = strtoupper(t($mat[2]));
  106. $val = t($mat[1]);
  107. if($unit == 'B')
  108. $ret = $val/1024/1024;
  109. elseif($unit == 'K' || $unit == 'KB')
  110. $ret = $val/1024;
  111. elseif($unit == 'M' || $unit == 'MB')
  112. $ret = $val;
  113. elseif($unit == 'G' || $unit == 'GB')
  114. $ret = $val*1024;
  115. elseif($unit == 'T' || $unit == 'TB')
  116. $ret = $val*1024*1024;
  117. elseif($unit == 'E' || $unit == 'EB')
  118. $ret = $val*1024*1024*1024;
  119. else
  120. return false;
  121. }
  122. else
  123. return false;
  124. return round($ret, 2);
  125. }
  126. /**
  127. * format the num data with MB unit
  128. */
  129. function normal_m_data($data)
  130. {
  131. $len = strlen($data);
  132. if($len < 3)
  133. return intval($data).'M';
  134. elseif($len < 6)
  135. return intval($data/1024).'G';
  136. elseif($len < 9)
  137. return intval($data/1024/1024).'T';
  138. else
  139. return intval($data/1024/1024/1024).'E';
  140. }
  141. /**
  142. * @desc e.g: unix time to 2years5days45hours53mins42secs
  143. */
  144. function sec2time($time, $units=null)
  145. {
  146. if(!$units)
  147. $units = c('timeunits_en_us');
  148. if(is_numeric($time))
  149. {
  150. $value = array();
  151. if($time >= 31556926)
  152. {
  153. $value["year"] = floor($time/31556926);
  154. $time = ($time%31556926);
  155. }
  156. if($time >= 86400)
  157. {
  158. $value["day"] = floor($time/86400);
  159. $time = ($time%86400);
  160. }
  161. if($time >= 3600)
  162. {
  163. $value["hour"] = floor($time/3600);
  164. $time = ($time%3600);
  165. }
  166. if($time >= 60)
  167. {
  168. $value["minute"] = floor($time/60);
  169. $time = ($time%60);
  170. }
  171. $value["second"] = floor($time);
  172. $str = '';
  173. if( isset( $value['year'] ) ) $str .= $value['year'] . $units['year'];
  174. if( isset( $value['day'] ) ) $str .= $value['day'] . $units['day'];
  175. if( isset( $value['hour'] ) ) $str .= $value['hour'] . $units['hour'];
  176. if( isset( $value['minute'] ) ) $str .= $value['minute'] . $units['min'];
  177. if( isset( $value['second'] ) ) $str .= $value['second'] . $units['sec'];
  178. return $str;
  179. }
  180. else
  181. return false;
  182. }
  183. /**
  184. * @param mixed $sz Bytes unit
  185. * @return string
  186. */
  187. function smart_size($sz)
  188. {
  189. if($sz < 1024)
  190. return $sz.' Bytes';
  191. elseif($sz <1048576)
  192. return round($sz/1024,2).' KB';
  193. elseif($sz < 1073741824)
  194. return round($sz/1048576,2).' MB';
  195. else
  196. return round($sz/1073741824,2).' GB';
  197. }
  198. /**
  199. * @param mixed $month Values from 1 to 12
  200. * @param mixed $year
  201. * @return string the days count of a month of a year
  202. */
  203. function days_in_month($year, $month)
  204. {
  205. return date('t', mktime(0, 0, 0, $month, 0, $year));
  206. }
  207. /**
  208. * @return boolean
  209. * @note: the is just an func example
  210. */
  211. function is_intranet_ip()
  212. {
  213. $ip=$_SERVER['REMOTE_ADDR'];
  214. if(!$ip) return false;
  215. $available_ips = array(
  216. array('low' => '10.0.0.0', 'high' => '10.255.255.255'),
  217. array('low' => '172.16.0.0', 'high' => '172.31.255.255'),
  218. array('low' => '192.168.0.0', 'high' => '192.168.255.255'),
  219. );
  220. $ip=ip2long($ip);
  221. foreach($available_ips as $v)
  222. {
  223. if($ip >= ip2long($v['low']) && $ip <= ip2long($v['high']))
  224. return true;
  225. }
  226. return false;
  227. }
  228. function get_client_ip()
  229. {
  230. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  231. return $_SERVER['HTTP_X_FORWARDED_FOR'];
  232. else if(isset($_SERVER['HTTP_CLIENT_IP']))
  233. return $_SERVER['HTTP_CLIENT_IP'];
  234. else if($_SERVER['REMOTE_ADDR'])
  235. return $_SERVER['REMOTE_ADDR'];
  236. else
  237. return false;
  238. }
  239. /**
  240. * @param unknown_type $url
  241. * @param unknown_type $posts
  242. * @param unknown_type $headers
  243. * @param unknown_type $basic
  244. * @param unknown_type $params
  245. * @return mixed|string >>> todo log the error rest
  246. */
  247. function http_request($url,$posts = array(),$headers=array(), $basic = null, $params=null)
  248. {
  249. $params['cntout'] = isset($params['cntout'])?$params['cntout']:3;
  250. $params['tout'] = isset($params['tout'])?$params['tout']:15;
  251. $ch=curl_init();
  252. curl_setopt($ch,CURLOPT_URL,$url);
  253. if(!empty($posts))
  254. {
  255. curl_setopt($ch,CURLOPT_POST,true);
  256. curl_setopt($ch,CURLOPT_POSTFIELDS,$posts);
  257. }
  258. if($basic)
  259. {
  260. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  261. curl_setopt($ch, CURLOPT_USERPWD, "{$basic['username']}:{$basic['password']}");
  262. }
  263. if(!empty($headers)) curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
  264. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  265. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$params['cntout']);
  266. curl_setopt($ch,CURLOPT_TIMEOUT,$params['tout']);
  267. $txt=curl_exec($ch);
  268. curl_close($ch);
  269. $txt = ltrim($txt,'#');
  270. $ret=json_decode($txt,true);
  271. if($ret)
  272. return $ret;
  273. return $txt;
  274. }
  275. /**
  276. * the rule was copied from http://www.10086c.com/index.php/2411.html
  277. * @return the service provider of the mobile number
  278. */
  279. function get_mobile_sp($mobile)
  280. {
  281. $prefix = intval(substr($mobile,0,3));
  282. if( ($prefix >= 134 && $prefix <= 139)
  283. || ($prefix >=187 && $prefix <= 188)
  284. || ($prefix == 147 || $prefix == 182)
  285. || ($prefix >= 150 && $prefix <= 152)
  286. || ($prefix >= 157 && $prefix <= 159)
  287. ) return 'mobile';
  288. if
  289. (
  290. ($prefix >= 130 && $prefix <= 132)
  291. || ($prefix >= 155 && $prefix <= 156)
  292. || ($prefix >= 185 && $prefix <= 186)
  293. || ($prefix == 144)
  294. ) return 'unicom';
  295. if( in_array($prefix,array(133,153,180,189))) return 'telecom';
  296. return 'unknown';
  297. }
  298. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix="...")
  299. {
  300. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  301. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  302. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  303. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  304. preg_match_all($re[$charset], $str, $match);
  305. if(count($match[0]) <= $length)
  306. return $str;
  307. if(function_exists("mb_substr"))
  308. return mb_substr($str, $start, $length, $charset).$suffix;
  309. elseif(function_exists('iconv_substr'))
  310. return iconv_substr($str,$start,$length,$charset).$suffix;
  311. $slice = implode("",array_slice($match[0], $start, $length));
  312. return $slice.$suffix;
  313. }
  314. //corresponding to JS escape() func
  315. function unescape($escstr)
  316. {
  317. preg_match_all("/%u[0-9A-Za-z]{4}|%.{2}|[0-9a-zA-Z.+-_]+/", $escstr, $matches);
  318. $ar = &$matches[0];
  319. $c = "";
  320. foreach($ar as $val)
  321. {
  322. if (substr($val, 0, 1) != "%")
  323. $c .= $val;
  324. elseif (substr($val, 1, 1) != "u")
  325. {
  326. $x = hexdec(substr($val, 1, 2));
  327. $c .= chr($x);
  328. }
  329. else
  330. {
  331. $val = intval(substr($val, 2), 16);
  332. if ($val < 0x7F) // 0000-007F
  333. $c .= chr($val);
  334. elseif ($val < 0x800) // 0080-0800
  335. {
  336. $c .= chr(0xC0 | ($val / 64));
  337. $c .= chr(0x80 | ($val % 64));
  338. }
  339. else // 0800-FFFF
  340. {
  341. $c .= chr(0xE0 | (($val / 64) / 64));
  342. $c .= chr(0x80 | (($val / 64) % 64));
  343. $c .= chr(0x80 | ($val % 64));
  344. }
  345. }
  346. }
  347. return $c;
  348. }
  349. /************** log functions *****************/
  350. define('TRACK_LOG', './log.php') ;
  351. function flog($msg, $cat='debug', $logf=TRACK_LOG)
  352. {
  353. file_put_contents($logf, date('Y-m-d')."::$cat::".str_replace("\n", ' ', $msg)."\n", FILE_APPEND|LOCK_EX );
  354. }
  355. /*************** example functions ************/
  356. function get_signature($name, $version, $expire )
  357. {
  358. $rip = $_SERVER['REMOTE_ADDR'];
  359. $sig = hash_hmac('sha256','name:'+$name+'version'+$version+'ip:'+$rip+'expire:'+$expire,'secret_key',true);
  360. return $sig;
  361. }