dolibarr 19.0.3
mailmanspip.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
4 * Copyright (C) 2004-2013 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
6 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
7 * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
8 * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
36
41{
45 public $db;
46
50 public $error = '';
51
55 public $errors = array();
56
57 public $mladded_ok;
58 public $mladded_ko;
59 public $mlremoved_ok;
60 public $mlremoved_ko;
61
62
68 public function __construct($db)
69 {
70 $this->db = $db;
71 }
72
78 public function isSpipEnabled()
79 {
80 if (getDolGlobalInt("ADHERENT_USE_SPIP") == 1) {
81 return true;
82 }
83
84 return false;
85 }
86
92 public function checkSpipConfig()
93 {
94 if (getDolGlobalString('ADHERENT_SPIP_SERVEUR') != '' && getDolGlobalString('ADHERENT_SPIP_USER') != '' && getDolGlobalString('ADHERENT_SPIP_PASS') != '' && getDolGlobalString('ADHERENT_SPIP_DB') != '') {
95 return true;
96 }
97
98 return false;
99 }
100
106 public function connectSpip()
107 {
108 $resource = getDoliDBInstance('mysql', getDolGlobalString('ADHERENT_SPIP_SERVEUR'), getDolGlobalString('ADHERENT_SPIP_USER'), getDolGlobalString('ADHERENT_SPIP_PASS'), getDolGlobalString('ADHERENT_SPIP_DB'), getDolGlobalInt('ADHERENT_SPIP_PORT'));
109
110 if ($resource->ok) {
111 return $resource;
112 }
113
114 dol_syslog('Error when connecting to SPIP '.getDolGlobalString('ADHERENT_SPIP_SERVEUR').' '.getDolGlobalString('ADHERENT_SPIP_USER').' '.getDolGlobalString('ADHERENT_SPIP_PASS').' '.getDolGlobalString('ADHERENT_SPIP_DB'), LOG_ERR);
115
116 return false;
117 }
118
127 private function callMailman($object, $url, $list)
128 {
129 global $conf;
130
131 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
132 //Patterns that are going to be replaced with their original value
133 $patterns = array(
134 '%LISTE%',
135 '%EMAIL%',
136 '%PASSWORD%',
137 '%MAILMAN_ADMINPW%'
138 );
139 $replace = array(
140 $list,
141 $object->email,
142 $object->pass,
143 getDolGlobalString('ADHERENT_MAILMAN_ADMIN_PASSWORD')
144 );
145
146 $curl_url = str_replace($patterns, $replace, $url);
147 dol_syslog('Calling Mailman: '.$curl_url);
148
149 $result = getURLContent($curl_url);
150
151 return $result['content'];
152 }
153
154 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
161 public function add_to_spip($object)
162 {
163 // phpcs:enable
164 dol_syslog(get_class($this)."::add_to_spip");
165
166 if ($this->isSpipEnabled()) {
167 if ($this->checkSpipConfig()) {
168 $mydb = $this->connectSpip();
169
170 if ($mydb) {
171 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
172 $mdpass = dol_hash($object->pass);
173 $htpass = crypt($object->pass, makesalt());
174 $query = "INSERT INTO spip_auteurs (nom, email, login, pass, htpass, alea_futur, statut) VALUES(\"".dolGetFirstLastname($object->firstname, $object->lastname)."\",\"".$object->email."\",\"".$object->login."\",\"$mdpass\",\"$htpass\",FLOOR(32000*RAND()),\"1comite\")";
175
176 $result = $mydb->query($query);
177
178 $mydb->close();
179
180 if ($result) {
181 return 1;
182 } else {
183 $this->error = $mydb->lasterror();
184 }
185 } else {
186 $this->error = 'Failed to connect to SPIP';
187 }
188 } else {
189 $this->error = 'BadSPIPConfiguration';
190 }
191 } else {
192 $this->error = 'SPIPNotEnabled';
193 }
194
195 return 0;
196 }
197
198 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
205 public function del_to_spip($object)
206 {
207 // phpcs:enable
208 dol_syslog(get_class($this)."::del_to_spip");
209
210 if ($this->isSpipEnabled()) {
211 if ($this->checkSpipConfig()) {
212 $mydb = $this->connectSpip();
213
214 if ($mydb) {
215 $query = "DELETE FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
216
217 $result = $mydb->query($query);
218
219 $mydb->close();
220
221 if ($result) {
222 return 1;
223 } else {
224 $this->error = $mydb->lasterror();
225 }
226 } else {
227 $this->error = 'Failed to connect to SPIP';
228 }
229 } else {
230 $this->error = 'BadSPIPConfiguration';
231 }
232 } else {
233 $this->error = 'SPIPNotEnabled';
234 }
235
236 return 0;
237 }
238
239 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
246 public function is_in_spip($object)
247 {
248 // phpcs:enable
249 if ($this->isSpipEnabled()) {
250 if ($this->checkSpipConfig()) {
251 $mydb = $this->connectSpip();
252
253 if ($mydb) {
254 $query = "SELECT login FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
255
256 $result = $mydb->query($query);
257
258 if ($result) {
259 if ($mydb->num_rows($result)) {
260 // nous avons au moins une reponse
261 $mydb->close();
262 return 1;
263 } else {
264 // nous n'avons pas de reponse => n'existe pas
265 $mydb->close();
266 return 0;
267 }
268 } else {
269 $this->error = $mydb->lasterror();
270 $mydb->close();
271 }
272 } else {
273 $this->error = 'Failed to connect to SPIP';
274 }
275 } else {
276 $this->error = 'BadSPIPConfiguration';
277 }
278 } else {
279 $this->error = 'SPIPNotEnabled';
280 }
281
282 return -1;
283 }
284
285 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
293 public function add_to_mailman($object, $listes = '')
294 {
295 // phpcs:enable
296 global $conf, $langs, $user;
297
298 dol_syslog(get_class($this)."::add_to_mailman");
299
300 $this->mladded_ok = array();
301 $this->mladded_ko = array();
302
303 if (!function_exists("curl_init")) {
304 $langs->load("errors");
305 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
306 return -1;
307 }
308
309 if (isModEnabled('adherent')) { // Synchro for members
310 if (getDolGlobalString('ADHERENT_MAILMAN_URL')) {
311 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
312 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
313 } else {
314 $lists = explode(',', $listes);
315 }
316
317 $categstatic = new Categorie($this->db);
318
319 foreach ($lists as $list) {
320 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
321 $tmp = explode(':', $list);
322 if (!empty($tmp[2])) {
323 $list = $tmp[2];
324 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
325 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
326 continue;
327 }
328 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
329 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
330 continue;
331 }
332 }
333
334 //We call Mailman to subscribe the user
335 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
336
337 if ($result === false) {
338 $this->mladded_ko[$list] = $object->email;
339 return -2;
340 } else {
341 $this->mladded_ok[$list] = $object->email;
342 }
343 }
344 return count($lists);
345 } else {
346 $this->error = "ADHERENT_MAILMAN_URL not defined";
347 return -1;
348 }
349 }
350
351 return 0;
352 }
353
354 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
363 public function del_to_mailman($object, $listes = '')
364 {
365 // phpcs:enable
366 global $conf, $langs, $user;
367
368 dol_syslog(get_class($this)."::del_to_mailman");
369
370 $this->mlremoved_ok = array();
371 $this->mlremoved_ko = array();
372
373 if (!function_exists("curl_init")) {
374 $langs->load("errors");
375 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
376 return -1;
377 }
378
379 if (isModEnabled('adherent')) { // Synchro for members
380 if (getDolGlobalString('ADHERENT_MAILMAN_UNSUB_URL')) {
381 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
382 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
383 } else {
384 $lists = explode(',', $listes);
385 }
386
387 $categstatic = new Categorie($this->db);
388
389 foreach ($lists as $list) {
390 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
391 $tmp = explode(':', $list);
392 if (!empty($tmp[2])) {
393 $list = $tmp[2];
394 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
395 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
396 continue;
397 }
398 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
399 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
400 continue;
401 }
402 }
403
404 //We call Mailman to unsubscribe the user
405 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
406
407 if ($result === false) {
408 $this->mlremoved_ko[$list] = $object->email;
409 return -2;
410 } else {
411 $this->mlremoved_ok[$list] = $object->email;
412 }
413 }
414 return count($lists);
415 } else {
416 $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
417 return -1;
418 }
419 }
420
421 return 0;
422 }
423}
Class to manage categories.
Class to manage mailman and spip.
del_to_mailman($object, $listes='')
Unsubscribe an email from all mailing-lists Used when a user is resiliated.
add_to_mailman($object, $listes='')
Subscribe an email to all mailing-lists.
del_to_spip($object)
Fonction qui enleve les droits redacteurs dans spip.
connectSpip()
Function used to connect to SPIP.
checkSpipConfig()
Function used to check if the SPIP config is correct.
callMailman($object, $url, $list)
Function used to connect to Mailman.
add_to_spip($object)
Fonction qui donne les droits redacteurs dans spip.
__construct($db)
Constructor.
isSpipEnabled()
Function used to check if SPIP is enabled on the system.
is_in_spip($object)
Fonction qui dit si cet utilisateur est un redacteur existant dans spip.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
if(!function_exists( 'dol_loginfunction')) makesalt($type=CRYPT_SALT_LENGTH)
Fonction pour initialiser un salt pour la fonction crypt.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.