65 public $element =
'emailcollector';
70 public $table_element =
'emailcollector_emailcollector';
75 public $picto =
'email';
80 public $fk_element =
'fk_emailcollector';
85 protected $childtables = array();
90 protected $childtablesoncascade = array(
'emailcollector_emailcollectorfilter',
'emailcollector_emailcollectoraction');
116 public $fields = array(
117 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'visible' => -2,
'enabled' => 1,
'position' => 1,
'notnull' => 1,
'index' => 1),
118 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'enabled' => 1,
'visible' => 0,
'default' =>
'1',
'notnull' => 1,
'index' => 1,
'position' => 20),
119 'ref' => array(
'type' =>
'varchar(128)',
'label' =>
'Ref',
'enabled' => 1,
'visible' => 1,
'notnull' => 1,
'showoncombobox' => 1,
'index' => 1,
'position' => 10,
'searchall' => 1,
'help' =>
'Example: MyCollector1',
'csslist' =>
'tdoverflowmax200'),
120 'label' => array(
'type' =>
'varchar(255)',
'label' =>
'Label',
'visible' => 1,
'enabled' => 1,
'position' => 30,
'notnull' => -1,
'searchall' => 1,
'help' =>
'Example: My Email collector',
'csslist' =>
'tdoverflowmax150 small',
'tdcss' =>
'titlefieldmiddle'),
121 'description' => array(
'type' =>
'text',
'label' =>
'Description',
'visible' => -1,
'enabled' => 1,
'position' => 60,
'notnull' => -1,
'cssview' =>
'small',
'csslist' =>
'small tdoverflowmax200'),
122 'host' => array(
'type' =>
'varchar(255)',
'label' =>
'EMailHost',
'visible' => 1,
'enabled' => 1,
'position' => 90,
'notnull' => 1,
'searchall' => 1,
'comment' =>
"IMAP server",
'help' =>
'Example: imap.gmail.com',
'csslist' =>
'tdoverflowmax125'),
123 'port' => array(
'type' =>
'varchar(10)',
'label' =>
'EMailHostPort',
'visible' => 1,
'enabled' => 1,
'position' => 91,
'notnull' => 1,
'searchall' => 0,
'comment' =>
"IMAP server port",
'help' =>
'Example: 993',
'csslist' =>
'tdoverflowmax50',
'default' =>
'993'),
124 'imap_encryption' => array(
'type' =>
'varchar(16)',
'label' =>
'ImapEncryption',
'visible' => -1,
'enabled' => 1,
'position' => 92,
'searchall' => 0,
'comment' =>
"IMAP encryption",
'help' =>
'ImapEncryptionHelp',
'arrayofkeyval' => array(
'ssl' =>
'SSL',
'tls' =>
'TLS',
'notls' =>
'NOTLS'),
'default' =>
'ssl'),
125 'hostcharset' => array(
'type' =>
'varchar(16)',
'label' =>
'HostCharset',
'visible' => -1,
'enabled' => 1,
'position' => 93,
'notnull' => 0,
'searchall' => 0,
'comment' =>
"IMAP server charset",
'help' =>
'Example: "UTF-8" (May be "US-ASCII" with some Office365)',
'default' =>
'UTF-8'),
126 'norsh' => array(
'type' =>
'integer',
'label' =>
'NoRSH',
'visible' => -1,
'enabled' =>
"!getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')",
'position' => 94,
'searchall' => 0,
'help' =>
'NoRSHHelp',
'arrayofkeyval' => array(0 =>
'No', 1 =>
'Yes'),
'default' =>
'0'),
127 'acces_type' => array(
'type' =>
'integer',
'label' =>
'AuthenticationMethod',
'visible' => -1,
'enabled' =>
"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')",
'position' => 101,
'notnull' => 1,
'index' => 1,
'comment' =>
"IMAP login type",
'arrayofkeyval' => array(0 =>
'loginPassword', 1 =>
'oauthToken'),
'default' =>
'0',
'help' =>
''),
128 'login' => array(
'type' =>
'varchar(128)',
'label' =>
'Login',
'visible' => -1,
'enabled' => 1,
'position' => 102,
'notnull' => -1,
'index' => 1,
'comment' =>
"IMAP login",
'help' =>
'Example: myaccount@gmail.com'),
129 'password' => array(
'type' =>
'password',
'label' =>
'Password',
'visible' => -1,
'enabled' =>
"1",
'position' => 103,
'notnull' => -1,
'comment' =>
"IMAP password",
'help' =>
'WithGMailYouCanCreateADedicatedPassword'),
130 'oauth_service' => array(
'type' =>
'varchar(128)',
'label' =>
'oauthService',
'visible' => -1,
'enabled' =>
"getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')",
'position' => 104,
'notnull' => 0,
'index' => 1,
'comment' =>
"IMAP login oauthService",
'arrayofkeyval' => array(),
'help' =>
'TokenMustHaveBeenCreated'),
131 'source_directory' => array(
'type' =>
'varchar(255)',
'label' =>
'MailboxSourceDirectory',
'visible' => -1,
'enabled' => 1,
'position' => 109,
'notnull' => 1,
'default' =>
'Inbox',
'csslist' =>
'tdoverflowmax100',
'help' =>
'Example: INBOX, [Gmail]/Spam, [Gmail]/Draft, [Gmail]/Brouillons, [Gmail]/Sent Mail, [Gmail]/Messages envoyés, ...'),
132 'target_directory' => array(
'type' =>
'varchar(255)',
'label' =>
'MailboxTargetDirectory',
'visible' => 1,
'enabled' => 1,
'position' => 110,
'notnull' => 0,
'csslist' =>
'tdoverflowmax100',
'help' =>
"EmailCollectorTargetDir"),
133 'maxemailpercollect' => array(
'type' =>
'integer',
'label' =>
'MaxEmailCollectPerCollect',
'visible' => -1,
'enabled' => 1,
'position' => 111,
'default' =>
'50'),
134 'datelastresult' => array(
'type' =>
'datetime',
'label' =>
'DateLastCollectResult',
'visible' => 1,
'enabled' =>
'$action != "create" && $action != "edit"',
'position' => 121,
'notnull' => -1,
'csslist' =>
'nowraponall'),
135 'codelastresult' => array(
'type' =>
'varchar(16)',
'label' =>
'CodeLastResult',
'visible' => 1,
'enabled' =>
'$action != "create" && $action != "edit"',
'position' => 122,
'notnull' => -1,),
136 'lastresult' => array(
'type' =>
'varchar(255)',
'label' =>
'LastResult',
'visible' => 1,
'enabled' =>
'$action != "create" && $action != "edit"',
'position' => 123,
'notnull' => -1,
'cssview' =>
'small',
'csslist' =>
'maxwidth250imp'),
137 'datelastok' => array(
'type' =>
'datetime',
'label' =>
'DateLastcollectResultOk',
'visible' => 1,
'enabled' =>
'$action != "create"',
'position' => 125,
'notnull' => -1,
'csslist' =>
'nowraponall'),
138 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'visible' => 0,
'enabled' => 1,
'position' => 61,
'notnull' => -1,),
139 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'visible' => 0,
'enabled' => 1,
'position' => 62,
'notnull' => -1,),
140 'date_creation' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'visible' => -2,
'enabled' => 1,
'position' => 500,
'notnull' => 1,),
141 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'visible' => -2,
'enabled' => 1,
'position' => 501,
'notnull' => 1,),
143 'fk_user_creat' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'visible' => -2,
'enabled' => 1,
'position' => 510,
'notnull' => 1,),
144 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'visible' => -2,
'enabled' => 1,
'position' => 511,
'notnull' => -1,),
146 'import_key' => array(
'type' =>
'varchar(14)',
'label' =>
'ImportId',
'visible' => -2,
'enabled' => 1,
'position' => 1000,
'notnull' => -1,),
147 'status' => array(
'type' =>
'integer',
'label' =>
'Status',
'visible' => 1,
'enabled' => 1,
'position' => 1000,
'notnull' => 1,
'default' =>
'0',
'index' => 1,
'arrayofkeyval' => array(0 =>
'Inactive', 1 =>
'Active'))
179 public $fk_user_creat;
184 public $fk_user_modif;
218 public $oauth_service;
222 public $imap_encryption;
230 public $source_directory;
234 public $target_directory;
238 public $maxemailpercollect;
243 public $datelastresult;
248 public $codelastresult;
273 const STATUS_DISABLED = 0;
274 const STATUS_ENABLED = 1;
284 global
$conf, $langs;
288 $this->ismultientitymanaged = 1;
289 $this->isextrafieldmanaged = 0;
294 if (!
isModEnabled(
'multicompany') && isset($this->fields[
'entity'])) {
295 $this->fields[
'entity'][
'enabled'] = 0;
299 $oauthservices = array();
301 foreach (
$conf->global as $key => $val) {
302 if (!empty($val) && preg_match(
'/^OAUTH_.*_ID$/', $key)) {
303 $key = preg_replace(
'/^OAUTH_/',
'', $key);
304 $key = preg_replace(
'/_ID$/',
'', $key);
305 if (preg_match(
'/^.*-/', $key)) {
306 $name = preg_replace(
'/^.*-/',
'', $key);
308 $name = $langs->trans(
"NoName");
310 $provider = preg_replace(
'/-.*$/',
'', $key);
311 $provider = ucfirst(strtolower($provider));
313 $oauthservices[$key] = $name.
" (".$provider.
")";
317 $this->fields[
'oauth_service'][
'arrayofkeyval'] = $oauthservices;
320 foreach ($this->fields as $key => $val) {
321 if (isset($val[
'enabled']) && empty($val[
'enabled'])) {
322 unset($this->fields[$key]);
327 foreach ($this->fields as $key => $val) {
328 if (!empty($val[
'arrayofkeyval']) && is_array($val[
'arrayofkeyval'])) {
329 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
330 $this->fields[$key][
'arrayofkeyval'][$key2] = $langs->trans($val2);
348 if ($this->host && preg_match(
'/^http:/i', trim($this->host))) {
349 $langs->load(
"errors");
350 $this->error = $langs->trans(
"ErrorHostMustNotStartWithHttp", $this->host);
354 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
355 $this->password =
dolEncrypt($this->password);
359 $this->password =
dolDecrypt($this->password);
361 if (is_array($this->filters) && count($this->filters)) {
364 foreach ($this->filters as $filter) {
365 $emailcollectorfilter->type = $filter[
'type'];
366 $emailcollectorfilter->rulevalue = $filter[
'rulevalue'];
367 $emailcollectorfilter->fk_emailcollector = $this->id;
368 $emailcollectorfilter->status = $filter[
'status'];
370 $emailcollectorfilter->create($user);
374 if (is_array($this->actions) && count($this->actions)) {
377 foreach ($this->actions as $operation) {
378 $emailcollectoroperation->type = $operation[
'type'];
379 $emailcollectoroperation->actionparam = $operation[
'actionparam'];
380 $emailcollectoroperation->fk_emailcollector = $this->id;
381 $emailcollectoroperation->status = $operation[
'status'];
382 $emailcollectoroperation->position = $operation[
'position'];
384 $emailcollectoroperation->create($user);
400 global $langs, $extrafields;
421 unset(
$object->codelastresult);
422 unset(
$object->datelastresult);
427 $object->ref =
"copy_of_".$object->ref;
430 $object->host =
'imap.example.com';
433 if (is_array(
$object->array_options) && count(
$object->array_options) > 0) {
434 $extrafields->fetch_name_optionals_label($this->table_element);
435 foreach (
$object->array_options as $key => $option) {
436 $shortkey = preg_replace(
'/options_/',
'', $key);
437 if (!empty($extrafields->attributes[$this->element][
'unique'][$shortkey])) {
438 unset(
$object->array_options[$key]);
444 $object->context[
'createfromclone'] =
'createfromclone';
445 $result =
$object->create($user);
451 unset(
$object->context[
'createfromclone']);
458 $this->db->rollback();
470 public function fetch($id, $ref =
null)
474 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
475 $this->password =
dolDecrypt($this->password);
508 public function fetchAll(
User $user, $activeOnly = 0, $sortfield =
's.rowid', $sortorder =
'ASC', $limit = 100, $page = 0)
512 $sql =
"SELECT s.rowid";
513 $sql .=
" FROM ".MAIN_DB_PREFIX.
"emailcollector_emailcollector as s";
514 $sql .=
' WHERE s.entity IN ('.getEntity(
'emailcollector').
')';
516 $sql .=
" AND s.status = 1";
518 $sql .= $this->db->order($sortfield, $sortorder);
523 $offset = $limit * $page;
525 $sql .= $this->db->plimit($limit + 1, $offset);
528 $result = $this->db->query($sql);
530 $num = $this->db->num_rows($result);
533 $obj = $this->db->fetch_object($result);
535 if ($emailcollector_static->fetch($obj->rowid)) {
536 $obj_ret[] = $emailcollector_static;
541 $this->errors[] =
'EmailCollector::fetchAll Error when retrieve emailcollector list';
542 dol_syslog(
'EmailCollector::fetchAll Error when retrieve emailcollector list', LOG_ERR);
544 if (!count($obj_ret)) {
545 dol_syslog(
'EmailCollector::fetchAll No emailcollector found', LOG_DEBUG);
563 if ($this->host && preg_match(
'/^http:/i', trim($this->host))) {
564 $langs->load(
"errors");
565 $this->error = $langs->trans(
"ErrorHostMustNotStartWithHttp", $this->host);
569 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
570 $this->password =
dolEncrypt($this->password);
574 $this->password =
dolDecrypt($this->password);
586 public function delete(
User $user, $notrigger = 0)
601 public function getNomUrl($withpicto = 0, $option =
'', $notooltip = 0, $morecss =
'', $save_lastsearch_value = -1)
603 global
$conf, $langs, $action, $hookmanager;
605 if (!empty(
$conf->dol_no_mouse_hover)) {
611 $label =
'<u>'.$langs->trans(
"EmailCollector").
'</u>';
613 $label .=
'<b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
615 $url = DOL_URL_ROOT.
'/admin/emailcollector_card.php?id='.$this->id;
617 if ($option !=
'nolink') {
619 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
620 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
621 $add_save_lastsearch_values = 1;
623 if ($add_save_lastsearch_values) {
624 $url .=
'&save_lastsearch_values=1';
629 if (empty($notooltip)) {
631 $label = $langs->trans(
"ShowEmailCollector");
632 $linkclose .=
' alt="'.dolPrintHTMLForAttribute($label).
'"';
634 $linkclose .=
' title="'.dolPrintHTMLForAttribute($label).
'"';
635 $linkclose .=
' class="classfortooltip'.($morecss ?
' '.$morecss :
'').
'"';
637 $linkclose = ($morecss ?
' class="'.$morecss.
'"' :
'');
640 $linkstart =
'<a href="'.$url.
'"';
641 $linkstart .= $linkclose.
'>';
644 $result .= $linkstart;
646 $result .=
img_object(($notooltip ?
'' : $label), ($this->picto ? $this->picto :
'generic'), ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
648 if ($withpicto != 2) {
649 $result .= $this->ref;
654 $hookmanager->initHooks(array(
'emailcollectordao'));
655 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
656 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
658 $result = $hookmanager->resPrint;
660 $result .= $hookmanager->resPrint;
688 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
691 $this->labelStatus[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Enabled');
692 $this->labelStatus[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
693 $this->labelStatusShort[self::STATUS_ENABLED] = $langs->transnoentitiesnoconv(
'Enabled');
694 $this->labelStatusShort[self::STATUS_DISABLED] = $langs->transnoentitiesnoconv(
'Disabled');
697 $statusType =
'status5';
698 if ($status == self::STATUS_ENABLED) {
699 $statusType =
'status4';
702 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status],
'', $statusType, $mode);
713 $sql =
'SELECT rowid, date_creation as datec, tms as datem,';
714 $sql .=
' fk_user_creat, fk_user_modif';
715 $sql .=
' FROM '.MAIN_DB_PREFIX.$this->table_element.
' as t';
716 $sql .=
' WHERE t.rowid = '.((int) $id);
717 $result = $this->db->query($sql);
719 if ($this->db->num_rows($result)) {
720 $obj = $this->db->fetch_object($result);
722 $this->
id = $obj->rowid;
724 $this->user_creation_id = $obj->fk_user_creat;
725 $this->user_modification_id = $obj->fk_user_modif;
726 $this->date_creation = $this->db->jdate($obj->datec);
727 $this->date_modification = empty($obj->datem) ?
'' : $this->db->jdate($obj->datem);
730 $this->db->free($result);
744 $this->host =
'localhost';
745 $this->login =
'alogin';
758 $this->filters = array();
760 $sql =
'SELECT rowid, type, rulevalue, status';
761 $sql .=
' FROM '.MAIN_DB_PREFIX.
'emailcollector_emailcollectorfilter';
762 $sql .=
' WHERE fk_emailcollector = '.((int) $this->
id);
765 $resql = $this->db->query($sql);
767 $num = $this->db->num_rows($resql);
770 $obj = $this->db->fetch_object($resql);
771 $this->filters[$obj->rowid] = array(
'id' => $obj->rowid,
'type' => $obj->type,
'rulevalue' => $obj->rulevalue,
'status' => $obj->status);
774 $this->db->free($resql);
790 $this->actions = array();
792 $sql =
'SELECT rowid, type, actionparam, status';
793 $sql .=
' FROM '.MAIN_DB_PREFIX.
'emailcollector_emailcollectoraction';
794 $sql .=
' WHERE fk_emailcollector = '.((int) $this->
id);
795 $sql .=
' ORDER BY position';
797 $resql = $this->db->query($sql);
799 $num = $this->db->num_rows($resql);
802 $obj = $this->db->fetch_object($resql);
803 $this->actions[$obj->rowid] = array(
'id' => $obj->rowid,
'type' => $obj->type,
'actionparam' => $obj->actionparam,
'status' => $obj->status);
806 $this->db->free($resql);
825 $flags =
'/service=imap';
828 } elseif (empty($this->imap_encryption) || ($this->imap_encryption ==
'ssl' &&
getDolGlobalString(
'IMAP_FORCE_NOSSL'))) {
831 $flags .=
'/' . $this->imap_encryption;
834 $flags .=
'/novalidate-cert';
841 if (!empty($this->login) && strpos($this->login,
'/') !=
false) {
842 $partofauth = explode(
'/', $this->login);
843 $flags .=
'/authuser='.$partofauth[0].
'/user='.$partofauth[1];
846 $connectstringserver =
'{'.$this->host.
':'.$this->port.$flags.
'}';
848 return $connectstringserver;
859 if (function_exists(
'mb_convert_encoding')) {
861 $str = preg_replace(
"/ /",
"xxxSPACExxx", $str);
862 $str = preg_replace(
"/_/",
"xxxUNDERSCORExxx", $str);
863 $str = preg_replace(
"/\[Gmail\]/",
"xxxGMAILxxx", $str);
865 if ($str = mb_convert_encoding($str,
"UTF-7")) {
867 $str = preg_replace(
"/\+A/",
"&A", $str);
868 $str = preg_replace(
"/xxxUNDERSCORExxx/",
"_", $str);
870 $str = preg_replace(
"/xxxSPACExxx/",
" ", $str);
872 $str = preg_replace(
"/xxxGMAILxxx/",
"[Gmail]", $str);
876 $this->error =
"error: is not possible to encode this string '".$str.
"'";
896 $arrayofcollectors = $this->
fetchAll($user, 1);
898 foreach ($arrayofcollectors as $emailcollector) {
899 $result = $emailcollector->doCollectOneCollector(0);
901 dol_syslog(
"doCollect result = ".$result.
" for emailcollector->id = ".$emailcollector->id);
903 $this->error .=
'EmailCollector ID '.$emailcollector->id.
':'.$emailcollector->error.
'<br>';
904 if (!empty($emailcollector->errors)) {
905 $this->error .= implode(
'<br>', $emailcollector->errors);
908 $this->output .=
'EmailCollector ID '.$emailcollector->id.
': '.$emailcollector->lastresult.
'<br>';
931 global
$conf, $langs;
933 $errorforthisaction = 0;
936 $outputlangs = $langs;
939 $newlang =
GETPOST(
'lang_id',
'aZ09');
942 $newlang = !empty(
$object->thirdparty->default_lang) ?
$object->thirdparty->default_lang : $newlang;
944 if (!empty($newlang)) {
946 $outputlangs->setDefaultLang($newlang);
956 foreach ($arrayvaluetouse as $propertytooverwrite => $valueforproperty) {
959 $tmparray = explode(
'.', $propertytooverwrite);
960 if (count($tmparray) == 2) {
961 $tmpclass = $tmparray[0];
962 $tmpproperty = $tmparray[1];
964 $tmpproperty = $tmparray[0];
966 if ($tmpclass && ($tmpclass !=
$object->element)) {
976 $regforregex = array();
977 if (preg_match(
'/^EXTRACT:([a-zA-Z0-9_]+):(.*):([^:])$/', $valueforproperty, $regforregex)) {
978 $sourcefield = $regforregex[1];
979 $regexstring = $regforregex[2];
981 } elseif (preg_match(
'/^EXTRACT:([a-zA-Z0-9_]+):(.*)$/', $valueforproperty, $regforregex)) {
982 $sourcefield = $regforregex[1];
983 $regexstring = $regforregex[2];
986 if (!empty($sourcefield) && !empty($regexstring)) {
987 if (strtolower($sourcefield) ==
'body') {
988 $sourcestring = $messagetext;
989 } elseif (strtolower($sourcefield) ==
'subject') {
990 $sourcestring = $subject;
991 } elseif (strtolower($sourcefield) ==
'header') {
992 $sourcestring = $header;
996 $regforval = array();
998 if (strtolower($sourcefield) ==
'body') {
999 $regexoptions =
'ms';
1001 if (strtolower($sourcefield) ==
'header') {
1002 $regexoptions =
'm';
1005 if (preg_match(
'/'.$regexstring.
'/'.$regexoptions, $sourcestring, $regforval)) {
1007 $valueextracted = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) :
null;
1008 if (strtolower($sourcefield) ==
'header') {
1009 if (preg_match(
'/^options_/', $tmpproperty)) {
1012 if (property_exists(
$object, $tmpproperty)) {
1019 if (preg_match(
'/^options_/', $tmpproperty)) {
1022 if (property_exists(
$object, $tmpproperty)) {
1030 if (preg_match(
'/^options_/', $tmpproperty)) {
1033 if (property_exists(
$object, $tmpproperty)) {
1041 if (property_exists(
$object, $tmpproperty)) {
1043 $operationslog .=
'<br>Regex /'.dol_escape_htmltag($regexstring).
'/'.
dol_escape_htmltag($regexoptions).
' into '.strtolower($sourcefield).
' -> not found, so object property '.
dol_escape_htmltag($tmpproperty).
' is set to null.';
1045 $tmp[$tmpproperty] =
null;
1046 $operationslog .=
'<br>Regex /'.dol_escape_htmltag($regexstring).
'/'.
dol_escape_htmltag($regexoptions).
' into '.strtolower($sourcefield).
' -> not found, so var '.
dol_escape_htmltag($tmpproperty).
' is set to null.';
1051 $errorforthisaction++;
1052 $this->error =
'The extract rule to use to overwrite properties has on an unknown source (must be HEADER, SUBJECT or BODY)';
1053 $this->errors[] = $this->error;
1055 $operationslog .=
'<br>'.$this->error;
1057 } elseif (preg_match(
'/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $regforregex)) {
1059 if (preg_match(
'/^options_/', $tmpproperty)) {
1060 $valuecurrent =
$object->array_options[preg_replace(
'/^options_/',
'', $tmpproperty)];
1062 if (property_exists(
$object, $tmpproperty)) {
1063 $valuecurrent =
$object->$tmpproperty;
1066 $valuecurrent = $tmp[$tmpproperty];
1070 if ($regforregex[1] ==
'SET' || empty($valuecurrent)) {
1071 $valuetouse = $regforregex[2];
1072 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
1074 $matcharray = array();
1075 preg_match_all(
'/__([a-z0-9]+(?:_[a-z0-9]+)?)__/i', $valuetouse, $matcharray);
1076 if (is_array($matcharray[1])) {
1077 foreach ($matcharray[1] as $keytoreplace) {
1078 if ($keytoreplace) {
1079 if (preg_match(
'/^options_/', $keytoreplace)) {
1080 $substitutionarray[
'__'.$keytoreplace.
'__'] =
$object->array_options[preg_replace(
'/^options_/',
'', $keytoreplace)];
1082 if (property_exists(
$object, $keytoreplace)) {
1083 $substitutionarray[
'__'.$keytoreplace.
'__'] =
$object->$keytoreplace;
1086 $substitutionarray[
'__'.$keytoreplace.
'__'] = $tmp[$keytoreplace];
1095 if (preg_match(
'/^options_/', $tmpproperty)) {
1096 $object->array_options[preg_replace(
'/^options_/',
'', $tmpproperty)] = $valuetouse;
1098 $operationslog .=
'<br>Set value '.dol_escape_htmltag($valuetouse).
' into object->array_options['.
dol_escape_htmltag(preg_replace(
'/^options_/',
'', $tmpproperty)).
']';
1100 if (property_exists(
$object, $tmpproperty)) {
1101 $object->$tmpproperty = $valuetouse;
1103 $tmp[$tmpproperty] = $valuetouse;
1106 $operationslog .=
'<br>Set value '.dol_escape_htmltag($valuetouse).
' into object->'.
dol_escape_htmltag($tmpproperty);
1110 $errorforthisaction++;
1111 $this->error =
'Bad syntax for description of action parameters: '.$actionparam;
1112 $this->errors[] = $this->error;
1117 return $errorforthisaction;
1126 public function doCollectOneCollector($mode = 0)
1128 global $db,
$conf, $langs, $user;
1129 global $hookmanager;
1133 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1135 require_once DOL_DOCUMENT_ROOT.
'/includes/webklex/php-imap/vendor/autoload.php';
1138 dol_syslog(
"EmailCollector::doCollectOneCollector start for id=".$this->
id.
" - ".$this->
ref, LOG_INFO);
1140 $langs->loadLangs(array(
"project",
"companies",
"mails",
"errors",
"ticket",
"agenda",
"commercial"));
1145 $this->debuginfo =
'';
1149 $searchfilterdoltrackid = 0;
1150 $searchfilternodoltrackid = 0;
1151 $searchfilterisanswer = 0;
1152 $searchfilterisnotanswer = 0;
1153 $searchfilterreplyto = 0;
1154 $searchfilterexcludebodyarray = array();
1155 $searchfilterexcludesubjectarray = array();
1156 $searchfilterexcludeemailarray = array();
1157 $searchfilterexcludedomainarray = array();
1158 $searchfilterexcludeemailmap = array();
1159 $operationslog =
'';
1160 $rulesreplyto = array();
1161 $connectstringsource =
'';
1162 $connectstringtarget =
'';
1163 $connection =
false;
1164 $arrayofemail = array();
1169 if (empty($this->host)) {
1170 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'EMailHost'));
1173 if (empty($this->login)) {
1174 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'Login'));
1177 if (empty($this->source_directory)) {
1178 $this->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
'MailboxSourceDirectory'));
1184 $sourcedir = $this->source_directory;
1185 $targetdir = ($this->target_directory ? $this->target_directory :
'');
1190 $sourcedir = $this->source_directory;
1191 $targetdir = ($this->target_directory ? $this->target_directory :
'');
1194 $timeoutconnect = (int)
getDolGlobalInt(
'MAIN_USE_CONNECT_TIMEOUT', 5);
1195 if ($timeoutconnect <= 0) {
1196 $timeoutconnect = 5;
1198 $timeoutread = (int)
getDolGlobalInt(
'MAIN_USE_RESPONSE_TIMEOUT', 20);
1199 if ($timeoutread <= 0) {
1202 if (function_exists(
'imap_timeout')) {
1203 imap_timeout(IMAP_OPENTIMEOUT, $timeoutconnect);
1204 imap_timeout(IMAP_READTIMEOUT, $timeoutread);
1205 imap_timeout(IMAP_WRITETIMEOUT, 5);
1206 imap_timeout(IMAP_CLOSETIMEOUT, 5);
1207 $this->debuginfo .=
'IMAP timeouts: connect='.$timeoutconnect.
's, read='.$timeoutread.
's<br>';
1211 if ($this->acces_type == 1) {
1213 $this->debuginfo .=
'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=1, access_type=1 (OAUTH2)<br>';
1215 require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
1219 $keyforsupportedoauth2array = $this->oauth_service;
1220 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
1221 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
1223 $keyforprovider =
'';
1225 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
1226 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
1228 if (!empty($supportedoauth2array)) {
1229 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
1230 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
1231 $OAUTH_SERVICENAME = $nameofservice;
1233 $OAUTH_SERVICENAME =
'Unknown';
1236 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
1238 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
1247 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1250 if (is_object($tokenobj) && method_exists($tokenobj,
'getEndOfLife')) {
1251 $endOfLife = $tokenobj->getEndOfLife();
1252 if ($endOfLife !== -9002 && $endOfLife !== -9001 && time() > ($endOfLife - 30)) {
1258 if (is_object($tokenobj) && $expire) {
1259 $this->debuginfo .=
'Refresh token '.$OAUTH_SERVICENAME.
'<br>';
1260 $credentials =
new Credentials(
1265 $serviceFactory = new \OAuth\ServiceFactory();
1266 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
1270 if (preg_match(
'/^Microsoft/', $OAUTH_SERVICENAME)) {
1272 $tmp = explode(
'-', $OAUTH_SERVICENAME);
1273 $scopes = explode(
',',
getDolGlobalString(
'OAUTH_'.strtoupper($tmp[0]).(empty($tmp[1]) ?
'' :
'-'.$tmp[1]).
'_SCOPE'));
1276 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $scopes);
1278 '@phan-var-force OAuth\OAuth2\Service\AbstractService|OAuth\OAuth1\Service\AbstractService $apiService';
1280 $refreshtoken = $tokenobj->getRefreshToken();
1281 $tokenobj = $apiService->refreshAccessToken($tokenobj);
1284 $tokenobj->setRefreshToken($refreshtoken);
1285 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
1287 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
1288 if (is_object($tokenobj)) {
1289 $token = $tokenobj->getAccessToken();
1291 $this->error =
"Token not found";
1296 $this->error = $e->getMessage();
1297 dol_syslog(
"CMailFile::sendfile: mail end error=".$this->error, LOG_ERR);
1301 $cm =
new ClientManager();
1302 $client = $cm->make([
1303 'host' => $this->host,
1304 'port' => $this->port,
1305 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption : false,
1306 'validate_cert' => true,
1307 'protocol' =>
'imap',
1308 'username' => $this->login,
1309 'password' => $token,
1310 'authentication' =>
"oauth",
1314 $this->debuginfo .=
'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=1, access_type=0 (LOGIN)<br>';
1316 $cm =
new ClientManager();
1317 $client = $cm->make([
1318 'host' => $this->host,
1319 'port' => $this->port,
1320 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption : false,
1321 'validate_cert' => true,
1322 'protocol' =>
'imap',
1323 'username' => $this->login,
1324 'password' => $this->password,
1325 'authentication' =>
"login",
1333 }
catch (ConnectionFailedException $e) {
1334 $this->error = $e->getMessage();
1335 $this->errors[] = $this->error;
1336 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1340 $host = dol_getprefix(
'email');
1343 $this->debuginfo .=
'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=0 (native PHP imap, LOGIN)<br>';
1345 if (!function_exists(
'imap_open')) {
1346 $this->error =
'IMAP function not enabled on your PHP';
1352 $connectstringsource = $connectstringserver.$this->getEncodedUtf7($sourcedir);
1353 $connectstringtarget = $connectstringserver.$this->getEncodedUtf7($targetdir);
1355 $connectstringsource = $connectstringserver.$sourcedir;
1356 $connectstringtarget = $connectstringserver.$targetdir;
1359 $this->debuginfo .=
'connectstringsource = '.$connectstringsource.
', $connectstringtarget='.$connectstringtarget.
'<br>';
1361 $connection = imap_open($connectstringsource, $this->login, $this->password);
1362 if ($connection ===
false) {
1363 $this->error =
'Failed to open IMAP connection '.$connectstringsource.
' '.imap_last_error();
1366 '@phan-var-force resource|IMAP\Connection $connection';
1369 $host = dol_getprefix(
'email');
1378 $criteria = array();
1381 $criteria = array(array(
'UNDELETED'));
1382 foreach ($this->filters as $rule) {
1383 if (empty($rule[
'status'])) {
1388 if (strpos($rule[
'rulevalue'],
'!') === 0) {
1392 $rule[
'rulevalue'] = substr($rule[
'rulevalue'], 1);
1395 if ($rule[
'type'] ==
'excludeemail') {
1396 $tmpvalues = preg_split(
'/[\\r\\n,;]+/', (
string) $rule[
'rulevalue']);
1397 if (is_array($tmpvalues)) {
1398 foreach ($tmpvalues as $tmpvalue) {
1399 $tmpvalue = trim((
string) $tmpvalue);
1400 if ($tmpvalue ===
'') {
1404 if (preg_match_all(
'/[a-z0-9._%+\\-]+@[a-z0-9.\\-]+\\.[a-z]{2,}/i', $tmpvalue, $tmpmatches)) {
1405 foreach ($tmpmatches[0] as $tmpemail) {
1406 $tmpemail = strtolower(trim($tmpemail));
1407 if ($tmpemail !==
'') {
1408 $searchfilterexcludeemailarray[] = $tmpemail;
1416 if ($rule[
'type'] ==
'excludedomain') {
1417 $tmpvalues = preg_split(
'/[\\r\\n,;]+/', (
string) $rule[
'rulevalue']);
1418 if (is_array($tmpvalues)) {
1419 foreach ($tmpvalues as $tmpvalue) {
1420 $tmpvalue = trim((
string) $tmpvalue);
1421 if ($tmpvalue ===
'') {
1425 if (preg_match_all(
'/[a-z0-9._%+\\-]+@([a-z0-9.\\-]+\\.[a-z]{2,})/i', $tmpvalue, $tmpmatches)) {
1426 foreach ($tmpmatches[1] as $tmpdomain) {
1427 $tmpdomain = strtolower(trim($tmpdomain));
1428 $tmpdomain = trim($tmpdomain,
". \t\n\r\0\x0B");
1429 if ($tmpdomain !==
'') {
1430 $searchfilterexcludedomainarray[] = $tmpdomain;
1436 $tmpdomain = strtolower($tmpvalue);
1437 $tmpdomain = preg_replace(
'/^mailto:/i',
'', $tmpdomain);
1438 $tmpdomain = trim($tmpdomain);
1439 $tmpdomain = trim($tmpdomain,
"<> \t\n\r\0\x0B.,;");
1440 if (strpos($tmpdomain,
'@') === 0) {
1441 $tmpdomain = substr($tmpdomain, 1);
1443 $posat = strrpos($tmpdomain,
'@');
1444 if ($posat !==
false) {
1445 $tmpdomain = substr($tmpdomain, $posat + 1);
1447 $tmpdomain = trim($tmpdomain,
". \t\n\r\0\x0B");
1448 if ($tmpdomain !==
'') {
1449 $searchfilterexcludedomainarray[] = $tmpdomain;
1456 if ($rule[
'type'] ==
'from') {
1457 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1458 if (count($tmprulevaluearray) >= 2) {
1459 foreach ($tmprulevaluearray as $tmprulevalue) {
1460 array_push($criteria, array($not.
"FROM" => $tmprulevalue));
1463 array_push($criteria, array($not.
"FROM" => $rule[
'rulevalue']));
1466 if ($rule[
'type'] ==
'to') {
1467 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1468 if (count($tmprulevaluearray) >= 2) {
1469 foreach ($tmprulevaluearray as $tmprulevalue) {
1470 array_push($criteria, array($not.
"TO" => $tmprulevalue));
1473 array_push($criteria, array($not.
"TO" => $rule[
'rulevalue']));
1476 if ($rule[
'type'] ==
'bcc') {
1477 array_push($criteria, array($not.
"BCC" => $rule[
'rulevalue']));
1479 if ($rule[
'type'] ==
'cc') {
1480 array_push($criteria, array($not.
"CC" => $rule[
'rulevalue']));
1482 if ($rule[
'type'] ==
'subject') {
1485 $searchfilterexcludesubjectarray[] = $rule[
'rulevalue'];
1487 array_push($criteria, array(
"SUBJECT" => $rule[
'rulevalue']));
1490 if ($rule[
'type'] ==
'body') {
1493 $searchfilterexcludebodyarray[] = $rule[
'rulevalue'];
1495 array_push($criteria, array(
"BODY" => $rule[
'rulevalue']));
1498 if ($rule[
'type'] ==
'header') {
1499 array_push($criteria, array($not.
"HEADER" => $rule[
'rulevalue']));
1511 if ($rule[
'type'] ==
'seen') {
1512 array_push($criteria, array($not.
"SEEN"));
1514 if ($rule[
'type'] ==
'unseen') {
1515 array_push($criteria, array($not.
"UNSEEN"));
1517 if ($rule[
'type'] ==
'unanswered') {
1518 array_push($criteria, array($not.
"UNANSWERED"));
1520 if ($rule[
'type'] ==
'answered') {
1521 array_push($criteria, array($not.
"ANSWERED"));
1523 if ($rule[
'type'] ==
'smaller') {
1524 array_push($criteria, array($not.
"SMALLER"));
1526 if ($rule[
'type'] ==
'larger') {
1527 array_push($criteria, array($not.
"LARGER"));
1531 if ($rule[
'type'] ==
'withtrackingidinmsgid') {
1532 $searchfilterdoltrackid++;
1533 $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1535 if ($rule[
'type'] ==
'withouttrackingidinmsgid') {
1536 $searchfilterdoltrackid++;
1537 $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1539 if ($rule[
'type'] ==
'withtrackingid') {
1540 $searchfilterdoltrackid++;
1541 $searchhead .=
'/References.*@'.preg_quote($host,
'/').
'/';
1543 if ($rule[
'type'] ==
'withouttrackingid') {
1544 $searchfilternodoltrackid++;
1545 $searchhead .=
'! /References.*@'.preg_quote($host,
'/').
'/';
1548 if ($rule[
'type'] ==
'isanswer') {
1549 $searchfilterisanswer++;
1550 $searchhead .=
'/References.*@.*/';
1552 if ($rule[
'type'] ==
'isnotanswer') {
1553 $searchfilterisnotanswer++;
1554 $searchhead .=
'! /References.*@.*/';
1557 if ($rule[
'type'] ==
'replyto') {
1558 $searchfilterreplyto++;
1559 $rulesreplyto[] = $rule[
'rulevalue'];
1560 $searchhead .=
'/Reply-To.*'.preg_quote($rule[
'rulevalue'],
'/').
'/';
1564 if (empty($targetdir) || !
getDolGlobalString(
'EMAILCOLLECTOR_NO_FILTER_ON_DATE_IF_THERE_IS_A_TARGETDIR')) {
1566 if ($this->datelastok) {
1567 $fromdate = $this->datelastok;
1569 if ($fromdate > 0) {
1571 array_push($criteria, array(
"SINCE" =>
date(
'j-M-Y', $fromdate - 86400)));
1577 $search = var_export($criteria,
true);
1580 $search =
'UNDELETED';
1581 foreach ($this->filters as $rule) {
1582 if (empty($rule[
'status'])) {
1590 if (!empty($rule[
'rulevalue']) && strpos($rule[
'rulevalue'],
'!') === 0) {
1594 $rule[
'rulevalue'] = substr($rule[
'rulevalue'], 1);
1597 if ($rule[
'type'] ==
'excludeemail') {
1598 $tmpvalues = preg_split(
'/[\\r\\n,;]+/', (
string) $rule[
'rulevalue']);
1599 if (is_array($tmpvalues)) {
1600 foreach ($tmpvalues as $tmpvalue) {
1601 $tmpvalue = trim((
string) $tmpvalue);
1602 if ($tmpvalue ===
'') {
1606 if (preg_match_all(
'/[a-z0-9._%+\\-]+@[a-z0-9.\\-]+\\.[a-z]{2,}/i', $tmpvalue, $tmpmatches)) {
1607 foreach ($tmpmatches[0] as $tmpemail) {
1608 $tmpemail = strtolower(trim($tmpemail));
1609 if ($tmpemail !==
'') {
1610 $searchfilterexcludeemailarray[] = $tmpemail;
1618 if ($rule[
'type'] ==
'excludedomain') {
1619 $tmpvalues = preg_split(
'/[\\r\\n,;]+/', (
string) $rule[
'rulevalue']);
1620 if (is_array($tmpvalues)) {
1621 foreach ($tmpvalues as $tmpvalue) {
1622 $tmpvalue = trim((
string) $tmpvalue);
1623 if ($tmpvalue ===
'') {
1627 if (preg_match_all(
'/[a-z0-9._%+\\-]+@([a-z0-9.\\-]+\\.[a-z]{2,})/i', $tmpvalue, $tmpmatches)) {
1628 foreach ($tmpmatches[1] as $tmpdomain) {
1629 $tmpdomain = strtolower(trim($tmpdomain));
1630 $tmpdomain = trim($tmpdomain,
". \t\n\r\0\x0B");
1631 if ($tmpdomain !==
'') {
1632 $searchfilterexcludedomainarray[] = $tmpdomain;
1638 $tmpdomain = strtolower($tmpvalue);
1639 $tmpdomain = preg_replace(
'/^mailto:/i',
'', $tmpdomain);
1640 $tmpdomain = trim($tmpdomain);
1641 $tmpdomain = trim($tmpdomain,
"<> \t\n\r\0\x0B.,;");
1642 if (strpos($tmpdomain,
'@') === 0) {
1643 $tmpdomain = substr($tmpdomain, 1);
1645 $posat = strrpos($tmpdomain,
'@');
1646 if ($posat !==
false) {
1647 $tmpdomain = substr($tmpdomain, $posat + 1);
1649 $tmpdomain = trim($tmpdomain,
". \t\n\r\0\x0B");
1650 if ($tmpdomain !==
'') {
1651 $searchfilterexcludedomainarray[] = $tmpdomain;
1658 if ($rule[
'type'] ==
'from') {
1659 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1660 if (count($tmprulevaluearray) >= 2) {
1661 foreach ($tmprulevaluearray as $tmprulevalue) {
1662 $search .= ($search ?
' ' :
'').$not.
'FROM "'.str_replace(
'"',
'', $tmprulevalue).
'"';
1665 $search .= ($search ?
' ' :
'').$not.
'FROM "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1668 if ($rule[
'type'] ==
'to') {
1669 $tmprulevaluearray = explode(
'*', $rule[
'rulevalue']);
1670 if (count($tmprulevaluearray) >= 2) {
1671 foreach ($tmprulevaluearray as $tmprulevalue) {
1672 $search .= ($search ?
' ' :
'').$not.
'TO "'.str_replace(
'"',
'', $tmprulevalue).
'"';
1675 $search .= ($search ?
' ' :
'').$not.
'TO "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1678 if ($rule[
'type'] ==
'bcc') {
1679 $search .= ($search ?
' ' :
'').$not.
'BCC';
1681 if ($rule[
'type'] ==
'cc') {
1682 $search .= ($search ?
' ' :
'').$not.
'CC';
1684 if ($rule[
'type'] ==
'subject') {
1687 $searchfilterexcludesubjectarray[] = $rule[
'rulevalue'];
1689 $search .= ($search ?
' ' :
'').
'SUBJECT "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1692 if ($rule[
'type'] ==
'body') {
1695 $searchfilterexcludebodyarray[] = $rule[
'rulevalue'];
1698 $search .= ($search ?
' ' :
'').
'BODY "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1701 if ($rule[
'type'] ==
'header') {
1702 $search .= ($search ?
' ' :
'').$not.
'HEADER '.$rule[
'rulevalue'];
1714 if ($rule[
'type'] ==
'seen') {
1715 $search .= ($search ?
' ' :
'').$not.
'SEEN';
1717 if ($rule[
'type'] ==
'unseen') {
1718 $search .= ($search ?
' ' :
'').$not.
'UNSEEN';
1720 if ($rule[
'type'] ==
'unanswered') {
1721 $search .= ($search ?
' ' :
'').$not.
'UNANSWERED';
1723 if ($rule[
'type'] ==
'answered') {
1724 $search .= ($search ?
' ' :
'').$not.
'ANSWERED';
1726 if ($rule[
'type'] ==
'smaller') {
1727 $search .= ($search ?
' ' :
'').$not.
'SMALLER "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1729 if ($rule[
'type'] ==
'larger') {
1730 $search .= ($search ?
' ' :
'').$not.
'LARGER "'.str_replace(
'"',
'', $rule[
'rulevalue']).
'"';
1734 if ($rule[
'type'] ==
'withtrackingidinmsgid') {
1735 $searchfilterdoltrackid++;
1736 $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1738 if ($rule[
'type'] ==
'withouttrackingidinmsgid') {
1739 $searchfilterdoltrackid++;
1740 $searchhead .=
'/Message-ID.*@'.preg_quote($host,
'/').
'/';
1742 if ($rule[
'type'] ==
'withtrackingid') {
1743 $searchfilterdoltrackid++;
1744 $searchhead .=
'/References.*@'.preg_quote($host,
'/').
'/';
1746 if ($rule[
'type'] ==
'withouttrackingid') {
1747 $searchfilternodoltrackid++;
1748 $searchhead .=
'! /References.*@'.preg_quote($host,
'/').
'/';
1751 if ($rule[
'type'] ==
'isanswer') {
1752 $searchfilterisanswer++;
1753 $searchhead .=
'/References.*@.*/';
1755 if ($rule[
'type'] ==
'isnotanswer') {
1756 $searchfilterisnotanswer++;
1757 $searchhead .=
'! /References.*@.*/';
1760 if ($rule[
'type'] ==
'replyto') {
1761 $searchfilterreplyto++;
1762 $rulesreplyto[] = $rule[
'rulevalue'];
1763 $searchhead .=
'/Reply-To.*'.preg_quote($rule[
'rulevalue'],
'/').
'/';
1767 if (empty($targetdir)) {
1769 if ($this->datelastok) {
1770 $fromdate = $this->datelastok;
1772 if ($fromdate > 0) {
1774 $search .= ($search ?
' ' :
'').
'SINCE '.
date(
'j-M-Y', $fromdate - 86400);
1782 if (!empty($searchfilterexcludeemailarray)) {
1783 $searchfilterexcludeemailarray = array_values(array_filter(array_unique($searchfilterexcludeemailarray)));
1784 $searchfilterexcludeemailmap = array_fill_keys($searchfilterexcludeemailarray,
true);
1786 if (!empty($searchfilterexcludedomainarray)) {
1787 $searchfilterexcludedomainarray = array_values(array_filter(array_unique($searchfilterexcludedomainarray)));
1790 $nbemailprocessed = 0;
1793 $charset = ($this->hostcharset ? $this->hostcharset :
"UTF-8");
1794 $arrayofemail = array();
1802 $tmpsourcedir = $sourcedir;
1807 $f = $client->getFolders(
false, $tmpsourcedir);
1809 foreach ($f as $_f) {
1810 if ($_f->path == $this->source_directory && $_f instanceof Webklex\PHPIMAP\Folder) {
1811 $Query = $_f->messages()->where($criteria);
1815 if (empty($Query)) {
1817 $this->error =
"Source directory ".$sourcedir.
" not found";
1818 $this->errors[] = $this->error;
1819 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_WARNING);
1824 $this->error =
"Failed to execute getfolders";
1825 $this->errors[] = $this->error;
1826 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1829 }
catch (InvalidWhereQueryCriteriaException $e) {
1830 $this->error = $e->getMessage();
1831 $this->errors[] = $this->error;
1832 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1835 $this->error = $e->getMessage();
1836 $this->errors[] = $this->error;
1837 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1841 '@phan-var-force Webklex\PHPIMAP\Query\Query $Query';
1844 $Query->leaveUnread();
1846 $arrayofemail = $Query->limit($this->maxemailpercollect)->setFetchOrder(
"asc")->get();
1847 dol_syslog(
"EmailCollector::doCollectOneCollector nb arrayofemail ".(is_array($arrayofemail) ? count($arrayofemail) :
'Not array'));
1849 $this->error = $e->getMessage();
1850 $this->errors[] = $this->error;
1851 dol_syslog(
"EmailCollector::doCollectOneCollector ".$this->error, LOG_ERR);
1854 } elseif ($connection !==
false) {
1856 $arrayofemail = imap_search($connection, $search, SE_UID, $charset);
1858 if ($arrayofemail ===
false) {
1860 $mapoferrrors = imap_errors();
1861 if ($mapoferrrors !==
false) {
1863 $this->error =
"Search string not understood - ".implode(
',', $mapoferrrors);
1864 $this->errors[] = $this->error;
1869 $arrayofemailtodelete = array();
1872 if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0 && $connection !==
false) {
1889 dol_syslog(
"Start of loop on email", LOG_INFO, 1);
1891 $richarrayofemail = array();
1893 foreach ($arrayofemail as $imapemail) {
1894 if ($nbemailprocessed > 1000) {
1900 '@phan-var-force Webklex\PHPIMAP\Message $imapemail';
1901 $header = $imapemail->getHeader()->raw;
1902 $overview = $imapemail->getAttributes();
1904 $header = imap_fetchheader($connection, $imapemail, FT_UID);
1905 $overview = imap_fetch_overview($connection, $imapemail, FT_UID);
1908 $header = preg_replace(
'/\r\n\s+/m',
' ', $header);
1911 preg_match_all(
'/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)(\r\n|\s$)/m', $header, $matches);
1912 $headers = array_combine($matches[1], $matches[2]);
1915 $richarrayofemail[] = array(
'imapemail' => $imapemail,
'header' => $header,
'headers' => $headers,
'overview' => $overview,
'date' => strtotime($headers[
'Date']));
1920 $richarrayofemail =
dol_sort_array($richarrayofemail,
'date',
'asc');
1924 foreach ($richarrayofemail as $tmpval) {
1927 $imapemail = $tmpval[
'imapemail'];
1928 $header = $tmpval[
'header'];
1929 $overview = $tmpval[
'overview'];
1930 $headers = $tmpval[
'headers'];
1932 if (!empty($headers[
'in-reply-to']) && empty($headers[
'In-Reply-To'])) {
1933 $headers[
'In-Reply-To'] = $headers[
'in-reply-to'];
1935 if (!empty($headers[
'references']) && empty($headers[
'References'])) {
1936 $headers[
'References'] = $headers[
'references'];
1938 if (!empty($headers[
'message-id']) && empty($headers[
'Message-ID'])) {
1939 $headers[
'Message-ID'] = $headers[
'message-id'];
1941 if (!empty($headers[
'subject']) && empty($headers[
'Subject'])) {
1942 $headers[
'Subject'] = $headers[
'subject'];
1948 $emailto = (
string) $overview[
'to'];
1953 $operationslog .=
'<br>** Process email #'.dol_escape_htmltag((
string) $iforemailloop);
1957 '@phan-var-force Webklex\PHPIMAP\Message $imapemail';
1959 $msgid = str_replace(array(
'<',
'>'),
'', $overview[
'message_id']);
1961 $operationslog .=
" - ".dol_escape_htmltag((
string) $imapemail);
1962 $msgid = str_replace(array(
'<',
'>'),
'', $overview[0]->message_id);
1964 $operationslog .=
" - MsgId: ".$msgid;
1965 $operationslog .=
" - Date: ".($headers[
'Date'] ?? $langs->transnoentitiesnoconv(
"NotFound"));
1966 $operationslog .=
" - References: ".dol_escape_htmltag($headers[
'References'] ?? $langs->transnoentitiesnoconv(
"NotFound")).
" - Subject: ".
dol_escape_htmltag($headers[
'Subject']);
1968 dol_syslog(
"-- Process email #".$iforemailloop.
", MsgId: ".$msgid.
", Date: ".($headers[
'Date'] ??
'').
", References: ".($headers[
'References'] ??
'').
", Subject: ".$headers[
'Subject']);
1971 $trackidfoundintorecipienttype =
'';
1972 $trackidfoundintorecipientid = 0;
1977 if (preg_match(
'/\+(th[i]|ctc|use|mem|sub|proj|tas|con|tic|pro|ord|inv|spro|sor|sin|leav|stockinv|job|surv|salary)([0-9]+)@/', $emailto, $reg)) {
1978 $trackidfoundintorecipienttype = $reg[1];
1979 $trackidfoundintorecipientid = $reg[2];
1980 } elseif (preg_match(
'/\+emailing-(\w+)@/', $emailto, $reg)) {
1981 $trackidfoundintorecipienttype =
'emailing';
1982 $trackidfoundintorecipientid = $reg[1];
1985 $trackidfoundintomsgidtype =
'';
1986 $trackidfoundintomsgidid = 0;
1991 if (preg_match(
'/(?:[\+\-])(th[i]|ctc|use|mem|sub|proj|tas|con|tic|pro|ord|inv|spro|sor|sin|leav|stockinv|job|surv|salary)([0-9]+)@/', $msgid, $reg)) {
1992 $trackidfoundintomsgidtype = $reg[1];
1993 $trackidfoundintomsgidid = $reg[2];
1994 } elseif (preg_match(
'/(?:[\+\-])emailing-(\w+)@/', $msgid, $reg)) {
1995 $trackidfoundintomsgidtype =
'emailing';
1996 $trackidfoundintomsgidid = $reg[1];
2002 if ($searchfilterdoltrackid > 0) {
2003 $referencesForFilter = $headers[
'References'] ??
'';
2004 if (!empty($headers[
'References']) && !empty($headers[
'In-Reply-To'])) {
2005 $referencesForFilter .=
' ';
2007 $referencesForFilter .= ($headers[
'In-Reply-To'] ??
'');
2009 if (empty($trackidfoundintorecipienttype) && empty($trackidfoundintomsgidtype)) {
2010 if (empty($referencesForFilter) || !preg_match(
'/@'.preg_quote($host,
'/').
'/', $referencesForFilter)) {
2011 $nbemailprocessed++;
2012 dol_syslog(
" Discarded - No suffix in email recipient, and no Header 'References/In-Reply-To' found matching the signature of the application, so with a trackid coming from the application");
2018 if ($searchfilternodoltrackid > 0) {
2019 $referencesForFilter = $headers[
'References'] ??
'';
2020 if (!empty($headers[
'References']) && !empty($headers[
'In-Reply-To'])) {
2021 $referencesForFilter .=
' ';
2023 $referencesForFilter .= ($headers[
'In-Reply-To'] ??
'');
2025 if (!empty($trackidfoundintorecipienttype) || !empty($trackidfoundintomsgidtype) || (!empty($referencesForFilter) && preg_match(
'/@'.preg_quote($host,
'/').
'/', $referencesForFilter))) {
2026 $nbemailprocessed++;
2027 dol_syslog(
" Discarded - Suffix found into email recipient, or Header 'References/In-Reply-To' found and matching signature of application so with a trackid");
2032 if ($searchfilterisanswer > 0) {
2033 $referencesforanswer =
'';
2034 if (!empty($headers[
'References'])) {
2035 $referencesforanswer .= $headers[
'References'].
' ';
2037 if (!empty($headers[
'In-Reply-To'])) {
2038 $referencesforanswer .= $headers[
'In-Reply-To'];
2041 $hasdolibarrreference = 0;
2042 if (!empty($referencesforanswer)) {
2043 if (preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host,
'/').
'/', $referencesforanswer)) {
2044 $hasdolibarrreference = 1;
2046 if (preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote(
getDolGlobalString(
'EMAIL_ALTERNATIVE_HOST_SIGNATURE'),
'/').
'/', $referencesforanswer)) {
2047 $hasdolibarrreference = 1;
2053 if (preg_match(
'/^(回复|回覆|SV|Antw|VS|RE|Re|AW|Aw|ΑΠ|השב| תשובה | הועבר|Vá|R|RIF|BLS|Atb|RES|Odp|பதில்|YNT|ATB)\s*:\s+/i', $headers[
'Subject'])) {
2059 if (!empty($headers[
'In-Reply-To'])) {
2063 if ($hasdolibarrreference) {
2070 $nbemailprocessed++;
2071 dol_syslog(
" Discarded - Email is not an answer (no reply marker detected, and test on In-Reply-To not requested because not reliable, or test on In-Reply-To requested but not found)");
2075 if ($searchfilterisnotanswer > 0) {
2079 if (preg_match(
'/^(回复|回覆|SV|Antw|VS|RE|Re|AW|Aw|ΑΠ|השב| תשובה | הועבר|Vá|R|RIF|BLS|Atb|RES|Odp|பதில்|YNT|ATB)\s*:\s+/i', $headers[
'Subject'])) {
2084 if (!$isanswer &&
getDolGlobalInt(
'EMAILCOLLECTOR_ISNOTANSWER_USE_REFERENCES') && !empty($headers[
'References'])) {
2087 if (!$isanswer &&
getDolGlobalString(
'EMAILCOLLECTOR_USE_IN_REPLY_TO_TO_DETECT_ANSWERS')) {
2088 if (!empty($headers[
'In-Reply-To'])) {
2095 $nbemailprocessed++;
2096 dol_syslog(
" Discarded - Email is an answer");
2100 if ($searchfilterreplyto > 0) {
2101 if (!empty($headers[
'Reply-To'])) {
2103 foreach ($rulesreplyto as $key => $rulereplyto) {
2104 if (preg_match(
'/'.preg_quote($rulereplyto,
'/').
'/', $headers[
'Reply-To'])) {
2109 if (!$isreplytook || $isreplytook != count($rulesreplyto)) {
2110 $nbemailprocessed++;
2111 dol_syslog(
" Discarded - Reply-to does not match");
2119 $thirdpartystatic =
new Societe($this->db);
2120 $contactstatic =
new Contact($this->db);
2121 $projectstatic =
new Project($this->db);
2123 $nbactiondoneforemail = 0;
2125 $errorforactions = 0;
2126 $thirdpartyfoundby =
'';
2127 $contactfoundby =
'';
2128 $projectfoundby =
'';
2129 $ticketfoundby =
'';
2130 $candidaturefoundby =
'';
2134 $dateformated =
dol_print_date($overview[
'date'],
'dayrfc',
'gmt');
2135 dol_syslog(
"msgid=".$overview[
'message_id'].
" date=".$dateformated.
" from=".$overview[
'from'].
" to=".$overview[
'to'].
" subject=".$overview[
'subject']);
2140 dol_syslog(
"msgid=".$overview[0]->message_id.
" date=".
dol_print_date($overview[0]->udate,
'dayrfc',
'gmt').
" from=".$overview[0]->from.
" to=".$overview[0]->to.
" subject=".$overview[0]->subject);
2150 global $htmlmsg, $plainmsg, $charset, $attachments;
2154 '@phan-var-force Webklex\PHPIMAP\Message $imapemail';
2157 $htmlmsg = $plainmsg =
'';
2158 $attachments = array();
2160 if ($imapemail->hasHTMLBody()) {
2161 $htmlmsg = $imapemail->getHTMLBody();
2163 if ($imapemail->hasTextBody() && $imapemail->getTextBody() !=
"\n") {
2164 $plainmsg = $imapemail->getTextBody();
2166 if ($imapemail->hasAttachments()) {
2167 $attachments = $imapemail->getAttachments()->all();
2169 $attachments = array();
2172 $getMsg = $this->
getmsg($connection, $imapemail);
2174 $this->errors = array_merge($this->errors, [$this->error]);
2178 '@phan-var-force Webklex\PHPIMAP\Attachment[] $attachments';
2188 $operationslog .=
'<br>Discarded - Email body is not valid utf8';
2189 dol_syslog(
" Discarded - Email body is not valid utf8");
2193 if (!empty($searchfilterexcludebodyarray)) {
2194 foreach ($searchfilterexcludebodyarray as $searchfilterexcludebody) {
2195 if (preg_match(
'/'.preg_quote($searchfilterexcludebody,
'/').
'/ms', $messagetext)) {
2196 $nbemailprocessed++;
2197 $operationslog .=
'<br>Discarded - Email body contains string '.$searchfilterexcludebody;
2198 dol_syslog(
" Discarded - Email body contains string ".$searchfilterexcludebody);
2240 $replytostring =
'';
2244 $replytostring = !empty($headers[
'Reply-To']) ? $this->
decodeSMTPSubject($headers[
'Reply-To']) :
'';
2251 $tmpdate = $overview[
'date']->toDate();
2252 $tmptimezone = $tmpdate->getTimezone()->getName();
2255 if (preg_match(
'/^([+\-])(\d\d):(\d\d)/', $tmptimezone, $reg)) {
2256 if ($reg[1] ==
'+' && ($reg[2] !=
'00' || $reg[3] !=
'00')) {
2257 $dateemail -= (3600 * (int) $reg[2]);
2258 $dateemail -= (60 * (int) $reg[3]);
2260 if ($reg[1] ==
'-' && ($reg[2] !=
'00' || $reg[3] !=
'00')) {
2261 $dateemail += (3600 * (int) $reg[2]);
2262 $dateemail += (60 * (int) $reg[3]);
2267 $fromstring = (
string) $overview[0]->from;
2268 $replytostring = (!empty($overview[
'in_reply-to']) ? $overview[
'in_reply-to'] : (!empty($headers[
'Reply-To']) ? $headers[
'Reply-To'] :
"")) ;
2270 $sender = !empty($overview[0]->sender) ? $overview[0]->sender :
'';
2271 $to = $overview[0]->to;
2272 $sendtocc = !empty($overview[0]->cc) ? $overview[0]->cc :
'';
2273 $sendtobcc = !empty($overview[0]->bcc) ? $overview[0]->bcc :
'';
2275 $subject = (
string) $overview[0]->subject;
2278 if (!empty($searchfilterexcludesubjectarray)) {
2279 foreach ($searchfilterexcludesubjectarray as $searchfilterexcludesubject) {
2280 if (preg_match(
'/'.preg_quote($searchfilterexcludesubject,
'/').
'/ms', $subject)) {
2281 $nbemailprocessed++;
2282 $operationslog .=
'<br>Discarded - Email subject contains string '.$searchfilterexcludesubject;
2283 dol_syslog(
" Discarded - Email subject contains string ".$searchfilterexcludesubject);
2290 if (preg_match(
'/^(.*)<(.*)>$/', $fromstring, $reg)) {
2292 $fromtext = $reg[1];
2294 $from = $fromstring;
2297 if (preg_match(
'/^(.*)<(.*)>$/', $replytostring, $reg)) {
2299 $replytotext = $reg[1];
2301 $replyto = $replytostring;
2305 if (!empty($searchfilterexcludeemailmap) || !empty($searchfilterexcludedomainarray)) {
2306 $emailsToCheck = array();
2307 $tmpaddressblob = trim($fromstring.
' '.$replytostring);
2308 if ($tmpaddressblob !==
'' && preg_match_all(
'/[a-z0-9._%+\\-]+@[a-z0-9.\\-]+\\.[a-z]{2,}/i', $tmpaddressblob, $tmpmatches)) {
2309 foreach ($tmpmatches[0] as $tmpEmail) {
2310 $tmpEmail = strtolower(trim($tmpEmail));
2311 if ($tmpEmail !==
'') {
2312 $emailsToCheck[$tmpEmail] =
true;
2317 if (empty($emailsToCheck)) {
2318 foreach (array($from, $replyto) as $tmpEmail) {
2319 $tmpEmail = strtolower(trim((
string) $tmpEmail));
2320 if ($tmpEmail !==
'' && strpos($tmpEmail,
'@') !==
false) {
2321 $emailsToCheck[$tmpEmail] =
true;
2327 $discardDomain =
'';
2328 $matchedDomainRule =
'';
2330 foreach (array_keys($emailsToCheck) as $tmpEmail) {
2331 if (!empty($searchfilterexcludeemailmap[$tmpEmail])) {
2332 $discardEmail = $tmpEmail;
2336 if (!empty($searchfilterexcludedomainarray)) {
2337 $posat = strrpos($tmpEmail,
'@');
2338 if ($posat ===
false) {
2341 $tmpDomain = strtolower(substr($tmpEmail, $posat + 1));
2342 foreach ($searchfilterexcludedomainarray as $excludedDomain) {
2343 if ($excludedDomain ===
'') {
2346 if ($tmpDomain === $excludedDomain || substr($tmpDomain, -strlen(
'.'.$excludedDomain)) ===
'.'.$excludedDomain) {
2347 $discardDomain = $tmpDomain;
2348 $matchedDomainRule = $excludedDomain;
2355 if ($discardEmail !==
'' || $discardDomain !==
'') {
2356 $nbemailprocessed++;
2357 if ($discardEmail !==
'') {
2358 $operationslog .=
'<br>Discarded - Sender email excluded: '.$discardEmail;
2359 dol_syslog(
" Discarded - Sender email excluded: ".$discardEmail);
2361 $operationslog .=
'<br>Discarded - Sender domain excluded: '.$discardDomain.($matchedDomainRule !==
'' && $matchedDomainRule !== $discardDomain ?
' (matched '.$matchedDomainRule.
')' :
'');
2362 dol_syslog(
" Discarded - Sender domain excluded: ".$discardDomain.($matchedDomainRule !==
'' && $matchedDomainRule !== $discardDomain ?
" (matched ".$matchedDomainRule.
")" :
""));
2369 $fk_element_type =
'';
2386 $objectemail =
null;
2389 $arrayofreferences = array();
2390 if (!empty($headers[
'References'])) {
2391 $arrayofreferences = preg_split(
'/(,|\s+)/', $headers[
'References']);
2393 if (!in_array(
'<'.$msgid.
'>', $arrayofreferences)) {
2394 $arrayofreferences = array_merge($arrayofreferences, array(
'<'.$msgid.
'>'));
2399 foreach ($arrayofreferences as $reference) {
2401 if (!empty($trackidfoundintorecipienttype)) {
2402 $resultsearchtrackid = -1;
2403 $reg[1] = $trackidfoundintorecipienttype;
2404 $reg[2] = $trackidfoundintorecipientid;
2405 } elseif (!empty($trackidfoundintomsgidtype)) {
2406 $resultsearchtrackid = -1;
2407 $reg[1] = $trackidfoundintomsgidtype;
2408 $reg[2] = $trackidfoundintomsgidid;
2410 $resultsearchtrackid = preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote($host,
'/').
'/', $reference, $reg);
2411 if (empty($resultsearchtrackid) &&
getDolGlobalString(
'EMAIL_ALTERNATIVE_HOST_SIGNATURE')) {
2412 $resultsearchtrackid = preg_match(
'/dolibarr-([a-z]+)([0-9]+)@'.preg_quote(
getDolGlobalString(
'EMAIL_ALTERNATIVE_HOST_SIGNATURE'),
'/').
'/', $reference, $reg);
2416 if (!empty($resultsearchtrackid)) {
2418 $trackid = $reg[1].$reg[2];
2420 $objectid = $reg[2];
2422 if ($reg[1] ==
'thi') {
2423 $objectemail =
new Societe($this->db);
2425 if ($reg[1] ==
'ctc') {
2426 $objectemail =
new Contact($this->db);
2428 if ($reg[1] ==
'inv') {
2429 $objectemail =
new Facture($this->db);
2431 if ($reg[1] ==
'sinv') {
2434 if ($reg[1] ==
'pro') {
2435 $objectemail =
new Propal($this->db);
2437 if ($reg[1] ==
'ord') {
2438 $objectemail =
new Commande($this->db);
2440 if ($reg[1] ==
'shi') {
2443 if ($reg[1] ==
'spro') {
2446 if ($reg[1] ==
'sord') {
2449 if ($reg[1] ==
'rec') {
2450 $objectemail =
new Reception($this->db);
2452 if ($reg[1] ==
'proj') {
2453 $objectemail =
new Project($this->db);
2454 $projectfoundby =
'TrackID dolibarr-'.$trackid.
'@...';
2456 if ($reg[1] ==
'tas') {
2457 $objectemail =
new Task($this->db);
2459 if ($reg[1] ==
'con') {
2460 $objectemail =
new Contact($this->db);
2462 if ($reg[1] ==
'use') {
2463 $objectemail =
new User($this->db);
2465 if ($reg[1] ==
'tic') {
2466 $objectemail =
new Ticket($this->db);
2467 $ticketfoundby =
'TrackID dolibarr-'.$trackid.
'@...';
2469 if ($reg[1] ==
'recruitmentcandidature') {
2471 $candidaturefoundby =
'TrackID dolibarr-'.$trackid.
'@...';
2473 if ($reg[1] ==
'mem') {
2474 $objectemail =
new Adherent($this->db);
2482 } elseif (preg_match(
'/<(.*@.*)>/', $reference, $reg)) {
2485 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"ticket";
2486 $sql .=
" WHERE email_msgid = '".$this->db->escape($reg[1]).
"' OR origin_references LIKE '%".$this->db->escape($this->db->escapeforlike($reg[1])).
"%'";
2487 $resql = $this->db->query($sql);
2489 $obj = $this->db->fetch_object($resql);
2491 $objectid = $obj->rowid;
2492 $objectemail =
new Ticket($this->db);
2493 $ticketfoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2500 if (!is_object($objectemail) &&
isModEnabled(
'project')) {
2501 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"projet where email_msgid = '".$this->db->escape($reg[1]).
"'";
2502 $resql = $this->db->query($sql);
2504 $obj = $this->db->fetch_object($resql);
2506 $objectid = $obj->rowid;
2507 $objectemail =
new Project($this->db);
2508 $projectfoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2515 if (!is_object($objectemail) &&
isModEnabled(
'recruitment')) {
2516 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"recruitment_recruitmentcandidature where email_msgid = '".$this->db->escape($reg[1]).
"'";
2517 $resql = $this->db->query($sql);
2519 $obj = $this->db->fetch_object($resql);
2521 $objectid = $obj->rowid;
2523 $candidaturefoundby = $langs->transnoentitiesnoconv(
"EmailMsgID").
' ('.$reg[1].
')';
2532 if (is_object($objectemail)) {
2533 $result = $objectemail->fetch($objectid);
2535 $fk_element_id = $objectemail->id;
2536 $fk_element_type = $objectemail->element;
2538 if ($fk_element_type ==
'facture') {
2539 $fk_element_type =
'invoice';
2542 if (get_class($objectemail) !=
'Societe') {
2543 $thirdpartyid = $objectemail->fk_soc ?? $objectemail->socid;
2545 $thirdpartyid = $objectemail->id;
2548 if (get_class($objectemail) !=
'Contact') {
2549 $contactid = $objectemail->fk_socpeople;
2551 $contactid = $objectemail->id;
2554 if (get_class($objectemail) !=
'Project') {
2555 $projectid = isset($objectemail->fk_project) ? $objectemail->fk_project : $objectemail->fk_projet;
2557 $projectid = $objectemail->id;
2560 if ($objectemail instanceof
Ticket) {
2561 $ticketid = $objectemail->id;
2563 $changeonticket_references =
false;
2564 if (empty($trackid)) {
2565 $trackid = $objectemail->track_id;
2567 if (empty($objectemail->origin_references)) {
2568 $objectemail->origin_references = !empty($headers[
'References']) ? $headers[
'References'] :
null;
2569 $changeonticket_references =
true;
2571 foreach ($arrayofreferences as $key => $referencetmp) {
2572 if (!str_contains($objectemail->origin_references, $referencetmp)) {
2573 $objectemail->origin_references .=
" ".$referencetmp;
2574 $changeonticket_references =
true;
2578 if ($changeonticket_references) {
2579 $operationslog .=
'<br>We complete ticket ID='.$ticketid.
' with property origin_references='.$objectemail->origin_references;
2580 $objectemail->update($user, 1);
2587 if ($projectid > 0) {
2588 $result = $projectstatic->fetch($projectid);
2590 $projectstatic->id = 0;
2592 $projectid = $projectstatic->id;
2594 $projectfoundby =
'trackid ('.$trackid.
')';
2596 if (empty($contactid)) {
2597 $contactid = $projectstatic->fk_contact;
2599 if (empty($thirdpartyid)) {
2600 $thirdpartyid = $projectstatic->fk_soc;
2605 if ($contactid > 0) {
2606 $result = $contactstatic->fetch($contactid);
2608 $contactstatic->id = 0;
2610 $contactid = $contactstatic->id;
2612 $contactfoundby =
'trackid ('.$trackid.
')';
2614 if (empty($thirdpartyid)) {
2615 $thirdpartyid = $contactstatic->fk_soc;
2620 if ($thirdpartyid > 0) {
2621 $result = $thirdpartystatic->fetch($thirdpartyid);
2623 $thirdpartystatic->id = 0;
2625 $thirdpartyid = $thirdpartystatic->id;
2627 $thirdpartyfoundby =
'trackid ('.$trackid.
')';
2632 if (is_object($objectemail)) {
2637 if (empty($contactid)) {
2638 $result = $contactstatic->fetch(0,
null,
'', $from);
2641 dol_syslog(
"We found a contact with the email ".$from);
2642 $contactid = $contactstatic->id;
2643 $contactfoundby =
'email of contact ('.$from.
')';
2644 if (empty($thirdpartyid) && $contactstatic->socid > 0) {
2645 $result = $thirdpartystatic->fetch($contactstatic->socid);
2647 $thirdpartyid = $thirdpartystatic->id;
2648 $thirdpartyfoundby =
'email of contact ('.$from.
')';
2654 if (empty($thirdpartyid)) {
2655 $result = $thirdpartystatic->fetch(0,
'',
'',
'',
'',
'',
'',
'',
'',
'', $from);
2657 dol_syslog(
"We found a thirdparty with the email ".$from);
2658 $thirdpartyid = $thirdpartystatic->id;
2659 $thirdpartyfoundby =
'email ('.$from.
')';
2694 $savedattachments = array();
2695 $savedattachmentsdir =
'';
2696 $savedattachmentsnote =
'';
2697 if (empty($mode) && !empty($attachments) && $fk_element_id > 0 && $fk_element_type ===
'order_supplier' && $objectemail instanceof
CommandeFournisseur) {
2698 $orderref = isset($objectemail->ref) ? (
string) $objectemail->ref :
'';
2699 if ($orderref !==
'' && !empty(
$conf->fournisseur->commande)) {
2700 $entityforobject = isset($objectemail->entity) ? (int) $objectemail->entity : (int)
$conf->entity;
2701 $basedir = (!empty(
$conf->fournisseur->commande->multidir_output[$entityforobject]) ?
$conf->fournisseur->commande->multidir_output[$entityforobject] :
$conf->fournisseur->commande->dir_output);
2703 $savedattachments = $this->saveEmailCollectorAttachmentsToDir($savedattachmentsdir, $attachments);
2704 if (!empty($savedattachments)) {
2705 $operationslog .=
'<br>Saved '.count($savedattachments).
' attachment(s) into '.
dol_escape_htmltag($savedattachmentsdir);
2709 if (!empty($savedattachments)) {
2711 foreach ($savedattachments as $attmeta) {
2712 if (!is_array($attmeta) || empty($attmeta[
'name'])) {
2715 $names[] = (
string) $attmeta[
'name'];
2717 if (!empty($names)) {
2718 $savedattachmentsnote = $langs->trans(
"NbOfAttachedFiles").
' : '.count($names);
2719 $savedattachmentsnote .=
' ('.implode(
', ', array_slice($names, 0, 10)).(count($names) > 10 ?
'...' :
'').
')';
2726 foreach ($this->actions as $operation) {
2727 $errorforthisaction = 0;
2728 $ticketalreadyexists = 0;
2729 if ($errorforactions) {
2732 if (empty($operation[
'status'])) {
2736 $operationslog .=
'<br>* Process operation '.$operation[
'type'];
2739 dol_syslog(
"Execute action ".$operation[
'type'].
" actionparam=".$operation[
'actionparam'].
' thirdpartystatic->id='.$thirdpartystatic->id.
' contactstatic->id='.$contactstatic->id.
' projectstatic->id='.$projectstatic->id);
2740 dol_syslog(
"Execute action fk_element_id=".$fk_element_id.
" fk_element_type=".$fk_element_type);
2743 $actioncode =
'EMAIL_IN';
2745 if (preg_match(
'/Sent$/', $sourcedir) || preg_match(
'/envoyés$/i', $sourcedir)) {
2746 $actioncode =
'EMAIL';
2749 $arrayofemailtoconsidersender = array_filter(array_map(
'trim', explode(
',',
getDolGlobalString(
'MAIL_FROM_EMAILS_TO_CONSIDER_SENDING'))));
2750 foreach ($arrayofemailtoconsidersender as $emailtoconsidersender) {
2751 if ($emailtoconsidersender ===
'') {
2754 if (preg_match(
'/'.preg_quote($emailtoconsidersender,
'/').
'/', $fromstring)) {
2755 $actioncode =
'EMAIL';
2758 $operationslog .=
'<br>Email will have actioncode='.$actioncode;
2760 $description = $descriptiontitle = $descriptionmeta = $descriptionfull =
'';
2762 $descriptiontitle = $langs->transnoentitiesnoconv(
"RecordCreatedByEmailCollector", $this->
ref);
2767 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailFrom").($langs->trans(
"MailFrom") !=
'From' ?
' (From)' :
'').
' : '.
dol_escape_htmltag($fromstring));
2769 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"Sender").($langs->trans(
"Sender") !=
'Sender' ?
' (Sender)' :
'').
' : '.
dol_escape_htmltag($sender));
2771 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailTo").($langs->trans(
"MailTo") !=
'To' ?
' (To)' :
'').
' : '.
dol_escape_htmltag($to));
2773 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailReply").($langs->trans(
"MailReply") !=
'Reply to' ?
' (Reply to)' :
'').
' : '.
dol_escape_htmltag($replyto));
2776 $descriptionmeta =
dol_concatdesc($descriptionmeta, $langs->trans(
"MailCC").($langs->trans(
"MailCC") !=
'CC' ?
' (CC)' :
'').
' : '.
dol_escape_htmltag($sendtocc));
2778 if ($savedattachmentsnote) {
2779 $descriptionmeta =
dol_concatdesc($descriptionmeta, $savedattachmentsnote);
2782 if ($operation[
'type'] ==
'ticket') {
2784 $tickettocreate =
new Ticket($this->db);
2785 $errorfetchticket = 0;
2786 $alreadycreated = 0;
2787 if (!empty($objectid) && $objectemail instanceof
Ticket) {
2788 $alreadycreated = $tickettocreate->fetch((
int) $objectid);
2790 if ($alreadycreated == 0 && $ticketid > 0) {
2791 $alreadycreated = $tickettocreate->fetch($ticketid);
2793 if ($alreadycreated == 0 && !empty($trackid)) {
2794 $alreadycreated = $tickettocreate->fetch(0,
'', $trackid);
2796 if ($alreadycreated == 0 && !empty($msgid)) {
2797 $alreadycreated = $tickettocreate->fetch(0,
'',
'', $msgid);
2799 if ($alreadycreated < 0) {
2800 $errorfetchticket++;
2802 if (empty($errorfetchticket)) {
2803 if ($alreadycreated == 0) {
2804 $operationslog .=
'<br>Ticket not found using trackid='.$trackid.
' or msgid='.$msgid;
2805 $ticketalreadyexists = 0;
2807 $operationslog .=
'<br>Ticket already found using trackid='.$trackid.
' or msgid='.$msgid.
", we replace operation 'ticket' with 'recordevent' to add a new message";
2808 $ticketalreadyexists = 1;
2809 $operation[
'type'] =
'recordevent';
2812 $ticketalreadyexists = -1;
2819 if ($operation[
'type'] ==
'loadthirdparty' || $operation[
'type'] ==
'loadandcreatethirdparty') {
2820 if ($thirdpartyid > 0) {
2822 $idtouseforthirdparty = $thirdpartyid;
2823 $operationslog .=
'<br>We already have found a related thirdparty id, so we bypass the action loadthirdparty and use idtouseforthirdparty='.$idtouseforthirdparty;
2824 } elseif (empty($operation[
'actionparam'])) {
2826 $this->error =
"Action loadthirdparty or loadandcreatethirdparty has empty parameter. Must be a rule like 'name=HEADER:^From:(.*);' or 'name=SET:xxx' or 'name=EXTRACT:(body|subject):regex where 'name' can be replaced with 'id' or 'email' to define how to set or extract data. More properties can also be set, for example client=SET:2;";
2827 $this->errors[] = $this->error;
2829 $actionparam = $operation[
'actionparam'];
2830 $idtouseforthirdparty =
'';
2831 $nametouseforthirdparty =
'';
2832 $emailtouseforthirdparty =
'';
2833 $namealiastouseforthirdparty =
'';
2835 $operationslog .=
'<br>Loop on each property to set into actionparam';
2839 foreach ($arrayvaluetouse as $propertytooverwrite => $valueforproperty) {
2843 $regforregex = array();
2845 if (preg_match(
'/^EXTRACT:([a-zA-Z0-9_]+):(.*)$/', $valueforproperty, $regforregex)) {
2846 $sourcefield = $regforregex[1];
2847 $regexstring = $regforregex[2];
2850 if (!empty($sourcefield) && !empty($regexstring)) {
2851 if (strtolower($sourcefield) ==
'body') {
2852 $sourcestring = $messagetext;
2853 } elseif (strtolower($sourcefield) ==
'subject') {
2854 $sourcestring = $subject;
2855 } elseif (strtolower($sourcefield) ==
'header') {
2856 $sourcestring = $header;
2859 if ($sourcestring) {
2860 $regforval = array();
2861 if (preg_match(
'/'.$regexstring.
'/ms', $sourcestring, $regforval)) {
2863 if ($propertytooverwrite ==
'id') {
2864 $idtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null;
2866 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found idtouseforthirdparty='.
dol_escape_htmltag($idtouseforthirdparty);
2867 } elseif ($propertytooverwrite ==
'email') {
2868 $emailtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null;
2870 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found emailtouseforthirdparty='.
dol_escape_htmltag($emailtouseforthirdparty);
2871 } elseif ($propertytooverwrite ==
'name') {
2872 $nametouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null;
2874 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found nametouseforthirdparty='.
dol_escape_htmltag($nametouseforthirdparty);
2875 } elseif ($propertytooverwrite ==
'name_alias') {
2876 $namealiastouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null;
2878 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Found namealiastouseforthirdparty='.
dol_escape_htmltag($namealiastouseforthirdparty);
2880 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> We discard this, not a field used to search an existing thirdparty';
2884 if (in_array($propertytooverwrite, array(
'id',
'email',
'name',
'name_alias'))) {
2885 $idtouseforthirdparty =
null;
2886 $nametouseforthirdparty =
null;
2887 $emailtouseforthirdparty =
null;
2888 $namealiastouseforthirdparty =
null;
2890 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Not found. Property searched is critical so we cancel the search.';
2892 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' Regex /'.
dol_escape_htmltag($regexstring).
'/ms into '.strtoupper($sourcefield).
' -> Not found';
2898 $this->error =
'The extract rule to use to load thirdparty for email '.$msgid.
' has an unknown source (must be HEADER, SUBJECT or BODY)';
2899 $this->errors[] = $this->error;
2901 $operationslog .=
'<br>'.$this->error;
2903 } elseif (preg_match(
'/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $reg)) {
2907 if ($propertytooverwrite ==
'id') {
2908 $idtouseforthirdparty = $reg[2];
2910 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property idtouseforthrdparty='.
dol_escape_htmltag($idtouseforthirdparty);
2911 } elseif ($propertytooverwrite ==
'email') {
2912 $emailtouseforthirdparty = $reg[2];
2914 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property emailtouseforthrdparty='.
dol_escape_htmltag($emailtouseforthirdparty);
2915 } elseif ($propertytooverwrite ==
'name') {
2916 $nametouseforthirdparty = $reg[2];
2918 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property nametouseforthirdparty='.
dol_escape_htmltag($nametouseforthirdparty);
2919 } elseif ($propertytooverwrite ==
'name_alias') {
2920 $namealiastouseforthirdparty = $reg[2];
2922 $operationslog .=
'<br>propertytooverwrite='.$propertytooverwrite.
' We set property namealiastouseforthirdparty='.
dol_escape_htmltag($namealiastouseforthirdparty);
2926 $this->error =
'Bad syntax for description of action parameters: '.$actionparam;
2927 $this->errors[] = $this->error;
2932 if (!$errorforactions && ($idtouseforthirdparty || $emailtouseforthirdparty || $nametouseforthirdparty || $namealiastouseforthirdparty)) {
2934 $operationslog .=
'<br>We have this initial main data to search thirdparty: id='.$idtouseforthirdparty.
', email='.$emailtouseforthirdparty.
', name='.$nametouseforthirdparty.
', name_alias='.$namealiastouseforthirdparty.
'.';
2937 $tmpobject->element =
'generic';
2938 $tmpobject->id = $idtouseforthirdparty;
2939 $tmpobject->name = $nametouseforthirdparty;
2940 $tmpobject->name_alias = $namealiastouseforthirdparty;
2941 $tmpobject->email = $emailtouseforthirdparty;
2945 $idtouseforthirdparty = $tmpobject->id;
2946 $nametouseforthirdparty = $tmpobject->name;
2947 $namealiastouseforthirdparty = $tmpobject->name_alias;
2948 $emailtouseforthirdparty = $tmpobject->email;
2950 $operationslog .=
'<br>We try to search existing thirdparty with idtouseforthirdparty='.$idtouseforthirdparty.
' emailtouseforthirdparty='.$emailtouseforthirdparty.
' nametouseforthirdparty='.$nametouseforthirdparty.
' namealiastouseforthirdparty='.$namealiastouseforthirdparty;
2953 $result = $thirdpartystatic->findNearest((
int) $idtouseforthirdparty, (
string) $nametouseforthirdparty,
'',
'',
'',
'',
'',
'',
'',
'', (
string) $emailtouseforthirdparty, (
string) $namealiastouseforthirdparty);
2956 if (
getDolGlobalInt(
'EMAILCOLLECTOR_USE_THIS_THIRDPARTY_ID_IF_DUPLICATE') && $result == -2) {
2957 $idtouseforthirdparty =
getDolGlobalInt(
'EMAILCOLLECTOR_USE_THIS_THIRDPARTY_ID_IF_DUPLICATE');
2959 $thirdpartystatic->fetch($idtouseforthirdparty);
2961 dol_syslog(
'Thirdparty found twice (or more) so, according to option EMAILCOLLECTOR_USE_THIS_THIRDPARTY_ID_IF_DUPLICATE, we will use the generic one with id = '.
dol_escape_htmltag((
string) $thirdpartystatic->id).
" and name ".
dol_escape_htmltag($thirdpartystatic->name));
2963 $operationslog .=
'<br>Thirdparty found twice (or more) so, according to option EMAILCOLLECTOR_USE_THIS_THIRDPARTY_ID_IF_DUPLICATE, we will use the generic one with id = '.dol_escape_htmltag((
string) $thirdpartystatic->id).
" and name ".
dol_escape_htmltag($thirdpartystatic->name);
2966 $this->error =
'Error when getting thirdparty with name '.((string) $nametouseforthirdparty).
', alternative name '.((
string) $namealiastouseforthirdparty).
' and email '.$emailtouseforthirdparty.
' (may be 2 record exists with same name ?)';
2967 $this->errors[] = $this->error;
2972 if ($operation[
'type'] ==
'loadthirdparty') {
2973 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was not found");
2976 $resultContact = $contactstatic->findNearest(0,
'',
'',
'', (
string) $emailtouseforthirdparty,
'', 0);
2977 if ($resultContact > 0) {
2978 $contactstatic->fetch($resultContact);
2979 $idtouseforthirdparty = $contactstatic->socid;
2980 $result = $thirdpartystatic->fetch($idtouseforthirdparty);
2982 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was found thanks to linked contact search");
2985 $langs->load(
"errors");
2986 $this->error = $langs->trans(
'ErrorFailedToLoadThirdParty', (
string) $idtouseforthirdparty, (
string) $emailtouseforthirdparty, (
string) $nametouseforthirdparty, (
string) $namealiastouseforthirdparty);
2987 $this->errors[] = $this->error;
2991 $langs->load(
"errors");
2992 $this->error = $langs->trans(
'ErrorFailedToLoadThirdParty', (
string) $idtouseforthirdparty, (
string) $emailtouseforthirdparty, (
string) $nametouseforthirdparty, (
string) $namealiastouseforthirdparty);
2993 $this->errors[] = $this->error;
2995 } elseif ($operation[
'type'] ==
'loadandcreatethirdparty') {
2996 dol_syslog(
"Third party with id=".$idtouseforthirdparty.
" email=".$emailtouseforthirdparty.
" name=".$nametouseforthirdparty.
" name_alias=".$namealiastouseforthirdparty.
" was not found. We try to create it.");
2999 $thirdpartystatic =
new Societe($db);
3000 $thirdpartystatic->name = (
string) $nametouseforthirdparty;
3001 if (!empty($namealiastouseforthirdparty)) {
3002 if ($namealiastouseforthirdparty != $nametouseforthirdparty) {
3003 $thirdpartystatic->name_alias = $namealiastouseforthirdparty;
3006 $thirdpartystatic->name_alias = (empty($replytostring) ? (empty($fromtext) ?
'' : $fromtext) : $replytostring);
3008 $thirdpartystatic->email = (empty($emailtouseforthirdparty) ? (empty($replyto) ? (empty($from) ?
'' : $from) : $replyto) : $emailtouseforthirdparty);
3011 $errorforthisaction = $this->
overwritePropertiesOfObject($thirdpartystatic, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3013 if ($thirdpartystatic->client && empty($thirdpartystatic->code_client)) {
3014 $thirdpartystatic->code_client =
'auto';
3016 if ($thirdpartystatic->fournisseur && empty($thirdpartystatic->code_fournisseur)) {
3017 $thirdpartystatic->code_fournisseur =
'auto';
3020 if ($errorforthisaction) {
3023 $result = $thirdpartystatic->create($user);
3026 $this->error = $thirdpartystatic->error;
3027 $this->errors = $thirdpartystatic->errors;
3029 $operationslog .=
'<br>Thirdparty created -> id = '.dol_escape_htmltag((
string) $thirdpartystatic->id);
3033 } elseif ($result > 0) {
3034 dol_syslog(
"One and only one existing third party has been found");
3036 $thirdpartystatic->fetch($result);
3038 $operationslog .=
'<br>Thirdparty already exists with id = '.dol_escape_htmltag((
string) $thirdpartystatic->id).
" and name ".
dol_escape_htmltag($thirdpartystatic->name);
3042 } elseif ($operation[
'type'] ==
'loadandcreatecontact') {
3043 if (empty($operation[
'actionparam'])) {
3045 $this->error =
"Action loadandcreatecontact has empty parameter. Must be 'SET:xxx' or 'EXTRACT:(body|subject):regex' to define how to extract data";
3046 $this->errors[] = $this->error;
3048 $contact_static =
new Contact($this->db);
3050 $errorforthisaction = $this->
overwritePropertiesOfObject($contact_static, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3051 if ($errorforthisaction) {
3054 if (!empty($contact_static->email) && $contact_static->email != $from) {
3055 $from = $contact_static->email;
3061 $result = $contactstatic->fetch(0,
null,
'', $from);
3064 $this->error =
'Error when getting contact with email ' . $from;
3065 $this->errors[] = $this->error;
3067 } elseif ($result == 0) {
3068 dol_syslog(
"Contact with email " . $from .
" was not found. We try to create it.");
3069 $contactstatic =
new Contact($this->db);
3072 $contactstatic->email = $from;
3073 $operationslog .=
'<br>We set property email='.dol_escape_htmltag($from);
3076 $errorforthisaction = $this->
overwritePropertiesOfObject($contactstatic, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3078 if ($errorforthisaction) {
3082 if (!empty($contactstatic->country)) {
3083 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
3084 $result =
getCountry(
'',
'3', $this->db,
null, 1, $contactstatic->country);
3085 if ($result ==
'NotDefined') {
3087 $this->error =
"Error country not found by this name '" . $contactstatic->country .
"'";
3088 } elseif (!($result > 0)) {
3090 $this->error =
"Error when search country by this name '" . $contactstatic->country .
"'";
3091 $this->errors[] = $this->db->lasterror();
3093 $contactstatic->country_id = $result;
3094 $operationslog .=
'<br>We set property country_id='.dol_escape_htmltag($result);
3096 } elseif (!empty($contactstatic->country_code)) {
3097 require_once DOL_DOCUMENT_ROOT .
'/core/lib/company.lib.php';
3098 $result =
getCountry($contactstatic->country_code,
'3', $this->db);
3099 if ($result ==
'NotDefined') {
3101 $this->error =
"Error country not found by this code '" . $contactstatic->country_code .
"'";
3102 } elseif (!($result > 0)) {
3104 $this->error =
"Error when search country by this code '" . $contactstatic->country_code .
"'";
3105 $this->errors[] = $this->db->lasterror();
3107 $contactstatic->country_id = $result;
3108 $operationslog .=
'<br>We set property country_id='.dol_escape_htmltag($result);
3112 if (!$errorforactions) {
3114 if (!empty($contactstatic->state)) {
3115 require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions.lib.php';
3116 $result =
dol_getIdFromCode($this->db, $contactstatic->state,
'c_departements',
'nom',
'rowid');
3117 if (empty($result)) {
3119 $this->error =
"Error state not found by this name '" . $contactstatic->state .
"'";
3120 } elseif (!($result > 0)) {
3122 $this->error =
"Error when search state by this name '" . $contactstatic->state .
"'";
3123 $this->errors[] = $this->db->lasterror();
3125 $contactstatic->state_id = $result;
3126 $operationslog .=
'<br>We set property state_id='.dol_escape_htmltag($result);
3128 } elseif (!empty($contactstatic->state_code)) {
3129 require_once DOL_DOCUMENT_ROOT .
'/core/lib/functions.lib.php';
3130 $result =
dol_getIdFromCode($this->db, $contactstatic->state_code,
'c_departements',
'code_departement',
'rowid');
3131 if (empty($result)) {
3133 $this->error =
"Error state not found by this code '" . $contactstatic->state_code .
"'";
3134 } elseif (!($result > 0)) {
3136 $this->error =
"Error when search state by this code '" . $contactstatic->state_code .
"'";
3137 $this->errors[] = $this->db->lasterror();
3139 $contactstatic->state_id = $result;
3140 $operationslog .=
'<br>We set property state_id='.dol_escape_htmltag($result);
3145 if (!$errorforactions) {
3146 $result = $contactstatic->create($user);
3149 $this->error = $contactstatic->error;
3150 $this->errors = $contactstatic->errors;
3152 $operationslog .=
'<br>Contact created -> id = '.dol_escape_htmltag((
string) $contactstatic->id);
3159 } elseif ($operation[
'type'] ==
'recordevent') {
3163 $alreadycreated = $actioncomm->fetch(0,
'',
'', $msgid);
3164 if ($alreadycreated == 0) {
3165 $operationslog .=
'<br>We did not find existing actioncomm with msgid='.$msgid;
3167 if ($projectstatic->id > 0) {
3168 if ($projectfoundby) {
3169 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Project found from '.$projectfoundby);
3172 if ($thirdpartystatic->id > 0) {
3173 if ($thirdpartyfoundby) {
3174 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
3177 if ($contactstatic->id > 0) {
3178 if ($contactfoundby) {
3179 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
3183 $description = $descriptiontitle;
3189 $descriptionfull = $description;
3191 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
3196 $actioncomm->type_code =
'AC_OTH_AUTO';
3197 $actioncomm->code =
'AC_'.$actioncode;
3198 $actioncomm->label = $langs->trans(
"ActionAC_".$actioncode).
' - '.$langs->trans(
"MailFrom").
' '.$from;
3199 $actioncomm->note_private = $descriptionfull;
3200 $actioncomm->fk_project = $projectstatic->id;
3201 $actioncomm->datep = $dateemail;
3202 $actioncomm->datef = $dateemail;
3203 $actioncomm->percentage = -1;
3204 $actioncomm->socid = $thirdpartystatic->id;
3205 $actioncomm->contact_id = $contactstatic->id;
3206 $actioncomm->socpeopleassigned = (!empty($contactstatic->id) ? array($contactstatic->id) : array());
3207 $actioncomm->authorid = $user->id;
3208 $actioncomm->userownerid = $user->id;
3210 $actioncomm->email_msgid = $msgid;
3211 $actioncomm->email_from = $fromstring;
3212 $actioncomm->email_sender = $sender;
3213 $actioncomm->email_to = $to;
3214 $actioncomm->email_tocc = $sendtocc;
3215 $actioncomm->email_tobcc = $sendtobcc;
3216 $actioncomm->email_subject = $subject;
3217 $actioncomm->errors_to =
'';
3219 if (!in_array($fk_element_type, array(
'societe',
'contact',
'project',
'user'))) {
3220 $actioncomm->fk_element = $fk_element_id;
3221 $actioncomm->elementid = $fk_element_id;
3222 $actioncomm->elementtype = $fk_element_type;
3223 if (is_object($objectemail) && $objectemail->module) {
3224 $actioncomm->elementtype .=
'@'.$objectemail->module;
3231 $errorforthisaction = $this->
overwritePropertiesOfObject($actioncomm, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3233 if ($errorforthisaction) {
3236 $result = $actioncomm->create($user);
3239 $this->errors = $actioncomm->errors;
3241 if ($fk_element_type ==
"ticket" && is_object($objectemail)) {
3242 if ($objectemail->status == Ticket::STATUS_CLOSED || $objectemail->status == Ticket::STATUS_CANCELED || $objectemail->status == Ticket::STATUS_NEED_MORE_INFO || $objectemail->status == Ticket::STATUS_WAITING) {
3243 if ($objectemail->fk_user_assign !=
null) {
3244 $res = $objectemail->setStatut(Ticket::STATUS_ASSIGNED);
3246 $res = $objectemail->setStatut(Ticket::STATUS_NOT_READ);
3250 $operationslog .=
'<br>Ticket Re-Opened successfully -> ref='.$objectemail->ref;
3253 $this->error =
'Error while changing the ticket status -> ref='.$objectemail->ref;
3254 $this->errors[] = $this->error;
3257 if (!empty($attachments)) {
3259 $ticket =
new Ticket($this->db);
3260 $ticket->fetch($fk_element_id);
3261 $destdir =
$conf->ticket->dir_output.
'/'.$ticket->ref;
3266 foreach ($attachments as $attachment) {
3267 $attachment->save($destdir.
'/');
3270 $this->
getmsg($connection, $imapemail, $destdir);
3275 $operationslog .=
'<br>Event created -> id='.dol_escape_htmltag((
string) $actioncomm->id);
3279 $operationslog .=
'<br>An event in actioncomm table already exists for the msgid = '.$msgid.
' so we bypass this action.';
3281 } elseif ($operation[
'type'] ==
'recordjoinpiece') {
3284 foreach ($attachments as $attachment) {
3285 if ($attachment->getName() ===
'undefined') {
3288 $data[$attachment->getName()] = $attachment->getContent();
3292 foreach ($pj as $key => $val) {
3293 $data[$val[
'filename']] =
getFileData($imapemail, (
string) $val[
'pos'], $val[
'type'], $connection);
3296 if (count($data) > 0) {
3297 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"user WHERE email LIKE '%".$this->db->escape($from).
"%'";
3298 $resql = $this->db->query($sql);
3299 if ($this->db->num_rows($resql) == 0) {
3300 $this->errors[] =
"User Not allowed to add documents ({$from})";
3302 $arrayobject = array(
3303 'propale' => array(
'table' =>
'propal',
3304 'fields' => array(
'ref'),
3305 'class' =>
'comm/propal/class/propal.class.php',
3306 'object' =>
'Propal'),
3307 'holiday' => array(
'table' =>
'holiday',
3308 'fields' => array(
'ref'),
3309 'class' =>
'holiday/class/holiday.class.php',
3310 'object' =>
'Holiday'),
3311 'expensereport' => array(
'table' =>
'expensereport',
3312 'fields' => array(
'ref'),
3313 'class' =>
'expensereport/class/expensereport.class.php',
3314 'object' =>
'ExpenseReport'),
3315 'recruitment/recruitmentjobposition' => array(
'table' =>
'recruitment_recruitmentjobposition',
3316 'fields' => array(
'ref'),
3317 'class' =>
'recruitment/class/recruitmentjobposition.class.php',
3318 'object' =>
'RecruitmentJobPosition'),
3319 'recruitment/recruitmentcandidature' => array(
'table' =>
'recruitment_recruitmentcandidature',
3320 'fields' => array(
'ref'),
3321 'class' =>
'recruitment/class/recruitmentcandidature.class.php',
3322 'object' =>
'RecruitmentCandidature'),
3323 'societe' => array(
'table' =>
'societe',
3324 'fields' => array(
'code_client',
'code_fournisseur'),
3325 'class' =>
'societe/class/societe.class.php',
3326 'object' =>
'Societe'),
3327 'commande' => array(
'table' =>
'commande',
3328 'fields' => array(
'ref'),
3329 'class' =>
'commande/class/commande.class.php',
3330 'object' =>
'Commande'),
3331 'expedition' => array(
'table' =>
'expedition',
3332 'fields' => array(
'ref'),
3333 'class' =>
'expedition/class/expedition.class.php',
3334 'object' =>
'Expedition'),
3335 'contract' => array(
'table' =>
'contrat',
3336 'fields' => array(
'ref'),
3337 'class' =>
'contrat/class/contrat.class.php',
3338 'object' =>
'Contrat'),
3339 'fichinter' => array(
'table' =>
'fichinter',
3340 'fields' => array(
'ref'),
3341 'class' =>
'fichinter/class/fichinter.class.php',
3342 'object' =>
'Fichinter'),
3343 'ticket' => array(
'table' =>
'ticket',
3344 'fields' => array(
'ref'),
3345 'class' =>
'ticket/class/ticket.class.php',
3346 'object' =>
'Ticket'),
3347 'knowledgemanagement' => array(
'table' =>
'knowledgemanagement_knowledgerecord',
3348 'fields' => array(
'ref'),
3349 'class' =>
'knowledgemanagement/class/knowledgemanagement.class.php',
3350 'object' =>
'KnowledgeRecord'),
3351 'supplier_proposal' => array(
'table' =>
'supplier_proposal',
3352 'fields' => array(
'ref'),
3353 'class' =>
'supplier_proposal/class/supplier_proposal.class.php',
3354 'object' =>
'SupplierProposal'),
3355 'fournisseur/commande' => array(
'table' =>
'commande_fournisseur',
3356 'fields' => array(
'ref',
'ref_supplier'),
3357 'class' =>
'fourn/class/fournisseur.commande.class.php',
3358 'object' =>
'SupplierProposal'),
3359 'facture' => array(
'table' =>
'facture',
3360 'fields' => array(
'ref'),
3361 'class' =>
'compta/facture/class/facture.class.php',
3362 'object' =>
'Facture'),
3363 'fournisseur/facture' => array(
'table' =>
'facture_fourn',
3364 'fields' => array(
'ref',
'ref_client'),
3365 'class' =>
'fourn/class/fournisseur.facture.class.php',
3366 'object' =>
'FactureFournisseur'),
3367 'produit' => array(
'table' =>
'product',
3368 'fields' => array(
'ref'),
3369 'class' =>
'product/class/product.class.php',
3370 'object' =>
'Product'),
3371 'productlot' => array(
'table' =>
'product_lot',
3372 'fields' => array(
'batch'),
3373 'class' =>
'product/stock/class/productlot.class.php',
3374 'object' =>
'Productlot'),
3375 'projet' => array(
'table' =>
'projet',
3376 'fields' => array(
'ref'),
3377 'class' =>
'projet/class/projet.class.php',
3378 'object' =>
'Project'),
3379 'projet_task' => array(
'table' =>
'projet_task',
3380 'fields' => array(
'ref'),
3381 'class' =>
'projet/class/task.class.php',
3382 'object' =>
'Task'),
3383 'ressource' => array(
'table' =>
'resource',
3384 'fields' => array(
'ref'),
3385 'class' =>
'ressource/class/dolressource.class.php',
3386 'object' =>
'Dolresource'),
3387 'bom' => array(
'table' =>
'bom_bom',
3388 'fields' => array(
'ref'),
3389 'class' =>
'bom/class/bom.class.php',
3391 'mrp' => array(
'table' =>
'mrp_mo',
3392 'fields' => array(
'ref'),
3393 'class' =>
'mrp/class/mo.class.php',
3397 if (!is_object($hookmanager)) {
3398 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
3401 $hookmanager->initHooks(array(
'emailcolector'));
3402 $parameters = array(
'arrayobject' => $arrayobject);
3403 $reshook = $hookmanager->executeHooks(
'addmoduletoeamailcollectorjoinpiece', $parameters);
3405 $arrayobject = $hookmanager->resArray;
3408 $resultobj = array();
3410 foreach ($arrayobject as $key => $objectdesc) {
3411 $sql =
'SELECT DISTINCT t.rowid ';
3412 $sql .=
' FROM ' . MAIN_DB_PREFIX . $this->db->sanitize($objectdesc[
'table']) .
' AS t';
3414 foreach ($objectdesc[
'fields'] as $field) {
3415 $sql .=
"('" .$this->db->escape($subject) .
"' LIKE CONCAT('%', t." . $this->db->sanitize($field) .
", '%') AND t." . $this->db->sanitize($field) .
" <> '') OR ";
3417 $sql = substr($sql, 0, -4);
3419 $ressqlobj = $this->db->query($sql);
3421 while ($obj = $this->db->fetch_object($ressqlobj)) {
3422 $resultobj[$key][] = $obj->rowid;
3427 foreach ($resultobj as $mod => $ids) {
3428 $moddesc = $arrayobject[$mod];
3429 $elementpath = $mod;
3431 $objectmanaged =
new $moddesc[
'object']($this->db);
3432 '@phan-var-force CommonObject $objectmanaged';
3433 foreach ($ids as $val) {
3434 $res = $objectmanaged->fetch($val);
3436 $path = ($objectmanaged->entity > 1 ?
"/" . $objectmanaged->entity :
'');
3437 $dirs[] = DOL_DATA_ROOT . $path .
"/" . $elementpath .
'/' .
dol_sanitizeFileName($objectmanaged->ref) .
'/';
3439 $this->errors[] =
'object not found';
3443 foreach ($dirs as $target) {
3444 $prefix = $this->actions[$this->id][
'actionparam'];
3445 foreach ($data as $filename => $content) {
3446 $resr =
saveAttachment($target, $prefix .
'_' . $filename, $content);
3448 $this->errors[] =
'Doc not saved';
3453 $operationslog .=
'<br>Save attachment files on disk';
3455 $this->errors[] =
'no joined piece';
3457 $operationslog .=
'<br>No joinded files';
3459 } elseif ($operation[
'type'] ==
'project') {
3461 $projecttocreate =
new Project($this->db);
3462 $alreadycreated = $projecttocreate->fetch(0,
'',
'', $msgid);
3463 if ($alreadycreated == 0) {
3464 if ($thirdpartystatic->id > 0) {
3465 $projecttocreate->socid = $thirdpartystatic->id;
3466 if ($thirdpartyfoundby) {
3467 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
3470 if ($contactstatic->id > 0) {
3471 $projecttocreate->contact_id = $contactstatic->id;
3472 if ($contactfoundby) {
3473 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
3477 $description = $descriptiontitle;
3483 $descriptionfull = $description;
3485 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
3489 $id_opp_status =
dol_getIdFromCode($this->db,
'PROSP',
'c_lead_status',
'code',
'rowid');
3490 $percent_opp_status =
dol_getIdFromCode($this->db,
'PROSP',
'c_lead_status',
'code',
'percent');
3492 $projecttocreate->title = $subject;
3493 $projecttocreate->date_start = $dateemail;
3494 $projecttocreate->date_end = 0;
3495 $projecttocreate->opp_status = $id_opp_status;
3496 $projecttocreate->opp_percent = $percent_opp_status;
3498 $projecttocreate->note_private = $descriptionfull;
3499 $projecttocreate->entity =
$conf->entity;
3501 $projecttocreate->email_date = $dateemail;
3502 $projecttocreate->email_msgid = $msgid;
3503 $projecttocreate->email_from = $fromstring;
3504 $projecttocreate->email_sender = $sender;
3505 $projecttocreate->email_to = $to;
3506 $projecttocreate->email_tocc = $sendtocc;
3507 $projecttocreate->email_tobcc = $sendtobcc;
3508 $projecttocreate->email_subject = $subject;
3509 $projecttocreate->errors_to =
'';
3511 $savesocid = $projecttocreate->socid;
3515 $errorforthisaction = $this->
overwritePropertiesOfObject($projecttocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3519 if (empty($projecttocreate->ref)) {
3528 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
3529 foreach ($dirmodels as $reldir) {
3530 $file =
dol_buildpath($reldir.
"core/modules/project/".$modele.
'.php', 0);
3531 if (file_exists($file)) {
3532 $classname = $modele;
3537 if ($classname !==
'') {
3538 if ($savesocid > 0) {
3539 if ($savesocid != $projecttocreate->socid) {
3541 setEventMessages(
'You loaded a thirdparty (id='.$savesocid.
') and you force another thirdparty id (id='.$projecttocreate->socid.
') by setting socid in operation with a different value',
null,
'errors');
3544 if ($projecttocreate->socid > 0) {
3545 $thirdpartystatic->fetch($projecttocreate->socid);
3549 $result =
dol_include_once($reldir.
"core/modules/project/".$modele.
'.php');
3550 $modModuleToUseForNextValue =
new $classname();
3551 '@phan-var-force ModeleNumRefProjects $modModuleToUseForNextValue';
3552 $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $projecttocreate);
3554 $projecttocreate->ref = $defaultref;
3558 if ($errorforthisaction) {
3561 if (empty($projecttocreate->ref) || (is_numeric($projecttocreate->ref) && $projecttocreate->ref <= 0)) {
3563 $this->error =
'Failed to create project: Can\'t get a valid value for the field ref with numbering template = '.$modele.
', thirdparty id = '.$thirdpartystatic->id;
3565 $operationslog .=
'<br>'.$this->error;
3568 $result = $projecttocreate->create($user);
3571 $this->error =
'Failed to create project: '.$langs->trans($projecttocreate->error);
3572 $this->errors = $projecttocreate->errors;
3574 $operationslog .=
'<br>'.$this->error;
3577 $destdir =
$conf->project->dir_output.
'/'.$projecttocreate->ref;
3582 foreach ($attachments as $attachment) {
3584 $typeattachment = (
string) $attachment->getDisposition();
3585 $filename = $attachment->getFilename();
3586 $content = $attachment->getContent();
3590 $getMsg = $this->
getmsg($connection, $imapemail, $destdir);
3592 $this->errors = array_merge($this->errors, [$this->error]);
3597 $operationslog .=
'<br>Project created with attachments -> id='.dol_escape_htmltag((
string) $projecttocreate->id);
3599 $operationslog .=
'<br>Project created without attachments -> id='.dol_escape_htmltag((
string) $projecttocreate->id);
3607 $operationslog .=
'<br>Project already exists for msgid ='.dol_escape_htmltag($msgid);
3609 } elseif ($operation[
'type'] ==
'ticket') {
3611 $tickettocreate =
new Ticket($this->db);
3612 if ($ticketalreadyexists == 0) {
3613 if ($thirdpartystatic->id > 0) {
3614 $tickettocreate->socid = $thirdpartystatic->id;
3615 $tickettocreate->fk_soc = $thirdpartystatic->id;
3616 if ($thirdpartyfoundby) {
3617 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Third party found from '.$thirdpartyfoundby);
3620 if ($contactstatic->id > 0) {
3621 $tickettocreate->contact_id = $contactstatic->id;
3622 if ($contactfoundby) {
3623 $descriptionmeta =
dol_concatdesc($descriptionmeta,
'Contact/address found from '.$contactfoundby);
3627 $description = $descriptiontitle;
3633 $descriptionfull = $description;
3635 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
3639 $tickettocreate->subject = $subject;
3640 $tickettocreate->message = $description;
3642 $tickettocreate->category_code = (
getDolGlobalString(
'MAIN_EMAILCOLLECTOR_TICKET_CATEGORY_CODE',
dol_getIdFromCode($this->db, 1,
'c_ticket_category',
'use_default',
'code', 1)));
3643 $tickettocreate->severity_code = (
getDolGlobalString(
'MAIN_EMAILCOLLECTOR_TICKET_SEVERITY_CODE',
dol_getIdFromCode($this->db, 1,
'c_ticket_severity',
'use_default',
'code', 1)));
3644 $tickettocreate->origin_email = $from;
3645 $tickettocreate->origin_replyto = (!empty($replyto) ? $replyto :
null);
3646 $tickettocreate->origin_references = (!empty($headers[
'References']) ? $headers[
'References'] :
null);
3647 $tickettocreate->fk_user_create = $user->id;
3648 $tickettocreate->datec =
dol_now();
3649 $tickettocreate->fk_project = $projectstatic->id;
3650 $tickettocreate->notify_tiers_at_create =
getDolGlobalInt(
'TICKET_CHECK_NOTIFY_THIRDPARTY_AT_CREATION');
3651 $tickettocreate->note_private = $descriptionfull;
3652 $tickettocreate->entity =
$conf->entity;
3654 $tickettocreate->email_date = $dateemail;
3655 $tickettocreate->email_msgid = $msgid;
3656 $tickettocreate->email_from = $fromstring;
3657 $tickettocreate->email_sender = $sender;
3658 $tickettocreate->email_to = $to;
3659 $tickettocreate->email_tocc = $sendtocc;
3660 $tickettocreate->email_tobcc = $sendtobcc;
3661 $tickettocreate->email_subject = $subject;
3662 $tickettocreate->errors_to =
'';
3666 $savesocid = $tickettocreate->socid;
3670 $errorforthisaction = $this->
overwritePropertiesOfObject($tickettocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3672 $modele =
'UNDEFINED';
3674 if (empty($tickettocreate->ref)) {
3683 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
3684 foreach ($dirmodels as $reldir) {
3685 $file =
dol_buildpath($reldir.
"core/modules/ticket/".$modele.
'.php', 0);
3686 if (file_exists($file)) {
3687 $classname = $modele;
3692 if ($classname !==
'') {
3693 if ($savesocid > 0) {
3694 if ($savesocid != $tickettocreate->socid) {
3696 setEventMessages(
'You loaded a thirdparty (id='.$savesocid.
') and you force another thirdparty id (id='.$tickettocreate->socid.
') by setting socid in operation with a different value',
null,
'errors');
3699 if ($tickettocreate->socid > 0) {
3700 $thirdpartystatic->fetch($tickettocreate->socid);
3705 $modModuleToUseForNextValue =
new $classname();
3706 '@phan-var-force ModeleNumRefTicket $modModuleToUseForNextValue';
3707 $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $tickettocreate);
3709 $tickettocreate->ref = $defaultref;
3712 if ($errorforthisaction) {
3715 if (is_numeric($tickettocreate->ref) && $tickettocreate->ref <= 0) {
3717 $this->error =
'Failed to create ticket: Can\'t get a valid value for the field ref with numbering template = '.$modele.
', thirdparty id = '.$thirdpartystatic->id;
3720 $tickettocreate->context[
'actionmsg2'] = $langs->trans(
"ActionAC_EMAIL_IN").
' - '.$langs->trans(
"TICKET_CREATEInDolibarr");
3721 $tickettocreate->context[
'actionmsg'] = $langs->trans(
"ActionAC_EMAIL_IN").
' - '.$langs->trans(
"TICKET_CREATEInDolibarr");
3725 $sender_contact =
new Contact($this->db);
3726 $sender_contact->fetch(0,
null,
'', $from);
3727 if (!empty($sender_contact->id)) {
3728 $tickettocreate->context[
'contact_id'] = $sender_contact->id;
3731 $result = $tickettocreate->create($user);
3734 $this->error =
'Failed to create ticket: '.$langs->trans($tickettocreate->error);
3735 $this->errors = $tickettocreate->errors;
3738 $destdir =
$conf->ticket->dir_output.
'/'.$tickettocreate->ref;
3743 foreach ($attachments as $attachment) {
3745 $typeattachment = (
string) $attachment->getDisposition();
3746 $filename = $attachment->getName();
3747 $content = $attachment->getContent();
3751 $getMsg = $this->
getmsg($connection, $imapemail, $destdir);
3753 $this->errors = array_merge($this->errors, [$this->error]);
3758 $operationslog .=
'<br>Ticket created with attachments -> id='.dol_escape_htmltag((
string) $tickettocreate->id);
3760 $operationslog .=
'<br>Ticket created without attachments -> id='.dol_escape_htmltag((
string) $tickettocreate->id);
3769 $operationslog .=
'<br>Ticket already exists, so we bypass action "ticket"';
3771 } elseif ($operation[
'type'] ==
'candidature') {
3775 $alreadycreated = $candidaturetocreate->fetch(0,
'', $msgid);
3776 if ($alreadycreated == 0) {
3777 $description = $descriptiontitle;
3783 $descriptionfull = $description;
3785 $descriptionfull =
dol_concatdesc($descriptionfull,
"----- Header");
3789 $candidaturetocreate->subject = $subject;
3790 $candidaturetocreate->message = $description;
3791 $candidaturetocreate->type_code = 0;
3792 $candidaturetocreate->category_code =
null;
3793 $candidaturetocreate->severity_code =
null;
3794 $candidaturetocreate->email = $from;
3796 $candidaturetocreate->fk_user_creat = $user->id;
3797 $candidaturetocreate->date_creation =
dol_now();
3798 $candidaturetocreate->fk_project = $projectstatic->id;
3799 $candidaturetocreate->description = $description;
3800 $candidaturetocreate->note_private = $descriptionfull;
3801 $candidaturetocreate->entity =
$conf->entity;
3802 $candidaturetocreate->email_msgid = $msgid;
3803 $candidaturetocreate->email_date = $dateemail;
3804 $candidaturetocreate->status = $candidaturetocreate::STATUS_DRAFT;
3809 $errorforthisaction = $this->
overwritePropertiesOfObject($candidaturetocreate, $operation[
'actionparam'], $messagetext, $subject, $header, $operationslog);
3852 if ($errorforthisaction) {
3856 $result = $candidaturetocreate->create($user);
3859 $this->error =
'Failed to create candidature: '.implode(
', ', $candidaturetocreate->errors);
3860 $this->errors = $candidaturetocreate->errors;
3863 $operationslog .=
'<br>Candidature created without attachments -> id='.dol_escape_htmltag((
string) $candidaturetocreate->id);
3866 } elseif (substr($operation[
'type'], 0, 4) ==
'hook') {
3869 if (!is_object($hookmanager)) {
3870 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
3873 $hookmanager->initHooks([
'emailcolector']);
3875 $parameters = array(
3876 'connection' => $connection,
3877 'imapemail' => $imapemail,
3878 'overview' => $overview,
3881 'fromtext' => $fromtext,
3883 'actionparam' => $operation[
'actionparam'],
3885 'thirdpartyid' => ($thirdpartyid ? $thirdpartyid : $thirdpartystatic->id),
3886 'objectid' => $objectid,
3887 'objectemail' => $objectemail,
3889 'messagetext' => $messagetext,
3890 'subject' => $subject,
3891 'header' => $header,
3892 'attachments' => $attachments,
3893 'savedattachments' => $savedattachments,
3894 'savedattachmentsdir' => $savedattachmentsdir,
3896 $reshook = $hookmanager->executeHooks(
'doCollectImapOneCollector', $parameters, $this, $operation[
'type']);
3899 $errorforthisaction++;
3900 $this->error = $hookmanager->resPrint;
3901 if (!empty($hookmanager->resPrint)) {
3902 $operationslog .=
'<br>Hook error: '.dol_escape_htmltag($hookmanager->resPrint);
3905 if ($errorforthisaction) {
3907 $operationslog .=
'<br>Hook doCollectImapOneCollector executed with error';
3909 $operationslog .=
'<br>Hook doCollectImapOneCollector executed without error';
3913 if (!$errorforactions) {
3914 $nbactiondoneforemail++;
3920 if (!$errorforactions) {
3921 if (!empty($targetdir)) {
3924 dol_syslog(
"EmailCollector::doCollectOneCollector move message ".($imapemail->getHeader()->get(
'subject')).
" to ".$targetdir, LOG_DEBUG);
3925 $operationslog .=
'<br>Move mail '.($this->uidAsString($imapemail)).
' - '.$msgid.
' - '.$imapemail->getHeader()->get(
'subject').
' to '.$targetdir;
3927 $arrayofemailtodelete[$this->
uidAsString($imapemail)] = $imapemail;
3930 dol_syslog(
"EmailCollector::doCollectOneCollector move message ".($this->
uidAsString($imapemail)).
" to ".$connectstringtarget, LOG_DEBUG);
3931 $operationslog .=
'<br>Move mail '.($this->uidAsString($imapemail)).
' - '.$msgid;
3933 $arrayofemailtodelete[$imapemail] = $msgid;
3938 dol_syslog(
"EmailCollector::doCollectOneCollector message ".($this->
uidAsString($imapemail)).
" '".($imapemail->getHeader()->get(
'subject')).
"' using this->host=".$this->host.
", this->access_type=".$this->acces_type.
" was set to read", LOG_DEBUG);
3940 dol_syslog(
"EmailCollector::doCollectOneCollector message ".($this->
uidAsString($imapemail)).
" to ".$connectstringtarget.
" was set to read", LOG_DEBUG);
3948 unset($objectemail);
3949 unset($projectstatic);
3950 unset($thirdpartystatic);
3951 unset($contactstatic);
3953 $nbemailprocessed++;
3955 if (!$errorforemail) {
3956 $nbactiondone += $nbactiondoneforemail;
3960 $this->db->commit();
3962 $this->db->rollback();
3966 if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) {
3967 dol_syslog(
"EmailCollect::doCollectOneCollector We reach maximum of ".$nbemailok.
" collected with success, so we stop this collector now.");
3968 $datelastok = strtotime($headers[
'Date']);
3974 $this->db->rollback();
3978 $output = $langs->trans(
'XEmailsDoneYActionsDone', $nbemailprocessed, $nbemailok, $nbactiondone);
3980 dol_syslog(
"End of loop on emails", LOG_INFO, -1);
3982 $langs->load(
"admin");
3983 $output = $langs->trans(
'NoNewEmailToProcess');
3984 $output .=
' (defaultlang='.$langs->defaultlang.
')';
3990 krsort($arrayofemailtodelete, SORT_NUMERIC);
3992 foreach ($arrayofemailtodelete as $imapemailnum => $imapemail) {
3993 dol_syslog(
"EmailCollect::doCollectOneCollector delete email ".$imapemailnum);
3995 $operationslog .=
"<br> move email ".$imapemailnum.($mode > 0 ?
' (test)' :
'');
3997 if (empty($mode) && empty($error)) {
3998 $tmptargetdir = $targetdir;
4005 $result = $imapemail->move($tmptargetdir,
false);
4008 $operationslog .=
'<br>Exception !!!! '.$e->getMessage();
4010 if (empty($result)) {
4011 dol_syslog(
"Failed to move email into target directory ".$targetdir);
4012 $operationslog .=
'<br>Failed to move email into target directory '.$targetdir;
4018 if (empty($mode) && empty($error)) {
4020 $operationslog .=
"<br>Expunge";
4025 $client->disconnect();
4027 foreach ($arrayofemailtodelete as $imapemail => $msgid) {
4028 dol_syslog(
"EmailCollect::doCollectOneCollector delete email ".$imapemail.
" ".$msgid);
4030 $operationslog .=
"<br> delete email ".$imapemail.
" ".$msgid.($mode > 0 ?
' (test)' :
'');
4032 if (empty($mode) && empty($error)) {
4033 $res = imap_mail_move($connection, $imapemail, $targetdir, CP_UID);
4036 $this->error = imap_last_error();
4037 $this->errors[] = $this->error;
4039 $operationslog .=
'<br>Error in move '.$this->error;
4046 if (empty($mode) && empty($error)) {
4048 $operationslog .=
"<br>Expunge";
4050 imap_expunge($connection);
4052 imap_close($connection);
4055 $this->datelastresult = $now;
4056 $this->lastresult = $output;
4058 $this->debuginfo .=
'IMAP search array used : '.$search;
4060 $this->debuginfo .=
'IMAP search string used : '.$search;
4063 $this->debuginfo .=
'<br>Then search string into email header : '.dol_escape_htmltag($searchhead);
4065 if ($operationslog) {
4066 $this->debuginfo .= $operationslog;
4069 if (empty($error) && empty($mode)) {
4070 $this->datelastok = $datelastok;
4073 if (!empty($this->errors)) {
4074 $this->lastresult .=
"<br>".implode(
"<br>", $this->errors);
4076 $this->codelastresult = ($error ?
'KO' :
'OK');
4082 dol_syslog(
"EmailCollector::doCollectOneCollector end error=".$error, LOG_INFO);
4084 return $error ? -1 : 1;
4099 private function getmsg($mbox, $mid, $destdir =
''): int
4103 global $charset, $htmlmsg, $plainmsg, $attachments;
4104 $htmlmsg = $plainmsg = $charset =
'';
4105 $attachments = array();
4112 $s = imap_fetchstructure($mbox, $mid, FT_UID);
4114 $this->errors = array_merge($this->errors, [imap_last_error()]);
4118 if (empty($s->parts)) {
4120 $this->
getpart($mbox, $mid, $s,
'0');
4123 foreach ($s->parts as $partno0 => $p) {
4124 $this->
getpart($mbox, $mid, $p, (
string) ($partno0 + 1), $destdir);
4157 private function getpart($mbox, $mid, $p, $partno, $destdir =
'')
4160 global $htmlmsg, $plainmsg, $charset, $attachments;
4164 imap_fetchbody($mbox, $mid, $partno, FT_UID) :
4165 imap_body($mbox, $mid, FT_UID);
4167 if ($p->encoding == 4) {
4168 $data = quoted_printable_decode($data);
4169 } elseif ($p->encoding == 3) {
4170 $data = base64_decode($data);
4176 if ($p->parameters) {
4177 foreach ($p->parameters as $x) {
4178 $params[strtolower($x->attribute)] = $x->value;
4181 if (!empty($p->dparameters)) {
4182 foreach ($p->dparameters as $x) {
4183 $params[strtolower($x->attribute)] = $x->value;
4186 '@phan-var-force array{filename?:string,name?:string,charset?:string} $params';
4191 if (!empty($params[
'filename']) || !empty($params[
'name'])) {
4193 $filename = $params[
'filename'] ?? $params[
'name'];
4195 $attachments[$filename] = $data;
4197 if (strlen($destdir)) {
4198 if (substr($destdir, -1) !=
'/') {
4203 $file_name_complete = $filename;
4204 $destination = $destdir.$file_name_complete;
4207 $extension = pathinfo($file_name_complete, PATHINFO_EXTENSION);
4210 $file_name = pathinfo($file_name_complete, PATHINFO_FILENAME);
4213 $file_name_original = $file_name;
4222 while (file_exists($destdir.$file_name.
".".$extension)) {
4223 $file_name = $file_name_original .
' (' . $num .
')';
4224 $file_name_complete = $file_name .
"." . $extension;
4225 $destination = $destdir.$file_name_complete;
4231 file_put_contents($destination, $data);
4237 if ($p->type == 0 && $data) {
4238 if (!empty($params[
'charset'])) {
4243 if (strtolower($p->subtype) ==
'plain') {
4244 $plainmsg .= trim($data).
"\n\n";
4246 $htmlmsg .= $data.
"<br><br>";
4248 $charset = $params[
'charset'];
4249 } elseif ($p->type == 2 && $data) {
4255 if (!empty($params[
'charset'])) {
4258 $plainmsg .= $data.
"\n\n";
4262 if (!empty($p->parts)) {
4263 foreach ($p->parts as $partno0 => $p2) {
4264 $this->
getpart($mbox, $mid, $p2, $partno.
'.'.($partno0 + 1), $destdir);
4280 if (!$string || $fromEncoding == $toEncoding) {
4283 $convertedString = function_exists(
'iconv') ? @iconv($fromEncoding, $toEncoding.
'//IGNORE', $string) :
null;
4284 if (!$convertedString && extension_loaded(
'mbstring')) {
4285 $convertedString = @mb_convert_encoding($string, $toEncoding, $fromEncoding);
4287 if (!$convertedString) {
4288 throw new Exception(
'Mime string encoding conversion failed');
4290 return $convertedString;
4309 if (function_exists(
'imap_mime_header_decode') && function_exists(
'iconv_mime_decode')) {
4310 $elements = imap_mime_header_decode($subject);
4312 if (!empty($elements)) {
4313 $num = count($elements);
4314 for ($i = 0; $i < $num; $i++) {
4315 $stringinutf8 = (in_array(strtoupper($elements[$i]->charset), array(
'DEFAULT',
'UTF-8')) ? $elements[$i]->text : iconv_mime_decode($elements[$i]->text, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, $elements[$i]->charset));
4316 $newstring .= $stringinutf8;
4318 $subject = $newstring;
4320 } elseif (!function_exists(
'mb_decode_mimeheader')) {
4321 $subject = mb_decode_mimeheader($subject);
4322 } elseif (function_exists(
'iconv_mime_decode')) {
4323 $subject = iconv_mime_decode($subject, ICONV_MIME_DECODE_CONTINUE_ON_ERROR,
'UTF-8');
4326 return (
string) $subject;
4336 private function saveEmailCollectorAttachmentsToDir($destdir, $attachments)
4338 $destdir = rtrim((
string) $destdir,
'/');
4339 if ($destdir ===
'' || empty($attachments) || !is_array($attachments)) {
4350 $seenHashes = array();
4353 $filelist =
dol_dir_list($destdir,
'files', 0,
'',
'(\\.meta|_preview.*.*\\.png)$',
'date', SORT_DESC, 0, 1);
4354 foreach ($filelist as $fileinfo) {
4355 if (empty($fileinfo[
'fullname']) || !is_file($fileinfo[
'fullname']) || !is_readable($fileinfo[
'fullname'])) {
4358 $hash = hash_file(
'sha256', $fileinfo[
'fullname']);
4359 if ($hash !==
false && empty($seenHashes[$hash])) {
4360 $seenHashes[$hash] = $fileinfo[
'name'];
4366 if (defined(
'DOL_DATA_ROOT')) {
4367 $root = rtrim((
string) DOL_DATA_ROOT,
'/').
'/';
4368 $destdirwithslash = $destdir.
'/';
4369 if (strpos($destdirwithslash, $root) === 0) {
4370 $relativeDir = rtrim(substr($destdirwithslash, strlen($root)),
'/');
4375 foreach ($attachments as $key => $attachment) {
4382 if (is_object($attachment)) {
4383 '@phan-var-force Webklex\PHPIMAP\Attachment $attachment';
4386 $origName = (
string) $attachment->getName();
4387 }
catch (Throwable $e) {
4391 $content = (
string) $attachment->getContent();
4392 }
catch (Throwable $e) {
4396 $tmpmime = (
string) $attachment->getContentType();
4397 $mime = ($tmpmime !==
'' ? $tmpmime :
null);
4398 }
catch (Throwable $e) {
4403 $origName = (
string) $key;
4404 $content = (
string) $attachment;
4407 if ($origName ===
'' || $origName ===
'undefined') {
4408 $origName =
'attachment-'.$index;
4410 if ($content ===
'') {
4415 if ($safeName ===
'') {
4416 $safeName =
'attachment-'.$index;
4419 $contentHash = hash(
'sha256', $content);
4420 $finalName = $safeName;
4421 $useExisting =
false;
4422 if (!empty($seenHashes[$contentHash])) {
4423 $finalName = $seenHashes[$contentHash];
4424 if (file_exists($destdir.
'/'.$finalName)) {
4425 $useExisting =
true;
4429 if (!$useExisting) {
4431 while (file_exists($destdir.
'/'.$finalName)) {
4432 $finalName = preg_replace(
'/(\\.[A-Za-z0-9]{1,10})$/',
'', $safeName).
'-'.$n;
4433 if (preg_match(
'/\\.[A-Za-z0-9]{1,10}$/', $safeName, $m)) {
4434 $finalName .= $m[0];
4440 $seenHashes[$contentHash] = $finalName;
4443 $fullPath = $destdir.
'/'.$finalName;
4444 $size = (file_exists($fullPath) ? filesize($fullPath) : null);
4445 $sizevalue = ($size ===
false ? null : $size);
4446 $sha256 = $contentHash;
4449 'name' => $finalName,
4450 'original_name' => $origName,
4451 'relative_path' => ($relativeDir !==
'' ? $relativeDir.
'/'.$finalName :
''),
4452 'content_type' => $mime,
4453 'size' => ($sizevalue !== null ? (int) $sizevalue : null),
4454 'sha256' => $sha256,
4471 require_once DOL_DOCUMENT_ROOT .
'/core/lib/images.lib.php';
4474 $maxwidthsmall = $tmparraysize[
'maxwidthsmall'];
4475 $maxheightsmall = $tmparraysize[
'maxheightsmall'];
4476 $maxwidthmini = $tmparraysize[
'maxwidthmini'];
4477 $maxheightmini = $tmparraysize[
'maxheightmini'];
4478 $quality = $tmparraysize[
'quality'];
4480 file_put_contents($destdir.
'/'.$filename, $content);
4485 vignette($destdir.
'/'.$filename, $maxwidthsmall, $maxheightsmall,
'_small', $quality,
"thumbs");
4487 vignette($destdir.
'/'.$filename, $maxwidthmini, $maxheightmini,
'_mini', $quality,
"thumbs");
4501 if (is_object($imapemail)) {
4502 return $imapemail->getAttributes()[
"uid"];
4504 return (
string) $imapemail;