dolibarr 21.0.0-beta
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26if (!defined('NOSESSION')) {
27 define('NOSESSION', '1');
28}
29if (!defined('MAXEMAILS')) {
30 define('MAXEMAILS', 100);
31}
32
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
35$path = __DIR__.'/';
36
37// Test if batch mode
38if (substr($sapi_type, 0, 3) == 'cgi') {
39 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
40 exit(1);
41}
42
43if (!isset($argv[3]) || !$argv[3]) {
44 print "Usage: ".$script_file." inputfile-with-invalid-emails type [test|confirm]\n";
45 print "- inputfile-with-invalid-emails is a file with list of invalid email\n";
46 print "- type can be 'all' or 'thirdparties', 'contacts', 'members', 'users'\n";
47 exit(1);
48}
49$fileofinvalidemail = $argv[1];
50$type = $argv[2];
51$mode = $argv[3];
52
53require_once $path."../../htdocs/master.inc.php";
54require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
55require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
56require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
63// Global variables
64$version = DOL_VERSION;
65$error = 0;
66
67if (!isModEnabled('mailing')) {
68 print 'Module Emailing not enabled';
69 exit(1);
70}
71
72$hookmanager->initHooks(array('cli'));
73
74
75/*
76 * Main
77 */
78
79$user = new User($db);
80
81@set_time_limit(0);
82print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
83
84if (!in_array($type, array('all', 'thirdparties', 'contacts', 'users', 'members'))) {
85 print "Bad value for parameter type.\n";
86 exit(1);
87}
88
89if (!empty($dolibarr_main_db_readonly)) {
90 print "Error: instance in read-onyl mode\n";
91 exit(1);
92}
93
94$db->begin();
95
96
97$myfile = fopen($fileofinvalidemail, "r");
98if (!$myfile) {
99 echo "Failed to open file";
100 exit(1);
101}
102
103$tmp = 1;
104$counter = 1;
105$numerasedtotal = 0;
106
107while ($tmp != null) {
108 $groupofemails = array();
109 for ($i = 0; $i < MAXEMAILS; $i++) {
110 $tmp = fgets($myfile);
111 if ($tmp == null) {
112 break;
113 }
114 $groupofemails[$i] = trim($tmp, "\n");
115 }
116
117 // Generate the string tp allow a mass update (with a limit of MAXEMAILS per request).
118 $emailsin = '';
119 foreach ($groupofemails as $email) {
120 $emailsin .= ($emailsin ? ", " : "")."'".$db->escape($email)."'";
121 }
122
123 // For each groupofemail, we update tables to set email field to empty
124 $nbingroup = count($groupofemails);
125
126 print "Process group of ".$nbingroup." emails (".$counter." - ".($counter + $nbingroup - 1)."), type = ".$type."\n";
127
128 $numerased = 0;
129
130 $sql_base = "UPDATE ".MAIN_DB_PREFIX;
131
132 if ($type == 'all' || $type == 'users') {
133 // Loop on each record and update the email to null if email into $groupofemails
134 $sql = $sql_base."user as u SET u.email = NULL WHERE u.email IN (".$db->sanitize($emailsin, 1).");";
135 print "Try to update users, ";
136 $resql = $db->query($sql);
137 if (!$resql) {
138 dol_print_error($db);
139 }
140 $numerased += $db->affected_rows($resql);
141 }
142
143 if ($type == 'all' || $type == 'thirdparties') {
144 // Loop on each record and update the email to null if email into $groupofemails
145 $sql = $sql_base."societe as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
146 print "Try to update thirdparties, ";
147 $resql = $db->query($sql);
148 if (!$resql) {
149 dol_print_error($db);
150 }
151 $numerased += $db->affected_rows($resql);
152 }
153
154 if ($type == 'all' || $type == 'contacts') {
155 // Loop on each record and update the email to null if email into $groupofemails
156
157 $sql = $sql_base."socpeople as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
158 print "Try to update contacts, ";
159 $resql = $db->query($sql);
160 if (!$resql) {
161 dol_print_error($db);
162 }
163 $numerased += $db->affected_rows($resql);
164 }
165
166 if ($type == 'all' || $type == 'members') {
167 // Loop on each record and update the email to null if email into $groupofemails
168
169 $sql = $sql_base."adherent as a SET a.email = NULL WHERE a.email IN (".$db->sanitize($emailsin, 1).");";
170 print "Try to update members, ";
171 $resql = $db->query($sql);
172 if (!$resql) {
173 dol_print_error($db);
174 }
175 $numerased += $db->affected_rows($resql);
176 }
177
178 $numerasedtotal += $numerased;
179
180 print $numerased." emails cleared.\n";
181 $counter = $counter + $nbingroup;
182}
183
184if (!$error && $mode == 'confirm') {
185 print "Commit - ".$numerasedtotal." operations validated.\n";
186 $db->commit();
187} else {
188 print "Rollback - ".$numerasedtotal." Operations canceled.\n";
189 $db->rollback();
190}
191
192exit($error);
Class to manage Dolibarr users.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...