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