<?php
//验证客户端信息输入
function cinput($value)
{
if(is_numeric($value)){
	if(strlen($value)){
		if(strlen($value)>5) {
			$value = intval(substr($value,0,6));
		}else{
			$value = intval(substr($value,0,6));
			}
		}
	}else{
// 如果不是数字则加引号,取值时把两端引号去掉
if(substr($value,0,3)=="'''"){
	$value = $value;
		}else{
			if(substr($value,strlen($value)-3,3)=="'''"){
				$value = $value;
			}else{
				 $value = "'". $value ."'";	
				}
				
}
//去除敏感字段
$badstring="select|insert|update|delete|#|%|&|union|into|load_file|limit|outfile";
$value = preg_replace("/$badstring/i",'',$value);
}
return $value;
}


//前端取值时去除引号
function rinput($value)
{
$bstring="'|#";
$value = preg_replace("/$bstring/i",'',$value);
$value = stripslashes($value);
return $value;
}

//常量设置
$defsql="select * from limao_sysset limit 1";
$defrs=mysql_query($defsql,$conn);
if(!$defrs){
 	die('参数错误:'.mysql_error());
 	}else{
$data=mysql_fetch_object($defrs);
define("websitename",$data->lm_webname,true);
define("websitetitle",$data->lm_webtitle,true);
define("websitekey",$data->lm_webkey,true);
define("websitedes",$data->lm_webdes,true);
define("websiteby",$data->lm_powerby,true);
define("websitebeian",$data->lm_beian,true);
define("websiteid",$data->lm_id,true);
}

$syssql="select * from limao_sysbase limit 1";
$sysrs=mysql_query($syssql,$conn);
if(!$sysrs){
 	die('参数错误:'.mysql_error());
 	}else{
$sysdata=mysql_fetch_object($sysrs);
define("webindexurl",$sysdata->lm_indexurl,true);
define("webupimgurl",$sysdata->lm_upimgurl,true);
define("webbackbaseurl",$sysdata->lm_backbaseurl,true);
define("webtemplateurl",$sysdata->lm_templateurl,true);
define("webklyz",$sysdata->lm_klyz,true);
define("webklma1",$sysdata->lm_klma1,true);
define("webklma2",$sysdata->lm_klma2,true);
define("websysid",$sysdata->lm_id,true);
}

define("webphpurl",rinput(webindexurl)."plus/",true);



//信息返回函数
function viewmsgold($test,$url){
	echo "<script type='text/javascript'>alert('$test');location.href='$url';</script>";
}
//友好提示界面
function viewmsg($test,$url){
	echo "<div style='margin:100px auto;width:40%;height:auto;border:solid 1px #666;font-size:16px;'><div style='margin:0 auto;background:#666;width:100%;height:50px;line-height:50px;color:#fff;'><center><h2>LIMAOCMS信息提示</h2></center></div>";
	echo "<div style='background:#fff;width:100%;line-height:30px;display:block;'><center><font color=red><b>".$test."</b></font>, &nbsp;&nbsp; 系统3秒后将自动转跳。</center></div>";
	echo "<div style='background:#fff;width:100%;line-height:30px;display:block;'><center><a href='".$url."'>如不能转跳请点这里</a></center></div>";
	echo "<script type='text/javascript'>";
  echo "function redirect(){";
  echo "window.location.href='".$url."';";
  echo "}";
  echo "setTimeout('redirect()',3000);";
  echo "</SCRIPT></div>";
}


//数据查询函数
function limaors($sqlstr){
 $result=mysql_query($sqlstr);
 if(!$result){
 	die('参数错误:'.mysql_error());
 	}else{
 	return $result;
}
}

//生成随机字符
function GetRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}
//获得文件名
function getExtension($filename){
  $myext = substr($filename, strrpos($filename, '.'));
  return str_replace('.','',$myext);
}
//获得文件大小并格式化
function formatSize($size) {
    $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    if ($size == 0) { 
        return('n/a'); 
    } else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); 
    }
}
/**
*$thefile = filesize('test_file.mp3');
*echo formatSize($thefile);
*/

//获取目录下文件名
function listDirFiles($DirPath){
    if($dir = opendir($DirPath)){
         while(($file = readdir($dir))!== false){
                if(!is_dir($DirPath.$file))
                {
                    echo "filename: $file<br />";
                }
         }
    }
}
/*listDirFiles('home/some_folder/');*/

//获得当前URL
function getPageURL() {
    $pageURL = 'http';
    if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}
//强制下制文件
function download($filename){
    if ((isset($filename))&&(file_exists($filename))){
       header("Content-length: ".filesize($filename));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $filename . '"');
       readfile("$filename");
    } else {
       echo "Looks like file does not exist!";
    }
}
//获取用户真实IP
function getip() {
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else
            if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                $ip = getenv("REMOTE_ADDR");
            else
                if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                    $ip = $_SERVER['REMOTE_ADDR'];
                else
                    $ip = "unknown";
    return ($ip);
}
//统计函数返回查询总记录数
function lmtotal($psql){
	$total = 0;  //总记录数
	$prs = mysql_query($psql);
  if(!$prs){
 	die('参数错误:'.mysql_error());
 	}else{
 	$total = mysql_num_rows($prs);
	if($total){  
   $total = $total;
    }
     else{
    $total = 0;
    }	
	}	
 	
return $total;
	}

