dolibarr  9.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 <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
24 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
26 
27 
32 {
33  var $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  var $desc='EMails from a file'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
36  var $require_module=array(); // Module mailing actif si modules require_module actifs
37  var $require_admin=0; // Module mailing actif pour user admin ou non
38  var $picto='generic';
39  var $tooltip='UseFormatFileEmailToTarget';
40 
41 
47  function __construct($db)
48  {
49  $this->db=$db;
50  }
51 
52 
62  {
63  global $langs;
64  $langs->load("users");
65 
66  $statssql=array();
67  return $statssql;
68  }
69 
70 
79  function getNbOfRecipients($sql='')
80  {
81  return '';
82  }
83 
84 
91  function url($id)
92  {
93  global $langs;
94  return $langs->trans('LineInFile',$id);
95  //' - '.$langs->trans("File").' '.dol_trunc(,12);
96  }
97 
98 
104  function formFilter()
105  {
106  global $langs;
107 
108  $s='';
109  $s.='<input type="file" name="username" class="flat">';
110  return $s;
111  }
112 
113  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
121  function add_to_target($mailing_id,$filtersarray=array())
122  {
123  // phpcs:enable
124  global $conf,$langs,$_FILES;
125 
126  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
127 
128  // For compatibility with Unix, MS-Dos or Macintosh
129  ini_set('auto_detect_line_endings', true);
130 
131  $cibles = array();
132 
133  $upload_dir=$conf->mailing->dir_temp;
134 
135  if (dol_mkdir($upload_dir) >= 0)
136  {
137  $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir . "/" . $_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
138  if (is_numeric($resupload) && $resupload > 0)
139  {
140  $cpt=0;
141 
142  $file=$upload_dir . "/" . $_FILES['username']['name'];
143  $handle = @fopen($file, "r");
144  if ($handle)
145  {
146  $i = 0;
147  $j = 0;
148 
149  $old = '';
150  while (!feof($handle))
151  {
152  $cpt++;
153  $buffer = trim(fgets($handle));
154  $tab=explode(';',$buffer,4);
155  $email=$tab[0];
156  $name=$tab[1];
157  $firstname=$tab[2];
158  $other=$tab[3];
159  if (! empty($buffer))
160  {
161  //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
162  $id=$cpt;
163  if (isValidEMail($email))
164  {
165  if ($old <> $email)
166  {
167  $cibles[$j] = array(
168  'email' => $email,
169  'lastname' => $name,
170  'firstname' => $firstname,
171  'other' => $other,
172  'source_url' => '',
173  'source_id' => '',
174  'source_type' => 'file'
175  );
176  $old = $email;
177  $j++;
178  }
179  }
180  else
181  {
182  $i++;
183  $langs->load("errors");
184  $this->error = $langs->trans("ErrorFoundBadEmailInFile",$i,$cpt,$email);
185  }
186  }
187  }
188  fclose($handle);
189 
190  if ($i > 0)
191  {
192  return -$i;
193  }
194  }
195  else
196  {
197  $this->error = $langs->trans("ErrorFaildToOpenFile");
198  return -1;
199  }
200 
201  dol_syslog(get_class($this)."::add_to_target mailing ".$cpt." targets found");
202  }
203  else
204  {
205  $langs->load("errors");
206  if ($resupload < 0) // Unknown error
207  {
208  $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
209  }
210  else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
211  {
212  $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
213  }
214  else // Known error
215  {
216  $this->error = '<div class="error">'.$langs->trans($resupload).'</div>';
217  }
218  }
219  }
220 
221  ini_set('auto_detect_line_endings', false);
222 
223  return parent::add_to_target($mailing_id, $cibles);
224  }
225 }
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile')
Make control on an uploaded file from an GUI page and move it to final destination.
Definition: files.lib.php:996
Class to offer a selector of emailing targets with Rule &#39;xinputfile&#39;.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target($mailing_id, $filtersarray=array())
Ajoute destinataires dans table des cibles.
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Parent class of emailing target selectors modules.