| Conditions | 496 |
| Total Lines | 2517 |
| Code Lines | 1584 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 768 | public function doCollectOneCollector($mode = 0) |
||
| 769 | { |
||
| 770 | global $db, $conf, $langs, $user; |
||
| 771 | global $hookmanager; |
||
| 772 | |||
| 773 | //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log'; |
||
| 774 | |||
| 775 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 776 | require_once constant('DOL_DOCUMENT_ROOT') . '/includes/webklex/php-imap/vendor/autoload.php'; |
||
| 777 | } |
||
| 778 | |||
| 779 | dol_syslog("EmailCollector::doCollectOneCollector start for id=" . $this->id . " - " . $this->ref, LOG_INFO); |
||
| 780 | |||
| 781 | $langs->loadLangs(array("project", "companies", "mails", "errors", "ticket", "agenda", "commercial")); |
||
| 782 | |||
| 783 | $error = 0; |
||
| 784 | $this->output = ''; |
||
| 785 | $this->error = ''; |
||
| 786 | $this->debuginfo = ''; |
||
| 787 | |||
| 788 | $search = ''; |
||
| 789 | $searchhead = ''; |
||
| 790 | $searchfilterdoltrackid = 0; |
||
| 791 | $searchfilternodoltrackid = 0; |
||
| 792 | $searchfilterisanswer = 0; |
||
| 793 | $searchfilterisnotanswer = 0; |
||
| 794 | $searchfilterreplyto = 0; |
||
| 795 | $searchfilterexcludebodyarray = array(); |
||
| 796 | $searchfilterexcludesubjectarray = array(); |
||
| 797 | $operationslog = ''; |
||
| 798 | $rulesreplyto = array(); |
||
| 799 | |||
| 800 | $now = dol_now(); |
||
| 801 | |||
| 802 | if (empty($this->host)) { |
||
| 803 | $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMailHost')); |
||
| 804 | return -1; |
||
| 805 | } |
||
| 806 | if (empty($this->login)) { |
||
| 807 | $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Login')); |
||
| 808 | return -1; |
||
| 809 | } |
||
| 810 | if (empty($this->source_directory)) { |
||
| 811 | $this->error = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('MailboxSourceDirectory')); |
||
| 812 | return -1; |
||
| 813 | } |
||
| 814 | |||
| 815 | $sourcedir = $this->source_directory; |
||
| 816 | $targetdir = ($this->target_directory ? $this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' |
||
| 817 | |||
| 818 | $this->fetchFilters(); |
||
| 819 | $this->fetchActions(); |
||
| 820 | |||
| 821 | $sourcedir = $this->source_directory; |
||
| 822 | $targetdir = ($this->target_directory ? $this->target_directory : ''); // Can be '[Gmail]/Trash' or 'mytag' |
||
| 823 | |||
| 824 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 825 | if ($this->acces_type == 1) { |
||
| 826 | // Mode OAUth2 (access_type == 1) with PHP-IMAP |
||
| 827 | $this->debuginfo .= 'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=1, access_type=1 (OAUTH2)<br>'; |
||
| 828 | |||
| 829 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/oauth.lib.php'; |
||
| 830 | |||
| 831 | $supportedoauth2array = getSupportedOauth2Array(); |
||
| 832 | |||
| 833 | $keyforsupportedoauth2array = $this->oauth_service; |
||
| 834 | if (preg_match('/^.*-/', $keyforsupportedoauth2array)) { |
||
| 835 | $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array); |
||
| 836 | } else { |
||
| 837 | $keyforprovider = ''; |
||
| 838 | } |
||
| 839 | $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array); |
||
| 840 | $keyforsupportedoauth2array = 'OAUTH_' . $keyforsupportedoauth2array . '_NAME'; |
||
| 841 | |||
| 842 | $OAUTH_SERVICENAME = 'Unknown'; |
||
| 843 | if ( |
||
| 844 | array_key_exists($keyforsupportedoauth2array, $supportedoauth2array) |
||
| 845 | && array_key_exists('name', $supportedoauth2array[$keyforsupportedoauth2array]) |
||
| 846 | && !empty($supportedoauth2array[$keyforsupportedoauth2array]['name']) |
||
| 847 | ) { |
||
| 848 | $OAUTH_SERVICENAME = $supportedoauth2array[$keyforsupportedoauth2array]['name'] . (!empty($keyforprovider) ? '-' . $keyforprovider : ''); |
||
| 849 | } |
||
| 850 | |||
| 851 | require_once constant('DOL_DOCUMENT_ROOT') . '/includes/OAuth/bootstrap.php'; |
||
| 852 | //$debugtext = "Host: ".$this->host."<br>Port: ".$this->port."<br>Login: ".$this->login."<br>Password: ".$this->password."<br>access type: ".$this->acces_type."<br>oauth service: ".$this->oauth_service."<br>Max email per collect: ".$this->maxemailpercollect; |
||
| 853 | //dol_syslog($debugtext); |
||
| 854 | |||
| 855 | $token = ''; |
||
| 856 | |||
| 857 | $storage = new DoliStorage($db, $conf, $keyforprovider); |
||
| 858 | |||
| 859 | try { |
||
| 860 | $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); |
||
| 861 | |||
| 862 | $expire = true; |
||
| 863 | // TODO |
||
| 864 | // Is token expired or will token expire in the next 30 seconds |
||
| 865 | // if (is_object($tokenobj)) { |
||
| 866 | // $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30)); |
||
| 867 | // } |
||
| 868 | // Token expired so we refresh it |
||
| 869 | if (is_object($tokenobj) && $expire) { |
||
| 870 | $this->debuginfo .= 'Refresh token<br>'; |
||
| 871 | $credentials = new Credentials( |
||
| 872 | getDolGlobalString('OAUTH_' . $this->oauth_service . '_ID'), |
||
| 873 | getDolGlobalString('OAUTH_' . $this->oauth_service . '_SECRET'), |
||
| 874 | getDolGlobalString('OAUTH_' . $this->oauth_service . '_URLAUTHORIZE') |
||
| 875 | ); |
||
| 876 | $serviceFactory = new \OAuth\ServiceFactory(); |
||
| 877 | $oauthname = explode('-', $OAUTH_SERVICENAME); |
||
| 878 | // ex service is Google-Emails we need only the first part Google |
||
| 879 | $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array()); |
||
| 880 | // We have to save the token because Google give it only once |
||
| 881 | $refreshtoken = $tokenobj->getRefreshToken(); |
||
| 882 | $tokenobj = $apiService->refreshAccessToken($tokenobj); |
||
| 883 | $tokenobj->setRefreshToken($refreshtoken); |
||
| 884 | $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj); |
||
| 885 | } |
||
| 886 | $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME); |
||
| 887 | if (is_object($tokenobj)) { |
||
| 888 | $token = $tokenobj->getAccessToken(); |
||
| 889 | } else { |
||
| 890 | $this->error = "Token not found"; |
||
| 891 | return -1; |
||
| 892 | } |
||
| 893 | } catch (Exception $e) { |
||
| 894 | // Return an error if token not found |
||
| 895 | $this->error = $e->getMessage(); |
||
| 896 | dol_syslog("CMailFile::sendfile: mail end error=" . $this->error, LOG_ERR); |
||
| 897 | return -1; |
||
| 898 | } |
||
| 899 | |||
| 900 | $cm = new ClientManager(); |
||
| 901 | $client = $cm->make([ |
||
| 902 | 'host' => $this->host, |
||
| 903 | 'port' => $this->port, |
||
| 904 | 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption : false, |
||
| 905 | 'validate_cert' => true, |
||
| 906 | 'protocol' => 'imap', |
||
| 907 | 'username' => $this->login, |
||
| 908 | 'password' => $token, |
||
| 909 | 'authentication' => "oauth", |
||
| 910 | ]); |
||
| 911 | } else { |
||
| 912 | // Mode LOGIN (login/pass) with PHP-IMAP |
||
| 913 | $this->debuginfo .= 'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=1, access_type=0 (LOGIN)<br>'; |
||
| 914 | |||
| 915 | $cm = new ClientManager(); |
||
| 916 | $client = $cm->make([ |
||
| 917 | 'host' => $this->host, |
||
| 918 | 'port' => $this->port, |
||
| 919 | 'encryption' => !empty($this->imap_encryption) ? $this->imap_encryption : false, |
||
| 920 | 'validate_cert' => true, |
||
| 921 | 'protocol' => 'imap', |
||
| 922 | 'username' => $this->login, |
||
| 923 | 'password' => $this->password, |
||
| 924 | 'authentication' => "login", |
||
| 925 | ]); |
||
| 926 | } |
||
| 927 | |||
| 928 | try { |
||
| 929 | $client->connect(); |
||
| 930 | } catch (ConnectionFailedException $e) { |
||
| 931 | $this->error = $e->getMessage(); |
||
| 932 | $this->errors[] = $this->error; |
||
| 933 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_ERR); |
||
| 934 | return -1; |
||
| 935 | } |
||
| 936 | |||
| 937 | $host = dol_getprefix('email'); |
||
| 938 | } else { |
||
| 939 | // Use native IMAP functions |
||
| 940 | $this->debuginfo .= 'doCollectOneCollector is using method MAIN_IMAP_USE_PHPIMAP=0 (native PHP imap, LOGIN)<br>'; |
||
| 941 | |||
| 942 | if (!function_exists('imap_open')) { |
||
| 943 | $this->error = 'IMAP function not enabled on your PHP'; |
||
| 944 | return -2; |
||
| 945 | } |
||
| 946 | |||
| 947 | $connectstringserver = $this->getConnectStringIMAP(); |
||
| 948 | if (!getDolGlobalString('MAIL_DISABLE_UTF7_ENCODE_OF_DIR')) { |
||
| 949 | $connectstringsource = $connectstringserver . $this->getEncodedUtf7($sourcedir); |
||
| 950 | $connectstringtarget = $connectstringserver . $this->getEncodedUtf7($targetdir); |
||
| 951 | } else { |
||
| 952 | $connectstringsource = $connectstringserver . $sourcedir; |
||
| 953 | $connectstringtarget = $connectstringserver . $targetdir; |
||
| 954 | } |
||
| 955 | |||
| 956 | $this->debuginfo .= 'connectstringsource = ' . $connectstringsource . ', $connectstringtarget=' . $connectstringtarget . '<br>'; |
||
| 957 | |||
| 958 | $connection = imap_open($connectstringsource, $this->login, $this->password); |
||
| 959 | if (!$connection) { |
||
| 960 | $this->error = 'Failed to open IMAP connection ' . $connectstringsource . ' ' . imap_last_error(); |
||
| 961 | return -3; |
||
| 962 | } |
||
| 963 | imap_errors(); // Clear stack of errors. |
||
| 964 | |||
| 965 | $host = dol_getprefix('email'); |
||
| 966 | //$host = '123456'; |
||
| 967 | |||
| 968 | // Define the IMAP search string |
||
| 969 | // See https://tools.ietf.org/html/rfc3501#section-6.4.4 for IMAPv4 (PHP not yet compatible) |
||
| 970 | // See https://tools.ietf.org/html/rfc1064 page 13 for IMAPv2 |
||
| 971 | //$search='ALL'; |
||
| 972 | } |
||
| 973 | |||
| 974 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 975 | // Use PHPIMAP external library |
||
| 976 | $criteria = array(array('UNDELETED')); // Seems not supported by some servers |
||
| 977 | foreach ($this->filters as $rule) { |
||
| 978 | if (empty($rule['status'])) { |
||
| 979 | continue; |
||
| 980 | } |
||
| 981 | |||
| 982 | $not = ''; |
||
| 983 | if (strpos($rule['rulevalue'], '!') === 0) { |
||
| 984 | // The value start with !, so we exclude the criteria |
||
| 985 | $not = 'NOT '; |
||
| 986 | // Then remove the ! from the string for next filters |
||
| 987 | $rule['rulevalue'] = substr($rule['rulevalue'], 1); |
||
| 988 | } |
||
| 989 | |||
| 990 | if ($rule['type'] == 'from') { |
||
| 991 | $tmprulevaluearray = explode('*', $rule['rulevalue']); |
||
| 992 | if (count($tmprulevaluearray) >= 2) { |
||
| 993 | foreach ($tmprulevaluearray as $tmprulevalue) { |
||
| 994 | array_push($criteria, array($not . "FROM" => $tmprulevalue)); |
||
| 995 | } |
||
| 996 | } else { |
||
| 997 | array_push($criteria, array($not . "FROM" => $rule['rulevalue'])); |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | if ($rule['type'] == 'to') { |
||
| 1001 | $tmprulevaluearray = explode('*', $rule['rulevalue']); |
||
| 1002 | if (count($tmprulevaluearray) >= 2) { |
||
| 1003 | foreach ($tmprulevaluearray as $tmprulevalue) { |
||
| 1004 | array_push($criteria, array($not . "TO" => $tmprulevalue)); |
||
| 1005 | } |
||
| 1006 | } else { |
||
| 1007 | array_push($criteria, array($not . "TO" => $rule['rulevalue'])); |
||
| 1008 | } |
||
| 1009 | } |
||
| 1010 | if ($rule['type'] == 'bcc') { |
||
| 1011 | array_push($criteria, array($not . "BCC" => $rule['rulevalue'])); |
||
| 1012 | } |
||
| 1013 | if ($rule['type'] == 'cc') { |
||
| 1014 | array_push($criteria, array($not . "CC" => $rule['rulevalue'])); |
||
| 1015 | } |
||
| 1016 | if ($rule['type'] == 'subject') { |
||
| 1017 | if (strpos($rule['rulevalue'], '!') === 0) { |
||
| 1018 | //array_push($criteria, array("NOT SUBJECT" => $rule['rulevalue'])); |
||
| 1019 | $searchfilterexcludesubjectarray[] = preg_replace('/^!/', '', $rule['rulevalue']); |
||
| 1020 | } else { |
||
| 1021 | array_push($criteria, array("SUBJECT" => $rule['rulevalue'])); |
||
| 1022 | } |
||
| 1023 | } |
||
| 1024 | if ($rule['type'] == 'body') { |
||
| 1025 | if (strpos($rule['rulevalue'], '!') === 0) { |
||
| 1026 | //array_push($criteria, array("NOT BODY" => $rule['rulevalue'])); |
||
| 1027 | $searchfilterexcludebodyarray[] = preg_replace('/^!/', '', $rule['rulevalue']); |
||
| 1028 | } else { |
||
| 1029 | array_push($criteria, array("BODY" => $rule['rulevalue'])); |
||
| 1030 | } |
||
| 1031 | } |
||
| 1032 | if ($rule['type'] == 'header') { |
||
| 1033 | array_push($criteria, array($not . "HEADER" => $rule['rulevalue'])); |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | /* seems not used */ |
||
| 1037 | /* |
||
| 1038 | if ($rule['type'] == 'notinsubject') { |
||
| 1039 | array_push($criteria, array($not."SUBJECT NOT" => $rule['rulevalue'])); |
||
| 1040 | } |
||
| 1041 | if ($rule['type'] == 'notinbody') { |
||
| 1042 | array_push($criteria, array($not."BODY NOT" => $rule['rulevalue'])); |
||
| 1043 | }*/ |
||
| 1044 | |||
| 1045 | if ($rule['type'] == 'seen') { |
||
| 1046 | array_push($criteria, array($not . "SEEN")); |
||
| 1047 | } |
||
| 1048 | if ($rule['type'] == 'unseen') { |
||
| 1049 | array_push($criteria, array($not . "UNSEEN")); |
||
| 1050 | } |
||
| 1051 | if ($rule['type'] == 'unanswered') { |
||
| 1052 | array_push($criteria, array($not . "UNANSWERED")); |
||
| 1053 | } |
||
| 1054 | if ($rule['type'] == 'answered') { |
||
| 1055 | array_push($criteria, array($not . "ANSWERED")); |
||
| 1056 | } |
||
| 1057 | if ($rule['type'] == 'smaller') { |
||
| 1058 | array_push($criteria, array($not . "SMALLER")); |
||
| 1059 | } |
||
| 1060 | if ($rule['type'] == 'larger') { |
||
| 1061 | array_push($criteria, array($not . "LARGER")); |
||
| 1062 | } |
||
| 1063 | |||
| 1064 | // Rules to filter after the search imap |
||
| 1065 | if ($rule['type'] == 'withtrackingidinmsgid') { |
||
| 1066 | $searchfilterdoltrackid++; |
||
| 1067 | $searchhead .= '/Message-ID.*@' . preg_quote($host, '/') . '/'; |
||
| 1068 | } |
||
| 1069 | if ($rule['type'] == 'withouttrackingidinmsgid') { |
||
| 1070 | $searchfilterdoltrackid++; |
||
| 1071 | $searchhead .= '/Message-ID.*@' . preg_quote($host, '/') . '/'; |
||
| 1072 | } |
||
| 1073 | if ($rule['type'] == 'withtrackingid') { |
||
| 1074 | $searchfilterdoltrackid++; |
||
| 1075 | $searchhead .= '/References.*@' . preg_quote($host, '/') . '/'; |
||
| 1076 | } |
||
| 1077 | if ($rule['type'] == 'withouttrackingid') { |
||
| 1078 | $searchfilternodoltrackid++; |
||
| 1079 | $searchhead .= '! /References.*@' . preg_quote($host, '/') . '/'; |
||
| 1080 | } |
||
| 1081 | |||
| 1082 | if ($rule['type'] == 'isanswer') { |
||
| 1083 | $searchfilterisanswer++; |
||
| 1084 | $searchhead .= '/References.*@.*/'; |
||
| 1085 | } |
||
| 1086 | if ($rule['type'] == 'isnotanswer') { |
||
| 1087 | $searchfilterisnotanswer++; |
||
| 1088 | $searchhead .= '! /References.*@.*/'; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | if ($rule['type'] == 'replyto') { |
||
| 1092 | $searchfilterreplyto++; |
||
| 1093 | $rulesreplyto[] = $rule['rulevalue']; |
||
| 1094 | $searchhead .= '/Reply-To.*' . preg_quote($rule['rulevalue'], '/') . '/'; |
||
| 1095 | } |
||
| 1096 | } |
||
| 1097 | |||
| 1098 | if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined. |
||
| 1099 | $fromdate = 0; |
||
| 1100 | if ($this->datelastok) { |
||
| 1101 | $fromdate = $this->datelastok; |
||
| 1102 | } |
||
| 1103 | if ($fromdate > 0) { |
||
| 1104 | // $search .= ($search ? ' ' : '').'SINCE '.date('j-M-Y', $fromdate - 1); // SENTSINCE not supported. Date must be X-Abc-9999 (X on 1 digit if < 10) |
||
| 1105 | array_push($criteria, array("SINCE" => date('j-M-Y', $fromdate - 1))); |
||
| 1106 | } |
||
| 1107 | //$search.=($search?' ':'').'SINCE 8-Apr-2022'; |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | dol_syslog("IMAP search string = " . var_export($criteria, true)); |
||
| 1111 | $search = var_export($criteria, true); |
||
| 1112 | } else { |
||
| 1113 | // Use native IMAP functions |
||
| 1114 | $search = 'UNDELETED'; // Seems not supported by some servers |
||
| 1115 | foreach ($this->filters as $rule) { |
||
| 1116 | if (empty($rule['status'])) { |
||
| 1117 | continue; |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | // Forge the IMAP search string. |
||
| 1121 | // See https://www.rfc-editor.org/rfc/rfc3501 |
||
| 1122 | |||
| 1123 | $not = ''; |
||
| 1124 | if (!empty($rule['rulevalue']) && strpos($rule['rulevalue'], '!') === 0) { |
||
| 1125 | // The value start with !, so we exclude the criteria |
||
| 1126 | $not = 'NOT '; |
||
| 1127 | // Then remove the ! from the string for next filters |
||
| 1128 | $rule['rulevalue'] = substr($rule['rulevalue'], 1); |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | if ($rule['type'] == 'from') { |
||
| 1132 | $tmprulevaluearray = explode('*', $rule['rulevalue']); // Search on abc*def means searching on 'abc' and on 'def' |
||
| 1133 | if (count($tmprulevaluearray) >= 2) { |
||
| 1134 | foreach ($tmprulevaluearray as $tmprulevalue) { |
||
| 1135 | $search .= ($search ? ' ' : '') . $not . 'FROM "' . str_replace('"', '', $tmprulevalue) . '"'; |
||
| 1136 | } |
||
| 1137 | } else { |
||
| 1138 | $search .= ($search ? ' ' : '') . $not . 'FROM "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1139 | } |
||
| 1140 | } |
||
| 1141 | if ($rule['type'] == 'to') { |
||
| 1142 | $tmprulevaluearray = explode('*', $rule['rulevalue']); // Search on abc*def means searching on 'abc' and on 'def' |
||
| 1143 | if (count($tmprulevaluearray) >= 2) { |
||
| 1144 | foreach ($tmprulevaluearray as $tmprulevalue) { |
||
| 1145 | $search .= ($search ? ' ' : '') . $not . 'TO "' . str_replace('"', '', $tmprulevalue) . '"'; |
||
| 1146 | } |
||
| 1147 | } else { |
||
| 1148 | $search .= ($search ? ' ' : '') . $not . 'TO "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1149 | } |
||
| 1150 | } |
||
| 1151 | if ($rule['type'] == 'bcc') { |
||
| 1152 | $search .= ($search ? ' ' : '') . $not . 'BCC'; |
||
| 1153 | } |
||
| 1154 | if ($rule['type'] == 'cc') { |
||
| 1155 | $search .= ($search ? ' ' : '') . $not . 'CC'; |
||
| 1156 | } |
||
| 1157 | if ($rule['type'] == 'subject') { |
||
| 1158 | if ($not) { |
||
| 1159 | //$search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 1160 | $searchfilterexcludesubjectarray[] = $rule['rulevalue']; |
||
| 1161 | } else { |
||
| 1162 | $search .= ($search ? ' ' : '') . 'SUBJECT "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1163 | } |
||
| 1164 | } |
||
| 1165 | if ($rule['type'] == 'body') { |
||
| 1166 | if ($not) { |
||
| 1167 | //$search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 1168 | $searchfilterexcludebodyarray[] = $rule['rulevalue']; |
||
| 1169 | } else { |
||
| 1170 | // Warning: Google doesn't implement IMAP properly, and only matches whole words, |
||
| 1171 | $search .= ($search ? ' ' : '') . 'BODY "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1172 | } |
||
| 1173 | } |
||
| 1174 | if ($rule['type'] == 'header') { |
||
| 1175 | $search .= ($search ? ' ' : '') . $not . 'HEADER ' . $rule['rulevalue']; |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | /* seems not used */ |
||
| 1179 | /* |
||
| 1180 | if ($rule['type'] == 'notinsubject') { |
||
| 1181 | $search .= ($search ? ' ' : '').'NOT SUBJECT "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 1182 | } |
||
| 1183 | if ($rule['type'] == 'notinbody') { |
||
| 1184 | $search .= ($search ? ' ' : '').'NOT BODY "'.str_replace('"', '', $rule['rulevalue']).'"'; |
||
| 1185 | }*/ |
||
| 1186 | |||
| 1187 | if ($rule['type'] == 'seen') { |
||
| 1188 | $search .= ($search ? ' ' : '') . $not . 'SEEN'; |
||
| 1189 | } |
||
| 1190 | if ($rule['type'] == 'unseen') { |
||
| 1191 | $search .= ($search ? ' ' : '') . $not . 'UNSEEN'; |
||
| 1192 | } |
||
| 1193 | if ($rule['type'] == 'unanswered') { |
||
| 1194 | $search .= ($search ? ' ' : '') . $not . 'UNANSWERED'; |
||
| 1195 | } |
||
| 1196 | if ($rule['type'] == 'answered') { |
||
| 1197 | $search .= ($search ? ' ' : '') . $not . 'ANSWERED'; |
||
| 1198 | } |
||
| 1199 | if ($rule['type'] == 'smaller') { |
||
| 1200 | $search .= ($search ? ' ' : '') . $not . 'SMALLER "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1201 | } |
||
| 1202 | if ($rule['type'] == 'larger') { |
||
| 1203 | $search .= ($search ? ' ' : '') . $not . 'LARGER "' . str_replace('"', '', $rule['rulevalue']) . '"'; |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | // Rules to filter after the search imap |
||
| 1207 | if ($rule['type'] == 'withtrackingidinmsgid') { |
||
| 1208 | $searchfilterdoltrackid++; |
||
| 1209 | $searchhead .= '/Message-ID.*@' . preg_quote($host, '/') . '/'; |
||
| 1210 | } |
||
| 1211 | if ($rule['type'] == 'withouttrackingidinmsgid') { |
||
| 1212 | $searchfilterdoltrackid++; |
||
| 1213 | $searchhead .= '/Message-ID.*@' . preg_quote($host, '/') . '/'; |
||
| 1214 | } |
||
| 1215 | if ($rule['type'] == 'withtrackingid') { |
||
| 1216 | $searchfilterdoltrackid++; |
||
| 1217 | $searchhead .= '/References.*@' . preg_quote($host, '/') . '/'; |
||
| 1218 | } |
||
| 1219 | if ($rule['type'] == 'withouttrackingid') { |
||
| 1220 | $searchfilternodoltrackid++; |
||
| 1221 | $searchhead .= '! /References.*@' . preg_quote($host, '/') . '/'; |
||
| 1222 | } |
||
| 1223 | |||
| 1224 | if ($rule['type'] == 'isanswer') { |
||
| 1225 | $searchfilterisanswer++; |
||
| 1226 | $searchhead .= '/References.*@.*/'; |
||
| 1227 | } |
||
| 1228 | if ($rule['type'] == 'isnotanswer') { |
||
| 1229 | $searchfilterisnotanswer++; |
||
| 1230 | $searchhead .= '! /References.*@.*/'; |
||
| 1231 | } |
||
| 1232 | |||
| 1233 | if ($rule['type'] == 'replyto') { |
||
| 1234 | $searchfilterreplyto++; |
||
| 1235 | $rulesreplyto[] = $rule['rulevalue']; |
||
| 1236 | $searchhead .= '/Reply-To.*' . preg_quote($rule['rulevalue'], '/') . '/'; |
||
| 1237 | } |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | if (empty($targetdir)) { // Use last date as filter if there is no targetdir defined. |
||
| 1241 | $fromdate = 0; |
||
| 1242 | if ($this->datelastok) { |
||
| 1243 | $fromdate = $this->datelastok; |
||
| 1244 | } |
||
| 1245 | if ($fromdate > 0) { |
||
| 1246 | $search .= ($search ? ' ' : '') . 'SINCE ' . date('j-M-Y', $fromdate - 1); // SENTSINCE not supported. Date must be X-Abc-9999 (X on 1 digit if < 10) |
||
| 1247 | } |
||
| 1248 | //$search.=($search?' ':'').'SINCE 8-Apr-2018'; |
||
| 1249 | } |
||
| 1250 | |||
| 1251 | dol_syslog("IMAP search string = " . $search); |
||
| 1252 | //var_dump($search); |
||
| 1253 | } |
||
| 1254 | |||
| 1255 | $nbemailprocessed = 0; |
||
| 1256 | $nbemailok = 0; |
||
| 1257 | $nbactiondone = 0; |
||
| 1258 | $charset = ($this->hostcharset ? $this->hostcharset : "UTF-8"); |
||
| 1259 | |||
| 1260 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1261 | try { |
||
| 1262 | // Uncomment this to output debug info |
||
| 1263 | //$client->getConnection()->enableDebug(); |
||
| 1264 | |||
| 1265 | $tmpsourcedir = $sourcedir; |
||
| 1266 | if (!getDolGlobalString('MAIL_DISABLE_UTF7_ENCODE_OF_DIR')) { |
||
| 1267 | $tmpsourcedir = $this->getEncodedUtf7($sourcedir); |
||
| 1268 | } |
||
| 1269 | |||
| 1270 | $f = $client->getFolders(false, $tmpsourcedir); // Note the search of directory do a search on sourcedir* |
||
| 1271 | if ($f) { |
||
| 1272 | $folder = $f[0]; |
||
| 1273 | if ($folder instanceof Webklex\PHPIMAP\Folder) { |
||
| 1274 | $Query = $folder->messages()->where($criteria); |
||
| 1275 | } else { |
||
| 1276 | $error++; |
||
| 1277 | $this->error = "Source directory " . $sourcedir . " not found"; |
||
| 1278 | $this->errors[] = $this->error; |
||
| 1279 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_WARNING); |
||
| 1280 | return -1; |
||
| 1281 | } |
||
| 1282 | } else { |
||
| 1283 | $error++; |
||
| 1284 | $this->error = "Failed to execute getfolders"; |
||
| 1285 | $this->errors[] = $this->error; |
||
| 1286 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_ERR); |
||
| 1287 | return -1; |
||
| 1288 | } |
||
| 1289 | } catch (InvalidWhereQueryCriteriaException $e) { |
||
| 1290 | $this->error = $e->getMessage(); |
||
| 1291 | $this->errors[] = $this->error; |
||
| 1292 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_ERR); |
||
| 1293 | return -1; |
||
| 1294 | } catch (Exception $e) { |
||
| 1295 | $this->error = $e->getMessage(); |
||
| 1296 | $this->errors[] = $this->error; |
||
| 1297 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_ERR); |
||
| 1298 | return -1; |
||
| 1299 | } |
||
| 1300 | |||
| 1301 | try { |
||
| 1302 | //var_dump($Query->count()); |
||
| 1303 | if ($mode > 0) { |
||
| 1304 | $Query->leaveUnread(); |
||
| 1305 | } |
||
| 1306 | $arrayofemail = $Query->limit($this->maxemailpercollect)->setFetchOrder("asc")->get(); |
||
| 1307 | //var_dump($arrayofemail); |
||
| 1308 | } catch (Exception $e) { |
||
| 1309 | $this->error = $e->getMessage(); |
||
| 1310 | $this->errors[] = $this->error; |
||
| 1311 | dol_syslog("EmailCollector::doCollectOneCollector " . $this->error, LOG_ERR); |
||
| 1312 | return -1; |
||
| 1313 | } |
||
| 1314 | } else { |
||
| 1315 | // Scan IMAP dir (for native IMAP, the source dir is inside the $connection variable) |
||
| 1316 | $arrayofemail = imap_search($connection, $search, SE_UID, $charset); |
||
| 1317 | |||
| 1318 | if ($arrayofemail === false) { |
||
| 1319 | // Nothing found or search string not understood |
||
| 1320 | $mapoferrrors = imap_errors(); |
||
| 1321 | if ($mapoferrrors !== false) { |
||
| 1322 | $error++; |
||
| 1323 | $this->error = "Search string not understood - " . implode(',', $mapoferrrors); |
||
| 1324 | $this->errors[] = $this->error; |
||
| 1325 | } |
||
| 1326 | } |
||
| 1327 | } |
||
| 1328 | |||
| 1329 | $arrayofemailtodelete = array(); // Track email to delete to make the deletion at end. |
||
| 1330 | |||
| 1331 | // Loop on each email found |
||
| 1332 | if (!$error && !empty($arrayofemail) && count($arrayofemail) > 0) { |
||
| 1333 | // Loop to get part html and plain |
||
| 1334 | /* |
||
| 1335 | 0 multipart/mixed |
||
| 1336 | 1 multipart/alternative |
||
| 1337 | 1.1 text/plain |
||
| 1338 | 1.2 text/html |
||
| 1339 | 2 message/rfc822 |
||
| 1340 | 2 multipart/mixed |
||
| 1341 | 2.1 multipart/alternative |
||
| 1342 | 2.1.1 text/plain |
||
| 1343 | 2.1.2 text/html |
||
| 1344 | 2.2 message/rfc822 |
||
| 1345 | 2.2 multipart/alternative |
||
| 1346 | 2.2.1 text/plain |
||
| 1347 | 2.2.2 text/html |
||
| 1348 | */ |
||
| 1349 | dol_syslog("Start of loop on email", LOG_INFO, 1); |
||
| 1350 | |||
| 1351 | $iforemailloop = 0; |
||
| 1352 | foreach ($arrayofemail as $imapemail) { |
||
| 1353 | if ($nbemailprocessed > 1000) { |
||
| 1354 | break; // Do not process more than 1000 email per launch (this is a different protection than maxnbcollectedpercollect) |
||
| 1355 | } |
||
| 1356 | $iforemailloop++; |
||
| 1357 | |||
| 1358 | |||
| 1359 | // GET header and overview datas |
||
| 1360 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1361 | $header = $imapemail->getHeader()->raw; |
||
| 1362 | $overview = $imapemail->getAttributes(); |
||
| 1363 | } else { |
||
| 1364 | $header = imap_fetchheader($connection, $imapemail, FT_UID); |
||
| 1365 | $overview = imap_fetch_overview($connection, $imapemail, FT_UID); |
||
| 1366 | } |
||
| 1367 | |||
| 1368 | $header = preg_replace('/\r\n\s+/m', ' ', $header); // When a header line is on several lines, merge lines |
||
| 1369 | |||
| 1370 | $matches = array(); |
||
| 1371 | preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $header, $matches); |
||
| 1372 | $headers = array_combine($matches[1], $matches[2]); |
||
| 1373 | //var_dump($headers);exit; |
||
| 1374 | |||
| 1375 | if (!empty($headers['in-reply-to']) && empty($headers['In-Reply-To'])) { |
||
| 1376 | $headers['In-Reply-To'] = $headers['in-reply-to']; |
||
| 1377 | } |
||
| 1378 | if (!empty($headers['references']) && empty($headers['References'])) { |
||
| 1379 | $headers['References'] = $headers['references']; |
||
| 1380 | } |
||
| 1381 | if (!empty($headers['message-id']) && empty($headers['Message-ID'])) { |
||
| 1382 | $headers['Message-ID'] = $headers['message-id']; |
||
| 1383 | } |
||
| 1384 | if (!empty($headers['subject']) && empty($headers['Subject'])) { |
||
| 1385 | $headers['Subject'] = $headers['subject']; |
||
| 1386 | } |
||
| 1387 | |||
| 1388 | $headers['Subject'] = $this->decodeSMTPSubject($headers['Subject']); |
||
| 1389 | |||
| 1390 | $emailto = $this->decodeSMTPSubject($overview[0]->to); |
||
| 1391 | |||
| 1392 | $operationslog .= '<br>** Process email #' . dol_escape_htmltag($iforemailloop); |
||
| 1393 | |||
| 1394 | if (getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1395 | /** @var Webklex\PHPIMAP\Message $imapemail */ |
||
| 1396 | // $operationslog .= " - ".dol_escape_htmltag((string) $imapemail); |
||
| 1397 | $msgid = str_replace(array('<', '>'), '', $overview['message_id']); |
||
| 1398 | } else { |
||
| 1399 | $operationslog .= " - " . dol_escape_htmltag((string)$imapemail); |
||
| 1400 | $msgid = str_replace(array('<', '>'), '', $overview[0]->message_id); |
||
| 1401 | } |
||
| 1402 | $operationslog .= " - MsgId: " . $msgid . " - References: " . dol_escape_htmltag($headers['References'] ?? '') . " - Subject: " . dol_escape_htmltag($headers['Subject']); |
||
| 1403 | |||
| 1404 | dol_syslog("-- Process email " . $iforemailloop . " References: " . ($headers['References'] ?? '') . " Subject: " . $headers['Subject']); |
||
| 1405 | |||
| 1406 | |||
| 1407 | $trackidfoundintorecipienttype = ''; |
||
| 1408 | $trackidfoundintorecipientid = 0; |
||
| 1409 | $reg = array(); |
||
| 1410 | // See also later list of all supported tags... |
||
| 1411 | // Note: "th[i]" to avoid matching a codespell suggestion to convert to "this". |
||
| 1412 | // TODO Add host after the @'.preg_quote($host, '/') |
||
| 1413 | 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)) { |
||
| 1414 | $trackidfoundintorecipienttype = $reg[1]; |
||
| 1415 | $trackidfoundintorecipientid = $reg[2]; |
||
| 1416 | } elseif (preg_match('/\+emailing-(\w+)@/', $emailto, $reg)) { // Can be 'emailing-test' or 'emailing-IdMailing-IdRecipient' |
||
| 1417 | $trackidfoundintorecipienttype = 'emailing'; |
||
| 1418 | $trackidfoundintorecipientid = $reg[1]; |
||
| 1419 | } |
||
| 1420 | |||
| 1421 | $trackidfoundintomsgidtype = ''; |
||
| 1422 | $trackidfoundintomsgidid = 0; |
||
| 1423 | $reg = array(); |
||
| 1424 | // See also later list of all supported tags... |
||
| 1425 | // Note: "th[i]" to avoid matching a codespell suggestion to convert to "this". |
||
| 1426 | // TODO Add host after the @ |
||
| 1427 | 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)) { |
||
| 1428 | $trackidfoundintomsgidtype = $reg[1]; |
||
| 1429 | $trackidfoundintomsgidid = $reg[2]; |
||
| 1430 | } elseif (preg_match('/(?:[\+\-])emailing-(\w+)@/', $msgid, $reg)) { // Can be 'emailing-test' or 'emailing-IdMailing-IdRecipient' |
||
| 1431 | $trackidfoundintomsgidtype = 'emailing'; |
||
| 1432 | $trackidfoundintomsgidid = $reg[1]; |
||
| 1433 | } |
||
| 1434 | |||
| 1435 | // If there is an emailcollecter filter on trackid |
||
| 1436 | if ($searchfilterdoltrackid > 0) { |
||
| 1437 | if (empty($trackidfoundintorecipienttype) && empty($trackidfoundintomsgidtype)) { |
||
| 1438 | if (empty($headers['References']) || !preg_match('/@' . preg_quote($host, '/') . '/', $headers['References'])) { |
||
| 1439 | $nbemailprocessed++; |
||
| 1440 | dol_syslog(" Discarded - No suffix in email recipient and no Header References found matching the signature of the application, so with a trackid coming from the application"); |
||
| 1441 | continue; // Exclude email |
||
| 1442 | } |
||
| 1443 | } |
||
| 1444 | } |
||
| 1445 | if ($searchfilternodoltrackid > 0) { |
||
| 1446 | if (!empty($trackidfoundintorecipienttype) || !empty($trackidfoundintomsgidtype) || (!empty($headers['References']) && preg_match('/@' . preg_quote($host, '/') . '/', $headers['References']))) { |
||
| 1447 | $nbemailprocessed++; |
||
| 1448 | dol_syslog(" Discarded - Suffix found into email or Header References found and matching signature of application so with a trackid"); |
||
| 1449 | continue; // Exclude email |
||
| 1450 | } |
||
| 1451 | } |
||
| 1452 | |||
| 1453 | if ($searchfilterisanswer > 0) { |
||
| 1454 | if (empty($headers['In-Reply-To'])) { |
||
| 1455 | $nbemailprocessed++; |
||
| 1456 | dol_syslog(" Discarded - Email is not an answer (no In-Reply-To header)"); |
||
| 1457 | continue; // Exclude email |
||
| 1458 | } |
||
| 1459 | $isanswer = 0; |
||
| 1460 | if (preg_match('/^(Re|AW)\s*:\s+/i', $headers['Subject'])) { |
||
| 1461 | $isanswer = 1; |
||
| 1462 | } |
||
| 1463 | if (getDolGlobalString('EMAILCOLLECTOR_USE_IN_REPLY_TO_TO_DETECT_ANSWERS')) { |
||
| 1464 | // Note: "In-Reply-To" to detect if mail is an answer of another mail is not reliable because we can have: |
||
| 1465 | // Message-ID=A, In-Reply-To=B, References=B and message can BE an answer but may be NOT (for example a transfer of an email rewritten) |
||
| 1466 | if (!empty($headers['In-Reply-To'])) { |
||
| 1467 | $isanswer = 1; |
||
| 1468 | } |
||
| 1469 | } |
||
| 1470 | //if ($headers['In-Reply-To'] != $headers['Message-ID'] && empty($headers['References'])) $isanswer = 1; // If in-reply-to differs of message-id, this is a reply |
||
| 1471 | //if ($headers['In-Reply-To'] != $headers['Message-ID'] && !empty($headers['References']) && strpos($headers['References'], $headers['Message-ID']) !== false) $isanswer = 1; |
||
| 1472 | |||
| 1473 | if (!$isanswer) { |
||
| 1474 | $nbemailprocessed++; |
||
| 1475 | dol_syslog(" Discarded - Email is not an answer (no RE prefix in subject)"); |
||
| 1476 | continue; // Exclude email |
||
| 1477 | } |
||
| 1478 | } |
||
| 1479 | if ($searchfilterisnotanswer > 0) { |
||
| 1480 | if (!empty($headers['In-Reply-To'])) { |
||
| 1481 | // Note: we can have |
||
| 1482 | // Message-ID=A, In-Reply-To=B, References=B and message can BE an answer or NOT (a transfer rewritten) |
||
| 1483 | $isanswer = 0; |
||
| 1484 | if (preg_match('/Re\s*:\s+/i', $headers['Subject'])) { |
||
| 1485 | $isanswer = 1; |
||
| 1486 | } |
||
| 1487 | //if ($headers['In-Reply-To'] != $headers['Message-ID'] && empty($headers['References'])) $isanswer = 1; // If in-reply-to differs of message-id, this is a reply |
||
| 1488 | //if ($headers['In-Reply-To'] != $headers['Message-ID'] && !empty($headers['References']) && strpos($headers['References'], $headers['Message-ID']) !== false) $isanswer = 1; |
||
| 1489 | if ($isanswer) { |
||
| 1490 | $nbemailprocessed++; |
||
| 1491 | dol_syslog(" Discarded - Email is an answer"); |
||
| 1492 | continue; // Exclude email |
||
| 1493 | } |
||
| 1494 | } |
||
| 1495 | } |
||
| 1496 | if ($searchfilterreplyto > 0) { |
||
| 1497 | if (!empty($headers['Reply-To'])) { |
||
| 1498 | $isreplytook = 0; |
||
| 1499 | foreach ($rulesreplyto as $key => $rulereplyto) { |
||
| 1500 | if (preg_match('/' . preg_quote($rulereplyto, '/') . '/', $headers['Reply-To'])) { |
||
| 1501 | $isreplytook++; |
||
| 1502 | } |
||
| 1503 | } |
||
| 1504 | |||
| 1505 | if (!$isreplytook || $isreplytook != count($rulesreplyto)) { |
||
| 1506 | $nbemailprocessed++; |
||
| 1507 | dol_syslog(" Discarded - Reply-to does not match"); |
||
| 1508 | continue; // Exclude email |
||
| 1509 | } |
||
| 1510 | } |
||
| 1511 | } |
||
| 1512 | |||
| 1513 | //print "Process mail ".$iforemailloop." Subject: ".dol_escape_htmltag($headers['Subject'])." selected<br>\n"; |
||
| 1514 | |||
| 1515 | $thirdpartystatic = new Societe($this->db); |
||
| 1516 | $contactstatic = new Contact($this->db); |
||
| 1517 | $projectstatic = new Project($this->db); |
||
| 1518 | |||
| 1519 | $nbactiondoneforemail = 0; |
||
| 1520 | $errorforemail = 0; |
||
| 1521 | $errorforactions = 0; |
||
| 1522 | $thirdpartyfoundby = ''; |
||
| 1523 | $contactfoundby = ''; |
||
| 1524 | $projectfoundby = ''; |
||
| 1525 | $ticketfoundby = ''; |
||
| 1526 | $candidaturefoundby = ''; |
||
| 1527 | |||
| 1528 | |||
| 1529 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1530 | dol_syslog("msgid=" . $overview['message_id'] . " date=" . dol_print_date($overview['date'], 'dayrfc', 'gmt') . " from=" . $overview['from'] . " to=" . $overview['to'] . " subject=" . $overview['subject']); |
||
| 1531 | |||
| 1532 | // Removed emojis |
||
| 1533 | $overview['subject'] = Filters::removeEmoji($overview['subject'], getDolGlobalInt('MAIN_EMAIL_COLLECTOR_ACCEPT_EMOJIS', 1)); |
||
| 1534 | } else { |
||
| 1535 | 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); |
||
| 1536 | |||
| 1537 | $overview[0]->subject = $this->decodeSMTPSubject($overview[0]->subject); |
||
| 1538 | |||
| 1539 | $overview[0]->from = $this->decodeSMTPSubject($overview[0]->from); |
||
| 1540 | |||
| 1541 | // Removed emojis |
||
| 1542 | $overview[0]->subject = Filters::removeEmoji($overview[0]->subject, getDolGlobalInt('MAIN_EMAIL_COLLECTOR_ACCEPT_EMOJIS', 1)); |
||
| 1543 | } |
||
| 1544 | // GET IMAP email structure/content |
||
| 1545 | global $htmlmsg, $plainmsg, $charset, $attachments; |
||
| 1546 | |||
| 1547 | if (getDolGlobalInt('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1548 | /** @var Webklex\PHPIMAP\Message $imapemail */ |
||
| 1549 | if ($imapemail->hasHTMLBody()) { |
||
| 1550 | $htmlmsg = $imapemail->getHTMLBody(); |
||
| 1551 | } |
||
| 1552 | if ($imapemail->hasTextBody()) { |
||
| 1553 | $plainmsg = $imapemail->getTextBody(); |
||
| 1554 | } |
||
| 1555 | if ($imapemail->hasAttachments()) { |
||
| 1556 | $attachments = $imapemail->getAttachments()->all(); |
||
| 1557 | } else { |
||
| 1558 | $attachments = []; |
||
| 1559 | } |
||
| 1560 | } else { |
||
| 1561 | $this->getmsg($connection, $imapemail); // This set global var $charset, $htmlmsg, $plainmsg, $attachments |
||
| 1562 | } |
||
| 1563 | '@phan-var-force Webklex\PHPIMAP\Attachment[] $attachments'; |
||
| 1564 | |||
| 1565 | //print $plainmsg; |
||
| 1566 | //var_dump($plainmsg); exit; |
||
| 1567 | |||
| 1568 | //$htmlmsg,$plainmsg,$charset,$attachments |
||
| 1569 | $messagetext = $plainmsg ? $plainmsg : dol_string_nohtmltag($htmlmsg, 0); |
||
| 1570 | // Removed emojis |
||
| 1571 | |||
| 1572 | if (utf8_valid($messagetext)) { |
||
| 1573 | $messagetext = Filters::removeEmoji($messagetext, getDolGlobalInt('MAIN_EMAIL_COLLECTOR_ACCEPT_EMOJIS', 1)); |
||
| 1574 | } else { |
||
| 1575 | $operationslog .= '<br>Discarded - Email body is not valid utf8'; |
||
| 1576 | dol_syslog(" Discarded - Email body is not valid utf8"); |
||
| 1577 | continue; // Exclude email |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | if (!empty($searchfilterexcludebodyarray)) { |
||
| 1581 | foreach ($searchfilterexcludebodyarray as $searchfilterexcludebody) { |
||
| 1582 | if (preg_match('/' . preg_quote($searchfilterexcludebody, '/') . '/ms', $messagetext)) { |
||
| 1583 | $nbemailprocessed++; |
||
| 1584 | $operationslog .= '<br>Discarded - Email body contains string ' . $searchfilterexcludebody; |
||
| 1585 | dol_syslog(" Discarded - Email body contains string " . $searchfilterexcludebody); |
||
| 1586 | continue 2; // Exclude email |
||
| 1587 | } |
||
| 1588 | } |
||
| 1589 | } |
||
| 1590 | |||
| 1591 | //var_dump($plainmsg); |
||
| 1592 | //var_dump($htmlmsg); |
||
| 1593 | //var_dump($messagetext); |
||
| 1594 | //var_dump($charset); |
||
| 1595 | //var_dump($attachments); |
||
| 1596 | //exit; |
||
| 1597 | |||
| 1598 | // Parse IMAP email structure |
||
| 1599 | /* |
||
| 1600 | $structure = imap_fetchstructure($connection, $imapemail, FT_UID); |
||
| 1601 | |||
| 1602 | $partplain = $parthtml = -1; |
||
| 1603 | $encodingplain = $encodinghtml = ''; |
||
| 1604 | |||
| 1605 | $result = createPartArray($structure, ''); |
||
| 1606 | |||
| 1607 | foreach($result as $part) |
||
| 1608 | { |
||
| 1609 | // $part['part_object']->type seems 0 for content |
||
| 1610 | // $part['part_object']->type seems 5 for attachment |
||
| 1611 | if (empty($part['part_object'])) continue; |
||
| 1612 | if ($part['part_object']->subtype == 'HTML') |
||
| 1613 | { |
||
| 1614 | $parthtml=$part['part_number']; |
||
| 1615 | if ($part['part_object']->encoding == 4) |
||
| 1616 | { |
||
| 1617 | $encodinghtml = 'aaa'; |
||
| 1618 | } |
||
| 1619 | } |
||
| 1620 | if ($part['part_object']->subtype == 'PLAIN') |
||
| 1621 | { |
||
| 1622 | $partplain=$part['part_number']; |
||
| 1623 | if ($part['part_object']->encoding == 4) |
||
| 1624 | { |
||
| 1625 | $encodingplain = 'rr'; |
||
| 1626 | } |
||
| 1627 | } |
||
| 1628 | } |
||
| 1629 | //var_dump($result); |
||
| 1630 | //var_dump($partplain); |
||
| 1631 | //var_dump($parthtml); |
||
| 1632 | |||
| 1633 | //var_dump($structure); |
||
| 1634 | //var_dump($parthtml); |
||
| 1635 | //var_dump($partplain); |
||
| 1636 | |||
| 1637 | $messagetext = imap_fetchbody($connection, $imapemail, ($parthtml != '-1' ? $parthtml : ($partplain != '-1' ? $partplain : 1)), FT_PEEK|FTP_UID); |
||
| 1638 | */ |
||
| 1639 | |||
| 1640 | //var_dump($messagetext); |
||
| 1641 | //var_dump($structure->parts[0]->parts); |
||
| 1642 | //print $header; |
||
| 1643 | //print $messagetext; |
||
| 1644 | //exit; |
||
| 1645 | |||
| 1646 | $fromstring = ''; |
||
| 1647 | $replytostring = ''; |
||
| 1648 | |||
| 1649 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 1650 | $fromstring = $overview['from']; |
||
| 1651 | $replytostring = empty($overview['in_reply-to']) ? $headers['Reply-To'] : $overview['in_reply-to']; |
||
| 1652 | |||
| 1653 | $sender = $overview['sender']; |
||
| 1654 | $to = $overview['to']; |
||
| 1655 | $sendtocc = empty($overview['cc']) ? '' : $overview['cc']; |
||
| 1656 | $sendtobcc = empty($overview['bcc']) ? '' : $overview['bcc']; |
||
| 1657 | |||
| 1658 | $tmpdate = $overview['date']->toDate(); |
||
| 1659 | $tmptimezone = $tmpdate->getTimezone()->getName(); |
||
| 1660 | |||
| 1661 | $dateemail = dol_stringtotime((string)$overview['date'], 'gmt'); // if $overview['timezone'] is "+00:00" |
||
| 1662 | if (preg_match('/^([+\-])(\d\d):(\d\d)/', $tmptimezone, $reg)) { |
||
| 1663 | if ($reg[1] == '+' && ($reg[2] != '00' || $reg[3] != '00')) { |
||
| 1664 | $dateemail -= (3600 * (int)$reg[2]); |
||
| 1665 | $dateemail -= (60 * (int)$reg[3]); |
||
| 1666 | } |
||
| 1667 | if ($reg[1] == '-' && ($reg[2] != '00' || $reg[3] != '00')) { |
||
| 1668 | $dateemail += (3600 * (int)$reg[2]); |
||
| 1669 | $dateemail += (60 * (int)$reg[3]); |
||
| 1670 | } |
||
| 1671 | } |
||
| 1672 | $subject = $overview['subject']; |
||
| 1673 | } else { |
||
| 1674 | $fromstring = $overview[0]->from; |
||
| 1675 | $replytostring = (!empty($overview['in_reply-to']) ? $overview['in_reply-to'] : (!empty($headers['Reply-To']) ? $headers['Reply-To'] : "")); |
||
| 1676 | |||
| 1677 | $sender = !empty($overview[0]->sender) ? $overview[0]->sender : ''; |
||
| 1678 | $to = $overview[0]->to; |
||
| 1679 | $sendtocc = !empty($overview[0]->cc) ? $overview[0]->cc : ''; |
||
| 1680 | $sendtobcc = !empty($overview[0]->bcc) ? $overview[0]->bcc : ''; |
||
| 1681 | $dateemail = dol_stringtotime((string)$overview[0]->udate, 'gmt'); |
||
| 1682 | $subject = $overview[0]->subject; |
||
| 1683 | //var_dump($msgid);exit; |
||
| 1684 | } |
||
| 1685 | |||
| 1686 | if (!empty($searchfilterexcludesubjectarray)) { |
||
| 1687 | foreach ($searchfilterexcludesubjectarray as $searchfilterexcludesubject) { |
||
| 1688 | if (preg_match('/' . preg_quote($searchfilterexcludesubject, '/') . '/ms', $subject)) { |
||
| 1689 | $nbemailprocessed++; |
||
| 1690 | $operationslog .= '<br>Discarded - Email subject contains string ' . $searchfilterexcludesubject; |
||
| 1691 | dol_syslog(" Discarded - Email subject contains string " . $searchfilterexcludesubject); |
||
| 1692 | continue 2; // Exclude email |
||
| 1693 | } |
||
| 1694 | } |
||
| 1695 | } |
||
| 1696 | |||
| 1697 | $reg = array(); |
||
| 1698 | if (preg_match('/^(.*)<(.*)>$/', $fromstring, $reg)) { |
||
| 1699 | $from = $reg[2]; |
||
| 1700 | $fromtext = $reg[1]; |
||
| 1701 | } else { |
||
| 1702 | $from = $fromstring; |
||
| 1703 | $fromtext = ''; |
||
| 1704 | } |
||
| 1705 | if (preg_match('/^(.*)<(.*)>$/', $replytostring, $reg)) { |
||
| 1706 | $replyto = $reg[2]; |
||
| 1707 | $replytotext = $reg[1]; |
||
| 1708 | } else { |
||
| 1709 | $replyto = $replytostring; |
||
| 1710 | $replytotext = ''; |
||
| 1711 | } |
||
| 1712 | $fk_element_id = 0; |
||
| 1713 | $fk_element_type = ''; |
||
| 1714 | |||
| 1715 | |||
| 1716 | $this->db->begin(); |
||
| 1717 | |||
| 1718 | $contactid = 0; |
||
| 1719 | $thirdpartyid = 0; |
||
| 1720 | $projectid = 0; |
||
| 1721 | $ticketid = 0; |
||
| 1722 | |||
| 1723 | // Analyze TrackId in field References (already analyzed previously into the "To:" and "Message-Id"). |
||
| 1724 | // For example: |
||
| 1725 | // References: <1542377954.SMTPs-dolibarr-thi649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1726 | // References: <1542377954.SMTPs-dolibarr-tic649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1727 | // References: <1542377954.SMTPs-dolibarr-abc649@8f6014fde11ec6cdec9a822234fc557e> |
||
| 1728 | $trackid = ''; |
||
| 1729 | $objectid = 0; |
||
| 1730 | $objectemail = null; |
||
| 1731 | |||
| 1732 | $reg = array(); |
||
| 1733 | $arrayofreferences = array(); |
||
| 1734 | if (!empty($headers['References'])) { |
||
| 1735 | $arrayofreferences = preg_split('/(,|\s+)/', $headers['References']); |
||
| 1736 | } |
||
| 1737 | if (!in_array('<' . $msgid . '>', $arrayofreferences)) { |
||
| 1738 | $arrayofreferences = array_merge($arrayofreferences, array('<' . $msgid . '>')); |
||
| 1739 | } |
||
| 1740 | // var_dump($headers['References']); |
||
| 1741 | // var_dump($arrayofreferences); |
||
| 1742 | |||
| 1743 | foreach ($arrayofreferences as $reference) { |
||
| 1744 | //print "Process mail ".$iforemailloop." email_msgid ".$msgid.", date ".dol_print_date($dateemail, 'dayhour', 'gmt').", subject ".$subject.", reference ".dol_escape_htmltag($reference)."<br>\n"; |
||
| 1745 | if (!empty($trackidfoundintorecipienttype)) { |
||
| 1746 | $resultsearchtrackid = -1; // trackid found |
||
| 1747 | $reg[1] = $trackidfoundintorecipienttype; |
||
| 1748 | $reg[2] = $trackidfoundintorecipientid; |
||
| 1749 | } elseif (!empty($trackidfoundintomsgidtype)) { |
||
| 1750 | $resultsearchtrackid = -1; // trackid found |
||
| 1751 | $reg[1] = $trackidfoundintomsgidtype; |
||
| 1752 | $reg[2] = $trackidfoundintomsgidid; |
||
| 1753 | } else { |
||
| 1754 | $resultsearchtrackid = preg_match('/dolibarr-([a-z]+)([0-9]+)@' . preg_quote($host, '/') . '/', $reference, $reg); // trackid found or not |
||
| 1755 | if (empty($resultsearchtrackid) && getDolGlobalString('EMAIL_ALTERNATIVE_HOST_SIGNATURE')) { |
||
| 1756 | $resultsearchtrackid = preg_match('/dolibarr-([a-z]+)([0-9]+)@' . preg_quote(getDolGlobalString('EMAIL_ALTERNATIVE_HOST_SIGNATURE'), '/') . '/', $reference, $reg); // trackid found |
||
| 1757 | } |
||
| 1758 | } |
||
| 1759 | |||
| 1760 | if (!empty($resultsearchtrackid)) { |
||
| 1761 | // We found a tracker (in recipient email or msgid or into a Reference matching the Dolibarr server) |
||
| 1762 | $trackid = $reg[1] . $reg[2]; |
||
| 1763 | |||
| 1764 | $objectid = $reg[2]; |
||
| 1765 | // See also list into interface_50_modAgenda_ActionsAuto |
||
| 1766 | if ($reg[1] == 'thi') { // Third-party |
||
| 1767 | $objectemail = new Societe($this->db); |
||
| 1768 | } |
||
| 1769 | if ($reg[1] == 'ctc') { // Contact |
||
| 1770 | $objectemail = new Contact($this->db); |
||
| 1771 | } |
||
| 1772 | if ($reg[1] == 'inv') { // Customer Invoice |
||
| 1773 | $objectemail = new Facture($this->db); |
||
| 1774 | } |
||
| 1775 | if ($reg[1] == 'sinv') { // Supplier Invoice |
||
| 1776 | $objectemail = new FactureFournisseur($this->db); |
||
| 1777 | } |
||
| 1778 | if ($reg[1] == 'pro') { // Customer Proposal |
||
| 1779 | $objectemail = new Propal($this->db); |
||
| 1780 | } |
||
| 1781 | if ($reg[1] == 'ord') { // Sale Order |
||
| 1782 | $objectemail = new Commande($this->db); |
||
| 1783 | } |
||
| 1784 | if ($reg[1] == 'shi') { // Shipment |
||
| 1785 | $objectemail = new Expedition($this->db); |
||
| 1786 | } |
||
| 1787 | if ($reg[1] == 'spro') { // Supplier Proposal |
||
| 1788 | $objectemail = new SupplierProposal($this->db); |
||
| 1789 | } |
||
| 1790 | if ($reg[1] == 'sord') { // Supplier Order |
||
| 1791 | $objectemail = new CommandeFournisseur($this->db); |
||
| 1792 | } |
||
| 1793 | if ($reg[1] == 'rec') { // Reception |
||
| 1794 | $objectemail = new Reception($this->db); |
||
| 1795 | } |
||
| 1796 | if ($reg[1] == 'proj') { // Project |
||
| 1797 | $objectemail = new Project($this->db); |
||
| 1798 | $projectfoundby = 'TrackID dolibarr-' . $trackid . '@...'; |
||
| 1799 | } |
||
| 1800 | if ($reg[1] == 'tas') { // Task |
||
| 1801 | $objectemail = new Task($this->db); |
||
| 1802 | } |
||
| 1803 | if ($reg[1] == 'con') { // Contact |
||
| 1804 | $objectemail = new Contact($this->db); |
||
| 1805 | } |
||
| 1806 | if ($reg[1] == 'use') { // User |
||
| 1807 | $objectemail = new User($this->db); |
||
| 1808 | } |
||
| 1809 | if ($reg[1] == 'tic') { // Ticket |
||
| 1810 | $objectemail = new Ticket($this->db); |
||
| 1811 | $ticketfoundby = 'TrackID dolibarr-' . $trackid . '@...'; |
||
| 1812 | } |
||
| 1813 | if ($reg[1] == 'recruitmentcandidature') { // Recruiting Candidate |
||
| 1814 | $objectemail = new RecruitmentCandidature($this->db); |
||
| 1815 | $candidaturefoundby = 'TrackID dolibarr-' . $trackid . '@...'; |
||
| 1816 | } |
||
| 1817 | if ($reg[1] == 'mem') { // Member |
||
| 1818 | $objectemail = new Adherent($this->db); |
||
| 1819 | } |
||
| 1820 | /*if ($reg[1] == 'leav') { // Leave / Holiday |
||
| 1821 | $objectemail = new Holiday($db); |
||
| 1822 | } |
||
| 1823 | if ($reg[1] == 'exp') { // ExpenseReport |
||
| 1824 | $objectemail = new ExpenseReport($db); |
||
| 1825 | }*/ |
||
| 1826 | } elseif (preg_match('/<(.*@.*)>/', $reference, $reg)) { |
||
| 1827 | // This is an external reference, we check if we have it in our database |
||
| 1828 | if (is_null($objectemail) && isModEnabled('ticket')) { |
||
| 1829 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "ticket"; |
||
| 1830 | $sql .= " WHERE email_msgid = '" . $this->db->escape($reg[1]) . "' OR origin_references like '%" . $this->db->escape($this->db->escapeforlike($reg[1])) . "%'"; |
||
| 1831 | $resql = $this->db->query($sql); |
||
| 1832 | if ($resql) { |
||
| 1833 | $obj = $this->db->fetch_object($resql); |
||
| 1834 | if ($obj) { |
||
| 1835 | $objectid = $obj->rowid; |
||
| 1836 | $objectemail = new Ticket($this->db); |
||
| 1837 | $ticketfoundby = $langs->transnoentitiesnoconv("EmailMsgID") . ' (' . $reg[1] . ')'; |
||
| 1838 | } |
||
| 1839 | } else { |
||
| 1840 | $errorforemail++; |
||
| 1841 | } |
||
| 1842 | } |
||
| 1843 | |||
| 1844 | if (!is_object($objectemail) && isModEnabled('project')) { |
||
| 1845 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "projet where email_msgid = '" . $this->db->escape($reg[1]) . "'"; |
||
| 1846 | $resql = $this->db->query($sql); |
||
| 1847 | if ($resql) { |
||
| 1848 | $obj = $this->db->fetch_object($resql); |
||
| 1849 | if ($obj) { |
||
| 1850 | $objectid = $obj->rowid; |
||
| 1851 | $objectemail = new Project($this->db); |
||
| 1852 | $projectfoundby = $langs->transnoentitiesnoconv("EmailMsgID") . ' (' . $reg[1] . ')'; |
||
| 1853 | } |
||
| 1854 | } else { |
||
| 1855 | $errorforemail++; |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | |||
| 1859 | if (!is_object($objectemail) && isModEnabled('recruitment')) { |
||
| 1860 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "recruitment_recruitmentcandidature where email_msgid = '" . $this->db->escape($reg[1]) . "'"; |
||
| 1861 | $resql = $this->db->query($sql); |
||
| 1862 | if ($resql) { |
||
| 1863 | $obj = $this->db->fetch_object($resql); |
||
| 1864 | if ($obj) { |
||
| 1865 | $objectid = $obj->rowid; |
||
| 1866 | $objectemail = new RecruitmentCandidature($this->db); |
||
| 1867 | $candidaturefoundby = $langs->transnoentitiesnoconv("EmailMsgID") . ' (' . $reg[1] . ')'; |
||
| 1868 | } |
||
| 1869 | } else { |
||
| 1870 | $errorforemail++; |
||
| 1871 | } |
||
| 1872 | } |
||
| 1873 | } |
||
| 1874 | |||
| 1875 | // Load object linked to email |
||
| 1876 | if (is_object($objectemail)) { |
||
| 1877 | $result = $objectemail->fetch($objectid); |
||
| 1878 | if ($result > 0) { |
||
| 1879 | $fk_element_id = $objectemail->id; |
||
| 1880 | $fk_element_type = $objectemail->element; |
||
| 1881 | // Fix fk_element_type |
||
| 1882 | if ($fk_element_type == 'facture') { |
||
| 1883 | $fk_element_type = 'invoice'; |
||
| 1884 | } |
||
| 1885 | |||
| 1886 | if (get_only_class($objectemail) != 'Societe') { |
||
| 1887 | $thirdpartyid = $objectemail->fk_soc ?? $objectemail->socid; |
||
| 1888 | } else { |
||
| 1889 | $thirdpartyid = $objectemail->id; |
||
| 1890 | } |
||
| 1891 | |||
| 1892 | if (get_only_class($objectemail) != 'Contact') { |
||
| 1893 | $contactid = $objectemail->fk_socpeople; |
||
| 1894 | } else { |
||
| 1895 | $contactid = $objectemail->id; |
||
| 1896 | } |
||
| 1897 | |||
| 1898 | if (get_only_class($objectemail) != 'Project') { |
||
| 1899 | $projectid = isset($objectemail->fk_project) ? $objectemail->fk_project : $objectemail->fk_projet; |
||
| 1900 | } else { |
||
| 1901 | $projectid = $objectemail->id; |
||
| 1902 | } |
||
| 1903 | |||
| 1904 | if (get_only_class($objectemail) == 'Ticket') { |
||
| 1905 | $ticketid = $objectemail->id; |
||
| 1906 | |||
| 1907 | $changeonticket_references = false; |
||
| 1908 | if (empty($trackid)) { |
||
| 1909 | $trackid = $objectemail->track_id; |
||
| 1910 | } |
||
| 1911 | if (empty($objectemail->origin_references)) { |
||
| 1912 | $objectemail->origin_references = $headers['References']; |
||
| 1913 | $changeonticket_references = true; |
||
| 1914 | } else { |
||
| 1915 | foreach ($arrayofreferences as $key => $referencetmp) { |
||
| 1916 | if (!str_contains($objectemail->origin_references, $referencetmp)) { |
||
| 1917 | $objectemail->origin_references .= " " . $referencetmp; |
||
| 1918 | $changeonticket_references = true; |
||
| 1919 | } |
||
| 1920 | } |
||
| 1921 | } |
||
| 1922 | if ($changeonticket_references) { |
||
| 1923 | $objectemail->update($user); |
||
| 1924 | } |
||
| 1925 | } |
||
| 1926 | } |
||
| 1927 | } |
||
| 1928 | |||
| 1929 | // Project |
||
| 1930 | if ($projectid > 0) { |
||
| 1931 | $result = $projectstatic->fetch($projectid); |
||
| 1932 | if ($result <= 0) { |
||
| 1933 | $projectstatic->id = 0; |
||
| 1934 | } else { |
||
| 1935 | $projectid = $projectstatic->id; |
||
| 1936 | if ($trackid) { |
||
| 1937 | $projectfoundby = 'trackid (' . $trackid . ')'; |
||
| 1938 | } |
||
| 1939 | if (empty($contactid)) { |
||
| 1940 | $contactid = $projectstatic->fk_contact; |
||
| 1941 | } |
||
| 1942 | if (empty($thirdpartyid)) { |
||
| 1943 | $thirdpartyid = $projectstatic->fk_soc; |
||
| 1944 | } |
||
| 1945 | } |
||
| 1946 | } |
||
| 1947 | // Contact |
||
| 1948 | if ($contactid > 0) { |
||
| 1949 | $result = $contactstatic->fetch($contactid); |
||
| 1950 | if ($result <= 0) { |
||
| 1951 | $contactstatic->id = 0; |
||
| 1952 | } else { |
||
| 1953 | $contactid = $contactstatic->id; |
||
| 1954 | if ($trackid) { |
||
| 1955 | $contactfoundby = 'trackid (' . $trackid . ')'; |
||
| 1956 | } |
||
| 1957 | if (empty($thirdpartyid)) { |
||
| 1958 | $thirdpartyid = $contactstatic->fk_soc; |
||
| 1959 | } |
||
| 1960 | } |
||
| 1961 | } |
||
| 1962 | // Thirdparty |
||
| 1963 | if ($thirdpartyid > 0) { |
||
| 1964 | $result = $thirdpartystatic->fetch($thirdpartyid); |
||
| 1965 | if ($result <= 0) { |
||
| 1966 | $thirdpartystatic->id = 0; |
||
| 1967 | } else { |
||
| 1968 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1969 | if ($trackid) { |
||
| 1970 | $thirdpartyfoundby = 'trackid (' . $trackid . ')'; |
||
| 1971 | } |
||
| 1972 | } |
||
| 1973 | } |
||
| 1974 | |||
| 1975 | if (is_object($objectemail)) { |
||
| 1976 | break; // Exit loop of references. We already found an accurate reference |
||
| 1977 | } |
||
| 1978 | } |
||
| 1979 | |||
| 1980 | if (empty($contactid)) { // Try to find contact using email |
||
| 1981 | $result = $contactstatic->fetch(0, null, '', $from); |
||
| 1982 | |||
| 1983 | if ($result > 0) { |
||
| 1984 | dol_syslog("We found a contact with the email " . $from); |
||
| 1985 | $contactid = $contactstatic->id; |
||
| 1986 | $contactfoundby = 'email of contact (' . $from . ')'; |
||
| 1987 | if (empty($thirdpartyid) && $contactstatic->socid > 0) { |
||
| 1988 | $result = $thirdpartystatic->fetch($contactstatic->socid); |
||
| 1989 | if ($result > 0) { |
||
| 1990 | $thirdpartyid = $thirdpartystatic->id; |
||
| 1991 | $thirdpartyfoundby = 'email of contact (' . $from . ')'; |
||
| 1992 | } |
||
| 1993 | } |
||
| 1994 | } |
||
| 1995 | } |
||
| 1996 | |||
| 1997 | if (empty($thirdpartyid)) { // Try to find thirdparty using email |
||
| 1998 | $result = $thirdpartystatic->fetch(0, '', '', '', '', '', '', '', '', '', $from); |
||
| 1999 | if ($result > 0) { |
||
| 2000 | dol_syslog("We found a thirdparty with the email " . $from); |
||
| 2001 | $thirdpartyid = $thirdpartystatic->id; |
||
| 2002 | $thirdpartyfoundby = 'email (' . $from . ')'; |
||
| 2003 | } |
||
| 2004 | } |
||
| 2005 | |||
| 2006 | /* |
||
| 2007 | if ($replyto) { |
||
| 2008 | if (empty($contactid)) { // Try to find contact using email |
||
| 2009 | $result = $contactstatic->fetch(0, null, '', $replyto); |
||
| 2010 | |||
| 2011 | if ($result > 0) { |
||
| 2012 | dol_syslog("We found a contact with the email ".$replyto); |
||
| 2013 | $contactid = $contactstatic->id; |
||
| 2014 | $contactfoundby = 'email of contact ('.$replyto.')'; |
||
| 2015 | if (empty($thirdpartyid) && $contactstatic->socid > 0) { |
||
| 2016 | $result = $thirdpartystatic->fetch($contactstatic->socid); |
||
| 2017 | if ($result > 0) { |
||
| 2018 | $thirdpartyid = $thirdpartystatic->id; |
||
| 2019 | $thirdpartyfoundby = 'email of contact ('.$replyto.')'; |
||
| 2020 | } |
||
| 2021 | } |
||
| 2022 | } |
||
| 2023 | } |
||
| 2024 | |||
| 2025 | if (empty($thirdpartyid)) { // Try to find thirdparty using email |
||
| 2026 | $result = $thirdpartystatic->fetch(0, '', '', '', '', '', '', '', '', '', $replyto); |
||
| 2027 | if ($result > 0) { |
||
| 2028 | dol_syslog("We found a thirdparty with the email ".$replyto); |
||
| 2029 | $thirdpartyid = $thirdpartystatic->id; |
||
| 2030 | $thirdpartyfoundby = 'email ('.$replyto.')'; |
||
| 2031 | } |
||
| 2032 | } |
||
| 2033 | } |
||
| 2034 | */ |
||
| 2035 | |||
| 2036 | // Do operations (extract variables and creating data) |
||
| 2037 | if ($mode < 2) { // 0=Mode production, 1=Mode test (read IMAP and try SQL update then rollback), 2=Mode test with no SQL updates |
||
| 2038 | foreach ($this->actions as $operation) { |
||
| 2039 | $errorforthisaction = 0; |
||
| 2040 | $ticketalreadyexists = 0; |
||
| 2041 | if ($errorforactions) { |
||
| 2042 | break; |
||
| 2043 | } |
||
| 2044 | if (empty($operation['status'])) { |
||
| 2045 | continue; |
||
| 2046 | } |
||
| 2047 | |||
| 2048 | $operationslog .= '<br>* Process operation ' . $operation['type']; |
||
| 2049 | |||
| 2050 | // Make Operation |
||
| 2051 | dol_syslog("Execute action " . $operation['type'] . " actionparam=" . $operation['actionparam'] . ' thirdpartystatic->id=' . $thirdpartystatic->id . ' contactstatic->id=' . $contactstatic->id . ' projectstatic->id=' . $projectstatic->id); |
||
| 2052 | dol_syslog("Execute action fk_element_id=" . $fk_element_id . " fk_element_type=" . $fk_element_type); // If a Dolibarr tracker id is found, we should now the id of object |
||
| 2053 | |||
| 2054 | // Try to guess if this is an email in or out. |
||
| 2055 | $actioncode = 'EMAIL_IN'; |
||
| 2056 | // If we scan the Sent box, we use the code for out email |
||
| 2057 | if (preg_match('/Sent$/', $sourcedir) || preg_match('/envoyés$/i', $sourcedir)) { |
||
| 2058 | $actioncode = 'EMAIL'; |
||
| 2059 | } |
||
| 2060 | // If sender is in the list MAIL_FROM_EMAILS_TO_CONSIDER_SENDING |
||
| 2061 | $arrayofemailtoconsideresender = explode(',', getDolGlobalString('MAIL_FROM_EMAILS_TO_CONSIDER_SENDING')); |
||
| 2062 | foreach ($arrayofemailtoconsideresender as $emailtoconsidersender) { |
||
| 2063 | if (preg_match('/' . preg_quote($emailtoconsidersender, '/') . '/', $fromstring)) { |
||
| 2064 | $actioncode = 'EMAIL'; |
||
| 2065 | } |
||
| 2066 | } |
||
| 2067 | $operationslog .= '<br>Email will have actioncode=' . $actioncode; |
||
| 2068 | |||
| 2069 | $description = $descriptiontitle = $descriptionmeta = $descriptionfull = ''; |
||
| 2070 | |||
| 2071 | $descriptiontitle = $langs->trans("RecordCreatedByEmailCollector", $this->ref, $msgid); |
||
| 2072 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailTopic") . ' : ' . dol_escape_htmltag($subject)); |
||
| 2073 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailDate") . ($langs->trans("MailDate") != 'Date' ? ' (Date)' : '') . ' : ' . dol_escape_htmltag(dol_print_date($dateemail, "dayhourtext", "gmt"))); |
||
| 2074 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailFrom") . ($langs->trans("MailFrom") != 'From' ? ' (From)' : '') . ' : ' . dol_escape_htmltag($fromstring)); |
||
| 2075 | if ($sender) { |
||
| 2076 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("Sender") . ($langs->trans("Sender") != 'Sender' ? ' (Sender)' : '') . ' : ' . dol_escape_htmltag($sender)); |
||
| 2077 | } |
||
| 2078 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailTo") . ($langs->trans("MailTo") != 'To' ? ' (To)' : '') . ' : ' . dol_escape_htmltag($to)); |
||
| 2079 | if ($replyto) { |
||
| 2080 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailReply") . ($langs->trans("MailReply") != 'Reply to' ? ' (Reply to)' : '') . ' : ' . dol_escape_htmltag($replyto)); |
||
| 2081 | } |
||
| 2082 | if ($sendtocc) { |
||
| 2083 | $descriptionmeta = dol_concatdesc($descriptionmeta, $langs->trans("MailCC") . ($langs->trans("MailCC") != 'CC' ? ' (CC)' : '') . ' : ' . dol_escape_htmltag($sendtocc)); |
||
| 2084 | } |
||
| 2085 | |||
| 2086 | if ($operation['type'] == 'ticket') { |
||
| 2087 | // Verify if ticket already exists to fall back on the right operation |
||
| 2088 | $tickettocreate = new Ticket($this->db); |
||
| 2089 | $errorfetchticket = 0; |
||
| 2090 | $alreadycreated = 0; |
||
| 2091 | if (!empty($trackid)) { |
||
| 2092 | $alreadycreated = $tickettocreate->fetch(0, '', $trackid); |
||
| 2093 | } |
||
| 2094 | if ($alreadycreated == 0 && !empty($msgid)) { |
||
| 2095 | $alreadycreated = $tickettocreate->fetch(0, '', '', $msgid); |
||
| 2096 | } |
||
| 2097 | if ($alreadycreated < 0) { |
||
| 2098 | $errorfetchticket++; |
||
| 2099 | } |
||
| 2100 | if (empty($errorfetchticket)) { |
||
| 2101 | if ($alreadycreated == 0) { |
||
| 2102 | $operationslog .= '<br>Ticket not found using trackid=' . $trackid . ' or msgid=' . $msgid; |
||
| 2103 | $ticketalreadyexists = 0; |
||
| 2104 | } else { |
||
| 2105 | $operationslog .= '<br>Ticket already found using trackid=' . $trackid . ' or msgid=' . $msgid; // We change the operation type to do |
||
| 2106 | $ticketalreadyexists = 1; |
||
| 2107 | $operation['type'] = 'recordevent'; |
||
| 2108 | } |
||
| 2109 | } else { |
||
| 2110 | $ticketalreadyexists = -1; |
||
| 2111 | } |
||
| 2112 | } |
||
| 2113 | |||
| 2114 | // Process now the operation type |
||
| 2115 | |||
| 2116 | // Search and create thirdparty |
||
| 2117 | if ($operation['type'] == 'loadthirdparty' || $operation['type'] == 'loadandcreatethirdparty') { |
||
| 2118 | if (empty($operation['actionparam'])) { |
||
| 2119 | $errorforactions++; |
||
| 2120 | $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;"; |
||
| 2121 | $this->errors[] = $this->error; |
||
| 2122 | } else { |
||
| 2123 | $actionparam = $operation['actionparam']; |
||
| 2124 | $idtouseforthirdparty = ''; |
||
| 2125 | $nametouseforthirdparty = ''; |
||
| 2126 | $emailtouseforthirdparty = ''; |
||
| 2127 | $namealiastouseforthirdparty = ''; |
||
| 2128 | |||
| 2129 | $operationslog .= '<br>Loop on each property to set into actionparam'; |
||
| 2130 | |||
| 2131 | // $actionparam = 'param=SET:aaa' or 'param=EXTRACT:BODY:....' |
||
| 2132 | $arrayvaluetouse = dolExplodeIntoArray($actionparam, '(\n\r|\r|\n|;)', '='); |
||
| 2133 | foreach ($arrayvaluetouse as $propertytooverwrite => $valueforproperty) { |
||
| 2134 | $sourcestring = ''; |
||
| 2135 | $sourcefield = ''; |
||
| 2136 | $regexstring = ''; |
||
| 2137 | $regforregex = array(); |
||
| 2138 | |||
| 2139 | if (preg_match('/^EXTRACT:([a-zA-Z0-9_]+):(.*)$/', $valueforproperty, $regforregex)) { |
||
| 2140 | $sourcefield = $regforregex[1]; |
||
| 2141 | $regexstring = $regforregex[2]; |
||
| 2142 | } |
||
| 2143 | |||
| 2144 | if (!empty($sourcefield) && !empty($regexstring)) { |
||
| 2145 | if (strtolower($sourcefield) == 'body') { |
||
| 2146 | $sourcestring = $messagetext; |
||
| 2147 | } elseif (strtolower($sourcefield) == 'subject') { |
||
| 2148 | $sourcestring = $subject; |
||
| 2149 | } elseif (strtolower($sourcefield) == 'header') { |
||
| 2150 | $sourcestring = $header; |
||
| 2151 | } |
||
| 2152 | |||
| 2153 | if ($sourcestring) { |
||
| 2154 | $regforval = array(); |
||
| 2155 | //var_dump($regexstring);var_dump($sourcestring); |
||
| 2156 | if (preg_match('/' . $regexstring . '/ms', $sourcestring, $regforval)) { |
||
| 2157 | //var_dump($regforval[count($regforval)-1]);exit; |
||
| 2158 | // Overwrite param $tmpproperty |
||
| 2159 | if ($propertytooverwrite == 'id') { |
||
| 2160 | $idtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null; |
||
| 2161 | |||
| 2162 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Found idtouseforthirdparty=' . dol_escape_htmltag($idtouseforthirdparty); |
||
| 2163 | } elseif ($propertytooverwrite == 'email') { |
||
| 2164 | $emailtouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null; |
||
| 2165 | |||
| 2166 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Found emailtouseforthirdparty=' . dol_escape_htmltag($emailtouseforthirdparty); |
||
| 2167 | } elseif ($propertytooverwrite == 'name') { |
||
| 2168 | $nametouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null; |
||
| 2169 | |||
| 2170 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Found nametouseforthirdparty=' . dol_escape_htmltag($nametouseforthirdparty); |
||
| 2171 | } elseif ($propertytooverwrite == 'name_alias') { |
||
| 2172 | $namealiastouseforthirdparty = isset($regforval[count($regforval) - 1]) ? trim($regforval[count($regforval) - 1]) : null; |
||
| 2173 | |||
| 2174 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Found namealiastouseforthirdparty=' . dol_escape_htmltag($namealiastouseforthirdparty); |
||
| 2175 | } else { |
||
| 2176 | $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'; |
||
| 2177 | } |
||
| 2178 | } else { |
||
| 2179 | // Regex not found |
||
| 2180 | if (in_array($propertytooverwrite, array('id', 'email', 'name', 'name_alias'))) { |
||
| 2181 | $idtouseforthirdparty = null; |
||
| 2182 | $nametouseforthirdparty = null; |
||
| 2183 | $emailtouseforthirdparty = null; |
||
| 2184 | $namealiastouseforthirdparty = null; |
||
| 2185 | |||
| 2186 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Not found. Property searched is critical so we cancel the search.'; |
||
| 2187 | } else { |
||
| 2188 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' Regex /' . dol_escape_htmltag($regexstring) . '/ms into ' . strtoupper($sourcefield) . ' -> Not found'; |
||
| 2189 | } |
||
| 2190 | } |
||
| 2191 | //var_dump($object->$tmpproperty);exit; |
||
| 2192 | } else { |
||
| 2193 | // Nothing can be done for this param |
||
| 2194 | $errorforactions++; |
||
| 2195 | $this->error = 'The extract rule to use to load thirdparty for email ' . $msgid . ' has an unknown source (must be HEADER, SUBJECT or BODY)'; |
||
| 2196 | $this->errors[] = $this->error; |
||
| 2197 | |||
| 2198 | $operationslog .= '<br>' . $this->error; |
||
| 2199 | } |
||
| 2200 | } elseif (preg_match('/^(SET|SETIFEMPTY):(.*)$/', $valueforproperty, $reg)) { |
||
| 2201 | //if (preg_match('/^options_/', $tmpproperty)) $object->array_options[preg_replace('/^options_/', '', $tmpproperty)] = $reg[1]; |
||
| 2202 | //else $object->$tmpproperty = $reg[1]; |
||
| 2203 | // Example: id=SETIFEMPTY:123 |
||
| 2204 | if ($propertytooverwrite == 'id') { |
||
| 2205 | $idtouseforthirdparty = $reg[2]; |
||
| 2206 | |||
| 2207 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' We set property idtouseforthrdparty=' . dol_escape_htmltag($idtouseforthirdparty); |
||
| 2208 | } elseif ($propertytooverwrite == 'email') { |
||
| 2209 | $emailtouseforthirdparty = $reg[2]; |
||
| 2210 | |||
| 2211 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' We set property emailtouseforthrdparty=' . dol_escape_htmltag($emailtouseforthirdparty); |
||
| 2212 | } elseif ($propertytooverwrite == 'name') { |
||
| 2213 | $nametouseforthirdparty = $reg[2]; |
||
| 2214 | |||
| 2215 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' We set property nametouseforthirdparty=' . dol_escape_htmltag($nametouseforthirdparty); |
||
| 2216 | } elseif ($propertytooverwrite == 'name_alias') { |
||
| 2217 | $namealiastouseforthirdparty = $reg[2]; |
||
| 2218 | |||
| 2219 | $operationslog .= '<br>propertytooverwrite=' . $propertytooverwrite . ' We set property namealiastouseforthirdparty=' . dol_escape_htmltag($namealiastouseforthirdparty); |
||
| 2220 | } |
||
| 2221 | } else { |
||
| 2222 | $errorforactions++; |
||
| 2223 | $this->error = 'Bad syntax for description of action parameters: ' . $actionparam; |
||
| 2224 | $this->errors[] = $this->error; |
||
| 2225 | break; |
||
| 2226 | } |
||
| 2227 | } |
||
| 2228 | |||
| 2229 | if (!$errorforactions && ($idtouseforthirdparty || $emailtouseforthirdparty || $nametouseforthirdparty || $namealiastouseforthirdparty)) { |
||
| 2230 | // We make another search on thirdparty |
||
| 2231 | $operationslog .= '<br>We have this data to search thirdparty: id=' . $idtouseforthirdparty . ', email=' . $emailtouseforthirdparty . ', name=' . $nametouseforthirdparty . ', name_alias=' . $namealiastouseforthirdparty; |
||
| 2232 | |||
| 2233 | $tmpobject = new stdClass(); |
||
| 2234 | $tmpobject->element = 'generic'; |
||
| 2235 | $tmpobject->id = $idtouseforthirdparty; |
||
| 2236 | $tmpobject->name = $nametouseforthirdparty; |
||
| 2237 | $tmpobject->name_alias = $namealiastouseforthirdparty; |
||
| 2238 | $tmpobject->email = $emailtouseforthirdparty; |
||
| 2239 | |||
| 2240 | $this->overwritePropertiesOfObject($tmpobject, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2241 | |||
| 2242 | $idtouseforthirdparty = $tmpobject->id; |
||
| 2243 | $nametouseforthirdparty = $tmpobject->name; |
||
| 2244 | $namealiastouseforthirdparty = $tmpobject->name_alias; |
||
| 2245 | $emailtouseforthirdparty = $tmpobject->email; |
||
| 2246 | |||
| 2247 | $operationslog .= '<br>We try to search existing thirdparty with ' . $idtouseforthirdparty . ' ' . $emailtouseforthirdparty . ' ' . $nametouseforthirdparty . ' ' . $namealiastouseforthirdparty; |
||
| 2248 | |||
| 2249 | $result = $thirdpartystatic->fetch($idtouseforthirdparty, $nametouseforthirdparty, '', '', '', '', '', '', '', '', $emailtouseforthirdparty, $namealiastouseforthirdparty); |
||
| 2250 | if ($result < 0) { |
||
| 2251 | $errorforactions++; |
||
| 2252 | $this->error = 'Error when getting thirdparty with name ' . $nametouseforthirdparty . ' (may be 2 record exists with same name ?)'; |
||
| 2253 | $this->errors[] = $this->error; |
||
| 2254 | break; |
||
| 2255 | } elseif ($result == 0) { |
||
| 2256 | if ($operation['type'] == 'loadthirdparty') { |
||
| 2257 | dol_syslog("Third party with id=" . $idtouseforthirdparty . " email=" . $emailtouseforthirdparty . " name=" . $nametouseforthirdparty . " name_alias=" . $namealiastouseforthirdparty . " was not found"); |
||
| 2258 | |||
| 2259 | //search into contacts of thirdparty |
||
| 2260 | $resultContact = $contactstatic->fetch('', '', '', $emailtouseforthirdparty); |
||
| 2261 | if ($resultContact > 0) { |
||
| 2262 | $idtouseforthirdparty = $contactstatic->socid; |
||
| 2263 | $result = $thirdpartystatic->fetch($idtouseforthirdparty); |
||
| 2264 | if ($result > 0) { |
||
| 2265 | dol_syslog("Third party with id=" . $idtouseforthirdparty . " email=" . $emailtouseforthirdparty . " name=" . $nametouseforthirdparty . " name_alias=" . $namealiastouseforthirdparty . " was found thanks to linked contact search"); |
||
| 2266 | } else { |
||
| 2267 | $errorforactions++; |
||
| 2268 | $langs->load("errors"); |
||
| 2269 | $this->error = $langs->trans('ErrorFailedToLoadThirdParty', $idtouseforthirdparty, $emailtouseforthirdparty, $nametouseforthirdparty, $namealiastouseforthirdparty); |
||
| 2270 | $this->errors[] = $this->error; |
||
| 2271 | } |
||
| 2272 | } else { |
||
| 2273 | $errorforactions++; |
||
| 2274 | $langs->load("errors"); |
||
| 2275 | $this->error = $langs->trans('ErrorFailedToLoadThirdParty', $idtouseforthirdparty, $emailtouseforthirdparty, $nametouseforthirdparty, $namealiastouseforthirdparty); |
||
| 2276 | $this->errors[] = $this->error; |
||
| 2277 | } |
||
| 2278 | } elseif ($operation['type'] == 'loadandcreatethirdparty') { |
||
| 2279 | dol_syslog("Third party with id=" . $idtouseforthirdparty . " email=" . $emailtouseforthirdparty . " name=" . $nametouseforthirdparty . " name_alias=" . $namealiastouseforthirdparty . " was not found. We try to create it."); |
||
| 2280 | |||
| 2281 | // Create thirdparty |
||
| 2282 | $thirdpartystatic = new Societe($db); |
||
| 2283 | $thirdpartystatic->name = $nametouseforthirdparty; |
||
| 2284 | if (!empty($namealiastouseforthirdparty)) { |
||
| 2285 | if ($namealiastouseforthirdparty != $nametouseforthirdparty) { |
||
| 2286 | $thirdpartystatic->name_alias = $namealiastouseforthirdparty; |
||
| 2287 | } |
||
| 2288 | } else { |
||
| 2289 | $thirdpartystatic->name_alias = (empty($replytostring) ? (empty($fromtext) ? '' : $fromtext) : $replytostring); |
||
| 2290 | } |
||
| 2291 | $thirdpartystatic->email = (empty($emailtouseforthirdparty) ? (empty($replyto) ? (empty($from) ? '' : $from) : $replyto) : $emailtouseforthirdparty); |
||
| 2292 | |||
| 2293 | // Overwrite values with values extracted from source email |
||
| 2294 | $errorforthisaction = $this->overwritePropertiesOfObject($thirdpartystatic, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2295 | |||
| 2296 | if ($thirdpartystatic->client && empty($thirdpartystatic->code_client)) { |
||
| 2297 | $thirdpartystatic->code_client = 'auto'; |
||
| 2298 | } |
||
| 2299 | if ($thirdpartystatic->fournisseur && empty($thirdpartystatic->code_fournisseur)) { |
||
| 2300 | $thirdpartystatic->code_fournisseur = 'auto'; |
||
| 2301 | } |
||
| 2302 | |||
| 2303 | if ($errorforthisaction) { |
||
| 2304 | $errorforactions++; |
||
| 2305 | } else { |
||
| 2306 | $result = $thirdpartystatic->create($user); |
||
| 2307 | if ($result <= 0) { |
||
| 2308 | $errorforactions++; |
||
| 2309 | $this->error = $thirdpartystatic->error; |
||
| 2310 | $this->errors = $thirdpartystatic->errors; |
||
| 2311 | } else { |
||
| 2312 | $operationslog .= '<br>Thirdparty created -> id = ' . dol_escape_htmltag($thirdpartystatic->id); |
||
| 2313 | } |
||
| 2314 | } |
||
| 2315 | } |
||
| 2316 | } else { |
||
| 2317 | dol_syslog("One and only one existing third party has been found"); |
||
| 2318 | |||
| 2319 | $operationslog .= '<br>Thirdparty already exists with id = ' . dol_escape_htmltag($thirdpartystatic->id); |
||
| 2320 | } |
||
| 2321 | } |
||
| 2322 | } |
||
| 2323 | } elseif ($operation['type'] == 'loadandcreatecontact') { // Search and create contact |
||
| 2324 | if (empty($operation['actionparam'])) { |
||
| 2325 | $errorforactions++; |
||
| 2326 | $this->error = "Action loadandcreatecontact has empty parameter. Must be 'SET:xxx' or 'EXTRACT:(body|subject):regex' to define how to extract data"; |
||
| 2327 | $this->errors[] = $this->error; |
||
| 2328 | } else { |
||
| 2329 | $contact_static = new Contact($this->db); |
||
| 2330 | // Overwrite values with values extracted from source email |
||
| 2331 | $errorforthisaction = $this->overwritePropertiesOfObject($contact_static, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2332 | if ($errorforthisaction) { |
||
| 2333 | $errorforactions++; |
||
| 2334 | } else { |
||
| 2335 | if (!empty($contact_static->email) && $contact_static->email != $from) { |
||
| 2336 | $from = $contact_static->email; |
||
| 2337 | } |
||
| 2338 | |||
| 2339 | $result = $contactstatic->fetch(0, null, '', $from); |
||
| 2340 | if ($result < 0) { |
||
| 2341 | $errorforactions++; |
||
| 2342 | $this->error = 'Error when getting contact with email ' . $from; |
||
| 2343 | $this->errors[] = $this->error; |
||
| 2344 | break; |
||
| 2345 | } elseif ($result == 0) { |
||
| 2346 | dol_syslog("Contact with email " . $from . " was not found. We try to create it."); |
||
| 2347 | $contactstatic = new Contact($this->db); |
||
| 2348 | |||
| 2349 | // Create contact |
||
| 2350 | $contactstatic->email = $from; |
||
| 2351 | $operationslog .= '<br>We set property email=' . dol_escape_htmltag($from); |
||
| 2352 | |||
| 2353 | // Overwrite values with values extracted from source email |
||
| 2354 | $errorforthisaction = $this->overwritePropertiesOfObject($contactstatic, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2355 | |||
| 2356 | if ($errorforthisaction) { |
||
| 2357 | $errorforactions++; |
||
| 2358 | } else { |
||
| 2359 | // Search country by name or code |
||
| 2360 | if (!empty($contactstatic->country)) { |
||
| 2361 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/company.lib.php'; |
||
| 2362 | $result = getCountry('', 3, $this->db, '', 1, $contactstatic->country); |
||
| 2363 | if ($result == 'NotDefined') { |
||
| 2364 | $errorforactions++; |
||
| 2365 | $this->error = "Error country not found by this name '" . $contactstatic->country . "'"; |
||
| 2366 | } elseif (!($result > 0)) { |
||
| 2367 | $errorforactions++; |
||
| 2368 | $this->error = "Error when search country by this name '" . $contactstatic->country . "'"; |
||
| 2369 | $this->errors[] = $this->db->lasterror(); |
||
| 2370 | } else { |
||
| 2371 | $contactstatic->country_id = $result; |
||
| 2372 | $operationslog .= '<br>We set property country_id=' . dol_escape_htmltag($result); |
||
| 2373 | } |
||
| 2374 | } elseif (!empty($contactstatic->country_code)) { |
||
| 2375 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/company.lib.php'; |
||
| 2376 | $result = getCountry($contactstatic->country_code, 3, $this->db); |
||
| 2377 | if ($result == 'NotDefined') { |
||
| 2378 | $errorforactions++; |
||
| 2379 | $this->error = "Error country not found by this code '" . $contactstatic->country_code . "'"; |
||
| 2380 | } elseif (!($result > 0)) { |
||
| 2381 | $errorforactions++; |
||
| 2382 | $this->error = "Error when search country by this code '" . $contactstatic->country_code . "'"; |
||
| 2383 | $this->errors[] = $this->db->lasterror(); |
||
| 2384 | } else { |
||
| 2385 | $contactstatic->country_id = $result; |
||
| 2386 | $operationslog .= '<br>We set property country_id=' . dol_escape_htmltag($result); |
||
| 2387 | } |
||
| 2388 | } |
||
| 2389 | |||
| 2390 | if (!$errorforactions) { |
||
| 2391 | // Search state by name or code (for country if defined) |
||
| 2392 | if (!empty($contactstatic->state)) { |
||
| 2393 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/functions.lib.php'; |
||
| 2394 | $result = dol_getIdFromCode($this->db, $contactstatic->state, 'c_departements', 'nom', 'rowid'); |
||
| 2395 | if (empty($result)) { |
||
| 2396 | $errorforactions++; |
||
| 2397 | $this->error = "Error state not found by this name '" . $contactstatic->state . "'"; |
||
| 2398 | } elseif (!($result > 0)) { |
||
| 2399 | $errorforactions++; |
||
| 2400 | $this->error = "Error when search state by this name '" . $contactstatic->state . "'"; |
||
| 2401 | $this->errors[] = $this->db->lasterror(); |
||
| 2402 | } else { |
||
| 2403 | $contactstatic->state_id = $result; |
||
| 2404 | $operationslog .= '<br>We set property state_id=' . dol_escape_htmltag($result); |
||
| 2405 | } |
||
| 2406 | } elseif (!empty($contactstatic->state_code)) { |
||
| 2407 | require_once constant('DOL_DOCUMENT_ROOT') . '/core/lib/functions.lib.php'; |
||
| 2408 | $result = dol_getIdFromCode($this->db, $contactstatic->state_code, 'c_departements', 'code_departement', 'rowid'); |
||
| 2409 | if (empty($result)) { |
||
| 2410 | $errorforactions++; |
||
| 2411 | $this->error = "Error state not found by this code '" . $contactstatic->state_code . "'"; |
||
| 2412 | } elseif (!($result > 0)) { |
||
| 2413 | $errorforactions++; |
||
| 2414 | $this->error = "Error when search state by this code '" . $contactstatic->state_code . "'"; |
||
| 2415 | $this->errors[] = $this->db->lasterror(); |
||
| 2416 | } else { |
||
| 2417 | $contactstatic->state_id = $result; |
||
| 2418 | $operationslog .= '<br>We set property state_id=' . dol_escape_htmltag($result); |
||
| 2419 | } |
||
| 2420 | } |
||
| 2421 | } |
||
| 2422 | |||
| 2423 | if (!$errorforactions) { |
||
| 2424 | $result = $contactstatic->create($user); |
||
| 2425 | if ($result <= 0) { |
||
| 2426 | $errorforactions++; |
||
| 2427 | $this->error = $contactstatic->error; |
||
| 2428 | $this->errors = $contactstatic->errors; |
||
| 2429 | } else { |
||
| 2430 | $operationslog .= '<br>Contact created -> id = ' . dol_escape_htmltag($contactstatic->id); |
||
| 2431 | } |
||
| 2432 | } |
||
| 2433 | } |
||
| 2434 | } |
||
| 2435 | } |
||
| 2436 | } |
||
| 2437 | } elseif ($operation['type'] == 'recordevent') { |
||
| 2438 | // Create event |
||
| 2439 | $actioncomm = new ActionComm($this->db); |
||
| 2440 | |||
| 2441 | $alreadycreated = $actioncomm->fetch(0, '', '', $msgid); |
||
| 2442 | if ($alreadycreated == 0) { |
||
| 2443 | $operationslog .= '<br>We did not find existing actionmail with msgid=' . $msgid; |
||
| 2444 | |||
| 2445 | if ($projectstatic->id > 0) { |
||
| 2446 | if ($projectfoundby) { |
||
| 2447 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Project found from ' . $projectfoundby); |
||
| 2448 | } |
||
| 2449 | } |
||
| 2450 | if ($thirdpartystatic->id > 0) { |
||
| 2451 | if ($thirdpartyfoundby) { |
||
| 2452 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from ' . $thirdpartyfoundby); |
||
| 2453 | } |
||
| 2454 | } |
||
| 2455 | if ($contactstatic->id > 0) { |
||
| 2456 | if ($contactfoundby) { |
||
| 2457 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from ' . $contactfoundby); |
||
| 2458 | } |
||
| 2459 | } |
||
| 2460 | |||
| 2461 | $description = $descriptiontitle; |
||
| 2462 | $description = dol_concatdesc($description, "-----"); |
||
| 2463 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 2464 | $description = dol_concatdesc($description, "-----"); |
||
| 2465 | $description = dol_concatdesc($description, $messagetext); |
||
| 2466 | |||
| 2467 | $descriptionfull = $description; |
||
| 2468 | if (!getDolGlobalString('MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER')) { |
||
| 2469 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 2470 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 2471 | } |
||
| 2472 | |||
| 2473 | // Insert record of emails sent |
||
| 2474 | $actioncomm->type_code = 'AC_OTH_AUTO'; // Type of event ('AC_OTH', 'AC_OTH_AUTO', 'AC_XXX'...) |
||
| 2475 | $actioncomm->code = 'AC_' . $actioncode; |
||
| 2476 | $actioncomm->label = $langs->trans("ActionAC_" . $actioncode) . ' - ' . $langs->trans("MailFrom") . ' ' . $from; |
||
| 2477 | $actioncomm->note_private = $descriptionfull; |
||
| 2478 | $actioncomm->fk_project = $projectstatic->id; |
||
| 2479 | $actioncomm->datep = $dateemail; // date of email |
||
| 2480 | $actioncomm->datef = $dateemail; // date of email |
||
| 2481 | $actioncomm->percentage = -1; // Not applicable |
||
| 2482 | $actioncomm->socid = $thirdpartystatic->id; |
||
| 2483 | $actioncomm->contact_id = $contactstatic->id; |
||
| 2484 | $actioncomm->socpeopleassigned = (!empty($contactstatic->id) ? array($contactstatic->id) : array()); |
||
| 2485 | $actioncomm->authorid = $user->id; // User saving action |
||
| 2486 | $actioncomm->userownerid = $user->id; // Owner of action |
||
| 2487 | // Fields when action is an email (content should be added into note) |
||
| 2488 | $actioncomm->email_msgid = $msgid; |
||
| 2489 | $actioncomm->email_from = $fromstring; |
||
| 2490 | $actioncomm->email_sender = $sender; |
||
| 2491 | $actioncomm->email_to = $to; |
||
| 2492 | $actioncomm->email_tocc = $sendtocc; |
||
| 2493 | $actioncomm->email_tobcc = $sendtobcc; |
||
| 2494 | $actioncomm->email_subject = $subject; |
||
| 2495 | $actioncomm->errors_to = ''; |
||
| 2496 | |||
| 2497 | if (!in_array($fk_element_type, array('societe', 'contact', 'project', 'user'))) { |
||
| 2498 | $actioncomm->fk_element = $fk_element_id; |
||
| 2499 | $actioncomm->elementid = $fk_element_id; |
||
| 2500 | $actioncomm->elementtype = $fk_element_type; |
||
| 2501 | if (is_object($objectemail) && $objectemail->module) { |
||
| 2502 | $actioncomm->elementtype .= '@' . $objectemail->module; |
||
| 2503 | } |
||
| 2504 | } |
||
| 2505 | |||
| 2506 | //$actioncomm->extraparams = $extraparams; |
||
| 2507 | |||
| 2508 | // Overwrite values with values extracted from source email |
||
| 2509 | $errorforthisaction = $this->overwritePropertiesOfObject($actioncomm, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2510 | |||
| 2511 | if ($errorforthisaction) { |
||
| 2512 | $errorforactions++; |
||
| 2513 | } else { |
||
| 2514 | $result = $actioncomm->create($user); |
||
| 2515 | if ($result <= 0) { |
||
| 2516 | $errorforactions++; |
||
| 2517 | $this->errors = $actioncomm->errors; |
||
| 2518 | } else { |
||
| 2519 | if ($fk_element_type == "ticket" && is_object($objectemail)) { |
||
| 2520 | if ($objectemail->status == Ticket::STATUS_CLOSED || $objectemail->status == Ticket::STATUS_CANCELED) { |
||
| 2521 | if ($objectemail->fk_user_assign != null) { |
||
| 2522 | $res = $objectemail->setStatut(Ticket::STATUS_ASSIGNED); |
||
| 2523 | } else { |
||
| 2524 | $res = $objectemail->setStatut(Ticket::STATUS_NOT_READ); |
||
| 2525 | } |
||
| 2526 | |||
| 2527 | if ($res) { |
||
| 2528 | $operationslog .= '<br>Ticket Re-Opened successfully -> ref=' . $objectemail->ref; |
||
| 2529 | } else { |
||
| 2530 | $errorforactions++; |
||
| 2531 | $this->error = 'Error while changing the ticket status -> ref=' . $objectemail->ref; |
||
| 2532 | $this->errors[] = $this->error; |
||
| 2533 | } |
||
| 2534 | } |
||
| 2535 | if (!empty($attachments)) { |
||
| 2536 | // There is an attachment for the ticket -> store attachment |
||
| 2537 | $ticket = new Ticket($this->db); |
||
| 2538 | $ticket->fetch($fk_element_id); |
||
| 2539 | $destdir = $conf->ticket->dir_output . '/' . $ticket->ref; |
||
| 2540 | if (!dol_is_dir($destdir)) { |
||
| 2541 | dol_mkdir($destdir); |
||
| 2542 | } |
||
| 2543 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 2544 | foreach ($attachments as $attachment) { |
||
| 2545 | $attachment->save($destdir . '/'); |
||
| 2546 | } |
||
| 2547 | } else { |
||
| 2548 | $this->getmsg($connection, $imapemail, $destdir); |
||
| 2549 | } |
||
| 2550 | } |
||
| 2551 | } |
||
| 2552 | |||
| 2553 | $operationslog .= '<br>Event created -> id=' . dol_escape_htmltag($actioncomm->id); |
||
| 2554 | } |
||
| 2555 | } |
||
| 2556 | } |
||
| 2557 | } elseif ($operation['type'] == 'recordjoinpiece') { |
||
| 2558 | $data = []; |
||
| 2559 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 2560 | foreach ($attachments as $attachment) { |
||
| 2561 | if ($attachment->getName() === 'undefined') { |
||
| 2562 | continue; |
||
| 2563 | } |
||
| 2564 | $data[$attachment->getName()] = $attachment->getContent(); |
||
| 2565 | } |
||
| 2566 | } else { |
||
| 2567 | $pj = getAttachments($imapemail, $connection); |
||
| 2568 | foreach ($pj as $key => $val) { |
||
| 2569 | $data[$val['filename']] = getFileData($imapemail, $val['pos'], $val['type'], $connection); |
||
| 2570 | } |
||
| 2571 | } |
||
| 2572 | if (count($data) > 0) { |
||
| 2573 | $sql = "SELECT rowid as id FROM " . MAIN_DB_PREFIX . "user WHERE email LIKE '%" . $this->db->escape($from) . "%'"; |
||
| 2574 | $resql = $this->db->query($sql); |
||
| 2575 | if ($this->db->num_rows($resql) == 0) { |
||
| 2576 | $this->errors[] = "User Not allowed to add documents ({$from})"; |
||
| 2577 | } |
||
| 2578 | $arrayobject = array( |
||
| 2579 | 'propale' => array('table' => 'propal', |
||
| 2580 | 'fields' => array('ref'), |
||
| 2581 | 'class' => 'comm/propal/class/propal.class.php', |
||
| 2582 | 'object' => 'Propal'), |
||
| 2583 | 'holiday' => array('table' => 'holiday', |
||
| 2584 | 'fields' => array('ref'), |
||
| 2585 | 'class' => 'holiday/class/holiday.class.php', |
||
| 2586 | 'object' => 'Holiday'), |
||
| 2587 | 'expensereport' => array('table' => 'expensereport', |
||
| 2588 | 'fields' => array('ref'), |
||
| 2589 | 'class' => 'expensereport/class/expensereport.class.php', |
||
| 2590 | 'object' => 'ExpenseReport'), |
||
| 2591 | 'recruitment/recruitmentjobposition' => array('table' => 'recruitment_recruitmentjobposition', |
||
| 2592 | 'fields' => array('ref'), |
||
| 2593 | 'class' => 'recruitment/class/recruitmentjobposition.class.php', |
||
| 2594 | 'object' => 'RecruitmentJobPosition'), |
||
| 2595 | 'recruitment/recruitmentcandidature' => array('table' => 'recruitment_recruitmentcandidature', |
||
| 2596 | 'fields' => array('ref'), |
||
| 2597 | 'class' => 'recruitment/class/recruitmentcandidature.class.php', |
||
| 2598 | 'object' => ' RecruitmentCandidature'), |
||
| 2599 | 'societe' => array('table' => 'societe', |
||
| 2600 | 'fields' => array('code_client', 'code_fournisseur'), |
||
| 2601 | 'class' => 'societe/class/societe.class.php', |
||
| 2602 | 'object' => 'Societe'), |
||
| 2603 | 'commande' => array('table' => 'commande', |
||
| 2604 | 'fields' => array('ref'), |
||
| 2605 | 'class' => 'commande/class/commande.class.php', |
||
| 2606 | 'object' => 'Commande'), |
||
| 2607 | 'expedition' => array('table' => 'expedition', |
||
| 2608 | 'fields' => array('ref'), |
||
| 2609 | 'class' => 'expedition/class/expedition.class.php', |
||
| 2610 | 'object' => 'Expedition'), |
||
| 2611 | 'contract' => array('table' => 'contrat', |
||
| 2612 | 'fields' => array('ref'), |
||
| 2613 | 'class' => 'contrat/class/contrat.class.php', |
||
| 2614 | 'object' => 'Contrat'), |
||
| 2615 | 'fichinter' => array('table' => 'fichinter', |
||
| 2616 | 'fields' => array('ref'), |
||
| 2617 | 'class' => 'fichinter/class/fichinter.class.php', |
||
| 2618 | 'object' => 'Fichinter'), |
||
| 2619 | 'ticket' => array('table' => 'ticket', |
||
| 2620 | 'fields' => array('ref'), |
||
| 2621 | 'class' => 'ticket/class/ticket.class.php', |
||
| 2622 | 'object' => 'Ticket'), |
||
| 2623 | 'knowledgemanagement' => array('table' => 'knowledgemanagement_knowledgerecord', |
||
| 2624 | 'fields' => array('ref'), |
||
| 2625 | 'class' => 'knowledgemanagement/class/knowledgemanagement.class.php', |
||
| 2626 | 'object' => 'KnowledgeRecord'), |
||
| 2627 | 'supplier_proposal' => array('table' => 'supplier_proposal', |
||
| 2628 | 'fields' => array('ref'), |
||
| 2629 | 'class' => 'supplier_proposal/class/supplier_proposal.class.php', |
||
| 2630 | 'object' => 'SupplierProposal'), |
||
| 2631 | 'fournisseur/commande' => array('table' => 'commande_fournisseur', |
||
| 2632 | 'fields' => array('ref', 'ref_supplier'), |
||
| 2633 | 'class' => 'fourn/class/fournisseur.commande.class.php', |
||
| 2634 | 'object' => 'SupplierProposal'), |
||
| 2635 | 'facture' => array('table' => 'facture', |
||
| 2636 | 'fields' => array('ref'), |
||
| 2637 | 'class' => 'compta/facture/class/facture.class.php', |
||
| 2638 | 'object' => 'Facture'), |
||
| 2639 | 'fournisseur/facture' => array('table' => 'facture_fourn', |
||
| 2640 | 'fields' => array('ref', 'ref_client'), |
||
| 2641 | 'class' => 'fourn/class/fournisseur.facture.class.php', |
||
| 2642 | 'object' => 'FactureFournisseur'), |
||
| 2643 | 'produit' => array('table' => 'product', |
||
| 2644 | 'fields' => array('ref'), |
||
| 2645 | 'class' => 'product/class/product.class.php', |
||
| 2646 | 'object' => 'Product'), |
||
| 2647 | 'productlot' => array('table' => 'product_lot', |
||
| 2648 | 'fields' => array('batch'), |
||
| 2649 | 'class' => 'product/stock/class/productlot.class.php', |
||
| 2650 | 'object' => 'Productlot'), |
||
| 2651 | 'projet' => array('table' => 'projet', |
||
| 2652 | 'fields' => array('ref'), |
||
| 2653 | 'class' => 'projet/class/projet.class.php', |
||
| 2654 | 'object' => 'Project'), |
||
| 2655 | 'projet_task' => array('table' => 'projet_task', |
||
| 2656 | 'fields' => array('ref'), |
||
| 2657 | 'class' => 'projet/class/task.class.php', |
||
| 2658 | 'object' => 'Task'), |
||
| 2659 | 'ressource' => array('table' => 'resource', |
||
| 2660 | 'fields' => array('ref'), |
||
| 2661 | 'class' => 'ressource/class/dolressource.class.php', |
||
| 2662 | 'object' => 'Dolresource'), |
||
| 2663 | 'bom' => array('table' => 'bom_bom', |
||
| 2664 | 'fields' => array('ref'), |
||
| 2665 | 'class' => 'bom/class/bom.class.php', |
||
| 2666 | 'object' => 'BOM'), |
||
| 2667 | 'mrp' => array('table' => 'mrp_mo', |
||
| 2668 | 'fields' => array('ref'), |
||
| 2669 | 'class' => 'mrp/class/mo.class.php', |
||
| 2670 | 'object' => 'Mo'), |
||
| 2671 | ); |
||
| 2672 | |||
| 2673 | if (!is_object($hookmanager)) { |
||
| 2674 | $hookmanager = new HookManager($this->db); |
||
| 2675 | } |
||
| 2676 | $hookmanager->initHooks(array('emailcolector')); |
||
| 2677 | $parameters = array('arrayobject' => $arrayobject); |
||
| 2678 | $reshook = $hookmanager->executeHooks('addmoduletoeamailcollectorjoinpiece', $parameters); // Note that $action and $object may have been modified by some hooks |
||
| 2679 | if ($reshook > 0) { |
||
| 2680 | $arrayobject = $hookmanager->resArray; |
||
| 2681 | } |
||
| 2682 | |||
| 2683 | $resultobj = array(); |
||
| 2684 | |||
| 2685 | foreach ($arrayobject as $key => $objectdesc) { |
||
| 2686 | $sql = 'SELECT DISTINCT t.rowid '; |
||
| 2687 | $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->db->sanitize($objectdesc['table']) . ' AS t'; |
||
| 2688 | $sql .= ' WHERE '; |
||
| 2689 | foreach ($objectdesc['fields'] as $field) { |
||
| 2690 | $sql .= "('" . $this->db->escape($subject) . "' LIKE CONCAT('%', t." . $this->db->sanitize($field) . ", '%') AND t." . $this->db->sanitize($field) . " <> '') OR "; |
||
| 2691 | } |
||
| 2692 | $sql = substr($sql, 0, -4); |
||
| 2693 | |||
| 2694 | $ressqlobj = $this->db->query($sql); |
||
| 2695 | if ($ressqlobj) { |
||
| 2696 | while ($obj = $this->db->fetch_object($ressqlobj)) { |
||
| 2697 | $resultobj[$key][] = $obj->rowid; |
||
| 2698 | } |
||
| 2699 | } |
||
| 2700 | } |
||
| 2701 | $dirs = array(); |
||
| 2702 | foreach ($resultobj as $mod => $ids) { |
||
| 2703 | $moddesc = $arrayobject[$mod]; |
||
| 2704 | $elementpath = $mod; |
||
| 2705 | dol_include_once($moddesc['class']); |
||
| 2706 | $objectmanaged = new $moddesc['object']($this->db); |
||
| 2707 | foreach ($ids as $val) { |
||
| 2708 | $res = $objectmanaged->fetch($val); |
||
| 2709 | if ($res) { |
||
| 2710 | $path = ($objectmanaged->entity > 1 ? "/" . $objectmanaged->entity : ''); |
||
| 2711 | $dirs[] = DOL_DATA_ROOT . $path . "/" . $elementpath . '/' . dol_sanitizeFileName($objectmanaged->ref) . '/'; |
||
| 2712 | } else { |
||
| 2713 | $this->errors[] = 'object not found'; |
||
| 2714 | } |
||
| 2715 | } |
||
| 2716 | } |
||
| 2717 | foreach ($dirs as $target) { |
||
| 2718 | $prefix = $this->actions[$this->id]['actionparam']; |
||
| 2719 | foreach ($data as $filename => $content) { |
||
| 2720 | $resr = saveAttachment($target, $prefix . '_' . $filename, $content); |
||
| 2721 | if ($resr == -1) { |
||
| 2722 | $this->errors[] = 'Doc not saved'; |
||
| 2723 | } |
||
| 2724 | } |
||
| 2725 | } |
||
| 2726 | |||
| 2727 | $operationslog .= '<br>Save attachment files on disk'; |
||
| 2728 | } else { |
||
| 2729 | $this->errors[] = 'no joined piece'; |
||
| 2730 | |||
| 2731 | $operationslog .= '<br>No joinded files'; |
||
| 2732 | } |
||
| 2733 | } elseif ($operation['type'] == 'project') { |
||
| 2734 | // Create project / lead |
||
| 2735 | $projecttocreate = new Project($this->db); |
||
| 2736 | $alreadycreated = $projecttocreate->fetch(0, '', '', $msgid); |
||
| 2737 | if ($alreadycreated == 0) { |
||
| 2738 | if ($thirdpartystatic->id > 0) { |
||
| 2739 | $projecttocreate->socid = $thirdpartystatic->id; |
||
| 2740 | if ($thirdpartyfoundby) { |
||
| 2741 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from ' . $thirdpartyfoundby); |
||
| 2742 | } |
||
| 2743 | } |
||
| 2744 | if ($contactstatic->id > 0) { |
||
| 2745 | $projecttocreate->contact_id = $contactstatic->id; |
||
| 2746 | if ($contactfoundby) { |
||
| 2747 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from ' . $contactfoundby); |
||
| 2748 | } |
||
| 2749 | } |
||
| 2750 | |||
| 2751 | $description = $descriptiontitle; |
||
| 2752 | $description = dol_concatdesc($description, "-----"); |
||
| 2753 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 2754 | $description = dol_concatdesc($description, "-----"); |
||
| 2755 | $description = dol_concatdesc($description, $messagetext); |
||
| 2756 | |||
| 2757 | $descriptionfull = $description; |
||
| 2758 | if (!getDolGlobalString('MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER')) { |
||
| 2759 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 2760 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 2761 | } |
||
| 2762 | |||
| 2763 | $id_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'rowid'); |
||
| 2764 | $percent_opp_status = dol_getIdFromCode($this->db, 'PROSP', 'c_lead_status', 'code', 'percent'); |
||
| 2765 | |||
| 2766 | $projecttocreate->title = $subject; |
||
| 2767 | $projecttocreate->date_start = $date; // date of email |
||
| 2768 | $projecttocreate->date_end = 0; |
||
| 2769 | $projecttocreate->opp_status = $id_opp_status; |
||
| 2770 | $projecttocreate->opp_percent = $percent_opp_status; |
||
| 2771 | $projecttocreate->description = dol_concatdesc(dolGetFirstLineOfText(dol_string_nohtmltag($description, 2), 10), '...' . $langs->transnoentities("SeePrivateNote") . '...'); |
||
| 2772 | $projecttocreate->note_private = $descriptionfull; |
||
| 2773 | $projecttocreate->entity = $conf->entity; |
||
| 2774 | $projecttocreate->email_msgid = $msgid; |
||
| 2775 | |||
| 2776 | $savesocid = $projecttocreate->socid; |
||
| 2777 | |||
| 2778 | // Overwrite values with values extracted from source email. |
||
| 2779 | // This may overwrite any $projecttocreate->xxx properties. |
||
| 2780 | $errorforthisaction = $this->overwritePropertiesOfObject($projecttocreate, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2781 | |||
| 2782 | // Set project ref if not yet defined |
||
| 2783 | if (empty($projecttocreate->ref)) { |
||
| 2784 | // Get next Ref |
||
| 2785 | $defaultref = ''; |
||
| 2786 | $modele = !getDolGlobalString('PROJECT_ADDON') ? 'mod_project_simple' : $conf->global->PROJECT_ADDON; |
||
| 2787 | |||
| 2788 | // Search template files |
||
| 2789 | $file = ''; |
||
| 2790 | $classname = ''; |
||
| 2791 | $reldir = ''; |
||
| 2792 | $dirmodels = array_merge(array('/'), (array)$conf->modules_parts['models']); |
||
| 2793 | foreach ($dirmodels as $reldir) { |
||
| 2794 | $file = dol_buildpath($reldir . "core/modules/project/" . $modele . '.php', 0); |
||
| 2795 | if (file_exists($file)) { |
||
| 2796 | $classname = $modele; |
||
| 2797 | break; |
||
| 2798 | } |
||
| 2799 | } |
||
| 2800 | |||
| 2801 | if ($classname !== '') { |
||
| 2802 | if ($savesocid > 0) { |
||
| 2803 | if ($savesocid != $projecttocreate->socid) { |
||
| 2804 | $errorforactions++; |
||
| 2805 | 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'); |
||
| 2806 | } |
||
| 2807 | } else { |
||
| 2808 | if ($projecttocreate->socid > 0) { |
||
| 2809 | $thirdpartystatic->fetch($projecttocreate->socid); |
||
| 2810 | } |
||
| 2811 | } |
||
| 2812 | |||
| 2813 | $result = dol_include_once($reldir . "core/modules/project/" . $modele . '.php'); |
||
| 2814 | $modModuleToUseForNextValue = new $classname(); |
||
| 2815 | $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $projecttocreate); |
||
| 2816 | } |
||
| 2817 | $projecttocreate->ref = $defaultref; |
||
| 2818 | } |
||
| 2819 | |||
| 2820 | |||
| 2821 | if ($errorforthisaction) { |
||
| 2822 | $errorforactions++; |
||
| 2823 | } else { |
||
| 2824 | if (empty($projecttocreate->ref) || (is_numeric($projecttocreate->ref) && $projecttocreate->ref <= 0)) { |
||
| 2825 | $errorforactions++; |
||
| 2826 | $this->error = 'Failed to create project: Can\'t get a valid value for the field ref with numbering template = ' . $modele . ', thirdparty id = ' . $thirdpartystatic->id; |
||
| 2827 | |||
| 2828 | $operationslog .= '<br>' . $this->error; |
||
| 2829 | } else { |
||
| 2830 | // Create project |
||
| 2831 | $result = $projecttocreate->create($user); |
||
| 2832 | if ($result <= 0) { |
||
| 2833 | $errorforactions++; |
||
| 2834 | $this->error = 'Failed to create project: ' . $langs->trans($projecttocreate->error); |
||
| 2835 | $this->errors = $projecttocreate->errors; |
||
| 2836 | |||
| 2837 | $operationslog .= '<br>' . $this->error; |
||
| 2838 | } else { |
||
| 2839 | if ($attachments) { |
||
| 2840 | $destdir = $conf->project->dir_output . '/' . $projecttocreate->ref; |
||
| 2841 | if (!dol_is_dir($destdir)) { |
||
| 2842 | dol_mkdir($destdir); |
||
| 2843 | } |
||
| 2844 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 2845 | foreach ($attachments as $attachment) { |
||
| 2846 | // $attachment->save($destdir.'/'); |
||
| 2847 | $typeattachment = (string)$attachment->getDisposition(); |
||
| 2848 | $filename = $attachment->getFilename(); |
||
| 2849 | $content = $attachment->getContent(); |
||
| 2850 | $this->saveAttachment($destdir, $filename, $content); |
||
| 2851 | } |
||
| 2852 | } else { |
||
| 2853 | $this->getmsg($connection, $imapemail, $destdir); |
||
| 2854 | } |
||
| 2855 | |||
| 2856 | $operationslog .= '<br>Project created with attachments -> id=' . dol_escape_htmltag($projecttocreate->id); |
||
| 2857 | } else { |
||
| 2858 | $operationslog .= '<br>Project created without attachments -> id=' . dol_escape_htmltag($projecttocreate->id); |
||
| 2859 | } |
||
| 2860 | } |
||
| 2861 | } |
||
| 2862 | } |
||
| 2863 | } else { |
||
| 2864 | dol_syslog("Project already exists for msgid = " . dol_escape_htmltag($msgid) . ", so we do not recreate it."); |
||
| 2865 | |||
| 2866 | $operationslog .= '<br>Project already exists for msgid =' . dol_escape_htmltag($msgid); |
||
| 2867 | } |
||
| 2868 | } elseif ($operation['type'] == 'ticket') { |
||
| 2869 | // Create ticket |
||
| 2870 | $tickettocreate = new Ticket($this->db); |
||
| 2871 | if ($ticketalreadyexists == 0) { |
||
| 2872 | if ($thirdpartystatic->id > 0) { |
||
| 2873 | $tickettocreate->socid = $thirdpartystatic->id; |
||
| 2874 | $tickettocreate->fk_soc = $thirdpartystatic->id; |
||
| 2875 | if ($thirdpartyfoundby) { |
||
| 2876 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Third party found from ' . $thirdpartyfoundby); |
||
| 2877 | } |
||
| 2878 | } |
||
| 2879 | if ($contactstatic->id > 0) { |
||
| 2880 | $tickettocreate->contact_id = $contactstatic->id; |
||
| 2881 | if ($contactfoundby) { |
||
| 2882 | $descriptionmeta = dol_concatdesc($descriptionmeta, 'Contact/address found from ' . $contactfoundby); |
||
| 2883 | } |
||
| 2884 | } |
||
| 2885 | |||
| 2886 | $description = $descriptiontitle; |
||
| 2887 | $description = dol_concatdesc($description, "-----"); |
||
| 2888 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 2889 | $description = dol_concatdesc($description, "-----"); |
||
| 2890 | $description = dol_concatdesc($description, $messagetext); |
||
| 2891 | |||
| 2892 | $descriptionfull = $description; |
||
| 2893 | if (!getDolGlobalString('MAIN_EMAILCOLLECTOR_MAIL_WITHOUT_HEADER')) { |
||
| 2894 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 2895 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 2896 | } |
||
| 2897 | |||
| 2898 | $tickettocreate->subject = $subject; |
||
| 2899 | $tickettocreate->message = $description; |
||
| 2900 | $tickettocreate->type_code = (getDolGlobalString('MAIN_EMAILCOLLECTOR_TICKET_TYPE_CODE') ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_TYPE_CODE : dol_getIdFromCode($this->db, 1, 'c_ticket_type', 'use_default', 'code', 1)); |
||
| 2901 | $tickettocreate->category_code = (getDolGlobalString('MAIN_EMAILCOLLECTOR_TICKET_CATEGORY_CODE') ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_CATEGORY_CODE : dol_getIdFromCode($this->db, 1, 'c_ticket_category', 'use_default', 'code', 1)); |
||
| 2902 | $tickettocreate->severity_code = (getDolGlobalString('MAIN_EMAILCOLLECTOR_TICKET_SEVERITY_CODE') ? $conf->global->MAIN_EMAILCOLLECTOR_TICKET_SEVERITY_CODE : dol_getIdFromCode($this->db, 1, 'c_ticket_severity', 'use_default', 'code', 1)); |
||
| 2903 | $tickettocreate->origin_email = $from; |
||
| 2904 | $tickettocreate->origin_replyto = (!empty($replyto) ? $replyto : null); |
||
| 2905 | $tickettocreate->origin_references = (!empty($headers['References']) ? $headers['References'] : null); |
||
| 2906 | $tickettocreate->fk_user_create = $user->id; |
||
| 2907 | $tickettocreate->datec = dol_now(); |
||
| 2908 | $tickettocreate->fk_project = $projectstatic->id; |
||
| 2909 | $tickettocreate->notify_tiers_at_create = getDolGlobalInt('TICKET_CHECK_NOTIFY_THIRDPARTY_AT_CREATION'); |
||
| 2910 | $tickettocreate->note_private = $descriptionfull; |
||
| 2911 | $tickettocreate->entity = $conf->entity; |
||
| 2912 | $tickettocreate->email_msgid = $msgid; |
||
| 2913 | $tickettocreate->email_date = $date; |
||
| 2914 | //$tickettocreate->fk_contact = $contactstatic->id; |
||
| 2915 | |||
| 2916 | $savesocid = $tickettocreate->socid; |
||
| 2917 | |||
| 2918 | // Overwrite values with values extracted from source email. |
||
| 2919 | // This may overwrite any $projecttocreate->xxx properties. |
||
| 2920 | $errorforthisaction = $this->overwritePropertiesOfObject($tickettocreate, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 2921 | |||
| 2922 | // Set ticket ref if not yet defined |
||
| 2923 | if (empty($tickettocreate->ref)) { |
||
| 2924 | // Get next Ref |
||
| 2925 | $defaultref = ''; |
||
| 2926 | $modele = getDolGlobalString('TICKET_ADDON', 'mod_ticket_simple'); |
||
| 2927 | |||
| 2928 | // Search template files |
||
| 2929 | $file = ''; |
||
| 2930 | $classname = ''; |
||
| 2931 | $reldir = ''; |
||
| 2932 | $dirmodels = array_merge(array('/'), (array)$conf->modules_parts['models']); |
||
| 2933 | foreach ($dirmodels as $reldir) { |
||
| 2934 | $file = dol_buildpath($reldir . "core/modules/ticket/" . $modele . '.php', 0); |
||
| 2935 | if (file_exists($file)) { |
||
| 2936 | $classname = $modele; |
||
| 2937 | break; |
||
| 2938 | } |
||
| 2939 | } |
||
| 2940 | |||
| 2941 | if ($classname !== '') { |
||
| 2942 | if ($savesocid > 0) { |
||
| 2943 | if ($savesocid != $tickettocreate->socid) { |
||
| 2944 | $errorforactions++; |
||
| 2945 | 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'); |
||
| 2946 | } |
||
| 2947 | } else { |
||
| 2948 | if ($tickettocreate->socid > 0) { |
||
| 2949 | $thirdpartystatic->fetch($tickettocreate->socid); |
||
| 2950 | } |
||
| 2951 | } |
||
| 2952 | |||
| 2953 | $result = dol_include_once($reldir . "core/modules/ticket/" . $modele . '.php'); |
||
| 2954 | $modModuleToUseForNextValue = new $classname(); |
||
| 2955 | $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $tickettocreate); |
||
| 2956 | } |
||
| 2957 | $tickettocreate->ref = $defaultref; |
||
| 2958 | } |
||
| 2959 | |||
| 2960 | if ($errorforthisaction) { |
||
| 2961 | $errorforactions++; |
||
| 2962 | } else { |
||
| 2963 | if (is_numeric($tickettocreate->ref) && $tickettocreate->ref <= 0) { |
||
| 2964 | $errorforactions++; |
||
| 2965 | $this->error = 'Failed to create ticket: Can\'t get a valid value for the field ref with numbering template = ' . $modele . ', thirdparty id = ' . $thirdpartystatic->id; |
||
| 2966 | } else { |
||
| 2967 | // Create ticket |
||
| 2968 | $result = $tickettocreate->create($user); |
||
| 2969 | if ($result <= 0) { |
||
| 2970 | $errorforactions++; |
||
| 2971 | $this->error = 'Failed to create ticket: ' . $langs->trans($tickettocreate->error); |
||
| 2972 | $this->errors = $tickettocreate->errors; |
||
| 2973 | } else { |
||
| 2974 | if ($attachments) { |
||
| 2975 | $destdir = $conf->ticket->dir_output . '/' . $tickettocreate->ref; |
||
| 2976 | if (!dol_is_dir($destdir)) { |
||
| 2977 | dol_mkdir($destdir); |
||
| 2978 | } |
||
| 2979 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 2980 | foreach ($attachments as $attachment) { |
||
| 2981 | // $attachment->save($destdir.'/'); |
||
| 2982 | $typeattachment = (string)$attachment->getDisposition(); |
||
| 2983 | $filename = $attachment->getFilename(); |
||
| 2984 | $content = $attachment->getContent(); |
||
| 2985 | $this->saveAttachment($destdir, $filename, $content); |
||
| 2986 | } |
||
| 2987 | } else { |
||
| 2988 | $this->getmsg($connection, $imapemail, $destdir); |
||
| 2989 | } |
||
| 2990 | |||
| 2991 | $operationslog .= '<br>Ticket created with attachments -> id=' . dol_escape_htmltag($tickettocreate->id); |
||
| 2992 | } else { |
||
| 2993 | $operationslog .= '<br>Ticket created without attachments -> id=' . dol_escape_htmltag($tickettocreate->id); |
||
| 2994 | } |
||
| 2995 | } |
||
| 2996 | } |
||
| 2997 | } |
||
| 2998 | } |
||
| 2999 | } elseif ($operation['type'] == 'candidature') { |
||
| 3000 | // Create candidature |
||
| 3001 | $candidaturetocreate = new RecruitmentCandidature($this->db); |
||
| 3002 | |||
| 3003 | $alreadycreated = $candidaturetocreate->fetch(0, '', $msgid); |
||
| 3004 | if ($alreadycreated == 0) { |
||
| 3005 | $description = $descriptiontitle; |
||
| 3006 | $description = dol_concatdesc($description, "-----"); |
||
| 3007 | $description = dol_concatdesc($description, $descriptionmeta); |
||
| 3008 | $description = dol_concatdesc($description, "-----"); |
||
| 3009 | $description = dol_concatdesc($description, $messagetext); |
||
| 3010 | |||
| 3011 | $descriptionfull = $description; |
||
| 3012 | $descriptionfull = dol_concatdesc($descriptionfull, "----- Header"); |
||
| 3013 | $descriptionfull = dol_concatdesc($descriptionfull, $header); |
||
| 3014 | |||
| 3015 | $candidaturetocreate->subject = $subject; |
||
| 3016 | $candidaturetocreate->message = $description; |
||
| 3017 | $candidaturetocreate->type_code = 0; |
||
| 3018 | $candidaturetocreate->category_code = null; |
||
| 3019 | $candidaturetocreate->severity_code = null; |
||
| 3020 | $candidaturetocreate->email = $from; |
||
| 3021 | //$candidaturetocreate->lastname = $langs->trans("Anonymous").' - '.$from; |
||
| 3022 | $candidaturetocreate->fk_user_creat = $user->id; |
||
| 3023 | $candidaturetocreate->date_creation = dol_now(); |
||
| 3024 | $candidaturetocreate->fk_project = $projectstatic->id; |
||
| 3025 | $candidaturetocreate->description = $description; |
||
| 3026 | $candidaturetocreate->note_private = $descriptionfull; |
||
| 3027 | $candidaturetocreate->entity = $conf->entity; |
||
| 3028 | $candidaturetocreate->email_msgid = $msgid; |
||
| 3029 | $candidaturetocreate->email_date = $date; // date of email |
||
| 3030 | $candidaturetocreate->status = $candidaturetocreate::STATUS_DRAFT; |
||
| 3031 | //$candidaturetocreate->fk_contact = $contactstatic->id; |
||
| 3032 | |||
| 3033 | // Overwrite values with values extracted from source email. |
||
| 3034 | // This may overwrite any $projecttocreate->xxx properties. |
||
| 3035 | $errorforthisaction = $this->overwritePropertiesOfObject($candidaturetocreate, $operation['actionparam'], $messagetext, $subject, $header, $operationslog); |
||
| 3036 | |||
| 3037 | // Set candidature ref if not yet defined |
||
| 3038 | /*if (empty($candidaturetocreate->ref)) We do not need this because we create object in draft status |
||
| 3039 | { |
||
| 3040 | // Get next Ref |
||
| 3041 | $defaultref = ''; |
||
| 3042 | $modele = empty($conf->global->CANDIDATURE_ADDON) ? 'mod_candidature_simple' : $conf->global->CANDIDATURE_ADDON; |
||
| 3043 | |||
| 3044 | // Search template files |
||
| 3045 | $file = ''; $classname = ''; $filefound = 0; $reldir = ''; |
||
| 3046 | $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); |
||
| 3047 | foreach ($dirmodels as $reldir) |
||
| 3048 | { |
||
| 3049 | $file = dol_buildpath($reldir."core/modules/ticket/".$modele.'.php', 0); |
||
| 3050 | if (file_exists($file)) { |
||
| 3051 | $filefound = 1; |
||
| 3052 | $classname = $modele; |
||
| 3053 | break; |
||
| 3054 | } |
||
| 3055 | } |
||
| 3056 | |||
| 3057 | if ($filefound) { |
||
| 3058 | if ($savesocid > 0) { |
||
| 3059 | if ($savesocid != $candidaturetocreate->socid) { |
||
| 3060 | $errorforactions++; |
||
| 3061 | setEventMessages('You loaded a thirdparty (id='.$savesocid.') and you force another thirdparty id (id='.$candidaturetocreate->socid.') by setting socid in operation with a different value', null, 'errors'); |
||
| 3062 | } |
||
| 3063 | } else { |
||
| 3064 | if ($candidaturetocreate->socid > 0) |
||
| 3065 | { |
||
| 3066 | $thirdpartystatic->fetch($candidaturetocreate->socid); |
||
| 3067 | } |
||
| 3068 | } |
||
| 3069 | |||
| 3070 | $result = dol_include_once($reldir."core/modules/ticket/".$modele.'.php'); |
||
| 3071 | $modModuleToUseForNextValue = new $classname; |
||
| 3072 | $defaultref = $modModuleToUseForNextValue->getNextValue(($thirdpartystatic->id > 0 ? $thirdpartystatic : null), $tickettocreate); |
||
| 3073 | } |
||
| 3074 | $candidaturetocreate->ref = $defaultref; |
||
| 3075 | }*/ |
||
| 3076 | |||
| 3077 | if ($errorforthisaction) { |
||
| 3078 | $errorforactions++; |
||
| 3079 | } else { |
||
| 3080 | // Create project |
||
| 3081 | $result = $candidaturetocreate->create($user); |
||
| 3082 | if ($result <= 0) { |
||
| 3083 | $errorforactions++; |
||
| 3084 | $this->error = 'Failed to create candidature: ' . implode(', ', $candidaturetocreate->errors); |
||
| 3085 | $this->errors = $candidaturetocreate->errors; |
||
| 3086 | } |
||
| 3087 | |||
| 3088 | $operationslog .= '<br>Candidature created without attachments -> id=' . dol_escape_htmltag($candidaturetocreate->id); |
||
| 3089 | } |
||
| 3090 | } |
||
| 3091 | } elseif (substr($operation['type'], 0, 4) == 'hook') { |
||
| 3092 | // Create event specific on hook |
||
| 3093 | // this code action is hook..... for support this call |
||
| 3094 | if (!is_object($hookmanager)) { |
||
| 3095 | $hookmanager = new HookManager($this->db); |
||
| 3096 | $hookmanager->initHooks(['emailcolector']); |
||
| 3097 | } |
||
| 3098 | |||
| 3099 | $parameters = array( |
||
| 3100 | 'connection' => $connection, |
||
| 3101 | 'imapemail' => $imapemail, |
||
| 3102 | 'overview' => $overview, |
||
| 3103 | |||
| 3104 | 'from' => $from, |
||
| 3105 | 'fromtext' => $fromtext, |
||
| 3106 | |||
| 3107 | 'actionparam' => $operation['actionparam'], |
||
| 3108 | |||
| 3109 | 'thirdpartyid' => $thirdpartyid, |
||
| 3110 | 'objectid' => $objectid, |
||
| 3111 | 'objectemail' => $objectemail, |
||
| 3112 | |||
| 3113 | 'messagetext' => $messagetext, |
||
| 3114 | 'subject' => $subject, |
||
| 3115 | 'header' => $header, |
||
| 3116 | 'attachments' => $attachments, |
||
| 3117 | ); |
||
| 3118 | $reshook = $hookmanager->executeHooks('doCollectImapOneCollector', $parameters, $this, $operation['type']); |
||
| 3119 | |||
| 3120 | if ($reshook < 0) { |
||
| 3121 | $errorforthisaction++; |
||
| 3122 | $this->error = $hookmanager->resPrint; |
||
| 3123 | } |
||
| 3124 | if ($errorforthisaction) { |
||
| 3125 | $errorforactions++; |
||
| 3126 | $operationslog .= '<br>Hook doCollectImapOneCollector executed with error'; |
||
| 3127 | } else { |
||
| 3128 | $operationslog .= '<br>Hook doCollectImapOneCollector executed without error'; |
||
| 3129 | } |
||
| 3130 | } |
||
| 3131 | |||
| 3132 | if (!$errorforactions) { |
||
| 3133 | $nbactiondoneforemail++; |
||
| 3134 | } |
||
| 3135 | } |
||
| 3136 | } |
||
| 3137 | |||
| 3138 | // Error for email or not ? |
||
| 3139 | if (!$errorforactions) { |
||
| 3140 | if (!empty($targetdir)) { |
||
| 3141 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 3142 | // Move mail using PHP-IMAP |
||
| 3143 | dol_syslog("EmailCollector::doCollectOneCollector move message " . ($imapemail->getHeader()->get('subject')) . " to " . $targetdir, LOG_DEBUG); |
||
| 3144 | |||
| 3145 | if (empty($mode)) { // $mode > 0 is test |
||
| 3146 | $operationslog .= '<br>Move mail ' . ($this->uidAsString($imapemail)) . ' - ' . $msgid . ' to ' . $targetdir; |
||
| 3147 | |||
| 3148 | $tmptargetdir = $targetdir; |
||
| 3149 | if (!getDolGlobalString('MAIL_DISABLE_UTF7_ENCODE_OF_DIR')) { |
||
| 3150 | $tmptargetdir = $this->getEncodedUtf7($targetdir); |
||
| 3151 | } |
||
| 3152 | |||
| 3153 | try { |
||
| 3154 | $result = $imapemail->move($tmptargetdir); |
||
| 3155 | } catch (Exception $e) { |
||
| 3156 | // Nothing to do. $result will remain 0 |
||
| 3157 | } |
||
| 3158 | if (empty($result)) { |
||
| 3159 | dol_syslog("Failed to move email into target directory " . $targetdir); |
||
| 3160 | $operationslog .= '<br>Failed to move email into target directory ' . $targetdir; |
||
| 3161 | $errorforemail++; |
||
| 3162 | } |
||
| 3163 | } else { |
||
| 3164 | $operationslog .= '<br>Do not move mail ' . ($this->uidAsString($imapemail)) . ' - ' . $msgid . ' (test mode)'; |
||
| 3165 | } |
||
| 3166 | } else { |
||
| 3167 | dol_syslog("EmailCollector::doCollectOneCollector move message " . ($this->uidAsString($imapemail)) . " to " . $connectstringtarget, LOG_DEBUG); |
||
| 3168 | $operationslog .= '<br>Move mail ' . ($this->uidAsString($imapemail)) . ' - ' . $msgid; |
||
| 3169 | |||
| 3170 | $arrayofemailtodelete[$imapemail] = $msgid; |
||
| 3171 | // Note: Real move is done later using $arrayofemailtodelete |
||
| 3172 | } |
||
| 3173 | } else { |
||
| 3174 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 3175 | dol_syslog("EmailCollector::doCollectOneCollector message '" . ($imapemail->getHeader()->get('subject')) . "' using this->host=" . $this->host . ", this->access_type=" . $this->acces_type . " was set to read", LOG_DEBUG); |
||
| 3176 | } else { |
||
| 3177 | dol_syslog("EmailCollector::doCollectOneCollector message " . ($this->uidAsString($imapemail)) . " to " . $connectstringtarget . " was set to read", LOG_DEBUG); |
||
| 3178 | } |
||
| 3179 | } |
||
| 3180 | } else { |
||
| 3181 | $errorforemail++; |
||
| 3182 | } |
||
| 3183 | |||
| 3184 | |||
| 3185 | unset($objectemail); |
||
| 3186 | unset($projectstatic); |
||
| 3187 | unset($thirdpartystatic); |
||
| 3188 | unset($contactstatic); |
||
| 3189 | |||
| 3190 | $nbemailprocessed++; |
||
| 3191 | |||
| 3192 | if (!$errorforemail) { |
||
| 3193 | $nbactiondone += $nbactiondoneforemail; |
||
| 3194 | $nbemailok++; |
||
| 3195 | |||
| 3196 | if (empty($mode)) { |
||
| 3197 | $this->db->commit(); |
||
| 3198 | } else { |
||
| 3199 | $this->db->rollback(); |
||
| 3200 | } |
||
| 3201 | |||
| 3202 | // Stop the loop to process email if we reach maximum collected per collect |
||
| 3203 | if ($this->maxemailpercollect > 0 && $nbemailok >= $this->maxemailpercollect) { |
||
| 3204 | dol_syslog("EmailCollect::doCollectOneCollector We reach maximum of " . $nbemailok . " collected with success, so we stop this collector now."); |
||
| 3205 | break; |
||
| 3206 | } |
||
| 3207 | } else { |
||
| 3208 | $error++; |
||
| 3209 | |||
| 3210 | $this->db->rollback(); |
||
| 3211 | } |
||
| 3212 | } |
||
| 3213 | |||
| 3214 | $output = $langs->trans('XEmailsDoneYActionsDone', $nbemailprocessed, $nbemailok, $nbactiondone); |
||
| 3215 | |||
| 3216 | dol_syslog("End of loop on emails", LOG_INFO, -1); |
||
| 3217 | } else { |
||
| 3218 | $langs->load("admin"); |
||
| 3219 | $output = $langs->trans('NoNewEmailToProcess'); |
||
| 3220 | $output .= ' (defaultlang=' . $langs->defaultlang . ')'; |
||
| 3221 | } |
||
| 3222 | |||
| 3223 | // Disconnect |
||
| 3224 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 3225 | $client->disconnect(); |
||
| 3226 | } else { |
||
| 3227 | foreach ($arrayofemailtodelete as $imapemail => $msgid) { |
||
| 3228 | dol_syslog("EmailCollect::doCollectOneCollector delete email " . $imapemail . " " . $msgid); |
||
| 3229 | |||
| 3230 | $operationslog .= "<br> delete email " . $imapemail . " " . $msgid; |
||
| 3231 | |||
| 3232 | if (empty($mode) && empty($error)) { |
||
| 3233 | $res = imap_mail_move($connection, $imapemail, $targetdir, CP_UID); |
||
| 3234 | if ($res == false) { |
||
| 3235 | // $errorforemail++; // Not in loop, not needed, not initialised |
||
| 3236 | $this->error = imap_last_error(); |
||
| 3237 | $this->errors[] = $this->error; |
||
| 3238 | |||
| 3239 | $operationslog .= '<br>Error in move ' . $this->error; |
||
| 3240 | |||
| 3241 | dol_syslog(imap_last_error()); |
||
| 3242 | } |
||
| 3243 | } |
||
| 3244 | } |
||
| 3245 | |||
| 3246 | if (empty($mode) && empty($error)) { |
||
| 3247 | dol_syslog("Expunge", LOG_DEBUG); |
||
| 3248 | $operationslog .= "<br>Expunge"; |
||
| 3249 | |||
| 3250 | imap_expunge($connection); // To validate all moves |
||
| 3251 | } |
||
| 3252 | imap_close($connection); |
||
| 3253 | } |
||
| 3254 | |||
| 3255 | $this->datelastresult = $now; |
||
| 3256 | $this->lastresult = $output; |
||
| 3257 | if (getDolGlobalString('MAIN_IMAP_USE_PHPIMAP')) { |
||
| 3258 | $this->debuginfo .= 'IMAP search array used : ' . $search; |
||
| 3259 | } else { |
||
| 3260 | $this->debuginfo .= 'IMAP search string used : ' . $search; |
||
| 3261 | } |
||
| 3262 | if ($searchhead) { |
||
| 3263 | $this->debuginfo .= '<br>Then search string into email header : ' . dol_escape_htmltag($searchhead); |
||
| 3264 | } |
||
| 3265 | if ($operationslog) { |
||
| 3266 | $this->debuginfo .= $operationslog; |
||
| 3267 | } |
||
| 3268 | |||
| 3269 | if (empty($error) && empty($mode)) { |
||
| 3270 | $this->datelastok = $now; |
||
| 3271 | } |
||
| 3272 | |||
| 3273 | if (!empty($this->errors)) { |
||
| 3274 | $this->lastresult .= "<br>" . implode("<br>", $this->errors); |
||
| 3275 | } |
||
| 3276 | $this->codelastresult = ($error ? 'KO' : 'OK'); |
||
| 3277 | |||
| 3278 | if (empty($mode)) { |
||
| 3279 | $this->update($user); |
||
| 3280 | } |
||
| 3281 | |||
| 3282 | dol_syslog("EmailCollector::doCollectOneCollector end", LOG_INFO); |
||
| 3283 | |||
| 3284 | return $error ? -1 : 1; |
||
| 3285 | } |
||
| 3862 |