ページのフロントエンドでよく使われる小さな関数のまとめ、ページ分け、中国語の切り取り、ファイルの書き込み、など
24614 ワード
<?php
if( !defined('CORE') ) exit('Request Error!');
/**
*
*
* lib_common
*
* @author itprato<2500875@qq>
* @version $Id$
*/
class util
{
public static $client_ip = null;
public static $cfc_handle = null;
/**
* IP
*
* @param
* @return void
*/
public static function get_client_ip()
{
static $realip = NULL;
if( self::$client_ip !== NULL )
{
return self::$client_ip;
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR2']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR2']);
foreach ($arr as $ip)
{
$ip = trim($ip);
if ($ip != 'unknown')
{
$realip = $ip;
break;
}
}
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($arr as $ip)
{
$ip = trim($ip);
if ($ip != 'unknown' )
{
$realip = $ip;
break;
}
}
}
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
{
$realip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
if (isset($_SERVER['REMOTE_ADDR']))
{
$realip = $_SERVER['REMOTE_ADDR'];
}
else
{
$realip = '0.0.0.0';
}
}
preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
self::$client_ip = ! empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
return self::$client_ip;
}
/**
*
*/
function put_file($file, $content, $flag = 0)
{
$pathinfo = pathinfo ( $file );
if (! empty ( $pathinfo ['dirname'] ))
{
if (file_exists ( $pathinfo ['dirname'] ) === false)
{
if (@mkdir ( $pathinfo ['dirname'], 0777, true ) === false)
{
return false;
}
}
}
if ($flag === FILE_APPEND)
{
return @file_put_contents ( $file, $content, FILE_APPEND );
}
else
{
return @file_put_contents ( $file, $content, LOCK_EX );
}
}
/**
* Url
*/
public static function get_cururl()
{
if(!empty($_SERVER["REQUEST_URI"]))
{
$scriptName = $_SERVER["REQUEST_URI"];
$nowurl = $scriptName;
}
else
{
$scriptName = $_SERVER["PHP_SELF"];
$nowurl = empty($_SERVER["QUERY_STRING"]) ? $scriptName : $scriptName."?".$_SERVER["QUERY_STRING"];
}
return $nowurl;
}
/**
*
* @parem $path
* @return bool
*/
public static function path_exists( $path )
{
$pathinfo = pathinfo ( $path . '/tmp.txt' );
if ( !empty( $pathinfo ['dirname'] ) )
{
if (file_exists ( $pathinfo ['dirname'] ) === false)
{
if (mkdir ( $pathinfo ['dirname'], 0777, true ) === false)
{
return false;
}
}
}
return $path;
}
/**
* utf8
* @parem $str
* @return bool
*/
public static function is_utf8($str)
{
if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32"))
{
return true;
}
else
{
return false;
}
}
/**
*
*
* @param array $config
* $config['current_page'] //
* $config['page_size'] //
* $config['total_rs'] //
* $config['url_prefix'] //
* $config['page_name'] // ( page_no, url_prefix&page_no=xxx )
* $config['move_size'] // ( 5)
* $config['input'] // (0|1)
* :
* <div class="page">
* <span class="nextprev">« </span>
* <span class="current">1</span>
* <a href="">2</a>
* <a href="" class="nextprev"> »</a>
* <span> 100 </span>
* </div>
*
* @return string
*/
public static function pagination ( $config )
{
//
$url_prefix = empty($config['url_prefix']) ? '' : $config['url_prefix'];
$current_page = empty($config['current_page']) ? 1 : intval($config['current_page']);
$page_name = empty($config['page_name']) ? 'page_no' : $config['page_name'];
$page_size = empty($config['page_size']) ? 0 : intval($config['page_size']);
$total_rs = empty($config['total_rs']) ? 0 : intval($config['total_rs']);
$total_page = ceil($total_rs / $page_size);
$move_size = empty($config['move_size']) ? 5 : intval($config['move_size']);
//
if( $total_page < 2 )
{
return '';
}
//
$pages = '<div class="pagenumber">';
//
$next_page = $current_page + 1;
//
$prev_page = $current_page - 1;
//
$last_page = $total_page;
// 、
if( $current_page > 1 )
{
$pages .= "<a href='{$url_prefix}' class='first-page'> </a>
";
$pages .= "<a href='{$url_prefix}&{$page_name}={$prev_page}' class='prev'>«<</a>
";
}
else
{
$pages .= "<a class='first-page'> </a>
";
$pages .= "<a class='prev'>«<</a>
";
}
//
for( $i = $current_page - $move_size; $i < $current_page; $i++ )
{
if ($i < 1) {
continue;
}
$pages .= "<a href='{$url_prefix}&{$page_name}={$i}'>$i</a>
";
}
//
$pages .= "<span class='current'>" . $current_page . "</span>
";
//
$flag = 0;
if ( $current_page < $total_page )
{
for ($i = $current_page + 1; $i <= $total_page; $i++)
{
$pages .= "<a href='{$url_prefix}&{$page_name}={$i}'>$i</a>
";
$flag++;
if ($flag == $move_size)
{
break;
}
}
}
// 、
if( $current_page != $total_page )
{
$pages .= "<a href='{$url_prefix}&{$page_name}={$next_page}' class='next'>>»</a>
";
$pages .= "<a href='{$url_prefix}&{$page_name}={$last_page}' class='last-page'> </a>
";
}
else
{
$pages .= "<a class='next'>>»</a>
";
$pages .= "<a class='last-page'> </a>
";
}
//
if( !empty($config['input']) )
{
$pages .= '<input type="text" class="page" onkeydown="javascript:if(event.keyCode==13){ location=\''.$url_prefix.'&'.$page_name.'=\'+this.value; }" onkeyup="value=value.replace(/[^\d]/g,\'\')" />';
}
$pages .= "<span> {$total_page} / {$total_rs} </span>
";
$pages .= '</div>';
return $pages;
}
/**
* utf8 2,
* mbstring
* @return string
*/
public static function utf8_substr($str, $slen, $startdd=0)
{
return mb_substr($str , $startdd , $slen , 'UTF-8');
}
/**
* Linux (strtotime )
* @parem string $dtime
* @return int
*/
public static function cn_strtotime( $dtime )
{
if(!preg_match("/[^0-9]/", $dtime))
{
return $dtime;
}
$dtime = trim($dtime);
$dt = Array(1970, 1, 1, 0, 0, 0);
$dtime = preg_replace("/[\r
\t]| | /", " ", $dtime);
$dtime = str_replace(" ", "-", $dtime);
$dtime = str_replace(" ", "-", $dtime);
$dtime = str_replace(" ", ":", $dtime);
$dtime = str_replace(" ", ":", $dtime);
$dtime = trim(preg_replace("/[ ]{1,}/", " ", $dtime));
$ds = explode(" ", $dtime);
$ymd = explode("-", $ds[0]);
if(!isset($ymd[1]))
{
$ymd = explode(".", $ds[0]);
}
if(isset($ymd[0]))
{
$dt[0] = $ymd[0];
}
if(isset($ymd[1])) $dt[1] = $ymd[1];
if(isset($ymd[2])) $dt[2] = $ymd[2];
if(strlen($dt[0])==2) $dt[0] = '20'.$dt[0];
if(isset($ds[1]))
{
$hms = explode(":", $ds[1]);
if(isset($hms[0])) $dt[3] = $hms[0];
if(isset($hms[1])) $dt[4] = $hms[1];
if(isset($hms[2])) $dt[5] = $hms[2];
}
foreach($dt as $k=>$v)
{
$v = preg_replace("/^0{1,}/", '', trim($v));
if($v=='')
{
$dt[$k] = 0;
}
}
$mt = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
if(!empty($mt))
{
return $mt;
}
else
{
return strtotime( $dtime );
}
}
/**
*
* @param array $to
* @param string $subject
* @param string $body
* @return bool
* @author xiaocai
*/
public static function send_email($to, $subject, $body)
{
$send_account = $GLOBALS['config']['send_smtp_mail_account'];
try
{
$smtp = new cls_mail($send_account['host'], $send_account['port'], true, $send_account['user'], $send_account['password']);
$smtp->debug = $send_account['debug'];
$result = $smtp->sendmail($to, $send_account['from'], $subject, $body, $send_account['type']);
return $result;
}
catch( Exception $e )
{
return false;
}
}
// urlencode
public static function q_encode($str)
{
$data_code = "";
$data = array_filter(explode(" ",$str));
$data = array_flip(array_flip($data));
foreach ($data as $ss) {
if (strlen($ss)>1 )
$data_code .= str_replace("%","",urlencode($ss)) . " ";
}
$data_code = trim($data_code);
return $data_code;
}
public static function get_domain($url){
$pattern = "/[\w-]+\.(com|net|org|gov|cc|biz|info|cn)(\.(cn|hk))*/";
preg_match($pattern, $url, $matches);
if(count($matches) > 0) {
return $matches[0];
}else{
$rs = parse_url($url);
$main_url = $rs["host"];
if(!strcmp(long2ip(sprintf("%u",ip2long($main_url))),$main_url)) {
return $main_url;
}else{
$arr = explode(".",$main_url);
$count=count($arr);
$endArr = array("com","net","org","3322");//com.cn net.cn
if (in_array($arr[$count-2],$endArr)){
$domain = $arr[$count-3].".".$arr[$count-2].".".$arr[$count-1];
}else{
$domain = $arr[$count-2].".".$arr[$count-1];
}
return $domain;
}// end if(!strcmp...)
}// end if(count...)
}// end function
/**
*
* @param string $birthday Y-m-d
* @return <type>
*/
function get_age($birthday)
{
if(!$birthday) return "-";
$by = substr($birthday, 0,4);
$bm = substr($birthday, 4,2);
$bd = substr($birthday, 6,2);
list($y, $m, $d) = explode("-", date('Y-m-d'));
if(($m > $bm) || ($m == $bm && $d > $bd))
$y++;
return $y-$by;
}
/**
*getConstellation
*
*@param String $brithday yyyy-mm-dd
*
*@param Array $format
*
*@return String
*/
public static function get_constellation($birthday, $format=null)
{
$pattern = '/^\d{4}-\d{1,2}-\d{1,2}$/';
if (!preg_match($pattern, $birthday, $matchs))
{
return null;
}
$date = explode('-', $birthday);
$year = $date[0];
$month = $date[1];
$day = $date[2];
if ($month <1 || $month>12 || $day < 1 || $day >31)
{
return null;
}
//
$constellations = array(
' ', ' ', ' ', ' ', ' ', ' ',
' ',' ', ' ', ' ', ' ', ' ',);
//
/*$constellations = array(
'Capricorn', 'Aquarius', 'Pisces', 'Aries', 'Taurus', 'Gemini',
'Cancer','Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius',);*/
// ,
$enddays = array(19, 18, 20, 20, 20, 21, 22, 22, 22, 22, 21, 21,);
// format , format ,
if ($format != null)
{
$values = $format;
}
else
{
$values = $constellations;
}
//
switch ($month)
{
case 1:
if ($day <= $enddays[0])
{
$constellation = $values[0];
}
else
{
$constellation = $values[1];
}
break;
case 2:
if ($day <= $enddays[1])
{
$constellation = $values[1];
}
else
{
$constellation = $values[2];
}
break;
case 3:
if ($day <= $enddays[2])
{
$constellation = $values[2];
}
else
{
$constellation = $values[3];
}
break;
case 4:
if ($day <= $enddays[3])
{
$constellation = $values[3];
}
else
{
$constellation = $values[4];
}
break;
case 5:
if ($day <= $enddays[4])
{
$constellation = $values[4];
}
else
{
$constellation = $values[5];
}
break;
case 6:
if ($day <= $enddays[5])
{
$constellation = $values[5];
}
else
{
$constellation = $values[6];
}
break;
case 7:
if ($day <= $enddays[6])
{
$constellation = $values[6];
}
else
{
$constellation = $values[7];
}
break;
case 8:
if ($day <= $enddays[7])
{
$constellation = $values[7];
}
else
{
$constellation = $values[8];
}
break;
case 9:
if ($day <= $enddays[8])
{
$constellation = $values[8];
}
else
{
$constellation = $values[9];
}
break;
case 10:
if ($day <= $enddays[9])
{
$constellation = $values[9];
}
else
{
$constellation = $values[10];
}
break;
case 11:
if ($day <= $enddays[10])
{
$constellation = $values[10];
}
else
{
$constellation = $values[11];
}
break;
case 12:
if ($day <= $enddays[11])
{
$constellation = $values[11];
}
else
{
$constellation = $values[0];
}
break;
}
return $constellation;
}
/**
* 0 18 ,1 18
* gt18, 18
*/
public static function gt18($user_id)
{
$user_info = pub_mod_user::get_one_user_details($user_id);
$reg_time = $user_info['reg_time']; // 2012.11.20
$time = mktime(0,0,0,11,20,2012);
$IDCard = $user_info['idcard'];
if($reg_time<$time)
{
return 0;exit; // 18
}elseif(empty($IDCard)){
return 1;exit; // 18
}else{
if(!preg_match("/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/",$IDCard) && !preg_match("/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|x|X)$/",$IDCard)){
$flag=1;
}else{
if(strlen($IDCard)==18){
$tyear=intval(substr($IDCard,6,4));
$tmonth=intval(substr($IDCard,10,2));
$tday=intval(substr($IDCard,12,2));
if($tyear>date("Y")||$tyear<(date("Y")-100)){
$flag=0;
}
elseif($tmonth<0||$tmonth>12){
$flag=0;
}
elseif($tday<0||$tday>31){
$flag=0;
}else{
$tdate=$tyear."-".$tmonth."-".$tday." 00:00:00";
if((time()-mktime(0,0,0,$tmonth,$tday,$tyear))>18*365*24*60*60){
$flag=0;
}else{
$flag=1;
}
}
}elseif(strlen($IDCard)==15){
$tyear=intval("19".substr($IDCard,6,2));
$tmonth=intval(substr($IDCard,8,2));
$tday=intval(substr($IDCard,10,2));
if($tyear>date("Y")||$tyear<(date("Y")-100)){
$flag=0;
}
elseif($tmonth<0||$tmonth>12){
$flag=0;
}
elseif($tday<0||$tday>31){
$flag=0;
}else{
$tdate=$tyear."-".$tmonth."-".$tday." 00:00:00";
if((time()-mktime(0,0,0,$tmonth,$tday,$tyear))>18*365*24*60*60){
$flag=0;
}else{
$flag=1;
}
}
}
}
return $flag;
}
}
/**
* ,$key==$value
*
* @param array $array
* @param string $item
* @param string $value
* @return array or false
*/
function array_search2($array, $item, $value, $return_key = false)
{
if (!is_array($array) || empty($item) || empty($value))
{
return false;
}
foreach ($array as $k => $v)
{
if (isset($v[$item]) && $v[$item] === $value)
{
if ($return_key)
{
return $k;
}
return $v;
}
}
return false;
}
/**
*
* ip
*/
function ipallow()
{
$ip = util::get_client_ip();
$tip = $GLOBALS['config']['ipallow'];
if(in_array($ip,$tip))
{
return true;
}else{
echo "<script>alert(' ')</script>";
exit();
}
}
}
作者:北京小buger,出てきて売って、北京の生活の质は低すぎて、1ヶ月に小さいお金を稼いで、本当に情调がなくて、解决策を求めます