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