<?php
/**
 * @version $Id: common.inc.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);

$loadTime = explode(' ', microtime());
$loadTime = $loadTime[0] + $loadTime[1];
$timestamp = time();

define('IN_PBDIGG', TRUE);
define('PBDIGG_ROOT', str_replace('\\', '/', substr((dirname(__FILE__)), 0, -7)));

$_PBDEV = $_PBENV = $customer = array();
$_PBDEV['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$_PBDEV['REQUEST_URL'] = $_PBDEV['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
$_PBDEV['QUERY_STRING'] = addslashes($_SERVER['QUERY_STRING']);
$_PBDEV['PB_DOMAIN'] = addslashes($_SERVER['SERVER_NAME']);
$_DS = DIRECTORY_SEPARATOR;
$_PBVERSION = '2.0 ÖÜÄê°æ';
$_PBRELEASE = '20081118';

if (function_exists('date_default_timezone_set')) @date_default_timezone_set('Etc/GMT+0');

require_once(PBDIGG_ROOT.'include/global.func.php');
require_once(PBDIGG_ROOT.'data/sql.inc.php');
if (PB_CSCRIPT != 'ajax') header('Content-Type: text/html; charset='.$db_charset);

if (!get_magic_quotes_gpc())
{
	addS($_GET);
	addS($_POST);
	addS($_COOKIE);
}
@extract($_GET);
@extract($_POST);
unset($_ENV, $HTTP_ENV_VARS, $_REQUEST, $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_POST_FILES, $HTTP_COOKIE_VARS);

require_once(PBDIGG_ROOT.'include/Cache.class.php');
$Cache = new Cache;
if (!@include_once(PBDIGG_ROOT.'data/cache/cache_config.php'))
{
	$Cache->setCacheList('config');
}
$Cache->setLifeTime($pb_cachetime);
if ($pb_siteurl)
{
	$_PBENV['PB_URL'] = $pb_siteurl;
}
else
{
	$_PBDIR = dirname($_PBDEV['PHP_SELF']).'/';
	if ($_PBDIR{0} == '\\') $_PBDIR = substr($_PBDIR, 1);
	$_PBENV['PB_URL'] = 'http://'.$_SERVER['HTTP_HOST'].$_PBDIR;
}

if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== FALSE)
{
	$_PBDEV['PB_REFERER'] = addslashes($_SERVER['HTTP_REFERER']);
}

require_once(loadLang('common'));
require_once(loadLang('func'));
require_once(loadLang('ui'));
require_once(PBDIGG_ROOT.'include/smarty.inc.php');

if ($pb_ucenable)
{
	require_once(PBDIGG_ROOT.'config.inc.php');
	require_once(PBDIGG_ROOT.'client/client.php');
}
$TPL->assign('uimsg', $ui_message);
if ($pb_gzip && (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) && function_exists('ob_gzhandler'))
{
	ob_start('ob_gzhandler');
}
else
{
	ob_start();
}

if ($pb_refreshtime)
{
	$pb_lastvisit = gCookie('pb_lastvisit');
	if ($pb_lastvisit)
	{
		list($lastVisit, $lastPath) = explode("\t", $pb_lastvisit);
		$seperateTime = $timestamp - $lastVisit;
	}
	else
	{
		$seperateTime = $lastPath = '';
	}
	if (($seperateTime < $pb_refreshtime) && ($lastPath == $_PBDEV['REQUEST_URL']))
	{
		showMsg($u_message['refresh_limit']);
	}
}

list($_PBDEV['PB_IP'], $_PBDEV['PB_ISAGENT']) = getIP();

ipControl($_PBDEV['PB_IP']);

if ($_DS == '/' && $pb_loadavg)
{
	if($fp = @fopen('/proc/loadavg','rb'))
	{
		list($loadavg) = explode(' ', fread($fp, 6));
		fclose($fp);
		if (($loadavg > $pb_loadavg) || (!$_COOKIE && !$_SERVER['HTTP_USER_AGENT']))
		{
			 exit($u_message['loadavg_limit']);
		}
	}
}
$tid = (int)$tid;
$cid = (int)$cid;
$_PBENV['PB_VERSION'] = array($_PBVERSION, $_PBRELEASE);
$_PBENV['PB_CHARSET'] = $db_charset;
$TPL->assign('PB_ENV', $_PBENV);

require_once(PBDIGG_ROOT.'include/'.$_PBDEV['dbType'].'.class.php');
$DB = new MySQL($db_host, $db_username, $db_password, $db_name, $db_pconnect);

require_once(PBDIGG_ROOT.'include/authentication.func.php');
$pb_auth = '';
$authFailed = true;
$customer = array('sid'=>'','uid'=>'0','username'=>'','pw'=>'','groupid'=>'5', 'adminid'=>'0', 'online'=>'0', 'avatar'=>'', 'tdigg'=>'', 'cdigg'=>'', 'tbury'=>'', 'cbury'=>'');
$pb_auth = gCookie('pb_auth') ? gCookie('pb_auth') : $_POST['pb_auth'];
if ($pb_auth)
{
	list($p_uid, $p_pw) = explode("\t", PDecode($pb_auth, $pb_sitehash));
	if (is_numeric($p_uid) && strlen($p_pw) == 32)
	{
		$p_uid = (int)$p_uid;
		$p_pw = addslashes($p_pw);

		$c_data = $ifdigg = $ifbury = '';
		$pb_ifdigg && $ifdigg = 'md.tdigg, md.cdigg, ';
		$pb_ifbury && $ifbury = 'md.tbury, md.cbury, ';

		$SQL = "SELECT m.uid, m.username, m.password AS pw, m.email, m.adminid, m.groupid, m.publicemail, 
				md.realgroup, md.postnum, md.commentnum, {$ifdigg}md.diggnum, {$ifbury}md.burynum, md.currency, md.lastip, md.currentip, md.lastvisit, md.lastpost, md.lastcomment, md.uploadnum, md.newmsg, md.visitcount, 
				mx.avatar 
				FROM {$db_prefix}members m 
				INNER JOIN {$db_prefix}memberdata md 
				ON m.uid = md.uid 
				LEFT JOIN {$db_prefix}memberexp mx 
				ON m.uid = mx.uid 
				WHERE m.uid = '$p_uid'";
		$c_data = $DB->db_fetch_one_array($SQL);
		if ($c_data && $c_data['pw'] == $p_pw && is_array($c_data))
		{
			$customer = array_merge($customer, $c_data);
			$authFailed = false;
		}
	}
}
if ($authFailed)
{
	sCookie('pb_auth', '', -1);
	$pb_auth = '';
}

$TPL->assign('pb_auth', $pb_auth);
if ($customer['groupid'] == '-1')
{
	$customer['groupid'] = $customer['realgroup'];
}

$gtype = $grouptitle = $uplower = $uphigher = $allowvisit = $allowsort = $allowread = $allowpost = $allowcomment 
= $allowdigg = $allowbury = $allowbsearch = $allowasearch = $searchmax = $allowreport = $allowhtml = $allowmsg 
= $msgmax = $allowfavors = $favorsmax = $allowavatar = $allowaupload = $uploadmax = $uploadtype = $log_status = $siteFounder = '';
if (!@include_once(PBDIGG_ROOT.'data/cache/cache_usergroup_'.(int)$customer['groupid'].'.php'))
{
	$Cache->setCacheList('userGroupCache');
}

if ($customer['adminid'] && in_array($customer['adminid'], array('1', '2')))
{
	if (!@include_once(PBDIGG_ROOT.'data/cache/cache_admingroup_'.$customer['adminid'].'.php'))
	{
		$Cache->setCacheList('adminGroupCache');
	}
	$pbadmin = '1';
}
else
{
	$allowadmincp = $alloweditatc = $allowdelatc = $allowlockatc = $allowmoveatc = $allowcopyatc = $allowtopatc = '';
	$customer['adminid'] = $pbadmin = '0';
}
$TPL->assign('allowdigg', $allowdigg);
$TPL->assign('allowbury', $allowbury);
$TPL->assign('allowcomment', $allowcomment && ($pb_ifcomment || $pbadmin));

if (!$pb_ifopen && $customer['adminid'] != '1')
{
	clearcookie();
	showMsg($pb_whyclosed, $u_message['js_close_window']);
}
if (!$allowvisit && PB_CSCRIPT != 'login')
{
	clearcookie();
	showMsg($u_message['visit_deny'], 'login.php');
}
$grouplevel = $plugins = $categories = array();
$tdigg = $cdigg = $tbury = $cbury = '';
if (!@include_once(PBDIGG_ROOT.'data/cache/cache_grouplevel.php'))
{
	$Cache->setCacheList('grouplevel');
}
if (!@include_once(PBDIGG_ROOT.'data/cache/cache_categories.php'))
{
	$Cache->setCacheList('categories');
}
if (!@include_once(PBDIGG_ROOT.'data/cache/cache_plugins.php'))
{
	$Cache->setCacheList('plugins');
}

if ($Cache->refreshCache())
{
	exit('<p>'.$u_message['cache_load_success'].'</p>');
}
$cid && @require_once(PBDIGG_ROOT.'data/cache/cache_singlecate_'.$cid.'.php');
if ($customer['uid'])
{
	if ($customer['newmsg']) $customer['newmsg'] = '<span class="newmsg">'.$customer['newmsg'].'</span>';
	$TPL->assign('log_status', '1');
	$tdigg = $customer['tdigg'];
	$cdigg = $customer['cdigg'];
	$tbury = $customer['tbury'];
	$cbury = $customer['cbury'];
	$log_status = 1;
}
$customer['uurl'] = 'user.php?uid='.$customer['uid'];
if (!$customer['avatar'])
{
	$customer['avatar'] = $_PBENV['PB_URL'].'images/avatars/portrait/default.png';
}
elseif (substr($customer['avatar'], 0, 7) != 'http://')
{
	$customer['avatar'] = $_PBENV['PB_URL'].'images/avatars/'.$customer['avatar'];
}

$rewrite_ext = array('html','htm','shtml','php','asp','jsp','cgi');
$pb_rewriteext = $rewrite_ext[$pb_rewriteext];
$pb_gdcheck = unserialize($pb_gdcheck);
$_PBDEV['CK_HASH'] = substr(md5($_SERVER['HTTP_USER_AGENT'].$_PBDEV['PB_IP'].$pb_sitehash), 0, 10);
$TPL->assign('pb_gdcheck', $pb_gdcheck);
$TPL->assign('pb_editor', $pb_editor);
$TPL->assign('siteconfig', array('pb_statistic' => $pb_statistic, 'pb_icp' => $pb_icp, 'pb_seomore' => $pb_seomore, 'pb_sitename' => $pb_sitename, 'pb_adminmail' => $pb_adminmail, 'pb_customcredit' => $pb_customcredit));
$TPL->assign('verifyhash', $_PBDEV['CK_HASH']);

if ($plugins && is_array($plugins))
{
	foreach ($plugins as $value)
	{
		@include_once(PBDIGG_ROOT.'plugins/'.$value['pmark'].'/'.$value['pmark'].'.inc.php');
	}
}

if (PB_CSCRIPT != 'show')
{
	$nav_menu = PMenu();
	$TPL->assign('nav_menu', $nav_menu[0]);
	$TPL->assign('sub_menu', $nav_menu[1]);
	$TPL->assign('showsub', (empty($nav_menu[1]) ? '0' : '1'));
}

unset($db_host, $nav_menu, $db_username, $db_password, $db_name, $db_pconnect, $_PBVERSION, $_PBRELEASE, $p_uid, $p_pw, $pb_auth, $c_data, $plugins, $customer['sid'], $customer['pw']);
$TPL->assign('customer', $customer);
sCookie('pb_lastvisit', $timestamp."\t".$_PBDEV['REQUEST_URL']);

?>
