<?php
$optres['css'][] = $module_css_path . "amodules/account/css/forgot.css";
$err = '';
$msg = '';
$webpage_title_en = 'Password Assistance | ' . $domain_content_title;
$meta['description'] = $domain_content_title . ' | This is the page where you can request your password in case you forgot it.';


if(!function_exists("random_char")) {
	function random_char($string) { 
		$length = strlen($string); 
		$position = mt_rand(0, $length - 1); 
		return($string[$position]); 
	}
}

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

$user_ip = getUserIP();

if(!isset($_SESSION['forgot_attempts'])) {
    $_SESSION['forgot_attempts'] = 1;
}
else {
    $_SESSION['forgot_attempts'] = $_SESSION['forgot_attempts'] + 1;
}

$forgot_attempts = $_SESSION['forgot_attempts'];



//seed random generator 
mt_srand((double)microtime() * 1000000); 
$charset = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTWXYZ"; 

if(!function_exists("random_string")) {
	function random_string ($charset_string, $length) { 
		$return_string = random_char($charset_string); 
		for ($x = 1; $x < $length; $x++) 
		$return_string .= random_char($charset_string); 
		return($return_string); 
	}
}

// Hide sides
$cf_show_left_side = false;
$cf_show_right_side = false;

$breadcrumbs [] = array(
    'link' => '',
    'title' => 'Password Assistance'
);

?>
<section class="article">

    <div class="article-body" id="forgot-module">
        <div class="row">
            <div class="col-sm-4 col-sm-offset-3">
                <div class="forgot-box box box-primary">
                    <div class="box-header with-border">
                        <h1 class="no-margin"><i class="fa fa-key fa-lg"></i> Password Assistance</h1>
                    </div>
                    <div class="box-body">
                        <form action="account/forgot" method="post" name="ThisForm" id="ThisForm" class="form-horizontal" role="form">
                            <?php
                            $lookup = isset($_REQUEST['lookup']) ? trim($_REQUEST['lookup']) : '';
                            $shipmail = isset($_REQUEST['shipmail']) ? trim($_REQUEST['shipmail']) : '';

                            if ($shipmail != "" && !filter_var($shipmail, FILTER_VALIDATE_EMAIL)) {
                                $err .= "Invalid email format";
                            }

                            if($forgot_attempts > 3) {
                                $err .= "Are you sure you did have an account? Maybe you didn't.";

                                $f = fopen(dirname(__FILE__). "/../../emails.log", "a+");
                                fwrite($f, $shipmail . "|" . $user_ip . "\n");
                                fclose($f);
                            }


                            if($err == "" && isset($lookup) && $lookup=="yes") {
                                if (isset($shipmail) && $shipmail <> "") {
                                    $result = mysql_query("SELECT * FROM members WHERE shipmail='" . mysql_real_escape_string($shipmail) . "'");
                                    $NumberOfFields = ($result) ? mysql_num_rows($result) : 0;
                                    if ($NumberOfFields > 0) {
                                        $line = mysql_fetch_assoc($result);

                                        if (trim($line['password']) == "") {
                                            $edited_member_id = intval($line['member_id']);
                                            $new_password = random_string($charset, 8);
                                            $q = "update members set password='$new_password' where member_id='$edited_member_id';";
                                            $r = @mysql_query($q);
                                        } else {
                                            $new_password = stripslashes($line['password']);
                                        }
                                        include_once(dirname(__FILE__) . "/../classes/class.phpmailer.php");

                                        $mail = new PHPMailer();
                                        $mail->IsSMTP();
                                        //$mail->SMTPDebug  = 2;
                                        //Ask for HTML-friendly debug output
                                        //$mail->Debugoutput = 'html';
                                        //Set the hostname of the mail server
                                        $mail->Host = "mail.cyprusnet.com";
                                        //Set the SMTP port number - likely to be 25, 465 or 587
                                        $mail->Port = 26;
                                        //Whether to use SMTP authentication
                                        $mail->SMTPAuth = true;

                                        $mail->Username = "noreply@cyprusnet.com"; // SMTP account username
                                        $mail->Password = "6673759NO";        // SMTP account password


                                        $message = "Username: $shipmail<br/>Password: " . $new_password;
                                        $subject = "Password reminder from $ultra_domainname";

                                        $from_name = 'CyprusNet';
                                        $from_email = 'noreply@cyprusnet.com';

                                        //$to_email = html2txt($item->contact_email);
                                        $to_email = "$shipmail";

                                        $mail->From = $from_email;
                                        $mail->FromName = $from_name;
                                        //$mail->ReplyTo = $from_email;

                                        $mail->AddReplyTo($from_email, $from_name);

                                        $mail->Subject = $subject;

                                        $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
                                        $mail->IsHTML(true);
                                        $mail->MsgHTML($message);

                                        $mail->AddAddress($to_email);


                                        //print $body;

                                        if (!$mail->Send()) {
                                            $err .= 'Failed to send password recovery e-mail!';
                                            //$msg .= '<p>' . $mail->ErrorInfo . '</p>';
                                        } else {
                                            $msg = "Your password has been sent to your e-mail account.";
                                        }


                                    }
                                    else {
                                        $err = 'The e-mail account does not belong to a registered member. You may <a href="account/add_member.html">become a member</a> now.';
                                    }
                                }
                                else {
                                    $err = 'Please enter your e-mail account and try again.';
                                }
                            }

                            if($err != "") {
                                echo '<div class="alert"><div class="text-danger"><i class="fa fa-warning fa-lg"></i> '.$err.'</div></div>';
                            }
                            elseif($msg != "") {
                                echo '<div class="alert alert-success">'.$msg.'</div>';
                            }

                            ?>
                            <div class="well well-small"><p>If you forgot your password, please enter your e-mail address associated with your CyprusNet account then click Send. We will send the password to you shortly.</p></div>
                            <div class="form-group">
                                <label for="shipmail" class="sr-only">E-mail address</label>
                                <div class="col-md-10">
                                    <div class="input-group">
                                        <input name="shipmail" type="email" class="form-control input-sm" placeholder="E-mail address" id="shipmail" maxlength="80">
                                        <span class="input-group-btn">
                                            <button class="btn btn-primary btn-sm" type="submit">Send</button>
                                        </span>
                                    </div>
                                </div>
                            </div>

                            <input name="lookup" type="hidden" id="lookup" value="yes">
                            <input name="module_page" type="hidden" id="module_page" value="account">
                            <input name="account_url" type="hidden" value="forgot">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>