dolibarr 23.0.3
xinputfile.modules.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
28
29
34{
35 public $name = 'EmailsFromFile'; // Identifiant du module mailing
36 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
37 public $desc = 'EMails from a file'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouvée
38
42 public $require_module = array(); // Module mailing actif si modules require_module actifs
43
47 public $require_admin = 0; // Module mailing actif pour user admin ou non
48
52 public $picto = 'generic';
53 public $tooltip = 'UseFormatFileEmailToTarget';
54
55
61 public function __construct($db)
62 {
63 $this->db = $db;
64 }
65
66
75 public function getSqlArrayForStats()
76 {
77 global $langs;
78 $langs->load("users");
79
80 $statssql = array();
81 return $statssql;
82 }
83
84
93 public function getNbOfRecipients($sql = '')
94 {
95 return '';
96 }
97
98
105 public function url($id)
106 {
107 global $langs;
108 return $langs->trans('LineInFile', $id);
109 //' - '.$langs->trans("File").' '.dol_trunc(,12);
110 }
111
112
118 public function formFilter()
119 {
120 global $langs;
121
122 $s = '';
123 $maxfilesizearray = getMaxFileSizeArray();
124 $maxmin = $maxfilesizearray['maxmin'];
125 if ($maxmin > 0) {
126 $s .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
127 }
128 $s .= '<input type="file" name="username" class="flat">';
129 return $s;
130 }
131
132 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
139 public function add_to_target($mailing_id)
140 {
141 // phpcs:enable
142 global $conf, $langs, $_FILES;
143
144 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
145
146 // For compatibility with Unix, MS-Dos or Macintosh
147 ini_set('auto_detect_line_endings', true);
148
149 $cibles = array();
150
151 $upload_dir = $conf->mailing->dir_temp;
152
153 if (dol_mkdir($upload_dir) >= 0) {
154 $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir."/".$_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
155 if (is_numeric($resupload) && $resupload > 0) {
156 $cpt = 0;
157
158 $file = $upload_dir."/".$_FILES['username']['name'];
159 $handle = @fopen($file, "r");
160 if ($handle) {
161 $i = 0;
162 $j = 0;
163
164 $old = '';
165 while (!feof($handle)) {
166 $cpt++;
167 $buffer = trim(fgets($handle));
168 $tab = explode(';', $buffer, 4);
169
170 $email = dol_string_nohtmltag($tab[0]);
171 $name = dol_string_nohtmltag(empty($tab[1]) ? '' : $tab[1]);
172 $firstname = dol_string_nohtmltag(empty($tab[2]) ? '' : $tab[2]);
173 $other = dol_string_nohtmltag(empty($tab[3]) ? '' : $tab[3]);
174
175 if (!empty($buffer)) {
176 //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
177 if (isValidEmail($email)) {
178 if ($old != $email) {
179 $cibles[$j] = array(
180 'email' => $email,
181 'lastname' => $name,
182 'firstname' => $firstname,
183 'other' => $other,
184 'source_url' => '',
185 'source_id' => 0,
186 'source_type' => 'file'
187 );
188 $old = $email;
189 $j++;
190 }
191 } else {
192 $i++;
193 $langs->load("errors");
194 $msg = $langs->trans("ErrorFoundBadEmailInFile", $i, $cpt, $email);
195 if (!empty($msg)) {
196 $this->error = $msg;
197 } else {
198 $this->error = 'ErrorFoundBadEmailInFile '.$i.' '.$cpt.' '.$email; // We experience case where $langs->trans return an empty string.
199 }
200 }
201 }
202 }
203 fclose($handle);
204
205 if ($i > 0) {
206 return -$i;
207 }
208 } else {
209 $this->error = $langs->trans("ErrorFaildToOpenFile");
210 return -1;
211 }
212
213 dol_syslog(get_class($this)."::add_to_target mailing ".$cpt." targets found");
214 } else {
215 $langs->load("errors");
216 if ($resupload < 0) { // Unknown error
217 $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
218 } elseif (preg_match('/ErrorFileIsInfectedWithAVirus/', $resupload)) { // Files infected by a virus
219 $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
220 } else { // Known error
221 $this->error = '<div class="error">'.$langs->trans($resupload).'</div>';
222 }
223 }
224 }
225
226 ini_set('auto_detect_line_endings', false);
227
228 return parent::addTargetsToDatabase($mailing_id, $cibles);
229 }
230}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
Parent class of emailing target selectors modules.
Class to offer a selector of emailing targets with Rule 'xinputfile'.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
url($id)
Provide the URL to the car of the source information of the recipient for the mailing.
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings.
__construct($db)
Constructor.
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $keyforsourcefile='addedfile', $upload_dir='', $mode=0)
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
isValidEmail($address, $acceptsupervisorkey=0, $acceptuserkey=0)
Return true if email syntax is ok.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
getMaxFileSizeArray()
Return the max allowed for file upload.