File: /home/barbeatleanalyti/www/baris.beatleanalytics.com/runajaxmail.php
<?php
// Include common files.
error_reporting(0);
include_once("site/include/config.php");
include_once(INCLUDE_PATH."/dbClass.php");
$objDB = new MySQLCN();
include_once(INCLUDE_PATH."/functions.php");
include_once(INCLUDE_PATH."/storeconfig.php");
$storeconfig = new storeconfig();
include_once(TEMPLATE_PATH."/common_include.php");
// Create a new object for MySQLCN
// Check if data is variable is exists or not into query string or post data.
if(isset($_REQUEST['data'])){
$data = urldecode(base64_decode($_REQUEST['data']));
// Convert string to array of a data.
$DataExplode = explode("&",$data);
foreach($DataExplode as $key=>$val){
for($i=0; $i<count($DataExplode); $i++ ){
$DataExpSub = explode("=",$DataExplode[$i]);
if(preg_match('/acn=/',$DataExplode[$i])){
$acn = $DataExpSub[1];
}else if(preg_match('/pg=/',$DataExplode[$i])){
$pg = $DataExpSub[1];
}else if(preg_match('/val1=/',$DataExplode[$i])){
$val1 = $DataExpSub[1];
}else if(preg_match('/vari=/',$DataExplode[$i])){
$varI = $DataExpSub[1];
}else if(preg_match('/uid_=/',$DataExplode[$i])){
$uid = $DataExpSub[1];
}else if(preg_match('/start=/',$DataExplode[$i])){
$start = $DataExpSub[1];
}else if(preg_match('/end=/',$DataExplode[$i])){
$end = $DataExpSub[1];
}else if(preg_match('/page_=/',$DataExplode[$i])){
$page_ = $DataExpSub[1];
}else if(preg_match('/Recepiant=/',$DataExplode[$i])){
$Recepiant = $DataExpSub[1];
}else if(preg_match('/type=/',$DataExplode[$i])){
$type = $DataExpSub[1];
}else if(preg_match('/search_=/',$DataExplode[$i])){
$search_ = $DataExpSub[1];
}else if(preg_match('/EmailService=/',$DataExplode[$i])){
$EmailService = $DataExpSub[1];
}else if(preg_match('/UserName=/',$DataExplode[$i])){
$UserName = $DataExpSub[1];
}else if(preg_match('/EmailID=/',$DataExplode[$i])){
$EmailID = $DataExpSub[1];
}else if(preg_match('/Password=/',$DataExplode[$i])){
$Password = $DataExpSub[1];
}else if(preg_match('/SSLRequire=/',$DataExplode[$i])){
$SSLRequire = $DataExpSub[1];
}else if(preg_match('/Host_=/',$DataExplode[$i])){
$Host = $DataExpSub[1];
}else if(preg_match('/IMAPHost=/',$DataExplode[$i])){
$IMAPHost = $DataExpSub[1];
}else if(preg_match('/IMAPPort=/',$DataExplode[$i])){
$IMAPPort = $DataExpSub[1];
}else if(preg_match('/SMTPHost=/',$DataExplode[$i])){
$SMTPHost = $DataExpSub[1];
}else if(preg_match('/SMTPPort=/',$DataExplode[$i])){
$SMTPPort = $DataExpSub[1];
}else if(preg_match('/eid_=/',$DataExplode[$i])){
$eid_ = $DataExpSub[1];
}
}
}
if(empty($page_)){
$page_ = 1;
}
if($acn == 'FetchInboxData'){
$EmployeeType = $_SESSION['UserInfo']['UType'];
if($_SESSION['UserInfo']['UType'] == 'Admin'){
$EmployeeType = "Employee";
}
$SQL_ = "SELECT UserID FROM `email_config` WHERE EmployeeID = '".$_SESSION['UserInfo']['UserID']."' AND EmployeeType = '".$EmployeeType."' ";
$rsData = $objDB->sql_query($SQL_);
if(count($rsData) <= 0){
$returnArr[0] = "First you need to add email config. data <a href='mailconfig.html?v=add' >Add</a>";
$returnArr[1] = "1";
echo json_encode($returnArr);
exit;
}
return FetchMailboxListHTML();
}
else if($acn == 'FetchMailDetails'){
ReadMailHTML();
}
else if($acn == 'DownloadEmailAttachment'){
error_reporting(0);
global $objDB;
SetIMAPData();
//echo "<pre>";
//print_r($_SESSION['mailBox']['Attachments']);
//echo $uid." ".$varI;
//die;
$email_number = $uid;
$structure = imap_fetchstructure($objDB->IMAPData,$email_number);
$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => '');
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($objDB->IMAPData, $email_number, $i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
// for($i = 0; $i < count($structure->parts); $i++)
}
// if(isset($structure->parts) && count($structure->parts))
$varI = $varI + 1;
file_put_contents("tmp/".$attachments[$varI]['filename'], $attachments[$varI]['attachment']);
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . $attachments[$varI]['filename']);
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
echo $message;
/* if(count($attachments)!=0){
foreach($attachments as $at){
if($at[is_attachment]==1){
file_put_contents("tmp/".$at[filename], $at[attachment]);
}
}
} */
//file_put_contents($_SESSION['mailBox']['imapOBJ'][$val1]['filename'], $_SESSION['mailBox']['imapOBJ'][$val1]['attachment']);
//print_r($attachments);
die;
}
else if($acn == 'SetPaginationNext'){
return FetchMailboxListHTML();
}
else if($acn == 'setRecepiantEmail'){
$val1 = strtolower($val1);
$RecepiantArr = array();
if($Recepiant == ""){
$RecepiantArr[] = $val1;
}else{
$RecepiantArr = json_decode($Recepiant);
if(!in_array($val1,$RecepiantArr))
$RecepiantArr[] = $val1;
}
$html = '';
$html .= '<ul class="select2-selection__rendered">';
$i=0;
foreach($RecepiantArr as $value){
$html .= '<li class="select2-selection__choice" title="'.$value.'"><span role="presentation" class="select2-selection__choice__remove" onclick="javascript: return removeAnEmail(\''.$i.'\',\''.$type.'\');" >×</span>'.$value.'</li>';
$i++;
}
$html .= '</ul>';
$returnArr[0] = $html;
$returnArr[1] = json_encode($RecepiantArr);
echo json_encode($returnArr);
}
else if($acn == 'removeRecepiantEmail'){
$RecepiantArr = array();
$RecepiantArr = json_decode($Recepiant);
//echo "<pre>";
//print_r($RecepiantArr);
unset($RecepiantArr[$val1]);
$i=0;
$RecepiantArrNew = array();
foreach($RecepiantArr as $val){
$RecepiantArrNew[$i] = $val;
$i++;
}
//print_r($RecepiantArrNew);
$html = '';
$html .= '<ul class="select2-selection__rendered">';
$i=0;
foreach($RecepiantArrNew as $value){
$html .= '<li class="select2-selection__choice" title="'.$value.'"><span role="presentation" class="select2-selection__choice__remove" onclick="javascript: return removeAnEmail(\''.$i.'\',\''.$type.'\');" >×</span>'.$value.'</li>';
$i++;
}
$html .= '</ul>';
$returnArr[0] = $html;
$returnArr[1] = json_encode($RecepiantArrNew);
echo json_encode($returnArr);
}
else if($acn == 'removeAttechment'){
$RecepiantArr = array();
$RecepiantArr = $_SESSION['attachment'];
//echo "<pre>";
//print_r($RecepiantArr);
unlink($RecepiantArr[$val1]['location']);
unset($RecepiantArr[$val1]);
$i=0;
$RecepiantArrNew = array();
foreach($RecepiantArr as $val){
$RecepiantArrNew[$i] = $val;
//unlink()
$i++;
}
if(count($RecepiantArrNew) > 0){
$_SESSION['attachment'] = $RecepiantArrNew;
}else{
unset($_SESSION['attachment']);
$RecepiantArrNew = array();
}
//print_r($RecepiantArrNew);
$html = '';
$html .= '<ul class="select2-selection__rendered"> || '.count($RecepiantArrNew);
$i=0;
foreach($RecepiantArrNew as $value){
$html .= '<li title="'.$value['name'].'" class="select2-selection__choice">';
$html .= '<span onClick="javascript: return removeAttechment('.$i.');" class="select2-selection__choice__remove pull-right" role="presentation" style="padding-left:10px !important;">×</span> <i class="fa fa-paperclip"></i> ';
$html .= $value['name'];
$html .= '</li>';
$i++;
}
$html .= '</ul>';
//setcookie("attechment", serialize($RecepiantArrNew));
$returnArr[0] = $html;
$returnArr[1] = json_encode($RecepiantArrNew);
echo json_encode($returnArr);
}
else if($acn == 'checkIMAP'){
set_time_limit(0);
error_reporting(0);
global $objDB;
if($EmailService == 'gmail'){
$Host = 'smtp.gmail.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.gmail.com:993/novalidate-cert/imap/ssl}INBOX";
$IMAPPort = 993;
}else if($EmailService == 'yahoo'){
$Host = 'smtp.mail.yahoo.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.mail.yahoo.com:993/imap/ssl}INBOX";
$IMAPPort = 993;
}else{
$IMAPHost = "{".$Host.":".$IMAPPort."/imap";
//{imap.ipage.com:143/imap}INBOX
if(strtolower($SSLRequire) == 'y'){
$IMAPHost .= "/ssl";
}
$IMAPHost .= "}INBOX";
}
$imap = imap_open($IMAPHost,$EmailID,$Password);
$returnArr = array();
if($imap){
$returnArr[0] = 0;
$returnArr[1] = "Success";
}else{
$returnArr[0] = 1;
$returnArr[1] = imap_last_error();
}
echo json_encode($returnArr);
exit;
}
else if($acn == 'checkSMTP'){
error_reporting(0);
global $objDB;
if($EmailService == 'gmail'){
$Host = 'smtp.gmail.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.gmail.com:993/novalidate-cert/imap/ssl}INBOX";
$IMAPPort = 993;
}else if($EmailService == 'yahoo'){
$Host = 'smtp.mail.yahoo.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.mail.yahoo.com:993/imap/ssl}INBOX";
$IMAPPort = 993;
}else{
}
include_once('site/include/class.phpmailer.php');
include_once("site/include/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = $Host; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = $Host; // sets the SMTP server
if(strtolower($SSLRequire) == 'y')
$mail->SMTPSecure = "ssl";
$mail->Port = $SMTPPort; // set the SMTP port for the GMAIL server
$mail->Username = $EmailID; // SMTP account username
$mail->Password = $Password; // SMTP account password
$mail->AddReplyTo($EmailID, $UserName);
$mail->AddAddress($EmailID, $UserName);
$mail->SetFrom($EmailID, $UserName);
$mail->Subject = "Crane Maze System Test Message..";
$mail->MsgHTML("This is an e-mail message sent automatically by <strong>CraneMaze System</strong> while testing the settings for your e-config account.");
$mail->Send();
$rtnid = 0;
$msg = "Success";
} catch (phpmailerException $e) {
//$e->errorMessage(); //Pretty error messages from PHPMailer
$mail->SMTPDebug = 0;
$rtnid = 1;
$msg = "Something wrong to send message";
} catch (Exception $e) {
// $e->getMessage(); //Boring error messages from anything else!
$mail->SMTPDebug = 0;
$rtnid = 1;
//$msg = $e->getMessage();
$msg = "Something wrong to send message";
}
$returnArr = array();
$returnArr[0] = $rtnid;
$returnArr[1] = $msg;
echo json_encode($returnArr);
exit;
}
else if($acn == 'saveEmailData'){
error_reporting(0);
global $objDB;
$returnArr = array();
$EmployeeType = $_SESSION['UserInfo']['UType'];
if($_SESSION['UserInfo']['UType'] == 'Admin'){
$EmployeeType = "Employee";
}
if($EmailService == 'gmail'){
$Host = 'smtp.gmail.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.gmail.com:993/novalidate-cert/imap/ssl}INBOX";
$IMAPPort = 993;
}else if($EmailService == 'yahoo'){
$Host = 'smtp.mail.yahoo.com';
$SMTPPort = 465;
$SSLRequire = 'y';
$IMAPHost = "{imap.mail.yahoo.com:993/imap/ssl}INBOX";
$IMAPPort = 993;
}else{
$IMAPHost = "{".$Host.":".$IMAPPort."/imap";
//{imap.ipage.com:143/imap}INBOX
if(strtolower($SSLRequire) == 'y'){
$IMAPHost .= "/ssl";
}
$IMAPHost .= "}INBOX";
}
$SQL_ = "SELECT UserID FROM `email_config` WHERE UserID = '".$EmailID."' AND EmployeeID = '".$_SESSION['UserInfo']['UserID']."' AND EmployeeType = '".$EmployeeType."' ";
if(strtolower($val1) == 'edit')
$SQL_ .= " AND EmailID != '".$eid_."' ";
$rsData = $objDB->sql_query($SQL_);
if(count($rsData) > 0){
$returnArr[0] = 1;
$returnArr[1] = "Email id allready taken, please choose diff. email id.";
echo json_encode($returnArr);
exit;
}
/*echo "<pre>";
print_r($rsData);
echo "</pre>";
die;
*/
if(strtolower($val1) == 'edit') {
$SQL = "UPDATE `email_config` SET
EmployeeID = '".$_SESSION['UserInfo']['UserID']."',
EmployeeType = '".$EmployeeType."',
UserName = '".$UserName."',
UserID = '".$EmailID."',
Password = '".$Password."',
Type = '".$EmailService."',
Host = '".$IMAPHost."',
HostOnly = '".$Host."',
IMAPPort = '".$IMAPPort."',
SMTPPort = '".$SMTPPort."',
IsSSLRequired = '".strtoupper($SSLRequire)."' WHERE EmailID = '".$eid_."' ";
$rsData = $objDB->sql_query($SQL);
$returnArr[0] = 0;
$returnArr[1] = "Email updated successfuly";
// Add data into admin side databasae table named `email_manage`
// connect to admin database.
$objDB = $objDB->ConnectNewDatabase(DB_USERNAME_ADMIN, DB_PASS_ADMIN, DB_SERVER_ADMIN, DB_DATABASE_ADMIN);
$SQL = "UPDATE `email_manage` SET
EmailID = '".$EmailID."',
UserName = '".$UserName."'
WHERE
DomainName = '".strtolower(SUBDOMAIN_NAME)."' AND
EmployeeID = '".$_SESSION['UserInfo']['UserID']."' AND
EmployeeType = '".$EmployeeType."' ";
$rsData = $objDB->sql_query($SQL);
// connect back to client(customer) server databse.
$objDB = $objDB->ConnectNewDatabase(DB_USERNAME, DB_PASS, DB_SERVER, DB_DATABASE);
} else{
$SQL_ = "UPDATE `email_config` SET IsDefault = 'N' WHERE EmployeeID = '".$_SESSION['UserInfo']['UserID']."' AND
EmployeeType = '".$EmployeeType."' ";
$rsData = $objDB->sql_query($SQL_);
$SQL = "INSERT INTO `email_config` SET
EmployeeID = '".$_SESSION['UserInfo']['UserID']."',
EmployeeType = '".$EmployeeType."',
UserName = '".$UserName."',
UserID = '".$EmailID."',
Password = '".$Password."',
Type = '".$EmailService."',
Host = '".$IMAPHost."',
HostOnly = '".$Host."',
IMAPPort = '".$IMAPPort."',
SMTPPort = '".$SMTPPort."',
IsDefault = 'Y',
IsSSLRequired = '".strtoupper($SSLRequire)."' ";
$rsData = $objDB->sql_query($SQL);
$returnArr[0] = 0;
$returnArr[1] = "Email added successfuly";
// Add data into admin side databasae table named `email_manage`
// connect to admin database.
$objDB = $objDB->ConnectNewDatabase(DB_USERNAME_ADMIN, DB_PASS_ADMIN, DB_SERVER_ADMIN, DB_DATABASE_ADMIN);
$SQL = "INSERT INTO `email_manage` SET
EmailID = '".$EmailID."',
UserName = '".$UserName."',
ClientCode = '".strtolower(SUBDOMAIN_NAME)."',
DomainName = '".strtolower(SUBDOMAIN_NAME)."',
EmployeeID = '".$_SESSION['UserInfo']['UserID']."',
EmployeeType = '".$EmployeeType."',
IsActive = 'Y' ";
$rsData = $objDB->sql_query($SQL);
// connect back to client(customer) server databse.
$objDB = $objDB->ConnectNewDatabase(DB_USERNAME, DB_PASS, DB_SERVER, DB_DATABASE);
}
echo json_encode($returnArr);
exit;
}
}
?>