dolibarr  17.0.4
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 require_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', ADHERENT_SPIP_SERVEUR, ADHERENT_SPIP_USER, ADHERENT_SPIP_PASS, ADHERENT_SPIP_DB, ADHERENT_SPIP_PORT);
109 
110  if ($resource->ok) {
111  return $resource;
112  }
113 
114  dol_syslog('Error when connecting to SPIP '.ADHERENT_SPIP_SERVEUR.' '.ADHERENT_SPIP_USER.' '.ADHERENT_SPIP_PASS.' '.ADHERENT_SPIP_DB, LOG_ERR);
115 
116  return false;
117  }
118 
127  private function callMailman($object, $url, $list)
128  {
129  global $conf;
130 
131  //Patterns that are going to be replaced with their original value
132  $patterns = array(
133  '%LISTE%',
134  '%EMAIL%',
135  '%PASSWORD%',
136  '%MAILMAN_ADMINPW%'
137  );
138  $replace = array(
139  $list,
140  $object->email,
141  $object->pass,
142  $conf->global->ADHERENT_MAILMAN_ADMIN_PASSWORD
143  );
144 
145  $curl_url = str_replace($patterns, $replace, $url);
146  dol_syslog('Calling Mailman: '.$curl_url);
147 
148  $result = getURLContent($curl_url);
149 
150  return $result['content'];
151  }
152 
153  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
160  public function add_to_spip($object)
161  {
162  // phpcs:enable
163  dol_syslog(get_class($this)."::add_to_spip");
164 
165  if ($this->isSpipEnabled()) {
166  if ($this->checkSpipConfig()) {
167  $mydb = $this->connectSpip();
168 
169  if ($mydb) {
170  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
171  $mdpass = dol_hash($object->pass);
172  $htpass = crypt($object->pass, makesalt());
173  $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\")";
174 
175  $result = $mydb->query($query);
176 
177  $mydb->close();
178 
179  if ($result) {
180  return 1;
181  } else {
182  $this->error = $mydb->lasterror();
183  }
184  } else {
185  $this->error = 'Failed to connect to SPIP';
186  }
187  } else {
188  $this->error = 'BadSPIPConfiguration';
189  }
190  } else {
191  $this->error = 'SPIPNotEnabled';
192  }
193 
194  return 0;
195  }
196 
197  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
204  public function del_to_spip($object)
205  {
206  // phpcs:enable
207  dol_syslog(get_class($this)."::del_to_spip");
208 
209  if ($this->isSpipEnabled()) {
210  if ($this->checkSpipConfig()) {
211  $mydb = $this->connectSpip();
212 
213  if ($mydb) {
214  $query = "DELETE FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
215 
216  $result = $mydb->query($query);
217 
218  $mydb->close();
219 
220  if ($result) {
221  return 1;
222  } else {
223  $this->error = $mydb->lasterror();
224  }
225  } else {
226  $this->error = 'Failed to connect to SPIP';
227  }
228  } else {
229  $this->error = 'BadSPIPConfiguration';
230  }
231  } else {
232  $this->error = 'SPIPNotEnabled';
233  }
234 
235  return 0;
236  }
237 
238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245  public function is_in_spip($object)
246  {
247  // phpcs:enable
248  if ($this->isSpipEnabled()) {
249  if ($this->checkSpipConfig()) {
250  $mydb = $this->connectSpip();
251 
252  if ($mydb) {
253  $query = "SELECT login FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
254 
255  $result = $mydb->query($query);
256 
257  if ($result) {
258  if ($mydb->num_rows($result)) {
259  // nous avons au moins une reponse
260  $mydb->close();
261  return 1;
262  } else {
263  // nous n'avons pas de reponse => n'existe pas
264  $mydb->close();
265  return 0;
266  }
267  } else {
268  $this->error = $mydb->lasterror();
269  $mydb->close();
270  }
271  } else {
272  $this->error = 'Failed to connect to SPIP';
273  }
274  } else {
275  $this->error = 'BadSPIPConfiguration';
276  }
277  } else {
278  $this->error = 'SPIPNotEnabled';
279  }
280 
281  return -1;
282  }
283 
284  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
292  public function add_to_mailman($object, $listes = '')
293  {
294  // phpcs:enable
295  global $conf, $langs, $user;
296 
297  dol_syslog(get_class($this)."::add_to_mailman");
298 
299  $this->mladded_ok = array();
300  $this->mladded_ko = array();
301 
302  if (!function_exists("curl_init")) {
303  $langs->load("errors");
304  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
305  return -1;
306  }
307 
308  if ($conf->adherent->enabled) { // Synchro for members
309  if (!empty($conf->global->ADHERENT_MAILMAN_URL)) {
310  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) {
311  $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
312  } else {
313  $lists = explode(',', $listes);
314  }
315 
316  $categstatic = new Categorie($this->db);
317 
318  foreach ($lists as $list) {
319  // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
320  $tmp = explode(':', $list);
321  if (!empty($tmp[2])) {
322  $list = $tmp[2];
323  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
324  dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
325  continue;
326  }
327  if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
328  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
329  continue;
330  }
331  }
332 
333  //We call Mailman to subscribe the user
334  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
335 
336  if ($result === false) {
337  $this->mladded_ko[$list] = $object->email;
338  return -2;
339  } else {
340  $this->mladded_ok[$list] = $object->email;
341  }
342  }
343  return count($lists);
344  } else {
345  $this->error = "ADHERENT_MAILMAN_URL not defined";
346  return -1;
347  }
348  }
349  }
350 
351  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
360  public function del_to_mailman($object, $listes = '')
361  {
362  // phpcs:enable
363  global $conf, $langs, $user;
364 
365  dol_syslog(get_class($this)."::del_to_mailman");
366 
367  $this->mlremoved_ok = array();
368  $this->mlremoved_ko = array();
369 
370  if (!function_exists("curl_init")) {
371  $langs->load("errors");
372  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
373  return -1;
374  }
375 
376  if ($conf->adherent->enabled) { // Synchro for members
377  if (!empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL)) {
378  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) {
379  $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
380  } else {
381  $lists = explode(',', $listes);
382  }
383 
384  $categstatic = new Categorie($this->db);
385 
386  foreach ($lists as $list) {
387  // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
388  $tmp = explode(':', $list);
389  if (!empty($tmp[2])) {
390  $list = $tmp[2];
391  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
392  dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
393  continue;
394  }
395  if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
396  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
397  continue;
398  }
399  }
400 
401  //We call Mailman to unsubscribe the user
402  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
403 
404  if ($result === false) {
405  $this->mlremoved_ko[$list] = $object->email;
406  return -2;
407  } else {
408  $this->mlremoved_ok[$list] = $object->email;
409  }
410  }
411  return count($lists);
412  } else {
413  $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
414  return -1;
415  }
416  }
417  }
418 }
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 dolibarr global constant int value.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) 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).
Definition: geturl.lib.php:41
if(!function_exists('dol_loginfunction')) makesalt($type=CRYPT_SALT_LENGTH)
Fonction pour initialiser un salt pour la fonction crypt.
dol_hash($chain, $type='0')
Returns a hash (non reversible encryption) of a string.
$conf db
API class for accounts.
Definition: inc.php:41