dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
37
42{
46 public $db;
47
51 public $error = '';
52
56 public $errors = array();
57
61 public $mladded_ok;
62
66 public $mladded_ko;
67
71 public $mlremoved_ok;
72
76 public $mlremoved_ko;
77
78
84 public function __construct($db)
85 {
86 $this->db = $db;
87 }
88
94 public function isSpipEnabled()
95 {
96 if (getDolGlobalInt("ADHERENT_USE_SPIP") == 1) {
97 return true;
98 }
99
100 return false;
101 }
102
108 public function checkSpipConfig()
109 {
110 if (getDolGlobalString('ADHERENT_SPIP_SERVEUR') != '' && getDolGlobalString('ADHERENT_SPIP_USER') != '' && getDolGlobalString('ADHERENT_SPIP_PASS') != '' && getDolGlobalString('ADHERENT_SPIP_DB') != '') {
111 return true;
112 }
113
114 return false;
115 }
116
122 public function connectSpip()
123 {
124 $resource = getDoliDBInstance('mysql', getDolGlobalString('ADHERENT_SPIP_SERVEUR'), getDolGlobalString('ADHERENT_SPIP_USER'), getDolGlobalString('ADHERENT_SPIP_PASS'), getDolGlobalString('ADHERENT_SPIP_DB'), getDolGlobalInt('ADHERENT_SPIP_PORT'));
125
126 if ($resource->ok) {
127 return $resource;
128 }
129
130 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);
131
132 return false;
133 }
134
143 private function callMailman($object, $url, $list)
144 {
145 global $conf;
146
147 require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
148 //Patterns that are going to be replaced with their original value
149 $patterns = array(
150 '%LISTE%',
151 '%EMAIL%',
152 '%PASSWORD%',
153 '%MAILMAN_ADMINPW%'
154 );
155 $replace = array(
156 $list,
157 $object->email,
158 $object->pass,
159 getDolGlobalString('ADHERENT_MAILMAN_ADMIN_PASSWORD')
160 );
161
162 $curl_url = str_replace($patterns, $replace, $url);
163 dol_syslog('Calling Mailman: '.$curl_url);
164
165 $result = getURLContent($curl_url);
166
167 return $result['content'];
168 }
169
170 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
177 public function add_to_spip($object)
178 {
179 // phpcs:enable
180 dol_syslog(get_class($this)."::add_to_spip");
181
182 if ($this->isSpipEnabled()) {
183 if ($this->checkSpipConfig()) {
184 $mydb = $this->connectSpip();
185
186 if ($mydb) {
187 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
188 $mdpass = dol_hash($object->pass);
189 $htpass = crypt($object->pass, makesalt());
190
191 $query = "INSERT INTO spip_auteurs (nom, email, login, pass, htpass, alea_futur, statut)";
192 $query .= " VALUES('".$mydb->escape(dolGetFirstLastname($object->firstname, $object->lastname))."', '".$mydb->escape($object->email)."',";
193 $query .= " '".$mydb->escape($object->login)."', '".$mydb->escape($mdpass)."', '".$mydb->escape($htpass)."', FLOOR(32000*RAND()), '1comite')";
194
195 $result = $mydb->query($query);
196
197 $mydb->close();
198
199 if ($result) {
200 return 1;
201 } else {
202 $this->error = $mydb->lasterror();
203 }
204 } else {
205 $this->error = 'Failed to connect to SPIP';
206 }
207 } else {
208 $this->error = 'BadSPIPConfiguration';
209 }
210 } else {
211 $this->error = 'SPIPNotEnabled';
212 }
213
214 return 0;
215 }
216
217 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
224 public function del_to_spip($object)
225 {
226 // phpcs:enable
227 dol_syslog(get_class($this)."::del_to_spip");
228
229 if ($this->isSpipEnabled()) {
230 if ($this->checkSpipConfig()) {
231 $mydb = $this->connectSpip();
232
233 if ($mydb) {
234 $query = "DELETE FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
235
236 $result = $mydb->query($query);
237
238 $mydb->close();
239
240 if ($result) {
241 return 1;
242 } else {
243 $this->error = $mydb->lasterror();
244 }
245 } else {
246 $this->error = 'Failed to connect to SPIP';
247 }
248 } else {
249 $this->error = 'BadSPIPConfiguration';
250 }
251 } else {
252 $this->error = 'SPIPNotEnabled';
253 }
254
255 return 0;
256 }
257
258 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
265 public function is_in_spip($object)
266 {
267 // phpcs:enable
268 if ($this->isSpipEnabled()) {
269 if ($this->checkSpipConfig()) {
270 $mydb = $this->connectSpip();
271
272 if ($mydb) {
273 $query = "SELECT login FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
274
275 $result = $mydb->query($query);
276
277 if ($result) {
278 if ($mydb->num_rows($result)) {
279 // At least one result for the login query
280 $mydb->close();
281 return 1;
282 } else {
283 // No result for the login query
284 $mydb->close();
285 return 0;
286 }
287 } else {
288 $this->error = $mydb->lasterror();
289 $mydb->close();
290 }
291 } else {
292 $this->error = 'Failed to connect to SPIP';
293 }
294 } else {
295 $this->error = 'BadSPIPConfiguration';
296 }
297 } else {
298 $this->error = 'SPIPNotEnabled';
299 }
300
301 return -1;
302 }
303
304 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
312 public function add_to_mailman($object, $listes = '')
313 {
314 // phpcs:enable
315 global $conf, $langs, $user;
316
317 dol_syslog(get_class($this)."::add_to_mailman");
318
319 $this->mladded_ok = array();
320 $this->mladded_ko = array();
321
322 if (!function_exists("curl_init")) {
323 $langs->load("errors");
324 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
325 return -1;
326 }
327
328 if (isModEnabled('member')) { // Synchro for members
329 if (getDolGlobalString('ADHERENT_MAILMAN_URL')) {
330 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
331 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
332 } else {
333 $lists = explode(',', $listes);
334 }
335
336 $categstatic = new Categorie($this->db);
337
338 foreach ($lists as $list) {
339 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
340 $tmp = explode(':', $list);
341 if (!empty($tmp[2])) {
342 $list = $tmp[2];
343 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
344 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
345 continue;
346 }
347 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
348 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
349 continue;
350 }
351 }
352
353 //We call Mailman to subscribe the user
354 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
355
356 if ($result === false) {
357 $this->mladded_ko[$list] = $object->email;
358 return -2;
359 } else {
360 $this->mladded_ok[$list] = $object->email;
361 }
362 }
363 return count($lists);
364 } else {
365 $this->error = "ADHERENT_MAILMAN_URL not defined";
366 return -1;
367 }
368 }
369
370 return 0;
371 }
372
373 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
382 public function del_to_mailman($object, $listes = '')
383 {
384 // phpcs:enable
385 global $conf, $langs, $user;
386
387 dol_syslog(get_class($this)."::del_to_mailman");
388
389 $this->mlremoved_ok = array();
390 $this->mlremoved_ko = array();
391
392 if (!function_exists("curl_init")) {
393 $langs->load("errors");
394 $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
395 return -1;
396 }
397
398 if (isModEnabled('member')) { // Synchro for members
399 if (getDolGlobalString('ADHERENT_MAILMAN_UNSUB_URL')) {
400 if ($listes == '' && getDolGlobalString('ADHERENT_MAILMAN_LISTS')) {
401 $lists = explode(',', getDolGlobalString('ADHERENT_MAILMAN_LISTS'));
402 } else {
403 $lists = explode(',', $listes);
404 }
405
406 $categstatic = new Categorie($this->db);
407
408 foreach ($lists as $list) {
409 // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
410 $tmp = explode(':', $list);
411 if (!empty($tmp[2])) {
412 $list = $tmp[2];
413 if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
414 dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
415 continue;
416 }
417 if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
418 dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
419 continue;
420 }
421 }
422
423 //We call Mailman to unsubscribe the user
424 $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
425
426 if ($result === false) {
427 $this->mlremoved_ko[$list] = $object->email;
428 return -2;
429 } else {
430 $this->mlremoved_ok[$list] = $object->email;
431 }
432 }
433 return count($lists);
434 } else {
435 $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
436 return -1;
437 }
438 }
439
440 return 0;
441 }
442}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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 a 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).
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.