debug($message);
if(!isset($Vtiger_Utils_Log) || $Vtiger_Utils_Log == false) return;
print_r($message);
if($delimit) {
if(isset($_REQUEST)) echo "
";
else echo "\n";
}
}
/**
* Escape the string to avoid SQL Injection attacks.
* @param String Sql statement string
*/
static function SQLEscape($value) {
if($value == null) return $value;
global $adb;
return $adb->sql_escape_string($value);
}
/**
* Check if table is present in database
* @param String tablename to check
*/
static function CheckTable($tablename) {
global $adb;
$old_dieOnError = $adb->dieOnError;
$adb->dieOnError = false;
$tablename = Vtiger_Utils::SQLEscape($tablename);
$tablecheck = $adb->pquery("SHOW TABLES LIKE ?", array($tablename));
$tablePresent = true;
if(empty($tablecheck) || $adb->num_rows($tablecheck) === 0)
$tablePresent = false;
$adb->dieOnError = $old_dieOnError;
return $tablePresent;
}
/**
* Create table (supressing failure)
* @param String tablename to create
* @param String table creation criteria like '(columnname columntype, ....)'
* @param String Optional suffix to add during table creation
*
* will be appended to CREATE TABLE $tablename SQL
*/
static function CreateTable($tablename, $criteria, $suffixTableMeta=false) {
global $adb;
$org_dieOnError = $adb->dieOnError;
$adb->dieOnError = false;
$sql = "CREATE TABLE " . $tablename . $criteria;
if($suffixTableMeta !== false) {
if($suffixTableMeta === true) {
if($adb->isMySQL()) {
$suffixTableMeta = ' ENGINE=InnoDB DEFAULT CHARSET=utf8';
} else {
// TODO Handle other database types.
}
}
$sql .= $suffixTableMeta;
}
$adb->pquery($sql, array());
$adb->dieOnError = $org_dieOnError;
}
/**
* Alter existing table
* @param String tablename to alter
* @param String alter criteria like ' ADD columnname columntype'
* will be appended to ALTER TABLE $tablename SQL
*/
static function AlterTable($tablename, $criteria) {
global $adb;
$adb->query("ALTER TABLE " . $tablename . $criteria);
}
/**
* Add column to existing table
* @param String tablename to alter
* @param String columnname to add
* @param String columntype (criteria like 'VARCHAR(100)')
*/
static function AddColumn($tablename, $columnname, $criteria) {
global $adb;
if(!in_array($columnname, $adb->getColumnNames($tablename))) {
self::AlterTable($tablename, " ADD COLUMN $columnname $criteria");
}
}
/**
* Get SQL query
* @param String SQL query statement
*/
static function ExecuteQuery($sqlquery, $supressdie=false) {
global $adb;
$old_dieOnError = $adb->dieOnError;
if($supressdie) $adb->dieOnError = false;
$adb->pquery($sqlquery, array());
$adb->dieOnError = $old_dieOnError;
}
/**
* Get CREATE SQL for given table
* @param String tablename for which CREATE SQL is requried
*/
static function CreateTableSql($tablename) {
global $adb;
$create_table = $adb->pquery("SHOW CREATE TABLE $tablename", array());
$sql = decode_html($adb->query_result($create_table, 0, 1));
return $sql;
}
/**
* Check if the given SQL is a CREATE statement
* @param String SQL String
*/
static function IsCreateSql($sql) {
if(preg_match('/(CREATE TABLE)/', strtoupper($sql))) {
return true;
}
return false;
}
/**
* Check if the given SQL is destructive (DELETE's DATA)
* @param String SQL String
*/
static function IsDestructiveSql($sql) {
if(preg_match('/(DROP TABLE)|(DROP COLUMN)|(DELETE FROM)/',
strtoupper($sql))) {
return true;
}
return false;
}
/**
* funtion to log the exception messge to module.log file
* @global type $site_URL
* @param $module name of the log file and It should be a alphanumeric string
* @param / $exception Massage show in the log ,It should be a string or Exception object
* @param $extra extra massages need to be displayed
* @param $backtrace flag to enable or disable backtrace in log
* @param $request flag to enable or disable request in log
*/
static function ModuleLog($module, $mixed, $extra = array()) {
if (ALLOW_MODULE_LOGGING) {
global $site_URL;
$date = date('Y-m-d H:i:s');
$log = array($site_URL,$module, $date);
if ($mixed instanceof Exception) {
array_push($log, $mixed->getMessage());
array_push($log, $mixed->getTraceAsString());
} else {
array_push($log, $mixed);
array_push($log, "");
}
if (isset($_REQUEST)) {
array_push($log, json_encode($_REQUEST));
} else {
array_push($log, "");
};
if ($extra) {
if (is_array($extra))
$extra = json_encode($extra);
array_push($log, $extra);
} else {
array_push($log, "");
}
$fileName =self::$logFileName;
$fp = fopen("logs/$fileName", 'a+');
fputcsv($fp, $log);
fclose($fp);
}
}
}
?>