File: /var/www/ilya/data/www/povoleni.com/includes/functions.inc
<?
#————————————————————> CMS Termit <———————————————————————#
# #
# Author: Ilya Rudomilov #
# #
# E-mail: ilya@grafix.ru #
# URL: http://ilya.grafix.ru #
# ICQ: 206255213 #
# #
#—————————————> GrafiX Design — (c) 2005 <————————————————#
function get_application_number($data) {
while($data['code'][0]=="0") {
$data['code'] = substr($data['code'],1);
}
if(strpos($data['code'],"-"))
$data['code'] = substr($data['code'],0,strpos($data['code'],"-"));
return "OAM-".$data['code']."/".$data['type']."-".$data['year'];
}
function cache_write($dir,$filename,$content) {
$dir_target = "cache/";
foreach($dir as $dir_once) {
$dir_target .= $dir_once."/";
if(!is_dir($dir_target))
mkdir($dir_target);
}
//print $dir_target.$filename;
@unlink($dir_target.$filename);
$handle = fopen($dir_target.$filename,"x+");
fwrite($handle,$content);
fclose($handle);
}
function cache_check($dir,$filename,$expire) {
$dir_target = "cache/";
foreach($dir as $dir_once) {
$dir_target .= $dir_once."/";
}
if (file_exists($dir_target.$filename))
if((time()-filemtime($dir_target.$filename))<$expire) {
return true;
}
return false;
}
function cache_read($dir,$filename) {
$dir_target = "cache/";
foreach($dir as $dir_once) {
$dir_target .= $dir_once."/";
}
if(filesize($dir_target.$filename)>0) {
$handle = fopen($dir_target.$filename, "r");
if(!($handle))
return false;
$contents = fread($handle, filesize($dir_target.$filename));
fclose($handle);
return $contents;
}
else
return "";
}
function cache_delete($dir,$filename) {
$dir_target = "cache/";
foreach($dir as $dir_once) {
$dir_target .= $dir_once."/";
}
if(file_exists($dir_target.$filename)) {
unlink($dir_target.$filename);
}
else
return false;
}
function query($query) {
// ********* Выполнение запроса к базе MySQL *************
global $global_vars;
//timestats_start("sql");
$result = mysql_query($query,$global_vars['db_connection']) or error("1107");
//$result = mysql_query($query,$global_vars['db_connection']) or print("Error: <strong>".$query."</strong>");
//echo mysql_errno($global_vars['db_connection']) . ": " . mysql_error($global_vars['db_connection']);
//timestats_stop("sql");
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) {
// ********* Отправка 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`='".$global_vars['lang']."' LIMIT 1;");
$result = mysql_fetch_array($result);
$message = @join("",@file("./skin/".$global_vars['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 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;
}
function check_application_frs($data) {
print $data['code']; exit;
//set the header params
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";
//assign to the curl request.
$result = file_get_contents("https://frs.gov.cz/cs/ioff/application-status");
$frs_check = ereg_replace('(.*)form_build_id" value="', "", $result);
$frs_check = ereg_replace('" />(.*)', "", $data);
if(strpos($data['code'],"-"))
$data['code'] = substr($data['code'],0,strpos($data['code'],"-"));
while($data['code'][0]=="0") {
$data['code'] = substr($data['code'],1);
}
//set POST variables
$url = 'https://frs.gov.cz/cs/ioff/application-status';
$fields = array(
'ioff_application_oam'=>urlencode('OAM'),
'ioff_application_number'=>urlencode($data['code']),
'ioff_application_code'=>urlencode($data['type']),
'ioff_application_year'=>urlencode($data['year']),
'ioff_zov'=>urlencode(''),
'form_build_id'=>urlencode($frs_check),
'form_id'=>urlencode('ioff_application_status_form')
);
$fields_string = "";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt ($ch,CURLOPT_TIMEOUT,120);
curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$comment = ereg_replace('(.*)</em>', "", $result);
$comment = ereg_replace('</strong></span>(.*)', "", $comment);
$comment = ereg_replace('(.*)<strong>', "", $comment);
if(strpos($result,"nesmí být delší"))
$comment = 'Příliš dlouhý kod';
elseif(strlen($comment)>50)
$comment = '';
return $comment;
}
?>