dolibarr 18.0.6
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
25if (!defined('NOSESSION')) {
26 define('NOSESSION', '1');
27}
28if (!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
37if (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
42if (!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
52require_once $path."../../htdocs/master.inc.php";
53require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php";
54require_once DOL_DOCUMENT_ROOT."/comm/mailing/class/mailing.class.php";
55
56// Global variables
57$version = DOL_VERSION;
58$error = 0;
59
60if (!isModEnabled('mailing')) {
61 print 'Module Emailing not enabled';
62 exit(-1);
63}
64
65
66/*
67 * Main
68 */
69
70$user = new User($db);
71
72@set_time_limit(0);
73print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
74
75if (!in_array($type, array('all', 'thirdparties', 'contacts', 'users', 'members'))) {
76 print "Bad value for parameter type.\n";
77 exit(-1);
78}
79
80if (!empty($dolibarr_main_db_readonly)) {
81 print "Error: instance in read-onyl mode\n";
82 exit(-1);
83}
84
85$db->begin();
86
87
88$myfile = fopen($fileofinvalidemail, "r");
89if (!$myfile) {
90 echo "Failed to open file";
91 exit(-1);
92}
93
94$tmp = 1;
95$counter = 1;
96$numerasedtotal = 0;
97
98while ($tmp != null) {
99 $groupofemails = array();
100 for ($i = 0; $i < MAXEMAILS; $i++) {
101 $tmp = fgets($myfile);
102 if ($tmp == null) {
103 break;
104 }
105 $groupofemails[$i] = trim($tmp, "\n");
106 }
107
108 // Generate the string tp allow a mass update (with a limit of MAXEMAILS per request).
109 $emailsin = '';
110 foreach ($groupofemails as $email) {
111 $emailsin .= ($emailsin ? ", " : "")."'".$db->escape($email)."'";
112 }
113
114 // For each groupofemail, we update tables to set email field to empty
115 $nbingroup = count($groupofemails);
116
117 print "Process group of ".$nbingroup." emails (".$counter." - ".($counter + $nbingroup - 1)."), type = ".$type."\n";
118
119 $numerased = 0;
120
121 $sql_base = "UPDATE ".MAIN_DB_PREFIX;
122
123 if ($type == 'all' || $type == 'users') {
124 // Loop on each record and update the email to null if email into $groupofemails
125 $sql = $sql_base."user as u SET u.email = NULL WHERE u.email IN (".$db->sanitize($emailsin, 1).");";
126 print "Try to update users, ";
127 $resql = $db->query($sql);
128 if (!$resql) {
129 dol_print_error($db);
130 }
131 $numerased += $db->affected_rows($resql);
132 }
133
134 if ($type == 'all' || $type == 'thirdparties') {
135 // Loop on each record and update the email to null if email into $groupofemails
136 $sql = $sql_base."societe as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
137 print "Try to update thirdparties, ";
138 $resql = $db->query($sql);
139 if (!$resql) {
140 dol_print_error($db);
141 }
142 $numerased += $db->affected_rows($resql);
143 }
144
145 if ($type == 'all' || $type == 'contacts') {
146 // Loop on each record and update the email to null if email into $groupofemails
147
148 $sql = $sql_base."socpeople as s SET s.email = NULL WHERE s.email IN (".$db->sanitize($emailsin, 1).");";
149 print "Try to update contacts, ";
150 $resql = $db->query($sql);
151 if (!$resql) {
152 dol_print_error($db);
153 }
154 $numerased += $db->affected_rows($resql);
155 }
156
157 if ($type == 'all' || $type == 'members') {
158 // Loop on each record and update the email to null if email into $groupofemails
159
160 $sql = $sql_base."adherent as a SET a.email = NULL WHERE a.email IN (".$db->sanitize($emailsin, 1).");";
161 print "Try to update members, ";
162 $resql = $db->query($sql);
163 if (!$resql) {
164 dol_print_error($db);
165 }
166 $numerased += $db->affected_rows($resql);
167 }
168
169 $numerasedtotal += $numerased;
170
171 print $numerased." emails cleared.\n";
172 $counter = $counter + $nbingroup;
173}
174
175if (!$error && $mode == 'confirm') {
176 print "Commit - ".$numerasedtotal." operations validated.\n";
177 $db->commit();
178} else {
179 print "Rollback - ".$numerasedtotal." Operations canceled.\n";
180 $db->rollback();
181}
182
183exit($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='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...