<?php
/********************************************************/
/* NSN Your Account                                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright  2000-2004 by NukeScripts Network         */
/* ==================================================== */
/* Based from:                                          */
/* PHP-NUKE: Web Portal System                          */
/********************************************************/
defined('MAXIMUS') or Header("Location: index.php");
define('IN_YA', true);

function ya_banuser($userid,$username,$ip) {
    if(defined('ADMIN_ALLOW')) {
    $t = time();
    $yaba = $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNST_BLOCKED_IPS."` VALUES ('$ip','$userid','$username','Ban Admin','$t','Ban Admin','0','Your Account','','','$ip','','','','')");
    $GLOBALS['db']->sql_freeresult($yaba);
    redirect_url("modules.php?name=Your_Account&op=userinfo&username=$username");
	}
}
/*
function ya_get_configs(){
    global $prefix, $GLOBALS['db'];
    $configresult = $GLOBALS['db']->sql_query("SELECT config_name, config_value FROM ".TABLE_NSNYA_CONFIG."");
    while (list($config_name, $config_value) = $GLOBALS['db']->sql_fetchrow($configresult)) {
        $config[$config_name] = $config_value;
    }
    return $config;
}
*/
function ya_get_field($id){
    $result = $GLOBALS['db']->sql_query("SELECT `titre` FROM `".TABLE_NSNYA_FIELDS."` WHERE `active`='1' AND `id`='$id' ");
    $raw    = $GLOBALS['db']->sql_fetchrow($result);
    $titre  = stripslashes($raw['titre']);
    $GLOBALS['db']->sql_freeresult($result);
    return $titre;
}

function ya_userisadmin($username) {
    if(defined('USER_ALLOW')) {
    $result =  $GLOBALS['db']->sql_query("SELECT `aid` FROM `".TABLE_AUTHORS."` WHERE `aid`='$username'");
    $row    = $GLOBALS['db']->sql_fetchrow($result);
    $GLOBALS['db']->sql_freeresult($result);
	if($row) {
	    return 1;
	} else {
      return 0;
	}
    }
}


