PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Webmails/Save.php

https://bitbucket.org/jhunsinfotech/blue-blues
PHP | 201 lines | 155 code | 29 blank | 17 comment | 23 complexity | 85f4a1b0d0a9d83fd295c1bb574a70a8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0
  1. <?php
  2. /*+********************************************************************************
  3. * The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Initial Developer of the Original Code is FOSS Labs.
  6. * Portions created by FOSS Labs are Copyright (C) FOSS Labs.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. ********************************************************************************/
  10. require_once('modules/Emails/Emails.php');
  11. require_once('modules/Webmails/Webmails.php');
  12. require_once('include/logging.php');
  13. require_once('include/database/PearDatabase.php');
  14. require_once('include/utils/UserInfoUtil.php');
  15. require_once('include/utils/CommonUtils.php');
  16. require_once('modules/Webmails/MailParse.php');
  17. require_once('modules/Webmails/MailBox.php');
  18. require_once('modules/Documents/Documents.php');
  19. require_once('modules/Settings/MailScanner/core/MailAttachmentMIME.php');
  20. global $current_user;
  21. $local_log =& LoggerManager::getLogger('index');
  22. $focus = new Emails();
  23. $to_address = explode(";",$_REQUEST['to_list']);
  24. $cc_address = explode(";",$_REQUEST['cc_list']);
  25. $bcc_address = explode(";",$_REQUEST['bcc_list']);
  26. $start_message=vtlib_purify($_REQUEST["start_message"]);
  27. if($_REQUEST["mailbox"] && $_REQUEST["mailbox"] != "") {$mailbox=vtlib_purify($_REQUEST["mailbox"]);} else {$mailbox="INBOX";}
  28. $MailBox = new MailBox($mailbox);
  29. $mail = $MailBox->mbox;
  30. $email = new Webmails($MailBox->mbox, $_REQUEST["mailid"]);
  31. $subject = imap_utf8($email->subject);
  32. $date = $email->date;
  33. $array_tab = Array();
  34. $email->loadMail($array_tab);
  35. $msgData = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">',"",$email->body);
  36. $content['attachtab'] = $email->attachtab;
  37. while ($tmp = array_pop($content['attachtab'])){
  38. if ((!preg_match('/ATTACHMENT/i', $tmp['disposition'])) && $conf->display_text_attach && (preg_match('/text\/plain/i', $tmp['mime'])))
  39. $msgData .= '<hr />'.view_part_detail($mail, $mailid, $tmp['number'], $tmp['transfer'], $tmp['charset'], $charset);
  40. }
  41. $focus->column_fields['subject']=$subject;
  42. $focus->column_fields["activitytype"]="Emails";
  43. $ddate = date("Y-m-d",strtotime($date));
  44. $dtime = date("h:m");
  45. $focus->column_fields["assigned_user_id"] = $current_user->id;
  46. $focus->column_fields["date_start"] = $ddate;
  47. $focus->column_fields["time_start"] = $dtime;
  48. //Set the flag as 'Webmails' to show up the sent date
  49. $focus->column_fields["email_flag"] = "WEBMAIL";
  50. //Save the To field information in vtiger_emaildetails
  51. $all_to_ids = $email->to;
  52. $focus->column_fields["saved_toid"] = implode(',',$all_to_ids);
  53. //store the sent date in 'yyyy-mm-dd' format
  54. $user_old_date_format = $current_user->date_format;
  55. $current_user->date_format = 'yyyy-mm-dd';
  56. $focus->column_fields["description"]=$msgData;
  57. //to save the email details in vtiger_emaildetails vtiger_tables
  58. $fieldid = $adb->query_result($adb->pquery('select fieldid from vtiger_field where tablename="vtiger_contactdetails" and fieldname="email" and columnname="email" and vtiger_field.presence in (0,2)', array()),0,'fieldid');
  59. if(count($email->relationship) != 0) {
  60. $focus->column_fields['parent_id']=$email->relationship["id"].'@'.$fieldid.'|';
  61. $focus->save("Emails");
  62. if($email->relationship["type"] == "Contacts")
  63. add_attachment_to_contact($email->relationship["id"],$email,$focus->id);
  64. }else {
  65. //if relationship is not available create a contact and relate the email to the contact
  66. require_once('modules/Contacts/Contacts.php');
  67. $contact_focus = new Contacts();
  68. //Populate the lastname as emailid if email doesn't have from name
  69. if($email->fromname){
  70. $contact_focus->column_fields['lastname'] =$email->fromname;
  71. }else{
  72. $contact_focus->column_fields['lastname'] =$email->from;
  73. }
  74. $contact_focus->column_fields['email'] = $email->from;
  75. $contact_focus->column_fields["assigned_user_id"]=$current_user->id;
  76. $contact_focus->save("Contacts");
  77. $focus->column_fields['parent_id']=$contact_focus->id.'@'.$fieldid.'|';
  78. $focus->save("Emails");
  79. add_attachment_to_contact($contact_focus->id,$email,$focus->id);
  80. }
  81. function add_attachment_to_contact($cid,$email,$emailid) {
  82. // add vtiger_attachments to contact
  83. global $adb,$current_user,$default_charset;
  84. for($j=0;$j<2;$j++) {
  85. if($j==0)
  86. $attachments=$email->downloadAttachments();
  87. else
  88. $attachments=$email->downloadInlineAttachments();
  89. $upload_filepath = decideFilePath();
  90. for($i=0,$num_files=count($attachments);$i<$num_files;$i++)
  91. {
  92. $current_id = $adb->getUniqueID("vtiger_crmentity");
  93. $date_var = $adb->formatDate(date('Y-m-d H:i:s'), true);
  94. $filename = preg_replace("/[ ()-]+/", "_",$attachments[$i]["filename"]);
  95. preg_match_all('/=\?([^\?]+)\?([^\?]+)\?([^\?]+)\?=/', $filename, $matches);
  96. $totalmatches = count($matches[0]);
  97. for($index = 0; $index < $totalmatches; ++$index) {
  98. $charset = $matches[1][$index];
  99. $encoding= strtoupper($matches[2][$index]);
  100. $data = $matches[3][$index];
  101. if($encoding == 'B') {
  102. $filename = base64_decode($data);
  103. } else if($encoding == 'Q') {
  104. $filename = quoted_printable_decode($data);
  105. }
  106. $filename = iconv(str_replace('_','-',$charset),$default_charset,$filename);
  107. }
  108. $saveasfile = $upload_filepath.'/'.$current_id.'_'.$filename;
  109. $filetype = MailAttachmentMIME::detect($saveasfile);
  110. $filesize = $attachments[$i]["filesize"];
  111. $query = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(?,?,?,?,?,?,?)";
  112. $qparams = array($current_id, $current_user->id, $current_user->id, 'Contacts Attachment', 'Uploaded from webmail during qualification', $date_var, $date_var);
  113. $result = $adb->pquery($query, $qparams);
  114. $sql = "insert into vtiger_attachments (attachmentsid,name,description,type,path) values(?,?,?,?,?)";
  115. $params = array($current_id, $filename, 'Uploaded '.$filename.' from webmail', $filetype, $upload_filepath);
  116. $result = $adb->pquery($sql, $params);
  117. if(!empty($result)){
  118. // Create document record
  119. $document = new Documents();
  120. $document->column_fields['notes_title'] = $filename;
  121. $document->column_fields['filename'] = $filename;
  122. $document->column_fields['filesize'] = $filesize;
  123. $document->column_fields['filetype'] = $filetype;
  124. $document->column_fields['filestatus'] = 1;
  125. $document->column_fields['filelocationtype'] = 'I';
  126. $document->column_fields['folderid'] = 1; // Default Folder
  127. $document->column_fields['assigned_user_id'] = $current_user->id;
  128. $document->save('Documents');
  129. $sql1 = "insert into vtiger_senotesrel values(?,?)";
  130. $params1 = array($cid, $document->id);
  131. $result = $adb->pquery($sql1, $params1);
  132. $sql1 = "insert into vtiger_seattachmentsrel values(?,?)";
  133. $params1 = array($document->id, $current_id);
  134. $result = $adb->pquery($sql1, $params1);
  135. $sql1 = "insert into vtiger_seattachmentsrel values(?,?)";
  136. $params1 = array($emailid, $current_id);
  137. $result = $adb->pquery($sql1, $params1);
  138. }
  139. //we have to add attachmentsid_ as prefix for the filename
  140. $move_filename = $upload_filepath.'/'.$current_id.'_'.$filename;
  141. $fp = fopen($move_filename, "w") or die("Can't open file");
  142. fputs($fp, base64_decode($attachments[$i]["filedata"]));
  143. fclose($fp);
  144. }
  145. }
  146. }
  147. //Display the sent date in logged in user date format
  148. $current_user->date_format = $user_old_date_format;
  149. function view_part_detail($mail,$mailid,$part_no, &$transfer, &$msg_charset, &$charset)
  150. {
  151. $text = imap_fetchbody($mail,$mailid,$part_no);
  152. if ($transfer == 'BASE64')
  153. $str = nl2br(imap_base64($text));
  154. elseif($transfer == 'QUOTED-PRINTABLE')
  155. $str = nl2br(quoted_printable_decode($text));
  156. else
  157. $str = nl2br($text);
  158. return ($str);
  159. }
  160. $_REQUEST['parent_id'] = $focus->column_fields['parent_id'];
  161. $return_id = vtlib_purify($_REQUEST["mailid"]);
  162. $return_module='Webmails';
  163. $return_action='ListView';
  164. if($_POST["ajax"] != "true")
  165. header("Location: index.php?action=$return_action&module=$return_module&record=$return_id");
  166. return;
  167. ?>