dolibarr  13.0.2
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 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 
31 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
35 
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 (defined("ADHERENT_USE_SPIP") && (ADHERENT_USE_SPIP == 1))
81  {
82  return true;
83  }
84 
85  return false;
86  }
87 
93  public function checkSpipConfig()
94  {
95  if (defined('ADHERENT_SPIP_SERVEUR') && defined('ADHERENT_SPIP_USER') && defined('ADHERENT_SPIP_PASS') && defined('ADHERENT_SPIP_DB'))
96  {
97  if (ADHERENT_SPIP_SERVEUR != '' && ADHERENT_SPIP_USER != '' && ADHERENT_SPIP_PASS != '' && ADHERENT_SPIP_DB != '')
98  {
99  return true;
100  }
101  }
102 
103  return false;
104  }
105 
111  public function connectSpip()
112  {
113  $resource = getDoliDBInstance('mysql', ADHERENT_SPIP_SERVEUR, ADHERENT_SPIP_USER, ADHERENT_SPIP_PASS, ADHERENT_SPIP_DB, ADHERENT_SPIP_PORT);
114 
115  if ($resource->ok)
116  {
117  return $resource;
118  }
119 
120  dol_syslog('Error when connecting to SPIP '.ADHERENT_SPIP_SERVEUR.' '.ADHERENT_SPIP_USER.' '.ADHERENT_SPIP_PASS.' '.ADHERENT_SPIP_DB, LOG_ERR);
121 
122  return false;
123  }
124 
133  private function callMailman($object, $url, $list)
134  {
135  global $conf;
136 
137  //Patterns that are going to be replaced with their original value
138  $patterns = array(
139  '%LISTE%',
140  '%EMAIL%',
141  '%PASSWORD%',
142  '%MAILMAN_ADMINPW%'
143  );
144  $replace = array(
145  $list,
146  $object->email,
147  $object->pass,
148  $conf->global->ADHERENT_MAILMAN_ADMINPW
149  );
150 
151  $curl_url = str_replace($patterns, $replace, $url);
152  dol_syslog('Calling Mailman: '.$curl_url);
153 
154  $ch = curl_init($curl_url);
155 
156  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
157  curl_setopt($ch, CURLOPT_FAILONERROR, true);
158  @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
159  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT) ? 5 : $conf->global->MAIN_USE_CONNECT_TIMEOUT);
160  curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT) ? 30 : $conf->global->MAIN_USE_RESPONSE_TIMEOUT);
161  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
162  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
163 
164  $result = curl_exec($ch);
165  dol_syslog('result curl_exec='.$result);
166 
167  //An error was found, we store it in $this->error for later
168  if ($result === false || curl_errno($ch) > 0)
169  {
170  $this->error = curl_errno($ch).' '.curl_error($ch);
171  dol_syslog('Error using curl '.$this->error, LOG_ERR);
172  }
173 
174  curl_close($ch);
175 
176  return $result;
177  }
178 
179  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
186  public function add_to_spip($object)
187  {
188  // phpcs:enable
189  dol_syslog(get_class($this)."::add_to_spip");
190 
191  if ($this->isSpipEnabled())
192  {
193  if ($this->checkSpipConfig())
194  {
195  $mydb = $this->connectSpip();
196 
197  if ($mydb)
198  {
199  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
200  $mdpass = dol_hash($object->pass);
201  $htpass = crypt($object->pass, makesalt());
202  $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\")";
203 
204  $result = $mydb->query($query);
205 
206  $mydb->close();
207 
208  if ($result)
209  {
210  return 1;
211  } else $this->error = $mydb->lasterror();
212  } else $this->error = 'Failed to connect to SPIP';
213  } else $this->error = 'BadSPIPConfiguration';
214  } else $this->error = 'SPIPNotEnabled';
215 
216  return 0;
217  }
218 
219  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
226  public function del_to_spip($object)
227  {
228  // phpcs:enable
229  dol_syslog(get_class($this)."::del_to_spip");
230 
231  if ($this->isSpipEnabled())
232  {
233  if ($this->checkSpipConfig())
234  {
235  $mydb = $this->connectSpip();
236 
237  if ($mydb)
238  {
239  $query = "DELETE FROM spip_auteurs WHERE login='".$object->login."'";
240 
241  $result = $mydb->query($query);
242 
243  $mydb->close();
244 
245  if ($result)
246  {
247  return 1;
248  } else $this->error = $mydb->lasterror();
249  } else $this->error = 'Failed to connect to SPIP';
250  } else $this->error = 'BadSPIPConfiguration';
251  } else $this->error = 'SPIPNotEnabled';
252 
253  return 0;
254  }
255 
256  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
263  public function is_in_spip($object)
264  {
265  // phpcs:enable
266  if ($this->isSpipEnabled())
267  {
268  if ($this->checkSpipConfig())
269  {
270  $mydb = $this->connectSpip();
271 
272  if ($mydb)
273  {
274  $query = "SELECT login FROM spip_auteurs WHERE login='".$object->login."'";
275 
276  $result = $mydb->query($query);
277 
278  if ($result)
279  {
280  if ($mydb->num_rows($result))
281  {
282  // nous avons au moins une reponse
283  $mydb->close($result);
284  return 1;
285  } else {
286  // nous n'avons pas de reponse => n'existe pas
287  $mydb->close($result);
288  return 0;
289  }
290  } else {
291  $this->error = $mydb->lasterror();
292  $mydb->close();
293  }
294  } else $this->error = 'Failed to connect to SPIP';
295  } else $this->error = 'BadSPIPConfiguration';
296  } else $this->error = 'SPIPNotEnabled';
297 
298  return -1;
299  }
300 
301  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
309  public function add_to_mailman($object, $listes = '')
310  {
311  // phpcs:enable
312  global $conf, $langs, $user;
313 
314  dol_syslog(get_class($this)."::add_to_mailman");
315 
316  $this->mladded_ok = array();
317  $this->mladded_ko = array();
318 
319  if (!function_exists("curl_init"))
320  {
321  $langs->load("errors");
322  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
323  return -1;
324  }
325 
326  if ($conf->adherent->enabled) // Synchro for members
327  {
328  if (!empty($conf->global->ADHERENT_MAILMAN_URL))
329  {
330  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
331  else $lists = explode(',', $listes);
332 
333  $categstatic = new Categorie($this->db);
334 
335  foreach ($lists as $list)
336  {
337  // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
338  $tmp = explode(':', $list);
339  if (!empty($tmp[2]))
340  {
341  $list = $tmp[2];
342  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) // Filter on member type label
343  {
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  {
349  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
350  continue;
351  }
352  }
353 
354  //We call Mailman to subscribe the user
355  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
356 
357  if ($result === false)
358  {
359  $this->mladded_ko[$list] = $object->email;
360  return -2;
361  } else $this->mladded_ok[$list] = $object->email;
362  }
363  return count($lists);
364  } else {
365  $this->error = "ADHERENT_MAILMAN_URL not defined";
366  return -1;
367  }
368  }
369  }
370 
371  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
380  public function del_to_mailman($object, $listes = '')
381  {
382  // phpcs:enable
383  global $conf, $langs, $user;
384 
385  dol_syslog(get_class($this)."::del_to_mailman");
386 
387  $this->mlremoved_ok = array();
388  $this->mlremoved_ko = array();
389 
390  if (!function_exists("curl_init"))
391  {
392  $langs->load("errors");
393  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
394  return -1;
395  }
396 
397  if ($conf->adherent->enabled) // Synchro for members
398  {
399  if (!empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL))
400  {
401  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
402  else $lists = explode(',', $listes);
403 
404  $categstatic = new Categorie($this->db);
405 
406  foreach ($lists as $list)
407  {
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  {
412  $list = $tmp[2];
413  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) // Filter on member type label
414  {
415  dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
416  continue;
417  }
418  if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) // Filter on member category
419  {
420  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
421  continue;
422  }
423  }
424 
425  //We call Mailman to unsubscribe the user
426  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
427 
428  if ($result === false)
429  {
430  $this->mlremoved_ko[$list] = $object->email;
431  return -2;
432  } else $this->mlremoved_ok[$list] = $object->email;
433  }
434  return count($lists);
435  } else {
436  $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
437  return -1;
438  }
439  }
440  }
441 }
dol_hash($chain, $type= '0')
Returns a hash of a string.
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
__construct($db)
Constructor.
connectSpip()
Function used to connect to SPIP.
del_to_mailman($object, $listes= '')
Unsubscribe an email from all mailing-lists Used when a user is resiliated.
if(!function_exists('dol_loginfunction')) makesalt($type=CRYPT_SALT_LENGTH)
Fonction pour initialiser un salt pour la fonction crypt.
isSpipEnabled()
Function used to check if SPIP is enabled on the system.
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage categories.
callMailman($object, $url, $list)
Function used to connect to Mailman.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
add_to_spip($object)
Fonction qui donne les droits redacteurs dans spip.
checkSpipConfig()
Function used to check if the SPIP config is correct.
del_to_spip($object)
Fonction qui enleve les droits redacteurs dans spip.
is_in_spip($object)
Fonction qui dit si cet utilisateur est un redacteur existant dans spip.
add_to_mailman($object, $listes= '')
Subscribe an email to all mailing-lists.
Class to manage mailman and spip.