function ya_userCheck($username) {
    global $stop, $ya_config;
    // Need to find a way to include extended caracters
    //if ((!$username) || ($username=="") || (ereg("[^�a-zA-Z0-9_-]",$username))) $stop = "<center>"._ERRORINVNICK."</center><br />";
    if ((!$username) || ($username=="") || (ereg("[^a-zA-Z0-9_-]",$username))) $stop = "<center>"._ERRORINVNICK."</center><br />";
    if (strlen($username) > $ya_config['nick_max']) $stop = "<center>"._YA_NICKLENGTH."</center>";
    if (strlen($username) < $ya_config['nick_min']) $stop = "<center>"._YA_NICKLENGTH."</center>";
    if ($ya_config['bad_nick'] > "") {
        $BadNickList = explode("\r\n",$ya_config['bad_nick']);
        for ($i=0; $i < count($BadNickList); $i++) {
            if (eregi($BadNickList[$i], $username)) $stop = "<center>"._NAMERESTRICTED."</center><br />";
        }
    }
    if (strrpos($username,' ') > 0) $stop = "<center>"._NICKNOSPACES."</center>";
    if ($GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `username` FROM `".TABLE_USERS."` WHERE `username`='$username'")) > 0) $stop = "<center>"._NICKTAKEN."</center><br />";
    if ($GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `username` FROM `".TABLE_USERS_TEMP."` WHERE `username`='$username'")) > 0) $stop = "<center>"._NICKTAKEN."</center><br />";
    return($stop);
}

function ya_mailCheck($user_email) {
    global $stop, $ya_config;
    $user_email = strtolower($user_email);
    if ((!$user_email) || ($user_email=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$user_email))) $stop = "<center>"._ERRORINVIP."</center><br />";
    if ($ya_config['bad_mail'] > "") {
        $BadMailList = explode("\r\n",$ya_config['bad_mail']);
        for ($i=0; $i < count($BadMailList); $i++) {
            if (eregi($BadMailList[$i], $user_email)) $stop = "<center>"._IPBLOCKED." <b>".$BadMailList[$i]."</b></center><br />";
        }
    }
    if (strrpos($user_email,' ') > 0) $stop = "<center>"._ERROREMAILSPACES."</center><br />";
    if ($GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `user_email` FROM `".TABLE_USERS."` WHERE `user_email`='$user_email'")) > 0) $stop = "<center>"._EMAILREGISTERED."</center><br />";
    if ($GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `user_email` FROM `".TABLE_USERS."` WHERE `user_email`='".md5($user_email)."'")) > 0) $stop = "<center>"._EMAILNOTUSABLE."</center><br />";
    if ($GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT `user_email` FROM `".TABLE_USERS_TEMP."` WHERE `user_email`='$user_email'")) > 0) $stop = "<center>"._EMAILREGISTERED."</center><br />";
    return($stop);
}

function ya_IPCheck($ip) {
    global $ya_config;
    if ($ip == "") { echo "<center>"._ERRORINVIP."</center><br />";incof();die(); }
    if (($ya_config['bad_ip'] > "") && ($ya_config['bad_ip'] != "")){
        $BadIPList = explode("\r\n",$ya_config['bad_ip']);
        for ($i=0; $i < count($BadIPList); $i++) {
            if (eregi($BadIPList[$i], $ip)){ echo "<center>"._IPBLOCKED." <b>".$BadIPList[$i]."</b></center><br />";incof();die();}
        }
    }
}

function ya_passCheck($user_pass1, $user_pass2) {
    global $stop, $ya_config;
    if (strlen($user_pass1) > $ya_config['pass_max']) $stop = "<center>"._YA_PASSLENGTH."</center><br />";
    if (strlen($user_pass1) < $ya_config['pass_min']) $stop = "<center>"._YA_PASSLENGTH."</center><br />";
    if ($user_pass1 != $user_pass2) $stop = "<center>"._PASSWDNOMATCH."</center><br />";
    return($stop);
}

function ya_fixtext($ya_fixtext) {
    if ($ya_fixtext == "") { return $ya_fixtext; }
    $ya_fixtext = stripslashes($ya_fixtext);
    //$ya_fixtext = ereg_replace("\'","&acute;",$ya_fixtext);
    $ya_fixtext = ereg_replace("\'","",$ya_fixtext);
    $ya_fixtext = ereg_replace("\"","&quot;",$ya_fixtext);
    $ya_fixtext = strip_tags($ya_fixtext);
    if (!get_magic_quotes_gpc()) { $ya_fixtext = addslashes($ya_fixtext); }
    return $ya_fixtext;
}

function ya_save_config($config_name, $config_value){
    $GLOBALS['db']->sql_query("UPDATE `".TABLE_NSNYA_CONFIG."` SET `config_value`='$config_value' WHERE `config_name`='$config_name'");
}

function ya_get_config($config_name){
    $sql = "SELECT `config_value` FROM `".TABLE_NSNYA_CONFIG."` WHERE `config_name`='$config_name'";
    if ( !($configresult = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain _nsnya_config', '', __LINE__, __FILE__, $sql);}
    $raw = $GLOBALS['db']->sql_fetchrow($configresult);
    $config_value = $raw['config_value'];
    $GLOBALS['db']->sql_freeresult($configresult);
    return $config_value;
}

function ya_get_configs(){
    $sql = "SELECT `config_name`, `config_value` FROM `".TABLE_NSNYA_CONFIG."`";
    if ( !($configresult = $GLOBALS['db']->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain _nsnya_config', '', __LINE__, __FILE__, $sql);}
    while ($raw = $GLOBALS['db']->sql_fetchrow($configresult)) {
    $config_name  = stripslashes($raw['config_name']);
    $config_value = stripslashes($raw['config_value']);
        $config[$config_name] = $config_value;
    }
    $GLOBALS['db']->sql_freeresult($configresult);
    return $config;
}

function yacookie($setuid, $setusername, $setpass, $setstorynum, $setumode, $setuorder, $setthold, $setnoscore, $setublockon, $settheme, $setcommentmax) {
global $ya_config;
    $info = base64_encode("$setuid:$setusername:$setpass:$setstorynum:$setumode:$setuorder:$setthold:$setnoscore:$setublockon:$settheme:$setcommentmax");
   if ($ya_config['temp_cookie']==0) { @setcookie("user","$info",0); }
   else { $tempo = $ya_config['temp_cookie'];
   @setcookie("user","$info",time()+$tempo); }
}

function YA_CoolSize($size) {
    $mb = 1024*1024;
    if ( $size > $mb ) {
        $mysize = sprintf ("%01.2f",$size/$mb) . " MB";
    } elseif ( $size >= 1024 ) {
        $mysize = sprintf ("%01.2f",$size/1024) . " Kb";
    } else {
        $mysize = $size . " bytes";
    }
    return $mysize;
}

function YA_MakePass() {
    $makepass = "";
    $strs = "abc2def3ghj4kmn5opq6rst7uvw8xyz9";
    for ($x=0; $x < 20; $x++) {
        mt_srand ((double) microtime() * 1000000);
        $str[$x] = substr($strs, mt_rand(0, strlen($strs)-1), 1);
        $makepass = $makepass.$str[$x];
    }
    return($makepass);
}

function mmain($user) {
    global $stop, $module_name, $redirect, $mode, $t, $f, $ya_config, $sitename;
    if(!defined('USER_ALLOW')) {
        incoh();
        if ($stop) {
            title(_LOGININCOR);
        }
	$ip = getenv("REMOTE_ADDR");
        ya_IPCheck($ip);
        mt_srand ((double)microtime()*1000000);
        $maxran = 1000000;
        $random_num = mt_rand(0, $maxran);
        br('2');
        echo "<table border='0' align='center' width='500'><tr>"
            ."<td width='50%' valign='top'><center><img src='images/login.gif' alt=\""._USERLOGIN."\" title=\""._USERLOGIN."\" /></center>"
            ."<b>"._WELCOMETOMAX." $sitename<br /><br />"
            .""._WELCOMETOMAX2."";
	br('2');
	echo "<form action='modules.php?name=Your_Account' method='post'>\n"
 	    ."<input type='hidden' name='op' value='new_user' />\n"
 	    ."<input type='submit' value='"._REGNEWUSER."' onclick=\"this.form.submit();this.disabled=true;this.value='"._WAITING."'\" />"
 	    ."</form>";
	br('1');
	echo "<form action='modules.php?name=Your_Account' method='post'>\n"
 	    ."<input type='hidden' name='op' value='pass_lost' />\n"
 	    ."<input type='submit' value='"._PASSWORDLOST."' onclick=\"this.form.submit();this.disabled=true;this.value='"._WAITING."'\" />"
 	    ."</form></b>"
	    ."</td><td width='50%' valign='top'>"
            ."<img src='modules/Your_Account/images/login.gif' alt='"._USERLOGIN."' title='"._USERLOGIN."' />";
	Max_OpenTable2();
        echo "<form action='modules.php?name=Your_Account' method='post'>\n"
 	    ."<table border='0' width='200' align='center'><tr><td width='50%' valign='top'>"
	    ."<tr><td><b>"._NICKNAME."</b><br /><input type='text' name='username' size='30' maxlength='25' /></td></tr>\n"
            ."<tr><td><b>"._PASSWORD.":</b><br /><input type='password' name='user_password' size='30' maxlength='20' /></td></tr>\n";
        if (extension_loaded("gd") AND ($ya_config['usegfxcheck'] == 2 OR $ya_config['usegfxcheck'] == 3)) {
            echo "<tr><td><b>"._SECURITYCODE.":</b><br /><img src='./?gfx=gfx&amp;random_num=$random_num' border='0' alt='"._SECURITYCODE."' title='"._SECURITYCODE."' /></td></tr>\n";
            echo "<tr><td><b>"._TYPESECCODE.":</b><br /><input type='text' name='gfx_check' size='10' maxlength='8' /></td></tr>\n";
            echo "<input type='hidden' name='random_num' value='$random_num' />\n";
        }
        echo "<input type='hidden' name='redirect' value='$redirect' />\n"
            ."<input type='hidden' name='mode' value='$mode' />\n"
            ."<input type='hidden' name='f' value='$f' />\n"
            ."<input type='hidden' name='t' value='$t' />\n"
            ."<input type='hidden' name='op' value='login' />\n"
            ."<tr><td colspan='2' align='center'><br /><b><input type='submit' value=\""._LOGIN."\" onclick=\"this.form.submit();this.disabled=true;this.value='"._WAITING."'\" /></b>";
        if ($ya_config['useactivate'] == 0) { echo "<br /><br /><font size='1'>( "._BESUREACT." )</font>\n"; }
        echo "</form></td></tr></center></table>";
        Max_CloseTable2();
        echo "</td></tr></table>";
	br('4');
        incof();
    } elseif (defined('USER_ALLOW')) {
        global $cookie;
        cookiedecode($user);
        redirect_url("modules.php?name=Your_Account&op=userinfo&username=$cookie[1]");
    }
}

function disabled() {
    incoh();
    Max_OpenTable();
    echo "<font class='option'>"._ACTDISABLED."</font>";
    Max_CloseTable();
    incof();
}

function notuser() {
    incoh();
    Max_OpenTable();
    echo "<font class='option'>"._MUSTBEUSER."</font>";
    Max_CloseTable();
    incof();
}

function french_date($endate) {
$user_regdate = str_replace(",", " ", $endate);
$regdate      = explode(" ", $user_regdate);
$regdate_replaced = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$regdate_replace = array(""._JANUARY."",""._FEBRUARY."", ""._MARCH."", ""._APRIL."", ""._MAY."", ""._JUNE."", ""._JULY."", ""._AUGUST."", ""._SEPTEMBER."", ""._OCTOBER."", ""._NOVEMBER."", ""._DECEMBER."");
$regdate['month'] = str_replace($regdate_replaced, $regdate_replace, $regdate[0]);
$frenchdate = $regdate['1']." ".$regdate['month']." ".$regdate['3'];
return $frenchdate;
}