dolibarr 21.0.0-alpha
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-2024 Frédéric France <frederic.france@free.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
60 public $mladded_ok;
61
65 public $mladded_ko;
66
70 public $mlremoved_ok;
71
75 public $mlremoved_ko;
76
77
83 public function __construct($db)
84 {
85 $this->db = $db;
86 }
87
93 public function isSpipEnabled()
94 {
95 if (getDolGlobalInt("ADHERENT_USE_SPIP") == 1) {
96 return true;
97 }
98
99 return false;
100 }
101
107 public function checkSpipConfig()
108 {
109 if (getDolGlobalString('ADHERENT_SPIP_SERVEUR') != '' && getDolGlobalString('ADHERENT_SPIP_USER') != '' && getDolGlobalString('ADHERENT_SPIP_PASS') != '' && getDolGlobalString('ADHERENT_SPIP_DB') != '') {
110 return true;
111 }
112
113 return false;
114 }
115
121 public function connectSpip()
122 {
123 $resource = getDoliDBInstance('mysql', getDolGlobalString('ADHERENT_SPIP_SERVEUR'), getDolGlobalString('ADHERENT_SPIP_USER'), getDolGlobalString('ADHERENT_SPIP_PASS'), getDolGlobalString('ADHERENT_SPIP_DB'), getDolGlobalInt('ADHERENT_SPIP_PORT'));
124
125 if ($resource->ok) {
126 return $resource;
127 }
128
129 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);
130
131 return false;
132 }
133
142 private function callMailman($object, $url, $list)
143 {
144 global $conf;
145
146 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
147 //Patterns that are going to be replaced with their original value
148 $patterns = array(
149 '%LISTE%',
150 '%EMAIL%',
151 '%PASSWORD%',
152 '%MAILMAN_ADMINPW%'
153 );
154 $replace = array(
155 $list,
156 $object->email,
157 $object->pass,
158 getDolGlobalString('ADHERENT_MAILMAN_ADMIN_PASSWORD')
159 );
160
161 $curl_url = str_replace($patterns, $replace, $url);
162 dol_syslog('Calling Mailman: '.$curl_url);
163
164 $result = getURLContent($curl_url);
165
166 return $result['content'];
167 }
168
169 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
176 public function add_to_spip($object)
177 {
178 // phpcs:enable
179 dol_syslog(get_class($this)."::add_to_spip");
180
181 if ($this->isSpipEnabled()) {
182 if ($this->checkSpipConfig()) {
183 $mydb = $this->connectSpip();
184
185 if ($mydb) {
186 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
187 $mdpass = dol_hash($object->pass);
188 $htpass = crypt($object->pass, makesalt());
189
190 $query = "INSERT INTO spip_auteurs (nom, email, login, pass, htpass, alea_futur, statut)";
191 $query .= " VALUES('".$mydb->escape(dolGetFirstLastname($object->firstname, $object->lastname))."', '".$mydb->escape($object->email)."',";
192 $query .= " '".$mydb->escape($object->login)."', '".$mydb->escape($mdpass)."', '".$mydb->escape($htpass)."', FLOOR(32000*RAND()), '1comite')";
193
194 $result = $mydb->query($query);
195
196 $mydb->close();
197
198 if ($result) {
199 return 1;
200 } else {
201 $this->error = $mydb->lasterror();
202 }
203 } else {
204 $this->error = 'Failed to connect to SPIP';
205 }
206 } else {
207 $this->error = 'BadSPIPConfiguration';
208 }
209 } else {
210 $this->error = 'SPIPNotEnabled';
211 }
212
213 return 0;
214 }
215
216 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
223 public function del_to_spip($object)
224 {
225 // phpcs:enable
226 dol_syslog(get_class($this)."::del_to_spip");
227
228 if ($this->isSpipEnabled()) {
229 if ($this->checkSpipConfig()) {
230 $mydb = $this->connectSpip();
231
232 if ($mydb) {
233 $query = "DELETE FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
234
235 $result = $mydb->query($query);
236
237 $mydb->close();
238
239 if ($result) {
240 return 1;
241 } else {
242 $this->error = $mydb->lasterror();
243 }
244 } else {
245 $this->error = 'Failed to connect to SPIP';
246 }
247 } else {
248 $this->error = 'BadSPIPConfiguration';
249 }
250 } else {
251 $this->error = 'SPIPNotEnabled';
252 }
253
254 return 0;
255 }
256
257 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
264 public function is_in_spip($object)
265 {
266 // phpcs:enable
267 if ($this->isSpipEnabled()) {
268 if ($this->checkSpipConfig()) {
269 $mydb = $this->connectSpip();
270
271 if ($mydb) {
272 $query = "SELECT login FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
273
274 $result = $mydb->query($query);
275
276 if ($result) {
277 if ($mydb->num_rows($result)) {
278 // At least one result for the login query
279 $mydb->close();
280 return 1;
281 } else {
282 // No result for the login query
283 $mydb->close();
284 return 0;
285 }
286 } else {
287 $this->error = $mydb->lasterror();
288 $mydb->close();
289 }
290 } else {
291 $this->error = 'Failed to connect to SPIP';
292 }
293 } else {
294 $this->error = 'BadSPIPConfiguration';
295 }
296 } else {
297 $this->error = 'SPIPNotEnabled';
298 }
299
300 return -1;
301 }
302
303 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
311 public function add_to_mailman($object, $listes = '')
312 {
313 // phpcs:enable
314 global $conf, $langs, $user;
315
316 dol_syslog(get_class($this)."::add_to_mailman");
317
318 $this->mladded_ok = array();
319 $this->mladded_ko = array();
320
321 if (!function_exists("curl_init")) {
322 $langs->load("errors");
323 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
324 return -1;
325 }
326
327 if (isModEnabled('member')) { // Synchro for members
328 if (getDolGlobalString('ADHERENT_MAILMAN_URL')) {
329 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
330 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
331 } else {
332 $lists = explode(',', $listes);
333 }
334
335 $categstatic = new Categorie($this->db);
336
337 foreach ($lists as $list) {
338 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
339 $tmp = explode(':', $list);
340 if (!empty($tmp[2])) {
341 $list = $tmp[2];
342 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
343 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
344 continue;
345 }
346 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
347 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
348 continue;
349 }
350 }
351
352 //We call Mailman to subscribe the user
353 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
354
355 if ($result === false) {
356 $this->mladded_ko[$list] = $object->email;
357 return -2;
358 } else {
359 $this->mladded_ok[$list] = $object->email;
360 }
361 }
362 return count($lists);
363 } else {
364 $this->error = "ADHERENT_MAILMAN_URL not defined";
365 return -1;
366 }
367 }
368
369 return 0;
370 }
371
372 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
381 public function del_to_mailman($object, $listes = '')
382 {
383 // phpcs:enable
384 global $conf, $langs, $user;
385
386 dol_syslog(get_class($this)."::del_to_mailman");
387
388 $this->mlremoved_ok = array();
389 $this->mlremoved_ko = array();
390
391 if (!function_exists("curl_init")) {
392 $langs->load("errors");
393 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
394 return -1;
395 }
396
397 if (isModEnabled('member')) { // Synchro for members
398 if (getDolGlobalString('ADHERENT_MAILMAN_UNSUB_URL')) {
399 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
400 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
401 } else {
402 $lists = explode(',', $listes);
403 }
404
405 $categstatic = new Categorie($this->db);
406
407 foreach ($lists as $list) {
408 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
409 $tmp = explode(':', $list);
410 if (!empty($tmp[2])) {
411 $list = $tmp[2];
412 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
413 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
414 continue;
415 }
416 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
417 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
418 continue;
419 }
420 }
421
422 //We call Mailman to unsubscribe the user
423 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
424
425 if ($result === false) {
426 $this->mlremoved_ko[$list] = $object->email;
427 return -2;
428 } else {
429 $this->mlremoved_ok[$list] = $object->email;
430 }
431 }
432 return count($lists);
433 } else {
434 $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
435 return -1;
436 }
437 }
438
439 return 0;
440 }
441}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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)
Indicate if the user is an existing editor in spip.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
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)
Initialise the salt for the crypt function.
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.