<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

//set variable against bad boys


$userID = '';

//login sessions

systeminc('session');

//get IP

systeminc('ip');

//installed?

if(file_exists('install/index.php')) system_error('Please remove the install-folder first.',0);

//webSPELL functions

if(!function_exists('file_get_contents')) {
 function file_get_contents($filename)
 {
  $fd = fopen("$filename", "rb");
  $content = fread($fd, filesize($filename));
  fclose($fd);
  return $content;
 }
}

if(!function_exists('str_split')){
   function str_split($str,$split_length=1){

       $cnt = strlen($str);

       for ($i=0;$i<$cnt;$i+=$split_length)
           $result[]= substr($str,$i,$split_length);   
  
       return $result;
   }
}

function gettemplate($template,$endung="html") {
    $templatefolder = "templates";
    return str_replace("\"","\\\"",implode("",file($templatefolder."/".$template.".".$endung)));
}

function sql_quote($value) {

	if( get_magic_quotes_gpc() )	{
		$value = stripslashes( $value );
	}
	if( function_exists( "mysql_real_escape_string" ) ) {
		$value = mysql_real_escape_string( $value );
	}
	else
	{
		$value = addslashes( $value );
		}
	return $value;
}

function percent($sub, $total, $dec) {
	if ($sub) {
	    $perc = $sub / $total * 100;
		$perc = round($perc, $dec);
		return $perc;
	}
	else return 0;
}

function showlock($reason, $time) {
 eval ("\$lock = \"".gettemplate("lock")."\";");
 die($lock);
}

function checkenv($systemvar,$checkfor) {
	return eregi($checkfor, ini_get($systemvar));
}

function createkey($length) {
	$key='';
	for($i=0;$i<$length;$i++) {
		switch(rand(1,3)) {
			case 1: $key.=chr(rand(48,57));break;
			case 2: $key.=chr(rand(65,90));break;
			case 3: $key.=chr(rand(97,122));break;
		}
	}
	return md5($key);
}

// -- FILESYSTEM -- //

systeminc('func/filesystem');

// -- USER INFORMATION -- //

systeminc('func/user');

// -- ACCESS INFORMATION -- //

systeminc('func/useraccess');

// -- MESSENGER INFORMATION -- //

systeminc('func/messenger');

// -- NEWS INFORMATION -- //

systeminc('func/news');

// -- GAME INFORMATION -- //

systeminc('func/game');

// -- BOARD INFORMATION -- //

systeminc('func/board');

// -- CAPTCHA -- //

systeminc('func/captcha');

// -- VARIOUS -- //

function makepagelink($link, $page, $pages) {
	$page_link = '<img src="images/icons/multipage.gif" width="10" height="12"> <span class="small">';
	if($page!=1) $page_link .= "&nbsp;<a href=\"$link&page=1\">&laquo;</a>&nbsp;<a href=\"$link&page=".($page-1)."\">‹</a>";
	if($page>=6) $page_link .= "&nbsp;<a href=\"$link&page=".($page-5)."\">...</a>";
	if($page+4>=$pages) $pagex=$pages;
	else $pagex=$page+4;
	for($i=$page-4 ; $i<=$pagex ; $i++) { 	
		if($i<=0) $i=1;
		if($i==$page) $page_link .= "&nbsp;<b><u>$i</u></b>";
		else $page_link .= "&nbsp;<a href=\"$link&page=$i\">$i</a>";
	}
	if(($pages-$page)>=5) $page_link .= "&nbsp;<a href=\"$link&page=".($page+5)."\">...</a>";
	if($page!=$pages) $page_link .= "&nbsp;<a href=\"$link&page=".($page+1)."\">›</a>&nbsp;<a href=\"$link&page=".$pages."\">&raquo;</a>";
	$page_link .= "</span>";

	return $page_link;
}

function str_break($str, $maxlen) { 
  $nobr = 0; 
  $len = strlen($str); 
  for ($i = 0; $i<$len; $i++) { 
    // Überprüfen ob ein das Zeichen ein möglicher Zeilenubruch ist 
    if (($str[$i]!=' ') && ($str[$i]!='-') && ($str[$i]!="\n")) $nobr++; 
    else { 
      $nobr = 0; // Zeichen ist Trennzeichen - Zähler zurücksetzen 
      if($maxlen+$i>$len) { 
        // Ist der Rest kürzer als $maxlen, muss er nicht mehr überprüft werden 
        $str_br .= substr($str, $i); 
        break; 
      } 
    } 
    // Wenn die Zeichenkette ohne Trennzeichen länger als zulässig ist 
    // wird ein Trennzeichen eingefügt und der Zähler zurückgesetzt 
    if ($nobr>$maxlen) { 
      $str_br .= '-'.$str[$i]; 
      $nobr = 1; 
    } else $str_br .= $str[$i]; 
  } 
  return $str_br; 
}

// -- BB CODE -- //

systeminc('func/bbcode');

function cleartext($text,$bbcode=true) {
	$text=getinput($text);
	$text=strip_tags($text);
	$text=smileys($text,1);
	$text=insertlinks($text);
	$text=flags($text);
	$text=replacement($text, $bbcode);
	$text=nl2br($text);
	
	return $text;
}

function htmloutput($text) {
	$text=smileys($text);
	$text=insertlinks($text);
	$text=flags($text);
	$text=replacement($text);
	$text=stripslashes($text);
	$text=nl2br($text);
		
	return $text;
}

function clearfromtags($text) {
	$text=getinput($text);
  $text=strip_tags($text);
	$text=nl2br($text);
	
	return $text;
}

