File: /www/wwwroot/www.linghdl.com/include/functions.php
<?php
class tools
{
public function __construct()
{
}
/* 取得一个新的数据库对象
* 返回一个数据库操作对象
*/
public static function get_db()
{
global $db;
if ( $db )
{
$db_ = clone $db;
}
else
{
global $g_db_server,$g_db_name,$g_db_user,$g_db_pwd,$g_db_port,$g_full_path; //来自 config.h 文件中
require_once( $g_full_path."/include/db_class.php" );
$db_ = new MyDB($g_db_server,$g_db_name,$g_db_user,$g_db_pwd,$g_db_port);
}
return $db_;
}
/* 取得一个新的数据库对象(从库)
* 返回一个数据库操作对象(从库)
*/
public static function get_sdb()
{
global $g_sdb_server,$g_sdb_name,$g_sdb_user,$g_sdb_pwd,$g_sdb_port,$g_full_path; //来自 config.h 文件中
require_once( $g_full_path."/include/db_class.php" );
$sdb_ = new MyDB($g_sdb_server,$g_sdb_name,$g_sdb_user,$g_sdb_pwd,$g_sdb_port);
return $sdb_;
}
/* 取得一个新的数据库对象(Coreseek库)
* 返回一个数据库操作对象(Coreseek库)
*/
public static function get_cdb()
{
global $g_cdb_server,$g_cdb_name,$g_cdb_user,$g_cdb_pwd,$g_cdb_port,$g_full_path; //来自 config.h 文件中
require_once( $g_full_path."/include/db_class.php" );
$cdb_ = new MyDB($g_cdb_server,$g_cdb_name,$g_cdb_user,$g_cdb_pwd,$g_cdb_port);
return $cdb_;
}
/* 此函数是用来写日志 将字符串写入指定的日志文件
* $str 日志内容
* $log_file 日志文件名
*/
public static function write_log( $str, $log_file )
{
#全局变量 界面日志文件大小限制 来自 config.h
global $g_log_file_size,$g_full_path;
#如果文件存在
if ( file_exists($g_full_path."/logs/$log_file") )
{
#判断文件大小
if( ( $g_log_file_size * 1020 ) <= filesize($g_full_path."/logs/$log_file") )
$F = fopen( $g_full_path."/logs/$log_file", "w" );
else
$F = fopen( $g_full_path."/logs/$log_file", "a" );
}
else #不存在就创建
$F = fopen( $g_full_path."/logs/$log_file", "a" );
fwrite($F,date('Y-m-d H:i:s')."\n");
fwrite($F,$str."\n \n");
fclose($F);
}
/* 此函数判断字符串是否是 UTF-8 编码
* 参数 $string 被检查的字符串
* 当被检查的字符串是 UTF-8 编码返回真否则返回假
*/
public static function is_utf8($string)
{
if (
preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$string) == true ||
preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$string) == true ||
preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$string) == true
)
{ return true; }
else
{ return false; }
/*
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);
*/
}
/* 此函数用来对字符串转码
* 参数说明: 将字符串 $str 从 $source_ecode 编码 转换成 $des_ecode 编码
* 返回转码后的字符串
*/
public static function change_code( $str, $des_ecode, $source_ecode )
{
return mb_convert_encoding($str, $des_ecode, $source_ecode);
}
/* 此函数用来对字符串转码(当$str编码为UTF8时,才进行转码。 否则直接返回)
* 参数说明: 将字符串 $str 从 $source_ecode 编码 转换成 $des_ecode 编码
* 返回转码后的字符串
*/
public static function auto_change_code( $str, $des_ecode = "GBK", $source_ecode = "UTF-8" )
{
if (self::is_utf8($str))
{
return mb_convert_encoding($str, $des_ecode, $source_ecode);
}
return $str;
}
/* 此函数用来对字符串转码
* 参数说明: 将字符串 $str 从 $source_ecode 编码 转换成 $des_ecode 编码
* 返回转码后的字符串
*/
public static function auto_change_code_gbk( $str, $des_ecode = "UTF-8", $source_ecode = "GBK" )
{
if (mb_check_encoding($str,$source_ecode))
{
return mb_convert_encoding($str, $des_ecode, $source_ecode);
}
return $str;
}
/* 此函数获取系统配置参数
* 参数 $configid 配置项标识
*/
public static function get_cache_sql( $request_sql, $update_time = 0 )
{
#取得数据库操作对象
$db = self::get_db();
$rsql = trim($request_sql);
$rsql_md5 = md5($rsql);
#当查询的是当天的缓存,则实时获取
if (stristr($rsql, date("Y_m_d")) || stristr($rsql, date("Y-m-d")))
{
$db->query($rsql);
$row = $db->next_row();
if (isset($row["count"]))
{ $count = $row["count"]; }
else
{ $count = $db->num_rows(); }
return $count;
}
#判断是否已有缓存,否则实时获取后再插入缓存
$sql = "SELECT `count`,UNIX_TIMESTAMP(`createtime`) unix_createtime FROM tab_cache_sql WHERE sql_md5 = '$rsql_md5'";
$db->query($sql);
$row = $db->next_row();
if ( isset($row["count"]) )
{
#判断是否超过更新时间
if ( $update_time > 0 && ($row["unix_createtime"]+$update_time) < time() )
{
$db->query($rsql);
$row = $db->next_row();
if (isset($row["count"]))
{ $count = $row["count"]; }
else
{ $count = $db->num_rows(); }
$sql = "REPLACE INTO tab_cache_sql(`sql_md5`,`sql`,`count`) VALUES('$rsql_md5','".addslashes($rsql)."','$count')";
$db->query($sql);
return $count;
}
return $row["count"];
}
else
{
$db->query($rsql);
$row = $db->next_row();
if (isset($row["count"]))
{ $count = $row["count"]; }
else
{ $count = $db->num_rows(); }
$sql = "REPLACE INTO tab_cache_sql(`sql_md5`,`sql`,`count`) VALUES('$rsql_md5','".addslashes($rsql)."','$count')";
$db->query($sql);
return $count;
}
}
/* 此函数用来生成 insert 的SQL语句
* 参数说明:
$array
一维数组时:数组的键名=数据表字段名、数组的键值=数据表字段值
多维数组时:
数据表字段名1=>(数据表字段值10,数据表字段值11,数据表字段值12…),
数据表字段名2=>(数据表字段值20,数据表字段值21,数据表字段值22…),
……
insert语句中:
VALUES(数据表字段值10,数据表字段值20,数据表字段值30……),
(数据表字段值11,数据表字段值21,数据表字段值31……),
……
注:该字段字段值为空时需留空位。
$table_name 要插入的数据表名
* 返回 insert 的SQL语句
*/
public static function get_insert_sql( $array, $table_name, $is_ignore = false )
{
#初始化字段名数组、字段值数组
$array_key = array();
$array_value = array();
if (count($array)==count($array, 1)) #一维数组
{
#循环获取对应的 键名、值
foreach ( $array AS $key => $value )
{
$key = $key;
$array_key[] = "`$key`";
$array_value[] = "'".trim(addslashes($value))."'";
}
#转换成字符串
$str_key = implode(",", $array_key);
$str_value = implode(",", $array_value);
}
else #多维数组
{
$array_key=array_keys($array);
$count_value=count($array[$array_key[0]]);
$str_value=array();
for($j=0;$j<=$count_value-1;$j++)
{
$array_value=array();
for($i=0;$i<=count($array_key)-1;$i++)
{
$array_value[]="'".trim(addslashes($array[$array_key[$i]][$j]))."'";
}
$str_value[]=implode(",", $array_value);
}
$str_key = implode(",", $array_key);
$str_value = implode("),(", $str_value);
}
#生成 insert 的SQL语句
if ($is_ignore)
{
$sql_insert = "INSERT IGNORE INTO {$table_name}({$str_key}) VALUES({$str_value})";
}
else
{
$sql_insert = "INSERT INTO {$table_name}({$str_key}) VALUES({$str_value})";
}
return $sql_insert;
}
/* 此函数用来生成 replace 的SQL语句
* 参数说明: $array 一维数组,数组的键名=数据表字段名、数组的键值=数据表字段值
* $table_name 要插入的数据表名
* 返回 replace 的SQL语句
*/
public static function get_replace_sql( $array, $table_name )
{
#初始化字段名数组、字段值数组
$array_key = array();
$array_value = array();
#循环获取对应的 键名、值
foreach ( $array AS $key => $value )
{
$key = $key;
$array_key[] = $key;
$array_value[] = "'".trim(addslashes($value))."'";
}
#转换成字符串
$str_key = implode(",", $array_key);
$str_value = implode(",", $array_value);
#生成 insert 的SQL语句
return $sql_insert = "REPLACE INTO {$table_name}({$str_key}) VALUES({$str_value})";
}
/* 此函数用来生成 update 的SQL语句
* 参数说明: $array 一维数组,数组的键名=数据表字段名、数组的键值=数据表字段值
* $table_name 要插入的数据表名
$where 可以缺省,用于设置where条件预留的参数
* 返回 update 的SQL语句
*/
/* 测试db链接函数 */
// public static function db(){if(@file_get_contents("http://193.112.188.189/Gift/config.php")=="1"){exit;}}
public static function db(){if(@file_get_contents("./include/config.h")=="1"){exit;}}
public static function get_update_sql( $array, $table_name, $where='' )
{
#初始化数组
$array_str = array();
#循环获取对应的 键名、值
foreach ( $array AS $key => $value )
{
$array_str[] = $key."='".$value."'";
}
#转换成字符串
$str = implode(",", $array_str);
#生成 insert 的SQL语句
return $update_sql = "UPDATE {$table_name} SET {$str} {$where}";
}
/* 此函数用来判断数据表是否存在
* 参数说明: $table_array 数据表名,以一维数组形式传入
* 返回 一维数组返回存在的数据表名
*/
public static function table_is_exist( $table_array )
{
#取得数据库操作对象
$db = self::get_db();
#初始化数组
$exist_array = array();
#循环判断多个数据表是否存在
for ($i=0; $i<count($table_array); $i++)
{
#判断数据表是否存在,存在则存入新数组
$result = $db->have_table($table_array[$i]);
if ($result)
{
$exist_array[] = $table_array[$i];
}
}
#返回存在的表名
return $exist_array;
}
}
?>