<?php

@error_reporting(E_ALL ^ E_NOTICE);
@ini_set('display_errors', true);
//@ini_set('html_errors', false);
@ini_set('error_reporting', E_ALL ^ E_NOTICE);
date_default_timezone_set('Europe/Kiew');

list($msec, $sec) = explode(chr(32), microtime());
$conf['headtime'] = $sec + $msec;

$window = $_POST['window'];
$ajax = $_POST['ajax'];
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {$ajax_query = true;}

session_start();
session_name('sid');

foreach ($_GET as $check_url) {
	if (!is_string($check_url) || !preg_match("#^([A-zА-я-0-9ІіЇїЄє.])+$#ui", str_replace("'", "", $check_url))) {
    	header ('Location: ../');
    	exit;
	} 
} 


define ('DBHOST', 'localhost');
define ('DBNAME', 'kphp');
define ('DBPASS', 'hIP3te6IYAVI2agaB7q14iCALepu8o');
define ('DBUSER', 'root');

/*
define ('DBHOST', 'localhost');
define ('DBNAME', 'test');
define ('DBPASS', '');
define ('DBUSER', 'root');
*/

$db = mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error('Оновлення сайту.Сайт буде працювати через декільки хвилин!'));
mysql_query('SET NAMES `utf8`', $db);
mysql_select_db(DBNAME, $db) or die('Онолення!');

$system = mysql_fetch_array(mysql_query("SELECT * FROM `settings` WHERE `id` = 1 LIMIT 1"));
define('URL', 'http://'. $system['url']);
define('HOME', $_SERVER['DOCUMENT_ROOT']);

function go($link) {
	header('location: '.$link); exit;
	}

function check($str) {
    $str = htmlentities(trim($str), ENT_QUOTES, 'UTF-8');
    $str = nl2br($str);
	
    $str = strtr($str, array (
        chr(0)=> '',
        chr(1)=> '',
        chr(2)=> '',
        chr(3)=> '',
        chr(4)=> '',
        chr(5)=> '',
        chr(6)=> '',
        chr(7)=> '',
        chr(8)=> '',
        chr(9)=> '',
        chr(10)=> '',
        chr(11)=> '',
        chr(12)=> '',
        chr(13)=> '',
        chr(14)=> '',
        chr(15)=> '',
        chr(16)=> '',
        chr(17)=> '',
        chr(18)=> '',
        chr(19)=> '',
        chr(20)=> '',
        chr(21)=> '',
        chr(22)=> '',
        chr(23)=> '',
        chr(24)=> '',
        chr(25)=> '',
        chr(26)=> '',
        chr(27)=> '',
        chr(28)=> '',
        chr(29)=> '',
        chr(30)=> '',
        chr(31)=> ''
    ));
	
    $str = str_replace("\'", "&#39;", $str);
    $str = str_replace('\\', "&#92;", $str);
    $str = str_replace("|", "I", $str);
    $str = str_replace("||", "I", $str);
    $str = str_replace("/\\\$/", "&#36;", $str);
	$str = str_replace("</br></br></br>", "</br>", $str);
    $str = mysql_real_escape_string($str);
    return $str;
	
}



function links($text) {

	$text = preg_replace("/(^|[\n ])([\w]*?)((ht|f)tp(s)?:\/\/[\w]+[^ \,\"\n\r\t<]*)/is", "$1$2<a href=\"$3\" target=\"blank\">$3</a>", str_replace("<br />", "\n", $text,$result));
	return str_replace("\n", "</br>", $text);

	}
	
	
function hastag($tagg) {
$tagg = preg_replace("/@([\S']+)/", "<a href='/feed/search.php?search=\\1'>@\\1</a>", $tagg);
return str_replace("\n", "<br />", $tagg);
}




if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif (isset($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP)) $ip = $_SERVER['REMOTE_ADDR'];
else die('Unknown');
$ua = check($_SERVER['HTTP_USER_AGENT']); 

function check_mobile() { 
$ua = strtolower($_SERVER['HTTP_USER_AGENT']); 
$mobile_agent_array = array('ipad', 'iphone', 'android', 'pocket', 'palm', 'windows ce', 'windowsce', 'cellphone', 'opera mobi', 'ipod', 'small', 'sharp', 'sonyericsson', 'symbian', 'opera mini', 'nokia', 'htc_', 'samsung', 'motorola', 'smartphone', 'blackberry', 'playstation portable', 'tablet browser');
// var_dump($agent);exit;
foreach ($mobile_agent_array as $value) { 
if (strpos($ua, $value) !== false) return true; 
} 
return false; 
}

$is_mobile_device = check_mobile();
if($is_mobile_device) $device = 'Mobile'; else $device = 'Computer';

?>