dolibarr  17.0.4
reset-invalid-emails.php
Go to the documentation of this file.
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 
25 if (!defined('NOSESSION')) {
26  define('NOSESSION', '1');
27 }
28 if (!defined('MAXEMAILS')) {
29  define('MAXEMAILS', 100);
30 }
31 
32 $sapi_type = php_sapi_name();
33 $script_file = basename(__FILE__);
34 $path = __DIR__.'/';
35 
36 // Test if batch mode
37 if (substr($sapi_type, 0, 3) == 'cgi') {
38  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
39  exit(-1);
40 }
41 
42 if (!isset($argv[3]) || !$argv[3]) {
43  print "Usage: ".$script_file." inputfile-with-invalid-emails type [test|confirm]\n";
44  print "- inputfile-with-invalid-emails is a file with list of invalid email\n";
45  print "- type can be 'all' or 'thirdparties', 'contacts', 'members', 'users'\n";
46  exit(-1);
47 }
48 $fileofinvalidemail = $argv[1];
49 $type = $argv[2];
50 $mode = $argv[3];
51 
52 require_once $path."../../htdocs/master.inc.php";
53 require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
54 require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
55 
56 // Global variables
57 $version = DOL_VERSION;
58 $error = 0;
59 
60 
61 /*
62  * Main
63  */
64 
65 $user = new User($db);
66 
67 @set_time_limit(0);
68 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
69 
70 if (!in_array($type, array('all', 'thirdparties', 'contacts', 'users', 'members'))) {
71  print "Bad value for parameter type.\n";
72  exit(-1);
73 }
74 
75 if (!empty($dolibarr_main_db_readonly)) {
76  print "Error: instance in read-onyl mode\n";
77  exit(-1);
78 }
79 
80 $db->begin();
81 
82 
83 $myfile = fopen($fileofinvalidemail, "r");
84 if (!$myfile) {
85  echo "Failed to open file";
86  exit(-1);
87 }
88 
89 $tmp = 1;
90 $counter = 1;
91 $numerasedtotal = 0;
92 
93 while ($tmp != null) {
94  $groupofemails = array();
95  for ($i = 0; $i < MAXEMAILS; $i++) {
96  $tmp = fgets($myfile);
97  if ($tmp == null) {
98  break;
99  }
100  $groupofemails[$i] = trim($tmp, "\n");
101  }
102 
103  // Generate the string tp allow a mass update (with a limit of MAXEMAILS per request).
104  $emailsin = '';
105  foreach ($groupofemails as $email) {
106  $emailsin .= ($emailsin ? ", " : "")."'".$db->escape($email)."'";
107  }
108 
109  // For each groupofemail, we update tables to set email field to empty
110  $nbingroup = count($groupofemails);
111 
112  print "Process group of ".$nbingroup." emails (".$counter." - ".($counter + $nbingroup - 1)."), type = ".$type."\n";
113 
114  $numerased = 0;
115 
116  $sql_base = "UPDATE ".MAIN_DB_PREFIX;
117 
118  if ($type == 'all' || $type == 'users') {
119  // Loop on each record and update the email to null if email into $groupofemails
120  $sql = $sql_base."user as u SET u.email = NULL WHERE u.email IN (".$db->sanitize($emailsin, 1).");";
121  print "Try to update users, ";
122  $resql = $db->query($sql);
123  if (!$resql) {
124  dol_print_error($db);
125  }
126  $numerased += $db->affected_rows($resql);
127  }
128 
129  if ($type == 'all' || $type == 'thirdparties') {
130  // Loop on each record and update the email to null if email into $groupofemails
131  $sql = $sql_base."societe as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
132  print "Try to update thirdparties, ";
133  $resql = $db->query($sql);
134  if (!$resql) {
135  dol_print_error($db);
136  }
137  $numerased += $db->affected_rows($resql);
138  }
139 
140  if ($type == 'all' || $type == 'contacts') {
141  // Loop on each record and update the email to null if email into $groupofemails
142 
143  $sql = $sql_base."socpeople as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
144  print "Try to update contacts, ";
145  $resql = $db->query($sql);
146  if (!$resql) {
147  dol_print_error($db);
148  }
149  $numerased += $db->affected_rows($resql);
150  }
151 
152  if ($type == 'all' || $type == 'members') {
153  // Loop on each record and update the email to null if email into $groupofemails
154 
155  $sql = $sql_base."adherent as a SET a.email = NULL WHERE a.email IN (".$db->sanitize($emailsin, 1).");";
156  print "Try to update members, ";
157  $resql = $db->query($sql);
158  if (!$resql) {
159  dol_print_error($db);
160  }
161  $numerased += $db->affected_rows($resql);
162  }
163 
164  $numerasedtotal += $numerased;
165 
166  print $numerased." emails cleared.\n";
167  $counter = $counter + $nbingroup;
168 }
169 
170 if (!$error && $mode == 'confirm') {
171  print "Commit - ".$numerasedtotal." operations validated.\n";
172  $db->commit();
173 } else {
174  print "Rollback - ".$numerasedtotal." Operations canceled.\n";
175  $db->rollback();
176 }
177 
178 exit($error);
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
if(!defined( 'CSRFCHECK_WITH_TOKEN'))