Helper.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace Heanup\Frame;
  3. class Helper
  4. {
  5. /*
  6. * 检测链接是否是SSL连接
  7. * @return bool
  8. */
  9. public static function isSSL()
  10. {
  11. if (!isset($_SERVER['HTTPS']))
  12. return false;
  13. if ($_SERVER['HTTPS'] === 1) { //Apache
  14. return true;
  15. } elseif ($_SERVER['HTTPS'] === 'on') { //IIS
  16. return true;
  17. } elseif ($_SERVER['SERVER_PORT'] == 443) { //其他
  18. return true;
  19. }
  20. return false;
  21. }
  22. /**
  23. * 随机数生成器
  24. */
  25. public static function random()
  26. {
  27. return md5(microtime() . rand() . uniqid() . $_SERVER['REMOTE_ADDR']);
  28. }
  29. /**
  30. * 简单验证email
  31. * @param $email
  32. * @return bool
  33. */
  34. public static function validateEmail($email)
  35. {
  36. $email = trim($email);
  37. if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
  38. return False;
  39. }
  40. return True;
  41. }
  42. /**
  43. * 简单验证是否有效的URL
  44. * @param $url
  45. * @return bool
  46. */
  47. public static function validateURL($url)
  48. {
  49. $url = trim($url);
  50. if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
  51. return False;
  52. }
  53. return True;
  54. }
  55. /**
  56. * 转换为友好时间
  57. * @param int|string $timestamp
  58. * @return string
  59. */
  60. public static function convertToFriendlyTime($timestamp)
  61. {
  62. if (!is_numeric($timestamp)) {
  63. $timestamp = strtotime($timestamp);
  64. }
  65. $now = time();
  66. $interval = abs($now - $timestamp);
  67. $today = strtotime(date("Ymd"));
  68. $tomorrow = strtotime(date("Ymd", $now + 86400));
  69. $thisYear = strtotime(date("Y-01-01 00:00"));
  70. if ($interval < 60) {
  71. if ($interval < 10) {
  72. $interval = 10;
  73. }
  74. $string = $interval . "秒前";
  75. } else if ($interval < 3600) {
  76. $string = floor($interval / 60) . '分钟前';
  77. } else if ($today < $timestamp AND $timestamp < $tomorrow) {
  78. $string = '今天' . date('H:i', $timestamp);
  79. } else if ($thisYear < $timestamp) {
  80. $string = date('n月j日', $timestamp);
  81. } else {
  82. $string = date('Y-m-d', $timestamp);
  83. }
  84. return $string;
  85. }
  86. /**
  87. * 计算字符串长度,汉字按两个字符
  88. * @param string $str
  89. * @return int
  90. */
  91. public static function strlenUtf8($str)
  92. {
  93. $length = strlen(preg_replace('/[u4e00-u9fa5 ]/', '', $str));
  94. if ($length) {
  95. if($length < 3){
  96. return $length;
  97. }
  98. return strlen($str) - $length + intval($length / 3) * 2;
  99. } else {
  100. return strlen($str);
  101. }
  102. }
  103. /**
  104. * 获取服务器IP
  105. * @param bool $toLong 是否返回整形
  106. * @return string
  107. */
  108. public static function getServerIp($toLong = false)
  109. {
  110. if (isset($_SERVER['SERVER_ADDR'])) {
  111. $ip = $_SERVER['SERVER_ADDR'];
  112. } else {
  113. $domain = gethostname();
  114. $ip = gethostbyname($domain);
  115. }
  116. if ($toLong) {
  117. $ip = ip2long($ip);
  118. }
  119. return $ip;
  120. }
  121. /**
  122. * 获取客户端Ip
  123. * @param bool $toLong 是否返回整形
  124. * @return string|int
  125. */
  126. public static function getClientIp($toLong = false)
  127. {
  128. $ip = '';
  129. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]) && $_SERVER["HTTP_X_FORWARDED_FOR"]) {
  130. $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
  131. } elseif (isset($_SERVER["HTTP_CLIENT_IP"]) && $_SERVER["HTTP_CLIENT_IP"]) {
  132. $ip = $_SERVER["HTTP_CLIENT_IP"];
  133. } elseif (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"]) {
  134. $ip = $_SERVER["REMOTE_ADDR"];
  135. }
  136. $pos = strpos($ip, ',');
  137. if ($pos) {
  138. $ip = substr($ip, 0, $pos);
  139. }
  140. $ip = trim($ip);
  141. if ($toLong) {
  142. $ip = ip2long($ip);
  143. }
  144. return $ip;
  145. }
  146. /**
  147. * 从 $argv 中解析合并参数, 兼容一下情况:
  148. * 无前缀, 被解析成索引数组
  149. * 单前缀(只能是单字母, 后面可以直接跟value, 也可以空格后面跟着value,二者不能混用), 被解析成关联数组
  150. * 双前缀形式(可以是单字母也可以是多字母,后面必需跟着 '='), 被解析成关联数组
  151. * 如果不确定, 请自行打印结果查看是否正确
  152. * @example : php index.php first -s 'second' --third='third'
  153. * @return array('first', 's' => 'second', 'third' => 'third')
  154. */
  155. public static function getArguments()
  156. {
  157. $arguments = $_SERVER['argv'];
  158. for ($i = 0; $i < $_SERVER['argc']; $i++) {
  159. if (!isset($arguments[$i][0])) {
  160. continue;
  161. }
  162. //判断第一位是否是 '-'
  163. if (substr($arguments[$i], 0, 1) != '-') {
  164. continue;
  165. }
  166. //判断第二位是否是 '-'
  167. if (substr($arguments[$i], 1, 1) != '-') {
  168. if (strlen($arguments[$i]) == 2) {
  169. $key = substr($arguments[$i], 1);
  170. $value = $arguments[$i + 1];
  171. $arguments[$key] = trim($value);
  172. unset($arguments[$i]);
  173. unset($arguments[$i + 1]);
  174. } elseif (strlen($arguments[$i]) > 2) {
  175. $key = substr($arguments[$i], 1, 1);
  176. $value = substr($arguments[$i], 2);
  177. $arguments[$key] = trim($value);
  178. unset($arguments[$i]);
  179. }
  180. } else {
  181. $value = substr($arguments[$i], 2);
  182. $value = explode('=', $value, 2);
  183. $arguments[$value[0]] = trim($value[1]);
  184. unset($arguments[$i]);
  185. }
  186. }
  187. return $arguments;
  188. }
  189. /**
  190. * 尝试解析字符串,如果解析失败则返回原值
  191. * @param $string
  192. * @return string | array
  193. */
  194. public static function jsonDecode($string)
  195. {
  196. if (!$string) {
  197. return $string;
  198. }
  199. $result = json_decode($string, true);
  200. if ($result) {
  201. $string = $result;
  202. }
  203. return $string;
  204. }
  205. /**
  206. * 获取数组分片, 从指定列表中获取指定元素之后的若干个切片
  207. * @param $idList
  208. * @param $lastId
  209. * @param $count
  210. * @return array
  211. */
  212. public static function getSlice($idList, $lastId = 0, $count = 20)
  213. {
  214. $result = array();
  215. if (!$idList || ($lastId && !in_array($lastId, $idList))) {
  216. return $result;
  217. }
  218. if ($lastId) {
  219. $position = array_search($lastId, $idList) + 1;
  220. } else {
  221. $position = 0;
  222. }
  223. $result = array_slice($idList, $position, $count);
  224. return $result;
  225. }
  226. /**
  227. * 创建目录
  228. * @param string $dir
  229. * @param integer $mode
  230. */
  231. public static function mkdir($dir, $mode = 0755)
  232. {
  233. if (!is_dir($dir)) {
  234. $temp = explode('/', $dir);
  235. $cur_dir = '';
  236. for ($i = 0; $i < count($temp); $i++) {
  237. $cur_dir .= $temp[$i] . '/';
  238. if (!is_dir($cur_dir)) {
  239. @mkdir($cur_dir, 0777);
  240. @chmod($cur_dir, $mode);
  241. }
  242. }
  243. }
  244. }
  245. public static function copydir($strSrcDir, $strDstDir)
  246. {
  247. $dir = opendir($strSrcDir);
  248. if (!$dir) {
  249. return false;
  250. }
  251. if (!is_dir($strDstDir)) {
  252. if (!mkdir($strDstDir)) {
  253. return false;
  254. }
  255. }
  256. while (false !== ($file = readdir($dir))) {
  257. if (($file != '.') && ($file != '..')) {
  258. if (is_dir($strSrcDir . '/' . $file)) {
  259. if (!self::copydir($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
  260. return false;
  261. }
  262. } else {
  263. if (!copy($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
  264. return false;
  265. }
  266. }
  267. }
  268. }
  269. closedir($dir);
  270. return true;
  271. }
  272. /**
  273. * 获取强密码
  274. * @param int $length
  275. * @return string
  276. */
  277. public static function getStrongCode($length = 8)
  278. {
  279. $length = $length > 8 ? intval($length) : 8;
  280. $arr = array();
  281. // $arr[] = self::getRandoms($length - 4);
  282. $arr[] = strtolower(self::getRandoms($length, 'char'));
  283. // $arr[] = strtolower(self::getRandoms(1, 'char'));
  284. $arr[] = mt_rand(0, 9);
  285. // $special = '!@#$%^*()';
  286. // $arr[] = $special{mt_rand(0, 11)};
  287. shuffle($arr);
  288. return implode('', $arr);
  289. }
  290. /**
  291. * 获取随机数
  292. *
  293. * @param int $length
  294. * @param string $type (可选)
  295. * @return string
  296. */
  297. public static function getRandoms($length = 4, $type = '')
  298. {
  299. $returnStr = '';
  300. $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
  301. if ($type == 'char') {
  302. $start = 10;
  303. $end = 61;
  304. } elseif ($type == 'num') {
  305. $start = 0;
  306. $end = 9;
  307. } else {
  308. $start = 0;
  309. $end = 61;
  310. }
  311. for ($i = 0; $i < $length; $i++) {
  312. $returnStr .= $pattern{mt_rand($start, $end)}; // 生成php随机数
  313. }
  314. return $returnStr;
  315. }
  316. }