reset-invalid-emails.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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[2]) || !$argv[2]) {
  34. print "Usage: ".$script_file." inputfile-with-invalid-emails type\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. require_once $path."../../htdocs/master.inc.php";
  42. require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
  43. require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
  44. // Global variables
  45. $version = DOL_VERSION;
  46. $error = 0;
  47. /*
  48. * Main
  49. */
  50. @set_time_limit(0);
  51. print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
  52. $user = new User($db);
  53. // for signature, we use user send as parameter
  54. if (!empty($login))
  55. $user->fetch('', $login);
  56. $db->begin();
  57. $myfile = fopen($fileofinvalidemail, "r");
  58. if (!$myfile)
  59. {
  60. echo "Failed to open file";
  61. exit(-1);
  62. }
  63. $tmp = 1;
  64. while($tmp!=null)
  65. {
  66. $groupofemails=array();
  67. for ($i=0; $i < MAXEMAILS; $i++)
  68. {
  69. $tmp =fgets($myfile);
  70. if ($tmp == null)
  71. {
  72. break;
  73. }
  74. $groupofemails[$i] = trim($tmp, "\n");
  75. }
  76. // For each groupofemail, we update tables to set email field to empty
  77. $sql_base = "UPDATE ".MAIN_DB_PREFIX;
  78. foreach ($groupofemails as $email)
  79. {
  80. if ($type == 'all' || $type == 'thirdparty')
  81. {
  82. // Loop on each record and update the email to null if email into $groupofemails
  83. $sql=$sql_base."societe as s SET s.email = NULL WHERE s.email = '".$db->escape($email)."';";
  84. $db->query($sql);
  85. }
  86. if ($type == 'all' || $type == 'contact')
  87. {
  88. // Loop on each record and update the email to null if email into $groupofemails
  89. $sql=$sql_base."socpeople as s SET s.email = NULL WHERE s.email = '".$db->escape($email)."';";
  90. $db->query($sql);
  91. }
  92. if ($type == 'all' || $type == 'user')
  93. {
  94. // Loop on each record and update the email to null if email into $groupofemails
  95. $sql=$sql_base."user as u SET u.email = NULL WHERE u.email = '".$db->escape($email)."';";
  96. $db->query($sql);
  97. }
  98. if ($type == 'all' || $type == 'member')
  99. {
  100. // Loop on each record and update the email to null if email into $groupofemails
  101. $sql=$sql_base."adherent as a SET a.email = NULL WHERE a.email = '".$db->escape($email)."';";
  102. $resql=$db->query($sql);
  103. }
  104. echo $email;
  105. }
  106. }
  107. if (!$error) {
  108. $db->commit();
  109. } else {
  110. $db->rollback();
  111. }
  112. exit($error);