<?php
/**
*本程序由侯爷的博客出品
*仅供大家交流和学习之用
*侯爷的博客　www.palmcivet.ren
*请勿用于商业发布
*/

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

//验证客户端信息输入
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;
}


//常量设置
$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",rinput($data->lm_webname),true);
define("websitetitle",rinput($data->lm_webtitle),true);
define("websitekey",rinput($data->lm_webkey),true);
define("websitedes",rinput($data->lm_webdes),true);
define("websiteby",rinput($data->lm_powerby),true);
define("websitebeian",rinput($data->lm_beian),true);
define("websiteid",rinput($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",rinput($sysdata->lm_indexurl),true);
define("webupimgurl",rinput($sysdata->lm_upimgurl),true);
define("webbackbaseurl",rinput($sysdata->lm_backbaseurl),true);
define("webtemplateurl",rinput($sysdata->lm_templateurl),true);
define("webklyz",rinput($sysdata->lm_klyz),true);
define("webklma1",rinput($sysdata->lm_klma1),true);
define("webklma2",rinput($sysdata->lm_klma2),true);
define("websysid",rinput($sysdata->lm_id),true);
}
define("webphpurl",rinput($sysdata->lm_indexurl)."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;
}

//获得当前URL不带传递参数
function geturl() {
    $pageURL = 'http';
    if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
    }
    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 php_self(){
    $php_self=substr($_SERVER['PHP_SELF'],strrpos($_SERVER['PHP_SELF'],'/')+1);
    return $php_self;
  }
  
  


//调用主菜单
function callmenu(){
	$myindexurl = webphpurl;
	$msql = "select * from limao_nm where lm_nmid=0 and lm_action=1";
	$mrs = mysql_query($msql) or die('传入的参数错误');
	$str = '';
	while($mdata=mysql_fetch_object($mrs)){
switch($mdata->lm_error){		
	case 1:
 $tmp = "<li><a href='".rinput($myindexurl)."index_arc.php?lm_nmid=".rinput($mdata->lm_id)."'>".rinput($mdata->lm_nmname)."</a></li>";	
  break;
  case 2:
 $tmp =  "<li><a href='".rinput($myindexurl)."index_shop.php?lm_nmid=".rinput($mdata->lm_id)."'>".rinput($mdata->lm_nmname)."</a></li>";	
  break;
 }
 $str .= $tmp; 
 }
 
return $str;
		}
	
	
//调用下级菜单
function callrjmenu(){
	$lmid = isset($_GET['lm_nmid']) ? cinput($_GET['lm_nmid']) : 0;
	if(!is_numeric($lmid)){
return "<script>alert('传入的参数错误,系统正返回首页');location.href='index.php';</script>";
		}
	if(isset($lmid) && !empty($lmid)){
	$myindexurl = webphpurl;
	$str = '';
	$msql = "select * from limao_nm where lm_nmid=$lmid and lm_action=1";
	$mrs = mysql_query($msql);
	if($mrs){
	while($mdata=mysql_fetch_object($mrs)){
switch($mdata->lm_error){
	case 1:
	$tmp = "&nbsp;<a href='".rinput($myindexurl)."list_article.php?lm_nmid=".$lmid."&lm_rjnmid=".rinput($mdata->lm_id)."'>".rinput($mdata->lm_nmname)."</a>&nbsp;";	
break;
  case 2:
 	$tmp =  "&nbsp;<a href='".rinput($myindexurl)."list_shop.php?lm_nmid=".$lmid."&lm_rjnmid=".rinput($mdata->lm_id)."'>".rinput($mdata->lm_nmname)."</a>&nbsp;";	
break;
}
 $str .= $tmp;
		}
return $str;	
	}else{
return "<script>alert('传入的参数错误,系统正返回首页');location.href='index.php';</script>";
		}
	}
	}



//点击量
function addclick(){
	$id = isset($_GET['id']) ? cinput($_GET['id']) : 0;
	$msql = "update limao_article set lm_action=lm_action+1 where lm_id=".$id;
	$mrs = mysql_query($msql) or die('传入的参数错误');	
 
}


//调用下级菜单
function getnmseo(){
	$nmid = isset($_GET['lm_nmid']) ? cinput($_GET['lm_nmid']) :'';
	$rjnmid = isset($_GET['lm_rjnmid']) ? cinput($_GET['lm_rjnmid']) :'';	
  $tmp1='';
  $tmp2='';
  $tmpall='';
  	if(isset($rjnmid) && !empty($rjnmid)){
	if(!is_numeric($rjnmid)){
  return "<script>alert('传入的参数错误,系统正返回首页');location.href='index.php';</script>";
  exit();
		}			
	$msql = "select * from limao_nm where lm_id=$rjnmid limit 1";
	$mrs = mysql_query($msql);
	if($mrs){
	$mdata=mysql_fetch_object($mrs);
  $tmp1 = rinput($mdata->lm_nmseo);
		}
  $tmpall .= $tmp1."-";
	}
	if(isset($nmid) && !empty($nmid)){
	if(!is_numeric($nmid)){
  return "<script>alert('传入的参数错误,系统正返回首页');location.href='index.php';</script>";
  exit();
		}		
	$nmsql = "select * from limao_nm where lm_id=$nmid limit 1";
	$nmrs = mysql_query($nmsql);
	if($nmrs){
	$nmdata=mysql_fetch_object($nmrs);
  $tmp2 = rinput($nmdata->lm_nmseo);				
  
		}
	$tmpall .= $tmp2;
	}
return  $tmpall;
}