<?php
	/*
	Hezecom Email Solution
	Author: Hezecom Technologies (http://hezecom.com) info@hezecom.net
	COPYRIGHT 2014 ALL RIGHTS RESERVED
	
	You must have purchased a valid license from CodeCanyon.com in order to have 
	access this file.

	You may only use this file according to the respective licensing terms 
	you agreed to when purchasing this item.
	*/
	
	session_start();	
	include('../config/config.php');
	include('../language/eng.php');
	include('../libraries/functions.php');
	include_once('../libraries/class_dbcon.php');
	include_once('../libraries/upload_class.php');	
	include("../libraries/xmlapi.php");
    include('../libraries/class_cpanel.php');
	$new = new MyCpanel();
	$db=DB::getInstance();
	$dbc=new dboptions();
	
	if($_POST)
	{
		$myemail =  post('myemail');
		$currentPassword = post('oldpwd');
		$newPassword = post('newpwd');
		$newPassword2 = post('newpwd2');
		
	if(strlen($newPassword)<6){
		die ('<div class="alert alert-danger">'.LANG_SHORT_PASSWORD.'</div>');
	}
	
	elseif (!preg_match("#.*^(?=.{8,30})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $newPassword)){
    	die('<div class="alert alert-danger">'.LANG_INVALID_PASSWORD.'</div>');
}

	elseif($newPassword!=$newPassword2){
		die('<div class="alert alert-danger">'.LANG_UNMATCHED_PASSWORD.'</div>');
	}
	
	else{
	$email_user = $myemail;
	$oldpass = $currentPassword;
	$newpass = $newPassword;
	$xmlapi = new xmlapi(C_IP);
	$xmlapi->set_port(2096);
	$xmlapi->password_auth($email_user, $oldpass);
	$xmlapi->set_output('json');
	$xmlapi->set_debug(1);
	$response= $xmlapi->api1_query($email_user, "Email", "passwdpop", array(C_USERNAME, $newpass, 0, C_URL));
	if (strpos($response, 'error')=== false)  
	{ 
	//db conn
	$new_pwd=base64_encode($newPassword);
	$sql = "UPDATE mailusers SET cpassword=:cpassword WHERE cusername = :cusername";
	$stmt=$db->prepare($sql);
	$stmt->bindParam(':cpassword',$new_pwd);
	$stmt->bindParam(':cusername',$email_user);
	$stmt->execute();
	
	//message
	$subject = LANG_PASSWORD_SUBJECT; 
	$notes = LANG_PASSWORD_MESSAGE1.$email_user.LANG_PASSWORD_MESSAGE2; 
	$message = "
	$notes \n 
	";
	$from = LANG_FROM;
	mail($email_user, $subject, $message, $from);
	die( '<div class="alert alert-success">'.LANG_PASSWORD_CHANGED_SUCCESS.'</div>'); 
	} 
	else { 
	die( '<div class="alert alert-danger">'.LANG_INVALID_OLD_PASSWORD.'</div>');   
	}  
	
	}
	}
	?>