Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
EmailArchivingUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
49  public static function resolveOwnerOfEmailMessage(ImapMessage $emailMessage)
50  {
51  if (isset($emailMessage->fromEmail) && $emailMessage->fromEmail != '')
52  {
53  $searchAttributeData = array();
54  $searchAttributeData['clauses'] = array(
55  1 => array(
56  'attributeName' => 'primaryEmail',
57  'relatedAttributeName' => 'emailAddress',
58  'operatorType' => 'equals',
59  'value' => $emailMessage->fromEmail,
60  ),
61  2 => array(
62  'attributeName' => 'secondaryEmail',
63  'relatedAttributeName' => 'emailAddress',
64  'operatorType' => 'equals',
65  'value' => $emailMessage->fromEmail,
66  ),
67  3 => array(
68  'attributeName' => 'isActive',
69  'operatorType' => 'equals',
70  'value' => 1,
71  )
72  );
73  $searchAttributeData['structure'] = '((1 or 2) and 3)';
74  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('User');
75  $where = RedBeanModelDataProvider::makeWhere('User', $searchAttributeData, $joinTablesAdapter);
76  $models = User::getSubset($joinTablesAdapter, null, null, $where, null);
77  }
78 
79  if (count($models) == 1)
80  {
81  return $models[0];
82  }
83  else
84  {
85  throw new NotFoundException();
86  }
87  }
88 
96  public static function resolveEmailSenderFromEmailMessage(ImapMessage $emailMessage)
97  {
98  // Check if email is forwarded or not.
99  $emailSender = false;
100  if (self::isMessageForwarded($emailMessage))
101  {
102  // Somebody sent email to user, and the user forwarded it to dropbox,
103  // sender is in body->from field
104  $emailSender = self::resolveEmailSenderFromForwardedEmailMessage($emailMessage);
105  }
106  else
107  {
108  // User sent email to somebody, and to dropbox(to, cc, bcc), so the user is sender
109  $emailSender['email'] = $emailMessage->fromEmail;
110  if (isset($emailMessage->fromName))
111  {
112  $emailSender['name'] = $emailMessage->fromName;
113  }
114  else
115  {
116  $emailSender['name'] = '';
117  }
118  }
119  return $emailSender;
120  }
121 
131  public static function resolveEmailRecipientsFromEmailMessage(ImapMessage $emailMessage)
132  {
133  // Check if email is forwarded or not.
134  if (self::isMessageForwarded($emailMessage))
135  {
136  // Somebody sent email to Zurmo user, the user forwarded it to dropbox, so the user is a recipient
137  $emailRecipients = array(
138  array(
139  'email' => $emailMessage->fromEmail,
140  'name' => $emailMessage->fromName,
141  'type' => EmailMessageRecipient::TYPE_TO
142  )
143  );
144  }
145  else
146  {
147  $emailRecipients = array();
148  // Zurmo user sent email, so recipients are in 'To' and 'CC' fields
149  if (!empty($emailMessage->to))
150  {
151  foreach ($emailMessage->to as $key => $value)
152  {
153  $emailMessage->to[$key]['type'] = EmailMessageRecipient::TYPE_TO;
154  if ($value['email'] == Yii::app()->imap->imapUsername)
155  {
156  unset($emailMessage->to[$key]);
157  }
158  }
159  $emailRecipients = array_merge($emailRecipients, $emailMessage->to);
160  }
161  if (!empty($emailMessage->cc))
162  {
163  foreach ($emailMessage->cc as $key => $value)
164  {
165  $emailMessage->cc[$key]['type'] = EmailMessageRecipient::TYPE_CC;
166  if ($value['email'] == Yii::app()->imap->imapUsername)
167  {
168  unset($emailMessage->cc[$key]);
169  }
170  }
171  $emailRecipients = array_merge($emailRecipients, $emailMessage->cc);
172  }
173  }
174  if (count($emailRecipients) == 0)
175  {
176  throw new NotSupportedException();
177  }
178  $emailRecipients = ArrayUtil::arrayUniqueRecursive($emailRecipients);
179  //After removing duplicates email get removed if its the same as name
180  foreach ($emailRecipients as $key => $emailRecipient)
181  {
182  if (!isset($emailRecipient['email']))
183  {
184  $emailRecipients[$key]['email'] = $emailRecipient['name'];
185  }
186  }
187  return $emailRecipients;
188  }
189 
196  public static function isMessageForwarded(ImapMessage $emailMessage)
197  {
198  $isForwarded = false;
199  foreach ($emailMessage->to as $toAddress)
200  {
201  $dropboxEmail = strtolower(str_replace('+', '@', Yii::app()->imap->imapUsername)); // Not Coding Standard
202  if (strtolower($toAddress['email']) == $dropboxEmail)
203  {
204  $isForwarded = true;
205  break;
206  }
207  }
208  return $isForwarded;
209  }
210 
217  public static function resolveEmailSenderFromForwardedEmailMessage(ImapMessage $emailMessage)
218  {
219  $emailSender = false;
220  $pattern = '/^\s*(?:.*?):\s*(.*)?\s*(?:\[mailto:|<)([\w.%+\-]+@[\w.\-]+\.[A-Za-z]{2,6})(?:[\]>])(?:.*)?$/mi'; // Not Coding Standard
221  $noOfMatches = false;
222  if ($emailMessage->textBody != '')
223  {
224  $noOfMatches = preg_match($pattern, $emailMessage->textBody, $matches);
225  }
226  elseif ($emailMessage->htmlBody != '')
227  {
228  $noOfMatches = preg_match($pattern, $emailMessage->htmlBody, $matches);
229  }
230 
231  if ($noOfMatches > 0)
232  {
233  $emailSender['name'] = trim($matches[1]);
234  $emailSender['email'] = trim($matches[2]);
235  }
236 
237  return $emailSender;
238  }
239 
247  public static function getPersonsAndAccountsByEmailAddressForUser($emailAddress, User $user)
248  {
249  assert('is_string($emailAddress)');
250  $userCanAccessContacts = RightsUtil::canUserAccessModule('ContactsModule', $user);
251  $userCanAccessLeads = RightsUtil::canUserAccessModule('LeadsModule', $user);
252  $userCanAccessAccounts = RightsUtil::canUserAccessModule('AccountsModule', $user);
253  return self::getPersonsAndAccountsByEmailAddress($emailAddress,
254  $userCanAccessContacts,
255  $userCanAccessLeads,
256  $userCanAccessAccounts);
257  }
258 
267  public static function getPersonsAndAccountsByEmailAddress($emailAddress,
268  $userCanAccessContacts = false,
269  $userCanAccessLeads = false,
270  $userCanAccessAccounts = false)
271  {
272  assert('is_string($emailAddress)');
273  assert('is_bool($userCanAccessContacts)');
274  assert('is_bool($userCanAccessLeads)');
275  assert('is_bool($userCanAccessAccounts)');
276  $personsAndAccounts = array();
277  if ($userCanAccessContacts || $userCanAccessLeads)
278  {
279  $stateMetadataAdapterClassName = LeadsStateMetadataAdapter::
280  resolveStateMetadataAdapterClassNameByAccess($userCanAccessContacts, $userCanAccessLeads);
281  $personsAndAccounts = ContactSearch::
282  getContactsByAnyEmailAddress($emailAddress, 1, $stateMetadataAdapterClassName);
283  }
284  if ($userCanAccessAccounts)
285  {
286  $personsAndAccounts = array_merge($personsAndAccounts,
288  }
289 
290  $personsAndAccounts = array_merge($personsAndAccounts,
292 
293  return $personsAndAccounts;
294  }
295 
304  public static function resolvePersonOrAccountByEmailAddress($emailAddress,
305  $userCanAccessContacts = false,
306  $userCanAccessLeads = false,
307  $userCanAccessAccounts = false)
308  {
309  assert('is_string($emailAddress)');
310  assert('is_bool($userCanAccessContacts)');
311  assert('is_bool($userCanAccessLeads)');
312  assert('is_bool($userCanAccessAccounts)');
313 
314  $personOrAccount = null;
315  $contactsOrLeads = array();
316  if ($userCanAccessContacts || $userCanAccessLeads)
317  {
318  $stateMetadataAdapterClassName = LeadsStateMetadataAdapter::
319  resolveStateMetadataAdapterClassNameByAccess($userCanAccessContacts, $userCanAccessLeads);
320  $contactsOrLeads = ContactSearch::getContactsByAnyEmailAddress($emailAddress, null, $stateMetadataAdapterClassName);
321  }
322 
323  if (!empty($contactsOrLeads))
324  {
325  $personOrAccount = $contactsOrLeads[0];
326  }
327  else
328  {
329  $accounts = array();
330  // Check if email belongs to account
331  if ($userCanAccessAccounts)
332  {
333  $accounts = AccountSearch::getAccountsByAnyEmailAddress($emailAddress);
334  }
335 
336  if (count($accounts))
337  {
338  $personOrAccount = $accounts[0];
339  }
340  else
341  {
342  $users = UserSearch::getUsersByAnyEmailAddress($emailAddress);
343  if (count($users))
344  {
345  $personOrAccount = $users[0];
346  }
347  }
348  }
349  return $personOrAccount;
350  }
351 
357  public static function resolveSanitizeFromImapToUtf8(EmailMessage $message)
358  {
359  $message->subject = imap_utf8($message->subject);
360  }
361 
370  public static function createEmailMessageSender($senderInfo, $userCanAccessContacts, $userCanAccessLeads,
371  $userCanAccessAccounts)
372  {
373  $sender = new EmailMessageSender();
374  $sender->fromAddress = $senderInfo['email'];
375  if (isset($senderInfo['name']))
376  {
377  $sender->fromName = $senderInfo['name'];
378  }
380  $senderInfo['email'],
381  $userCanAccessContacts,
382  $userCanAccessLeads,
383  $userCanAccessAccounts);
384  if (!empty($personsOrAccounts))
385  {
386  foreach ($personsOrAccounts as $personOrAccount)
387  {
388  $sender->personsOrAccounts->add($personOrAccount);
389  }
390  }
391  return $sender;
392  }
393 
402  public static function createEmailMessageRecipient($recipientInfo, $userCanAccessContacts, $userCanAccessLeads,
403  $userCanAccessAccounts)
404  {
405  $recipient = new EmailMessageRecipient();
406  $recipient->toAddress = $recipientInfo['email'];
407  if (isset($recipientInfo['name']))
408  {
409  $recipient->toName = $recipientInfo['name'];
410  }
411  $recipient->type = $recipientInfo['type'];
412 
414  $recipientInfo['email'],
415  $userCanAccessContacts,
416  $userCanAccessLeads,
417  $userCanAccessAccounts);
418  if (!empty($personsOrAccounts))
419  {
420  foreach ($personsOrAccounts as $personOrAccount)
421  {
422  $recipient->personsOrAccounts->add($personOrAccount);
423  }
424  }
425  return $recipient;
426  }
427 
434  public static function createEmailAttachment($attachment, $base64encoded = false)
435  {
436  // Save attachments
437  if ($attachment['filename'] != null && static::isAttachmentExtensionAllowed($attachment['filename']))
438  {
439  $fileContent = new FileContent();
440  if ($base64encoded)
441  {
442  $fileContent->content = base64_decode($attachment['attachment']);
443  }
444  else
445  {
446  $fileContent->content = $attachment['attachment'];
447  }
448  $file = new FileModel();
449  $file->fileContent = $fileContent;
450  $file->name = $attachment['filename'];
451  $file->type = ZurmoFileHelper::getMimeType($attachment['filename']);
452  $file->size = strlen($fileContent->content);
453  $saved = $file->save();
454  assert('$saved'); // Not Coding Standard
455  return $file;
456  }
457  else
458  {
459  return false;
460  }
461  }
462 
463  protected static function isAttachmentExtensionAllowed($attachmentFileName)
464  {
465  $allowed = array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'gif', 'png', 'jpg', 'jpeg', 'txt', 'csv');
466  $filenameArray = explode('.', $attachmentFileName);
467  $ext = end($filenameArray);
468  if ($ext !== '')
469  {
470  $ext = strtolower($ext);
471  if (in_array($ext, $allowed))
472  {
473  return true;
474  }
475  }
476  return false;
477  }
478  }
479 ?>
static resolveEmailSenderFromForwardedEmailMessage(ImapMessage $emailMessage)
static getAccountsByAnyEmailAddress($emailAddress, $pageSize=null)
Definition: User.php:37
static getPersonsAndAccountsByEmailAddressForUser($emailAddress, User $user)
static resolveEmailSenderFromEmailMessage(ImapMessage $emailMessage)
static resolveEmailRecipientsFromEmailMessage(ImapMessage $emailMessage)
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static resolvePersonOrAccountByEmailAddress($emailAddress, $userCanAccessContacts=false, $userCanAccessLeads=false, $userCanAccessAccounts=false)
static getMimeType($file, $magicFile=null, $checkExtension=true)
static getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $modelClassName=null, $selectDistinct=false)
static createEmailMessageSender($senderInfo, $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts)
static resolveOwnerOfEmailMessage(ImapMessage $emailMessage)
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static isMessageForwarded(ImapMessage $emailMessage)
static createEmailAttachment($attachment, $base64encoded=false)
static getUsersByAnyEmailAddress($emailAddress, $operatorType=null, $filterOutHideFromSelecting=false, $autoCompleteOptions=null, $pageSize=null)
Definition: UserSearch.php:156
static getPersonsAndAccountsByEmailAddress($emailAddress, $userCanAccessContacts=false, $userCanAccessLeads=false, $userCanAccessAccounts=false)
static getContactsByAnyEmailAddress($emailAddress, $pageSize=null, $stateMetadataAdapterClassName=null, $autoCompleteOptions=null)
static resolveStateMetadataAdapterClassNameByAccess($userCanAccessContacts, $userCanAccessLeads)
static resolveSanitizeFromImapToUtf8(EmailMessage $message)
static createEmailMessageRecipient($recipientInfo, $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts)
Generated on Sun Aug 9 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.