View file include/function.inc.php

File size: 9.27Kb
<?php 

/****************************
 * 
 *    @file		function.inc.php
 *    @copy		GMC (c) 2010
 *    @contact  ICQ 4884679
 *    @info		free
 *    
 ****************************/

/////////Таймер выполения скрипта

 function timer()
 {
         static $mark = null;

         if ($mark === null)
         {
                 $mark = microtime(0);
         }
         else 
         {
                 $res = microtime(0) - $mark;
                 $mark = null;
                 return substr($res, 0, 6);
         }
 }

 timer();
 
//////////////Подключение к БД/////////

 $mysqli = new mysqli(F_DB_HOST,F_DB_USER,F_DB_PASSWORD,F_DB);
 
 if (mysqli_connect_errno())
 {
     exit(mysqli_connect_error());
 }
 
 $mysqli->query("SET NAMES UTF8");

///////////////////////////////////////

function bParseCount($str, $do="CALL", $val = FALSE,$cat=FALSE)
{
	/*$do = CALL = count all
			$val = count $val action
			-------------------------
			ADD  = add 1 to action $val
			DEL  = del 1 from action $val
	*/
	//$str = '1;2;3;4';
	
	$strArrO = explode(";", $str);

	$cat = explode(";",$cat);

	foreach ($cat as $k => $v)
    {
		if(empty($v))
        {
            $cArr[$v] = '0';
        }
		else
        {
            $cArr[$v] = $strArrO[$k];
        }
	}
	$strArr = $cArr;

	if($do == "CALL")
    {
		if(!$val)
        {
            return array_sum($strArrO);
        }
		elseif ($val>0)
        {
            return $strArr[$val];
        }
		else
        {
            return FALSE;
        }
	}
	elseif($do == "ADD" AND $val)
    {
		$strArr[$val] = $strArr[$val]+1;

		return implode(";", $strArr);
	}
	elseif ($do == "DEL" AND $val)
    {
		$strArr[$val] = ($strArr[$val]>0)?$strArr[$val]-1:'0';

		return implode(";", $strArr);
	}
	else
    {
        return FALSE;
    }
}

function bDate($time)
{
	global $arrMonth;

	$enMonth=array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

    return str_replace($enMonth,$arrMonth,date("d M Y (H:i)",$time));
}

function bArrToSelect($name, $arr, $view_act, $checked=FALSE)
{
	global $lng_arr;

	$sel.= "<select name=\"".$name."\">";
	
	$sel.= "<option value=\"0\" selected=\"selected\">".$lng_arr['SELECT_CAT']."</option>";
	
	foreach ($arr as $key => $val)
    {
		foreach (bGetArrAct($view_act[$key]) as $key_a => $val_a)
        {
			$value = $key."_".$key_a;
			$check = ($value == $checked)?"selected=\"selected\"":"";
			
			$sel.= '<option value="'.$value.'" '.$check.'>'.$val.' / '.$val_a.'</option>';
		
		}
	}
	
	$sel.= "</select>";
	
	return $sel;
}

function bParseDataAdd(&$cat,&$city,&$mail,&$text,&$phone,&$url,&$error)
{
	global $lng_arr;
	
	if(empty($cat) OR empty($city) OR empty($mail) OR empty($text))
    {
		$error.= "<div class=\"error\">ERROR: ".$lng_arr['ERROR_EMPTY_FIELD']."</div><br/>";

        return FALSE;
	}
	
	$cat = explode("_", $cat);
	
	if(!is_numeric($cat['0']))
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SELECT_CAT']."</div><br/>";;

        return FALSE;
	}//Проверка $cat
	
	if(!preg_match("/^[a-zа-я0-9 _-]{3,}$/ui", $city))
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SELECT_CITY']."</div><br/>";;

        return FALSE;
	} //Проверка города

	if(!preg_match("/^([a-zа-я0-9_\.-]+)@([a-zа-я0-9_\.-]+)\.([a-zа-я\.]{2,6})$/ui",$mail))
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SELECT_EMAIL']."</div><br/>";;

        return FALSE;
	}//Проверка e-mail

	if(!empty($phone))
    {
		if(!preg_match("/^\+[0-9]{11}$/i", $phone))
        {
			$error.= 
			"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SELECT_PHONE']."</div><br/>";;

            return FALSE;
		}
	}
	if(!empty($url))
    {
		if(!preg_match("/^([a-zа-я0-9_\.-]+)\.[a-zа-я]{2,6}$/ui", $url))
        {
			$error.= 
			"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SELECT_URL']."</div><br/>";;

            return FALSE;
		}
	}
	return TRUE;
}

