🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ // 移除HTML标签 $this->request->filter('trim,strip_tags,htmlspecialchars'); ~~~ ~~~ if (!function_exists('is_really_writable')) { /** * 判断文件或文件夹是否可写 * @param string $file 文件或目录 * @return bool */ function is_really_writable($file) { if (DIRECTORY_SEPARATOR === '/') { return is_writable($file); } if (is_dir($file)) { $file = rtrim($file, '/') . '/' . md5(mt_rand()); if (($fp = @fopen($file, 'ab')) === false) { return false; } fclose($fp); @chmod($file, 0777); @unlink($file); return true; } elseif (!is_file($file) or ($fp = @fopen($file, 'ab')) === false) { return false; } fclose($fp); return true; } } ~~~ ~~~ if (!function_exists('rmdirs')) { /** * 删除文件夹 * @param string $dirname 目录 * @param bool $withself 是否删除自身 * @return boolean */ function rmdirs($dirname, $withself = true) { if (!is_dir($dirname)) { return false; } $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dirname, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST ); foreach ($files as $fileinfo) { $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); $todo($fileinfo->getRealPath()); } if ($withself) { @rmdir($dirname); } return true; } } ~~~ ~~~ if (!function_exists('copydirs')) { /** * 复制文件夹 * @param string $source 源文件夹 * @param string $dest 目标文件夹 */ function copydirs($source, $dest) { if (!is_dir($dest)) { mkdir($dest, 0755, true); } foreach ( $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ) as $item ) { if ($item->isDir()) { $sontDir = $dest . DS . $iterator->getSubPathName(); if (!is_dir($sontDir)) { mkdir($sontDir, 0755, true); } } else { copy($item, $dest . DS . $iterator->getSubPathName()); } } } } ~~~ ~~~ if (!function_exists('check_cors_request')) { /** * 跨域检测 */ function check_cors_request() { if (isset($_SERVER['HTTP_ORIGIN']) && $_SERVER['HTTP_ORIGIN']) { $info = parse_url($_SERVER['HTTP_ORIGIN']); // localhost,127.0.0.1 $domainArr = explode(',', config('fastadmin.cors_request_domain')); $domainArr[] = request()->host(true); if (in_array("*", $domainArr) || in_array($_SERVER['HTTP_ORIGIN'], $domainArr) || (isset($info['host']) && in_array($info['host'], $domainArr))) { header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']); } else { $response = Response::create('跨域检测无效', 'html', 403); throw new HttpResponseException($response); } header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) { header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); } if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) { header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); } $response = Response::create('', 'html'); throw new HttpResponseException($response); } } } } ~~~ ``` // 生成随机字符串 function generate_rand($l){ $c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; srand((double)microtime()*1000000); for($i=0; $i < $l; $i++) { $rand.= $c[rand()%strlen($c)]; } return $rand; } ``` ``` // 邮箱验证 function is_valid_email($email, $test_mx = false) { if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) if($test_mx) { list($username, $domain) = split("@", $email); return getmxrr($domain, $mxrecords); } else return true; else return false; } ``` ~~~ if (!function_exists('get_zodiac_sign')) { /** * 根据月、日获取星座 * * @param unknown $month 月 * @param unknown $day 日 * @return boolean|multitype: * @author 牧羊人 * @date 2020-04-21 */ function get_zodiac_sign($month, $day) { // 检查参数有效性 if ($month < 1 || $month > 12 || $day < 1 || $day > 31) { return false; } // 星座名称以及开始日期 $signs = array( array("20" => "水瓶座"), array("19" => "双鱼座"), array("21" => "白羊座"), array("20" => "金牛座"), array("21" => "双子座"), array("22" => "巨蟹座"), array("23" => "狮子座"), array("23" => "处女座"), array("23" => "天秤座"), array("24" => "天蝎座"), array("22" => "射手座"), array("22" => "摩羯座") ); list($sign_start, $sign_name) = each($signs[(int)$month - 1]); if ($day < $sign_start) { list($sign_start, $sign_name) = each($signs[($month - 2 < 0) ? $month = 11 : $month -= 2]); } return $sign_name; } } ~~~ ~~~ if (!function_exists('get_device_type')) { /** * 获取设备类型(苹果或安卓) * @return int 返回结果 * @author 牧羊人 * @date 2020-04-21 */ function get_device_type() { // 全部变成小写字母 $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $type = 0; // 分别进行判断 if (strpos($agent, 'iphone') !== false || strpos($agent, 'ipad') !== false) { $type = 1; } if (strpos($agent, 'android') !== false) { $type = 2; } return $type; } } ~~~ ~~~ if (!function_exists('get_ip_city')) { /** * 根据IP获取城市 * @param string $ip IP地址 * @return bool|mixed|string * @author 牧羊人 * @since 2021/9/23 */ function get_ip_city($ip = '') { if (empty($ip)) { return '请输入IP地址'; } $res = @file_get_contents('http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=' . $ip); if (empty($res)) { return ""; } return isset($res['city']) ? $res['city'] : ""; } } ~~~ ~~~ if (!function_exists('is_email')) { /** * 判断是否为邮箱 * @param string $str 邮箱 * @return false 返回结果true或false * @author 牧羊人 * @date 2020-04-21 */ function is_email($str) { return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $str); } } if (!function_exists('is_mobile')) { /** * 判断是否为手机号 * @param string $mobile 手机号码 * @return false 返回结果true或false * @author 牧羊人 * @date 2020-04-21 */ function is_mobile($mobile) { // return preg_match('/^1(3|4|5|6|7|8|9)\d{9}$/', $mobile); return preg_match('/^1[3456789]{1}\d{9}$/', $mobile); } } if (!function_exists('is_zipcode')) { /** * 验证邮编是否正确 * @param string $code 邮编 * @return false 返回结果true或false * @author 牧羊人 * @date 2020-04-21 */ function is_zipcode($code) { return preg_match('/^[1-9][0-9]{5}$/', $code); } } if (!function_exists('is_idcard')) { /** * 验证身份证是否正确 * @param string $idno 身份证号 * @return bool 返回结果true或false * @author 牧羊人 * @date 2020-04-21 */ function is_idcard($idno) { $idno = strtoupper($idno); $regx = '/(^\d{15}$)|(^\d{17}([0-9]|X)$)/'; $arr_split = array(); if (!preg_match($regx, $idno)) { return false; } // 检查15位 if (15 == strlen($idno)) { $regx = '/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/'; @preg_match($regx, $idno, $arr_split); $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4]; if (!strtotime($dtm_birth)) { return false; } else { return true; } } else { // 检查18位 $regx = '/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/'; @preg_match($regx, $idno, $arr_split); $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4]; // 检查生日日期是否正确 if (!strtotime($dtm_birth)) { return false; } else { // 检验18位身份证的校验码是否正确。 // 校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); $sign = 0; for ($i = 0; $i < 17; $i++) { $b = (int)$idno{ $i}; $w = $arr_int[$i]; $sign += $b * $w; } $n = $sign % 11; $val_num = $arr_ch[$n]; if ($val_num != substr($idno, 17, 1)) { return false; } else { return true; } } } } } ~~~ ~~~ if (!function_exists('is_image')) { /** * 判断是否为图片格式 * @param $filename * @return bool|false|int * @author 牧羊人 * @since 2021/7/10 */ function is_image($filename) { $types = '.gif|.GIF|.jpg|.JPG|.jpeg|.JPEG|.png|.PNG|.bmp|.BMP'; //定义检查的图片类型 if (file_exists($filename)) { $info = getimagesize($filename); $ext = image_type_to_extension($info['2']); return stripos($types, $ext); } else { return false; } } } ~~~ ~~~ if (!function_exists('mkdirs')) { /** * 递归创建目录 * @param string $dir 需要创建的目录路径 * @param int $mode 权限值 * @return bool 返回结果true或false * @author 牧羊人 * @date 2020-04-21 */ function mkdirs($dir, $mode = 0777) { if (is_dir($dir) || mkdir($dir, $mode, true)) { return true; } if (!mkdirs(dirname($dir), $mode)) { return false; } return mkdir($dir, $mode, true); } } ~~~