<?php
error_reporting(0);
$configfile = "./includes/mysql.php";
$configcontents = @fread(@fopen($configfile, 'r'), @filesize($configfile));

$pos = strpos($configcontents, "INSTALLED");

if ($pos === false) { header("Location:./install/index.php");;
} else { ;
//    header("Location:./index.php");
}

session_start();

require_once '../config/settings.php';  // MAIN SITE'S INSTALLED CONFIGURATIONS .....

define('MAINSITE_URL','../');

$backUrl=MAINSITE_URL;

include("mysql.php");
$startTime = time();

define("THEME_ID", 0);
define("SBB_VERSION", 1.01);
$inforum = true;
$query = mysqli_query($connection,"SELECT * FROM `data`");

$qry= "select `value` from ".MYSQL_TABLE_PREFIX."settings  WHERE `settingfield`='siteLogo'";
$main= mysqli_query($connection,$qry) or die();

$main_logo=mysqli_fetch_array($main);

define( 'MAIN_LOGO','../public/images/default/'.$main_logo['value']);


$qry= "select `value` from ".MYSQL_TABLE_PREFIX."settings  WHERE `settingfield`='siteStyle'";
$main= mysqli_query($connection,$qry) or die();

$main_theme=mysqli_fetch_array($main);

$q = mysqli_fetch_array($query);

// page information
$pageInfo = $q;
$pageInfo['theme'] = $q['defaultTheme'];

$pageInfo['theme'] = $main_theme['value'];


define( 'MAIN_THEME',$pageInfo['theme']);

$pageInfo['sitename'] = stripslashes($pageInfo['sitename']);
$pageInfo['homelink'] = "<a href=\"". MAINSITE_URL."\">".$pageInfo['sitename']."</a>";
$pageInfo['maxPerPage'] = 10;




// user information
$userInfo = array();
if (@$_SESSION['loggedin']) {
	 $u = @$_SESSION['forum_user'];
	 $query = mysqli_query($connection,"SELECT * FROM `users` WHERE username='$u'") or die();
	 $q = mysqli_fetch_array($query);

         

	 $userInfo = $q;

         if($userInfo['title']=='Administrator')
         {  
               $pageInfo['homelink'] = "<a href=\"". MAINSITE_URL."admin/index/dashboard/\">".$pageInfo['sitename']."</a>";
               $backUrl=MAINSITE_URL."admin/index/dashboard/";
         }
         else
         {
               $pageInfo['homelink'] = "<a href=\"". MAINSITE_URL."business/index/dashboard/\">".$pageInfo['sitename']."</a>";
               $backUrl=MAINSITE_URL."business/index/dashboard/";
         }


	 $userInfo['loggedin'] = 1;
	 $q = mysqli_query($connection,"SELECT * FROM `posts` WHERE posterId='{$userInfo['id']}'");
	 $q = mysqli_num_rows($q);
	 $userInfo['posts'] = $q;

         


        

	 $userInfo['defaultAvatar'] = false;
	 $file = $userInfo['avatar'];
	 if (!preg_match("/^.*\.gif/",$file)) {
		  if (!preg_match("/^.*\.jpg/",$file)) {
			   if (!preg_match("/^.*\.png/",$file)) {
				    $userInfo['avatar'] = "templates/{$pageInfo['theme']}/images/noavatar.gif";
				    $userInfo['defaultAvatar'] = true;
			   }
		  }
	 }
}
else $userInfo['loggedin'] = 0;

$userInfo['xBackUrl']=$backUrl;

function fetchTemplate($name) {

  
 global $pageInfo;
 global $connection;
 

 if (file_exists("templates/".MAIN_THEME."/".$name.".php")) {
 	  require("templates/".MAIN_THEME."/".$name.".php");
 }
 else {
	  echo "<b>Warning:</b> Unable to find ".$name." file.<br />\n";
 }
}

function preg_chars($pat, $str) {
 $error = 0;
 for ($i=0; $i<strlen($str); $i++) {
  //if (!ereg($pat, $str[$i])) {
  if (!preg_match($pat, $str[$i])) {
   $error = 1;
   $i = strlen($str);
  }
 }
 return $error;
}

function showSmileys() {
  global $connection;
 $query = mysqli_query($connection,"SELECT * FROM `smileys`");
 while ($row = mysqli_fetch_array($query)) {
  echo "<a onClick=\"JavaScript: insertSmiley('{$row['keyVal']}')\"><img src=\"images/smileys/{$row['src']}\" alt=\"{row['keyVal']}\" title=\"{$row['keyVal']}\"></a>\n";
 }
}

function format_chars($str) {
 $str = preg_replace("/\//", "&#47", $str);
 $str = preg_replace("/\\\\/", "&#92", $str);
 $str = preg_replace("/\"/", "&#34", $str);
 $str = preg_replace("/'/", "&#39", $str);
 return $str;
}

/////added by roshith on 9-8-06

function isValidEmail($email) {
        //if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
        if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
                return false;
        }
        return true;
}

/////////
// banned IP addresses
$myip = $_SERVER['REMOTE_ADDR'];
$q = mysqli_query($connection,"SELECT * FROM `bannedips`");
while ($row = mysqli_fetch_array($q)) {
 if ($myip == $row['ip']) {
  $pageInfo['title'] = "Banned";
  $pageInfo['homelink'] .= " -> <b>BANNED</b>";
  fetchTemplate("header");
  fetchTemplate("banned");
  fetchTemplate("footer");
  die();
 }
}
?>