File: /var/www/ilya/data/www/afish-ka.ru/admin/includes/functions.inc
<?php
#————————————————————> CMS Termit <———————————————————————#
# #
# Author: Ilya Rudomilov #
# #
# E-mail: ilya@grafix.ru #
# URL: http://ilya.grafix.ru #
# ICQ: 203015 #
# #
#—————————————> Grafix Design — (c) 2005 <————————————————#
function image_get_filetype($img_src_addr) {
$filetype = exif_imagetype($img_src_addr);
switch(exif_imagetype($img_src_addr)) {
case "2": $filetype = "jpg"; break;
case "1": $filetype = "gif"; break;
}
return $filetype;
}
function image_convert($img_src_addr,$img_dst_addr,$img_type) { // source path, destination path, type of image ("poster" or "image")
$src_size = GetImageSize($img_src_addr);
$img_filetype = image_get_filetype($img_src_addr);
if($img_type=="poster")
if($src_size[1]>700) {
$dst_size[1] = 700;
$dst_size[0] = $src_size[0]/($src_size[1]/$dst_size[1]);
}
else
$dst_size = $src_size;
else
if($src_size[0]>800) {
$dst_size[0] = 800;
$dst_size[1] = $src_size[1]/($src_size[0]/$dst_size[0]);
}
else
$dst_size = $src_size;
if ($img_filetype=="jpg")
$src_im = imagecreatefromjpeg($img_src_addr);
elseif ($img_filetype==("gif"))
$src_im = imagecreatefromgif($img_src_addr);
$dst_quality = 80; //Качество картинки JPEG
if ($img_filetype=="jpg")
$dst_im = ImageCreateTrueColor($dst_size[0],$dst_size[1]);
elseif ($img_filetype=="gif")
$dst_im = ImageCreate($dst_size[0],$dst_size[1]);
ImageCopyResampled($dst_im,$src_im,0,0,0,0,$dst_size[0],$dst_size[1],$src_size[0],$src_size[1]);
if ($img_filetype=="jpg")
$result = ImageJPEG ($dst_im,$img_dst_addr,$dst_quality);
elseif ($img_filetype==("gif"))
$result = ImageGIF ($dst_im,$img_dst_addr);
ImageDestroy ($dst_im);
ImageDestroy ($src_im);
return $result;
}
function mysql_fetch_array($data) {
return mysqli_fetch_array($data);
}
function mysql_num_rows($data) {
return mysqli_num_rows($data);
}
function mysql_real_escape_string($string) {
global $global_vars;
return mysqli_real_escape_string($global_vars['db_connection'],$string);
}
function query($query) {
// ********* Выполнение запроса к базе MySQL *************
global $global_vars;
$result = mysqli_query($global_vars['db_connection'],$query) or error("1107");
return $result;
}
function get_images($dir) {
$handle=opendir($dir);
$files = array();
while(false !== ($files[] = readdir($handle)));
sort($files);
closedir($handle);
return $files;
}
function mail_send($recipient,$subject,$message,$lang) {
// ********* Отправка email *************
global $global_vars;
$headers = "X-Sender: <".SITE_EMAIL.">\n";
$headers .= "X-Mailer: CMS Termit 1.8\n";
$headers .= "X-Priority: 3 (Normal)\n";
$headers .= "From: <".SITE_EMAIL.">\n";
$headers .= "Reply-To: <".SITE_EMAIL.">\n";
$headers .= "MIME-Version: 1.0\n";
$result = query("SELECT `mail_charset` FROM `languages` WHERE `code`='".$lang."' LIMIT 1;");
$result = mysql_fetch_array($result);
$message = @join("",@file("../skin/".$lang."/"."mails/".$message.".txt"));
$headers .= "Content-Type: text/plain; charset=".$result[0]."\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
$headers .= "MIME-Version: 1.0\n";
@mail($recipient,$subject,$message,$headers);
}
function utf8($text) {
// ********* Перекодирование текста из win1251 в UTF-8 *************
$text = iconv("windows-1251","UTF-8",$text);
return $text;
}
function str_islatin ($text) {
// ********* Проверка текста на то, что он состоит только из латинских символов и цифр *************
$text = strtolower($text);
$text = preg_replace("#[0-9]#i", "", $text);
$text = preg_replace("#[a-z]#i", "", $text);
$text = str_replace("-", "", $text);
$text = str_replace("_", "", $text);
if (strlen($text)==0) return TRUE;
else return FALSE;
}
function bb($txt) {
// ********* Конвертация bb-кодов *************
$txt = preg_replace( "#\[b\](.+?)\[/b\]#is", "<b>\\1</b>", $txt );
$txt = preg_replace( "#\[i\](.+?)\[/i\]#is", "<i>\\1</i>", $txt );
$txt = preg_replace( "#\[u\](.+?)\[/u\]#is", "<u>\\1</u>", $txt );
$txt = preg_replace( "#\[s\](.+?)\[/s\]#is", "<s>\\1</s>", $txt );
$txt = preg_replace( "#\(c\)#i" , "©" , $txt );
$txt = preg_replace( "#\(tm\)#i" , "™" , $txt );
$txt = preg_replace( "#\(r\)#i" , "®" , $txt );
$txt = preg_replace( "#\[email\](\S+?)\[/email\]#i" , "<a href='mailto:\\1'>\\1</a>", $txt );
$txt = preg_replace( "#\[email\s*=\s*\"\;([\.\w\-]+\@[\.\w\-]+\.[\.\w\-]+)\s*\"\;\s*\](.*?)\[\/email\]#i" , "<a href='mailto:\\1'>\\2</a>", $txt );
$txt = preg_replace( "#\[email\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/email\]#i" , "<a href='mailto:\\1'>\\2</a>", $txt );
$txt = preg_replace( "/\n/", "<br>", $txt );
$txt = preg_replace("/&#([0-9]+);/s", "&#\\1;", $txt );
$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie", "regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#ie" , "regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie", "regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
return $txt;
}
function regex_build_url($url=array()) {
$skip_it = 0;
// Make sure the last character isn't punctuation.. if it is, remove it and add it to the
// end array
if ( preg_match( "/([\.,\?]|!)$/", $url['html'], $match) )
{
$url['end'] .= $match[1];
$url['html'] = preg_replace( "/([\.,\?]|!)$/", "", $url['html'] );
$url['show'] = preg_replace( "/([\.,\?]|!)$/", "", $url['show'] );
}
// Make sure it's not being used in a closing code/quote/html or sql block
if (preg_match( "/\[\/(html|quote|code|sql)/i", $url['html']) )
{
return $url['html'];
}
// clean up the ampersands
$url['html'] = preg_replace( "/&/" , "&" , $url['html'] );
// Make sure we don't have a JS link
$url['html'] = preg_replace( "/javascript:/i", "java script: ", $url['html'] );
// Do we have http:// at the front?
if ( ! preg_match("#^(http|news|https|ftp|aim)://#", $url['html'] ) )
{
$url['html'] = 'http://'.$url['html'];
}
//-------------------------
// Tidy up the viewable URL
//-------------------------
if (preg_match( "/^<img src/i", $url['show'] )) $skip_it = 1;
$url['show'] = preg_replace( "/&/" , "&" , $url['show'] );
$url['show'] = preg_replace( "/javascript:/i", "javascript: ", $url['show'] );
if ( (strlen($url['show']) -58 ) < 3 ) $skip_it = 1;
// Make sure it's a "proper" url
if (!preg_match( "/^(http|ftp|https|news):\/\//i", $url['show'] )) $skip_it = 1;
$show = $url['show'];
if ($skip_it != 1) {
$stripped = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\2", $url['show'] );
$uri_type = preg_replace( "#^(http|ftp|https|news)://(\S+)$#i", "\\1", $url['show'] );
$show = $uri_type.'://'.substr( $stripped , 0, 35 ).'...'.substr( $stripped , -15 );
}
return $url['st'] . "<a href='".$url['html']."' target='_blank'>".$show."</a>" . $url['end'];
}
function convert_date($sql_date) {
// ********* Перевод даты в удобочитаемую форму день.мес *************
list($sql_date,$time)=split(" ",$sql_date);
list($year,$mon,$day)=split("-",$sql_date);
$convert_date=$day.".".$mon.".".substr($year,2,2);
return $convert_date;
}
function date2stamp($datetime) {
// ****** Конвертация SQL-datetime в SQL-timestamp **********
list($date,$time)=split(" ",$datetime);
list($hour,$mins,$sec) = split(":",$date);
list($year,$mon,$day) = split("-",$date);
$timestamp = mktime($hour,$mins,$sec,$mon,$day,$year);
return $timestamp;
}
function human_date($sql_date) {
// ********* Перевод даты в удобочитаемую форму "день месяц" *************
list($date,$time)=split(" ",$sql_date);
list($year,$mon,$day)=split("-",$date);
switch ($day_w)
{
case "0": $day_w = "воскресенье"; break;
case "1": $day_w = "понедельник"; break;
case "2": $day_w = "вторник"; break;
case "3": $day_w = "среда"; break;
case "4": $day_w = "четверг"; break;
case "5": $day_w = "пятница"; break;
case "6": $day_w = "суббота"; break;
}
switch($mon)
{
case 1: $mon="января"; break;
case 2: $mon="февраля"; break;
case 3: $mon="марта"; break;
case 4: $mon="апреля"; break;
case 5: $mon="мая"; break;
case 6: $mon="июня"; break;
case 7: $mon="июля"; break;
case 8: $mon="августа"; break;
case 9: $mon="сентября"; break;
case 10: $mon="октября"; break;
case 11: $mon="ноября"; break;
case 12: $mon="декабря"; break;
default: $mon="";
}
if (substr($day,0,1)=="0") $day=substr($day,1,1); //Убираем ноль в начале дня, если он есть
$convert_date=$day." ".$mon;
return $convert_date;
}
function text_replace($text) {
// *************** Перевод HTML'я в текст *******************
$search = array ("'<script[^>]*?>.*?</script>'si", // Strip out javascript
"'<[\/\!]*?[^<>]*?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out white space
"'&(quot|#34);'i", // Replace html entities
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as php
$replace = array ("",
"",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
$text = preg_replace ($search, $replace, $text);
return $text;
}
?>