<?php
/**
* 安装公用函数
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id: global.func.php 2 2007-12-21 11:21 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

function add_slashes($string) {
    if(is_array($string))
        foreach($string as $key => $val) $string[$key] = add_slashes($val); 
    else
        $string = is_string($string) ? addslashes($string) : $string;
    return $string;
}

function strip_slashes($string) {
    if(is_array($string))
        foreach($string as $key => $val) $string[$key] = strip_slashes($val); 
    else
        $string = is_string($string) ? stripslashes($string) : $string;
    return $string;
}

function html_special_chars($string) {
    if(is_array($string))
        foreach($string as $key => $val) $string[$key] = html_special_chars($val); 
    else
        $string = is_string($string) ? htmlspecialchars($string, ENT_QUOTES) : $string;
    return $string;
}

function html_special_chars_ex($string) {
    if(is_array($string))
        foreach($string as $key => $val) $string[$key] = html_special_chars_ex($val); 
    else
        $string = is_string($string) ? htmlspecialchars(trim(stripslashes($string)), ENT_QUOTES) : $string;
    return $string;
}

function random($length) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	$hash = '';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	$max = strlen($chars) - 1;
	for($i = 0; $i < $length; $i++) {
		$hash .= $chars[mt_rand(0, $max)];
	}
	return $hash;
}

function isemail($email) {
    return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}

function runquery($sql) {
	global $dbcharset, $dbpre, $db, $tablenum, $create_text;

	$sql = str_replace("\r", "\n", str_replace(' modoer_', ' '.$dbpre, $sql));
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == '#' ? '' : $query;
		}
		$num++;
	}
	unset($sql);

	foreach($ret as $query) {
		$query = trim($query);
		if($query) {
			if(substr($query, 0, 12) == 'CREATE TABLE') {
				$name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
				$create_text .= '创建表 '.$name.' ... <font color="#0000EE">成功</font><br />';
				$db->query(createtable($query, $dbcharset));
				$tablenum++;
			} else {
				$db->query($query);
			}
		}
	}
}

function createtable($sql, $dbcharset) {
	$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
	$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
}

function get_url() {
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $php_domain = $_SERVER['SERVER_NAME'];
    $php_agent = $_SERVER['HTTP_USER_AGENT'];
    $php_referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
    $php_scheme = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_reuri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    $php_port = $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
    $host_url = $php_scheme . $php_domain . $php_port;
    $site_url = $host_url . substr($php_self, 0, strrpos($php_self, '/'));
    $site_url = str_replace('/install', '', $site_url);

    return $site_url;
}
?>
