reset-invalid-emails.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file scripts/emailings/reset-invalid-emails.php
  20. * \ingroup mailing
  21. * \brief Script to reset (set email to empty) from a list of email
  22. */
  23. if (!defined('NOSESSION')) {
  24. define('NOSESSION', '1');
  25. }
  26. if (!defined('MAXEMAILS')) {
  27. define('MAXEMAILS', 100);
  28. }
  29. $sapi_type = php_sapi_name();
  30. $script_file = basename(__FILE__);
  31. $path = __DIR__.'/';
  32. // Test if batch mode
  33. if (substr($sapi_type, 0, 3) == 'cgi') {
  34. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  35. exit(-1);
  36. }
  37. if (!isset($argv[3]) || !$argv[3]) {
  38. print "Usage: ".$script_file." inputfile-with-invalid-emails type [test|confirm]\n";
  39. print "- inputfile-with-invalid-emails is a file with list of invalid email\n";
  40. print "- type can be 'all' or 'thirdparties', 'contacts', 'members', 'users'\n";
  41. exit(-1);
  42. }
  43. $fileofinvalidemail = $argv[1];
  44. $type = $argv[2];
  45. $mode = $argv[3];
  46. require_once $path."../../htdocs/master.inc.php";
  47. require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
  48. require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
  49. // Global variables
  50. $version = DOL_VERSION;
  51. $error = 0;
  52. if (!isModEnabled('mailing')) {
  53. print 'Module Emailing not enabled';
  54. exit(-1);
  55. }
  56. /*
  57. * Main
  58. */
  59. $user = new User($db);
  60. @set_time_limit(0);
  61. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  62. if (!in_array($type, array('all', 'thirdparties', 'contacts', 'users', 'members'))) {
  63. print "Bad value for parameter type.\n";
  64. exit(-1);
  65. }
  66. if (!empty($dolibarr_main_db_readonly)) {
  67. print "Error: instance in read-onyl mode\n";
  68. exit(-1);
  69. }
  70. $db->begin();
  71. $myfile = fopen($fileofinvalidemail, "r");
  72. if (!$myfile) {
  73. echo "Failed to open file";
  74. exit(-1);
  75. }
  76. $tmp = 1;
  77. $counter = 1;
  78. $numerasedtotal = 0;
  79. while ($tmp != null) {
  80. $groupofemails = array();
  81. for ($i = 0; $i < MAXEMAILS; $i++) {
  82. $tmp = fgets($myfile);
  83. if ($tmp == null) {
  84. break;
  85. }
  86. $groupofemails[$i] = trim($tmp, "\n");
  87. }
  88. // Generate the string tp allow a mass update (with a limit of MAXEMAILS per request).
  89. $emailsin = '';
  90. foreach ($groupofemails as $email) {
  91. $emailsin .= ($emailsin ? ", " : "")."'".$db->escape($email)."'";
  92. }
  93. // For each groupofemail, we update tables to set email field to empty
  94. $nbingroup = count($groupofemails);
  95. print "Process group of ".$nbingroup." emails (".$counter." - ".($counter + $nbingroup - 1)."), type = ".$type."\n";
  96. $numerased = 0;
  97. $sql_base = "UPDATE ".MAIN_DB_PREFIX;
  98. if ($type == 'all' || $type == 'users') {
  99. // Loop on each record and update the email to null if email into $groupofemails
  100. $sql = $sql_base."user as u SET u.email = NULL WHERE u.email IN (".$db->sanitize($emailsin, 1).");";
  101. print "Try to update users, ";
  102. $resql = $db->query($sql);
  103. if (!$resql) {
  104. dol_print_error($db);
  105. }
  106. $numerased += $db->affected_rows($resql);
  107. }
  108. if ($type == 'all' || $type == 'thirdparties') {
  109. // Loop on each record and update the email to null if email into $groupofemails
  110. $sql = $sql_base."societe as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
  111. print "Try to update thirdparties, ";
  112. $resql = $db->query($sql);
  113. if (!$resql) {
  114. dol_print_error($db);
  115. }
  116. $numerased += $db->affected_rows($resql);
  117. }
  118. if ($type == 'all' || $type == 'contacts') {
  119. // Loop on each record and update the email to null if email into $groupofemails
  120. $sql = $sql_base."socpeople as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
  121. print "Try to update contacts, ";
  122. $resql = $db->query($sql);
  123. if (!$resql) {
  124. dol_print_error($db);
  125. }
  126. $numerased += $db->affected_rows($resql);
  127. }
  128. if ($type == 'all' || $type == 'members') {
  129. // Loop on each record and update the email to null if email into $groupofemails
  130. $sql = $sql_base."adherent as a SET a.email = NULL WHERE a.email IN (".$db->sanitize($emailsin, 1).");";
  131. print "Try to update members, ";
  132. $resql = $db->query($sql);
  133. if (!$resql) {
  134. dol_print_error($db);
  135. }
  136. $numerased += $db->affected_rows($resql);
  137. }
  138. $numerasedtotal += $numerased;
  139. print $numerased." emails cleared.\n";
  140. $counter = $counter + $nbingroup;
  141. }
  142. if (!$error && $mode == 'confirm') {
  143. print "Commit - ".$numerasedtotal." operations validated.\n";
  144. $db->commit();
  145. } else {
  146. print "Rollback - ".$numerasedtotal." Operations canceled.\n";
  147. $db->rollback();
  148. }
  149. exit($error);