function bSendMail($idann,$mess,&$error)
{
	global $lng_arr, $mysqli;
	
	$viewAnn = "SELECT * FROM `".F_DB_PX."announcement` 
	WHERE `id` = '".(int)$idann."'";

	if(($annRes = $mysqli->query($viewAnn)) && $mysqli->affected_rows>0)
    {
		$ann = $annRes->fetch_assoc();

	}
    else
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SEND_MAIL']."</div><br/>";;

        return FALSE;
	}
	
	if(empty($mess) OR (mb_strlen($mess,'UTF-8')<10))
    {
		$error.=
	 		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SEND_MAIL_MESS']."</div><br/>";
	
		return FALSE;
	}
	
	if(!preg_match("/^([a-zа-я0-9_\.-]+)@([a-zа-я0-9_\.-]+)\.([a-zа-я\.]{2,6})$/ui",$ann['email']))
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SEND_MAIL']."</div><br/>";;

        return FALSE;
	}//Проверка e-mail
	
	$subj = str_replace("F_SITE_NAME",F_SITE_NAME,$lng_arr['SEND_MAIL_SUBJ']);
	$subj = '=?koi8-r?B?'.base64_encode(iconv("UTF-8", "koi8-r".'//IGNORE', $subj)).'?=';
	
	$to = $ann['email'];
	
	$headers = 'Content-type: text/plain; charset=koi8-r' . "\r\n";
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'FROM: admin@'.F_SITE_URL . "\r\n";
	
	
	$sendMess = $lng_arr['SEND_MAIL_MESSAGE']."\n\n";

	$pat = array(
	"/BSENDMAIL_TIME/ui",
	"/BSENDMAIL_IDANN/ui",
	"/BSENDMAIL_TEXTANN/ui",
	"/BSENDMAIL_MESSAGE/ui",
	"/F_SITE_NAME/ui");

	$rep = array(
	bDate($ann['time_add']),
	(int)$idann,
	htmlspecialchars($ann['text']),
	htmlspecialchars($mess),
	F_SITE_NAME);
	
	$sendMess = preg_replace($pat,$rep,$sendMess);

	$sendMess = iconv("UTF-8", "koi8-r".'//IGNORE', $sendMess);
	
	$headers = iconv("UTF-8", "koi8-r".'//IGNORE', $headers);
	
	if(mail($to,$subj,$sendMess,$headers))
    {
		return TRUE;
	}
	else
    {
		$error.= 
		"<div class=\"error\">ERROR: ".$lng_arr['ERROR_SEND_MAIL']."</div><br/>";

        return FALSE;
	}
}

function bGetArrAct($view_action,$id_view = FALSE)
{
	global $mysqli;
	
	$actionCat = "SELECT * FROM `".F_DB_PX."action_cat";

	if($actionCatRes = $mysqli->query($actionCat))
    {
		while($actView = $actionCatRes->fetch_assoc())
        {
			$actionCatResArr[$actView['id']] =  $actView['name'];
		}
	}
    else
    {
        return FALSE;
    }

		$view_act = explode(";",$view_action);
		foreach($view_act as $k => $v)
        {
			$view_act_arr[$view_act[$k]] = $actionCatResArr[$v];
		}

	if(!$id_view)
    {
		return $view_act_arr;
	}
	elseif($id_view)
    {
		return (empty($view_act_arr[$id_view]))?NULL:$view_act_arr[$id_view];
	}
}

function bCheckSpam($ip)
{
	global $mysqli,$lng_arr;
	
	$dOldT = time() - 3600*24;
	
	$delOld = $mysqli->query("DELETE FROM `".F_DB_PX."antispam`
	 WHERE `time` > ".(int)$dOldT);
	
	$timeS = time()-F_TIME_SPAM;
	
	$checkIP = "SELECT * FROM `".F_DB_PX."antispam`
	WHERE `ip` = '".$ip."'
	AND `time` > '".$timeS."'";
	
	if($checkIPRes = $mysqli->query($checkIP) && $mysqli->affected_rows>0)
    {
		include_once './skins/tpl/header.tpl';
	
		include_once './skins/tpl/spam.tpl';
	
		include_once './skins/tpl/footer.tpl';
		
		exit();
		
	}
    else
    {
        return FALSE;
    }
}

function bViewNavigationCU($page)
{
	$url.= $_SERVER['PHP_SELF']."?";

    foreach ($_GET as $k => $v)
    {
        $url.= ($k=="p")?"&amp;".$k."=".$page:"&amp;".$k."=".$v;
    }
    $url.= empty($_GET['p'])?"&amp;p=".$page:"";
    $url.= "&amp;".SID;

    return $url;
}

function bViewNavigation($now_page = 1, $countAnn, &$start = 0)
{
	global $mysqli;
	
	$annC = $mysqli->query($countAnn);
	$annC = $annC->fetch_row();
	$stran = $annC['0'] / F_ANN_STR;
	$stran = ceil($stran);

	$tt = $now_page + 1;
	
	$ix=1; $all = $stran;
	
	echo "<div class=\"navigation\">";
	
	while($ix<=$all)
    {
		if($ix<=3 || $ix==$tt-1 || $ix==$tt || $ix==$tt+1 || $ix>=$all-2)
        {
			$page=$ix - 1;

			$url = bViewNavigationCU($page);
			
			$iix="<a href=\"".$url."\">$ix</a> ";
			
			if($ix==$tt) {
				
				if($ix!=$all)
                {
                    $c.= "<b>$ix</b> ";
                }
				else
                {
                    $c.= "<b>$ix</b> ";
                }
			}
			else
            {
				if($ix!=$all)
                {
                    $c.= "$iix ";
                }
                else
                {
                    $c.= "$iix";
                }
			}
		}
		else
        {
            $c.= "|:|:";
        }

		$ix++;
	} 

	$c=preg_replace("/[|:|:]+/","... ",$c); 
	
	echo $c."<br/>";
	
	if($now_page >= 1)
    {
		$back = $now_page - 1;
		echo "<a href=\"".bViewNavigationCU($back)."\">&lt;&lt;&lt;</a> |";
	}
	else
    {
        echo "&lt;&lt;&lt; |";
    }
		
	echo $now_page+1;
		
	if($stran-1 > $now_page)
    {
		$now_page = $now_page + 1;

		echo "| <a href=\"".bViewNavigationCU($now_page)."\">&gt;&gt;&gt;</a> ";
	}
	else
    {
        echo "| &gt;&gt;&gt;";
    }
	
	echo "</div>";
	
}

function bColorSearch($str,$s)
{
	$str = str_replace($s, "<b class=\"searchSTR\">".$s."</b>",$str);

	$s = mb_strtoupper($s,"UTF-8");

	$str = str_replace($s, "<b class=\"searchSTR\">".$s."</b>",$str);

	return $str;
}