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