dolibarr  13.0.2
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 
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  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  $s .= '<input type="file" name="username" class="flat">';
114  return $s;
115  }
116 
117  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
124  public function add_to_target($mailing_id)
125  {
126  // phpcs:enable
127  global $conf, $langs, $_FILES;
128 
129  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
130 
131  // For compatibility with Unix, MS-Dos or Macintosh
132  ini_set('auto_detect_line_endings', true);
133 
134  $cibles = array();
135 
136  $upload_dir = $conf->mailing->dir_temp;
137 
138  if (dol_mkdir($upload_dir) >= 0)
139  {
140  $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir."/".$_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
141  if (is_numeric($resupload) && $resupload > 0)
142  {
143  $cpt = 0;
144 
145  $file = $upload_dir."/".$_FILES['username']['name'];
146  $handle = @fopen($file, "r");
147  if ($handle)
148  {
149  $i = 0;
150  $j = 0;
151 
152  $old = '';
153  while (!feof($handle))
154  {
155  $cpt++;
156  $buffer = trim(fgets($handle));
157  $tab = explode(';', $buffer, 4);
158  $email = $tab[0];
159  $name = $tab[1];
160  $firstname = $tab[2];
161  $other = $tab[3];
162  if (!empty($buffer))
163  {
164  //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
165  if (isValidEMail($email))
166  {
167  if ($old <> $email)
168  {
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)) $this->error = $msg;
186  else $this->error = 'ErrorFoundBadEmailInFile '.$i.' '.$cpt.' '.$email; // We experience case where $langs->trans return an empty string.
187  }
188  }
189  }
190  fclose($handle);
191 
192  if ($i > 0)
193  {
194  return -$i;
195  }
196  } else {
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  } else {
203  $langs->load("errors");
204  if ($resupload < 0) // Unknown error
205  {
206  $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
207  } elseif (preg_match('/ErrorFileIsInfectedWithAVirus/', $resupload)) // Files infected by a virus
208  {
209  $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
210  } else // Known error
211  {
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 }
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
getNbOfRecipients($sql= '')
Return here number of distinct emails returned by your selector.
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.
__construct($db)
Constructor.
$conf db
API class for accounts.
Definition: inc.php:54
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles= 'addedfile', $upload_dir= '')
Make control on an uploaded file from an GUI page and move it to final destination.
Definition: files.lib.php:999
add_to_target($mailing_id)
Ajoute destinataires dans table des cibles.
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
Parent class of emailing target selectors modules.