<?php
ob_start('ob_gzhandler');

session_start();


// Title text
$title = "Chain Online Password Changer";

// Copyright text
$copy = "Copyright © Chain Online 2014-".date("Y")."<br>";

// Change XXXXXX to your computer name
$connection_string = "DRIVER={SQL Server};SERVER=CYBER951-PC\SQLEXPRESS;DATABASE=OdinAccounts";

// Change to your SQLEXPRESS username and password
$user = "sa";
$pass = "Test12345";

// DONT EDIT THIS

$func=new func;

class func
{
    function is_valid_string($string) {

        $cfgBadChars='`~!@#$%^&*()+-_=[]{};\'\\:"|,/<>? ';

        if (empty($string))
            return true;

        for ($i = 0; $i < strlen($cfgBadChars); $i++):
            if (strstr($string, $cfgBadChars[$i]))
            return true;
        endfor;
        
        return false;
    }

    // The function filter is the value $ojb what you will check
    function char_filter($ojb)
    {
        $badchars='Ý`~!@$%^()+-_=[]{}\'\\:"|,/<>? '; // The characters not allowed are listed here
        for ($i=0;$i<strlen($badchars);$i++){
            $ojb=str_replace($badchars[$i],"",$ojb);

        }

        return $ojb;
    }
}

echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>".$title."</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>";
?>
<style type="text/css">
a:link {
    font-family: Tahoma;
    font-size: 10px;
    color: #7f7f7f;
    text-decoration: none;
}

a:visited {
    font-family: Tahoma;
    font-size: 10px;
    color: #666666;
    text-decoration: none;
}


a:active {
    font-family: Tahoma;
    font-size: 10px;
    color: #7f7f7f;
    text-decoration: none;
}

a:hover {
    font-family: Tahoma;
    font-size: 10px;
    color: #2d88b6;
    text-decoration: underline;
}

#whitelink a:link {
    font-family: Tahoma;
    font-size: 10px;
    color: white;
    text-decoration: none;
}

#whitelink a:visited {
    font-family: Tahoma;
    font-size: 10px;
    color: white;
    text-decoration: none;
}


#whitelink a:active {
    font-family: Tahoma;
    font-size: 10px;
    color: white;
    text-decoration: none;
}

#whitelink a:hover {
    font-family: Tahoma;
    font-size: 10px;
    color: white;
    text-decoration: underline;
}

#whitelink_bot a:link {
    font-family: Tahoma;
    font-size: 9px;
    color: white;
    text-decoration: none;
}

#whitelink_bot a:visited {
    font-family: Tahoma;
    font-size: 9px;
    color: white;
    text-decoration: none;
}


#whitelink_bot a:active {
    font-family: Tahoma;
    font-size: 9px;
    color: white;
    text-decoration: none;
}

#whitelink_bot a:hover {
    font-family: Tahoma;
    font-size: 9px;
    color: white;
    text-decoration: underline;
}

#bluelink a:link {
    font-family: Tahoma;
    font-size: 10px;
    color: #2d88b6;
    text-decoration: none;
}

#bluelink a:visited {
    font-family: Tahoma;
    font-size: 10px;
    color: #2d88b6;
    text-decoration: none;
}


#bluelink a:active {
    font-family: Tahoma;
    font-size: 10px;
    color: #2d88b6;
    text-decoration: none;
}

#bluelink a:hover {
    font-family: Tahoma;
    font-size: 10px;
    color: #2d88b6;
    text-decoration: underline;
}

table {
    font-family: Tahoma;
    font-size: 10px;
}


body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
    font-family: Tahoma;
    font-size: 10px;
    color: #7f7f7f;
    background-color: transparent;
    scrollbar-arrow-color: white;
    scrollbar-3dlight-color: #e5e5e5;
    scrollbar-highlight-color: #2d88b6;
    scrollbar-face-color: #2d88b6;
    scrollbar-shadow-color: #2d88b6;
    scrollbar-darkshadow-color: #e5e5e5;
    scrollbar-track-color: #e5e5e5;
}

.padding_table {
    padding-left: 5px;
    padding-right: 5px;
}

.padding_all {
    padding-left:5px;
    padding-right:5px;
    padding-top:5px;
    padding-bottom:5px
}

.padding_left_sub {
    padding-left: 15px;
    padding-right: 15px;
}

input {
    font-family: Tahoma;
    font-size: 10px;
    color: #7f7f7f;
    background-color: white;
    border-top: 1px solid #cecece;
    border-bottom: 1px solid #cecece;
    border-left: 1px solid #cecece;
    border-right: 1px solid #cecece;
}

.button {
    font-family: Tahoma;
    font-size: 10px;
    color: #FFFFFF;
    background-color: #0000C0;
    border-top: 1px solid #2d88b6;
    border-bottom: 1px solid #2d88b6;
    border-left: 1px solid #2d88b6;
    border-right: 1px solid #2d88b6;
}

