]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/InboundEmail/InboundEmail.php
Release 6.5.1
[Github/sugarcrm.git] / modules / InboundEmail / InboundEmail.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 require_once('include/OutboundEmail/OutboundEmail.php');
40
41 function this_callback($str) {
42         foreach($str as $match) {
43                 $ret .= chr(hexdec(str_replace("%","",$match)));
44         }
45         return $ret;
46 }
47
48 /**
49  * Stub for certain interactions;
50  */
51 class temp {
52         var $name;
53 }
54
55 class InboundEmail extends SugarBean {
56         // module specific
57         var $conn;
58         var $purifier; // HTMLPurifier object placeholder
59         var $email;
60
61         // fields
62         var $id;
63         var $deleted;
64         var $date_entered;
65         var $date_modified;
66         var $modified_user_id;
67         var $created_by;
68         var $created_by_name;
69         var $modified_by_name;
70         var $name;
71         var $status;
72         var $server_url;
73         var $email_user;
74         var $email_password;
75         var $port;
76         var $service;
77         var $mailbox;
78         var $mailboxarray;
79         var $delete_seen;
80         var $mailbox_type;
81         var $template_id;
82         var $stored_options;
83         var $group_id;
84         var $is_personal;
85         var $groupfolder_id;
86
87         // email 2.0
88         var $pop3socket;
89         var $outboundInstance; // id to outbound_email instance
90         var $autoImport;
91         var $iconFlagged = "F";
92         var $iconDraft = "D";
93         var $iconAnswered = "A";
94         var $iconDeleted = "del";
95         var $isAutoImport = false;
96         var $smarty;
97         var $attachmentCount = 0;
98         var $tempAttachment = array();
99         var $unsafeChars = array("&", "!", "'", '"', '\\', '/', '<', '>', '|', '$',);
100         var $currentCache;
101         var $defaultSort = 'date';
102         var $defaultDirection = "DESC";
103         var $hrSort = array(
104                         0 => 'flagged',
105                         1 => 'status',
106                         2 => 'from',
107                         3 => 'subj',
108                         4 => 'date',
109                 );
110         var $hrSortLocal = array(
111                         'flagged' => 'flagged',
112                         'status'  => 'answered',
113                         'from'    => 'fromaddr',
114                         'subject' => 'subject',
115                         'date'    => 'senddate',
116                 );
117
118         // default attributes
119         var $transferEncoding                             = array(0 => '7BIT',
120                                                                                                 1 => '8BIT',
121                                                                                                 2 => 'BINARY',
122                                                                                                 3 => 'BASE64',
123                                                                                                 4 => 'QUOTED-PRINTABLE',
124                                                                                                 5 => 'OTHER'
125                                                                                         );
126         // object attributes
127         var $compoundMessageId; // concatenation of messageID and deliveredToEmail
128         var $serverConnectString;
129         var $disable_row_level_security = true;
130         var $InboundEmailCachePath;
131         var $InboundEmailCacheFile                      = 'InboundEmail.cache.php';
132         var $object_name                                        = 'InboundEmail';
133         var $module_dir                                 = 'InboundEmail';
134         var $table_name                                 = 'inbound_email';
135         var $new_schema                                 = true;
136         var $process_save_dates                         = true;
137         var $order_by;
138         var $db;
139         var $dbManager;
140         var $field_defs;
141         var $column_fields;
142         var $required_fields                            = array('name'                  => 'name',
143                                                                                                 'server_url'    => 'server_url',
144                                                                                                 'mailbox'               => 'mailbox',
145                                                                                                 'user'                  => 'user',
146                                                                                                 'port'                  => 'port',
147                                                                                         );
148         var $imageTypes                                 = array("JPG", "JPEG", "GIF", "PNG");
149         var $inlineImages                                       = array();  // temporary space to store ID of inlined images
150         var $defaultEmailNumAutoreplies24Hours = 10;
151         var $maxEmailNumAutoreplies24Hours = 10;
152         // custom ListView attributes
153         var $mailbox_type_name;
154         var $global_personal_string;
155         // service attributes
156         var $tls;
157         var $ca;
158         var $ssl;
159         var $protocol;
160         var $keyForUsersDefaultIEAccount = 'defaultIEAccount';
161         // prefix to use when importing inlinge images in emails
162         public $imagePrefix;
163
164         /**
165          * Sole constructor
166          */
167         function InboundEmail() {
168             $this->InboundEmailCachePath = sugar_cached('modules/InboundEmail');
169             $this->EmailCachePath = sugar_cached('modules/Emails');
170             parent::SugarBean();
171                 if(function_exists("imap_timeout")) {
172                         /*
173                          * 1: Open
174                          * 2: Read
175                          * 3: Write
176                          * 4: Close
177                          */
178                         imap_timeout(1, 60);
179                         imap_timeout(2, 60);
180                         imap_timeout(3, 60);
181                 }
182
183                 $this->smarty = new Sugar_Smarty();
184                 $this->overview = new Overview();
185                 $this->imagePrefix = "{$GLOBALS['sugar_config']['site_url']}/cache/images/";
186         }
187
188         /**
189          * retrieves I-E bean
190          * @param string id
191          * @return object Bean
192          */
193         function retrieve($id, $encode=true, $deleted=true) {
194                 $ret = parent::retrieve($id,$encode,$deleted);
195                 // if I-E bean exist
196                 if (!is_null($ret)) {
197                     $this->email_password = blowfishDecode(blowfishGetKey('InboundEmail'), $this->email_password);
198                     $this->retrieveMailBoxFolders();
199                 }
200                 return $ret;
201         }
202
203         /**
204          * wraps SugarBean->save()
205          * @param string ID of saved bean
206          */
207         function save($check_notify=false) {
208                 // generate cache table for email 2.0
209                 $multiDImArray = $this->generateMultiDimArrayFromFlatArray(explode(",", $this->mailbox), $this->retrieveDelimiter());
210                 $raw = $this->generateFlatArrayFromMultiDimArray($multiDImArray, $this->retrieveDelimiter());
211                 sort($raw);
212                 //_pp(explode(",", $this->mailbox));
213                 //_ppd($raw);
214                 $raw = $this->filterMailBoxFromRaw(explode(",", $this->mailbox), $raw);
215                 $this->mailbox = implode(",", $raw);
216                 if(!empty($this->email_password)) {
217                     $this->email_password = blowfishEncode(blowfishGetKey('InboundEmail'), $this->email_password);
218                 }
219                 $ret = parent::save($check_notify);
220                 return $ret;
221         }
222
223         function filterMailBoxFromRaw($mailboxArray, $rawArray) {
224                 $newArray = array_intersect($mailboxArray, $rawArray);
225                 sort($newArray);
226                 return $newArray;
227         } // fn
228
229         /**
230          * Overrides SugarBean's mark_deleted() to drop the related cache table
231          * @param string $id GUID of I-E instance
232          */
233         function mark_deleted($id) {
234                 parent::mark_deleted($id);
235                 $q = "update inbound_email set groupfolder_id = null WHERE id = '{$id}'";
236                 $r = $this->db->query($q);
237                 $this->deleteCache();
238         }
239
240         /**
241          * Mark cached email answered (replied)
242          * @param string $mailid (uid for imap, message_id for pop3)
243          */
244         function mark_answered($mailid, $type = 'smtp') {
245                 switch ($type) {
246                         case 'smtp' :
247                                 $q = "update email_cache set answered = 1 WHERE imap_uid = $mailid and ie_id = '{$this->id}'";
248                                 $this->db->query($q);
249                                 break;
250                         case 'pop3' :
251                                 $q = "update email_cache set answered = 1 WHERE message_id = '$mailid' and ie_id = '{$this->id}'";
252                                 $this->db->query($q);
253                                 break;
254                 }
255         }
256
257         /**
258          * Renames an IMAP mailbox
259          * @param string $newName
260          */
261         function renameFolder($oldName, $newName) {
262                 //$this->mailbox = "INBOX"
263                 $this->connectMailserver();
264         $oldConnect = $this->getConnectString('', $oldName);
265         $newConnect = $this->getConnectString('', $newName);
266                 if(!imap_renamemailbox($this->conn, $oldConnect , $newConnect)) {
267                         $GLOBALS['log']->debug("***INBOUNDEMAIL: failed to rename mailbox [ {$oldConnect} ] to [ {$newConnect} ]");
268                 } else {
269                 $this->mailbox = str_replace($oldName, $newName, $this->mailbox);
270                 $this->save();
271                 $sessionFoldersString  = $this->getSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol);
272                 $sessionFoldersString = str_replace($oldName, $newName, $sessionFoldersString);
273                         $this->setSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol, $sessionFoldersString);
274
275                 }
276         }
277
278         ///////////////////////////////////////////////////////////////////////////
279         ////    CUSTOM LOGIC HOOKS
280         /**
281          * Called from $this->getMessageText()
282          * Allows upgrade-safe custom processing of message text.
283          *
284          * To use:
285          * 1. Create a directory path: ./custom/modules/InboundEmail if it does not exist
286          * 2. Create a file in the ./custom/InboundEmail/ folder called "getMessageText.php"
287          * 3. Define a function named "custom_getMessageText()" that takes a string as an argument and returns a string
288          *
289          * @param string $msgPart
290          * @return string
291          */
292         function customGetMessageText($msgPart) {
293                 $custom = "custom/modules/InboundEmail/getMessageText.php";
294
295                 if(file_exists($custom)) {
296                         include_once($custom);
297
298                         if(function_exists("custom_getMessageText")) {
299                                 $GLOBALS['log']->debug("*** INBOUND EMAIL-CUSTOM_LOGIC: calling custom_getMessageText()");
300                                 $msgPart = custom_getMessageText($msgPart);
301                         }
302                 }
303
304                 return $msgPart;
305         }
306         ////    END CUSTOM LOGIC HOOKS
307         ///////////////////////////////////////////////////////////////////////////
308
309
310
311         ///////////////////////////////////////////////////////////////////////////
312         ////    EMAIL 2.0 SPECIFIC
313         /**
314          * constructs a nicely formatted version of raw source
315          * @param int $uid UID of email
316          * @return string
317          */
318         function getFormattedRawSource($uid) {
319                 global $app_strings;
320
321                 //if($this->protocol == 'pop3') {
322                 //$raw = $app_strings['LBL_EMAIL_VIEW_UNSUPPORTED'];
323                 //} else {
324                         if (empty($this->id)) {
325                                 $q = "SELECT raw_source FROM emails_text WHERE email_id = '{$uid}'";
326                                 $r = $this->db->query($q);
327                                 $a = $this->db->fetchByAssoc($r);
328                                 $ret = array();
329                                 $raw = $this->convertToUtf8($a['raw_source']);
330                                 if (empty($raw)) {
331                                         $raw = $app_strings['LBL_EMAIL_ERROR_VIEW_RAW_SOURCE'];
332                                 }
333                         } else {
334                                 if ($this->isPop3Protocol()) {
335                                         $uid = $this->getCorrectMessageNoForPop3($uid);
336                                 }
337                                 $raw  = imap_fetchheader($this->conn, $uid, FT_UID+FT_PREFETCHTEXT);
338                                 $raw .= $this->convertToUtf8(imap_body($this->conn, $uid, FT_UID));
339                         } // else
340                         $raw = to_html($raw);
341                         $raw = nl2br($raw);
342                 //}
343
344                 return $raw;
345         }
346
347
348     /**
349      * helper method to convert text to utf-8 if necessary
350      *
351      * @param string $input text
352      * @return string output text
353      */
354     function convertToUtf8($input)
355     {
356        $charset = $GLOBALS['locale']->detectCharset($input, true);
357
358        // we haven't a clue due to missing package?, just return as itself
359        if ($charset === FALSE) {
360            return $input;
361        }
362
363        // convert if we can or must
364        return $this->handleCharsetTranslation($input, $charset);
365     }
366
367         /**
368          * constructs a nicely formatted version of email headers.
369          * @param int $uid
370          * @return string
371          */
372         function getFormattedHeaders($uid) {
373                 global $app_strings;
374
375                 //if($this->protocol == 'pop3') {
376                 //      $header = $app_strings['LBL_EMAIL_VIEW_UNSUPPORTED'];
377                 //} else {
378                         if ($this->isPop3Protocol()) {
379                                 $uid = $this->getCorrectMessageNoForPop3($uid);
380                         }
381                         $headers = imap_fetchheader($this->conn, $uid, FT_UID);
382
383                         $lines = explode("\n", $headers);
384
385                         $header = "<table cellspacing='0' cellpadding='2' border='0' width='100%'>";
386
387                         foreach($lines as $line) {
388                                 $line = trim($line);
389
390                                 if(!empty($line)) {
391                                         $key = trim(substr($line, 0, strpos($line, ":")));
392                                         $key = strip_tags($key);
393                                         $value = trim(substr($line, strpos($line, ":") + 1));
394                                         $value = to_html($value);
395
396                                         $header .= "<tr>";
397                                         $header .= "<td class='displayEmailLabel' NOWRAP><b>{$key}</b>&nbsp;</td>";
398                                         $header .= "<td class='displayEmailValueWhite'>{$value}&nbsp;</td>";
399                                         $header .= "</tr>";
400                                 }
401                         }
402
403                         $header .= "</table>";
404                 //}
405                 return $header;
406         }
407
408         /**
409          * Empties Trash folders
410          */
411         function emptyTrash() {
412                 global $sugar_config;
413
414                 $this->mailbox = $this->get_stored_options("trashFolder");
415                 if (empty($this->mailbox)) {
416                         $this->mailbox = 'INBOX.Trash';
417                 }
418                 $this->connectMailserver();
419
420                 $uids = imap_search($this->conn, "ALL", SE_UID);
421
422                 foreach($uids as $uid) {
423                         if(!imap_delete($this->conn, $uid, FT_UID)) {
424                                 $lastError = imap_last_error();
425                                 $GLOBALS['log']->warn("INBOUNDEMAIL: emptyTrash() Could not delete message [ {$uid} ] from [ {$this->mailbox} ].  IMAP_ERROR [ {$lastError} ]");
426                         }
427                 }
428
429                 // remove local cache file
430                 $q = "DELETE FROM email_cache WHERE mbox = '{$this->mailbox}' AND ie_id = '{$this->id}'";
431                 $r = $this->db->query($q);
432         }
433
434         /**
435          * Fetches a timestamp
436          */
437         function getCacheTimestamp($mbox) {
438                 $key = $this->db->quote("{$this->id}_{$mbox}");
439                 $q = "SELECT ie_timestamp FROM inbound_email_cache_ts WHERE id = '{$key}'";
440                 $r = $this->db->query($q);
441                 $a = $this->db->fetchByAssoc($r);
442
443                 if(empty($a)) {
444                         return -1;
445                 }
446                 return $a['ie_timestamp'];
447         }
448
449         /**
450          * sets the cache timestamp
451          * @param string mbox
452          */
453         function setCacheTimestamp($mbox) {
454                 $key = $this->db->quote("{$this->id}_{$mbox}");
455                 $ts = mktime();
456                 $tsOld = $this->getCacheTimestamp($mbox);
457
458                 if($tsOld < 0) {
459                         $q = "INSERT INTO inbound_email_cache_ts (id, ie_timestamp) VALUES ('{$key}', {$ts})";
460                 } else {
461                         $q = "UPDATE inbound_email_cache_ts SET ie_timestamp = {$ts} WHERE id = '{$key}'";
462                 }
463
464                 $r = $this->db->query($q, true);
465                 $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: setting timestamp query [ {$q} ]");
466         }
467
468
469         /**
470          * Gets a count of all rows that are flagged seen = 0
471          * @param string $mbox
472          * @return int
473          */
474         function getCacheUnreadCount($mbox) {
475                 $q = "SELECT count(*) c FROM email_cache WHERE mbox = '{$mbox}' AND seen = 0 AND ie_id = '{$this->id}'";
476                 $r = $this->db->query($q);
477                 $a = $this->db->fetchByAssoc($r);
478
479                 return $a['c'];
480         }
481
482         /**
483          * Returns total number of emails for a mailbox
484          * @param string mbox
485          * @return int
486          */
487         function getCacheCount($mbox) {
488                 $q = "SELECT count(*) c FROM email_cache WHERE mbox = '{$mbox}' AND ie_id = '{$this->id}'";
489                 $r = $this->db->query($q);
490                 $a = $this->db->fetchByAssoc($r);
491
492                 return $a['c'];
493         }
494
495     function getCacheUnread($mbox) {
496         $q = "SELECT count(*) c FROM email_cache WHERE mbox = '{$mbox}' AND ie_id = '{$this->id}' AND seen = '0'";
497         $r = $this->db->query($q);
498         $a = $this->db->fetchByAssoc($r);
499
500         return $a['c'];
501     }
502
503
504         /**
505          * Deletes all rows for a given instance
506          */
507         function deleteCache() {
508                 $q = "DELETE FROM email_cache WHERE ie_id = '{$this->id}'";
509
510                 $GLOBALS['log']->info("INBOUNDEMAIL: deleting cache using query [ {$q} ]");
511
512                 $r = $this->db->query($q);
513         }
514
515         /**
516          * Deletes all the pop3 data which has been deleted from server
517          */
518         function deletePop3Cache() {
519                 global $sugar_config;
520                 $UIDLs = $this->pop3_getUIDL();
521                 $cacheUIDLs = $this->pop3_getCacheUidls();
522                 foreach($cacheUIDLs as $msgNo => $msgId) {
523                         if (!in_array($msgId, $UIDLs)) {
524                                 $md5msgIds = md5($msgId);
525                                 $file = "{$this->EmailCachePath}/{$this->id}/messages/INBOX{$md5msgIds}.PHP";
526                                 $GLOBALS['log']->debug("INBOUNDEMAIL: deleting file [ {$file} ] ");
527                                 if(file_exists($file)) {
528                                         if(!unlink($file)) {
529                                                 $GLOBALS['log']->debug("INBOUNDEMAIL: Could not delete [ {$file} ] ");
530                                         } // if
531                                 } // if
532                                 $q = "DELETE from email_cache where imap_uid = {$msgNo} AND msgno = {$msgNo} AND ie_id = '{$this->id}' AND message_id = '{$msgId}'";
533                                 $r = $this->db->query($q);
534                         } // if
535                 } // for
536         } // fn
537
538         /**
539          * Retrieves cached headers
540          * @return array
541          */
542         function getCacheValueForUIDs($mbox, $UIDs) {
543                 if (!is_array($UIDs) || empty($UIDs)) {
544                         return array();
545                 }
546
547                 $q = "SELECT * FROM email_cache WHERE ie_id = '{$this->id}' AND mbox = '{$mbox}' AND ";
548                 $startIndex = 0;
549                 $endIndex = 5;
550
551                 $slicedArray = array_slice($UIDs, $startIndex ,$endIndex);
552                 $columnName = ($this->isPop3Protocol() ? "message_id" : "imap_uid");
553                 $ret = array(
554                         'timestamp'     => $this->getCacheTimestamp($mbox),
555                         'uids'          => array(),
556                         'retArr'        => array(),
557                 );
558                 while (!empty($slicedArray)) {
559                         $messageIdString = implode(',', $slicedArray);
560                         $GLOBALS['log']->debug("sliced array = {$messageIdString}");
561                         $extraWhere = "{$columnName} IN (";
562                         $i = 0;
563                         foreach($slicedArray as $UID) {
564                                 if($i != 0) {
565                                         $extraWhere = $extraWhere . ",";
566                                 } // if
567                                 $i++;
568                                 $extraWhere = "{$extraWhere} '{$UID}'";
569                         } // foreach
570                         $newQuery = $q . $extraWhere . ")";
571                         $r = $this->db->query($newQuery);
572
573                         while($a = $this->db->fetchByAssoc($r)) {
574                                 if (isset($a['uid'])) {
575                                         if ($this->isPop3Protocol()) {
576                                                 $ret['uids'][] = $a['message_id'];
577                                         } else {
578                                         $ret['uids'][] = $a['uid'];
579                                         }
580                                 }
581
582                                 $overview = new Overview();
583
584                                 foreach($a as $k => $v) {
585                                         $k=strtolower($k);
586                                         switch($k) {
587                                                 case "imap_uid":
588                                                         $overview->imap_uid = $v;
589                                                         if ($this->isPop3Protocol()) {
590                                                                 $overview->uid = $a['message_id'];
591                                                         } else {
592                                                                 $overview->uid = $v;
593                                                         }
594                                                 break;
595                                                 case "toaddr":
596                                                         $overview->to = from_html($v);
597                                                 break;
598
599                                                 case "fromaddr":
600                                                         $overview->from = from_html($v);
601                                                 break;
602
603                                                 case "mailsize":
604                                                         $overview->size = $v;
605                                                 break;
606
607                                                 case "senddate":
608                                                         $overview->date = $v;
609                                                 break;
610
611                                                 default:
612                                                         $overview->$k = from_html($v);
613                                                 break;
614                                         } // switch
615                                 } // foreach
616                                 $ret['retArr'][] = $overview;
617                         } // while
618                         $startIndex = $startIndex + $endIndex;
619                         $slicedArray = array_slice($UIDs, $startIndex ,$endIndex);
620                         $messageIdString = implode(',', $slicedArray);
621                         $GLOBALS['log']->debug("sliced array = {$messageIdString}");
622                 } // while
623                 return $ret;
624         }
625
626         /**
627          * Retrieves cached headers
628          * @return array
629          */
630         function getCacheValue($mbox, $limit = 20, $page = 1, $sort='', $direction='') {
631                 // try optimizing this call as we don't want repeat queries
632                 if(!empty($this->currentCache)) {
633                         return $this->currentCache;
634                 }
635
636                 $sort = (empty($sort)) ? $this->defaultSort : $sort;
637                 $direction = (empty($direction)) ? $this->defaultDirection : $direction;
638                 $order = " ORDER BY {$this->hrSortLocal[$sort]} {$direction}";
639
640                 $q = "SELECT * FROM email_cache WHERE ie_id = '{$this->id}' AND mbox = '{$mbox}' {$order}";
641
642                 if(!empty($limit)) {
643                         $start = ( $page - 1 ) * $limit;
644                         $r = $this->db->limitQuery($q, $start, $limit);
645                 } else {
646                         $r = $this->db->query($q);
647                 }
648
649                 $ret = array(
650                         'timestamp'     => $this->getCacheTimestamp($mbox),
651                         'uids'          => array(),
652                         'retArr'        => array(),
653                 );
654
655                 while($a = $this->db->fetchByAssoc($r)) {
656                         if (isset($a['uid'])) {
657                                 if ($this->isPop3Protocol()) {
658                                         $ret['uids'][] = $a['message_id'];
659                                 } else {
660                                 $ret['uids'][] = $a['uid'];
661                                 }
662                         }
663
664                         $overview = new Overview();
665
666                         foreach($a as $k => $v) {
667                                 $k=strtolower($k);
668                                 switch($k) {
669                                         case "imap_uid":
670                                                 $overview->imap_uid = $v;
671                                                 if ($this->isPop3Protocol()) {
672                                                         $overview->uid = $a['message_id'];
673                                                 } else {
674                                                         $overview->uid = $v;
675                                                 }
676                                         break;
677                                         case "toaddr":
678                                                 $overview->to = from_html($v);
679                                         break;
680
681                                         case "fromaddr":
682                                                 $overview->from = from_html($v);
683                                         break;
684
685                                         case "mailsize":
686                                                 $overview->size = $v;
687                                         break;
688
689                                         case "senddate":
690                                                 $overview->date = $v;
691                                         break;
692
693                                         default:
694                                                 $overview->$k = from_html($v);
695                                         break;
696                                 }
697                         }
698                         $ret['retArr'][] = $overview;
699                 }
700
701                 $this->currentCache = $ret;
702
703                 return $ret;
704         }
705
706         /**
707          * Sets cache values
708          */
709         function setCacheValue($mbox, $insert, $update=array(), $remove=array()) {
710                 if(empty($mbox)) {
711                         return;
712                 }
713                 global $timedate;
714
715
716                 // reset in-memory cache
717                 $this->currentCache = null;
718
719                 $table = 'email_cache';
720                 $where = "WHERE ie_id = '{$this->id}' AND mbox = '{$mbox}'";
721
722                 // handle removed rows
723                 if(!empty($remove)) {
724                         $removeIds = '';
725                         foreach($remove as $overview) {
726                                 if(!empty($removeIds)) {
727                                         $removeIds .= ",";
728                                 }
729
730                                 $removeIds .= "'{$overview->imap_uid}'";
731                         }
732
733                         $q = "DELETE FROM {$table} {$where} AND imap_uid IN ({$removeIds})";
734
735                         $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: delete query [ {$q} ]");
736
737                         $r = $this->db->query($q, true, $q);
738                 }
739
740                 // handle insert rows
741                 if(!empty($insert)) {
742                         $q = "SELECT imap_uid FROM {$table} {$where}";
743                         $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: filter UIDs query [ {$q} ]");
744                         $r = $this->db->query($q);
745                         $uids = array();
746
747                         while($a = $this->db->fetchByAssoc($r)) {
748                                 $uids[] = $a['imap_uid'];
749                         }
750                         $count = count($uids);
751                         $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: found [ {$count} ] UIDs to filter against");
752
753                         $tmp = '';
754                         foreach($uids as $uid) {
755                                 if(!empty($tmp))
756                                         $tmp .= ", ";
757                                 $tmp .= "{$uid}";
758                         }
759                         $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: filter UIDs: [ {$tmp} ]");
760
761                         $cols = "";
762
763                         foreach($this->overview->fieldDefs as $colDef) {
764                                 if(!empty($cols))
765                                         $cols .= ",";
766
767                                 $cols .= "{$colDef['name']}";
768                         }
769                         foreach($insert as $overview) {
770                 if(in_array($overview->imap_uid, $uids))
771                 {
772                     // fixing bug #49543: setting 'mbox' property for the following updating of other items in this box
773                     if (!isset($overview->mbox))
774                     {
775                         $overview->mbox = $mbox;
776                     }
777                     $update[] = $overview;
778                     continue;
779                 }
780
781                                 $values = '';
782
783                                 foreach($this->overview->fieldDefs as $colDef) {
784                                         if(!empty($values)) {
785                                                 $values .= ", ";
786                                         }
787
788                                         // trim values for Oracle/MSSql
789                                         if(     isset($colDef['len']) && !empty($colDef['len']) &&
790                                                 isset($colDef['type']) && !empty($colDef['type']) &&
791                                                 $colDef['type'] == 'varchar'
792                                         )
793                     {
794                         if (isset($overview->$colDef['name']))
795                         {
796                             $overview->$colDef['name'] = substr($overview->$colDef['name'], 0, $colDef['len']);
797                         }
798                     }
799
800                                         switch($colDef['name']) {
801                                                 case "imap_uid":
802                                                         if(isset($overview->uid) && !empty($overview->uid)) {
803                                                                 $this->imap_uid = $overview->uid;
804                                                         }
805                                                         $values .= "'{$this->imap_uid}'";
806                                                 break;
807
808                                                 case "ie_id":
809                                                         $values .= "'{$this->id}'";
810                                                 break;
811
812                                                 case "toaddr":
813                                                         $values .= $this->db->quoted($overview->to);
814                                                 break;
815
816                                                 case "fromaddr":
817                                                         $values .= $this->db->quoted($overview->from);
818                                                 break;
819
820                                                 case "message_id" :
821                                                         $values .= $this->db->quoted($overview->message_id);
822                                                 break;
823
824                                                 case "mailsize":
825                                                         $values .= $overview->size;
826                                                 break;
827
828                                                 case "senddate":
829                                                         $conv=$timedate->fromString($overview->date);
830                                                         if (!empty($conv)) {
831                                                                 $values .= $this->db->quoted($conv->asDb());
832                                                         } else {
833                                                                 $values .= "NULL";
834                                                         }
835                                                 break;
836
837                                                 case "mbox":
838                                                         $values .= "'{$mbox}'";
839                                                 break;
840
841                                                 default:
842                                                         $overview->$colDef['name'] = SugarCleaner::cleanHtml(from_html($overview->$colDef['name']));
843                                                         $values .= $this->db->quoted($overview->$colDef['name']);
844                                                 break;
845                                         }
846                                 }
847
848                                 $q = "INSERT INTO {$table} ({$cols}) VALUES ({$values})";
849                                 $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: insert query [ {$q} ]");
850                                 $r = $this->db->query($q, true, $q);
851                         }
852                 }
853
854                 // handle update rows
855                 if(!empty($update)) {
856                         $cols = "";
857                         foreach($this->overview->fieldDefs as $colDef) {
858                                 if(!empty($cols))
859                                         $cols .= ",";
860
861                                 $cols .= "{$colDef['name']}";
862                         }
863
864                         foreach($update as $overview) {
865                                 $q = "UPDATE {$table} SET ";
866
867                                 $set = '';
868                                 foreach($this->overview->fieldDefs as $colDef) {
869
870                                         switch($colDef['name']) {
871                                                 case "toaddr":
872                                                 case "fromaddr":
873                                                 case "mailsize":
874                                                 case "senddate":
875                                                 case "mbox":
876                                                 case "ie_id":
877                                                 break;
878
879                                                 default:
880                             if(!empty($set))
881                             {
882                                 $set .= ",";
883                             }
884                             $value = '';
885                             if (isset($overview->$colDef['name']))
886                             {
887                                 $value = $this->db->quoted($overview->$colDef['name']);
888                             }
889                             else
890                             {
891                                 $value = $this->db->quoted($value);
892                             }
893                             $set .= "{$colDef['name']} = " . $value;
894                                                 break;
895                                         }
896                                 }
897
898                                 $q .= $set . " WHERE ie_id = '{$this->id}' AND mbox = '{$overview->mbox}' AND imap_uid = '{$overview->imap_uid}'";
899                                 $GLOBALS['log']->info("INBOUNDEMAIL-CACHE: update query [ {$q} ]");
900                                 $r = $this->db->query($q, true, $q);
901                         }
902                 }
903
904         }
905
906         /**
907          * Opens a socket connection to the pop3 server
908          * @return bool
909          */
910         function pop3_open() {
911                 if(!is_resource($this->pop3socket)) {
912                         $GLOBALS['log']->info("*** INBOUNDEMAIL: opening socket connection");
913                         $exServ = explode('::', $this->service);
914                         $socket  = ($exServ[2] == 'ssl') ? "ssl://" : "tcp://";
915                         $socket .= $this->server_url;
916                         $this->pop3socket = fsockopen($socket, $this->port);
917                 } else {
918                         $GLOBALS['log']->info("*** INBOUNDEMAIL: REUSING socket connection");
919                         return true;
920                 }
921
922                 if(!is_resource($this->pop3socket)) {
923                         $GLOBALS['log']->debug("*** INBOUNDEMAIL: unable to open socket connection");
924                         return false;
925                 }
926
927                 // clear buffer
928                 $ret = trim(fgets($this->pop3socket, 1024));
929                 $GLOBALS['log']->info("*** INBOUNDEMAIL: got socket connection [ {$ret} ]");
930                 return true;
931         }
932
933         /**
934          * Closes connections and runs clean-up routines
935          */
936         function pop3_cleanUp() {
937                 $GLOBALS['log']->info("*** INBOUNDEMAIL: cleaning up socket connection");
938                 fputs($this->pop3socket, "QUIT\r\n");
939                 $buf = fgets($this->pop3socket, 1024);
940                 fclose($this->pop3socket);
941         }
942
943         /**
944          * sends a command down to the POP3 server
945          * @param string command
946          * @param string args
947          * @param bool return
948          * @return string
949          */
950         function pop3_sendCommand($command, $args='', $return=true) {
951                 $command .= " {$args}";
952                 $command = trim($command);
953                 $GLOBALS['log']->info("*** INBOUNDEMAIL: pop3_sendCommand() SEND [ {$command} ]");
954                 $command .= "\r\n";
955
956                 fputs($this->pop3socket, $command);
957
958                 if($return) {
959                         $ret = trim(fgets($this->pop3socket, 1024));
960                         $GLOBALS['log']->info("*** INBOUNDEMAIL: pop3_sendCommand() RECEIVE [ {$ret} ]");
961                         return $ret;
962                 }
963         }
964
965         function getPop3NewMessagesToDownload() {
966                 $pop3UIDL = $this->pop3_getUIDL();
967                 $cacheUIDLs = $this->pop3_getCacheUidls();
968                 // new email cache values we should deal with
969                 $diff = array_diff_assoc($pop3UIDL, $cacheUIDLs);
970                 // this is msgNo to UIDL array
971                 $diff = $this->pop3_shiftCache($diff, $cacheUIDLs);
972                 // get all the keys which are msgnos;
973                 return array_keys($diff);
974         }
975
976         function getPop3NewMessagesToDownloadForCron() {
977                 $pop3UIDL = $this->pop3_getUIDL();
978                 $cacheUIDLs = $this->pop3_getCacheUidls();
979                 // new email cache values we should deal with
980                 $diff = array_diff_assoc($pop3UIDL, $cacheUIDLs);
981                 // this is msgNo to UIDL array
982                 $diff = $this->pop3_shiftCache($diff, $cacheUIDLs);
983                 // insert data into email_cache
984                 if ($this->groupfolder_id != null && $this->groupfolder_id != "" && $this->isPop3Protocol()) {
985                         $searchResults = array_keys($diff);
986                         $concatResults = implode(",", $searchResults);
987                         if ($this->connectMailserver() == 'true') {
988                                 $fetchedOverviews = imap_fetch_overview($this->conn, $concatResults);
989                                 // clean up cache entry
990                                 foreach($fetchedOverviews as $k => $overview) {
991                                         $overview->message_id = trim($diff[$overview->msgno]);
992                                         $fetchedOverviews[$k] = $overview;
993                                 }
994                                 $this->updateOverviewCacheFile($fetchedOverviews);
995                         }
996                 } // if
997                 return $diff;
998         }
999
1000         /**
1001          * This method returns all the UIDL for this account. This should be called if the protocol is pop3
1002          * @return array od messageno to UIDL array
1003          */
1004         function pop3_getUIDL() {
1005                 $UIDLs = array();
1006                 if($this->pop3_open()) {
1007                         // authenticate
1008                         $this->pop3_sendCommand("USER", $this->email_user);
1009                         $this->pop3_sendCommand("PASS", $this->email_password);
1010
1011                         // get UIDLs
1012                         $this->pop3_sendCommand("UIDL", '', false); // leave socket buffer alone until the while()
1013                         fgets($this->pop3socket, 1024); // handle "OK+";
1014                         $UIDLs = array();
1015
1016                         $buf = '!';
1017
1018                         if(is_resource($this->pop3socket)) {
1019                                 while(!feof($this->pop3socket)) {
1020                                         $buf = fgets($this->pop3socket, 1024); // 8kb max buffer - shouldn't be more than 80 chars via pop3...
1021                                         //_pp(trim($buf));
1022
1023                                         if(trim($buf) == '.') {
1024                                                 $GLOBALS['log']->debug("*** GOT '.'");
1025                                                 break;
1026                                         }
1027
1028                                         // format is [msgNo] [UIDL]
1029                                         $exUidl = explode(" ", $buf);
1030                                         $UIDLs[$exUidl[0]] = trim($exUidl[1]);
1031                                 } // while
1032                         } // if
1033                         $this->pop3_cleanUp();
1034                 } // if
1035                 return $UIDLs;
1036         } // fn
1037
1038         /**
1039          * Special handler for POP3 boxes.  Standard IMAP commands are useless.
1040          * This will fetch only partial emails for POP3 and hence needs to be call again and again based on status it returns
1041          */
1042         function pop3_checkPartialEmail($synch = false) {
1043                 require_once('include/utils/array_utils.php');
1044                 global $current_user;
1045                 global $sugar_config;
1046
1047                 $cacheDataExists = false;
1048                 $diff = array();
1049                 $results = array();
1050                 $cacheFilePath = clean_path("{$this->EmailCachePath}/{$this->id}/folders/MsgNOToUIDLData.php");
1051                 if(file_exists($cacheFilePath)) {
1052                         $cacheDataExists = true;
1053                         if($fh = @fopen($cacheFilePath, "rb")) {
1054                                 $data = "";
1055                                 $chunksize = 1*(1024*1024); // how many bytes per chunk
1056                                 while(!feof($fh)) {
1057                                         $buf = fgets($fh, $chunksize); // 8kb max buffer - shouldn't be more than 80 chars via pop3...
1058                                         $data = $data . $buf;
1059                                 flush();
1060                                 } // while
1061                                 fclose($fh);
1062                                 $diff = unserialize($data);
1063                                 if (!empty($diff)) {
1064                                         if (count($diff)> 50) {
1065                                 $newDiff = array_slice($diff, 50, count($diff), true);
1066                                         } else {
1067                                                 $newDiff=array();
1068                                         }
1069                         $results = array_slice(array_keys($diff), 0 ,50);
1070                                         $data = serialize($newDiff);
1071                                     if($fh = @fopen($cacheFilePath, "w")) {
1072                                         fputs($fh, $data);
1073                                         fclose($fh);
1074                                     } // if
1075                                 }
1076                         } // if
1077                 } // if
1078                 if (!$cacheDataExists) {
1079                         if ($synch) {
1080                             $this->deletePop3Cache();
1081                         }
1082                         $UIDLs = $this->pop3_getUIDL();
1083                         if(count($UIDLs) > 0) {
1084                                 // get cached UIDLs
1085                                 $cacheUIDLs = $this->pop3_getCacheUidls();
1086
1087                                 // new email cache values we should deal with
1088                                 $diff = array_diff_assoc($UIDLs, $cacheUIDLs);
1089                                 $diff = $this->pop3_shiftCache($diff, $cacheUIDLs);
1090                                 require_once('modules/Emails/EmailUI.php');
1091                                 EmailUI::preflightEmailCache("{$this->EmailCachePath}/{$this->id}");
1092
1093                                 if (count($diff)> 50) {
1094                         $newDiff = array_slice($diff, 50, count($diff), true);
1095                                 } else {
1096                                         $newDiff=array();
1097                                 }
1098
1099                                 $results = array_slice(array_keys($diff), 0 ,50);
1100                                 $data = serialize($newDiff);
1101                             if($fh = @fopen($cacheFilePath, "w")) {
1102                                 fputs($fh, $data);
1103                                 fclose($fh);
1104                             } // if
1105                         } else {
1106                                 $GLOBALS['log']->debug("*** INBOUNDEMAIL: could not open socket connection to POP3 server");
1107                                 return "could not open socket connection to POP3 server";
1108                         } // else
1109                 } // if
1110
1111                 // build up msgNo request
1112                 if(count($diff) > 0) {
1113                         // remove dirty cache entries
1114                         $startingNo = 0;
1115                         if (isset($_REQUEST['currentCount']) && $_REQUEST['currentCount'] > -1) {
1116                              $startingNo = $_REQUEST['currentCount'];
1117                         }
1118
1119                         $this->mailbox = 'INBOX';
1120                         $this->connectMailserver();
1121                         //$searchResults = array_keys($diff);
1122                         //$fetchedOverviews = array();
1123                         //$chunkArraySerachResults = array_chunk($searchResults, 50);
1124                         $concatResults = implode(",", $results);
1125                         $GLOBALS['log']->info('$$$$ '.$concatResults);
1126                         $GLOBALS['log']->info("[EMAIL] Start POP3 fetch overview on mailbox [{$this->mailbox}] for user [{$current_user->user_name}] on 50 data");
1127                         $fetchedOverviews = imap_fetch_overview($this->conn, $concatResults);
1128                         $GLOBALS['log']->info("[EMAIL] End POP3 fetch overview on mailbox [{$this->mailbox}] for user [{$current_user->user_name}] on "
1129                         . sizeof($fetchedOverviews) . " data");
1130
1131                         // clean up cache entry
1132                         foreach($fetchedOverviews as $k => $overview) {
1133                                 $overview->message_id = trim($diff[$overview->msgno]);
1134                                 $fetchedOverviews[$k] = $overview;
1135                         }
1136
1137                         $GLOBALS['log']->info("[EMAIL] Start updating overview cache for pop3 mailbox [{$this->mailbox}] for user [{$current_user->user_name}]");
1138                         $this->updateOverviewCacheFile($fetchedOverviews);
1139                         $GLOBALS['log']->info("[EMAIL] Start updating overview cache for pop3 mailbox [{$this->mailbox}] for user [{$current_user->user_name}]");
1140                         return array('status' => "In Progress", 'mbox' => $this->mailbox, 'count'=> (count($results) + $startingNo), 'totalcount' => count($diff), 'ieid' => $this->id);
1141                 } // if
1142                 unlink($cacheFilePath);
1143                 return  array('status' => "done");
1144         }
1145
1146
1147         /**
1148          * Special handler for POP3 boxes.  Standard IMAP commands are useless.
1149          */
1150         function pop3_checkEmail() {
1151                 if($this->pop3_open()) {
1152                         // authenticate
1153                         $this->pop3_sendCommand("USER", $this->email_user);
1154                         $this->pop3_sendCommand("PASS", $this->email_password);
1155
1156                         // get UIDLs
1157                         $this->pop3_sendCommand("UIDL", '', false); // leave socket buffer alone until the while()
1158                         fgets($this->pop3socket, 1024); // handle "OK+";
1159                         $UIDLs = array();
1160
1161                         $buf = '!';
1162
1163                         if(is_resource($this->pop3socket)) {
1164                                 while(!feof($this->pop3socket)) {
1165                                         $buf = fgets($this->pop3socket, 1024); // 8kb max buffer - shouldn't be more than 80 chars via pop3...
1166                                         //_pp(trim($buf));
1167
1168                                         if(trim($buf) == '.') {
1169                                                 $GLOBALS['log']->debug("*** GOT '.'");
1170                                                 break;
1171                                         }
1172
1173                                         // format is [msgNo] [UIDL]
1174                                         $exUidl = explode(" ", $buf);
1175                                         $UIDLs[$exUidl[0]] = trim($exUidl[1]);
1176                                 }
1177                         }
1178
1179                         $this->pop3_cleanUp();
1180
1181                         // get cached UIDLs
1182                         $cacheUIDLs = $this->pop3_getCacheUidls();
1183 //                      _pp($UIDLs);_pp($cacheUIDLs);
1184
1185                         // new email cache values we should deal with
1186                         $diff = array_diff_assoc($UIDLs, $cacheUIDLs);
1187
1188                         // remove dirty cache entries
1189                         $diff = $this->pop3_shiftCache($diff, $cacheUIDLs);
1190
1191                         // build up msgNo request
1192                         if(!empty($diff)) {
1193                                 $this->mailbox = 'INBOX';
1194                                 $this->connectMailserver();
1195                                 $searchResults = array_keys($diff);
1196                                 $concatResults = implode(",", $searchResults);
1197                                 $fetchedOverviews = imap_fetch_overview($this->conn, $concatResults);
1198
1199                                 // clean up cache entry
1200                                 foreach($fetchedOverviews as $k => $overview) {
1201                                         $overview->message_id = trim($diff[$overview->msgno]);
1202                                         $fetchedOverviews[$k] = $overview;
1203                                 }
1204
1205                                 $this->updateOverviewCacheFile($fetchedOverviews);
1206                         }
1207                 } else {
1208                         $GLOBALS['log']->debug("*** INBOUNDEMAIL: could not open socket connection to POP3 server");
1209                         return false;
1210                 }
1211         }
1212
1213         /**
1214          * Iterates through msgno and message_id to remove dirty cache entries
1215          * @param array diff
1216          */
1217         function pop3_shiftCache($diff, $cacheUIDLs) {
1218                 $msgNos = "";
1219                 $msgIds = "";
1220                 $newArray = array();
1221                 foreach($diff as $msgNo => $msgId) {
1222                         if (in_array($msgId, $cacheUIDLs)) {
1223                                 $q1 = "UPDATE email_cache SET imap_uid = {$msgNo}, msgno = {$msgNo} WHERE ie_id = '{$this->id}' AND message_id = '{$msgId}'";
1224                                 $this->db->query($q1);
1225                         } else {
1226                                 $newArray[$msgNo] = $msgId;
1227                         }
1228                 }
1229                 return $newArray;
1230                 /*
1231                 foreach($diff as $msgNo => $msgId) {
1232                         if(!empty($msgNos)) {
1233                                 $msgNos .= ", ";
1234                         }
1235                         if(!empty($msgIds)) {
1236                                 $msgIds .= ", ";
1237                         }
1238
1239                         $msgNos .= $msgNo;
1240                         $msgIds .= "'{$msgId}'";
1241                 }
1242
1243                 if(!empty($msgNos)) {
1244                         $q1 = "DELETE FROM email_cache WHERE ie_id = '{$this->id}' AND msgno IN ({$msgNos})";
1245                         $this->db->query($q1);
1246                 }
1247                 if(!empty($msgIds)) {
1248                         $q2 = "DELETE FROM email_cache WHERE ie_id = '{$this->id}' AND message_id IN ({$msgIds})";
1249                         $this->db->query($q2);
1250                 }
1251                 */
1252         }
1253
1254         /**
1255          * retrieves cached uidl values.
1256          * When dealing with POP3 accounts, the message_id column in email_cache will contain the UIDL.
1257          * @return array
1258          */
1259         function pop3_getCacheUidls() {
1260                 $q = "SELECT msgno, message_id FROM email_cache WHERE ie_id = '{$this->id}'";
1261                 $r = $this->db->query($q);
1262
1263                 $ret = array();
1264                 while($a = $this->db->fetchByAssoc($r)) {
1265                         $ret[$a['msgno']] = $a['message_id'];
1266                 }
1267
1268                 return $ret;
1269         }
1270
1271         /**
1272          * This function is used by cron job for group mailbox without group folder
1273          * @param string $msgno for pop
1274          * @param string $uid for imap
1275          */
1276         function getMessagesInEmailCache($msgno, $uid) {
1277                 $fetchedOverviews = array();
1278                 if ($this->isPop3Protocol()) {
1279                         $fetchedOverviews = imap_fetch_overview($this->conn, $msgno);
1280                         foreach($fetchedOverviews as $k => $overview) {
1281                                 $overview->message_id = $uid;
1282                                 $fetchedOverviews[$k] = $overview;
1283                         }
1284                 } else {
1285                         $fetchedOverviews = imap_fetch_overview($this->conn, $uid, FT_UID);
1286                 } // else
1287                 $this->updateOverviewCacheFile($fetchedOverviews);
1288
1289         } // fn
1290
1291         /**
1292          * Checks email (local caching too) for one mailbox
1293          * @param string $mailbox IMAP Mailbox path
1294          * @param bool $prefetch Flag to prefetch email body on check
1295          */
1296         function checkEmailOneMailbox($mailbox, $prefetch=true, $synchronize=false) {
1297                 global $sugar_config;
1298                 global $current_user;
1299                 global $app_strings;
1300
1301         $result = 1;
1302
1303                 $GLOBALS['log']->info("INBOUNDEMAIL: checking mailbox [ {$mailbox} ]");
1304                 $this->mailbox = $mailbox;
1305                 $this->connectMailserver();
1306
1307                 $checkTime = '';
1308                 $shouldProcessRules = true;
1309
1310                 $timestamp = $this->getCacheTimestamp($mailbox);
1311
1312                 if($timestamp > 0) {
1313                         $checkTime = date('r', $timestamp);
1314                 }
1315
1316                 /* first time through, process ALL emails */
1317                 if(empty($checkTime) || $synchronize) {
1318                         // do not process rules for the first time or sunchronize
1319                         $shouldProcessRules = false;
1320                         $criteria = "ALL UNDELETED";
1321                         $prefetch = false; // do NOT prefetch emails on a brand new account - timeouts happen.
1322                         $GLOBALS['log']->info("INBOUNDEMAIL: new account detected - not prefetching email bodies.");
1323                 } else {
1324                         $criteria = "SINCE \"{$checkTime}\" UNDELETED"; // not using UNSEEN
1325                 }
1326                 $this->setCacheTimestamp($mailbox);
1327                 $GLOBALS['log']->info("[EMAIL] Performing IMAP search using criteria [{$criteria}] on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1328                 $searchResults = imap_search($this->conn, $criteria, SE_UID);
1329                 $GLOBALS['log']->info("[EMAIL] Done IMAP search on mailbox [{$mailbox}] for user [{$current_user->user_name}]. Result count = ".count($searchResults));
1330
1331                 if(!empty($searchResults)) {
1332
1333                         $concatResults = implode(",", $searchResults);
1334                         $GLOBALS['log']->info("[EMAIL] Start IMAP fetch overview on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1335                         $fetchedOverview = imap_fetch_overview($this->conn, $concatResults, FT_UID);
1336                         $GLOBALS['log']->info("[EMAIL] Done IMAP fetch overview on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1337
1338                         $GLOBALS['log']->info("[EMAIL] Start updating overview cache for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1339                         $this->updateOverviewCacheFile($fetchedOverview);
1340                         $GLOBALS['log']->info("[EMAIL] Done updating overview cache for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1341
1342                         // prefetch emails
1343                         if($prefetch == true) {
1344                                 $GLOBALS['log']->info("[EMAIL] Start fetching emails for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1345                                 if(!$this->fetchCheckedEmails($fetchedOverview))
1346                 {
1347                     $result = 0;
1348                 }
1349                                 $GLOBALS['log']->info("[EMAIL] Done fetching emails for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1350                         }
1351                 } else {
1352                         $GLOBALS['log']->info("INBOUNDEMAIL: no results for mailbox [ {$mailbox} ]");
1353             $result = 1;
1354                 }
1355
1356                 /**
1357                  * To handle the use case where an external client is also connected, deleting emails, we need to clear our
1358                  * local cache of all emails with the "DELETED" flag
1359                  */
1360                 $criteria  = 'DELETED';
1361                 $criteria .= (!empty($checkTime)) ? " SINCE \"{$checkTime}\"" : "";
1362                 $GLOBALS['log']->info("INBOUNDEMAIL: checking for deleted emails using [ {$criteria} ]");
1363
1364                 $trashFolder = $this->get_stored_options("trashFolder");
1365                 if (empty($trashFolder)) {
1366                         $trashFolder = "INBOX.Trash";
1367                 }
1368
1369                 if($this->mailbox != $trashFolder) {
1370                         $searchResults = imap_search($this->conn, $criteria, SE_UID);
1371                         if(!empty($searchResults)) {
1372                                 $uids = implode($app_strings['LBL_EMAIL_DELIMITER'], $searchResults);
1373                                 $GLOBALS['log']->info("INBOUNDEMAIL: removing UIDs found deleted [ {$uids} ]");
1374                                 $this->getOverviewsFromCacheFile($uids, $mailbox, true);
1375                         }
1376                 }
1377         return $result;
1378         }
1379
1380            /**
1381      * Checks email (local caching too) for one mailbox
1382      * @param string $mailbox IMAP Mailbox path
1383      * @param bool $prefetch Flag to prefetch email body on check
1384      */
1385     function checkEmailOneMailboxPartial($mailbox, $prefetch=true, $synchronize=false, $start = 0, $max = -1) {
1386         global $sugar_config;
1387         global $current_user;
1388         global $app_strings;
1389
1390         $GLOBALS['log']->info("INBOUNDEMAIL: checking mailbox [ {$mailbox} ]");
1391         $this->mailbox = $mailbox;
1392         $this->connectMailserver();
1393
1394         $checkTime = '';
1395         $shouldProcessRules = true;
1396
1397         $timestamp = $this->getCacheTimestamp($mailbox);
1398
1399         if($timestamp > 0) {
1400             $checkTime = date('r', $timestamp);
1401         }
1402
1403         /* first time through, process ALL emails */
1404         if(empty($checkTime) || $synchronize) {
1405             // do not process rules for the first time or sunchronize
1406             $shouldProcessRules = false;
1407             $criteria = "ALL UNDELETED";
1408             $prefetch = false; // do NOT prefetch emails on a brand new account - timeouts happen.
1409             $GLOBALS['log']->info("INBOUNDEMAIL: new account detected - not prefetching email bodies.");
1410         } else {
1411             $criteria = "SINCE \"{$checkTime}\" UNDELETED"; // not using UNSEEN
1412         }
1413         $this->setCacheTimestamp($mailbox);
1414         $GLOBALS['log']->info("[EMAIL] Performing IMAP search using criteria [{$criteria}] on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1415         $searchResults = $this->getCachedIMAPSearch($criteria);
1416
1417         if(!empty($searchResults)) {
1418
1419             $total = sizeof($searchResults);
1420             $searchResults = array_slice($searchResults, $start, $max);
1421
1422             $GLOBALS['log']->info("INBOUNDEMAIL: there are  $total messages in [{$mailbox}], we are on $start");
1423             $GLOBALS['log']->info("INBOUNDEMAIL: getting the next " . sizeof($searchResults) . " messages");
1424             $concatResults = implode(",", $searchResults);
1425             $GLOBALS['log']->info("INBOUNDEMAIL: Start IMAP fetch overview on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1426             $fetchedOverview = imap_fetch_overview($this->conn, $concatResults, FT_UID);
1427             $GLOBALS['log']->info("INBOUNDEMAIL: Done IMAP fetch overview on mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1428
1429             $GLOBALS['log']->info("INBOUNDEMAIL: Start updating overview cache for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1430             $this->updateOverviewCacheFile($fetchedOverview);
1431             $GLOBALS['log']->info("INBOUNDEMAIL: Done updating overview cache for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1432
1433             // prefetch emails
1434             if($prefetch == true) {
1435                 $GLOBALS['log']->info("INBOUNDEMAIL: Start fetching emails for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1436                 $this->fetchCheckedEmails($fetchedOverview);
1437                 $GLOBALS['log']->info("INBOUNDEMAIL: Done fetching emails for mailbox [{$mailbox}] for user [{$current_user->user_name}]");
1438             }
1439             $status = ($total > $start + sizeof($searchResults)) ? 'continue' : 'done';
1440             $ret = array('status' => $status, 'count' => $start + sizeof($searchResults), 'mbox' => $mailbox, 'totalcount' => $total);
1441             $GLOBALS['log']->info("INBOUNDEMAIL: $status : Downloaded " . $start + sizeof($searchResults) . "messages of $total");
1442
1443         } else {
1444             $GLOBALS['log']->info("INBOUNDEMAIL: no results for mailbox [ {$mailbox} ]");
1445             $ret = array('status' =>'done');
1446         }
1447
1448         if ($ret['status'] == 'done') {
1449                 //Remove the cached search if we are done with this mailbox
1450                 $cacheFilePath = clean_path("{$this->EmailCachePath}/{$this->id}/folders/SearchData.php");
1451             unlink($cacheFilePath);
1452                 /**
1453                  * To handle the use case where an external client is also connected, deleting emails, we need to clear our
1454                  * local cache of all emails with the "DELETED" flag
1455                  */
1456                 $criteria  = 'DELETED';
1457                 $criteria .= (!empty($checkTime)) ? " SINCE \"{$checkTime}\"" : "";
1458                 $GLOBALS['log']->info("INBOUNDEMAIL: checking for deleted emails using [ {$criteria} ]");
1459
1460                         $trashFolder = $this->get_stored_options("trashFolder");
1461                         if (empty($trashFolder)) {
1462                                 $trashFolder = "INBOX.Trash";
1463                         }
1464
1465                 if($this->mailbox != $trashFolder) {
1466                     $searchResults = imap_search($this->conn, $criteria, SE_UID);
1467                     if(!empty($searchResults)) {
1468                         $uids = implode($app_strings['LBL_EMAIL_DELIMITER'], $searchResults);
1469                         $GLOBALS['log']->info("INBOUNDEMAIL: removing UIDs found deleted [ {$uids} ]");
1470                         $this->getOverviewsFromCacheFile($uids, $mailbox, true);
1471                     }
1472                 }
1473         }
1474         return $ret;
1475     }
1476
1477     function getCachedIMAPSearch($criteria) {
1478         global $current_user;
1479         global $sugar_config;
1480
1481         $cacheDataExists = false;
1482         $diff = array();
1483         $results = array();
1484         $cacheFolderPath = clean_path("{$this->EmailCachePath}/{$this->id}/folders");
1485         if (!file_exists($cacheFolderPath)) {
1486                 mkdir_recursive($cacheFolderPath);
1487         }
1488         $cacheFilePath = $cacheFolderPath . '/SearchData.php';
1489         $GLOBALS['log']->info("INBOUNDEMAIL: Cache path is $cacheFilePath");
1490         if(file_exists($cacheFilePath)) {
1491             $cacheDataExists = true;
1492             if($fh = @fopen($cacheFilePath, "rb")) {
1493                 $data = "";
1494                 $chunksize = 1*(1024*1024); // how many bytes per chunk
1495                 while(!feof($fh)) {
1496                     $buf = fgets($fh, $chunksize); // 8kb max buffer - shouldn't be more than 80 chars via pop3...
1497                     $data = $data . $buf;
1498                     flush();
1499                 } // while
1500                 fclose($fh);
1501                 $results = unserialize($data);
1502             } // if
1503         } // if
1504         if (!$cacheDataExists) {
1505             $searchResults = imap_search($this->conn, $criteria, SE_UID);
1506             if(count($searchResults) > 0) {
1507                 $results = $searchResults;
1508                 $data = serialize($searchResults);
1509                 if($fh = @fopen($cacheFilePath, "w")) {
1510                     fputs($fh, $data);
1511                     fclose($fh);
1512                 } // if
1513             }
1514         } // if
1515         return $results;
1516     }
1517
1518     function checkEmailIMAPPartial($prefetch=true, $synch = false) {
1519         $GLOBALS['log']->info("*****************INBOUNDEMAIL: at IMAP check partial");
1520         global $sugar_config;
1521         $result = $this->connectMailserver();
1522         if ($result == 'false')
1523         {
1524             return array(
1525                 'status' => 'error',
1526                 'message' => 'Email server is down'
1527             );
1528         }
1529         $mailboxes = $this->getMailboxes(true);
1530         if (!in_array('INBOX', $mailboxes)) {
1531             $mailboxes[] = 'INBOX';
1532         }
1533         sort($mailboxes);
1534         if (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox']) && isset($_REQUEST['currentCount'])) {
1535                 $GLOBALS['log']->info("INBOUNDEMAIL: Picking up from where we left off");
1536             $mbox = $_REQUEST['mbox'];
1537             $count = $_REQUEST['currentCount'];
1538         } else {
1539                 if ($synch) {
1540                         $GLOBALS['log']->info("INBOUNDEMAIL: Cleaning out the cache");
1541                         $this->cleanOutCache();
1542                 }
1543             $mbox = $mailboxes[0];
1544             $count = 0;
1545         }
1546         $GLOBALS['log']->info("INBOUNDEMAIL:found " . sizeof($mailboxes) . " Mailboxes");
1547         $index = array_search($mbox, $mailboxes) + 1;
1548         $ret = $this->checkEmailOneMailboxPartial($mbox, $prefetch, $synch, $count, 100);
1549         while($ret['status'] == 'done' && $index < sizeof($mailboxes)) {
1550             if ($ret['count'] > 100) {
1551                 $ret['mbox'] = $mailboxes[$index];
1552                 $ret['status'] = 'continue';
1553                 return $ret;
1554             }
1555             $GLOBALS['log']->info("INBOUNDEMAIL: checking account [ $index => $mbox : $count]");
1556             $mbox = $mailboxes[$index];
1557             $ret = $this->checkEmailOneMailboxPartial($mbox, $prefetch, $synch, 0, 100);
1558             $index++;
1559         }
1560
1561         return $ret;
1562     }
1563
1564         function checkEmail2_meta() {
1565                 global $sugar_config;
1566
1567                 $this->connectMailserver();
1568                 $mailboxes = $this->getMailboxes(true);
1569                 $mailboxes[] = 'INBOX';
1570                 sort($mailboxes);
1571
1572                 $GLOBALS['log']->info("INBOUNDEMAIL: checking account [ {$this->name} ]");
1573
1574                 $mailboxes_meta = array();
1575                 foreach($mailboxes as $mailbox) {
1576                         $mailboxes_meta[$mailbox] = $this->getMailboxProcessCount($mailbox);
1577                 }
1578
1579                 $ret = array();
1580                 $ret['mailboxes'] = $mailboxes_meta;
1581
1582                 foreach($mailboxes_meta as $count) {
1583                         $ret['processCount'] += $count;
1584                 }
1585                 return $ret;
1586         }
1587
1588         function getMailboxProcessCount($mailbox) {
1589                 global $sugar_config;
1590
1591                 $GLOBALS['log']->info("INBOUNDEMAIL: checking mailbox [ {$mailbox} ]");
1592                 $this->mailbox = $mailbox;
1593                 $this->connectMailserver();
1594
1595                 $timestamp = $this->getCacheTimestamp($mailbox);
1596
1597                 $checkTime = '';
1598                 if($timestamp > 0) {
1599                         $checkTime = date('r', $timestamp);
1600                 }
1601
1602                 /* first time through, process ALL emails */
1603                 if(empty($checkTime)) {
1604                         $criteria = "ALL UNDELETED";
1605                         $prefetch = false; // do NOT prefetch emails on a brand new account - timeouts happen.
1606                         $GLOBALS['log']->info("INBOUNDEMAIL: new account detected - not prefetching email bodies.");
1607                 } else {
1608                         $criteria = "SINCE \"{$checkTime}\" UNDELETED"; // not using UNSEEN
1609                 }
1610
1611                 $GLOBALS['log']->info("INBOUNDEMAIL: using [ {$criteria} ]");
1612                 $searchResults = imap_search($this->conn, $criteria, SE_UID);
1613
1614                 if(!empty($searchResults)) {
1615                         $concatResults = implode(",", $searchResults);
1616                 } else {
1617                         $GLOBALS['log']->info("INBOUNDEMAIL: no results for mailbox [ {$mailbox} ]");
1618                 }
1619
1620                 if(empty($searchResults)) {
1621                         return 0;
1622                 }
1623
1624                 return count($searchResults);
1625         }
1626
1627         /**
1628          * update INBOX
1629          */
1630         function checkEmail($prefetch=true, $synch = false) {
1631                 global $sugar_config;
1632
1633                 if($this->protocol == 'pop3') {
1634                         $this->pop3_checkEmail();
1635                 } else {
1636                         $this->connectMailserver();
1637                         $mailboxes = $this->getMailboxes(true);
1638                         sort($mailboxes);
1639
1640                         $GLOBALS['log']->info("INBOUNDEMAIL: checking account [ {$this->name} ]");
1641
1642                         foreach($mailboxes as $mailbox) {
1643                                 $this->checkEmailOneMailbox($mailbox, $prefetch, $synch);
1644                         }
1645                 }
1646         }
1647
1648         /**
1649          * full synchronization
1650          */
1651         function syncEmail() {
1652                 global $sugar_config;
1653                 global $current_user;
1654
1655                 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
1656
1657                 if(empty($showFolders)) {
1658                         $showFolders = array();
1659                 }
1660
1661                 $email = new Email();
1662                 $email->email2init();
1663
1664                 // personal accounts
1665                 if($current_user->hasPersonalEmail()) {
1666                         $personals = $this->retrieveByGroupId($current_user->id);
1667
1668                         foreach($personals as $personalAccount) {
1669                                 if(in_array($personalAccount->id, $showFolders)) {
1670                                         $personalAccount->email = $email;
1671                                         if ($personalAccount->isPop3Protocol()) {
1672                                                 $personalAccount->deletePop3Cache();
1673                                                 continue;
1674                                         }
1675                                         $personalAccount->cleanOutCache();
1676                                         $personalAccount->connectMailserver();
1677                                         $mailboxes = $personalAccount->getMailboxes(true);
1678                                         $mailboxes[] = 'INBOX';
1679                                         sort($mailboxes);
1680
1681                                         $GLOBALS['log']->info("[EMAIL] Start checking account [{$personalAccount->name}] for user [{$current_user->user_name}]");
1682
1683                                         foreach($mailboxes as $mailbox) {
1684                                                 $GLOBALS['log']->info("[EMAIL] Start checking mailbox [{$mailbox}] of account [{$personalAccount->name}] for user [{$current_user->user_name}]");
1685                                                 $personalAccount->checkEmailOneMailbox($mailbox, false, true);
1686                                                 $GLOBALS['log']->info("[EMAIL] Done checking mailbox [{$mailbox}] of account [{$personalAccount->name}] for user [{$current_user->user_name}]");
1687                                         }
1688                                         $GLOBALS['log']->info("[EMAIL] Done checking account [{$personalAccount->name}] for user [{$current_user->user_name}]");
1689                                 }
1690                         }
1691                 }
1692
1693                 // group accounts
1694                 $beans = $this->retrieveAllByGroupId($current_user->id, false);
1695                 foreach($beans as $k => $groupAccount) {
1696                         if(in_array($groupAccount->id, $showFolders)) {
1697                                 $groupAccount->email = $email;
1698                                 $groupAccount->cleanOutCache();
1699                                 $groupAccount->connectMailserver();
1700                                 $mailboxes = $groupAccount->getMailboxes(true);
1701                                 $mailboxes[] = 'INBOX';
1702                                 sort($mailboxes);
1703
1704                                 $GLOBALS['log']->info("INBOUNDEMAIL: checking account [ {$groupAccount->name} ]");
1705
1706                                 foreach($mailboxes as $mailbox) {
1707                                         $groupAccount->checkEmailOneMailbox($mailbox, false, true);
1708                                 }
1709                         }
1710                 }
1711         }
1712
1713
1714         /**
1715          * Deletes cached messages when moving from folder to folder
1716          * @param string $uids
1717          * @param string $fromFolder
1718          * @param string $toFolder
1719          */
1720         function deleteCachedMessages($uids, $fromFolder) {
1721                 global $sugar_config;
1722
1723                 if(!isset($this->email) && !isset($this->email->et)) {
1724                         $this->email = new Email();
1725                         $this->email->email2init();
1726                 }
1727
1728                 $uids = $this->email->et->_cleanUIDList($uids);
1729
1730                 foreach($uids as $uid) {
1731                         $file = "{$this->EmailCachePath}/{$this->id}/messages/{$fromFolder}{$uid}.php";
1732
1733                         if(file_exists($file)) {
1734                                 if(!unlink($file)) {
1735                                         $GLOBALS['log']->debug("INBOUNDEMAIL: Could not delete [ {$file} ]");
1736                                 }
1737                         }
1738                 }
1739         }
1740
1741         /**
1742          * similar to imap_fetch_overview, but it gets overviews from a local cache
1743          * file.
1744          * @param string $uids UIDs in comma-delimited format
1745          * @param string $mailbox The mailbox in focus, will default to $this->mailbox
1746          * @param bool $remove Default false
1747          * @return array
1748          */
1749         function getOverviewsFromCacheFile($uids, $mailbox='', $remove=false) {
1750                 global $app_strings;
1751                 if(!isset($this->email) && !isset($this->email->et)) {
1752                         $this->email = new Email();
1753                         $this->email->email2init();
1754                 }
1755
1756                 $uids = $this->email->et->_cleanUIDList($uids, true);
1757
1758                 // load current cache file
1759                 $mailbox = empty($mailbox) ? $this->mailbox : $mailbox;
1760                 $cacheValue = $this->getCacheValue($mailbox);
1761                 $ret = array();
1762
1763                 // prep UID array
1764                 $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
1765                 foreach($exUids as $k => $uid) {
1766                         $exUids[$k] = trim($uid);
1767                 }
1768
1769                 // fill $ret will requested $uids
1770                 foreach($cacheValue['retArr'] as $k => $overview) {
1771                         if(in_array($overview->imap_uid, $exUids)) {
1772                                 $ret[] = $overview;
1773                         }
1774                 }
1775
1776                 // remove requested $uids from current cache file (move_mail() type action)
1777                 if($remove) {
1778                         $this->setCacheValue($mailbox, array(), array(), $ret);
1779                 }
1780                 return $ret;
1781         }
1782
1783         /**
1784          * merges new info with the saved cached file
1785          * @param array $array Array of email Overviews
1786          * @param string $type 'append' or 'remove'
1787          * @param string $mailbox Target mailbox if not current assigned
1788          */
1789         function updateOverviewCacheFile($array, $type='append', $mailbox='') {
1790                 $mailbox = empty($mailbox) ? $this->mailbox : $mailbox;
1791
1792                 $cacheValue = $this->getCacheValue($mailbox);
1793                 $uids = $cacheValue['uids'];
1794
1795                 $updateRows = array();
1796                 $insertRows = array();
1797                 $removeRows = array();
1798
1799                 // update values
1800                 if($type == 'append') { // append
1801                         /* we are adding overviews to the cache file */
1802                         foreach($array as $overview) {
1803                                 if(isset($overview->uid)) {
1804                                         $overview->imap_uid = $overview->uid; // coming from imap_fetch_overview() call
1805                                 }
1806
1807                                 if(!in_array($overview->imap_uid, $uids)) {
1808                                         $insertRows[] = $overview;
1809                                 }
1810                         }
1811                 } else {
1812                         $updatedCacheOverviews = array();
1813                         // compare against generated list
1814                         /* we are removing overviews from the cache file */
1815                         foreach($cacheValue['retArr'] as $cacheOverview) {
1816                                 if(!in_array($cacheOverview->imap_uid, $uids)) {
1817                                         $insertRows[] = $cacheOverview;
1818                                 } else {
1819                                         $removeRows[] = $cacheOverview;
1820                                 }
1821                         }
1822
1823                         $cacheValue['retArr'] = $updatedCacheOverviews;
1824                 }
1825
1826                 $this->setCacheValue($mailbox, $insertRows, $updateRows, $removeRows);
1827         }
1828
1829         /**
1830          * Check email prefetches email bodies for quicker display
1831          * @param array array of fetched overviews
1832          */
1833         function fetchCheckedEmails($fetchedOverviews) {
1834                 global $sugar_config;
1835
1836                 if(is_array($fetchedOverviews) && !empty($fetchedOverviews)) {
1837                         foreach($fetchedOverviews as $overview) {
1838                                 if($overview->size < 10000) {
1839
1840                                         $uid = $overview->imap_uid;
1841
1842                                         if(!empty($uid)) {
1843                                                 $file = "{$this->mailbox}{$uid}.php";
1844                                                 $cacheFile = clean_path("{$this->EmailCachePath}/{$this->id}/messages/{$file}");
1845
1846                                                 if(!file_exists($cacheFile)) {
1847                                                         $GLOBALS['log']->info("INBOUNDEMAIL: Prefetching email [ {$file} ]");
1848                                                         $this->setEmailForDisplay($uid);
1849                                                         $out = $this->displayOneEmail($uid, $this->mailbox);
1850                                                         $this->email->et->writeCacheFile('out', $out, $this->id, 'messages', "{$this->mailbox}{$uid}.php");
1851                                                 } else {
1852                                                         $GLOBALS['log']->debug("INBOUNDEMAIL: Trying to prefetch an email we already fetched! [ {$cacheFile} ]");
1853                                                 }
1854                                         } else {
1855                                                 $GLOBALS['log']->debug("*** INBOUNDEMAIL: prefetch has a message with no UID");
1856                                         }
1857                     return true;
1858                                 } else {
1859                                         $GLOBALS['log']->debug("INBOUNDEMAIL: skipping email prefetch - size too large [ {$overview->size} ]");
1860                                 }
1861                         }
1862                 }
1863         return false;
1864         }
1865
1866         /**
1867          * Sets flags on emails.  Assumes that connection is live, correct folder is
1868          * set.
1869          * @param string $uids Sequence of UIDs, comma separated
1870          * @param string $type Flag to mark
1871          */
1872         function markEmails($uids, $type) {
1873                 switch($type) {
1874                         case 'unread':
1875                                 $result = imap_clearflag_full($this->conn, $uids, '\\SEEN', ST_UID);
1876                         break;
1877                         case 'read':
1878                                 $result = imap_setflag_full($this->conn, $uids, '\\SEEN', ST_UID);
1879                         break;
1880                         case 'flagged':
1881                                 $result = imap_setflag_full($this->conn, $uids, '\\FLAGGED', ST_UID);
1882                         break;
1883                         case 'unflagged':
1884                                 $result = imap_clearflag_full($this->conn, $uids, '\\FLAGGED', ST_UID);
1885                         break;
1886                         case 'answered':
1887                                 $result = imap_setflag_full($this->conn, $uids, '\\Answered', ST_UID);
1888                         break;
1889                 }
1890         }
1891         ////    END EMAIL 2.0 SPECIFIC
1892         ///////////////////////////////////////////////////////////////////////////
1893
1894
1895
1896         ///////////////////////////////////////////////////////////////////////////
1897         ////    SERVER MANIPULATION METHODS
1898         /**
1899          * Deletes the specified folder
1900          * @param string $mbox "::" delimited IMAP mailbox path, ie, INBOX.saved.stuff
1901          * @return bool
1902          */
1903         function deleteFolder($mbox) {
1904                 $returnArray = array();
1905                 if ($this->getCacheCount($mbox) > 0) {
1906                         $returnArray['status'] = false;
1907                         $returnArray['errorMessage'] = "Can not delete {$mbox} as it has emails.";
1908                         return $returnArray;
1909                 }
1910                 $connectString = $this->getConnectString('', $mbox);
1911                 //Remove Folder cache
1912                 global $sugar_config;
1913                 unlink("{$this->EmailCachePath}/{$this->id}/folders/folders.php");
1914
1915                 if(imap_unsubscribe($this->conn, imap_utf7_encode($connectString))) {
1916                         if(imap_deletemailbox($this->conn, $connectString)) {
1917                         $this->mailbox = str_replace(("," . $mbox), "", $this->mailbox);
1918                         $this->save();
1919                         $sessionFoldersString  = $this->getSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol);
1920                         $sessionFoldersString = str_replace(("," . $mbox), "", $sessionFoldersString);
1921                                 $this->setSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol, $sessionFoldersString);
1922                                 $returnArray['status'] = true;
1923                                 return $returnArray;
1924                         } else {
1925                                 $GLOBALS['log']->error("*** ERROR: EMAIL2.0 - could not delete IMAP mailbox with path: [ {$connectString} ]");
1926                                 $returnArray['status'] = false;
1927                                 $returnArray['errorMessage'] = "NOOP: could not delete folder: {$connectString}";
1928                                 return $returnArray;
1929                                 return false;
1930                         }
1931                 } else {
1932                         $GLOBALS['log']->error("*** ERROR: EMAIL2.0 - could not unsubscribe from folder, {$connectString} before deletion.");
1933                         $returnArray['status'] = false;
1934                         $returnArray['errorMessage'] = "NOOP: could not unsubscribe from folder, {$connectString} before deletion.";
1935                         return $returnArray;
1936                 }
1937         }
1938
1939         /**
1940          * Saves new folders
1941          * @param string $name Name of new IMAP mailbox
1942          * @param string $mbox "::" delimited IMAP mailbox path, ie, INBOX.saved.stuff
1943          * @return bool True on success
1944          */
1945         function saveNewFolder($name, $mbox) {
1946                 global $sugar_config;
1947         //Remove Folder cache
1948         global $sugar_config;
1949         //unlink("{$this->EmailCachePath}/{$this->id}/folders/folders.php");
1950
1951         //$mboxImap = $this->getImapMboxFromSugarProprietary($mbox);
1952         $delimiter = $this->get_stored_options('folderDelimiter');
1953         if (!$delimiter) {
1954                 $delimiter = '.';
1955         }
1956
1957         $newFolder = $mbox . $delimiter . $name;
1958         $mbox .= $delimiter.str_replace($delimiter, "_", $name);
1959         $connectString = $this->getConnectString('', $mbox);
1960
1961                 if(imap_createmailbox($this->conn, imap_utf7_encode($connectString))) {
1962                         imap_subscribe($this->conn, imap_utf7_encode($connectString));
1963                         $status = imap_status($this->conn, str_replace("{$delimiter}{$name}","",$connectString), SA_ALL);
1964                 $this->mailbox = $this->mailbox . "," . $newFolder;
1965                 $this->save();
1966                 $sessionFoldersString  = $this->getSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol);
1967                 $sessionFoldersString = $sessionFoldersString . "," . $newFolder;
1968                         $this->setSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol, $sessionFoldersString);
1969
1970                         echo json_encode($status);
1971                         return true;
1972                 } else {
1973                         echo "NOOP: could not create folder";
1974                         $GLOBALS['log']->error("*** ERROR: EMAIL2.0 - could not create IMAP mailbox with path: [ {$connectString} ]");
1975                         return false;
1976                 }
1977
1978         }
1979
1980         /**
1981          * Constructs an IMAP c-client compatible folder path from Sugar proprietary
1982          * @param string $mbox "::" delimited IMAP mailbox path, ie, INBOX.saved.stuff
1983          * @return string
1984          */
1985         function getImapMboxFromSugarProprietary($mbox) {
1986                 $exMbox = explode("::", $mbox);
1987
1988                 $mboxImap = '';
1989
1990                 for($i=2; $i<count($exMbox); $i++) {
1991                         if(!empty($mboxImap)) {
1992                                 $mboxImap .= ".";
1993                         }
1994                         $mboxImap .= $exMbox[$i];
1995                 }
1996
1997                 return $mboxImap;
1998         }
1999
2000         /**
2001          * Searches IMAP (and POP3?) accounts/folders for emails with qualifying criteria
2002          */
2003         function search($ieId, $subject='', $from='', $to='', $body='', $dateFrom='', $dateTo='') {
2004                 global $current_user;
2005                 global $app_strings;
2006                 global $timedate;
2007
2008                 $beans = array();
2009                 $bean = new InboundEmail();
2010                 $bean->retrieve($ieId);
2011                 $beans[] = $bean;
2012                 //$beans = $this->retrieveAllByGroupId($current_user->id, true);
2013
2014                 $subject = urldecode($subject);
2015
2016                 $criteria  = "";
2017                 $criteria .= (!empty($subject)) ? 'SUBJECT '.from_html($subject).'' : "";
2018                 $criteria .= (!empty($from)) ? ' FROM "'.$from.'"' : "";
2019                 $criteria .= (!empty($to)) ? ' FROM "'.$to.'"' : "";
2020                 $criteria .= (!empty($body)) ? ' TEXT "'.$body.'"' : "";
2021                 $criteria .= (!empty($dateFrom)) ? ' SINCE "'.$timedate->fromString($dateFrom)->format('d-M-Y').'"' : "";
2022                 $criteria .= (!empty($dateTo)) ? ' BEFORE "'.$timedate->fromString($dateTo)->format('d-M-Y').'"' : "";
2023                 //$criteria .= (!empty($from)) ? ' FROM "'.$from.'"' : "";
2024
2025                 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
2026
2027                 $out = array();
2028
2029                 foreach($beans as $bean) {
2030                         if(!in_array($bean->id, $showFolders)) {
2031                                 continue;
2032                         }
2033
2034                         $GLOBALS['log']->info("*** INBOUNDEMAIL: searching [ {$bean->name} ] for [ {$subject}{$from}{$to}{$body}{$dateFrom}{$dateTo} ]");
2035                         $group = (!$bean->is_personal) ? 'group.' : '';
2036                         $bean->connectMailServer();
2037                         $mailboxes = $bean->getMailboxes(true);
2038                         if (!in_array('INBOX', $mailboxes)) {
2039                                 $mailboxes[] = 'INBOX';
2040                         }
2041                         $totalHits = 0;
2042
2043                         foreach($mailboxes as $mbox) {
2044                                 $bean->mailbox = $mbox;
2045                                 $searchOverviews = array();
2046                                 if ($bean->protocol == 'pop3') {
2047                                         $pop3Criteria = "SELECT * FROM email_cache WHERE ie_id = '{$bean->id}' AND mbox = '{$mbox}'";
2048                                         $pop3Criteria .= (!empty($subject)) ? ' AND subject like "%'.$bean->db->quote($subject).'%"' : "";
2049                                         $pop3Criteria .= (!empty($from)) ? ' AND fromaddr like "%'.$from.'%"' : "";
2050                                         $pop3Criteria .= (!empty($to)) ? ' AND toaddr like "%'.$to.'%"' : "";
2051                                         $pop3Criteria .= (!empty($dateFrom)) ? ' AND senddate > "'.$dateFrom.'"' : "";
2052                                         $pop3Criteria .= (!empty($dateTo)) ? ' AND senddate < "'.$dateTo.'"' : "";
2053                                         $GLOBALS['log']->info("*** INBOUNDEMAIL: searching [ {$mbox} ] using criteria [ {$pop3Criteria} ]");
2054
2055                                         $r = $bean->db->query($pop3Criteria);
2056                                         while($a = $bean->db->fetchByAssoc($r)) {
2057                                                 $overview = new Overview();
2058
2059                                                 foreach($a as $k => $v) {
2060                                                         $k=strtolower($k);
2061                                                         switch($k) {
2062                                                                 case "imap_uid":
2063                                                                         $overview->imap_uid = $v;
2064                                                                         $overview->uid = $a['message_id'];
2065                                                                 break;
2066                                                                 case "toaddr":
2067                                                                         $overview->to = from_html($v);
2068                                                                 break;
2069
2070                                                                 case "fromaddr":
2071                                                                         $overview->from = from_html($v);
2072                                                                 break;
2073
2074                                                                 case "mailsize":
2075                                                                         $overview->size = $v;
2076                                                                 break;
2077
2078                                                                 case "senddate":
2079                                                                         $overview->date = $timedate->fromString($v)->format('r');
2080                                                                 break;
2081
2082                                                                 default:
2083                                                                         $overview->$k = from_html($v);
2084                                                                 break;
2085                                                         } // sqitch
2086                                                 } // foreach
2087                                                 $searchOverviews[] = $overview;
2088                                         } // while
2089                                 } else {
2090                                         $bean->connectMailServer();
2091                                         $searchResult = imap_search($bean->conn, $criteria, SE_UID);
2092                                         if (!empty($searchResult)) {
2093                                                 $searchOverviews = imap_fetch_overview($bean->conn, implode(',', $searchResult), FT_UID);
2094                                         } // if
2095                                 } // else
2096                                 $numHits = count($searchOverviews);
2097
2098                                 if($numHits > 0) {
2099                                         $totalHits = $totalHits + $numHits;
2100                                         $ret = $bean->sortFetchedOverview($searchOverviews, 'date', 'desc', true);
2101                                         $mbox = "{$bean->id}.SEARCH";
2102                                         $out = array_merge($out, $bean->displayFetchedSortedListXML($ret, $mbox, false));
2103                                 }
2104                         }
2105                 }
2106
2107                 $metadata = array();
2108                 $metadata['mbox'] = $app_strings['LBL_EMAIL_SEARCH_RESULTS_TITLE'];
2109                 $metadata['ieId'] = $this->id;
2110                 $metadata['name'] = $this->name;
2111                 $metadata['unreadChecked'] = ($current_user->getPreference('showUnreadOnly', 'Emails') == 1) ? 'CHECKED' : '';
2112                 $metadata['out'] = $out;
2113
2114                 return $metadata;
2115         }
2116
2117         /**
2118          * repairs the encrypted password for a given I-E account
2119          * @return bool True on success
2120          */
2121         function repairAccount() {
2122
2123                 for($i=0; $i<3; $i++) {
2124                         if($i != 0) { // decode is performed on retrieve already
2125                                 $this->email_password = blowfishDecode(blowfishGetKey('InboundEmail'), $this->email_password);
2126                         }
2127
2128                         if($this->connectMailserver() == 'true') {
2129                                 $this->save(); // save decoded password (is encoded on save())
2130                                 return true;
2131                         }
2132                 }
2133
2134                 return false;
2135         }
2136
2137         /**
2138          * soft deletes a User's personal inbox
2139          * @param string id I-E id
2140          * @param string user_name User name of User in focus, NOT current_user
2141          * @return bool True on success
2142          */
2143         function deletePersonalEmailAccount($id, $user_name) {
2144                 $q = "SELECT ie.id FROM inbound_email ie LEFT JOIN users u ON ie.group_id = u.id WHERE u.user_name = '{$user_name}'";
2145                 $r = $this->db->query($q, true);
2146
2147                 while($a = $this->db->fetchByAssoc($r)) {
2148                         if(!empty($a) && $a['id'] == $id) {
2149                                 $this->retrieve($id);
2150                                 $this->deleted = 1;
2151                                 $this->save();
2152                                 return true;
2153                         }
2154                 }
2155                 return false;
2156         }
2157
2158         function getTeamSetIdForTeams($teamIds) {
2159                 if(!is_array($teamIds)){
2160                    $teamIds = array($teamIds);
2161                 } // if
2162                 $teamSet = new TeamSet();
2163                 $team_set_id = $teamSet->addTeams($teamIds);
2164                 return $team_set_id;
2165         } // fn
2166
2167         /**
2168          * Saves Personal Inbox settings for Users
2169          * @param string userId ID of user to assign all emails for this account
2170          * @param strings userName Name of account, for Sugar purposes
2171          * @param bool forceSave Default true.  Flag to save errored settings.
2172          * @return boolean true on success, false on fail
2173          */
2174         function savePersonalEmailAccount($userId = '', $userName = '', $forceSave=true) {
2175                 $groupId = $userId;
2176                 $accountExists = false;
2177                 if(isset($_REQUEST['ie_id']) && !empty($_REQUEST['ie_id'])) {
2178                         $this->retrieve($_REQUEST['ie_id']);
2179                         $accountExists = true;
2180                 }
2181                 $ie_name = $_REQUEST['ie_name'];
2182
2183                 $this->is_personal = 1;
2184                 $this->name = $ie_name;
2185                 $this->group_id = $groupId;
2186                 $this->status = $_REQUEST['ie_status'];
2187                 $this->server_url = trim($_REQUEST['server_url']);
2188                 $this->email_user = trim($_REQUEST['email_user']);
2189                 if(!empty($_REQUEST['email_password'])) {
2190                     $this->email_password = html_entity_decode($_REQUEST['email_password'], ENT_QUOTES);
2191                 }
2192                 $this->port = trim($_REQUEST['port']);
2193                 $this->protocol = $_REQUEST['protocol'];
2194                 if ($this->protocol == "pop3") {
2195                         $_REQUEST['mailbox'] = "INBOX";
2196                 }
2197                 $this->mailbox = $_REQUEST['mailbox'];
2198                 $this->mailbox_type = 'pick'; // forcing this
2199
2200
2201                 if(isset($_REQUEST['ssl']) && $_REQUEST['ssl'] == 1) { $useSsl = true; }
2202                 else $useSsl = false;
2203                 $this->service = '::::::::::';
2204
2205                 if($forceSave) {
2206                         $id = $this->save(); // saving here to prevent user from having to re-enter all the info in case of error
2207                         $this->retrieve($id);
2208                 }
2209
2210                 $this->protocol = $_REQUEST['protocol']; // need to set this again since we safe the "service" string to empty explode values
2211                 $opts = $this->getSessionConnectionString($this->server_url, $this->email_user, $this->port, $this->protocol);
2212                 $detectedOpts = $this->findOptimumSettings($useSsl);
2213
2214                 //If $detectedOpts is empty, there was an error connecting, so clear $opts. If $opts was empty, use $detectedOpts
2215                 if (empty($opts) || empty($detectedOpts) || (empty($detectedOpts['good']) && empty($detectedOpts['serial'])))
2216                 {
2217                   $opts = $detectedOpts;
2218                 }
2219                 $delimiter = $this->getSessionInboundDelimiterString($this->server_url, $this->email_user, $this->port, $this->protocol);
2220
2221                 if(isset($opts['serial']) && !empty($opts['serial'])) {
2222                         $this->service = $opts['serial'];
2223                         if(isset($_REQUEST['mark_read']) && $_REQUEST['mark_read'] == 1) {
2224                                 $this->delete_seen = 0;
2225                         } else {
2226                                 $this->delete_seen = 1;
2227                         }
2228
2229                         // handle stored_options serialization
2230                         if(isset($_REQUEST['only_since']) && $_REQUEST['only_since'] == 1) {
2231                                 $onlySince = true;
2232                         } else {
2233                                 $onlySince = false;
2234                         }
2235
2236                         $focusUser = new User();
2237                         $focusUser->retrieve($groupId);
2238                         $mailerId = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : "";
2239
2240                         $oe = new OutboundEmail();
2241                         $oe->getSystemMailerSettings($focusUser, $mailerId);
2242
2243                         $stored_options = array();
2244                         $stored_options['from_name'] = trim($_REQUEST['from_name']);
2245                         $stored_options['from_addr'] = trim($_REQUEST['from_addr']);
2246                         $stored_options['reply_to_addr'] = trim($_REQUEST['reply_to_addr']);
2247
2248                         if (!$this->isPop3Protocol()) {
2249                                 $stored_options['trashFolder'] = (isset($_REQUEST['trashFolder']) ? trim($_REQUEST['trashFolder']) : "");
2250                                 $stored_options['sentFolder'] = (isset($_REQUEST['sentFolder']) ? trim($_REQUEST['sentFolder']) : "");
2251                         } // if
2252                         $stored_options['only_since'] = $onlySince;
2253                         $stored_options['filter_domain'] = '';
2254                         $storedOptions['folderDelimiter'] = $delimiter;
2255                         $stored_options['outbound_email'] = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : $oe->id;
2256                         $this->stored_options = base64_encode(serialize($stored_options));
2257
2258                         $ieId = $this->save();
2259
2260                         //If this is the first personal account the user has setup mark it as default for them.
2261                         $currentIECount = $this->getUserPersonalAccountCount($focusUser);
2262                         if($currentIECount == 1)
2263                             $this->setUsersDefaultOutboundServerId($focusUser, $ieId);
2264
2265                         return true;
2266                 } else {
2267                         // could not find opts, no save
2268                         $GLOBALS['log']->debug('-----> InboundEmail could not find optimums for User: '.$ie_name);
2269                         return false;
2270                 }
2271         }
2272         /**
2273          * Determines if this instance of I-E is for a Group Inbox or Personal Inbox
2274          */
2275         function handleIsPersonal() {
2276                 $qp = 'SELECT users.id, users.user_name FROM users WHERE users.is_group = 0 AND users.deleted = 0 AND users.status = \'active\' AND users.id = \''.$this->group_id.'\'';
2277                 $rp = $this->db->query($qp, true);
2278                 $personalBox = array();
2279                 while($ap = $this->db->fetchByAssoc($rp)) {
2280                         $personalBox[] = array($ap['id'], $ap['user_name']);
2281                 }
2282                 if(count($personalBox) > 0) {
2283                         return true;
2284                 } else {
2285                         return false;
2286                 }
2287         }
2288
2289         function getUserNameFromGroupId() {
2290                 $r = $this->db->query('SELECT users.user_name FROM users WHERE deleted=0 AND id=\''.$this->group_id.'\'', true);
2291                 while($a = $this->db->fetchByAssoc($r)) {
2292                         return $a['user_name'];
2293                 }
2294                 return '';
2295         }
2296
2297         function getFoldersListForMailBox() {
2298                 $return = array();
2299                 $foldersList = $this->getSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol);
2300                 if (empty($foldersList)) {
2301                         global $mod_strings;
2302                         $msg = $this->connectMailserver(true);
2303                         if (strpos($msg, "successfully")) {
2304                                 $foldersList = $this->getSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol);
2305                                 $return['status'] = true;
2306                                 $return['foldersList'] = $foldersList;
2307                                 $return['statusMessage'] = "";
2308                         } else {
2309                                 $return['status'] = false;
2310                                 $return['statusMessage'] = $msg;
2311                         } // else
2312                 } else {
2313                         $return['status'] = true;
2314                         $return['foldersList'] = $foldersList;
2315                         $return['statusMessage'] = "";
2316                 }
2317                 return $return;
2318         } // fn
2319         /**
2320          * Programatically determines best-case settings for imap_open()
2321          */
2322         function findOptimumSettings($useSsl=false, $user='', $pass='', $server='', $port='', $prot='', $mailbox='') {
2323                 global $mod_strings;
2324                 $serviceArr = array();
2325                 $returnService = array();
2326                 $badService = array();
2327                 $goodService = array();
2328                 $errorArr = array();
2329                 $raw = array();
2330                 $retArray = array(      'good' => $goodService,
2331                                                         'bad' => $badService,
2332                                                         'err' => $errorArr);
2333
2334                 if(!function_exists('imap_open')) {
2335                         $retArray['err'][0] = $mod_strings['ERR_NO_IMAP'];
2336                         return $retArray;
2337                 }
2338
2339                 imap_errors(); // clearing error stack
2340                 error_reporting(0); // turn off notices from IMAP
2341
2342                 if(isset($_REQUEST['ssl']) && $_REQUEST['ssl'] == 1) {
2343                         $useSsl = true;
2344                 }
2345
2346                 $exServ = explode('::', $this->service);
2347                 $service = '/'.$exServ[1];
2348
2349                 $nonSsl = array('both-secure'                   => '/notls/novalidate-cert/secure',
2350                                                 'both'                                  => '/notls/novalidate-cert',
2351                                                 'nocert-secure'                 => '/novalidate-cert/secure',
2352                                                 'nocert'                                => '/novalidate-cert',
2353                                                 'notls-secure'                  => '/notls/secure',
2354                                                 'secure'                                => '/secure', // for POP3 servers that force CRAM-MD5
2355                                                 'notls'                                 => '/notls',
2356                                                 'none'                                  => '', // try default nothing
2357                                         );
2358                 $ssl = array(
2359                                                 'ssl-both-on-secure'    => '/ssl/tls/validate-cert/secure',
2360                                                 'ssl-both-on'                   => '/ssl/tls/validate-cert',
2361                                                 'ssl-cert-secure'               => '/ssl/validate-cert/secure',
2362                                                 'ssl-cert'                              => '/ssl/validate-cert',
2363                                                 'ssl-tls-secure'                => '/ssl/tls/secure',
2364                                                 'ssl-tls'                               => '/ssl/tls',
2365                                                 'ssl-both-off-secure'   => '/ssl/notls/novalidate-cert/secure',
2366                                                 'ssl-both-off'                  => '/ssl/notls/novalidate-cert',
2367                                                 'ssl-nocert-secure'             => '/ssl/novalidate-cert/secure',
2368                                                 'ssl-nocert'                    => '/ssl/novalidate-cert',
2369                                                 'ssl-notls-secure'              => '/ssl/notls/secure',
2370                                                 'ssl-notls'                             => '/ssl/notls',
2371                                                 'ssl-secure'                    => '/ssl/secure',
2372                                                 'ssl-none'                              => '/ssl',
2373                                         );
2374
2375                 if(isset($user) && !empty($user) && isset($pass) && !empty($pass)) {
2376                         $this->email_password = $pass;
2377                         $this->email_user = $user;
2378                         $this->server_url = $server;
2379                         $this->port = $port;
2380                         $this->protocol = $prot;
2381                         $this->mailbox = $mailbox;
2382                 }
2383
2384                 // in case we flip from IMAP to POP3
2385                 if($this->protocol == 'pop3')
2386                   $this->mailbox = 'INBOX';
2387
2388                 //If user has selected multiple mailboxes, we only need to test the first mailbox for the connection string.
2389                 $a_mailbox = explode(",", $this->mailbox);
2390                 $tmpMailbox = isset($a_mailbox[0]) ? $a_mailbox[0] : "";
2391
2392                 if($useSsl == true)
2393                 {
2394                         foreach($ssl as $k => $service)
2395                         {
2396                                 $returnService[$k] = 'foo'.$service;
2397                                 $serviceArr[$k] = '{'.$this->server_url.':'.$this->port.'/service='.$this->protocol.$service.'}'.$tmpMailbox;
2398                         }
2399                 }
2400                 else
2401                 {
2402                         foreach($nonSsl as $k => $service)
2403                         {
2404                                 $returnService[$k] = 'foo'.$service;
2405                                 $serviceArr[$k] = '{'.$this->server_url.':'.$this->port.'/service='.$this->protocol.$service.'}'.$tmpMailbox;
2406                         }
2407                 }
2408
2409                 $GLOBALS['log']->debug('---------------STARTING FINDOPTIMUMS LOOP----------------');
2410                 $l = 1;
2411
2412                 //php imap library will capture c-client library warnings as errors causing good connections to be ignored.
2413                 //Check against known warnings to ensure good connections are used.
2414                 $acceptableWarnings = array("SECURITY PROBLEM: insecure server advertised AUTH=PLAIN", //c-client auth_pla.c
2415                                                 "Mailbox is empty");
2416                 $login = $this->email_user;
2417                 $passw = $this->email_password;
2418                 $foundGoodConnection = false;
2419                 foreach($serviceArr as $k => $serviceTest) {
2420                         $errors = '';
2421                         $alerts = '';
2422                         $GLOBALS['log']->debug($l.': I-E testing string: '.$serviceTest);
2423
2424                         // open the connection and try the test string
2425                         $this->conn = imap_open($serviceTest, $login, $passw);
2426
2427                         if(($errors = imap_last_error()) || ($alerts = imap_alerts())) {
2428                                 if($errors == 'Too many login failures' || $errors == '[CLOSED] IMAP connection broken (server response)') { // login failure means don't bother trying the rest
2429                                         $GLOBALS['log']->debug($l.': I-E failed using ['.$serviceTest.']');
2430                                         $retArray['err'][$k] = $mod_strings['ERR_BAD_LOGIN_PASSWORD'];
2431                                         $retArray['bad'][$k] = $serviceTest;
2432                                         $GLOBALS['log']->debug($l.': I-E ERROR: $ie->findOptimums() failed due to bad user credentials for user login: '.$this->email_user);
2433                                         return $retArray;
2434                                 } elseif( in_array($errors, $acceptableWarnings, TRUE)) { // false positive
2435                                         $GLOBALS['log']->debug($l.': I-E found good connection but with warnings ['.$serviceTest.'] Errors:' . $errors);
2436                                         $retArray['good'][$k] = $returnService[$k];
2437                                         $foundGoodConnection = true;
2438                                 }
2439                                 else {
2440                                         $GLOBALS['log']->debug($l.': I-E failed using ['.$serviceTest.'] - error: '.$errors);
2441                                         $retArray['err'][$k] = $errors;
2442                                         $retArray['bad'][$k] = $serviceTest;
2443                                 }
2444                         } else {
2445                                 $GLOBALS['log']->debug($l.': I-E found good connect using ['.$serviceTest.']');
2446                                 $retArray['good'][$k] = $returnService[$k];
2447                                 $foundGoodConnection = true;
2448                         }
2449
2450                         if(is_resource($this->conn)) {
2451                                 if (!$this->isPop3Protocol()) {
2452                                         $serviceTest = str_replace("INBOX", "", $serviceTest);
2453                                         $boxes = imap_getmailboxes($this->conn, $serviceTest, "*");
2454                                         $delimiter = '.';
2455                                         // clean MBOX path names
2456                                         foreach($boxes as $k => $mbox) {
2457                                                 $raw[] = $mbox->name;
2458                                                 if ($mbox->delimiter) {
2459                                                         $delimiter = $mbox->delimiter;
2460                                                 } // if
2461                                         } // foreach
2462                                         $this->setSessionInboundDelimiterString($this->server_url, $this->email_user, $this->port, $this->protocol, $delimiter);
2463                                 } // if
2464
2465                                 if(!imap_close($this->conn)) $GLOBALS['log']->debug('imap_close() failed!');
2466                         }
2467
2468                         $GLOBALS['log']->debug($l.': I-E clearing error and alert stacks.');
2469                         imap_errors(); // clear stacks
2470                         imap_alerts();
2471                         // If you find a good connection, then don't do any further testing to find URL
2472                         if ($foundGoodConnection) {
2473                                 break;
2474                         } // if
2475                         $l++;
2476                 }
2477                 $GLOBALS['log']->debug('---------------end FINDOPTIMUMS LOOP----------------');
2478
2479                 if(!empty($retArray['good'])) {
2480                         $newTls                         = '';
2481                         $newCert                        = '';
2482                         $newSsl                         = '';
2483                         $newNotls                       = '';
2484                         $newNovalidate_cert     = '';
2485                         $good = array_pop($retArray['good']); // get most complete string
2486                         $exGood = explode('/', $good);
2487                         foreach($exGood as $v) {
2488                                 switch($v) {
2489                                         case 'ssl':
2490                                                 $newSsl = 'ssl';
2491                                         break;
2492                                         case 'tls':
2493                                                 $newTls = 'tls';
2494                                         break;
2495                                         case 'notls':
2496                                                 $newNotls = 'notls';
2497                                         break;
2498                                         case 'cert':
2499                                                 $newCert = 'validate-cert';
2500                                         break;
2501                                         case 'novalidate-cert':
2502                                                 $newNovalidate_cert = 'novalidate-cert';
2503                                         break;
2504                                         case 'secure':
2505                                                 $secure = 'secure';
2506                                         break;
2507                                 }
2508                         }
2509
2510                         $goodStr['serial'] = $newTls.'::'.$newCert.'::'.$newSsl.'::'.$this->protocol.'::'.$newNovalidate_cert.'::'.$newNotls.'::'.$secure;
2511                         $goodStr['service'] = $good;
2512                         $testConnectString = str_replace('foo','', $good);
2513                         $testConnectString = '{'.$this->server_url.':'.$this->port.'/service='.$this->protocol.$testConnectString.'}';
2514                         $this->setSessionConnectionString($this->server_url, $this->email_user, $this->port, $this->protocol, $goodStr);
2515                         $i = 0;
2516                         foreach($raw as $mbox)
2517                         {
2518                                 $raw[$i] = str_replace($testConnectString, "", $GLOBALS['locale']->translateCharset($mbox, "UTF7-IMAP", "UTF8" ));
2519                                 $i++;
2520                         } // foreach
2521                         sort($raw);
2522                         $this->setSessionInboundFoldersString($this->server_url, $this->email_user, $this->port, $this->protocol, implode(",", $raw));
2523                         return $goodStr;
2524                 } else {
2525                         return false;
2526                 }
2527         }
2528
2529         function getSessionConnectionString($server_url, $email_user, $port, $protocol) {
2530                 $sessionConnectionString = $server_url . $email_user . $port . $protocol;
2531                 return (isset($_SESSION[$sessionConnectionString]) ? $_SESSION[$sessionConnectionString] : "");
2532         }
2533
2534         function setSessionConnectionString($server_url, $email_user, $port, $protocol, $goodStr) {
2535                 $sessionConnectionString = $server_url . $email_user . $port . $protocol;
2536                 $_SESSION[$sessionConnectionString] = $goodStr;
2537         }
2538
2539         function getSessionInboundDelimiterString($server_url, $email_user, $port, $protocol) {
2540                 $sessionInboundDelimiterString = $server_url . $email_user . $port . $protocol . "delimiter";
2541                 return (isset($_SESSION[$sessionInboundDelimiterString]) ? $_SESSION[$sessionInboundDelimiterString] : "");
2542         }
2543
2544         function setSessionInboundDelimiterString($server_url, $email_user, $port, $protocol, $delimiter) {
2545                 $sessionInboundDelimiterString = $server_url . $email_user . $port . $protocol . "delimiter";
2546                 $_SESSION[$sessionInboundDelimiterString] = $delimiter;
2547         }
2548
2549         function getSessionInboundFoldersString($server_url, $email_user, $port, $protocol) {
2550                 $sessionInboundFoldersListString = $server_url . $email_user . $port . $protocol . "foldersList";
2551                 return (isset($_SESSION[$sessionInboundFoldersListString]) ? $_SESSION[$sessionInboundFoldersListString] : "");
2552         }
2553
2554         function setSessionInboundFoldersString($server_url, $email_user, $port, $protocol, $foldersList) {
2555                 $sessionInboundFoldersListString = $server_url . $email_user . $port . $protocol . "foldersList";
2556                 $_SESSION[$sessionInboundFoldersListString] = $foldersList;
2557         }
2558
2559         /**
2560          * Checks for duplicate Group User names when creating a new one at save()
2561          * @return      GUID            returns GUID of Group User if user_name match is
2562          * found
2563          * @return      boolean         false if NO DUPE IS FOUND
2564          */
2565         function groupUserDupeCheck() {
2566                 $q = "SELECT u.id FROM users u WHERE u.deleted=0 AND u.is_group=1 AND u.user_name = '".$this->name."'";
2567                 $r = $this->db->query($q, true);
2568                 $uid = '';
2569                 while($a = $this->db->fetchByAssoc($r)) {
2570                         $uid = $a['id'];
2571                 }
2572
2573                 if(strlen($uid) > 0) {
2574                         return $uid;
2575                 } else {
2576                         return false;
2577                 }
2578         }
2579
2580         /**
2581          * Returns <option> markup with the contents of Group users
2582          * @param array $groups default empty array
2583          * @return string HTML options
2584          */
2585         function getGroupsWithSelectOptions($groups = array()) {
2586                 $r = $this->db->query('SELECT id, user_name FROM users WHERE users.is_group = 1 AND deleted = 0', true);
2587                 if(is_resource($r)) {
2588                         while($a = $this->db->fetchByAssoc($r)) {
2589                                 $groups[$a['id']] = $a['user_name'];
2590                         }
2591                 }
2592
2593                 $selectOptions = get_select_options_with_id_separate_key($groups, $groups, $this->group_id);
2594                 return $selectOptions;
2595         }
2596
2597         /**
2598          * handles auto-responses to inbound emails
2599          *
2600          * @param object email Email passed as reference
2601          */
2602         function handleAutoresponse(&$email, &$contactAddr) {
2603                 if($this->template_id) {
2604                         $GLOBALS['log']->debug('found auto-reply template id - prefilling and mailing response');
2605
2606                         if($this->getAutoreplyStatus($contactAddr)
2607                         && $this->checkOutOfOffice($email->name)
2608                         && $this->checkFilterDomain($email)) { // if we haven't sent this guy 10 replies in 24hours
2609
2610                                 if(!empty($this->stored_options)) {
2611                                         $storedOptions = unserialize(base64_decode($this->stored_options));
2612                                 }
2613                                 // get FROM NAME
2614                                 if(!empty($storedOptions['from_name'])) {
2615                                         $from_name = $storedOptions['from_name'];
2616                                         $GLOBALS['log']->debug('got from_name from storedOptions: '.$from_name);
2617                                 } else { // use system default
2618                                         $rName = $this->db->query('SELECT value FROM config WHERE name = \'fromname\'', true);
2619                                         if(is_resource($rName)) {
2620                                                 $aName = $this->db->fetchByAssoc($rName);
2621                                         }
2622                                         if(!empty($aName['value'])) {
2623                                                 $from_name = $aName['value'];
2624                                         } else {
2625                                                 $from_name = '';
2626                                         }
2627                                 }
2628                                 // get FROM ADDRESS
2629                                 if(!empty($storedOptions['from_addr'])) {
2630                                         $from_addr = $storedOptions['from_addr'];
2631                                 } else {
2632                                         $rAddr = $this->db->query('SELECT value FROM config WHERE name = \'fromaddress\'', true);
2633                                         if(is_resource($rAddr)) {
2634                                                 $aAddr = $this->db->fetchByAssoc($rAddr);
2635                                         }
2636                                         if(!empty($aAddr['value'])) {
2637                                                 $from_addr = $aAddr['value'];
2638                                         } else {
2639                                                 $from_addr = '';
2640                                         }
2641                                 }
2642
2643                                 $replyToName = (!empty($storedOptions['reply_to_name']))? from_html($storedOptions['reply_to_name']) :$from_name ;
2644                                 $replyToAddr = (!empty($storedOptions['reply_to_addr'])) ? $storedOptions['reply_to_addr'] : $from_addr;
2645
2646
2647                                 if(!empty($email->reply_to_email)) {
2648                                         $to[0]['email'] = $email->reply_to_email;
2649                                 } else {
2650                                         $to[0]['email'] = $email->from_addr;
2651                                 }
2652                                 // handle to name: address, prefer reply-to
2653                                 if(!empty($email->reply_to_name)) {
2654                                         $to[0]['display'] = $email->reply_to_name;
2655                                 } elseif(!empty($email->from_name)) {
2656                                         $to[0]['display'] = $email->from_name;
2657                                 }
2658
2659                                 $et = new EmailTemplate();
2660                                 $et->retrieve($this->template_id);
2661                                 if(empty($et->subject))         { $et->subject = ''; }
2662                                 if(empty($et->body))            { $et->body = ''; }
2663                                 if(empty($et->body_html))       { $et->body_html = ''; }
2664
2665                                 $reply = new Email();
2666                                 $reply->type                            = 'out';
2667                                 $reply->to_addrs                        = $to[0]['email'];
2668                                 $reply->to_addrs_arr            = $to;
2669                                 $reply->cc_addrs_arr            = array();
2670                                 $reply->bcc_addrs_arr           = array();
2671                                 $reply->from_name                       = $from_name;
2672                                 $reply->from_addr                       = $from_addr;
2673                                 $reply->name                            = $et->subject;
2674                                 $reply->description                     = $et->body;
2675                                 $reply->description_html        = $et->body_html;
2676                                 $reply->reply_to_name           = $replyToName;
2677                                 $reply->reply_to_addr           = $replyToAddr;
2678
2679                                 $GLOBALS['log']->debug('saving and sending auto-reply email');
2680                                 //$reply->save(); // don't save the actual email.
2681                                 $reply->send();
2682                                 $this->setAutoreplyStatus($contactAddr);
2683                         } else {
2684                                 $GLOBALS['log']->debug('InboundEmail: auto-reply threshold reached for email ('.$contactAddr.') - not sending auto-reply');
2685                         }
2686                 }
2687         }
2688
2689         function handleCaseAssignment($email) {
2690                 $c = new aCase();
2691                 if($caseId = $this->getCaseIdFromCaseNumber($email->name, $c)) {
2692                         $c->retrieve($caseId);
2693                         $email->retrieve($email->id);
2694             //assign the case info to parent id and parent type so that the case can be linked to the email on Email Save
2695                         $email->parent_type = "Cases";
2696                         $email->parent_id = $caseId;
2697                         // assign the email to the case owner
2698                         $email->assigned_user_id = $c->assigned_user_id;
2699                         $email->save();
2700                         $GLOBALS['log']->debug('InboundEmail found exactly 1 match for a case: '.$c->name);
2701                         return true;
2702                 } // if
2703                 return false;
2704         } // fn
2705
2706         /**
2707          * handles functionality specific to the Mailbox type (Cases, bounced
2708          * campaigns, etc.)
2709          *
2710          * @param object email Email object passed as a reference
2711          * @param object header Header object generated by imap_headerinfo();
2712          */
2713         function handleMailboxType(&$email, &$header) {
2714                 switch($this->mailbox_type) {
2715                         case 'support':
2716                                 $this->handleCaseAssignment($email);
2717                                 break;
2718                         case 'bug':
2719
2720                                 break;
2721
2722                         case 'info':
2723                                 // do something with this?
2724                                 break;
2725                         case 'sales':
2726                                 // do something with leads? we don't have an email_leads table
2727                                 break;
2728                         case 'task':
2729                                 // do something?
2730                                 break;
2731                         case 'bounce':
2732                                 require_once('modules/Campaigns/ProcessBouncedEmails.php');
2733                                 campaign_process_bounced_emails($email, $header);
2734                                 break;
2735                         case 'pick': // do all except bounce handling
2736                                 $GLOBALS['log']->debug('looking for a case for '.$email->name);
2737                                 $this->handleCaseAssignment($email);
2738                                 break;
2739                 }
2740         }
2741
2742         function isMailBoxTypeCreateCase() {
2743                 return ($this->mailbox_type == 'createcase' && !empty($this->groupfolder_id));
2744         } // fn
2745
2746         function handleCreateCase($email, $userId) {
2747                 global $current_user, $mod_strings, $current_language;
2748                 $mod_strings = return_module_language($current_language, "Emails");
2749                 $GLOBALS['log']->debug('In handleCreateCase');
2750                 $c = new aCase();
2751                 $this->getCaseIdFromCaseNumber($email->name, $c);
2752
2753                 if (!$this->handleCaseAssignment($email) && $this->isMailBoxTypeCreateCase()) {
2754                         // create a case
2755                         $GLOBALS['log']->debug('retrieveing email');
2756                         $email->retrieve($email->id);
2757                         $c = new aCase();
2758                         $c->description = $email->description;
2759                         $c->assigned_user_id = $userId;
2760                         $c->name = $email->name;
2761                         $c->status = 'New';
2762                         $c->priority = 'P1';
2763
2764                         if(!empty($email->reply_to_email)) {
2765                                 $contactAddr = $email->reply_to_email;
2766                         } else {
2767                                 $contactAddr = $email->from_addr;
2768                         }
2769
2770                         $GLOBALS['log']->debug('finding related accounts with address ' . $contactAddr);
2771                         if($accountIds = $this->getRelatedId($contactAddr, 'accounts')) {
2772                                 if (sizeof($accountIds) == 1) {
2773                                         $c->account_id = $accountIds[0];
2774
2775                                         $acct = new Account();
2776                                         $acct->retrieve($c->account_id);
2777                                         $c->account_name = $acct->name;
2778                                 } // if
2779                         } // if
2780                         $c->save(true);
2781                         $caseId = $c->id;
2782                         $c = new aCase();
2783                         $c->retrieve($caseId);
2784                         if($c->load_relationship('emails')) {
2785                                 $c->emails->add($email->id);
2786                         } // if
2787                         if($contactIds = $this->getRelatedId($contactAddr, 'contacts')) {
2788                                 if(!empty($contactIds) && $c->load_relationship('contacts')) {
2789                                         $c->contacts->add($contactIds);
2790                                 } // if
2791                         } // if
2792                         $c->email_id = $email->id;
2793                         $email->parent_type = "Cases";
2794                         $email->parent_id = $caseId;
2795                         // assign the email to the case owner
2796                         $email->assigned_user_id = $c->assigned_user_id;
2797                         $email->name = str_replace('%1', $c->case_number, $c->getEmailSubjectMacro()) . " ". $email->name;
2798                         $email->save();
2799                         $GLOBALS['log']->debug('InboundEmail created one case with number: '.$c->case_number);
2800                         $createCaseTemplateId = $this->get_stored_options('create_case_email_template', "");
2801                         if(!empty($this->stored_options)) {
2802                                 $storedOptions = unserialize(base64_decode($this->stored_options));
2803                         }
2804                         if(!empty($createCaseTemplateId)) {
2805                                 $fromName = "";
2806                                 $fromAddress = "";
2807                                 if (!empty($this->stored_options)) {
2808                                         $fromAddress = $storedOptions['from_addr'];
2809                                         $fromName = from_html($storedOptions['from_name']);
2810                                         $replyToName = (!empty($storedOptions['reply_to_name']))? from_html($storedOptions['reply_to_name']) :$fromName ;
2811                                         $replyToAddr = (!empty($storedOptions['reply_to_addr'])) ? $storedOptions['reply_to_addr'] : $fromAddress;
2812                                 } // if
2813                                 $defaults = $current_user->getPreferredEmail();
2814                                 $fromAddress = (!empty($fromAddress)) ? $fromAddress : $defaults['email'];
2815                                 $fromName = (!empty($fromName)) ? $fromName : $defaults['name'];
2816                                 $to[0]['email'] = $contactAddr;
2817
2818                                 // handle to name: address, prefer reply-to
2819                                 if(!empty($email->reply_to_name)) {
2820                                         $to[0]['display'] = $email->reply_to_name;
2821                                 } elseif(!empty($email->from_name)) {
2822                                         $to[0]['display'] = $email->from_name;
2823                                 }
2824
2825                                 $et = new EmailTemplate();
2826                                 $et->retrieve($createCaseTemplateId);
2827                                 if(empty($et->subject))         { $et->subject = ''; }
2828                                 if(empty($et->body))            { $et->body = ''; }
2829                                 if(empty($et->body_html))       { $et->body_html = ''; }
2830
2831                                 $et->subject = "Re:" . " " . str_replace('%1', $c->case_number, $c->getEmailSubjectMacro() . " ". $c->name);
2832
2833                                 $html = trim($email->description_html);
2834                                 $plain = trim($email->description);
2835
2836                                 $email->email2init();
2837                     $email->from_addr = $email->from_addr_name;
2838                     $email->to_addrs = $email->to_addrs_names;
2839                     $email->cc_addrs = $email->cc_addrs_names;
2840                     $email->bcc_addrs = $email->bcc_addrs_names;
2841                     $email->from_name = $email->from_addr;
2842
2843                 $email = $email->et->handleReplyType($email, "reply");
2844                 $ret = $email->et->displayComposeEmail($email);
2845                 $ret['description'] = empty($email->description_html) ?  str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
2846
2847                                 $reply = new Email();
2848                                 $reply->type                            = 'out';
2849                                 $reply->to_addrs                        = $to[0]['email'];
2850                                 $reply->to_addrs_arr            = $to;
2851                                 $reply->cc_addrs_arr            = array();
2852                                 $reply->bcc_addrs_arr           = array();
2853                                 $reply->from_name                       = $fromName;
2854                                 $reply->from_addr                       = $fromAddress;
2855                                 $reply->reply_to_name           = $replyToName;
2856                                 $reply->reply_to_addr           = $replyToAddr;
2857                                 $reply->name                            = $et->subject;
2858                                 $reply->description                     = $et->body . "<div><hr /></div>" .  $email->description;
2859                                 if (!$et->text_only) {
2860                                         $reply->description_html        = $et->body_html .  "<div><hr /></div>" . $email->description;
2861                                 }
2862                                 $GLOBALS['log']->debug('saving and sending auto-reply email');
2863                                 //$reply->save(); // don't save the actual email.
2864                                 $reply->send();
2865                         } // if
2866
2867                 } else {
2868                         if(!empty($email->reply_to_email)) {
2869                                 $contactAddr = $email->reply_to_email;
2870                         } else {
2871                                 $contactAddr = $email->from_addr;
2872                         }
2873                         $this->handleAutoresponse($email, $contactAddr);
2874                 }
2875
2876         } // fn
2877
2878         /**
2879          * handles linking contacts, accounts, etc. to an email
2880          *
2881          * @param object Email bean to be linked against
2882          * @return string contactAddr is the email address of the sender
2883          */
2884         function handleLinking(&$email) {
2885                 // link email to an User if emails match TO addr
2886                 if($userIds = $this->getRelatedId($email->to_addrs, 'users')) {
2887                         $GLOBALS['log']->debug('I-E linking email to User');
2888                         // link the user to the email
2889                         $email->load_relationship('users');
2890                         $email->users->add($userIds);
2891                 }
2892
2893                 // link email to a Contact, Lead, or Account if the emails match
2894                 // give precedence to REPLY-TO above FROM
2895                 if(!empty($email->reply_to_email)) {
2896                         $contactAddr = $email->reply_to_email;
2897                 } else {
2898                         $contactAddr = $email->from_addr;
2899                 }
2900
2901                 // Samir Gandhi : 12/06/07
2902                 // This changes has been done because the linking was done only with the from address and
2903                 // not with to address
2904                 $relationShipAddress = $contactAddr;
2905                 if (empty($relationShipAddress)) {
2906                         $relationShipAddress .= $email->to_addrs;
2907                 } else {
2908                         $relationShipAddress = $relationShipAddress . "," . $email->to_addrs;
2909                 }
2910                 if($leadIds = $this->getRelatedId($relationShipAddress, 'leads')) {
2911                         $GLOBALS['log']->debug('I-E linking email to Lead');
2912                         $email->load_relationship('leads');
2913                         $email->leads->add($leadIds);
2914
2915                         foreach($leadIds as $leadId) {
2916                                 $lead = new Lead();
2917                                 $lead->retrieve($leadId);
2918                                 $lead->load_relationship('emails');
2919                                 $lead->emails->add($email->id);
2920                         }
2921                 }
2922
2923                 if($contactIds = $this->getRelatedId($relationShipAddress, 'contacts')) {
2924                         $GLOBALS['log']->debug('I-E linking email to Contact');
2925                         // link the contact to the email
2926                         $email->load_relationship('contacts');
2927                         $email->contacts->add($contactIds);
2928                 }
2929
2930                 if($accountIds = $this->getRelatedId($relationShipAddress, 'accounts')) {
2931                         $GLOBALS['log']->debug('I-E linking email to Account');
2932                         // link the account to the email
2933                         $email->load_relationship('accounts');
2934                         $email->accounts->add($accountIds);
2935
2936                         /* cn: bug 9171 another cause of dying I-E - bad linking
2937                         foreach($accountIds as $accountId) {
2938                                 $GLOBALS['log']->debug('I-E reverse-linking Accounts to Emails');
2939                                 $acct = new Account();
2940                                 $acct->retrieve($accountId);
2941                                 $acct->load_relationship('emails');
2942                                 $acct->account_emails->add($email->id);
2943                         }
2944                         */
2945                 }
2946                 return $contactAddr;
2947         }
2948
2949         /**
2950          * Gets part by following breadcrumb path
2951          * @param string $bc the breadcrumb string in format (1.1.1)
2952          * @param array parts the root level parts array
2953          */
2954         protected function getPartByPath($bc, $parts)
2955         {
2956                 if(strstr($bc,'.')) {
2957                         $exBc = explode('.', $bc);
2958                 } else {
2959                         $exBc = array($bc);
2960                 }
2961
2962                 foreach($exBc as $step) {
2963                     if(empty($parts)) return false;
2964                     $res = $parts[$step-1]; // MIME starts with 1, array starts with 0
2965                     if(!empty($res->parts)) {
2966                         $parts = $res->parts;
2967                     } else {
2968                         $parts = false;
2969                     }
2970                 }
2971                 return $res;
2972         }
2973
2974         /**
2975          * takes a breadcrumb and returns the encoding at that level
2976          * @param       string bc the breadcrumb string in format (1.1.1)
2977          * @param       array parts the root level parts array
2978          * @return      int retInt Int key to transfer encoding (see handleTranserEncoding())
2979          */
2980         function getEncodingFromBreadCrumb($bc, $parts) {
2981                 if(strstr($bc,'.')) {
2982                         $exBc = explode('.', $bc);
2983                 } else {
2984                         $exBc[0] = $bc;
2985                 }
2986
2987                 $depth = count($exBc);
2988
2989                 for($i=0; $i<$depth; $i++) {
2990                         $tempObj[$i] = $parts[($exBc[$i]-1)];
2991                         $retInt = imap_utf8($tempObj[$i]->encoding);
2992                         if(!empty($tempObj[$i]->parts)) {
2993                                 $parts = $tempObj[$i]->parts;
2994                         }
2995                 }
2996                 return $retInt;
2997         }
2998
2999         /**
3000          * retrieves the charset for a given part of an email body
3001          *
3002          * @param string bc target part of the message in format (1.1.1)
3003          * @param array parts 1 level above ROOT array of Objects representing a multipart body
3004          * @return string charset name
3005          */
3006         function getCharsetFromBreadCrumb($bc, $parts)
3007         {
3008                 $tempObj = $this->getPartByPath($bc, $parts);
3009                 // now we have the tempObj at the end of the breadCrumb trail
3010
3011                 if(!empty($tempObj->ifparameters)) {
3012                         foreach($tempObj->parameters as $param) {
3013                                 if(strtolower($param->attribute) == 'charset') {
3014                                         return $param->value;
3015                                 }
3016                         }
3017                 }
3018
3019                 return 'default';
3020         }
3021
3022         /**
3023          * Get the message text from a single mime section, html or plain.
3024          *
3025          * @param string $msgNo
3026          * @param string $section
3027          * @param stdObject $structure
3028          * @return string
3029          */
3030         function getMessageTextFromSingleMimePart($msgNo,$section,$structure)
3031         {
3032             $msgPartTmp = imap_fetchbody($this->conn, $msgNo, $section);
3033             $enc = $this->getEncodingFromBreadCrumb($section, $structure->parts);
3034             $charset = $this->getCharsetFromBreadCrumb($section, $structure->parts);
3035             $msgPartTmp = $this->handleTranserEncoding($msgPartTmp, $enc);
3036             return $this->handleCharsetTranslation($msgPartTmp, $charset);
3037         }
3038
3039         /**
3040          * Givin an existing breadcrumb add a cooresponding offset
3041          *
3042          * @param string $bc
3043          * @param string $offset
3044          * @return string
3045          */
3046         function addBreadCrumbOffset($bc, $offset)
3047         {
3048             if( (empty($bc) || is_null($bc)) && !empty($offset) )
3049                return $offset;
3050
3051             $a_bc = explode(".", $bc);
3052             $a_offset = explode(".",$offset);
3053             if(count($a_bc) < count($a_offset))
3054                $a_bc = array_merge($a_bc,array_fill( count($a_bc), count($a_offset) - count($a_bc), 0));
3055
3056             $results = array();
3057             for($i=0;$i < count($a_bc); $i++)
3058             {
3059                 if(isset($a_offset[$i]))
3060                    $results[] = $a_bc[$i] + $a_offset[$i];
3061                 else
3062                    $results[] = $a_bc[$i];
3063             }
3064             return implode(".", $results);
3065         }
3066
3067         /**
3068          * returns the HTML text part of a multi-part message
3069          *
3070          * @param int msgNo the relative message number for the monitored mailbox
3071          * @param string $type the type of text processed, either 'PLAIN' or 'HTML'
3072          * @return string UTF-8 encoded version of the requested message text
3073          */
3074         function getMessageText($msgNo, $type, $structure, $fullHeader,$clean_email=true, $bcOffset = "") {
3075                 global $sugar_config;
3076
3077                 $msgPart = '';
3078                 $bc = $this->buildBreadCrumbs($structure->parts, $type);
3079                 //Add an offset if specified
3080                 if(!empty($bcOffset))
3081             $bc = $this->addBreadCrumbOffset($bc, $bcOffset);
3082
3083                 if(!empty($bc)) { // multi-part
3084                         // HUGE difference between PLAIN and HTML
3085                         if($type == 'PLAIN') {
3086                                 $msgPart = $this->getMessageTextFromSingleMimePart($msgNo,$bc,$structure);
3087                         } else {
3088                                 // get part of structure that will
3089                                 $msgPartRaw = '';
3090                                 $bcArray = $this->buildBreadCrumbsHTML($structure->parts,$bcOffset);
3091                                 // construct inline HTML/Rich msg
3092                                 foreach($bcArray as $bcArryKey => $bcArr) {
3093                                         foreach($bcArr as $type => $bcTrail) {
3094                                                 if($type == 'html')
3095                                                     $msgPartRaw .= $this->getMessageTextFromSingleMimePart($msgNo,$bcTrail,$structure);
3096                                                  else {
3097                                                         // deal with inline image
3098                                                         $part = $this->getPartByPath($bcTrail, $structure->parts);
3099                                                         if(empty($part) || empty($part->id)) continue;
3100                                                         $partid = substr($part->id, 1, -1); // strip <> around
3101                                                         if(isset($this->inlineImages[$partid])) {
3102                                                                 $imageName = $this->inlineImages[$partid];
3103                                                                 $newImagePath = "class=\"image\" src=\"{$this->imagePrefix}{$imageName}\"";
3104                                                                 $preImagePath = "src=\"cid:$partid\"";
3105                                                                 $msgPartRaw = str_replace($preImagePath, $newImagePath, $msgPartRaw);
3106                                                         }
3107                                                 }
3108                                         }
3109                                 }
3110                                 $msgPart = $msgPartRaw;
3111                         }
3112                 } else { // either PLAIN message type (flowed) or b0rk3d RFC
3113                         // make sure we're working on valid data here.
3114                         if($structure->subtype != $type) {
3115                                 return '';
3116                         }
3117
3118                         $decodedHeader = $this->decodeHeader($fullHeader);
3119
3120                         // now get actual body contents
3121                         $text = imap_body($this->conn, $msgNo);
3122
3123                         $upperCaseKeyDecodeHeader = array();
3124                         if (is_array($decodedHeader)) {
3125                                 $upperCaseKeyDecodeHeader = array_change_key_case($decodedHeader, CASE_UPPER);
3126                         } // if
3127                         if(isset($upperCaseKeyDecodeHeader[strtoupper('Content-Transfer-Encoding')])) {
3128                                 $flip = array_flip($this->transferEncoding);
3129                                 $text = $this->handleTranserEncoding($text, $flip[strtoupper($upperCaseKeyDecodeHeader[strtoupper('Content-Transfer-Encoding')])]);
3130                         }
3131
3132                         if(is_array($upperCaseKeyDecodeHeader['CONTENT-TYPE']) && isset($upperCaseKeyDecodeHeader['CONTENT-TYPE']['charset']) && !empty($upperCaseKeyDecodeHeader['CONTENT-TYPE']['charset'])) {
3133                                 // we have an explicit content type, use it
3134                 $msgPart = $this->handleCharsetTranslation($text, $upperCaseKeyDecodeHeader['CONTENT-TYPE']['charset']);
3135                         } else {
3136                 // make a best guess as to what our content type is
3137                 $msgPart = $this->convertToUtf8($text);
3138             }
3139                 } // end else clause
3140
3141                 $msgPart = $this->customGetMessageText($msgPart);
3142                 /* cn: bug 9176 - htmlEntitites hide XSS attacks. */
3143                 if($type == 'PLAIN') {
3144                     return SugarCleaner::cleanHtml(to_html($msgPart), false);
3145                 }
3146         // Bug 50241: can't process <?xml:namespace .../> properly. Strip <?xml ...> tag first.
3147                 $msgPart = preg_replace("/<\?xml[^>]*>/","",$msgPart);
3148
3149         return SugarCleaner::cleanHtml($msgPart, false);
3150         }
3151
3152         /**
3153          * decodes raw header information and passes back an associative array with
3154          * the important elements key'd by name
3155          * @param header string the raw header
3156          * @return decodedHeader array the associative array
3157          */
3158         function decodeHeader($fullHeader) {
3159                 $decodedHeader = array();
3160                 $exHeaders = explode("\r", $fullHeader);
3161                 if (!is_array($exHeaders)) {
3162                         $exHeaders = explode("\r\n", $fullHeader);
3163                 }
3164                 $quotes = array('"', "'");
3165
3166                 foreach($exHeaders as $lineNum => $head) {
3167                         $key    = '';
3168                         $key    = trim(substr($head, 0, strpos($head, ':')));
3169                         $value  = '';
3170                         $value  = trim(substr($head, (strpos($head, ':') + 1), strlen($head)));
3171
3172                         // handle content-type section in headers
3173                         if(strtolower($key) == 'content-type' && strpos($value, ';')) { // ";" means something follows related to (such as Charset)
3174                                 $semiColPos = mb_strpos($value, ';');
3175                                 $strLenVal = mb_strlen($value);
3176                                 if(($semiColPos + 4) >= $strLenVal) {
3177                                         // the charset="[something]" is on the next line
3178                                         $value .= str_replace($quotes, "", trim($exHeaders[$lineNum+1]));
3179                                 }
3180
3181                                 $newValue = array();
3182                                 $exValue = explode(';', $value);
3183                                 $newValue['type'] = $exValue[0];
3184
3185                                 for($i=1; $i<count($exValue); $i++) {
3186                                         $exContent = explode('=', $exValue[$i]);
3187                                         $newValue[trim($exContent[0])] = trim($exContent[1], "\t \"");
3188                                 }
3189                                 $value = $newValue;
3190                         }
3191
3192                         if(!empty($key) && !empty($value)) {
3193                                 $decodedHeader[$key] = $value;
3194                         }
3195                 }
3196
3197                 return $decodedHeader;
3198         }
3199
3200         /**
3201          * handles translating message text from orignal encoding into UTF-8
3202          *
3203          * @param string text test to be re-encoded
3204          * @param string charset original character set
3205          * @return string utf8 re-encoded text
3206          */
3207         function handleCharsetTranslation($text, $charset) {
3208                 global $locale;
3209
3210                 if(empty($charset)) {
3211                         $GLOBALS['log']->debug("***ERROR: InboundEmail::handleCharsetTranslation() called without a \$charset!");
3212                         $GLOBALS['log']->debug("***STACKTRACE: ".print_r(debug_backtrace(), true));
3213                         return $text;
3214                 }
3215
3216                 // typical headers have no charset - let destination pick (since it's all ASCII anyways)
3217                 if(strtolower($charset) == 'default' || strtolower($charset) == 'utf-8') {
3218                         return $text;
3219                 }
3220
3221                 return $locale->translateCharset($text, $charset);
3222         }
3223
3224
3225
3226         /**
3227          * Builds up the "breadcrumb" trail that imap_fetchbody() uses to return
3228          * parts of an email message, including attachments and inline images
3229          * @param       $parts  array of objects
3230          * @param       $subtype        what type of trail to return? HTML? Plain? binaries?
3231          * @param       $breadcrumb     text trail to build up
3232          */
3233         function buildBreadCrumbs($parts, $subtype, $breadcrumb = '0') {
3234                 //_pp('buildBreadCrumbs building for '.$subtype.' with BC at '.$breadcrumb);
3235                 // loop through available parts in the array
3236                 foreach($parts as $k => $part) {
3237                         // mark passage through level
3238                         $thisBc = ($k+1);
3239                         // if this is not the first time through, start building the map
3240                         if($breadcrumb != 0) {
3241                                 $thisBc = $breadcrumb.'.'.$thisBc;
3242                         }
3243
3244                         // found a multi-part/mixed 'part' - keep digging
3245                         if($part->type == 1 && (strtoupper($part->subtype) == 'RELATED' || strtoupper($part->subtype) == 'ALTERNATIVE' || strtoupper($part->subtype) == 'MIXED')) {
3246                                 //_pp('in loop: going deeper with subtype: '.$part->subtype.' $k is: '.$k);
3247                                 $thisBc = $this->buildBreadCrumbs($part->parts, $subtype, $thisBc);
3248                                 return $thisBc;
3249
3250                         } elseif(strtolower($part->subtype) == strtolower($subtype)) { // found the subtype we want, return the breadcrumb value
3251                                 //_pp('found '.$subtype.' bc! returning: '.$thisBc);
3252                                 return $thisBc;
3253                         } else {
3254                                 //_pp('found '.$part->subtype.' instead');
3255                         }
3256                 }
3257         }
3258
3259         /**
3260          * Similar to buildBreadCrumbs() but returns an ordered array containing all parts of the message that would be
3261          * considered "HTML" or Richtext (embedded images, formatting, etc.).
3262          * @param array parts Array of parts of a message
3263          * @param int breadcrumb Passed integer value to start breadcrumb trail
3264          * @param array stackedBreadcrumbs Persistent trail of breadcrumbs
3265          * @return array Ordered array of parts to retrieve via imap_fetchbody()
3266          */
3267         function buildBreadCrumbsHTML($parts, $breadcrumb = '0', $stackedBreadcrumbs = array()) {
3268                 $subtype = 'HTML';
3269                 $disposition = 'inline';
3270
3271                 foreach($parts as $k => $part) {
3272                         // mark passage through level
3273                         $thisBc = ($k+1);
3274
3275                         if($breadcrumb != 0) {
3276                                 $thisBc = $breadcrumb.'.'.$thisBc;
3277                         }
3278                         // found a multi-part/mixed 'part' - keep digging
3279                         if($part->type == 1 && (strtoupper($part->subtype) == 'RELATED' || strtoupper($part->subtype) == 'ALTERNATIVE' || strtoupper($part->subtype) == 'MIXED')) {
3280                                 $stackedBreadcrumbs = $this->buildBreadCrumbsHTML($part->parts, $thisBc, $stackedBreadcrumbs);
3281                         } elseif(
3282                                 (strtolower($part->subtype) == strtolower($subtype)) ||
3283                                         (
3284                                                 isset($part->disposition) && strtolower($part->disposition) == 'inline' &&
3285                                                 in_array(strtoupper($part->subtype), $this->imageTypes)
3286                                         )
3287                         ) {
3288                                 // found the subtype we want, return the breadcrumb value
3289                                 $stackedBreadcrumbs[] = array(strtolower($part->subtype) => $thisBc);
3290                         } elseif($part->type == 5) {
3291                                 $stackedBreadcrumbs[] = array(strtolower($part->subtype) => $thisBc);
3292                         }
3293                 }
3294
3295                 return $stackedBreadcrumbs;
3296         }
3297
3298         /**
3299          * Takes a PHP imap_* object's to/from/cc/bcc address field and converts it
3300          * to a standard string that SugarCRM expects
3301          * @param       $arr    an array of email address objects
3302          */
3303         function convertImapToSugarEmailAddress($arr) {
3304                 if(is_array($arr)) {
3305                         $addr = '';
3306                         foreach($arr as $key => $obj) {
3307                                 $addr .= $obj->mailbox.'@'.$obj->host.', ';
3308                         }
3309                         // strip last comma
3310                         $ret = substr_replace($addr,'',-2,-1);
3311                         return trim($ret);
3312                 }
3313         }
3314
3315         /**
3316          * tries to figure out what character set a given filename is using and
3317          * decode based on that
3318          *
3319          * @param string name Name of attachment
3320          * @return string decoded name
3321          */
3322         function handleEncodedFilename($name) {
3323                 $imapDecode = imap_mime_header_decode($name);
3324                 /******************************
3325                 $imapDecode => stdClass Object
3326                         (
3327                                 [charset] => utf-8
3328                                 [text] => w�hlen.php
3329                         )
3330
3331                                         OR
3332
3333                 $imapDecode => stdClass Object
3334                         (
3335                                 [charset] => default
3336                                 [text] => UTF-8''%E3%83%8F%E3%82%99%E3%82%A4%E3%82%AA%E3%82%AF%E3%82%99%E3%83%A9%E3%83%95%E3%82%A3%E3%83%BC.txt
3337                         )
3338                 *******************************/
3339                 if($imapDecode[0]->charset != 'default') { // mime-header encoded charset
3340                         $encoding = $imapDecode[0]->charset;
3341                         $name = $imapDecode[0]->text; // encoded in that charset
3342                 } else {
3343                         /* encoded filenames are formatted as [encoding]''[filename] */
3344                         if(strpos($name, "''") !== false) {
3345
3346                                 $encoding = substr($name, 0, strpos($name, "'"));
3347
3348                                 while(strpos($name, "'") !== false) {
3349                                         $name = trim(substr($name, (strpos($name, "'")+1), strlen($name)));
3350                                 }
3351                         }
3352                         $name = urldecode($name);
3353                 }
3354                 return (strtolower($encoding) == 'utf-8') ? $name : $GLOBALS['locale']->translateCharset($name, $encoding, 'UTF-8');
3355         }
3356
3357         /*
3358                 Primary body types for a part of a mail structure (imap_fetchstructure returned object)
3359                 0 => text
3360                 1 => multipart
3361                 2 => message
3362                 3 => application
3363                 4 => audio
3364                 5 => image
3365                 6 => video
3366                 7 => other
3367         */
3368
3369         /**
3370         Primary body types for a part of a mail structure (imap_fetchstructure returned object)
3371         @var array $imap_types
3372         */
3373         public $imap_types = array(
3374                         0 => 'text',
3375                         1 => 'multipart',
3376                         2 => 'message',
3377                         3 => 'application',
3378                         4 => 'audio',
3379                         5 => 'image',
3380                         6 => 'video',
3381         );
3382
3383         public function getMimeType($type, $subtype)
3384         {
3385                 if(isset($this->imap_types[$type])) {
3386                         return $this->imap_types[$type]."/$subtype";
3387                 } else {
3388                         return "other/$subtype";
3389                 }
3390         }
3391
3392         /**
3393          * Takes the "parts" attribute of the object that imap_fetchbody() method
3394          * returns, and recursively goes through looking for objects that have a
3395          * disposition of "attachement" or "inline"
3396          * @param int $msgNo The relative message number for the monitored mailbox
3397          * @param object $parts Array of objects to examine
3398          * @param string $emailId The GUID of the email saved prior to calling this method
3399          * @param array $breadcrumb Default 0, build up of the parts mapping
3400          * @param bool $forDisplay Default false
3401          */
3402         function saveAttachments($msgNo, $parts, $emailId, $breadcrumb='0', $forDisplay) {
3403                 global $sugar_config;
3404                 /*
3405                         Primary body types for a part of a mail structure (imap_fetchstructure returned object)
3406                         0 => text
3407                         1 => multipart
3408                         2 => message
3409                         3 => application
3410                         4 => audio
3411                         5 => image
3412                         6 => video
3413                         7 => other
3414                 */
3415
3416                 foreach($parts as $k => $part) {
3417                         $thisBc = $k+1;
3418                         if($breadcrumb != '0') {
3419                                 $thisBc = $breadcrumb.'.'.$thisBc;
3420                         }
3421                         $attach = null;
3422                         // check if we need to recurse into the object
3423                         //if($part->type == 1 && !empty($part->parts)) {
3424                         if(isset($part->parts) && !empty($part->parts) && !( isset($part->subtype) && strtolower($part->subtype) == 'rfc822')  ) {
3425                                 $this->saveAttachments($msgNo, $part->parts, $emailId, $thisBc, $forDisplay);
3426                 continue;
3427                         } elseif($part->ifdisposition) {
3428                                 // we will take either 'attachments' or 'inline'
3429                                 if(strtolower($part->disposition) == 'attachment' || ((strtolower($part->disposition) == 'inline') && $part->type != 0)) {
3430                                         $attach = $this->getNoteBeanForAttachment($emailId);
3431                                         $fname = $this->handleEncodedFilename($this->retrieveAttachmentNameFromStructure($part));
3432
3433                                         if(!empty($fname)) {//assign name to attachment
3434                                                 $attach->name = $fname;
3435                                         } else {//if name is empty, default to filename
3436                                                 $attach->name = urlencode($this->retrieveAttachmentNameFromStructure($part));
3437                                         }
3438                                         $attach->filename = $attach->name;
3439                                         if (empty($attach->filename)) {
3440                                                 continue;
3441                                         }
3442
3443                                         // deal with the MIME types email has
3444                                         $attach->file_mime_type = $this->getMimeType($part->type, $part->subtype);
3445                                         $attach->safeAttachmentName();
3446                                         if($forDisplay) {
3447                                                 $attach->id = $this->getTempFilename();
3448                                         } else {
3449                                                 // only save if doing a full import, else we want only the binaries
3450                                                 $attach->save();
3451                                         }
3452                                 } // end if disposition type 'attachment'
3453                         }// end ifdisposition
3454                         //Retrieve contents of subtype rfc8822
3455                         elseif ($part->type == 2 && isset($part->subtype) && strtolower($part->subtype) == 'rfc822' )
3456                         {
3457                             $tmp_eml =  imap_fetchbody($this->conn, $msgNo, $thisBc);
3458                             $attach = $this->getNoteBeanForAttachment($emailId);
3459                             $attach->file_mime_type = 'messsage/rfc822';
3460                             $attach->description = $tmp_eml;
3461                             $attach->filename = 'bounce.eml';
3462                             $attach->safeAttachmentName();
3463                             if($forDisplay) {
3464                                 $attach->id = $this->getTempFilename();
3465                             } else {
3466                                 // only save if doing a full import, else we want only the binaries
3467                                 $attach->save();
3468                             }
3469                         } elseif(!$part->ifdisposition && $part->type != 1 && $part->type != 2 && $thisBc != '1') {
3470                         // No disposition here, but some IMAP servers lie about disposition headers, try to find the truth
3471                                 // Also Outlook puts inline attachments as type 5 (image) without a disposition
3472                                 if($part->ifparameters) {
3473                     foreach($part->parameters as $param) {
3474                         if(strtolower($param->attribute) == "name" || strtolower($param->attribute) == "filename") {
3475                             $fname = $this->handleEncodedFilename($param->value);
3476                             break;
3477                         }
3478                     }
3479                     if(empty($fname)) continue;
3480
3481                                         // we assume that named parts are attachments too
3482                     $attach = $this->getNoteBeanForAttachment($emailId);
3483
3484                                         $attach->filename = $attach->name = $fname;
3485                                         $attach->file_mime_type = $this->getMimeType($part->type, $part->subtype);
3486
3487                                         $attach->safeAttachmentName();
3488                                         if($forDisplay) {
3489                                                 $attach->id = $this->getTempFilename();
3490                                         } else {
3491                                                 // only save if doing a full import, else we want only the binaries
3492                                                 $attach->save();
3493                                         }
3494                                 }
3495                         }
3496                         $this->saveAttachmentBinaries($attach, $msgNo, $thisBc, $part, $forDisplay);
3497                 } // end foreach
3498         }
3499
3500         /**
3501          * Return a new note object for attachments.
3502          *
3503          * @param string $emailId
3504          * @return Note
3505          */
3506         function getNoteBeanForAttachment($emailId)
3507         {
3508             $attach = new Note();
3509             $attach->parent_id = $emailId;
3510             $attach->parent_type = 'Emails';
3511
3512             return $attach;
3513         }
3514
3515         /**
3516          * Return the filename of the attachment by examining the dparameters or parameters returned from imap_fetch_structure
3517      *
3518          * @param object $part
3519          * @return string
3520          */
3521         function retrieveAttachmentNameFromStructure($part)
3522         {
3523            $result = "";
3524
3525            foreach ($part->dparamaters as $k => $v)
3526            {
3527                if( strtolower($v->attribute) == 'filename')
3528                {
3529                    $result = $v->value;
3530                    break;
3531                }
3532            }
3533
3534                 if (empty($result)) {
3535                         foreach ($part->parameters as $k => $v) {
3536                                 if (strtolower($v->attribute) == 'name') {
3537                                         $result = $v->value;
3538                                         break;
3539                                 }
3540                         }
3541                 }
3542                 
3543            return $result;
3544
3545     }
3546         /**
3547          * saves the actual binary file of a given attachment
3548          * @param object attach Note object that is attached to the binary file
3549          * @param string msgNo Message Number on IMAP/POP3 server
3550          * @param string thisBc Breadcrumb to navigate email structure to find the content
3551          * @param object part IMAP standard object that contains the "parts" of this section of email
3552          * @param bool $forDisplay
3553          */
3554         function saveAttachmentBinaries($attach, $msgNo, $thisBc, $part, $forDisplay) {
3555                 // decide where to place the file temporarily
3556                 $uploadDir = ($forDisplay) ? "{$this->EmailCachePath}/{$this->id}/attachments/" : "upload://";
3557
3558                 // decide what name to save file as
3559                 $fileName = $attach->id;
3560
3561                 // download the attachment if we didn't do it yet
3562                 if(!file_exists($uploadDir.$fileName)) {
3563                         $msgPartRaw = imap_fetchbody($this->conn, $msgNo, $thisBc);
3564                 // deal with attachment encoding and decode the text string
3565                         $msgPart = $this->handleTranserEncoding($msgPartRaw, $part->encoding);
3566
3567                         if(file_put_contents($uploadDir.$fileName, $msgPart)) {
3568                                 $GLOBALS['log']->debug('InboundEmail saved attachment file: '.$attach->filename);
3569                         } else {
3570                 $GLOBALS['log']->debug('InboundEmail could not create attachment file: '.$attach->filename ." - temp file target: [ {$uploadDir}{$fileName} ]");
3571                 return;
3572                         }
3573                 }
3574
3575                 $this->tempAttachment[$fileName] = urldecode($attach->filename);
3576                 // if all was successful, feel for inline and cache Note ID for display:
3577                 if((strtolower($part->disposition) == 'inline' && in_array($part->subtype, $this->imageTypes))
3578                     || ($part->type == 5)) {
3579                     if(copy($uploadDir.$fileName, sugar_cached("images/{$fileName}.").strtolower($part->subtype))) {
3580                             $id = substr($part->id, 1, -1); //strip <> around
3581                             $this->inlineImages[$id] = $attach->id.".".strtolower($part->subtype);
3582                         } else {
3583                                 $GLOBALS['log']->debug('InboundEmail could not copy '.$uploadDir.$fileName.' to cache');
3584                         }
3585                 }
3586         }
3587
3588         /**
3589          * decodes a string based on its associated encoding
3590          * if nothing is passed, we default to no-encoding type
3591          * @param       $str    encoded string
3592          * @param       $enc    detected encoding
3593          */
3594         function handleTranserEncoding($str, $enc=0) {
3595                 switch($enc) {
3596                         case 2:// BINARY
3597                                 $ret = $str;
3598                                 break;
3599                         case 3:// BASE64
3600                                 $ret = base64_decode($str);
3601                                 break;
3602                         case 4:// QUOTED-PRINTABLE
3603                                 $ret = quoted_printable_decode($str);
3604                                 break;
3605                         case 0:// 7BIT or 8BIT
3606                         case 1:// already in a string-useable format - do nothing
3607                         case 5:// OTHER
3608                         default:// catch all
3609                                 $ret = $str;
3610                                 break;
3611                 }
3612
3613                 return $ret;
3614         }
3615
3616
3617         /**
3618          * Some emails do not get assigned a message_id, specifically from
3619          * Outlook/Exchange.
3620          *
3621          * We need to derive a reliable one for duplicate import checking.
3622          */
3623         function getMessageId($header) {
3624                 $message_id = md5(print_r($header, true));
3625                 return $message_id;
3626         }
3627
3628         /**
3629          * checks for duplicate emails on polling.  The uniqueness of a given email message is determined by a concatenation
3630          * of 2 values, the messageID and the delivered-to field.  This allows multiple To: and B/CC: destination addresses
3631          * to be imported by Sugar without violating the true duplicate-email issues.
3632          *
3633          * @param string message_id message ID generated by sending server
3634          * @param int message number (mailserver's key) of email
3635          * @param object header object generated by imap_headerinfo()
3636          * @param string textHeader Headers in normal text format
3637          * @return bool
3638          */
3639         function importDupeCheck($message_id, $header, $textHeader) {
3640                 $GLOBALS['log']->debug('*********** InboundEmail doing dupe check.');
3641
3642                 // generate "delivered-to" seed for email duplicate check
3643                 $deliveredTo = $this->id; // cn: bug 12236 - cc's failing dupe check
3644                 $exHeader = explode("\n", $textHeader);
3645
3646                 foreach($exHeader as $headerLine) {
3647                         if(strpos(strtolower($headerLine), 'delivered-to:') !== false) {
3648                                 $deliveredTo = substr($headerLine, strpos($headerLine, " "), strlen($headerLine));
3649                                 $GLOBALS['log']->debug('********* InboundEmail found [ '.$deliveredTo.' ] as the destination address for email [ '.$message_id.' ]');
3650                         } elseif(strpos(strtolower($headerLine), 'x-real-to:') !== false) {
3651                                 $deliveredTo = substr($headerLine, strpos($headerLine, " "), strlen($headerLine));
3652                                 $GLOBALS['log']->debug('********* InboundEmail found [ '.$deliveredTo.' ] for non-standards compliant email x-header [ '.$message_id.' ]');
3653                         }
3654                 }
3655
3656                 //if(empty($message_id) && !isset($message_id)) {
3657                 if(empty($message_id) || !isset($message_id)) {
3658                         $GLOBALS['log']->debug('*********** NO MESSAGE_ID.');
3659                         $message_id = $this->getMessageId($header);
3660                 }
3661
3662                 // generate compound messageId
3663                 $this->compoundMessageId = trim($message_id).trim($deliveredTo);
3664                 if (empty($this->compoundMessageId)) {
3665                         $GLOBALS['log']->error('Inbound Email found a message without a header and message_id');
3666                         return false;
3667                 } // if
3668                 $this->compoundMessageId = md5($this->compoundMessageId);
3669
3670                 $query = 'SELECT count(emails.id) AS c FROM emails WHERE emails.message_id = \''.$this->compoundMessageId.'\' and emails.deleted = 0';
3671                 $r = $this->db->query($query, true);
3672                 $a = $this->db->fetchByAssoc($r);
3673
3674                 if($a['c'] > 0) {
3675                         $GLOBALS['log']->debug('InboundEmail found a duplicate email with ID ('.$this->compoundMessageId.')');
3676                         return false; // we have a dupe and don't want to import the email'
3677                 } else {
3678                         return true;
3679                 }
3680         }
3681
3682         /**
3683          * takes the output from imap_mime_hader_decode() and handles multiple types of encoding
3684          * @param string subject Raw subject string from email
3685          * @return string ret properly formatted UTF-8 string
3686          */
3687         function handleMimeHeaderDecode($subject) {
3688                 $subjectDecoded = imap_mime_header_decode($subject);
3689
3690                 $ret = '';
3691                 foreach($subjectDecoded as $object) {
3692                         if($object->charset != 'default') {
3693                                 $ret .= $this->handleCharsetTranslation($object->text, $object->charset);
3694                         } else {
3695                                 $ret .= $object->text;
3696                         }
3697                 }
3698                 return $ret;
3699         }
3700
3701         /**
3702          * Calculates the appropriate display date/time sent for an email.
3703          * @param string headerDate The date sent of email in MIME header format
3704          * @return string GMT-0 Unix timestamp
3705          */
3706         function getUnixHeaderDate($headerDate) {
3707                 global $timedate;
3708
3709                 if (empty($headerDate)) {
3710                         return "";
3711                 }
3712                 ///////////////////////////////////////////////////////////////////
3713                 ////    CALCULATE CORRECT SENT DATE/TIME FOR EMAIL
3714                 if(!empty($headerDate)) {
3715                     // Bug 25254 - Strip trailing space that come in some header dates (maybe ones with 1-digit day number)
3716                     $headerDate = trim($headerDate);
3717                         // need to hack PHP/windows' bad handling of strings when using POP3
3718                         if(strstr($headerDate,'+0000 GMT')) {
3719                                 $headerDate = str_replace('GMT','', $headerDate);
3720                         } elseif(!strtotime($headerDate)) {
3721                                 $headerDate = 'now'; // catch non-standard format times.
3722                         } else {
3723                                 // cn: bug 9196 parse the GMT offset
3724                                 if(strpos($headerDate, '-') || strpos($headerDate, '+')) {
3725                                         // cn: bug make sure last 5 chars are [+|-]nnnn
3726                                         if(strpos($headerDate, "(")) {
3727                                                 $headerDate = preg_replace('/\([\w]+\)/i', "", $headerDate);
3728                                                 $headerDate = trim($headerDate);
3729                                         }
3730
3731                                         // parse mailserver time
3732                                         $gmtEmail = trim(substr($headerDate, -5, 5));
3733                                         $posNeg = substr($gmtEmail, 0, 1);
3734                                         $gmtHours = substr($gmtEmail, 1, 2);
3735                                         $gmtMins = substr($gmtEmail, -2, 2);
3736
3737                                         // get seconds
3738                                         $secsHours = $gmtHours * 60 * 60;
3739                                         $secsTotal = $secsHours + ($gmtMins * 60);
3740                                         $secsTotal = ($posNeg == '-') ? $secsTotal : -1 * $secsTotal;
3741
3742                                         $headerDate = trim(substr_replace($headerDate, '', -5)); // mfh: bug 10961/12855 - date time values with GMT offsets not properly formatted
3743                                 }
3744                         }
3745                 } else {
3746                         $headerDate = 'now';
3747                 }
3748
3749                 $unixHeaderDate = strtotime($headerDate);
3750
3751                 if(isset($secsTotal)) {
3752                         // this gets the timestamp to true GMT-0
3753                         $unixHeaderDate += $secsTotal;
3754                 }
3755
3756                 if(strtotime('Jan 1, 2001') > $unixHeaderDate) {
3757                         $unixHeaderDate = strtotime('now');
3758                 }
3759
3760                 return $unixHeaderDate;
3761                 ////    END CALCULATE CORRECT SENT DATE/TIME FOR EMAIL
3762                 ///////////////////////////////////////////////////////////////////
3763         }
3764
3765         /**
3766          * This method returns the correct messageno for the pop3 protocol
3767          * @param String UIDL
3768          * @return returnMsgNo
3769          */
3770         function getCorrectMessageNoForPop3($messageId) {
3771                 $returnMsgNo = -1;
3772                 if ($this->protocol == 'pop3') {
3773                         if($this->pop3_open()) {
3774                                 // get the UIDL from database;
3775                                 $query = "SELECT msgno FROM email_cache WHERE ie_id = '{$this->id}' AND message_id = '{$messageId}'";
3776                                 $r = $this->db->query($query);
3777                                 $a = $this->db->fetchByAssoc($r);
3778                                 $msgNo = $a['msgno'];
3779                                 $returnMsgNo = $msgNo;
3780
3781                                 // authenticate
3782                                 $this->pop3_sendCommand("USER", $this->email_user);
3783                                 $this->pop3_sendCommand("PASS", $this->email_password);
3784
3785                                 // get UIDL for this msgNo
3786                                 $this->pop3_sendCommand("UIDL {$msgNo}", '', false); // leave socket buffer alone until the while()
3787                                 $buf = fgets($this->pop3socket, 1024); // handle "OK+ msgNo UIDL(UIDL for this messageno)";
3788
3789                                 // if it returns OK then we have found the message else get all the UIDL
3790                                 // and search for the correct msgNo;
3791                                 $foundMessageNo = false;
3792                                 if (preg_match("/OK/", $buf) > 0) {
3793                                         $mailserverResponse = explode(" ", $buf);
3794                                         // if the cachedUIDL and the UIDL from mail server matches then its the correct messageno
3795                                         if (trim($mailserverResponse[sizeof($mailserverResponse) - 1]) == $messageId) {
3796                                                 $foundMessageNo = true;
3797                                         }
3798                                 } //if
3799
3800                                 //get all the UIDL and then find the correct messageno
3801                                 if (!$foundMessageNo) {
3802                                         // get UIDLs
3803                                         $this->pop3_sendCommand("UIDL", '', false); // leave socket buffer alone until the while()
3804                                         fgets($this->pop3socket, 1024); // handle "OK+";
3805                                         $UIDLs = array();
3806                                         $buf = '!';
3807                                         if(is_resource($this->pop3socket)) {
3808                                                 while(!feof($this->pop3socket)) {
3809                                                         $buf = fgets($this->pop3socket, 1024); // 8kb max buffer - shouldn't be more than 80 chars via pop3...
3810                                                         if(trim($buf) == '.') {
3811                                                                 $GLOBALS['log']->debug("*** GOT '.'");
3812                                                                 break;
3813                                                         } // if
3814                                                         // format is [msgNo] [UIDL]
3815                                                         $exUidl = explode(" ", $buf);
3816                                                         $UIDLs[trim($exUidl[1])] = trim($exUidl[0]);
3817                                                 } // while
3818                                                 if (array_key_exists($messageId, $UIDLs)) {
3819                                                         $returnMsgNo = $UIDLs[$messageId];
3820                                                 } else {
3821                                                         // message could not be found on server
3822                                                         $returnMsgNo = -1;
3823                                                 } // else
3824                                         } // if
3825
3826                                 } // if
3827                                 $this->pop3_cleanUp();
3828                         } //if
3829                 } //if
3830                 return $returnMsgNo;
3831         }
3832
3833         /**
3834          * If the importOneEmail returns false, then findout if the duplicate email
3835          */
3836         function getDuplicateEmailId($msgNo, $uid) {
3837                 global $timedate;
3838                 global $app_strings;
3839                 global $app_list_strings;
3840                 global $sugar_config;
3841                 global $current_user;
3842
3843                 $header = imap_headerinfo($this->conn, $msgNo);
3844                 $fullHeader = imap_fetchheader($this->conn, $msgNo); // raw headers
3845
3846                 // reset inline images cache
3847                 $this->inlineImages = array();
3848
3849                 // handle messages deleted on server
3850                 if(empty($header)) {
3851                         if(!isset($this->email) || empty($this->email)) {
3852                                 $this->email = new Email();
3853                         } // if
3854                         return "";
3855                 } else {
3856                         $dupeCheckResult = $this->importDupeCheck($header->message_id, $header, $fullHeader);
3857                         if (!$dupeCheckResult && !empty($this->compoundMessageId)) {
3858                                 // we have a duplicate email
3859                                 $query = 'SELECT id FROM emails WHERE emails.message_id = \''.$this->compoundMessageId.'\' and emails.deleted = 0';
3860                                 $r = $this->db->query($query, true);
3861                                 $a = $this->db->fetchByAssoc($r);
3862
3863                                 $this->email = new Email();
3864                                 $this->email->id = $a['id'];
3865                                 return $a['id'];
3866                         } // if
3867                         return "";
3868                 } // else
3869         } // fn
3870
3871
3872         /**
3873          * shiny new importOneEmail() method
3874          * @param int msgNo
3875          * @param bool forDisplay
3876          * @param clean_email boolean, default true,
3877          */
3878         function importOneEmail($msgNo, $uid, $forDisplay=false, $clean_email=true) {
3879                 $GLOBALS['log']->debug("InboundEmail processing 1 email {$msgNo}-----------------------------------------------------------------------------------------");
3880                 global $timedate;
3881                 global $app_strings;
3882                 global $app_list_strings;
3883                 global $sugar_config;
3884                 global $current_user;
3885
3886         // Bug # 45477
3887         // So, on older versions of PHP (PHP VERSION < 5.3),
3888         // calling imap_headerinfo and imap_fetchheader can cause a buffer overflow for exteremly large headers,
3889         // This leads to the remaining messages not being read because Sugar crashes everytime it tries to read the headers.
3890         // The workaround is to mark a message as read before making trying to read the header of the msg in question
3891         // This forces this message not be read again, and we can continue processing remaining msgs.
3892
3893         // UNCOMMENT THIS IF YOU HAVE THIS PROBLEM!  See notes on Bug # 45477
3894         // $this->markEmails($uid, "read");
3895
3896                 $header = imap_headerinfo($this->conn, $msgNo);
3897                 $fullHeader = imap_fetchheader($this->conn, $msgNo); // raw headers
3898
3899                 // reset inline images cache
3900                 $this->inlineImages = array();
3901
3902                 // handle messages deleted on server
3903                 if(empty($header)) {
3904                         if(!isset($this->email) || empty($this->email)) {
3905                                 $this->email = new Email();
3906                         }
3907
3908                         $q = "";
3909                         if ($this->isPop3Protocol()) {
3910                                 $this->email->name = $app_strings['LBL_EMAIL_ERROR_MESSAGE_DELETED'];
3911                                 $q = "DELETE FROM email_cache WHERE message_id = '{$uid}' AND ie_id = '{$this->id}' AND mbox = '{$this->mailbox}'";
3912                         } else {
3913                                 $this->email->name = $app_strings['LBL_EMAIL_ERROR_IMAP_MESSAGE_DELETED'];
3914                                 $q = "DELETE FROM email_cache WHERE imap_uid = {$uid} AND ie_id = '{$this->id}' AND mbox = '{$this->mailbox}'";
3915                         } // else
3916                         // delete local cache
3917                         $r = $this->db->query($q);
3918
3919                         $this->email->date_sent = $timedate->nowDb();
3920                         return false;
3921                         //return "Message deleted from server.";
3922                 }
3923
3924                 ///////////////////////////////////////////////////////////////////////
3925                 ////    DUPLICATE CHECK
3926                 $dupeCheckResult = $this->importDupeCheck($header->message_id, $header, $fullHeader);
3927                 if($forDisplay || $dupeCheckResult) {
3928                         $GLOBALS['log']->debug('*********** NO duplicate found, continuing with processing.');
3929
3930                         $structure = imap_fetchstructure($this->conn, $msgNo); // map of email
3931
3932                         ///////////////////////////////////////////////////////////////////
3933                         ////    CREATE SEED EMAIL OBJECT
3934                         $email = new Email();
3935                         $email->isDuplicate = ($dupeCheckResult) ? false : true;
3936                         $email->mailbox_id = $this->id;
3937                         $message = array();
3938                         $email->id = create_guid();
3939                         $email->new_with_id = true; //forcing a GUID here to prevent double saves.
3940                         ////    END CREATE SEED EMAIL
3941                         ///////////////////////////////////////////////////////////////////
3942
3943                         ///////////////////////////////////////////////////////////////////
3944                         ////    PREP SYSTEM USER
3945                         if(empty($current_user)) {
3946                                 // I-E runs as admin, get admin prefs
3947
3948                                 $current_user = new User();
3949                                 $current_user->getSystemUser();
3950                         }
3951                         $tPref = $current_user->getUserDateTimePreferences();
3952                         ////    END USER PREP
3953                         ///////////////////////////////////////////////////////////////////
3954             if(!empty($header->date)) {
3955                             $unixHeaderDate = $timedate->fromString($header->date);
3956             }
3957                         ///////////////////////////////////////////////////////////////////
3958                         ////    HANDLE EMAIL ATTACHEMENTS OR HTML TEXT
3959                         ////    Inline images require that I-E handle attachments before body text
3960                         // parts defines attachments - be mindful of .html being interpreted as an attachment
3961                         if($structure->type == 1 && !empty($structure->parts)) {
3962                                 $GLOBALS['log']->debug('InboundEmail found multipart email - saving attachments if found.');
3963                                 $this->saveAttachments($msgNo, $structure->parts, $email->id, 0, $forDisplay);
3964                         } elseif($structure->type == 0) {
3965                                 $uuemail = ($this->isUuencode($email->description)) ? true : false;
3966                                 /*
3967                                  * UUEncoded attachments - legacy, but still have to deal with it
3968                                  * format:
3969                                  * begin 777 filename.txt
3970                                  * UUENCODE
3971                                  *
3972                                  * end
3973                                  */
3974                                 // set body to the filtered one
3975                                 if($uuemail) {
3976                                         $email->description = $this->handleUUEncodedEmailBody($email->description, $email->id);
3977                                         $email->retrieve($email->id);
3978                                         $email->save();
3979                                 }
3980                         } else {
3981                                 if($this->port != 110) {
3982                                         $GLOBALS['log']->debug('InboundEmail found a multi-part email (id:'.$msgNo.') with no child parts to parse.');
3983                                 }
3984                         }
3985                         ////    END HANDLE EMAIL ATTACHEMENTS OR HTML TEXT
3986                         ///////////////////////////////////////////////////////////////////
3987
3988                         ///////////////////////////////////////////////////////////////////
3989                         ////    ASSIGN APPROPRIATE ATTRIBUTES TO NEW EMAIL OBJECT
3990                         // handle UTF-8/charset encoding in the ***headers***
3991                         global $db;
3992                         $email->name                    = $this->handleMimeHeaderDecode($header->subject);
3993                         $email->type = 'inbound';
3994                         if(!empty($unixHeaderDate)) {
3995                             $email->date_sent = $timedate->asUser($unixHeaderDate);
3996                             list($email->date_start, $email->time_start) = $timedate->split_date_time($email->date_sent);
3997                         } else {
3998                             $email->date_start = $email->time_start = $email->date_sent = "";
3999                         }
4000                         $email->status = 'unread'; // this is used in Contacts' Emails SubPanel
4001                         if(!empty($header->toaddress)) {
4002                                 $email->to_name  = $this->handleMimeHeaderDecode($header->toaddress);
4003                                 $email->to_addrs_names = $email->to_name;
4004                         }
4005                         if(!empty($header->to)) {
4006                                 $email->to_addrs        = $this->convertImapToSugarEmailAddress($header->to);
4007                         }
4008                         $email->from_name               = $this->handleMimeHeaderDecode($header->fromaddress);
4009                         $email->from_addr_name = $email->from_name;
4010                         $email->from_addr               = $this->convertImapToSugarEmailAddress($header->from);
4011                         if(!empty($header->cc)) {
4012                                 $email->cc_addrs        = $this->convertImapToSugarEmailAddress($header->cc);
4013                         }
4014                         if(!empty($header->ccaddress)) {
4015                                 $email->cc_addrs_names   = $this->handleMimeHeaderDecode($header->ccaddress);
4016                         } // if
4017                         $email->reply_to_name   = $this->handleMimeHeaderDecode($header->reply_toaddress);
4018                         $email->reply_to_email  = $this->convertImapToSugarEmailAddress($header->reply_to);
4019                         if (!empty($email->reply_to_email)) {
4020                                 $email->reply_to_addr   = $email->reply_to_name;
4021                         }
4022                         $email->intent                  = $this->mailbox_type;
4023
4024                         $email->message_id              = $this->compoundMessageId; // filled by importDupeCheck();
4025
4026                         $oldPrefix = $this->imagePrefix;
4027                         if(!$forDisplay) {
4028                                 // Store CIDs in imported messages, convert on display
4029                                 $this->imagePrefix = "cid:";
4030                         }
4031                         // handle multi-part email bodies
4032                         $email->description_html= $this->getMessageText($msgNo, 'HTML', $structure, $fullHeader,$clean_email); // runs through handleTranserEncoding() already
4033                         $email->description     = $this->getMessageText($msgNo, 'PLAIN', $structure, $fullHeader,$clean_email); // runs through handleTranserEncoding() already
4034                         $this->imagePrefix = $oldPrefix;
4035
4036                         // empty() check for body content
4037                         if(empty($email->description)) {
4038                                 $GLOBALS['log']->debug('InboundEmail Message (id:'.$email->message_id.') has no body');
4039                         }
4040
4041                         // assign_to group
4042                         if (!empty($_REQUEST['user_id'])) {
4043                                 $email->assigned_user_id = $_REQUEST['user_id'];
4044                         } else {
4045                                 // Samir Gandhi : Commented out this code as its not needed
4046                                 //$email->assigned_user_id = $this->group_id;
4047                         }
4048
4049                 //Assign Parent Values if set
4050                 if (!empty($_REQUEST['parent_id']) && !empty($_REQUEST['parent_type'])) {
4051                 $email->parent_id = $_REQUEST['parent_id'];
4052                 $email->parent_type = $_REQUEST['parent_type'];
4053
4054                 $mod = strtolower($email->parent_type);
4055                 $rel = array_key_exists($mod, $email->field_defs) ? $mod : $mod . "_activities_emails"; //Custom modules rel name
4056
4057                 if(! $email->load_relationship($rel) )
4058                     return FALSE;
4059                 $email->$rel->add($email->parent_id);
4060                 }
4061
4062                         // override $forDisplay w/user pref
4063                         if($forDisplay) {
4064                                 if($this->isAutoImport()) {
4065                                         $forDisplay = false; // triggers save of imported email
4066                                 }
4067                         }
4068
4069                         if(!$forDisplay) {
4070                                 $email->save();
4071
4072                                 $email->new_with_id = false; // to allow future saves by UPDATE, instead of INSERT
4073                                 ////    ASSIGN APPROPRIATE ATTRIBUTES TO NEW EMAIL OBJECT
4074                                 ///////////////////////////////////////////////////////////////////
4075
4076                                 ///////////////////////////////////////////////////////////////////
4077                                 ////    LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL
4078                                 //$contactAddr = $this->handleLinking($email);
4079                                 ////    END LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL
4080                                 ///////////////////////////////////////////////////////////////////
4081
4082                                 ///////////////////////////////////////////////////////////////////
4083                                 ////    MAILBOX TYPE HANDLING
4084                                 $this->handleMailboxType($email, $header);
4085                                 ////    END MAILBOX TYPE HANDLING
4086                                 ///////////////////////////////////////////////////////////////////
4087
4088                                 ///////////////////////////////////////////////////////////////////
4089                                 ////    SEND AUTORESPONSE
4090                                 if(!empty($email->reply_to_email)) {
4091                                         $contactAddr = $email->reply_to_email;
4092                                 } else {
4093                                         $contactAddr = $email->from_addr;
4094                                 }
4095                                 if (!$this->isMailBoxTypeCreateCase()) {
4096                                         $this->handleAutoresponse($email, $contactAddr);
4097                                 }
4098                                 ////    END SEND AUTORESPONSE
4099                                 ///////////////////////////////////////////////////////////////////
4100                                 ////    END IMPORT ONE EMAIL
4101                                 ///////////////////////////////////////////////////////////////////
4102                         }
4103                 } else {
4104                         // only log if not POP3; pop3 iterates through ALL mail
4105                         if($this->protocol != 'pop3') {
4106                                 $GLOBALS['log']->info("InboundEmail found a duplicate email: ".$header->message_id);
4107                                 //echo "This email has already been imported";
4108                         }
4109                         return false;
4110                 }
4111                 ////    END DUPLICATE CHECK
4112                 ///////////////////////////////////////////////////////////////////////
4113
4114                 ///////////////////////////////////////////////////////////////////////
4115                 ////    DEAL WITH THE MAILBOX
4116                 if(!$forDisplay) {
4117                         $r = imap_setflag_full($this->conn, $msgNo, '\\SEEN');
4118
4119                         // if delete_seen, mark msg as deleted
4120                         if($this->delete_seen == 1  && !$forDisplay) {
4121                                 $GLOBALS['log']->info("INBOUNDEMAIL: delete_seen == 1 - deleting email");
4122                                 imap_setflag_full($this->conn, $msgNo, '\\DELETED');
4123                         }
4124                 } else {
4125                         // for display - don't touch server files?
4126                         //imap_setflag_full($this->conn, $msgNo, '\\UNSEEN');
4127                 }
4128
4129                 $GLOBALS['log']->debug('********************************* InboundEmail finished import of 1 email: '.$email->name);
4130                 ////    END DEAL WITH THE MAILBOX
4131                 ///////////////////////////////////////////////////////////////////////
4132
4133                 ///////////////////////////////////////////////////////////////////////
4134                 ////    TO SUPPORT EMAIL 2.0
4135                 $this->email = $email;
4136
4137                 if(empty($this->email->et)) {
4138                         $this->email->email2init();
4139                 }
4140
4141                 return true;
4142         }
4143
4144         /**
4145          * figures out if a plain text email body has UUEncoded attachments
4146          * @param string string The email body
4147          * @return bool True if UUEncode is detected.
4148          */
4149         function isUuencode($string) {
4150                 $rx = "begin [0-9]{3} .*";
4151
4152                 $exBody = explode("\r", $string);
4153                 foreach($exBody as $line) {
4154                         if(preg_match("/begin [0-9]{3} .*/i", $line)) {
4155                                 return true;
4156                         }
4157                 }
4158
4159                 return false;
4160         }
4161
4162         /**
4163          * handles UU Encoded emails - a legacy from pre-RFC 822 which must still be supported (?)
4164          * @param string raw The raw email body
4165          * @param string id Parent email ID
4166          * @return string The filtered email body, stripped of attachments
4167          */
4168         function handleUUEncodedEmailBody($raw, $id) {
4169                 global $locale;
4170
4171                 $emailBody = '';
4172                 $attachmentBody = '';
4173                 $inAttachment = false;
4174
4175                 $exRaw = explode("\n", $raw);
4176
4177                 foreach($exRaw as $k => $line) {
4178                         $line = trim($line);
4179
4180                         if(preg_match("/begin [0-9]{3} .*/i", $line, $m)) {
4181                                 $inAttachment = true;
4182                                 $fileName = $this->handleEncodedFilename(substr($m[0], 10, strlen($m[0])));
4183
4184                                 $attachmentBody = ''; // reset for next part of loop;
4185                                 continue;
4186                         }
4187
4188                         // handle "end"
4189                         if(strpos($line, "end") === 0) {
4190                                 if(!empty($fileName) && !empty($attachmentBody)) {
4191                                         $this->handleUUDecode($id, $fileName, trim($attachmentBody));
4192                                         $attachmentBody = ''; // reset for next part of loop;
4193                                 }
4194                         }
4195
4196                         if($inAttachment === false) {
4197                                 $emailBody .= "\n".$line;
4198                         } else {
4199                                 $attachmentBody .= "\n".$line;
4200                         }
4201                 }
4202
4203                 /* since UUEncode was developed before MIME, we have NO idea what character set encoding was used.  we will assume the user's locale character set */
4204                 $emailBody = $locale->translateCharset($emailBody, $locale->getExportCharset(), 'UTF-8');
4205                 return $emailBody;
4206         }
4207
4208         /**
4209          * wrapper for UUDecode
4210          * @param string id Id of the email
4211          * @param string UUEncode Encode US-ASCII
4212          */
4213         function handleUUDecode($id, $fileName, $UUEncode) {
4214                 global $sugar_config;
4215                 /* include PHP_Compat library; it auto-feels for PHP5's compiled convert_uuencode() function */
4216                 require_once('include/PHP_Compat/convert_uudecode.php');
4217
4218                 $attach = new Note();
4219                 $attach->parent_id = $id;
4220                 $attach->parent_type = 'Emails';
4221
4222                 $fname = $this->handleEncodedFilename($fileName);
4223
4224                 if(!empty($fname)) {//assign name to attachment
4225                         $attach->name = $fname;
4226                 } else {//if name is empty, default to filename
4227                         $attach->name = urlencode($fileName);
4228                 }
4229
4230                 $attach->filename = urlencode($attach->name);
4231
4232                 //get position of last "." in file name
4233                 $file_ext_beg = strrpos($attach->filename,".");
4234                 $file_ext = "";
4235                 //get file extension
4236                 if($file_ext_beg >0) {
4237                         $file_ext = substr($attach->filename, $file_ext_beg+1);
4238                 }
4239                 //check to see if this is a file with extension located in "badext"
4240                 foreach($sugar_config['upload_badext'] as $badExt) {
4241                         if(strtolower($file_ext) == strtolower($badExt)) {
4242                                 //if found, then append with .txt and break out of lookup
4243                                 $attach->name = $attach->name . ".txt";
4244                                 $attach->file_mime_type = 'text/';
4245                                 $attach->filename = $attach->filename . ".txt";
4246                                 break; // no need to look for more
4247                         }
4248                 }
4249                 $attach->save();
4250
4251                 $bin = convert_uudecode($UUEncode);
4252                 $filename = "upload://{$attach->id}";
4253                 if(file_put_contents($filename, $bin)) {
4254                 $GLOBALS['log']->debug('InboundEmail saved attachment file: '.$filename);
4255                 } else {
4256                 $GLOBALS['log']->debug('InboundEmail could not create attachment file: '.$filename);
4257                 }
4258         }
4259
4260         /**
4261          * returns true if the email's domain is NOT in the filter domain string
4262          *
4263          * @param object email Email object in question
4264          * @return bool true if not filtered, false if filtered
4265          */
4266         function checkFilterDomain($email) {
4267                 $filterDomain = $this->get_stored_options('filter_domain');
4268                 if(!isset($filterDomain) || empty($filterDomain)) {
4269                         return true; // nothing set for this
4270                 } else {
4271                         $replyTo = strtolower($email->reply_to_email);
4272                         $from = strtolower($email->from_addr);
4273                         $filterDomain = '@'.strtolower($filterDomain);
4274                         if(strpos($replyTo, $filterDomain) !== false) {
4275                                 $GLOBALS['log']->debug('Autoreply cancelled - [reply to] address domain matches filter domain.');
4276                                 return false;
4277                         } elseif(strpos($from, $filterDomain) !== false) {
4278                                 $GLOBALS['log']->debug('Autoreply cancelled - [from] address domain matches filter domain.');
4279                                 return false;
4280                         } else {
4281                                 return true; // no match
4282                         }
4283                 }
4284         }
4285
4286         /**
4287          * returns true if subject is NOT "out of the office" type
4288          *
4289          * @param string subject Subject line of email in question
4290          * @return bool returns false if OOTO found
4291          */
4292         function checkOutOfOffice($subject) {
4293                 $ooto = array("Out of the Office", "Out of Office");
4294
4295                 foreach($ooto as $str) {
4296                         if(preg_match('/'.$str.'/i', $subject)) {
4297                                 $GLOBALS['log']->debug('Autoreply cancelled - found "Out of Office" type of subject.');
4298                                 return false;
4299                         }
4300                 }
4301                 return true; // no matches to ooto strings
4302         }
4303
4304
4305         /**
4306          * sets a timestamp for an autoreply to a single email addy
4307          *
4308          * @param string addr Address of auto-replied target
4309          */
4310         function setAutoreplyStatus($addr) {
4311             $timedate = TimeDate::getInstance();
4312                 $this->db->query(       'INSERT INTO inbound_email_autoreply (id, deleted, date_entered, date_modified, autoreplied_to, ie_id) VALUES (
4313                                                         \''.create_guid().'\',
4314                                                         0,
4315                                                         \''.$timedate->nowDb().'\',
4316                                                         \''.$timedate->nowDb().'\',
4317                                                         \''.$addr.'\',
4318                                     \''.$this->id.'\') ', true);
4319         }
4320
4321
4322         /**
4323          * returns true if recipient has NOT received 10 auto-replies in 24 hours
4324          *
4325          * @param string from target address for auto-reply
4326          * @return bool true if target is valid/under limit
4327          */
4328         function getAutoreplyStatus($from) {
4329                 global $sugar_config;
4330         $timedate = TimeDate::getInstance();
4331
4332                 $q_clean = 'UPDATE inbound_email_autoreply SET deleted = 1 WHERE date_entered < \''.$timedate->getNow()->modify("-24 hours")->asDb().'\'';
4333                 $r_clean = $this->db->query($q_clean, true);
4334
4335                 $q = 'SELECT count(*) AS c FROM inbound_email_autoreply WHERE deleted = 0 AND autoreplied_to = \''.$from.'\' AND ie_id = \''.$this->id.'\'';
4336                 $r = $this->db->query($q, true);
4337                 $a = $this->db->fetchByAssoc($r);
4338
4339                 $email_num_autoreplies_24_hours = $this->get_stored_options('email_num_autoreplies_24_hours');
4340                 $maxReplies = (isset($email_num_autoreplies_24_hours)) ? $email_num_autoreplies_24_hours : $this->maxEmailNumAutoreplies24Hours;
4341
4342                 if($a['c'] >= $maxReplies) {
4343                         $GLOBALS['log']->debug('Autoreply cancelled - more than ' . $maxReplies . ' replies sent in 24 hours.');
4344                         return false;
4345                 } else {
4346                         return true;
4347                 }
4348         }
4349
4350         /**
4351          * returns exactly 1 id match. if more than one, than returns false
4352          * @param       $emailName              the subject of the email to match
4353          * @param       $tableName              the table of the matching bean type
4354          */
4355         function getSingularRelatedId($emailName, $tableName) {
4356                 $repStrings = array('RE:','Re:','re:');
4357                 $preppedName = str_replace($repStrings,'',trim($emailName));
4358
4359                 //TODO add team security to this query
4360                 $q = 'SELECT count(id) AS c FROM '.$tableName.' WHERE deleted = 0 AND name LIKE \'%'.$preppedName.'%\'';
4361                 $r = $this->db->query($q, true);
4362                 $a = $this->db->fetchByAssoc($r);
4363
4364                 if($a['c'] == 0) {
4365                         $q = 'SELECT id FROM '.$tableName.' WHERE deleted = 0 AND name LIKE \'%'.$preppedName.'%\'';
4366                         $r = $this->db->query($q, true);
4367                         $a = $this->db->fetchByAssoc($r);
4368                         return $a['id'];
4369                 } else {
4370                         return false;
4371                 }
4372         }
4373
4374         /**
4375          * saves InboundEmail parse macros to config.php
4376          * @param string type Bean to link
4377          * @param string macro The new macro
4378          */
4379         function saveInboundEmailSystemSettings($type, $macro) {
4380                 global $sugar_config;
4381
4382                 // inbound_email_case_subject_macro
4383                 $var = "inbound_email_".strtolower($type)."_subject_macro";
4384                 $sugar_config[$var] = $macro;
4385
4386                 ksort($sugar_config);
4387
4388                 $sugar_config_string = "<?php\n" .
4389                         '// created: ' . date('Y-m-d H:i:s') . "\n" .
4390                         '$sugar_config = ' .
4391                         var_export($sugar_config, true) .
4392                         ";\n?>\n";
4393
4394                 write_array_to_file("sugar_config", $sugar_config, "config.php");
4395         }
4396
4397         /**
4398          * returns the HTML for InboundEmail system settings
4399          * @return string HTML
4400          */
4401         function getSystemSettingsForm() {
4402                 global $sugar_config;
4403                 global $mod_strings;
4404                 global $app_strings;
4405                 global $app_list_strings;
4406
4407                 ////    Case Macro
4408                 $c = new aCase();
4409
4410                 $macro = $c->getEmailSubjectMacro();
4411
4412                 $ret =<<<eoq
4413                         <form action="index.php" method="post" name="Macro" id="form">
4414                                                 <input type="hidden" name="module" value="InboundEmail">
4415                                                 <input type="hidden" name="action" value="ListView">
4416                                                 <input type="hidden" name="save" value="true">
4417
4418                         <table width="100%" cellpadding="0" cellspacing="0" border="0">
4419                                 <tr>
4420                                         <td>
4421                                                 <input  title="{$app_strings['LBL_SAVE_BUTTON_TITLE']}"
4422                                                                 accessKey="{$app_strings['LBL_SAVE_BUTTON_KEY']}"
4423                                                                 class="button"
4424                                                                 onclick="this.form.return_module.value='InboundEmail'; this.form.return_action.value='ListView';"
4425                                                                 type="submit" name="Edit" value="  {$app_strings['LBL_SAVE_BUTTON_LABEL']}  ">
4426                                         </td>
4427                                 </tr>
4428                         </table>
4429
4430                         <table width="100%" border="0" cellspacing="0" cellpadding="0" class="detail view">
4431                                 <tr>
4432                                         <td valign="top" width='10%' NOWRAP scope="row">
4433                                                 <slot>
4434                                                         <b>{$mod_strings['LBL_CASE_MACRO']}:</b>
4435                                                 </slot>
4436                                         </td>
4437                                         <td valign="top" width='20%'>
4438                                                 <slot>
4439                                                         <input name="inbound_email_case_macro" type="text" value="{$macro}">
4440                                                 </slot>
4441                                         </td>
4442                                         <td valign="top" width='70%'>
4443                                                 <slot>
4444                                                         {$mod_strings['LBL_CASE_MACRO_DESC']}
4445                                                         <br />
4446                                                         <i>{$mod_strings['LBL_CASE_MACRO_DESC2']}</i>
4447                                                 </slot>
4448                                         </td>
4449                                 </tr>
4450                         </table>
4451                         </form>
4452 eoq;
4453                 return $ret;
4454         }
4455
4456         /**
4457          * for mailboxes of type "Support" parse for '[CASE:%1]'
4458          * @param       $emailName              the subject line of the email
4459          * @param       $aCase                  a Case object
4460          */
4461         function getCaseIdFromCaseNumber($emailName, $aCase) {
4462                 //$emailSubjectMacro
4463                 $exMacro = explode('%1', $aCase->getEmailSubjectMacro());
4464                 $open = $exMacro[0];
4465                 $close = $exMacro[1];
4466
4467                 if($sub = stristr($emailName, $open)) { // eliminate everything up to the beginning of the macro and return the rest
4468                         // $sub is [CASE:XX] xxxxxxxxxxxxxxxxxxxxxx
4469                         $sub2 = str_replace($open, '', $sub);
4470                         // $sub2 is XX] xxxxxxxxxxxxxx
4471                         $sub3 = substr($sub2, 0, strpos($sub2, $close));
4472
4473             // filter out deleted records in order to create a new case
4474             // if email is related to deleted one (bug #49840)
4475             $r = $this->db->query("SELECT id FROM cases WHERE case_number = '{$sub3}' and deleted = 0", true);
4476                         $a = $this->db->fetchByAssoc($r);
4477                         if(!empty($a['id'])) {
4478                                 return $a['id'];
4479                         } else {
4480                                 return false;
4481                         }
4482                 } else {
4483                         return false;
4484                 }
4485         }
4486
4487         function get_stored_options($option_name,$default_value=null,$stored_options=null) {
4488                 if (empty($stored_options)) {
4489                         $stored_options=$this->stored_options;
4490                 }
4491                 if(!empty($stored_options)) {
4492                         $storedOptions = unserialize(base64_decode($stored_options));
4493                         if (isset($storedOptions[$option_name])) {
4494                                 $default_value=$storedOptions[$option_name];
4495                         }
4496                 }
4497                 return $default_value;
4498         }
4499
4500
4501         /**
4502          * This function returns a contact or user ID if a matching email is found
4503          * @param       $email          the email address to match
4504          * @param       $table          which table to query
4505          */
4506         function getRelatedId($email, $module) {
4507                 $email = trim(strtoupper($email));
4508                 if(strpos($email, ',') !== false) {
4509                         $emailsArray = explode(',', $email);
4510                         $emailAddressString = "";
4511                         foreach($emailsArray as $emailAddress) {
4512                                 if (!empty($emailAddressString)) {
4513                                         $emailAddressString .= ",";
4514                                 }
4515                                 $emailAddressString .=  "'" . $emailAddress. "'";
4516                         } // foreach
4517                         $email = $emailAddressString;
4518                 } else {
4519                         $email = "'" . $email . "'";
4520                 } // else
4521                 $module = ucfirst($module);
4522
4523                 $q = "SELECT bean_id FROM email_addr_bean_rel eabr
4524                                 JOIN email_addresses ea ON (eabr.email_address_id = ea.id)
4525                                 WHERE bean_module = '{$module}' AND ea.email_address_caps in ( {$email} ) AND eabr.deleted=0";
4526
4527                 $r = $this->db->query($q, true);
4528
4529                 $retArr = array();
4530                 while($a = $this->db->fetchByAssoc($r)) {
4531                         $retArr[] = $a['bean_id'];
4532                 }
4533                 if(count($retArr) > 0) {
4534                         return $retArr;
4535                 } else {
4536                         return false;
4537                 }
4538         }
4539
4540         /**
4541          * finds emails tagged "//UNSEEN" on mailserver and "SINCE: [date]" if that
4542          * option is set
4543          *
4544          * @return array Array of messageNumbers (mail server's internal keys)
4545          */
4546         function getNewMessageIds() {
4547                 $storedOptions = unserialize(base64_decode($this->stored_options));
4548
4549                 //TODO figure out if the since date is UDT
4550                 if($storedOptions['only_since']) {// POP3 does not support Unseen flags
4551                         if(!isset($storedOptions['only_since_last']) && !empty($storedOptions['only_since_last'])) {
4552                                 $q = 'SELECT last_run FROM schedulers WHERE job = \'function::pollMonitoredInboxes\'';
4553                                 $r = $this->db->query($q, true);
4554                                 $a = $this->db->fetchByAssoc($r);
4555
4556                                 $date = date('r', strtotime($a['last_run']));
4557                         } else {
4558                                 $date = $storedOptions['only_since_last'];
4559                         }
4560                         $ret = imap_search($this->conn, 'SINCE "'.$date.'" UNDELETED UNSEEN');
4561                         $check = imap_check($this->conn);
4562                         $storedOptions['only_since_last'] = $check->Date;
4563                         $this->stored_options = base64_encode(serialize($storedOptions));
4564                         $this->save();
4565                 } else {
4566             $ret = imap_search($this->conn, 'UNDELETED UNSEEN');
4567                 }
4568
4569                 $GLOBALS['log']->debug('-----> getNewMessageIds() got '.count($ret).' new Messages');
4570                 return $ret;
4571         }
4572
4573         /**
4574          * Constructs the resource connection string that IMAP needs
4575          * @param string $service Service string, will generate if not passed
4576          * @return string
4577          */
4578         function getConnectString($service='', $mbox='', $includeMbox=true) {
4579                 $service = empty($service) ? $this->getServiceString() : $service;
4580                 $mbox = empty($mbox) ? $this->mailbox : $mbox;
4581
4582                 $connectString = '{'.$this->server_url.':'.$this->port.'/service='.$this->protocol.$service.'}';
4583                 $connectString .= ($includeMbox) ? $mbox : "";
4584
4585                 return $connectString;
4586         }
4587
4588         function disconnectMailserver() {
4589                 if(is_resource($this->conn)) {
4590                         imap_close($this->conn);
4591                 }
4592         }
4593
4594         /**
4595          * Connects to mailserver.  If an existing IMAP resource is available, it
4596          * will attempt to reuse the connection, updating the mailbox path.
4597          *
4598          * @param bool test Flag to test connection
4599          * @param bool force Force reconnect
4600          * @return string "true" on success, "false" or $errorMessage on failure
4601          */
4602         function connectMailserver($test=false, $force=false) {
4603                 global $mod_strings;
4604                 if(!function_exists("imap_open")) {
4605                         $GLOBALS['log']->debug('------------------------- IMAP libraries NOT available!!!! die()ing thread.----');
4606                         return $mod_strings['LBL_WARN_NO_IMAP'];
4607                 }
4608
4609                 imap_errors(); // clearing error stack
4610                 error_reporting(0); // turn off notices from IMAP
4611
4612                 // tls::ca::ssl::protocol::novalidate-cert::notls
4613                 $useSsl = ($_REQUEST['ssl'] == 'true') ? true : false;
4614                 if($test) {
4615                         imap_timeout(1, 15); // 60 secs is the default
4616                         imap_timeout(2, 15);
4617                         imap_timeout(3, 15);
4618
4619                         $opts = $this->findOptimumSettings($useSsl);
4620                         if(isset($opts['good']) && empty($opts['good'])) {
4621                                 return array_pop($opts['err']);
4622                         } else {
4623                                 $service = $opts['service'];
4624                                 $service = str_replace('foo','', $service); // foo there to support no-item explodes
4625                         }
4626                 } else {
4627                         $service = $this->getServiceString();
4628                 }
4629
4630                 $connectString = $this->getConnectString($service, $this->mailbox);
4631
4632                 /*
4633                  * Try to recycle the current connection to reduce response times
4634                  */
4635                 if(is_resource($this->conn)) {
4636                         if($force) {
4637                                 // force disconnect
4638                                 imap_close($this->conn);
4639                         }
4640
4641                         if(imap_ping($this->conn)) {
4642                                 // we have a live connection
4643                                 imap_reopen($this->conn, $connectString, CL_EXPUNGE);
4644                         }
4645                 }
4646
4647                 // final test
4648                 if(!is_resource($this->conn) && !$test) {
4649                         $this->conn = imap_open($connectString, $this->email_user, $this->email_password, CL_EXPUNGE);
4650                 }
4651
4652                 if($test) {
4653                         if ($opts == false && !is_resource($this->conn)) {
4654                                 $this->conn = imap_open($connectString, $this->email_user, $this->email_password, CL_EXPUNGE);
4655                         }
4656                         $errors = '';
4657                         $alerts = '';
4658                         $successful = false;
4659                         if(($errors = imap_last_error()) || ($alerts = imap_alerts())) {
4660                                 if($errors == 'Mailbox is empty') { // false positive
4661                                         $successful = true;
4662                                 } else {
4663                                         $msg .= $errors;
4664                                         $msg .= '<p>'.$alerts.'<p>';
4665                                         $msg .= '<p>'.$mod_strings['ERR_TEST_MAILBOX'];
4666                                 }
4667                         } else {
4668                                 $successful = true;
4669                         }
4670
4671                         if($successful) {
4672                                 if($this->protocol == 'imap') {
4673                                         $msg .= $mod_strings['LBL_TEST_SUCCESSFUL'];
4674                                         /*
4675                                         $testConnectString = '{'.$this->server_url.':'.$this->port.'/service='.$this->protocol.$service.'}';
4676                                         if (!is_resource($this->conn)) {
4677                                                 $this->conn = imap_open($connectString, $this->email_user, $this->email_password, CL_EXPUNGE);
4678                                         }
4679                                         $list = imap_getmailboxes($this->conn, $testConnectString, "*");
4680                                         if(isset($_REQUEST['personal']) && $_REQUEST['personal'] == 'true') {
4681                                                 $msg .= $mod_strings['LBL_TEST_SUCCESSFUL'];
4682                                         } elseif (is_array($list)) {
4683                                                 sort($list);
4684                                                 _ppd($boxes);
4685
4686                                                 $msg .= '<b>'.$mod_strings['LBL_FOUND_MAILBOXES'].'</b><p>';
4687                                                 foreach ($list as $key => $val) {
4688                                                         $mb = imap_utf7_decode(str_replace($testConnectString,'',$val->name));
4689                                                         $msg .= '<a onClick=\'setMailbox(\"'.$mb.'\"); window.close();\'>';
4690                                                         $msg .= $mb;
4691                                                         $msg .= '</a><br>';
4692                                                 }
4693                                         } else {
4694                                                 $msg .= $errors;
4695                                                 $msg .= '<p>'.$mod_strings['ERR_MAILBOX_FAIL'].imap_last_error().'</p>';
4696                                                 $msg .= '<p>'.$mod_strings['ERR_TEST_MAILBOX'].'</p>';
4697                                         }
4698                                         */
4699                                 } else {
4700                                         $msg .= $mod_strings['LBL_POP3_SUCCESS'];
4701                                 }
4702                         }
4703
4704                         imap_errors(); // collapse error stack
4705                         imap_close($this->conn);
4706                         return $msg;
4707                 } elseif(!is_resource($this->conn)) {
4708                         return "false";
4709                 } else {
4710                         return "true";
4711                 }
4712         }
4713
4714
4715
4716         function checkImap() {
4717                 global $mod_strings;
4718
4719                 if(!function_exists('imap_open')) {
4720                         echo '
4721                         <table cellpadding="0" cellspacing="0" width="100%" border="0" class="list view">
4722                                 <tr height="20">
4723                                         <td scope="col" width="25%"  colspan="2"><slot>
4724                                                 '.$mod_strings['LBL_WARN_IMAP_TITLE'].'
4725                                         </slot></td>
4726                                 </tr>
4727                                 <tr>
4728                                         <td scope="row" valign=TOP bgcolor="#fdfdfd" width="20%"><slot>
4729                                                 '.$mod_strings['LBL_WARN_IMAP'].'
4730                                         <td scope="row" valign=TOP class="oddListRowS1" bgcolor="#fdfdfd" width="80%"><slot>
4731                                                 <span class=error>'.$mod_strings['LBL_WARN_NO_IMAP'].'</span>
4732                                         </slot></td>
4733                                 </tr>
4734                         </table>
4735                         <br>';
4736                 }
4737         }
4738
4739         /**
4740          * retrieves an array of I-E beans based on the group_id
4741          * @param       string  $groupId        GUID of the group user or Individual
4742          * @return      array   $beans          array of beans
4743          * @return      boolean false if none returned
4744          */
4745         function retrieveByGroupId($groupId) {
4746                 $q = 'SELECT id FROM inbound_email WHERE group_id = \''.$groupId.'\' AND deleted = 0 AND status = \'Active\'';
4747                 $r = $this->db->query($q, true);
4748
4749                 $beans = array();
4750                 while($a = $this->db->fetchByAssoc($r)) {
4751                         $ie = new InboundEmail();
4752                         $ie->retrieve($a['id']);
4753                         $beans[$a['id']] = $ie;
4754                 }
4755                 return $beans;
4756         }
4757
4758         /**
4759          * Retrieves the current count of personal accounts for the user specified.
4760          *
4761          * @param unknown_type $user
4762          */
4763         function getUserPersonalAccountCount($user = null)
4764         {
4765             if($user == null)
4766                $user = $GLOBALS['current_user'];
4767
4768             $query = "SELECT count(*) as c FROM inbound_email WHERE deleted=0 AND is_personal='1' AND group_id='{$user->id}' AND status='Active'";
4769
4770             $rs = $this->db->query($query);
4771                 $row = $this->db->fetchByAssoc($rs);
4772         return $row['c'];
4773         }
4774
4775         /**
4776          * retrieves an array of I-E beans based on the group folder id
4777          * @param       string  $groupFolderId  GUID of the group folder
4778          * @return      array   $beans          array of beans
4779          * @return      boolean false if none returned
4780          */
4781         function retrieveByGroupFolderId($groupFolderId) {
4782                 $q = 'SELECT id FROM inbound_email WHERE groupfolder_id = \''.$groupFolderId.'\' AND deleted = 0 ';
4783                 $r = $this->db->query($q, true);
4784
4785                 $beans = array();
4786                 while($a = $this->db->fetchByAssoc($r)) {
4787                         $ie = new InboundEmail();
4788                         $ie->retrieve($a['id']);
4789                         $beans[] = $ie;
4790                 }
4791                 return $beans;
4792         }
4793
4794         /**
4795          * Retrieves an array of I-E beans that the user has team access to
4796          */
4797         function retrieveAllByGroupId($id, $includePersonal=true) {
4798                 global $current_user;
4799
4800                 $beans = ($includePersonal) ? $this->retrieveByGroupId($id) : array();
4801
4802                 $teamJoin = '';
4803
4804
4805
4806         // bug 50536: groupfolder_id cannot be updated to NULL from sugarbean's nullable check ('type' set to ID in the vardef)
4807         // hence the awkward or check -- rbacon
4808                 $q = "SELECT inbound_email.id FROM inbound_email {$teamJoin} WHERE is_personal = 0 AND (groupfolder_id is null OR groupfolder_id = '') AND mailbox_type not like 'bounce' AND inbound_email.deleted = 0 AND status = 'Active' ";
4809
4810
4811
4812                 $r = $this->db->query($q, true);
4813
4814                 while($a = $this->db->fetchByAssoc($r)) {
4815                         $found = false;
4816                         foreach($beans as $bean) {
4817                                 if($bean->id == $a['id']) {
4818                                         $found = true;
4819                                 }
4820                         }
4821
4822                         if(!$found) {
4823                                 $ie = new InboundEmail();
4824                                 $ie->retrieve($a['id']);
4825                                 $beans[$a['id']] = $ie;
4826                         }
4827                 }
4828
4829                 return $beans;
4830         }
4831
4832         /**
4833          * Retrieves an array of I-E beans that the user has team access to including group
4834          */
4835         function retrieveAllByGroupIdWithGroupAccounts($id, $includePersonal=true) {
4836                 global $current_user;
4837
4838                 $beans = ($includePersonal) ? $this->retrieveByGroupId($id) : array();
4839
4840                 $teamJoin = '';
4841
4842
4843
4844
4845
4846
4847                 $q = "SELECT DISTINCT inbound_email.id FROM inbound_email {$teamJoin} WHERE is_personal = 0 AND mailbox_type not like 'bounce' AND status = 'Active' AND inbound_email.deleted = 0 ";
4848
4849                 $r = $this->db->query($q, true);
4850
4851                 while($a = $this->db->fetchByAssoc($r)) {
4852                         $found = false;
4853                         foreach($beans as $bean) {
4854                                 if($bean->id == $a['id']) {
4855                                         $found = true;
4856                                 }
4857                         }
4858
4859                         if(!$found) {
4860                                 $ie = new InboundEmail();
4861                                 $ie->retrieve($a['id']);
4862                                 $beans[$a['id']] = $ie;
4863                         }
4864                 }
4865
4866                 return $beans;
4867         }
4868
4869
4870         /**
4871          * returns the bean name - overrides SugarBean's
4872          */
4873         function get_summary_text() {
4874                 return $this->name;
4875         }
4876
4877         /**
4878          * Override's SugarBean's
4879          */
4880         function create_export_query($order_by, $where, $show_deleted = 0) {
4881                 return $this->create_new_list_query($order_by, $where, $show_deleted = 0);
4882         }
4883
4884         /**
4885          * Override's SugarBean's
4886          */
4887
4888         /**
4889          * Override's SugarBean's
4890          */
4891         function get_list_view_data(){
4892                 global $mod_strings;
4893                 global $app_list_strings;
4894                 $temp_array = $this->get_list_view_array();
4895                 $temp_array['MAILBOX_TYPE_NAME']= $app_list_strings['dom_mailbox_type'][$this->mailbox_type];
4896                 //cma, fix bug 21670.
4897         $temp_array['GLOBAL_PERSONAL_STRING']= ($this->is_personal ? $mod_strings['LBL_IS_PERSONAL'] : $mod_strings['LBL_IS_GROUP']);
4898         $temp_array['STATUS'] = ($this->status == 'Active') ? $mod_strings['LBL_STATUS_ACTIVE'] : $mod_strings['LBL_STATUS_INACTIVE'];
4899                 return $temp_array;
4900         }
4901
4902         /**
4903          * Override's SugarBean's
4904          */
4905         function fill_in_additional_list_fields() {
4906                 $this->fill_in_additional_detail_fields();
4907         }
4908
4909         /**
4910          * Override's SugarBean's
4911          */
4912         function fill_in_additional_detail_fields() {
4913                 if(!empty($this->service)) {
4914                         $exServ = explode('::', $this->service);
4915                         $this->tls              = $exServ[0];
4916                         if ( isset($exServ[1]) )
4917                             $this->ca           = $exServ[1];
4918                         if ( isset($exServ[2]) )
4919                             $this->ssl          = $exServ[2];
4920                         if ( isset($exServ[3]) )
4921                             $this->protocol     = $exServ[3];
4922                 }
4923         }
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938         ///////////////////////////////////////////////////////////////////////////
4939         ////    IN SUPPORT OF EMAIL 2.0
4940         /**
4941          * Checks for $user's autoImport setting and returns the current value
4942          * @param object $user User in focus, defaults to $current_user
4943          * @return bool
4944          */
4945         function isAutoImport($user=null) {
4946                 if(!empty($this->autoImport)) {
4947                         return $this->autoImport;
4948                 }
4949
4950                 global $current_user;
4951                 if(empty($user)) $user = $current_user;
4952
4953                 $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
4954                 $emailSettings = is_string($emailSettings) ? unserialize($emailSettings) : $emailSettings;
4955
4956                 $this->autoImport = (isset($emailSettings['autoImport']) && !empty($emailSettings['autoImport'])) ? true : false;
4957                 return $this->autoImport;
4958         }
4959
4960         /**
4961          * Clears out cache files for a user
4962          */
4963         function cleanOutCache() {
4964                 $GLOBALS['log']->debug("INBOUNDEMAIL: at cleanOutCache()");
4965                 $this->deleteCache();
4966         }
4967
4968         /**
4969          * moves emails from folder to folder
4970          * @param string $fromIe I-E id
4971          * @param string $fromFolder IMAP path to folder in which the email lives
4972          * @param string $toIe I-E id
4973          * @param string $toFolder
4974          * @param string $uids UIDs of emails to move, either Sugar GUIDS or IMAP
4975          * UIDs
4976          */
4977         function copyEmails($fromIe, $fromFolder, $toIe, $toFolder, $uids) {
4978                 $this->moveEmails($fromIe, $fromFolder, $toIe, $toFolder, $uids, true);
4979         }
4980
4981         /**
4982          * moves emails from folder to folder
4983          * @param string $fromIe I-E id
4984          * @param string $fromFolder IMAP path to folder in which the email lives
4985          * @param string $toIe I-E id
4986          * @param string $toFolder
4987          * @param string $uids UIDs of emails to move, either Sugar GUIDS or IMAP
4988          * UIDs
4989          * @param bool $copy Default false
4990          * @return bool True on successful execution
4991          */
4992         function moveEmails($fromIe, $fromFolder, $toIe, $toFolder, $uids, $copy=false) {
4993                 global $app_strings;
4994                 global $current_user;
4995
4996
4997                 // same I-E server
4998                 if($fromIe == $toIe) {
4999                         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from I-E to I-E");
5000                         //$exDestFolder = explode("::", $toFolder);
5001                         //preserve $this->mailbox
5002                 if (isset($this->mailbox)) {
5003                     $oldMailbox = $this->mailbox;
5004                 }
5005
5006
5007                         $this->retrieve($fromIe);
5008                     $this->mailbox = $fromFolder;
5009                         $this->connectMailserver();
5010                         $exUids = explode('::;::', $uids);
5011                         $uids = implode(",", $exUids);
5012                         // imap_mail_move accepts comma-delimited lists of UIDs
5013                         if($copy) {
5014                                 if(imap_mail_copy($this->conn, $uids, $toFolder, CP_UID)) {
5015                                         $this->mailbox = $toFolder;
5016                                         $this->connectMailserver();
5017                                         $newOverviews = imap_fetch_overview($this->conn, $uids, FT_UID);
5018                                         $this->updateOverviewCacheFile($newOverviews, 'append');
5019                                     if (isset($oldMailbox)) {
5020                         $this->mailbox = $oldMailbox;
5021                     }
5022                                         return true;
5023                                 } else {
5024                                         $GLOBALS['log']->debug("INBOUNDEMAIL: could not imap_mail_copy() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
5025                                 }
5026                         } else {
5027                                 if(imap_mail_move($this->conn, $uids, $toFolder, CP_UID)) {
5028                                         $GLOBALS['log']->info("INBOUNDEMAIL: imap_mail_move() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
5029                                         imap_expunge($this->conn); // hard deletes moved messages
5030
5031                                         // update cache on fromFolder
5032                                         $newOverviews = $this->getOverviewsFromCacheFile($uids, $fromFolder, true);
5033                                         $this->deleteCachedMessages($uids, $fromFolder);
5034
5035                                         // update cache on toFolder
5036                                         $this->checkEmailOneMailbox($toFolder, true, true);
5037                                     if (isset($oldMailbox)) {
5038                         $this->mailbox = $oldMailbox;
5039                     }
5040
5041                                         return true;
5042                                 } else {
5043                                         $GLOBALS['log']->debug("INBOUNDEMAIL: could not imap_mail_move() [ {$uids} ] to folder [ {$toFolder} ] from folder [ {$fromFolder} ]");
5044                                 }
5045                         }
5046                 } elseif($toIe == 'folder' && $fromFolder == 'sugar::Emails') {
5047                         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from SugarFolder to SugarFolder");
5048                         // move from sugar folder to sugar folder
5049                         require_once("include/SugarFolders/SugarFolders.php");
5050                         $sugarFolder = new SugarFolder();
5051                         $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
5052                         foreach($exUids as $id) {
5053                                 if($copy) {
5054                                         $sugarFolder->copyBean($fromIe, $toFolder, $id, "Emails");
5055                                 } else {
5056                                         $fromSugarFolder = new SugarFolder();
5057                                         $fromSugarFolder->retrieve($fromIe);
5058                                         $toSugarFolder = new SugarFolder();
5059                                         $toSugarFolder->retrieve($toFolder);
5060
5061                                         $email = new Email();
5062                                         $email->retrieve($id);
5063                                         $email->status = 'unread';
5064
5065                                         // when you move from My Emails to Group Folder, Assign To field for the Email should become null
5066                                         if ($fromSugarFolder->is_dynamic && $toSugarFolder->is_group) {
5067                         // Bug 50972 - assigned_user_id set to empty string not true null
5068                         // Modifying the field defs in just this one place to allow
5069                         // a true null since this is what is expected when reading
5070                         // inbox folders
5071                         $email->setFieldNullable('assigned_user_id');
5072                                                 $email->assigned_user_id = "";
5073                                                 $email->save();
5074                         $email->revertFieldNullable('assigned_user_id');
5075                         // End fix 50972
5076                                                 if (!$toSugarFolder->checkEmailExistForFolder($id)) {
5077                                                         $fromSugarFolder->deleteEmailFromAllFolder($id);
5078                                                         $toSugarFolder->addBean($email);
5079                                                 }
5080                                         } elseif ($fromSugarFolder->is_group && $toSugarFolder->is_dynamic) {
5081                                                 $fromSugarFolder->deleteEmailFromAllFolder($id);
5082                                                 $email->assigned_user_id = $current_user->id;
5083                                                 $email->save();
5084                                         } else {
5085                                                 // If you are moving something from personal folder then delete an entry from all folder
5086                                                 if (!$fromSugarFolder->is_dynamic && !$fromSugarFolder->is_group) {
5087                                                         $fromSugarFolder->deleteEmailFromAllFolder($id);
5088                                                 } // if
5089
5090                                                 if ($fromSugarFolder->is_dynamic && !$toSugarFolder->is_dynamic && !$toSugarFolder->is_group) {
5091                                                         $email->assigned_user_id = "";
5092                                                         $toSugarFolder->addBean($email);
5093                                                 } // if
5094                                                 if (!$toSugarFolder->checkEmailExistForFolder($id)) {
5095                                                         if (!$toSugarFolder->is_dynamic) {
5096                                                                 $fromSugarFolder->deleteEmailFromAllFolder($id);
5097                                                                 $toSugarFolder->addBean($email);
5098                                                         } else {
5099                                                                 $fromSugarFolder->deleteEmailFromAllFolder($id);
5100                                                                 $email->assigned_user_id = $current_user->id;
5101                                                         }
5102                                                 } else {
5103                                                         $sugarFolder->move($fromIe, $toFolder, $id);
5104                                                 } // else
5105                                                 $email->save();
5106                                         } // else
5107                                 }
5108                         }
5109
5110                         return true;
5111                 } elseif($toIe == 'folder') {
5112                         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() moving email from I-E to SugarFolder");
5113                         // move to Sugar folder
5114                         require_once("include/SugarFolders/SugarFolders.php");
5115                         $sugarFolder = new SugarFolder();
5116                         $sugarFolder->retrieve($toFolder);
5117                         //Show the import form if we don't have the required info
5118                         if (!isset($_REQUEST['delete'])) {
5119                                 $json = getJSONobj();
5120                                 if ($sugarFolder->is_group) {
5121                                         $_REQUEST['showTeam'] = false;
5122                                         $_REQUEST['showAssignTo'] = false;
5123                                 }
5124                     $ret = $this->email->et->getImportForm($_REQUEST, $this->email);
5125                     $ret['move'] = true;
5126                     $ret['srcFolder'] = $fromFolder;
5127                     $ret['srcIeId']   = $fromIe;
5128                     $ret['dstFolder'] = $toFolder;
5129                     $ret['dstIeId']   = $toIe;
5130                     $out = trim($json->encode($ret, false));
5131                     echo  $out;
5132                     return true;
5133                         }
5134
5135
5136                         // import to Sugar
5137                         $this->retrieve($fromIe);
5138                         $this->mailbox = $fromFolder;
5139                         $this->connectMailserver();
5140                         // If its a group folder the team should be of the folder team
5141                         if ($sugarFolder->is_group) {
5142                                 $_REQUEST['team_id'] = $sugarFolder->team_id;
5143                                 $_REQUEST['team_set_id'] = $sugarFolder->team_set_id;
5144                         } else {
5145                                 // TODO - set team_id, team_set for new UI
5146                         } // else
5147
5148                         $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
5149
5150                         if(!empty($sugarFolder->id)) {
5151                                 $count = 1;
5152                                 $return = array();
5153                                 $json = getJSONobj();
5154                                 foreach($exUids as $k => $uid) {
5155                                         $msgNo = $uid;
5156                                         if ($this->isPop3Protocol()) {
5157                                                 $msgNo = $this->getCorrectMessageNoForPop3($uid);
5158                                         } else {
5159                                                 $msgNo = imap_msgno($this->conn, $uid);
5160                                         }
5161
5162                                         if(!empty($msgNo)) {
5163                                                 $importStatus = $this->importOneEmail($msgNo, $uid);
5164                                                 // add to folder
5165                                                 if($importStatus) {
5166                                                         $sugarFolder->addBean($this->email);
5167                                                         if(!$copy && isset($_REQUEST['delete']) && ($_REQUEST['delete'] == "true") && $importStatus) {
5168                                                                 $GLOBALS['log']->error("********* delete from mailserver [ {explode(",", $uids)} ]");
5169                                                                 // delete from mailserver
5170                                                                 $this->deleteMessageOnMailServer($uid);
5171                                                                 $this->deleteMessageFromCache($uid);
5172                                                         } // if
5173                                                 }
5174                                                 $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($importStatus ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
5175                                                 $count++;
5176                                         } // if
5177                                 } // foreach
5178                                 echo $json->encode($return);
5179                                 return true;
5180                         } else {
5181                                 $GLOBALS['log']->error("********* SUGARFOLDER - failed to retrieve folder ID [ {$toFolder} ]");
5182                         }
5183                 } else {
5184                         $GLOBALS['log']->debug("********* SUGARFOLDER - moveEmails() called with no passing criteria");
5185                 }
5186
5187                 return false;
5188         }
5189
5190
5191         /**
5192          * Hard deletes an I-E account
5193          * @param string id GUID
5194          */
5195         function hardDelete($id) {
5196                 $q = "DELETE FROM inbound_email WHERE id = '{$id}'";
5197                 $r = $this->db->query($q, true);
5198         }
5199
5200         /**
5201          * Generate a unique filename for attachments based on the message id.  There are no maximum
5202          * specifications for the length of the message id, the only requirement is that it be globally unique.
5203          *
5204          * @param bool $nameOnly Whether or not the attachment count should be appended to the filename.
5205          * @return string The temp file name
5206          */
5207         function getTempFilename($nameOnly=false) {
5208
5209         $str = $this->compoundMessageId;
5210
5211                 if(!$nameOnly) {
5212                         $str = $str.$this->attachmentCount;
5213                         $this->attachmentCount++;
5214                 }
5215
5216                 return $str;
5217         }
5218
5219         /**
5220          * deletes and expunges emails on server
5221          * @param string $uid UID(s), comma delimited, of email(s) on server
5222          * @return bool true on success
5223          */
5224         function deleteMessageOnMailServer($uid) {
5225                 global $app_strings;
5226                 $this->connectMailserver();
5227
5228                 if(strpos($uid, $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
5229                         $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uid);
5230                 } else {
5231                         $uids[] = $uid;
5232                 }
5233
5234                 $return = true;
5235
5236                 if($this->protocol == 'imap') {
5237                         $trashFolder = $this->get_stored_options("trashFolder");
5238                         if (empty($trashFolder)) {
5239                                 $trashFolder = "INBOX.Trash";
5240                         }
5241                         foreach($uids as $uid) {
5242                         if($this->moveEmails($this->id, $this->mailbox, $this->id, $trashFolder, $uid))
5243                         $GLOBALS['log']->debug("INBOUNDEMAIL: MoveEmail to {$trashFolder} successful.");
5244                     else {
5245                         $GLOBALS['log']->debug("INBOUNDEMAIL: MoveEmail to {$trashFolder} FAILED - trying hard delete for message: $uid");
5246                         imap_delete($this->conn, $uid, FT_UID);
5247                                         $return = true;
5248                     }
5249                 }
5250                 }
5251         else {
5252             $msgnos = array();
5253                 foreach($uids as $uid) {
5254                 $msgnos[] = $this->getCorrectMessageNoForPop3($uid);
5255                         }
5256                         $msgnos = implode(',', $msgnos);
5257                         imap_delete($this->conn, $msgnos);
5258                         $return = true;
5259                 }
5260
5261                 if(!imap_expunge($this->conn)) {
5262             $GLOBALS['log']->debug("NOOP: could not expunge deleted email.");
5263             $return = false;
5264          }
5265          else
5266             $GLOBALS['log']->info("INBOUNDEMAIL: hard-deleted mail with MSgno's' [ {$msgnos} ]");
5267
5268                 return $return;
5269         }
5270
5271         /**
5272          * deletes and expunges emails on server
5273          * @param string $uid UID(s), comma delimited, of email(s) on server
5274          */
5275         function deleteMessageOnMailServerForPop3($uid) {
5276                 if(imap_delete($this->conn, $uid)) {
5277             if(!imap_expunge($this->conn)) {
5278                 $GLOBALS['log']->debug("NOOP: could not expunge deleted email.");
5279                 $return = false;
5280             } else {
5281                 $GLOBALS['log']->info("INBOUNDEMAIL: hard-deleted mail with MSgno's' [ {$uid} ]");
5282             }
5283                 }
5284         }
5285
5286         /**
5287          * Checks if this is a pop3 type of an account or not
5288          * @return boolean
5289          */
5290         function isPop3Protocol() {
5291                 return ($this->protocol == 'pop3');
5292         }
5293
5294         /**
5295          * Gets the UIDL from database for the corresponding msgno
5296          * @param int messageNo of a message
5297          * @return UIDL for the message
5298          */
5299         function getUIDLForMessage($msgNo) {
5300                 $query = "SELECT message_id FROM email_cache WHERE ie_id = '{$this->id}' AND msgno = '{$msgNo}'";
5301                 $r = $this->db->query($query);
5302                 $a = $this->db->fetchByAssoc($r);
5303                 return $a['message_id'];
5304         }
5305                 /**
5306          * Get the users default IE account id
5307          *
5308          * @param User $user
5309          * @return string
5310          */
5311         function getUsersDefaultOutboundServerId($user)
5312         {
5313                 $id =  $user->getPreference($this->keyForUsersDefaultIEAccount,'Emails',$user);
5314                 //If no preference has been set, grab the default system id.
5315                 if(empty($id))
5316                 {
5317                         $oe = new OutboundEmail();
5318                         $system = $oe->getSystemMailerSettings();
5319                         $id=empty($system->id) ? '' : $system->id;
5320                 }
5321
5322                 return $id;
5323         }
5324
5325         /**
5326          * Get the users default IE account id
5327          *
5328          * @param User $user
5329          */
5330         function setUsersDefaultOutboundServerId($user,$oe_id)
5331         {
5332                 $user->setPreference($this->keyForUsersDefaultIEAccount, $oe_id, '', 'Emails');
5333         }
5334         /**
5335          * Gets the UIDL from database for the corresponding msgno
5336          * @param int messageNo of a message
5337          * @return UIDL for the message
5338          */
5339         function getMsgnoForMessageID($messageid) {
5340                 $query = "SELECT msgno FROM email_cache WHERE ie_id = '{$this->id}' AND message_id = '{$messageid}'";
5341                 $r = $this->db->query($query);
5342                 $a = $this->db->fetchByAssoc($r);
5343                 return $a['message_id'];
5344         }
5345
5346         /**
5347          * fills InboundEmail->email with an email's details
5348          * @param int uid Unique ID of email
5349          * @param bool isMsgNo flag that passed ID is msgNo, default false
5350          * @param bool setRead Sets the 'seen' flag in cache
5351          * @param bool forceRefresh Skips cache file
5352          * @return string
5353          */
5354         function setEmailForDisplay($uid, $isMsgNo=false, $setRead=false, $forceRefresh=false) {
5355
5356                 if(empty($uid)) {
5357                         $GLOBALS['log']->debug("*** ERROR: INBOUNDEMAIL trying to setEmailForDisplay() with no UID");
5358                         return 'NOOP';
5359                 }
5360
5361                 global $sugar_config;
5362                 global $app_strings;
5363
5364                 // if its a pop3 then get the UIDL and see if this file name exist or not
5365                 if ($this->isPop3Protocol()) {
5366                         // get the UIDL from database;
5367                         $cachedUIDL = md5($uid);
5368                         $cache = "{$this->EmailCachePath}/{$this->id}/messages/{$this->mailbox}{$cachedUIDL}.php";
5369                 } else {
5370                         $cache = "{$this->EmailCachePath}/{$this->id}/messages/{$this->mailbox}{$uid}.php";
5371                 }
5372
5373                 if(file_exists($cache) && !$forceRefresh) {
5374                         $GLOBALS['log']->info("INBOUNDEMAIL: Using cache file for setEmailForDisplay()");
5375
5376                         include($cache); // profides $cacheFile
5377             /** @var $cacheFile array */
5378
5379             $metaOut = unserialize($cacheFile['out']);
5380                         $meta = $metaOut['meta']['email'];
5381                         $email = new Email();
5382
5383                         foreach($meta as $k => $v) {
5384                                 $email->$k = $v;
5385                         }
5386
5387                         $email->to_addrs = $meta['toaddrs'];
5388                         $email->date_sent = $meta['date_start'];
5389                         //_ppf($email,true);
5390
5391                         $this->email = $email;
5392                         $this->email->email2init();
5393                         $ret = 'cache';
5394                 } else {
5395                         $GLOBALS['log']->info("INBOUNDEMAIL: opening new connection for setEmailForDisplay()");
5396             if($this->isPop3Protocol()) {
5397                 $msgNo = $this->getCorrectMessageNoForPop3($uid);
5398             } else {
5399                                 if(empty($this->conn)) {
5400                                         $this->connectMailserver();
5401                                 }
5402                 $msgNo = ($isMsgNo) ? $uid : imap_msgno($this->conn, $uid);
5403             }
5404                         if(empty($this->conn)) {
5405                                 $status = $this->connectMailserver();
5406                                 if($status == "false") {
5407                                         $this->email = new Email();
5408                                         $this->email->name = $app_strings['LBL_EMAIL_ERROR_MAILSERVERCONNECTION'];
5409                                         $ret = 'error';
5410                                         return $ret;
5411                                 }
5412
5413                         }
5414
5415                         $this->importOneEmail($msgNo, $uid, true);
5416                         $this->email->id = '';
5417                         $this->email->new_with_id = false;
5418                         $ret = 'import';
5419                 }
5420
5421                 if($setRead) {
5422                         $this->setStatuses($uid, 'seen', 1);
5423                 }
5424
5425                 return $ret;
5426         }
5427
5428
5429         /**
5430          * Sets status for a particular attribute on the mailserver and the local cache file
5431          */
5432         function setStatuses($uid, $field, $value) {
5433                 global $sugar_config;
5434                 /** available status fields
5435                     [subject] => aaa
5436                     [from] => Some Name
5437                     [to] => Some Name
5438                     [date] => Mon, 22 Jan 2007 17:32:57 -0800
5439                     [message_id] =>
5440                     [size] => 718
5441                     [uid] => 191
5442                     [msgno] => 141
5443                     [recent] => 0
5444                     [flagged] => 0
5445                     [answered] => 0
5446                     [deleted] => 0
5447                     [seen] => 1
5448                     [draft] => 0
5449                 */
5450                 // local cache
5451                 $file = "{$this->mailbox}.imapFetchOverview.php";
5452                 $overviews = $this->getCacheValueForUIDs($this->mailbox, array($uid));
5453
5454                 if(!empty($overviews)) {
5455                         $updates = array();
5456
5457                         foreach($overviews['retArr'] as $k => $obj) {
5458                                 if($obj->imap_uid == $uid) {
5459                                         $obj->$field = $value;
5460                                         $updates[] = $obj;
5461                                 }
5462                         }
5463
5464                         if(!empty($updates)) {
5465                                 $this->setCacheValue($this->mailbox, array(), $updates);
5466                         }
5467                 }
5468         }
5469
5470         /**
5471          * Removes an email from the cache file, deletes the message from the cache too
5472          * @param string String of uids, comma delimited
5473          */
5474         function deleteMessageFromCache($uids) {
5475                 global $sugar_config;
5476                 global $app_strings;
5477
5478                 // delete message cache file and email_cache file
5479                 $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
5480
5481                 foreach($exUids as $uid) {
5482                         // local cache
5483                         if ($this->isPop3Protocol()) {
5484                                 $q = "DELETE FROM email_cache WHERE message_id = '{$uid}' AND ie_id = '{$this->id}'";
5485                         } else {
5486                                 $q = "DELETE FROM email_cache WHERE imap_uid = {$uid} AND ie_id = '{$this->id}'";
5487                         }
5488                         $r = $this->db->query($q);
5489                         if ($this->isPop3Protocol()) {
5490                                 $uid = md5($uid);
5491                         } // if
5492                         $msgCacheFile = "{$this->EmailCachePath}/{$this->id}/messages/{$this->mailbox}{$uid}.php";
5493                         if(file_exists($msgCacheFile)) {
5494                                 if(!unlink($msgCacheFile)) {
5495                                         $GLOBALS['log']->error("***ERROR: InboundEmail could not delete the cache file [ {$msgCacheFile} ]");
5496                                 }
5497                         }
5498                 }
5499         }
5500
5501
5502         /**
5503          * Shows one email.
5504          * @param int uid UID of email to display
5505          * @param string mbox Mailbox to look in for the message
5506          * @param bool isMsgNo Flag to assume $uid is a MessageNo, not UniqueID, default false
5507          */
5508         function displayOneEmail($uid, $mbox, $isMsgNo=false) {
5509                 require_once("include/JSON.php");
5510
5511                 global $timedate;
5512                 global $app_strings;
5513                 global $app_list_strings;
5514                 global $sugar_smarty;
5515                 global $theme;
5516                 global $current_user;
5517                 global $sugar_config;
5518
5519                 $fetchedAttributes = array(
5520                         'name',
5521                         'from_name',
5522                         'from_addr',
5523                         'date_start',
5524                         'time_start',
5525                         'message_id',
5526                 );
5527
5528                 $souEmail = array();
5529                 foreach($fetchedAttributes as $k) {
5530                         if ($k == 'date_start') {
5531                                 $this->email->$k . " " . $this->email->time_start;
5532                                 $souEmail[$k] = $this->email->$k . " " . $this->email->time_start;
5533                         } elseif ($k == 'time_start') {
5534                                 $souEmail[$k] = "";
5535                         } else {
5536                                 $souEmail[$k] = trim($this->email->$k);
5537                         }
5538                 }
5539
5540                 // if a MsgNo is passed in, convert to UID
5541                 if($isMsgNo)
5542                         $uid = imap_uid($this->conn, $uid);
5543
5544                 // meta object to allow quick retrieval for replies
5545                 $meta = array();
5546                 $meta['type'] = $this->email->type;
5547                 $meta['uid'] = $uid;
5548                 $meta['ieId'] = $this->id;
5549                 $meta['email'] = $souEmail;
5550                 $meta['mbox'] = $this->mailbox;
5551                 $ccs = '';
5552                 // imap vs pop3
5553
5554                 // self mapping
5555                 $exMbox = explode("::", $mbox);
5556
5557                 // CC section
5558                 $cc = '';
5559                 if(!empty($this->email->cc_addrs)) {
5560                         //$ccs = $this->collapseLongMailingList($this->email->cc_addrs);
5561                         $ccs = to_html($this->email->cc_addrs_names);
5562                         $cc =<<<eoq
5563                                 <tr>
5564                                         <td NOWRAP valign="top" class="displayEmailLabel">
5565                                                 {$app_strings['LBL_EMAIL_CC']}:
5566                                         </td>
5567                                         <td class="displayEmailValue">
5568                                                 {$ccs}
5569                                         </td>
5570                                 </tr>
5571 eoq;
5572                 }
5573                 $meta['cc'] = $cc;
5574                 $meta['email']['cc_addrs'] = $ccs;
5575                 // attachments
5576                 $attachments = '';
5577                 if ($mbox == "sugar::Emails") {
5578
5579                         $q = "SELECT id, filename, file_mime_type FROM notes WHERE parent_id = '{$uid}' AND deleted = 0";
5580                         $r = $this->db->query($q);
5581                         $i = 0;
5582                         while($a = $this->db->fetchByAssoc($r)) {
5583                                 $url = "index.php?entryPoint=download&type=notes&id={$a['id']}";
5584                                 $lbl = ($i == 0) ? $app_strings['LBL_EMAIL_ATTACHMENTS'].":" : '';
5585                                 $i++;
5586                                 $attachments .=<<<EOQ
5587                                 <tr>
5588                                                         <td NOWRAP valign="top" class="displayEmailLabel">
5589                                                                 {$lbl}
5590                                                         </td>
5591                                                         <td NOWRAP valign="top" colspan="2" class="displayEmailValue">
5592                                                                 <a href="{$url}">{$a['filename']}</a>
5593                                                         </td>
5594                                                 </tr>
5595 EOQ;
5596                                 $this->email->cid2Link($a['id'], $a['file_mime_type']);
5597                     } // while
5598
5599
5600                 } else {
5601
5602                         if($this->attachmentCount > 0) {
5603                                 $theCount = $this->attachmentCount;
5604
5605                                 for($i=0; $i<$theCount; $i++) {
5606                                         $lbl = ($i == 0) ? $app_strings['LBL_EMAIL_ATTACHMENTS'].":" : '';
5607                                         $name = $this->getTempFilename(true).$i;
5608                                         $tempName = urlencode($this->tempAttachment[$name]);
5609
5610                                         $url = "index.php?entryPoint=download&type=temp&isTempFile=true&ieId={$this->id}&tempName={$tempName}&id={$name}";
5611
5612                                         $attachments .=<<<eoq
5613                                                 <tr>
5614                                                         <td NOWRAP valign="top" class="displayEmailLabel">
5615                                                                 {$lbl}
5616                                                         </td>
5617                                                         <td NOWRAP valign="top" colspan="2" class="displayEmailValue">
5618                                                                 <a href="{$url}">{$this->tempAttachment[$name]}</a>
5619                                                         </td>
5620                                                 </tr>
5621 eoq;
5622                                 } // for
5623                         } // if
5624                 } // else
5625                 $meta['email']['attachments'] = $attachments;
5626
5627                 // toasddrs
5628                 $meta['email']['toaddrs'] = $this->collapseLongMailingList($this->email->to_addrs);
5629                 $meta['email']['cc_addrs'] = $ccs;
5630
5631                 // body
5632                 $description = (empty($this->email->description_html)) ? nl2br($this->email->description) : $this->email->description_html;
5633                 $meta['email']['description'] = $description;
5634
5635                 // meta-metadata
5636                 $meta['is_sugarEmail'] = ($exMbox[0] == 'sugar') ? true : false;
5637
5638                 if(!$meta['is_sugarEmail']) {
5639                         if($this->isAutoImport) {
5640                                 $meta['is_sugarEmail'] = true;
5641                         }
5642                 } else {
5643                         if( $this->email->status != 'sent' ){
5644                                 // mark SugarEmail read
5645                                 $q = "UPDATE emails SET status = 'read' WHERE id = '{$uid}'";
5646                                 $r = $this->db->query($q);
5647                         }
5648                 }
5649
5650                 $return = array();
5651         $meta['email']['name'] = to_html($this->email->name);
5652         $meta['email']['from_addr'] = ( !empty($this->email->from_addr_name) ) ? to_html($this->email->from_addr_name) : to_html($this->email->from_addr);
5653         $meta['email']['toaddrs'] = ( !empty($this->email->to_addrs_names) ) ? to_html($this->email->to_addrs_names) : to_html($this->email->to_addrs);
5654         $meta['email']['cc_addrs'] = to_html($this->email->cc_addrs_names);
5655         $meta['email']['reply_to_addr'] = to_html($this->email->reply_to_addr);
5656                 $return['meta'] = $meta;
5657
5658                 return $return;
5659         }
5660
5661         /**
5662          * Takes a long list of email addresses from a To or CC field and shows the first 3, the rest hidden
5663          * @param string emails
5664          * @return string
5665          */
5666         function collapseLongMailingList($emails) {
5667                 global $app_strings;
5668
5669                 $ex = explode(",", $emails);
5670                 $i = 0;
5671                 $j = 0;
5672
5673                 if(count($ex) > 3) {
5674                         $emails = "";
5675                         $emailsHidden = "";
5676
5677                         foreach($ex as $email) {
5678                                 if($i < 2) {
5679                                         if(!empty($emails)) {
5680                                                 $emails .= ", ";
5681                                         }
5682                                         $emails .= trim($email);
5683                                 } else {
5684                                         if(!empty($emailsHidden)) {
5685                                                 $emailsHidden .= ", ";
5686                                         }
5687                                         $emailsHidden .= trim($email);
5688                                         $j++;
5689                                 }
5690                                 $i++;
5691                         }
5692
5693                         if(!empty($emailsHidden)) {
5694                                 $email2 = $emails;
5695                                 $emails = "<span onclick='javascript:SUGAR.email2.detailView.showFullEmailList(this);' style='cursor:pointer;'>{$emails} [...{$j} {$app_strings['LBL_MORE']}]</span>";
5696                                 $emailsHidden = "<span onclick='javascript:SUGAR.email2.detailView.showCroppedEmailList(this)' style='cursor:pointer; display:none;'>{$email2}, {$emailsHidden} [ {$app_strings['LBL_LESS']} ]</span>";
5697                         }
5698
5699                         $emails .= $emailsHidden;
5700                 }
5701
5702                 return $emails;
5703         }
5704
5705
5706         /**
5707          * Sorts IMAP's imap_fetch_overview() results
5708          * @param array $arr Array of standard objects
5709          * @param string $sort Column to sort by
5710          * @param string direction Direction to sort by (asc/desc)
5711          * @return array Sorted array of obj.
5712          */
5713         function sortFetchedOverview($arr, $sort=4, $direction='DESC', $forceSeen=false) {
5714                 global $current_user;
5715
5716                 $sortPrefs = $current_user->getPreference('folderSortOrder', 'Emails');
5717                 if(!empty($sortPrefs))
5718                         $listPrefs = $sortPrefs;
5719                 else
5720                         $listPrefs = array();
5721
5722                 if(isset($listPrefs[$this->id][$this->mailbox])) {
5723                         $currentNode = $listPrefs[$this->id][$this->mailbox];
5724                 }
5725
5726                 if(isset($currentNode['current']) && !empty($currentNode['current'])) {
5727                         $sort = $currentNode['current']['sort'];
5728                         $direction = $currentNode['current']['direction'];
5729                 }
5730
5731                 // sort defaults
5732                 if(empty($sort)) {
5733                         $sort = $this->defaultSort;//4;
5734                         $direction = $this->defaultDirection; //'DESC';
5735                 } elseif(!is_numeric($sort)) {
5736                         // handle bad sort index
5737                         $sort = $this->defaultSort;
5738                 } else {
5739                         // translate numeric index to human readable
5740             $sort = $this->hrSort[$sort];
5741                 }
5742                 if(empty($direction)) {
5743                         $direction = 'DESC';
5744                 }
5745
5746
5747
5748                 $retArr = array();
5749                 $sorts = array();
5750
5751                 foreach($arr as $k => $overview) {
5752                         $sorts['flagged'][$k] = $overview->flagged;
5753                         $sorts['status'][$k] = $overview->answered;
5754                         $sorts['from'][$k] = str_replace('"', "", $this->handleMimeHeaderDecode($overview->from));
5755                         $sorts['subj'][$k] = $this->handleMimeHeaderDecode(quoted_printable_decode($overview->subject));
5756                         $sorts['date'][$k] = $overview->date;
5757                 }
5758
5759                 // sort by column
5760                 natcasesort($sorts[$sort]);
5761                 //_ppd($sorts[$sort]);
5762                 // direction
5763                 if(strtolower($direction) == 'desc') {
5764                         $revSorts = array();
5765                         $keys = array_reverse(array_keys($sorts[$sort]));
5766 //                      _pp("count keys in DESC: ".count($keys));
5767 //                      _pp("count elements in sort[sort]: ".count($sorts[$sort]));
5768
5769                         for($i=0; $i<count($keys); $i++) {
5770                                 $v = $keys[$i];
5771                                 $revSorts[$v] = $sorts[$sort][$v];
5772                         }
5773
5774                         //_pp("count post-sort: ".count($revSorts));
5775                         $sorts[$sort] = $revSorts;
5776                 }
5777         $timedate = TimeDate::getInstance();
5778                 foreach($sorts[$sort] as $k2 => $overview2) {
5779                     $arr[$k2]->date = $timedate->fromString($arr[$k2]->date)->asDb();
5780                         $retArr[] = $arr[$k2];
5781                 }
5782                 //_pp("final count: ".count($retArr));
5783
5784                 $finalReturn = array();
5785                 $finalReturn['retArr'] = $retArr;
5786                 $finalReturn['sortBy'] = $sort;
5787                 $finalReturn['direction'] = $direction;
5788                 return $finalReturn;
5789         }
5790
5791
5792         function setReadFlagOnFolderCache($mbox, $uid) {
5793                 global $sugar_config;
5794
5795                 $this->mailbox = $mbox;
5796
5797                 // cache
5798                 if($this->validCacheExists($this->mailbox)) {
5799                         $ret = $this->getCacheValue($this->mailbox);
5800
5801                         $updates = array();
5802
5803                         foreach($ret as $k => $v) {
5804                                 if($v->imap_uid == $uid) {
5805                                         $v->seen = 1;
5806                                         $updates[] = $v;
5807                                         break;
5808                                 }
5809                         }
5810
5811                         $this->setCacheValue($this->mailbox, array(), $updates);
5812                 }
5813         }
5814
5815         /**
5816          * Returns a list of emails in a mailbox.
5817          * @param string mbox Name of mailbox using dot notation paths to display
5818          * @param string $forceRefresh Flag to use cache or not
5819          */
5820         function displayFolderContents($mbox, $forceRefresh='false', $page) {
5821                 global $current_user;
5822
5823                 $delimiter = $this->get_stored_options('folderDelimiter');
5824                 if ($delimiter) {
5825                         $mbox = str_replace('.', $delimiter, $mbox);
5826                 }
5827
5828                 $this->mailbox = $mbox;
5829
5830                 // jchi #9424, get sort and direction from user preference
5831                 $sort = 'date';
5832                 $direction = 'desc';
5833                 $sortSerial = $current_user->getPreference('folderSortOrder', 'Emails');
5834                 if(!empty($sortSerial) && !empty($_REQUEST['ieId']) && !empty($_REQUEST['mbox'])) {
5835                         $sortArray = unserialize($sortSerial);
5836                         $sort = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['sort'];
5837                         $direction = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['direction'];
5838                 }
5839                 //end
5840
5841                 // save sort order
5842                 if(!empty($_REQUEST['sort']) && !empty($_REQUEST['dir'])) {
5843                         $this->email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['mbox'], $_REQUEST['sort'], $_REQUEST['dir']);
5844                         $sort = $_REQUEST['sort'];
5845                         $direction = $_REQUEST['dir'];
5846                 } else {
5847                         $_REQUEST['sort'] = '';
5848                         $_REQUEST['dir'] = '';
5849                 }
5850
5851                 // cache
5852                 $ret = array();
5853                 $cacheUsed = false;
5854                 if($forceRefresh == 'false' && $this->validCacheExists($this->mailbox)) {
5855                         $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
5856
5857                         // cn: default to a low number until user specifies otherwise
5858                         if(empty($emailSettings['showNumInList'])) {
5859                                 $emailSettings['showNumInList'] = 20;
5860                         }
5861
5862                         $ret = $this->getCacheValue($this->mailbox, $emailSettings['showNumInList'], $page, $sort, $direction);
5863                         $cacheUsed = true;
5864                 }
5865
5866                 $out = $this->displayFetchedSortedListXML($ret, $mbox);
5867
5868                 $metadata = array();
5869                 $metadata['mbox'] = $mbox;
5870                 $metadata['ieId'] = $this->id;
5871                 $metadata['name'] = $this->name;
5872                 $metadata['fromCache'] = $cacheUsed ? 1 : 0;
5873                 $metadata['out'] = $out;
5874
5875                 return $metadata;
5876         }
5877
5878         /**
5879          * For a group email account, create subscriptions for all users associated with the
5880          * team assigned to the account.
5881          *
5882          */
5883         function createUserSubscriptionsForGroupAccount()
5884         {
5885             $team = new Team();
5886             $team->retrieve($this->team_id);
5887             $usersList = $team->get_team_members(true);
5888             foreach($usersList as $userObject)
5889             {
5890                 $previousSubscriptions = unserialize(base64_decode($userObject->getPreference('showFolders', 'Emails',$userObject)));
5891                 if($previousSubscriptions === FALSE)
5892                     $previousSubscriptions = array();
5893
5894                 $previousSubscriptions[] = $this->id;
5895
5896                 $encodedSubs = base64_encode(serialize($previousSubscriptions));
5897                 $userObject->setPreference('showFolders',$encodedSubs , '', 'Emails');
5898                 $userObject->savePreferencesToDB();
5899             }
5900     }
5901         /**
5902     * Create a sugar folder for this inbound email account
5903     * if the Enable Auto Import option is selected
5904     *
5905     * @return String Id of the sugar folder created.
5906     */
5907         function createAutoImportSugarFolder()
5908         {
5909             global $current_user;
5910             $guid = create_guid();
5911             $GLOBALS['log']->debug("Creating Sugar Folder for IE with id $guid");
5912             $folder = new SugarFolder();
5913             $folder->id = $guid;
5914             $folder->new_with_id = TRUE;
5915             $folder->name = $this->name;
5916             $folder->has_child = 0;
5917             $folder->is_group = 1;
5918             $folder->assign_to_id = $current_user->id;
5919             $folder->parent_folder = "";
5920
5921
5922             //If this inbound email is marked as inactive, don't add subscriptions.
5923             $addSubscriptions = ($this->status == 'Inactive' || $this->mailbox_type == 'bounce') ? FALSE : TRUE;
5924             $folder->save($addSubscriptions);
5925
5926             return $guid;
5927         }
5928
5929         function validCacheExists($mbox) {
5930                 $q = "SELECT count(*) c FROM email_cache WHERE ie_id = '{$this->id}'";
5931                 $r = $this->db->query($q);
5932                 $a = $this->db->fetchByAssoc($r);
5933                 $count = $a['c'];
5934
5935                 if($count > 0) {
5936                         return true;
5937                 }
5938
5939                 return false;
5940         }
5941
5942
5943
5944
5945         function displayFetchedSortedListXML($ret, $mbox) {
5946
5947                 global $timedate;
5948                 global $current_user;
5949                 global $sugar_config;
5950
5951                 if(empty($ret['retArr'])) {
5952                     return array();
5953                 }
5954
5955                 $tPref = $current_user->getUserDateTimePreferences();
5956
5957                 $return = array();
5958
5959                 foreach($ret['retArr'] as $msg) {
5960
5961                         $flagged        = ($msg->flagged == 0) ? "" : $this->iconFlagged;
5962                         $status         = ($msg->deleted) ? $this->iconDeleted : "";
5963                         $status         = ($msg->draft == 0) ? $status : $this->iconDraft;
5964                         $status         = ($msg->answered == 0) ? $status : $this->iconAnswered;
5965                         $from           = $this->handleMimeHeaderDecode($msg->from);
5966                         $subject        = $this->handleMimeHeaderDecode($msg->subject);
5967                         //$date         = date($tPref['date']." ".$tPref['time'], $msg->date);
5968                         $date           = $timedate->to_display_date_time($this->db->fromConvert($msg->date, 'datetime'));
5969                         //$date         = date($tPref['date'], $this->getUnixHeaderDate($msg->date));
5970
5971                         $temp = array();
5972                         $temp['flagged'] = $flagged;
5973                         $temp['status'] = $status;
5974                         $temp['from']   = to_html($from);
5975                         $temp['subject'] = $subject;
5976                         $temp['date']   = $date;
5977                         $temp['uid'] = $msg->uid; // either from an imap_search() or massaged cache value
5978                         $temp['mbox'] = $this->mailbox;
5979                         $temp['ieId'] = $this->id;
5980                         $temp['site_url'] = $sugar_config['site_url'];
5981                         $temp['seen'] = $msg->seen;
5982                         $temp['type'] = (isset($msg->type)) ? $msg->type: 'remote';
5983                         $temp['to_addrs'] = to_html($msg->to);
5984                         $temp['hasAttach'] = '0';
5985
5986                         $return[] = $temp;
5987                 }
5988
5989                 return $return;
5990         }
5991
5992
5993
5994         /**
5995          * retrieves the mailboxes for a given account in the following format
5996          * Array(
5997             [INBOX] => Array
5998                 (
5999                     [Bugs] => Bugs
6000                     [Builder] => Builder
6001                     [DEBUG] => Array
6002                         (
6003                             [out] => out
6004                             [test] => test
6005                         )
6006                 )
6007          * @param bool $justRaw Default false
6008          * @return array
6009          */
6010         function getMailboxes($justRaw=false) {
6011                 if($justRaw == true) {
6012                         return $this->mailboxarray;
6013                 } // if
6014
6015                 return $this->generateMultiDimArrayFromFlatArray($this->mailboxarray, $this->retrieveDelimiter());
6016                 /*
6017                 $serviceString = $this->getConnectString('', '', false);
6018
6019                 if(strpos($serviceString, 'pop3')) {
6020                         $obj = new temp();
6021                         $obj->name = $serviceString."INBOX";
6022                         $boxes = array("INBOX" => $obj);
6023                 } else {
6024                         $boxes = imap_getmailboxes($this->conn, $serviceString, "*");
6025                 }
6026                 $raw = array();
6027                 //_ppd($boxes);
6028                 $delimiter = '.';
6029                 // clean MBOX path names
6030                 foreach($boxes as $k => $mbox) {
6031                         $raw[] = str_replace($serviceString, "", $mbox->name);
6032                         if ($mbox->delimiter) {
6033                                 $delimiter = $mbox->delimiter;
6034                         }
6035                 }
6036                 $storedOptions = unserialize(base64_decode($this->stored_options));
6037                 $storedOptions['folderDelimiter'] = $delimiter;
6038                 $this->stored_options = base64_encode(serialize($storedOptions));
6039         $this->save();
6040
6041                 sort($raw);
6042                 //_ppd($raw);
6043
6044                 // used by $this->search()
6045                 if($justRaw == true) {
6046                         return $raw;
6047                 }
6048
6049
6050                 // generate a multi-dimensional array to iterate through
6051                 $ret = array();
6052                 foreach($raw as $mbox) {
6053                         $ret = $this->sortMailboxes($mbox, $ret, $delimiter);
6054                 }
6055                 //_ppd($ret);
6056                 return $ret;
6057                 */
6058         }
6059
6060         function getMailBoxesForGroupAccount() {
6061                 $mailboxes = $this->generateMultiDimArrayFromFlatArray(explode(",", $this->mailbox), $this->retrieveDelimiter());
6062                 $mailboxesArray = $this->generateFlatArrayFromMultiDimArray($mailboxes, $this->retrieveDelimiter());
6063                 $mailboxesArray = $this->filterMailBoxFromRaw(explode(",", $this->mailbox), $mailboxesArray);
6064                 $this->saveMailBoxFolders($mailboxesArray);
6065                 /*
6066                 if ($this->mailbox != $this->$email_user) {
6067                         $mailboxes = $this->sortMailboxes($this->mailbox, $this->retrieveDelimiter());
6068                         $mailboxesArray = $this->generateFlatArrayFromMultiDimArray($mailboxes, $this->retrieveDelimiter());
6069                         $this->saveMailBoxFolders($mailboxesArray);
6070                         // save mailbox value of an inbound email account to email user
6071                         $this->saveMailBoxValueOfInboundEmail();
6072                 } else {
6073                         $mailboxes = $this->getMailboxes();
6074                 }
6075                 */
6076                 return $mailboxes;
6077         } // fn
6078
6079         function saveMailBoxFolders($value) {
6080                 if (is_array($value)) {
6081                         $value = implode(",", $value);
6082                 }
6083                 $this->mailboxarray = explode(",", $value);
6084                 $value = $this->db->quoted($value);
6085                 $query = "update inbound_email set mailbox = $value where id ='{$this->id}'";
6086                 $this->db->query($query);
6087         }
6088
6089         function insertMailBoxFolders($value) {
6090                 $query = "select value from config where category='InboundEmail' and name='{$this->id}'";
6091                 $r = $this->db->query($query);
6092                 $a = $this->db->fetchByAssoc($r);
6093                 if (empty($a['value'])) {
6094                         if (is_array($value)) {
6095                                 $value = implode(",", $value);
6096                         }
6097                         $this->mailboxarray = explode(",", $value);
6098                         $value = $this->db->quoted($value);
6099
6100                         $query = "INSERT INTO config VALUES('InboundEmail', '{$this->id}', $value)";
6101                         $this->db->query($query);
6102                 } // if
6103         }
6104
6105         function saveMailBoxValueOfInboundEmail() {
6106                 $query = "update Inbound_email set mailbox = '{$this->email_user}'";
6107                 $this->db->query($query);
6108         }
6109
6110         function retrieveMailBoxFolders() {
6111                 $this->mailboxarray = explode(",", $this->mailbox);
6112                 /*
6113                 $query = "select value from config where category='InboundEmail' and name='{$this->id}'";
6114                 $r = $this->db->query($query);
6115                 $a = $this->db->fetchByAssoc($r);
6116                 $this->mailboxarray = explode(",", $a['value']);
6117                 */
6118         } // fn
6119
6120
6121         function retrieveDelimiter() {
6122                 $delimiter = $this->get_stored_options('folderDelimiter');
6123         if (!$delimiter) {
6124                 $delimiter = '.';
6125         }
6126                 return $delimiter;
6127         } // fn
6128
6129         function generateFlatArrayFromMultiDimArray($arraymbox, $delimiter) {
6130                 $ret = array();
6131                 foreach($arraymbox as $key => $value) {
6132                         $this->generateArrayData($key, $value, $ret, $delimiter);
6133                 } // foreach
6134                 return $ret;
6135
6136         } // fn
6137
6138         function generateMultiDimArrayFromFlatArray($raw, $delimiter) {
6139                 // generate a multi-dimensional array to iterate through
6140                 $ret = array();
6141                 foreach($raw as $mbox) {
6142                         $ret = $this->sortMailboxes($mbox, $ret, $delimiter);
6143                 }
6144                 return $ret;
6145
6146         } // fn
6147
6148         function generateArrayData($key, $arraymbox, &$ret, $delimiter) {
6149                 $ret [] = $key;
6150                 if (is_array($arraymbox)) {
6151                         foreach($arraymbox as $mboxKey => $value) {
6152                                 $newKey = $key . $delimiter . $mboxKey;
6153                                 $this->generateArrayData($newKey, $value, $ret, $delimiter);
6154                         } // foreach
6155                 } // if
6156         }
6157
6158         /**
6159          * sorts the folders in a mailbox in a multi-dimensional array
6160          * @param string $MBOX
6161          * @param array $ret
6162          * @return array
6163          */
6164         function sortMailboxes($mbox, $ret, $delimeter = ".") {
6165                 if(strpos($mbox, $delimeter)) {
6166                         $node = substr($mbox, 0, strpos($mbox, $delimeter));
6167                         $nodeAfter = substr($mbox, strpos($mbox, $node) + strlen($node) + 1, strlen($mbox));
6168
6169                         if(!isset($ret[$node])) {
6170                                 $ret[$node] = array();
6171                         } elseif(isset($ret[$node]) && !is_array($ret[$node])) {
6172                                 $ret[$node] = array();
6173                         }
6174                         $ret[$node] = $this->sortMailboxes($nodeAfter, $ret[$node], $delimeter);
6175                 } else {
6176                         $ret[$mbox] = $mbox;
6177                 }
6178
6179                 return $ret;
6180         }
6181
6182         /**
6183          * parses Sugar's storage method for imap server service strings
6184          * @return string
6185          */
6186         function getServiceString() {
6187                 $service = '';
6188                 $exServ = explode('::', $this->service);
6189
6190                 foreach($exServ as $v) {
6191                         if(!empty($v) && ($v != 'imap' && $v !='pop3')) {
6192                                 $service .= '/'.$v;
6193                         }
6194                 }
6195                 return $service;
6196         }
6197
6198
6199     /**
6200      * Get Email messages IDs from server which aren't in database
6201      * @return array Ids of messages, which aren't still in database
6202      */
6203     public function getNewEmailsForSyncedMailbox()
6204     {
6205         // ids's count limit for batch processing
6206         $limit = 20;
6207         $msgIds = imap_search($this->conn, 'ALL UNDELETED');
6208         $result = array();
6209         try{
6210             if(count($msgIds) > 0)
6211             {
6212                 /*
6213                  * @var collect results of queries and message headers
6214                  */
6215                 $tmpMsgs = array();
6216                 $repeats = 0;
6217                 $counter = 0;
6218
6219                 // sort IDs to get lastest on top
6220                 arsort($msgIds);
6221                 $GLOBALS['log']->debug('-----> getNewEmailsForSyncedMailbox() got '.count($msgIds).' Messages');
6222                 foreach($msgIds as $k => &$msgNo)
6223                 {
6224                     $uid = imap_uid($this->conn, $msgNo);
6225                     $header = imap_headerinfo($this->conn, $msgNo);
6226                     $fullHeader = imap_fetchheader($this->conn, $msgNo);
6227                     $message_id = $header->message_id;
6228                     $deliveredTo = $this->id;
6229                     $matches = array();
6230                     preg_match('/(delivered-to:|x-real-to:){1}\s*(\S+)\s*\n{1}/im', $fullHeader, $matches);
6231                     if(count($matches))
6232                     {
6233                         $deliveredTo = $matches[2];
6234                     }
6235                     if(empty($message_id) || !isset($message_id))
6236                     {
6237                         $GLOBALS['log']->debug('*********** NO MESSAGE_ID.');
6238                         $message_id = $this->getMessageId($header);
6239                     }
6240
6241                     // generate compound messageId
6242                     $this->compoundMessageId = trim($message_id) . trim($deliveredTo);
6243                     // if the length > 255 then md5 it so that the data will be of smaller length
6244                     if (strlen($this->compoundMessageId) > 255)
6245                     {
6246                         $this->compoundMessageId = md5($this->compoundMessageId);
6247                     } // if
6248
6249                     if (empty($this->compoundMessageId))
6250                     {
6251                         break;
6252                     } // if
6253                     $counter++;
6254                     $potentials = clean_xss($this->compoundMessageId, false);
6255
6256                     if(is_array($potentials) && !empty($potentials))
6257                     {
6258                         foreach($potentials as $bad)
6259                         {
6260                             $this->compoundMessageId = str_replace($bad, "", $this->compoundMessageId);
6261                         }
6262                     }
6263                     array_push($tmpMsgs, array('msgNo' => $msgNo, 'msgId' => $this->compoundMessageId, 'exists' => 0));
6264                     if($counter == $limit)
6265                     {
6266                         $counter = 0;
6267                         $query = array();
6268                         foreach(array_slice($tmpMsgs, -$limit, $limit) as $k1 => $v1)
6269                         {
6270                             $query[] = $v1['msgId'];
6271                         }
6272                         $query = 'SELECT count(emails.message_id) as cnt, emails.message_id AS mid FROM emails WHERE emails.message_id IN ("' . implode('","', $query) . '") and emails.deleted = 0 group by emails.message_id';
6273                         $r = $this->db->query($query);
6274                         $tmp = array();
6275                         while($a = $this->db->fetchByAssoc($r))
6276                         {
6277                             $tmp[html_entity_decode($a['mid'])] = $a['cnt'];
6278                         }
6279                         foreach($tmpMsgs as $k1 => $v1)
6280                         {
6281                             if(isset($tmp[$v1['msgId']]) && $tmp[$v1['msgId']] > 0)
6282                             {
6283                                 $tmpMsgs[$k1]['exists'] = 1;
6284                             }
6285                         }
6286                         foreach($tmpMsgs as $k1 => $v1)
6287                         {
6288                             if($v1['exists'] == 0)
6289                             {
6290                                 $repeats = 0;
6291                                 array_push($result, $v1['msgNo']);
6292                             }else{
6293                                 $repeats++;
6294                             }
6295                         }
6296                         if($repeats > 0)
6297                         {
6298                             if($repeats >= $limit)
6299                             {
6300                                 break;
6301                             }
6302                             else
6303                             {
6304                                 $tmpMsgs = array_splice($tmpMsgs, -$repeats, $repeats);
6305                             }
6306                         }
6307                         else
6308                         {
6309                             $tmpMsgs = array();
6310                         }
6311                     }
6312                 }
6313                 unset($msgNo);
6314             }
6315         }catch(Exception $ex)
6316         {
6317             $GLOBALS['log']->fatal($ex->getMessage());
6318         }
6319         $GLOBALS['log']->debug('-----> getNewEmailsForSyncedMailbox() got '.count($result).' unsynced messages');
6320         return $result;
6321     }
6322
6323 } // end class definition
6324
6325
6326 /**
6327  * Simple class to mirror the passed object from an imap_fetch_overview() call
6328  */
6329 class Overview {
6330         var $subject;
6331         var $from;
6332         var $fromaddr;
6333         var $to;
6334         var $toaddr;
6335         var $date;
6336         var $message_id;
6337         var $size;
6338         var $uid;
6339         var $msgno;
6340         var $recent;
6341         var $flagged;
6342         var $answered;
6343         var $deleted;
6344         var $seen;
6345         var $draft;
6346         var $indices; /* = array(
6347
6348                         array(
6349                                 'name'                  => 'mail_date',
6350                                 'type'                  => 'index',
6351                                 'fields'                => array(
6352                                         'mbox',
6353                                         'senddate',
6354                                 )
6355                         ),
6356                         array(
6357                                 'name'                  => 'mail_from',
6358                                 'type'                  => 'index',
6359                                 'fields'                => array(
6360                                         'mbox',
6361                                         'fromaddr',
6362                                 )
6363                         ),
6364                         array(
6365                                 'name'                  => 'mail_subj',
6366                                 'type'                  => 'index',
6367                                 'fields'                => array(
6368                                         'mbox',
6369                                         'subject',
6370                                 )
6371                         ),
6372                 );
6373         */
6374         var $fieldDefs;/* = array(
6375                         'mbox' => array(
6376                                 'name'          => 'mbox',
6377                                 'type'          => 'varchar',
6378                                 'len'           => 60,
6379                                 'required'      => true,
6380                         ),
6381                         'subject' => array(
6382                                 'name'          => 'subject',
6383                                 'type'          => 'varchar',
6384                                 'len'           => 100,
6385                                 'required'      => false,
6386                         ),
6387                         'fromaddr' => array(
6388                                 'name'          => 'fromaddr',
6389                                 'type'          => 'varchar',
6390                                 'len'           => 100,
6391                                 'required'      => true,
6392                         ),
6393                         'toaddr' => array(
6394                                 'name'          => 'toaddr',
6395                                 'type'          => 'varchar',
6396                                 'len'           => 100,
6397                                 'required'      => true,
6398                         ),
6399                         'senddate' => array(
6400                                 'name'          => 'senddate',
6401                                 'type'          => 'datetime',
6402                                 'required'      => true,
6403                         ),
6404                         'message_id' => array(
6405                                 'name'          => 'message_id',
6406                                 'type'          => 'varchar',
6407                                 'len'           => 255,
6408                                 'required'      => false,
6409                         ),
6410                         'mailsize' => array(
6411                                 'name'          => 'mailsize',
6412                                 'type'          => 'uint',
6413                                 'len'           => 16,
6414                                 'required'      => true,
6415                         ),
6416                         'uid' => array(
6417                                 'name'          => 'uid',
6418                                 'type'          => 'uint',
6419                                 'len'           => 32,
6420                                 'required'      => true,
6421                         ),
6422                         'msgno' => array(
6423                                 'name'          => 'msgno',
6424                                 'type'          => 'uint',
6425                                 'len'           => 32,
6426                                 'required'      => false,
6427                         ),
6428                         'recent' => array(
6429                                 'name'          => 'recent',
6430                                 'type'          => 'tinyint',
6431                                 'len'           => 1,
6432                                 'required'      => true,
6433                         ),
6434                         'flagged' => array(
6435                                 'name'          => 'flagged',
6436                                 'type'          => 'tinyint',
6437                                 'len'           => 1,
6438                                 'required'      => true,
6439                         ),
6440                         'answered' => array(
6441                                 'name'          => 'answered',
6442                                 'type'          => 'tinyint',
6443                                 'len'           => 1,
6444                                 'required'      => true,
6445                         ),
6446                         'deleted' => array(
6447                                 'name'          => 'deleted',
6448                                 'type'          => 'tinyint',
6449                                 'len'           => 1,
6450                                 'required'      => true,
6451                         ),
6452                         'seen' => array(
6453                                 'name'          => 'seen',
6454                                 'type'          => 'tinyint',
6455                                 'len'           => 1,
6456                                 'required'      => true,
6457                         ),
6458                         'draft' => array(
6459                                 'name'          => 'draft',
6460                                 'type'          => 'tinyint',
6461                                 'len'           => 1,
6462                                 'required'      => true,
6463                         ),
6464                 );
6465         */
6466         function Overview() {
6467                 global $dictionary;
6468
6469                 if(!isset($dictionary['email_cache']) || empty($dictionary['email_cache'])) {
6470                         if(file_exists('custom/metadata/email_cacheMetaData.php')) {
6471                            include('custom/metadata/email_cacheMetaData.php');
6472                         } else {
6473                            include('metadata/email_cacheMetaData.php');
6474                         }
6475                 }
6476
6477                 $this->fieldDefs = $dictionary['email_cache']['fields'];
6478                 $this->indices = $dictionary['email_cache']['indices'];
6479         }
6480 }