PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/modules/mailings/peche.modules.php

https://github.com/asterix14/dolibarr
PHP | 224 lines | 130 code | 30 blank | 64 comment | 12 complexity | 93648fb4366adb895fa3f5e0d85f7fe7 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005-2009 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 2 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. /**
  19. * \file htdocs/core/modules/mailings/peche.modules.php
  20. * \ingroup mailing
  21. * \brief File of class to offer a selector of emailing targets with Rule 'Peche'.
  22. */
  23. include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
  24. require_once(DOL_DOCUMENT_ROOT."/core/lib/functions2.lib.php");
  25. /**
  26. * \class mailing_peche
  27. * \brief Class to offer a selector of emailing targets with Rule 'Peche'.
  28. */
  29. class mailing_peche extends MailingTargets
  30. {
  31. var $name='EmailsFromFile'; // Identifiant du module mailing
  32. var $desc='EMails issus d\'un fichier'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
  33. var $require_module=array(); // Module mailing actif si modules require_module actifs
  34. var $require_admin=1; // Module mailing actif pour user admin ou non
  35. var $picto='generic';
  36. var $db;
  37. /**
  38. * Constructor
  39. *
  40. * @param DoliDB $db Database handler
  41. */
  42. function mailing_peche($db)
  43. {
  44. $this->db=$db;
  45. }
  46. /**
  47. * On the main mailing area, there is a box with statistics.
  48. * If you want to add a line in this report you must provide an
  49. * array of SQL request that returns two field:
  50. * One called "label", One called "nb".
  51. *
  52. * @return array Array with SQL requests
  53. */
  54. function getSqlArrayForStats()
  55. {
  56. global $langs;
  57. $langs->load("users");
  58. $statssql=array();
  59. return $statssql;
  60. }
  61. /*
  62. * \brief Return here number of distinct emails returned by your selector.
  63. * For example if this selector is used to extract 500 different
  64. * emails from a text file, this function must return 500.
  65. * \return int '' means NA
  66. */
  67. function getNbOfRecipients()
  68. {
  69. return '';
  70. }
  71. /**
  72. * \brief Renvoie url lien vers fiche de la source du destinataire du mailing
  73. * \return string Url lien
  74. */
  75. function url($id)
  76. {
  77. global $langs;
  78. return $langs->trans('LineInFile',$id);
  79. //' - '.$langs->trans("File").' '.dol_trunc(,12);
  80. }
  81. /**
  82. * \brief Affiche formulaire de filtre qui apparait dans page de selection
  83. * des destinataires de mailings
  84. * \return string Retourne zone select
  85. */
  86. function formFilter()
  87. {
  88. global $langs;
  89. $s='';
  90. $s.='<input type="file" name="username" class="flat">';
  91. return $s;
  92. }
  93. /**
  94. * \brief Ajoute destinataires dans table des cibles
  95. * \param mailing_id Id of emailing
  96. * \param filterarray Requete sql de selection des destinataires
  97. * \return int < 0 si erreur, nb ajout si ok
  98. */
  99. function add_to_target($mailing_id,$filtersarray=array())
  100. {
  101. global $conf,$langs,$_FILES;
  102. require_once(DOL_DOCUMENT_ROOT."/core/lib/files.lib.php");
  103. // For compatibility with Unix, MS-Dos or Macintosh
  104. ini_set('auto_detect_line_endings', true);
  105. $cibles = array();
  106. $upload_dir=$conf->mailing->dir_temp;
  107. if (create_exdir($upload_dir) >= 0)
  108. {
  109. $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir . "/" . $_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
  110. if (is_numeric($resupload) && $resupload > 0)
  111. {
  112. $cpt=0;
  113. //$mesg = '<div class="ok">'.$langs->trans("FileTransferComplete").'</div>';
  114. //print_r($_FILES);
  115. $file=$upload_dir . "/" . $_FILES['username']['name'];
  116. $handle = @fopen($file, "r");
  117. if ($handle)
  118. {
  119. $i = 0;
  120. $j = 0;
  121. $old = '';
  122. while (!feof($handle))
  123. {
  124. $cpt++;
  125. $buffer = trim(fgets($handle));
  126. $tab=explode(';',$buffer,4);
  127. $email=$tab[0];
  128. $name=$tab[1];
  129. $firstname=$tab[2];
  130. $other=$tab[3];
  131. if (! empty($buffer))
  132. {
  133. //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
  134. $id=$cpt;
  135. if (isValidEMail($email))
  136. {
  137. if ($old <> $email)
  138. {
  139. $cibles[$j] = array(
  140. 'email' => $email,
  141. 'name' => $name,
  142. 'firstname' => $firstname,
  143. 'other' => $other,
  144. 'source_url' => '',
  145. 'source_id' => '',
  146. 'source_type' => 'file'
  147. );
  148. $old = $email;
  149. $j++;
  150. }
  151. }
  152. else
  153. {
  154. $i++;
  155. $langs->load("errors");
  156. $this->error = $langs->trans("ErrorFoundBadEmailInFile",$i,$cpt,$email);
  157. }
  158. }
  159. }
  160. fclose($handle);
  161. if ($i > 0)
  162. {
  163. return -$i;
  164. }
  165. }
  166. else
  167. {
  168. $this->error = $langs->trans("ErrorFaildToOpenFile");
  169. return -1;
  170. }
  171. dol_syslog(get_class($this)."::add_to_target mailing ".$cpt." targets found");
  172. }
  173. else
  174. {
  175. $langs->load("errors");
  176. if ($resupload < 0) // Unknown error
  177. {
  178. $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
  179. }
  180. else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
  181. {
  182. $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
  183. }
  184. else // Known error
  185. {
  186. $this->error = '<div class="error">'.$langs->trans($resupload).'</div>';
  187. }
  188. }
  189. }
  190. ini_set('auto_detect_line_endings', false);
  191. return parent::add_to_target($mailing_id, $cibles);
  192. }
  193. }
  194. ?>