.alert_color {color: #2d88b6}

select {
    font-family: Tahoma;
    font-size: 10px;
    color: #7f7f7f;
    background-color: #FFFFFF;
}
</style>
						      <script type="text/javascript">
function click (e) {
  if (!e)
    e = window.event;
  if ((e.type && e.type == "contextmenu") || (e.button && e.button == 2) || (e.which && e.which == 3)) {
    if (window.opera)
      window.alert("Sorry: Diese Funktion ist deaktiviert.");
    return false;
  }
}
if (document.layers)
  document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = click;
document.oncontextmenu = click;
</script>
<?php
echo "<script language='JavaScript' type='text/JavaScript'>
<!-- 
function disabledBttn(formname)
{
    if (document.all || document.getElementById) {
        for (i=0;i<formname.length;i++) {
            var bttn=formname.elements[i];
            if(bttn.type.toLowerCase()=='submit' || bttn.type.toLowerCase()=='reset' || bttn.type.toLowerCase()=='button')
                bttn.disabled=true;
        }
    }
}
//-->
</script>
</head>

<body leftmargin='0' topmargin='10' marginwidth='0' marginheight='0'>
<table width='0%' height='0%' border='0' align='center' cellpadding='0' cellspacing='0'>
  <tr>
    <td>


      <br>

      <table width='400' border='0' align='center' cellpadding='0' cellspacing='0'>
        <tr>
          <td align='center' bgcolor='transparent' style='border-bottom: solid 0px #cecece; border-top: solid 0px #cecece; border-left: solid 0px #cecece; border-right: solid 0px #cecece' class='padding_all'>";
		  
    if($_POST[action]!="signup")
    {

        echo "<form method='post' onSubmit='disabledBttn(this)' action=".$_SERVER[PHP_SELF]."?id=reg".$_SERVER[QUERY_STRING].">
            <table width='100%' border='0' align='center' cellpadding='2' cellspacing='2'>
              <tr>
                <td width='30%'><font color=\"#0000C0\">Username</font color></td><td><input type='text' name='username' size='20' maxlength='20'></td>
              </tr>
              <tr>
                <td><font color=\"#0000C0\">old Password</font color></td><td><input type='password' name='oldpassword' size='20' maxlength='25'></td>
              </tr>
			  <tr>
                <td><font color=\"#0000C0\">new Password</font color></td><td><input type='password' name='newpassword1' size='20' maxlength='25'></td>
              </tr>
			  <tr>
                <td><font color=\"#0000C0\">new Password rep.</font color></td><td><input type='password' name='newpassword2' size='20' maxlength='25'></td>
              </tr>
              <tr>
                <td colspan='2' align='right'><input type='submit' value='update password' class='button'> <input type='reset' value='clear' class='button'></td>
              </tr>
            </table>
            <input type='hidden' name='action' value='signup'>
            </form>";

    }
    else
    {
        $required=array(
            "Username"=>$_POST[username],
            "old Password"=>$_POST[oldpassword],
			"new Password"=>$_POST[newpassword1],
			"new Password2"=>$_POST[newpassword2],
        );



        for($i=0;$i<count($required);$i++)
        {
            list($key,$value)=each($required);

            if(!$value)
                echo "<font color=\"#0000C0\"><b>$key</b> is required<br>";
            else
                $chkArr[]=true;
        }

        if(count($chkArr)==count($required))
        {
            $connection = odbc_connect( $connection_string, $user, $pass );

            if(!$func->is_valid_string($_POST[username]) && !$func->is_valid_string($_POST[oldpassword]) && !$func->is_valid_string($_POST[newpassword1]) && !$func->is_valid_string($_POST[newpassword2]))
            {

                $usernameP=$_POST[username];
				$newpassword=$_POST[newpassword1];
                $query = "SELECT * FROM [OdinAccounts].[dbo].[tAccounts] WHERE [sUsername]='$usernameP'";
                $q = odbc_exec($connection, $query);

                $qt = odbc_do($connection, $query);
                $i = 0;
                while(odbc_fetch_row($qt)) $i++;

                if($i<=0 or $_POST[newpassword1] != $_POST[newpassword2])
                    echo"<font color=\"#0000C0\"><b>$_POST[username]</b>, doesn't exist or new password isn't equal to password repetition!";
                else
				{
					$query = "SELECT TOP 1 sUserPass FROM OdinAccounts..tAccounts WHERE [sUsername]='$usernameP'";
					$q = odbc_exec($connection, $query);
					$qt = odbc_do($connection, $query);
					$get_password = odbc_result($qt, "sUserPass");
					$oldpassword = $get_password;
					if($_POST[oldpassword] != $oldpassword)
					{
						echo"<font color=\"#0000C0\">Invalid old Password!";
					}
					else
					{
						$query = "UPDATE [OdinAccounts].[dbo].[tAccounts] SET [sUserPass] = '$newpassword', [sUserPassSalt] = '$newpassword' WHERE sUsername = '$usernameP'";
                    	$q = odbc_exec($connection, $query);
                    	if($q)
                    	echo"<font color=\"#0000C0\">Username, <b>$_POST[username]</b> has been updated!";
					}
                }
            }
            else
                echo"Please re-enter username and password, remove all special characters!";

        }
        echo "<br><br><a href='".$_SERVER[PHP_SELF]."?id=back'>Return</a>";
}

    exit;

ob_end_flush();
?>
