<?php
/**
 *   File functions:
 *   Main site of game
 *
 *   @name                 : index.php                            
 *   @copyright            : (C) 2004,2005,2006 Vallheru Team based on Gamers-Fusion ver 2.5
 *   @author               : thindil <thindil@users.sourceforge.net>
 *   @version              : 1.0.1
 *   @since                : 15.02.2006
 *
 */
 
//
//
//       This program is free software; you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation; either version 2 of the License, or
//   (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; if not, write to the Free Software
//   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// $Id$

require_once ('includes/config.php');
if (!$gamename) 
{
    $host = $_SERVER['HTTP_HOST'];
    $path = str_replace("index.php","",$_SERVER['PHP_SELF']);
    $address = "http://".$host.$path."install/install.php";
    $meta = "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=".$address."\">";
    print "<html><head>".$meta."</head><body></body></html>";
    exit;
} 
    else 
{

    require_once ('libs/Smarty.class.php');

    $smarty = new Smarty;
    $smarty->compile_check = true;

    /**
    * Check avaible languages
    */    
    $path = 'languages/';
    $dir = opendir($path);
    $arrLanguage = array();
    $i = 0;
    while ($file = readdir($dir))
    {
        if (!ereg(".htm*$", $file))
        {
            if (!ereg("\.$", $file))
            {
                $arrLanguage[$i] = $file;
                $i = $i + 1;
            }
        }
    }
    closedir($dir);

    /**
    * Get the localization for game
    */
    $strLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    foreach ($arrLanguage as $strTrans)
    {
        $strSearch = "^".$strTrans;
        if (eregi($strSearch, $strLanguage))
        {
            $strTranslation = $strTrans;
            break;
        }
    }
    if (!isset($strTranslation))
    {
        $strTranslation = 'pl';
    }
    if (isset($_GET['lang']))
    {
        if (in_array($_GET['lang'], $arrLanguage))
        {
            $strTranslation = $_GET['lang'];
        }
    }
            
    require_once("languages/".$strTranslation."/index.php");

    $smarty -> assign(array("Gamename" => $gamename, 
        "Meta" => '',
        "Welcome" => WELCOME,
        "Register" => REGISTER,
        "Rules" => RULES,
        "Forums" => FORUMS,
        "Irc" => IRC));

    $objOpengame = $db -> Execute("SELECT value FROM settings WHERE setting='open'");
    /**
    * When game is close
    */
    if ($objOpengame -> fields['value'] == 'N') 
    {
        $objReason = $db -> Execute("SELECT value FROM settings WHERE setting='close_reason'");
        $smarty -> assign ("Error", REASON.":<br />".$objReason -> fields['value']);
        $objReason -> Close();
        $smarty -> display ('error.tpl');
        exit;
    }
    $objOpengame -> Close();

    $time = date("H:i:s");
    $hour = explode(":", $time);
    $newhour = $hour[0] + 0;
    if ($newhour > 23) 
    {
        $newhour = $newhour - 24;
    }
    $arrtime = array($newhour, $hour[1], $hour[2]);
    $newtime = implode(":",$arrtime);

    $query = $db -> Execute("SELECT count(*) FROM `players`");
    $nump = $query -> fields['count(*)'];
    $query -> Close();
    
    $span = (time() - 180);
    $objQuery = $db -> Execute("SELECT count(*) FROM `players` WHERE `lpv`>=".$span);
    $intNumo = $objQuery -> fields['count(*)'];
    $objQuery -> Close();

    $objMetakey = $db -> Execute("SELECT `value` FROM `settings` WHERE `setting`='metakeywords'");
    $objMetadesc = $db -> Execute("SELECT `value` FROM `settings` WHERE `setting`='metadescr'");

    $smarty->assign(array("Time" => $newtime, 
        "Players" => $nump, 
        "Online" => $intNumo, 
        "Email" => EMAIL,
        "Password" => PASSWORD,
        "Login" => LOGIN,
        "Lostpasswd" => LOST_PASSWORD,
        "Ctime" => CURRENT_TIME,
        "Whave" => WE_HAVE,
        "Registered" => REGISTERED,
        "Metakeywords" => $objMetakey -> fields['value'],
        "Metadescription" => $objMetadesc -> fields['value'],
        "Ingame" => IN_GAME));
    $objMetakey -> Close();
    $objMetadesc -> Close();

    /**
    * Main Page
    */
    if (!isset ($_GET['step'])) 
    {
    
        $update = $db -> SelectLimit("SELECT * FROM updates WHERE lang='".$strTranslation."' ORDER BY id DESC", 3);
        $arrnews = array();
        $i = 0;
        while (!$update -> EOF) 
        {
            if (isset($update -> fields['time']))
            {
                $time = " ".DAY." <b>".$update -> fields['time']."</b>";
            }
                else
            {
                $time = '';
            }
            $arrnews[$i] = "<center><b>".$update -> fields['title']."</b> ".WRITE_BY." <b>".$update -> fields['starter']."</b>".$time."...</center>\"".$update -> fields['updates']."\".<br /><br />";
            $i = $i + 1;
            $update -> MoveNext();        
        }
        $update -> Close();

        $adminmail1 = str_replace("@","[at]",$adminmail);

        $objCodexdate = $db -> Execute("SELECT date FROM court WHERE title='".CODEX." ".$gamename."'");
    
        $smarty->assign( array ("Update" => $arrnews, 
            "Adminname" => $adminname, 
            "Adminmail" => $adminmail, 
            "Adminmail1" => $adminmail1,
            "Whatis" => WHAT_IS,
            "Description" => DESCRIPTION,
            "Codex" => CODEX,
            "Codex2" => CODEX2,
            "News" => NEWS,
            "Charset" => CHARSET,
            "Codexdate" => $objCodexdate -> fields['date']));
        $smarty->display('index.tpl');
        $objCodexdate -> Close();
    }

    /**
    * Game rules
    */
    if (isset ($_GET['step']) && $_GET['step'] == 'rules') 
    {
        $objRules = $db -> Execute("SELECT body FROM court WHERE title='".CODEX." ".$gamename."'");
        $smarty -> assign(array("Title" => PAGE_NAME,
            "Rules2" => $objRules -> fields['body'],
			"Charset" => CHARSET));
        $smarty -> display('rules.tpl');
        $objRules -> Close();
    }

    /**
    * Password reminder
    */
    if (isset($_GET['step']) && $_GET['step'] == 'lostpasswd') 
    {
        if (isset ($_GET['action']) && $_GET['action'] == 'haslo') 
        {
            if (!$_POST['email']) 
            {
                $smarty -> assign ("Error", ERROR_MAIL);
                $smarty -> display ('error.tpl');
                exit;
            }
            $_POST['email'] =  strip_tags($_POST['email']);
            $query = $db -> Execute("SELECT id FROM players WHERE email='".$_POST['email']."'");
            $dupe1 = $query -> RecordCount();
            $query -> Close();
        
            if ($dupe1 == 0) 
            {
                $smarty -> assign ("Error", ERROR_NOEMAIL);
                $smarty -> display ('error.tpl');
                exit;
            }
            $new_pass = substr(md5(uniqid(rand(), true)), 3, 9);
            $adress = $_POST['email'];
            $message = MESSAGE_PART1." ".$gamename.".".MESSAGE_PART2." \n".$new_pass."\n ".MESSAGE_PART3." ".$gamename."\n".$adminname;
            $subject = MESSAGE_SUBJECT." ".$gamename;
            require_once('mailer/mailerconfig.php');
            if (!$mail -> Send()) 
            {
                $smarty -> assign ("Error", MESSAGE_NOT_SEND." ".$mail -> ErrorInfo);
                $smarty -> display ('error.tpl');
                exit;
            }
            $db -> Execute("UPDATE players SET pass = MD5('".$new_pass."') WHERE email='".$_POST['email']."'") or $db -> ErrorMsg();
        }
        /**
        * Initializantion of variable
        */
        if (!isset($_GET['action'])) 
        {
            $_GET['action'] = '';
        }
        $smarty -> assign(array("Action" => $_GET['action'],
            "Lostpassword2" => LOST_PASSWORD2,
            "Success" => SUCCESS,
            "Email" => EMAIL,
            "Send" => SEND,
			"Charset" => CHARSET));
        $smarty -> display ('passwd.tpl');
    }

    $db -> Close();
}
?>
