<?php
require('conf.php');
//Required for swift mailer
require_once (INCLUDE_URL.'/swift/lib/swift_required.php');
$VAR = array_merge($_GET,$_POST);
$page_title = $VAR['page_title'];
$employee_email = $VAR['employee_email'];
$email = mysql_real_escape_string($employee_email);
$smarty->assign('employee_email', $employee_email);
//Get Email Server settings
$q = "SELECT COMPANY_EMAIL_FROM, COMPANY_EMAIL_SERVER, COMPANY_EMAIL_PORT, COMPANY_SMTP_USERNAME, COMPANY_SMTP_PASSWORD, COMPANY_EMAIL_CONNECTION_TYPE FROM ".PRFX."TABLE_COMPANY";
        $rs = $db->Execute($q);
        $company_email = $rs->fields['COMPANY_EMAIL_FROM'];
        $company_server = $rs->fields['COMPANY_EMAIL_SERVER'];
        $server_port = $rs->fields['COMPANY_EMAIL_PORT'];
        $username = $rs->fields['COMPANY_SMTP_USERNAME'];
        $email_password = $rs->fields['COMPANY_SMTP_PASSWORD'];
        $conn_type = $rs->fields['COMPANY_EMAIL_CONNECTION_TYPE'];

//Grab the email address and search the database for match
if($VAR['human'] =='12') {
    if(isset($VAR['submit'])) {
        //Check to see if we have a valid email in the database                    
        $q = "SELECT count(*) AS VALIDATION FROM ".PRFX."TABLE_EMPLOYEE WHERE EMPLOYEE_EMAIL ='".$email."'";
        $rs = $db->Execute($q);
        $validation = $rs->fields['VALIDATION'];
        //If we have a valid email address then lets send the new password
        if($validation !=0) {
          //Generates a Random Password
            function createRandomPassword() {
                $chars = "abcdefghijkmnopqrstuvwxyz023456789";
                srand((double)microtime()*1000000);
                $i = 0;
                $pass = '' ;
                while ($i <= 7) {
                    $num = rand() % 33;
                    $tmp = substr($chars, $num, 1);
                    $pass = $pass . $tmp;
                    $i++;
                }
                return $pass;
            }
            // Usage

            $password = createRandomPassword();

            $passwordmd5 = md5($password);
            $q = "UPDATE ".PRFX."TABLE_EMPLOYEE SET EMPLOYEE_PASSWD ='".$passwordmd5."' WHERE EMPLOYEE_EMAIL ='".$email."'";
            $rs = $db->Execute($q);
            $smarty->assign('employee_password', $employee_password);

            //Create the Transport and determine what type of connection to use SSL of unsecure
            if($conn_type =='SSL'){
             $transport = Swift_SmtpTransport::newInstance($company_server , $server_port, 'ssl')
                    ->setUsername($username)
                    ->setPassword($email_password);
            }else{
                $transport = Swift_SmtpTransport::newInstance($company_server , $server_port)
                    ->setUsername($username)
                    ->setPassword($email_password);
            }
            ////Create the Mailer using your created Transport
            $mailer = Swift_Mailer::newInstance($transport);

            //Create a message
            $message = Swift_Message::newInstance('Password Reset request')
                    ->setFrom(array($company_email => 'System Admin'))
                    ->setTo(array($employee_email => $employee_email))
                    ->setBody('Your password has been reset to '.$password.' If you did not request this change please contact your IT department. It is advised that you login using this password and change it !')
            ;

            //Send the message
            $result = $mailer->send($message);
            //Display Pages
            echo "<script>alert('Successfully Reset and Sent password to $employee_email')</script>";
            $smarty->assign('page_title', 'Password Reset Request');
            $smarty->assign('VERSION', MYIT_CRM_VERSION);
            $smarty->display('core'.SEP.'submit.tpl');
        }else{
            //Display a message if supplied email can't be found
            echo "<script>alert('Sorry but your emaill adrress is not found. Please confirm that you have entered the correct email address')</script>";
            $smarty->display('core'.SEP.'password.tpl');
            
            }
    }
}else{
    $smarty->display('core'.SEP.'password.tpl');
}
?>
