reset-invalid-emails.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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')) define('NOSESSION', '1');
  24. if (!defined('MAXEMAILS')) define('MAXEMAILS', 100);
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path = __DIR__.'/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit(-1);
  32. }
  33. if (!isset($argv[3]) || !$argv[3]) {
  34. print "Usage: ".$script_file." inputfile-with-invalid-emails type [test|confirm]\n";
  35. print "- inputfile-with-invalid-emails is a file with list of invalid email\n";
  36. print "- type can be 'all' or 'thirdparties', 'contacts', 'members', 'users'\n";
  37. exit(-1);
  38. }
  39. $fileofinvalidemail = $argv[1];
  40. $type = $argv[2];
  41. $mode = $argv[3];
  42. require_once $path."../../htdocs/master.inc.php";
  43. require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
  44. require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
  45. // Global variables
  46. $version = DOL_VERSION;
  47. $error = 0;
  48. /*
  49. * Main
  50. */
  51. $user = new User($db);
  52. @set_time_limit(0);
  53. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  54. if (!in_array($type, array('thirdparties', 'contacts', 'users', 'members'))) {
  55. print "Bad value for parameter type.\n";
  56. exit(-1);
  57. }
  58. $db->begin();
  59. $myfile = fopen($fileofinvalidemail, "r");
  60. if (!$myfile)
  61. {
  62. echo "Failed to open file";
  63. exit(-1);
  64. }
  65. $tmp = 1;
  66. $counter = 1;
  67. $numerasedtotal = 0;
  68. while ($tmp != null)
  69. {
  70. $groupofemails = array();
  71. for ($i = 0; $i < MAXEMAILS; $i++)
  72. {
  73. $tmp = fgets($myfile);
  74. if ($tmp == null)
  75. {
  76. break;
  77. }
  78. $groupofemails[$i] = trim($tmp, "\n");
  79. }
  80. // Generate the string tp allow a mass update (with a limit of MAXEMAILS per request).
  81. $emailsin = '';
  82. foreach ($groupofemails as $email) {
  83. $emailsin .= ($emailsin ? ", " : "")."'".$db->escape($email)."'";
  84. }
  85. // For each groupofemail, we update tables to set email field to empty
  86. $nbingroup = count($groupofemails);
  87. print "Process group of ".$nbingroup." emails (".$counter." - ".($counter + $nbingroup - 1)."), type = ".$type."\n";
  88. $numerased = 0;
  89. $sql_base = "UPDATE ".MAIN_DB_PREFIX;
  90. if ($type == 'all' || $type == 'users')
  91. {
  92. // Loop on each record and update the email to null if email into $groupofemails
  93. $sql = $sql_base."user as u SET u.email = NULL WHERE u.email IN (".$emailsin.");";
  94. print "Try to update users, ";
  95. $resql = $db->query($sql);
  96. if (!$resql) {
  97. dol_print_error($db);
  98. }
  99. $numerased += $db->affected_rows($resql);
  100. }
  101. if ($type == 'all' || $type == 'thirdparties')
  102. {
  103. // Loop on each record and update the email to null if email into $groupofemails
  104. $sql = $sql_base."societe as s SET s.email = NULL WHERE s.email IN (".$emailsin.");";
  105. print "Try to update thirdparties, ";
  106. $resql = $db->query($sql);
  107. if (!$resql) {
  108. dol_print_error($db);
  109. }
  110. $numerased += $db->affected_rows($resql);
  111. }
  112. if ($type == 'all' || $type == 'contacts')
  113. {
  114. // Loop on each record and update the email to null if email into $groupofemails
  115. $sql = $sql_base."socpeople as s SET s.email = NULL WHERE s.email IN (".$emailsin.");";
  116. print "Try to update contacts, ";
  117. $resql = $db->query($sql);
  118. if (!$resql) {
  119. dol_print_error($db);
  120. }
  121. $numerased += $db->affected_rows($resql);
  122. }
  123. if ($type == 'all' || $type == 'members')
  124. {
  125. // Loop on each record and update the email to null if email into $groupofemails
  126. $sql = $sql_base."adherent as a SET a.email = NULL WHERE a.email IN (".$emailsin.");";
  127. print "Try to update members, ";
  128. $resql = $db->query($sql);
  129. if (!$resql) {
  130. dol_print_error($db);
  131. }
  132. $numerased += $db->affected_rows($resql);
  133. }
  134. $numerasedtotal += $numerased;
  135. print $numerased." emails cleared.\n";
  136. $counter = $counter + $nbingroup;
  137. }
  138. if (!$error && $mode == 'confirm') {
  139. print "Commit - ".$numerasedtotal." operations validated.\n";
  140. $db->commit();
  141. } else {
  142. print "Rollback - ".$numerasedtotal." Operations canceled.\n";
  143. $db->rollback();
  144. }
  145. exit($error);