<?php
	/* WebWall */
	error_reporting(1);
	ini_set('display_errors', 1);
	$servertimezone = 'Europe/Chisinau';
	date_default_timezone_set($servertimezone);
	session_start();
	require_once 'core/config/mysql.config.php';
	require_once('core/engine.php');
	$engine_session = new engine();
	require_once('core/template.php');
	$template_session = new template();
	if(ADMIN==1)  
	{
		require_once('administration/admin.php');
		$admin_session = new admin();
	}
	define("IMG_PATH", "../../media/images/users/");
	define("GIFTS_IMG_PATH", "../media/images/gifts/");
	define("USER_ID", $_SESSION['id']);
	define("USER_USERNAME", $_SESSION['account_username']);
	define("USER_RIGHTS", $_SESSION['__rights']);
	define("USER_RANK", $_SESSION['rank']);
	define("USER_IP", getenv("REMOTE_ADDR"), true);
	define("SESSION_STATUS", $_SESSION['login']);
	define("SESSION_MESSAGE", $_SESSION['msg']);
	define("ANONYMOUS",getlanguage(82,1));
	define("SCRIPT_VERSION", "1.1");
	//define("LICENCE", "http://xandr.me/portfolio/webwall");
	//define("AUTHOR", "http://xandr.me/");

	/* FACEBOOK AUTH */
	define("FB_ID","255556901270808"); // YOUR Client ID
	define("FB_SECRET","2b8c76b6322361da74c1087092612d99"); // YOUR Client secret

	$website = $engine_session->getinfo("site_config", "*", 0);
	define("SITE_NAME", $website['website_name'], true);
	define("SITE_DOMAIN", $website['website_domain'], true);
	define("SITE_DESCRIPTION", $website['website_description'], true);
	define("SITE_KEYWORDS", $website['website_keywords'], true);
	define("GET_FREE_POINTS", $website['website_free_points'], true);
	define("GIFT_PRICE", $website['website_gift_price'], true);
	define("USER_ANONYM", $engine_session->getlangvar(82), true);

	$paypal = $engine_session->getinfo("paypal_settings", "*", 0);
	define("PAYMENT_ENABLED", $paypal['enabled'], true);
	define("PAYMENT_MODE", $paypal['pay_mode'], true);
	define("PAYMENT_APIUSERNAME", $paypal['apiusername'], true);
	define("PAYMENT_APIPASSWORD", $paypal['apipassword'], true);
	define("PAYMENT_APISIGNATURE", $paypal['apisignature'], true);
	define("PAYMENT_CURRENCYCODE", $paypal['currencycode'], true);
	define("PAYMENT_PRICE", $paypal['price'], true);

	define("O_LENGTH", 200, true); // post max length
	define("PHOTO_MAX_SIZE", 5097152, true); // 5MB
	define("PHOTO_MAX_WIDTH", 5000, true); // 5000 Pixels
	define("PHOTO_MAX_HEIGHT", 5000, true); // 5000 Pixels

	if(USER_ID!="")
	{
		$engine_session->savevisit();
	}
	
	function counttime($date)
	{	
		$ndate = strtotime($date);
		$time = usertime(time());
		$time = strtotime($time);
		$time = $time - $ndate; 
		$tokens = array (
			86400 => ''.langVar(858).'',
			3600 => ''.langVar(857).'',
			60 => ''.langVar(856).'',
			1 => ''.langVar(855).''
		);
		if($time==0 || $time<4)
		{
			return ''.langVar(861).'';
		}
		elseif($time>3 && $time < 604800)
		{
			foreach ($tokens as $unit => $text) 
			{
				if ($time < $unit) continue;
				$numberOfUnits = floor($time / $unit);
				return $numberOfUnits.' '.$text.' '.langVar(859).'';
			}
		}
		else
		{
			return ''.langVar(860).' '.$date;
		}
	}
	
	function usertime($date)
	{	
		if($_SESSION['tzname']!="")
		{
			$tz = new DateTimeZone($_SESSION['tzname']);
		}
		else
		{
			$tz = new DateTimeZone($servertimezone);
		}
		$dtStr = date("c", $date);
		$date = new DateTime($dtStr);
		$date->setTimeZone($tz);
		return $date->format('d.m.Y H:i:s');	
	}	

	function hashtags($text)
	{
	    $in = array('`@(\S+[[:alnum:]]/?)`si');
	    $out = array('<a href="http://'.SITE_DOMAIN.'/$1" class=hashtag rel=nofollow target=_blank>@$1</a> ');
	    return preg_replace($in, $out, $text);
	}
	
	function txt2link($url)
	{
		$url = str_replace("\\r","\r",$url);
		$url = str_replace("\\n","\n<BR>",$url);
		$url = str_replace("\\n\\r","\n\r",$url);
		$in=array(
			'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
			'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
		);
		$out=array(
			'<a href="$1"  rel=nofollow>$1</a> ',
			'<a href="http://$1" rel=\'nofollow\'>$1</a>'
		);
		return hashtags(preg_replace($in,$out,$url));
	}
	
	function getsmiles($text)
	{
		$text = preg_replace("#]:\)#is","<img class='smile' src='media/images/smiles/12.gif' border='0'>",$text);
		$text = preg_replace("#:\)#is","<img class='smile' title='$1' src='media/images/smiles/1.gif'/>",$text);
		$text = preg_replace("#:\*#is","<img class='smile' src='media/images/smiles/2.gif' border='0'>",$text);
		$text = preg_replace("#\(:\|#is","<img class='smile' src='media/images/smiles/3.gif' border='0'>",$text);
		$text = preg_replace("#:\(#is","<img class='smile' src='media/images/smiles/4.gif' border='0'>",$text);
		$text = preg_replace("#-_#is","<img class='smile' src='media/images/smiles/5.gif' border='0'>",$text);
		$text = preg_replace("#:D#is","<img class='smile' src='media/images/smiles/6.gif' border='0'>",$text);
		$text = preg_replace("#\(o.o\)#is","<img class='smile' src='media/images/smiles/7.gif' border='0'>",$text);
		$text = preg_replace("#;\)#is","<img class='smile' src='media/images/smiles/8.gif' border='0'>",$text);
		$text = preg_replace("#:P#is","<img class='smile' src='media/images/smiles/9.gif' border='0'>",$text);
		$text = preg_replace("#\(:S\)#is","<img class='smile' src='media/images/smiles/10.gif' border='0'>",$text);
		$text = preg_replace("#\(8\)\)#is","<img class='smile' src='media/images/smiles/11.gif' border='0'>",$text);
		$text = preg_replace("#\(envy\)#is","<img class='smile' src='media/images/smiles/13.gif' border='0'>",$text);
		$text = preg_replace("#\(lol\)#is","<img class='smile' src='media/images/smiles/14.gif' border='0'>",$text);
		$text = preg_replace("#\(love\)#is","<img class='smile' src='media/images/smiles/15.gif' border='0'>",$text);
		$text = preg_replace("# :\/#is","<img class='smile' src='media/images/smiles/16.gif' border='0'>",$text);
		return $text;
	}

	function text_filter($text, $mode)
	{
		/* $mode = 1 - insert in database, 2 - read from database */
		if($mode==1) 
		{
			$text = preg_replace(sql_regcase("/(drop table|show tables|#|--|\\\\)/"),"",$text);
			$text = preg_replace("/(\<script)(.*?)(script>)/si", "", $text);
			$text = str_replace("<!--", "&lt;!--", $text);
			$text = mysql_real_escape_string($text);  
			//$text=str_replace('\r\n','<br>',$text);
		}
		if($mode==2)
		{
			$text = nl2br(htmlentities($text, ENT_QUOTES));
		}
		return $text;
	}
	
	function langvar($varID)
	{
		global $template_session;
		if(isset($varID))
		{
			if(is_int($varID))
			{
				return $template_session->getlangvar($varID);
			}
		}
	}

	function getpostcomments($postID)
	{
		return mysql_num_rows(mysql_query('SELECT * from comments where post_id = "'.$postID.'" ORDER BY id'));
	}
	
	function getlikes($id,$nr,$user,$type="1")
	{
		$message = "";
		$sqlquer = "SELECT * FROM likes WHERE post_id = ".$id."";
		$quer = mysql_query($sqlquer);
		$total = mysql_num_rows($quer);
		if($total != 0)
		{
			$sqlquery = "SELECT * FROM likes WHERE post_id = ".$id." ORDER BY id DESC limit 0, ".$nr."";
			$query = mysql_query($sqlquery);
			if(mysql_num_rows($query) != 0)
			{	
				while($ii = mysql_fetch_assoc($query))
				{
					$c_id = $ii['id']; 
					$c_q_id = $ii['q_id']; 
					$c_user = $ii['user'];
					if($c_user==USER_USERNAME) $liked = true;
				}
				$message .= "<div id='likesbox'><p id='countlike' style='float:left'><a onclick='loadlikes($id);'><span class='countlike".$id."'>".$total."</span> ".langVar(805)."</span></a>";
				if($liked) $message .= "</p><div class='liked' ";
				else $message .= "</p><div class='like' ";
				if(SESSION_STATUS != false) 
				{
					$message .= "onclick='like($id)'";
				}
				$message .= "></div></div>";
			}
		}
		else
		{
			$message .= "<div id='likesbox'><div class='like' ";
			if(SESSION_STATUS != false) 
			{
				$message .= "onclick='like($id)'";
			}
			$message .= "></div><p id='countlike' style='float:left'><span class='countlike".$id."'>".$total."</span> ".langVar(805)."</p></div>";
		}
		if($type!=0)
		{
			$message .= '<div id="commentbox"><a href="http://'.SITE_DOMAIN.'/'.$user.'/post/'.$id.'" class="postLink"><p id=\'countlike\'>'.getpostcomments($id).' '.langVar(406).'</p></a></div>';
		}
		return $message;
	}

	function getposts($user, $nr, $q)
	{	
		$message = '';
		if($q=='') $q=10;
		$sql = "SELECT * FROM posts WHERE to_user = '".$user."' ORDER BY time DESC limit ".$nr.", ".$q."";
		$query = mysql_query($sql);
		$i=1;
		if (@mysql_num_rows($query) != 0)
		{
			while($ii = mysql_fetch_assoc($query))
			{
				$id = $ii['id']; 
				$to_user = $ii['to_user']; 
				$post = txt2link(getsmiles($ii['post'])); 
				$from_user = $ii['from_user']; 
				$image = $ii['image'];
				$status = $ii['status']; 
				$anonymous = $ii['anonymous']; 
				$ip = $ii['ip']; 
				$time = $ii['time']; 
				$sqlgetphoto = mysql_query("SELECT photo FROM users WHERE username = '$from_user'");
					$iii = mysql_fetch_assoc($sqlgetphoto);
					if (!$iii['photo']) $iii['photo'] = "photo_anonymous.png";
				$message .= '<div class="post';
				if($i%2==0) $message .= ' postbg';
				$i++;
				$message .= '" id="post'.$id.'"><div id="postimage">';
				if (file_exists('../../media/images/users/tt_'.$iii['photo'].''))
				{
					if($anonymous!=1) $message .= "<a href=\"http://".SITE_DOMAIN."/".$from_user."\">";
					$message .= '<div class="img imgLiquid" style="width:60px; height:60px">';
					$message .= "<img src=\"media/images/users/tt_".$iii['photo']."\" id=\"postavatar\"/>";
					$message .= '</div>';
					if($anonymous!=1)  $message .= "</a>";
					$message .= "</div>";
				}
				else 
				{
					if($anonymous!=1) $message .= "<a href=\"http://".SITE_DOMAIN."/".$from_user."\">";
					$message .= '<div class="img imgLiquid" style="width:60px; height:60px">';
					$message .= "<img src=\"media/images/users/".$iii['photo']."\" id=\"postavatar\"/>";
					$message .= '</div>';
					if($anonymous!=1)  $message .= "</a>";
					$message .= "</div>";
				}
				$message .= '<div id="postbody"><h4>'.$post.'</h4>'; 
				if ($image!="")
				{
					$message .= '<a href="http://'.SITE_DOMAIN.'/image/'.$user.'/'.$id.'"><img src="media/images/answers/'.$image.'" width="300px"/></a>';
				}
				$message .= '</div>';
				$message .= "<div id='postInfo'><p>";
				$message .= ''.getlikes($id,8,$user,$id).'';
				$message .= counttime(usertime($time));
				switch($anonymous)
				{	case 0:	$message .= ' '.langVar(66).' <a href="http://'.SITE_DOMAIN.'/'.$from_user.'">'.$from_user.'</a>  ';
							break;
					case 1:	$message .= ' '.langVar(82).'  ';
							break;
				}
				if (($from_user == USER_USERNAME || $user == USER_USERNAME) && SESSION_STATUS != false)
				{	
					$message .= '<a href="delete/'.engine::crypts($id, 2, 0).'" class="removeResponseLink"> ['.langVar(68).']</a>';
				}
				$message .= '</p></div></div>';
				$message .= '<div class="clear"></div>';
			}
		}
		echo $message;
	}
	
	function readparameter($value) 
	{
		echo (''.$value.''); 
	}

	function getlanguage($id, $return = 0)
	{
		global $template_session;
		$res = is_numeric($id) ? true : false; 
		if($res && $return == 0)
		{
			echo $template_session->getlangval($id);
		}
		elseif($res && $return != 0)
		{
			return $template_session->getlangval($id);
		}
	}

	function fakebooltoenum($bool)
	{
		if($bool == "true")
		{
			return 1;
		}
		return 0;
	}

	function checkuserloggedoff() 
	{	
		if(SESSION_STATUS != true) 
		{ 
			header('Location: http://'.SITE_DOMAIN.'/account/login');
		} 
	}

	function checkuserloggedin() 
	{	
		if(SESSION_STATUS != false) 
		{
			header('Location: http://'.SITE_DOMAIN.'/'.USER_USERNAME.'');
		} 
	}	
?>