function getinput($text) {
	$text=stripslashes($text);
	$text=htmlspecialchars($text);

	return $text;
}

function isonline($userID) {
    $ergebnis=safe_query("SELECT site FROM ".PREFIX."whoisonline WHERE userID='$userID'");
	$anz=mysql_num_rows($ergebnis);
	if($anz) {
	    $ds=mysql_fetch_array($ergebnis);
		return '<b>online</b> @ <a href="index.php?site='.$ds[site].'">'.$ds[site].'</a>';
	}	
	else return 'offline';
}

// -- GALLERY -- //

systeminc('func/gallery');

// LOGIN

$login_per_cookie = false;
if(isset($_COOKIE['ws_auth']) AND !isset($_SESSION['ws_auth'])) {
 $login_per_cookie = true;
 $_SESSION['ws_auth'] = $_COOKIE['ws_auth'];
}

systeminc('login');

if($login_per_cookie) {
 $ll=mysql_fetch_array(safe_query("SELECT lastlogin FROM ".PREFIX."user WHERE userID='$userID'"));
 $_SESSION['ws_lastlogin'] = $ll[lastlogin];
}

// BANNED

$banned=safe_query("SELECT userID, banned FROM ".PREFIX."user WHERE userID='$userID' OR ip='$ip'");
while($db=mysql_fetch_array($banned)) {
	if($db[banned]) system_error('You have been banished.',0);
}

// WHO IS - WAS ONLINE
$timeout=5; // 1 sekunde
$deltime = time()-($timeout*60); // IS 1m
$wasdeltime = time()-(60*60*24); // WAS 24h

safe_query("UPDATE ".PREFIX."user SET ip='$ip' WHERE userID='$userID'");
safe_query("DELETE FROM ".PREFIX."whoisonline WHERE time < '$deltime'");  // IS ONLINE
safe_query("DELETE FROM ".PREFIX."whowasonline WHERE time < '$wasdeltime'");  // WAS ONLINE

// SITE VARIABLE

$site = $_GET['site'];
if($closed AND !isanyadmin($userID)) {
	$dl=mysql_fetch_array(safe_query("SELECT * FROM `".PREFIX."lock` LIMIT 0,1"));
 	$reason = $dl[reason];
 	$time = $dl[time];
  showlock($reason, $time);
}

//HELP MODE

systeminc('help');

//WHOISONLINE

if($userID!='') {
    // IS ONLINE
	if(mysql_num_rows(safe_query("SELECT userID FROM ".PREFIX."whoisonline WHERE userID='$userID'"))) {
	    safe_query("UPDATE ".PREFIX."whoisonline SET time='".time()."', site='$site' WHERE userID='$userID'");
		safe_query("UPDATE ".PREFIX."user SET lastlogin='".time()."' WHERE userID='$userID'");
	}	
    else safe_query("INSERT INTO ".PREFIX."whoisonline (time, userID, nickname, site) VALUES ('".time()."', '$userID', '".mysql_escape_string(getnickname($userID))."', '$site')");
	
	// WAS ONLINE
	if(mysql_num_rows(safe_query("SELECT userID FROM ".PREFIX."whowasonline WHERE userID='$userID'")))  
		safe_query("UPDATE ".PREFIX."whowasonline SET time='".time()."', site='$site' WHERE userID='$userID'");
    else safe_query("INSERT INTO ".PREFIX."whowasonline (time, userID, nickname, site) VALUES ('".time()."', '$userID', '".mysql_escape_string(getnickname($userID))."', '$site')");
}
else {
    $anz = mysql_num_rows(safe_query("SELECT ip FROM ".PREFIX."whoisonline WHERE ip='$ip'"));
    if($anz) safe_query("UPDATE ".PREFIX."whoisonline SET time='".time()."', site='$site' WHERE ip='$ip'");
    else safe_query("INSERT INTO ".PREFIX."whoisonline (time, ip, site) VALUES ('".time()."','$ip', '$site')");
}


// COUNTER
$time = time();
$date = date("d.m.Y", $time);
$deltime = $time-(3600*24);
safe_query("DELETE FROM ".PREFIX."counter_iplist WHERE del<".$deltime);

$anz=mysql_num_rows(safe_query("SELECT ip FROM ".PREFIX."counter_iplist WHERE ip='".$ip."'"));
if(!$anz) {
  safe_query("UPDATE ".PREFIX."counter SET hits=hits+1");
	safe_query("INSERT INTO ".PREFIX."counter_iplist (dates, del, ip) VALUES ('".$date."', '".$time."', '".$ip."')");
  $anz2=mysql_num_rows(safe_query("SELECT dates FROM ".PREFIX."counter_stats WHERE dates='".$date."'"));
  if(!$anz2) safe_query("INSERT INTO `".PREFIX."counter_stats` (`dates`, `count`) VALUES ('".$date."', '1')");
  else safe_query("UPDATE ".PREFIX."counter_stats SET count=count+1 WHERE dates='".$date."'");
}

$cur=mysql_fetch_array(safe_query("SELECT count(*) FROM ".PREFIX."whoisonline"));
$max=mysql_fetch_array(safe_query("SELECT maxonline FROM ".PREFIX."counter LIMIT 0,1"));
if($cur[0] > $max['maxonline']) safe_query("UPDATE ".PREFIX."counter SET maxonline='".$cur[0]."'");

//Country list

unset($countries);
$ergebnis = safe_query("SELECT * FROM `".PREFIX."countries` ORDER BY country");
while($ds = mysql_fetch_array($ergebnis)) {
  $countries .= '<option value="'.$ds[short].'">'.$ds[country].'</option>';
}

?>