<?php
/**
 * This is the common functions file created by PHPGEN system.
 *
 * PHPGEN is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHPGEN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHPGEN; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @copyright   EasySoft R&D Team (C) 2006
 * @author      Chunsheng Wang <wwccss@263.net>
 * @link        http://www.phpgen.com
 * @package     Include 
 */

//------------------------- SYSTEM FUNCTIONS -----------------------------------//
/**
 * Enhance the function addslashes())
 *
 * @author                  wwccss<wwccss@263.net>
 * @param  mix     $Data    the variable to addslashes.
 * @return mix              formated variable.
 */
function sysAddSlash($Data)
{
    if(!get_magic_quotes_gpc())
    {
        if(is_array($Data))
        {
            foreach($Data as $Key => $Value)
            {
                if(is_array($Value))
                {
                    $Data[$Key] = sysAddSlash($Value);
                }
                else
                {
                    $Data[$Key] = addslashes($Value);
                }
            }
        }
        else
        {
            $Data = addslashes($Data);
        }
    }
    return $Data;
}

/**
 * Data checking.
 *
 * This function check the $Var with $DataType's rule, which is defined in $_CFG["DataType"] in Language _COMMON.php
 *
 * @author                     wwccss<wwccss@263.net>
 * @global array               the check rule config.
 * @param  mix     $Var        the variable to check.
 * @param  string  $VarName    the variable name to echo when any error.
 * @param  string  $DataType   data type.
 * @param  mix     $ExtraParm  others paramas.
 * @return string              check result.
 */
function sysCheck($Var, $VarName, $DataType, $ExtraParam)
{
    global $_CFG;
    if($DataType == "Length")
    {
        $MaxLength = (int)$ExtraParam;
        if(strlen($Var)> $MaxLength)
        {
            return $VarName . " exceed the max length of $MaxLength!";
        }
    }
    elseif($DataType == "Int")
    {
        if(!is_numeric($Var))
        {
            return $VarName . " error, should be a number!";
        }
    }
    elseif(!eregi($_CFG["DataType"][$DataType]["Rule"], $Var))
    {
        return $VarName . " error, should be a " . $_CFG["DataType"][$DataType]["Desc"] . "!";
    }
}

//------------------------- DATABASE FUNCTIONS -----------------------------------//

/**
 * Create string like "IN('a','b')"  from "a,b"
 *
 * @author                     wwccss<wwccss@263.net>
 * @param  string $ItemList    item list like "a,b,c".
 * @return string $ItemList    string like "IN('a','b','c')".
 */
function dbCreateIN($ItemList)
{
    if(empty($ItemList))
    {
        return false;
    }
    else
    {
        $ItemList = explode(",",$ItemList);
        foreach($ItemList AS $Item)
        {
            $ItemListTMP[] = "'$Item'";
        }
        return " IN (".join(",",$ItemListTMP).") ";
    }
}

/**
 * Get the record number of a query.
 *
 * @author                     wwccss<wwccss@263.net>
 * @global object              Object of ADO class.
 * @param  string $TableName
 * @param  string $Where
 * @return int                 RecNumber
 */
function dbGetRecNumber($TableName,$Where = "")
{
    global $MyDB;
    $SQL      = "SELECT COUNT(*) AS RecNumber FROM $TableName $Where";
    $ResultID = $MyDB->query($SQL);
    if($ResultID)
    {
        $DataSet = $ResultID->fetchRow();
        $ResultID->close();
        return (int)$DataSet["RecNumber"];
    }
    else
    {
        die($MyDB->errorMsg());
    }
}

/**
 * Build ORDER BY sql according to $_GET["OrderBy"] and $_GET["OrderMode"] vars.
 *
 * @author            wwccss<wwccss@263.net>
 * @return string       
 */
function dbGetOrderSQL()
{
    if(!empty($_GET["OrderBy"]))
    {
        $OrderSQL = " ORDER BY $_GET[OrderBy] $_GET[OrderMode] ";
    }
    return $OrderSQL;
}

//------------------------- LANGUAGE FUNCTIONS -----------------------------------//

/**
 * 包含某一个表所对应的语言文件。
 *
 * @author                     wwccss<wwccss@263.net>
 * @global array  $_CFG        系统配置数组。
 * @global array  $_LANG       语言数组。
 * @global array  $MyTPL       模板对象变量。
 * @param  string $TableName   表名。
 * @return booble              是否包含成功。
 */
function langLoadTable($TableName)
{
    global $_CFG;
    global $_LANG;
    global $MyTPL;
    $TableLangFile = $_CFG["SysRoot"] . "Languages/" . $_CFG["UserLang"] . "/Tables/" . $TableName . ".php";
    if(!@file_exists($TableLangFile))
    {
        return false;
    }
    else
    {
        @require_once($TableLangFile);
        $MyTPL->assign("Lang", $_LANG);
        return true;
    }
}
?>