/*
Description: Global functions
Autor: Oleg Koshkin
Data: 12-07-2010
Version: 1.41
mailto: WizardF@narod.ru
copyright: (C) 2005 Oleg Koshkin
hystory:
1.1 mail notification added
1.2 added functions: GetList(), array2hash()
1.3 added functions: DeleteItem()
1.31 added file name in error notification
1.32 added function: DeleteItemEx()
1.33 added function: ConcatGetParams()
1.34 refactored function: LogEvent
1.35 added function: UploadFile()
1.36 added function: LogItem()
1.37 all SQL methods moved to separated class
1.4 refactored to use new named convension
1.41 writeItem() method added
*/
class Logger
{
static function logEvent($eventFormat, $_argList)
{
if(!is_array($_argList))
$argList = array($_argList);
else
$argList = $_argList;
$fileName = LOG_FILE_PATH;
$log = "";
if(is_readable($fileName))
$log = join("", file($fileName));
$time = date("Y/m/d (d F, D) H:i:s O");
$text = sprintf($eventFormat, $time, mysql_error(), preg_replace("/\t+/i", " ", str_replace("\n", "", $argList[0])), $_SERVER['PHP_SELF']. " page '". $_GET['global']['path'] . "' ");
if($fout = fopen($fileName, "a+"))
{
fwrite($fout, $text);
fclose($fout);
unset($fout);
}
if (1 == IS_SEND_ERRORS)
mail(SYS_ADMIN_MAIL, sprintf('Error occured at %s on %s ', $time, TITLE), $text);
}
static function logItem($fileName, $item)
{
if($fout = fopen($fileName, "a+"))
{
fwrite($fout, $item . "\r\n");
fclose($fout);
unset($fout);
}
}
static function writeItem($fileName, $item)
{
if($fout = fopen($fileName, "w"))
{
fwrite($fout, $item . "\r\n");
fclose($fout);
unset($fout);
}
}
static function logStackTrace($sErr)
{
$time = date("Y/m/d (d F, D) H:i:s O");
$text = sprintf(LOG_FRM_GENERAL, $time,
str_replace(array(' ', '
','',''), array(' ', "\r\n",'_','_'), $sErr),
$_SERVER['PHP_SELF'],
$_GET['global']['path']).
"Query string: ".$_SERVER['QUERY_STRING'];
Logger::logItem(LOG_FILE_PATH, $text);
if (1 == IS_SEND_ERRORS && Logger::filterSysEmail($text))
mail(SYS_ADMIN_MAIL, sprintf('Error occured at %s on %s (%s) ', $time, TITLE, $_SERVER['SERVER_NAME']), $text);
}
static function filterSysEmail($msg) {
if (strpos($msg, 'Duplicate entry')) return false;
return true;
}
}
?>
/*
Description: Util functions
Autor: Oleg Koshkin
Data: 06-06-2007
Version: 1.2
mailto: WizardF@narod.ru
copyright: (C) 2005 Oleg Koshkin
1.1 ConcatGetParams modifications, ConcatFriendlyGetParams addad
1.2 Add Array2String
1.3 Add String2Array
*/
class Util
{
// Concat all GET parameters into string for using in links.
// $exclude_arr specifies what parameters should be ignored
static function ConcatGetParams($exclude_arr = array())
{
$out_str = '';
$keys = array_keys(@$_GET);
$keys = array_unique($keys);
foreach($keys as $one_key)
{
if (!in_array($one_key, $exclude_arr))
$out_str .= $one_key . '=' . $_GET[$one_key] . '&';
}
return substr($out_str, 0, strlen($out_str)-1);
}
static function ConcatFriendlyGetParams($exclude_arr = array())
{
$gets = Util::ConcatGetParams($exclude_arr);
$gets = str_replace('&', '/', $gets);
return $gets;
}
static function Array2String($arr)
{
$str = '';
foreach($arr as $item)
$str .= $item . ',';
if ( strlen($str) > 1)
$str = substr($str, 0, -1);
return $str;
}
static function String2Array($str)
{
return explode(',', $str);
}
static function UploadFile($inputFileName, $uploaddir, $file_name)
{
$inputFile = $_FILES[$inputFileName];
$is_ok = 'false'; $err = '';
if( isset($inputFile) && !Val::IsEmpty($inputFile['tmp_name']) )
{
if(!is_dir($uploaddir)) mkdir($uploaddir);
$uploadfile = $uploaddir . '/' . $file_name;
if ( strlen($file_name) )
{
if (@move_uploaded_file($inputFile['tmp_name'], $uploadfile))
{
$is_ok = 'true';
@chmod ("$uploadfile", 0644);
}
else
{
$err = ERR_UPLOAD_IMG;
switch ($inputFile['error'])
{
case UPLOAD_ERR_INI_SIZE: $err .= ERR_UPLOAD_ERR_INI_SIZE; break;
case UPLOAD_ERR_FORM_SIZE: $err .= ERR_UPLOAD_ERR_FORM_SIZE; break;
case UPLOAD_ERR_PARTIAL: $err .= ERR_UPLOAD_ERR_PARTIAL; break;
case UPLOAD_ERR_NO_FILE: $err .= ERR_UPLOAD_ERR_NO_FILE; break;
}
if (trim($err) != '') return $err; /*{ echo UI::GetBlockError($err); return false; }*/
}
}
}
return $err;
}
static function getFileSize($fileName, $autodetect = false, $range = 0, $round = null)
{
$round = (null === $round) ? 3 : $round;
if (!file_exists($fileName)) return '0 байт';
$size = filesize($fileName);
//$sizes = array('b', 'Kb', 'Mb', 'Gb');
if ($autodetect)
{
if($size < pow(1024, 1)) return $size." байт";
elseif($size < pow(1024, 2)) return round($size/pow(1024, 1), $round)." КБ";
elseif($size < pow(1024, 3)) return round($size/pow(1024, 2), $round)." МБ";
elseif($size < pow(1024, 4)) return round($size/pow(1024, 3), $round)." ГБ";
}
else
{
if($range == 'b') return $size." байт";
elseif($range == 'Kb') return round($size/pow(1024, 1), $round)." КБ";
elseif($range == 'Mb') return round($size/pow(1024, 2), $round)." МБ";
elseif($range == 'Gb') return round($size/pow(1024, 3), $round)." ГБ";
}
}
static function getNonExistentFilename($path)
{
if (!file_exists($path)) return $path;
$ext = pathinfo($path, PATHINFO_EXTENSION);
$i = 1;
while(true == file_exists(str_replace('.'.$ext, '', $path).'_'.$i.'.'.$ext)) {
$i++;
}
return str_replace('.'.$ext, '', $path).'_'.$i.'.'.$ext;
}
static function translit($s, $params = array())
{
$map = array(
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'e',
'ж' => 'g',
'з' => 'z',
'и' => 'i',
'й' => 'i',
'к' => 'k',
'л' => 'l',
'м' => 'm',
'н' => 'n',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'y',
'ф' => 'f',
'х' => 'h',
'ц' => 'ts',
'ч' => 'ch',
'ш' => 'sh',
'щ' => 'sh',
'ь' => '',
'ы' => 'i',
'ъ' => '',
'э' => 'e',
'ю' => 'u',
'я' => 'ia',
'А' => 'A',
'Б' => 'B',
'В' => 'V',
'Г' => 'G',
'Д' => 'D',
'Е' => 'E',
'Ё' => 'E',
'Ж' => 'G',
'З' => 'Z',
'И' => 'I',
'Й' => 'I',
'К' => 'K',
'Л' => 'L',
'М' => 'M',
'Н' => 'N',
'О' => 'O',
'П' => 'P',
'Р' => 'R',
'С' => 'S',
'Т' => 'T',
'У' => 'Y',
'Ф' => 'F',
'Х' => 'H',
'Ц' => 'Ts',
'Ч' => 'Ch',
'Ш' => 'Sh',
'Щ' => 'Sh',
'Ь' => '',
'Ы' => 'I',
'Ъ' => '',
'Э' => 'E',
'Ю' => 'U',
'Я' => 'Ia',
);
$s = str_replace(array_keys($map), array_values($map), $s);
if(isset($params['tolower']) && $params['tolower'] == true) $s = strtolower($s);
if(isset($params['symbols'])) {
$s = preg_replace('/[^a-z0-9A-Z'.$params['symbols'].']/', '_', $s);
$s = preg_replace('/_{2,}/','_', $s);
$s = trim($s, '_');
}
return $s;
}
}
?>