dolibarr 21.0.0-alpha
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 * or see https://www.gnu.org/
18 */
19
25include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
27
28
33{
34 public $name = 'EmailsFromFile'; // Identifiant du module mailing
35 // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
36 public $desc = 'EMails from a file'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
37 public $require_module = array(); // Module mailing actif si modules require_module actifs
38 public $require_admin = 0; // Module mailing actif pour user admin ou non
39
43 public $picto = 'generic';
44 public $tooltip = 'UseFormatFileEmailToTarget';
45
46
52 public function __construct($db)
53 {
54 $this->db = $db;
55 }
56
57
66 public function getSqlArrayForStats()
67 {
68 global $langs;
69 $langs->load("users");
70
71 $statssql = array();
72 return $statssql;
73 }
74
75
84 public function getNbOfRecipients($sql = '')
85 {
86 return '';
87 }
88
89
96 public function url($id)
97 {
98 global $langs;
99 return $langs->trans('LineInFile', $id);
100 //' - '.$langs->trans("File").' '.dol_trunc(,12);
101 }
102
103
109 public function formFilter()
110 {
111 global $langs;
112
113 $s = '';
114 $maxfilesizearray = getMaxFileSizeArray();
115 $maxmin = $maxfilesizearray['maxmin'];
116 if ($maxmin > 0) {
117 $s .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
118 }
119 $s .= '<input type="file" name="username" class="flat">';
120 return $s;
121 }
122
123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
130 public function add_to_target($mailing_id)
131 {
132 // phpcs:enable
133 global $conf, $langs, $_FILES;
134
135 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
136
137 // For compatibility with Unix, MS-Dos or Macintosh
138 ini_set('auto_detect_line_endings', true);
139
140 $cibles = array();
141
142 $upload_dir = $conf->mailing->dir_temp;
143
144 if (dol_mkdir($upload_dir) >= 0) {
145 $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir."/".$_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
146 if (is_numeric($resupload) && $resupload > 0) {
147 $cpt = 0;
148
149 $file = $upload_dir."/".$_FILES['username']['name'];
150 $handle = @fopen($file, "r");
151 if ($handle) {
152 $i = 0;
153 $j = 0;
154
155 $old = '';
156 while (!feof($handle)) {
157 $cpt++;
158 $buffer = trim(fgets($handle));
159 $tab = explode(';', $buffer, 4);
160
161 $email = dol_string_nohtmltag($tab[0]);
162 $name = dol_string_nohtmltag(empty($tab[1]) ? '' : $tab[1]);
163 $firstname = dol_string_nohtmltag(empty($tab[2]) ? '' : $tab[2]);
164 $other = dol_string_nohtmltag(empty($tab[3]) ? '' : $tab[3]);
165
166 if (!empty($buffer)) {
167 //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
168 if (isValidEmail($email)) {
169 if ($old != $email) {
170 $cibles[$j] = array(
171 'email' => $email,
172 'lastname' => $name,
173 'firstname' => $firstname,
174 'other' => $other,
175 'source_url' => '',
176 'source_id' => '',
177 'source_type' => 'file'
178 );
179 $old = $email;
180 $j++;
181 }
182 } else {
183 $i++;
184 $langs->load("errors");
185 $msg = $langs->trans("ErrorFoundBadEmailInFile", $i, $cpt, $email);
186 if (!empty($msg)) {
187 $this->error = $msg;
188 } else {
189 $this->error = 'ErrorFoundBadEmailInFile '.$i.' '.$cpt.' '.$email; // We experience case where $langs->trans return an empty string.
190 }
191 }
192 }
193 }
194 fclose($handle);
195
196 if ($i > 0) {
197 return -$i;
198 }
199 } else {
200 $this->error = $langs->trans("ErrorFaildToOpenFile");
201 return -1;
202 }
203
204 dol_syslog(get_class($this)."::add_to_target mailing ".$cpt." targets found");
205 } else {
206 $langs->load("errors");
207 if ($resupload < 0) { // Unknown error
208 $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
209 } elseif (preg_match('/ErrorFileIsInfectedWithAVirus/', $resupload)) { // Files infected by a virus
210 $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
211 } else { // Known error
212 $this->error = '<div class="error">'.$langs->trans($resupload).'</div>';
213 }
214 }
215 }
216
217 ini_set('auto_detect_line_endings', false);
218
219 return parent::addTargetsToDatabase($mailing_id, $cibles);
220 }
221}
$id
Definition account.php:39
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, $varfiles='addedfile', $upload_dir='')
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.