xinputfile.modules.php 6.7 KB

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