dolibarr  16.0.5
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 (defined("ADHERENT_USE_SPIP") && (ADHERENT_USE_SPIP == 1)) {
81  return true;
82  }
83 
84  return false;
85  }
86 
92  public function checkSpipConfig()
93  {
94  if (defined('ADHERENT_SPIP_SERVEUR') && defined('ADHERENT_SPIP_USER') && defined('ADHERENT_SPIP_PASS') && defined('ADHERENT_SPIP_DB')) {
95  if (ADHERENT_SPIP_SERVEUR != '' && ADHERENT_SPIP_USER != '' && ADHERENT_SPIP_PASS != '' && ADHERENT_SPIP_DB != '') {
96  return true;
97  }
98  }
99 
100  return false;
101  }
102 
108  public function connectSpip()
109  {
110  $resource = getDoliDBInstance('mysql', ADHERENT_SPIP_SERVEUR, ADHERENT_SPIP_USER, ADHERENT_SPIP_PASS, ADHERENT_SPIP_DB, ADHERENT_SPIP_PORT);
111 
112  if ($resource->ok) {
113  return $resource;
114  }
115 
116  dol_syslog('Error when connecting to SPIP '.ADHERENT_SPIP_SERVEUR.' '.ADHERENT_SPIP_USER.' '.ADHERENT_SPIP_PASS.' '.ADHERENT_SPIP_DB, LOG_ERR);
117 
118  return false;
119  }
120 
129  private function callMailman($object, $url, $list)
130  {
131  global $conf;
132 
133  //Patterns that are going to be replaced with their original value
134  $patterns = array(
135  '%LISTE%',
136  '%EMAIL%',
137  '%PASSWORD%',
138  '%MAILMAN_ADMINPW%'
139  );
140  $replace = array(
141  $list,
142  $object->email,
143  $object->pass,
144  $conf->global->ADHERENT_MAILMAN_ADMINPW
145  );
146 
147  $curl_url = str_replace($patterns, $replace, $url);
148  dol_syslog('Calling Mailman: '.$curl_url);
149 
150  $result = getURLContent($curl_url);
151 
152  return $result['content'];
153  }
154 
155  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
162  public function add_to_spip($object)
163  {
164  // phpcs:enable
165  dol_syslog(get_class($this)."::add_to_spip");
166 
167  if ($this->isSpipEnabled()) {
168  if ($this->checkSpipConfig()) {
169  $mydb = $this->connectSpip();
170 
171  if ($mydb) {
172  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
173  $mdpass = dol_hash($object->pass);
174  $htpass = crypt($object->pass, makesalt());
175  $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\")";
176 
177  $result = $mydb->query($query);
178 
179  $mydb->close();
180 
181  if ($result) {
182  return 1;
183  } else {
184  $this->error = $mydb->lasterror();
185  }
186  } else {
187  $this->error = 'Failed to connect to SPIP';
188  }
189  } else {
190  $this->error = 'BadSPIPConfiguration';
191  }
192  } else {
193  $this->error = 'SPIPNotEnabled';
194  }
195 
196  return 0;
197  }
198 
199  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
206  public function del_to_spip($object)
207  {
208  // phpcs:enable
209  dol_syslog(get_class($this)."::del_to_spip");
210 
211  if ($this->isSpipEnabled()) {
212  if ($this->checkSpipConfig()) {
213  $mydb = $this->connectSpip();
214 
215  if ($mydb) {
216  $query = "DELETE FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
217 
218  $result = $mydb->query($query);
219 
220  $mydb->close();
221 
222  if ($result) {
223  return 1;
224  } else {
225  $this->error = $mydb->lasterror();
226  }
227  } else {
228  $this->error = 'Failed to connect to SPIP';
229  }
230  } else {
231  $this->error = 'BadSPIPConfiguration';
232  }
233  } else {
234  $this->error = 'SPIPNotEnabled';
235  }
236 
237  return 0;
238  }
239 
240  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
247  public function is_in_spip($object)
248  {
249  // phpcs:enable
250  if ($this->isSpipEnabled()) {
251  if ($this->checkSpipConfig()) {
252  $mydb = $this->connectSpip();
253 
254  if ($mydb) {
255  $query = "SELECT login FROM spip_auteurs WHERE login = '".$mydb->escape($object->login)."'";
256 
257  $result = $mydb->query($query);
258 
259  if ($result) {
260  if ($mydb->num_rows($result)) {
261  // nous avons au moins une reponse
262  $mydb->close();
263  return 1;
264  } else {
265  // nous n'avons pas de reponse => n'existe pas
266  $mydb->close();
267  return 0;
268  }
269  } else {
270  $this->error = $mydb->lasterror();
271  $mydb->close();
272  }
273  } else {
274  $this->error = 'Failed to connect to SPIP';
275  }
276  } else {
277  $this->error = 'BadSPIPConfiguration';
278  }
279  } else {
280  $this->error = 'SPIPNotEnabled';
281  }
282 
283  return -1;
284  }
285 
286  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
294  public function add_to_mailman($object, $listes = '')
295  {
296  // phpcs:enable
297  global $conf, $langs, $user;
298 
299  dol_syslog(get_class($this)."::add_to_mailman");
300 
301  $this->mladded_ok = array();
302  $this->mladded_ko = array();
303 
304  if (!function_exists("curl_init")) {
305  $langs->load("errors");
306  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
307  return -1;
308  }
309 
310  if ($conf->adherent->enabled) { // Synchro for members
311  if (!empty($conf->global->ADHERENT_MAILMAN_URL)) {
312  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) {
313  $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
314  } else {
315  $lists = explode(',', $listes);
316  }
317 
318  $categstatic = new Categorie($this->db);
319 
320  foreach ($lists as $list) {
321  // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
322  $tmp = explode(':', $list);
323  if (!empty($tmp[2])) {
324  $list = $tmp[2];
325  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
326  dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
327  continue;
328  }
329  if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
330  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
331  continue;
332  }
333  }
334 
335  //We call Mailman to subscribe the user
336  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
337 
338  if ($result === false) {
339  $this->mladded_ko[$list] = $object->email;
340  return -2;
341  } else {
342  $this->mladded_ok[$list] = $object->email;
343  }
344  }
345  return count($lists);
346  } else {
347  $this->error = "ADHERENT_MAILMAN_URL not defined";
348  return -1;
349  }
350  }
351  }
352 
353  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
362  public function del_to_mailman($object, $listes = '')
363  {
364  // phpcs:enable
365  global $conf, $langs, $user;
366 
367  dol_syslog(get_class($this)."::del_to_mailman");
368 
369  $this->mlremoved_ok = array();
370  $this->mlremoved_ko = array();
371 
372  if (!function_exists("curl_init")) {
373  $langs->load("errors");
374  $this->error = $langs->trans("ErrorFunctionNotAvailableInPHP", "curl_init");
375  return -1;
376  }
377 
378  if ($conf->adherent->enabled) { // Synchro for members
379  if (!empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL)) {
380  if ($listes == '' && !empty($conf->global->ADHERENT_MAILMAN_LISTS)) {
381  $lists = explode(',', $conf->global->ADHERENT_MAILMAN_LISTS);
382  } else {
383  $lists = explode(',', $listes);
384  }
385 
386  $categstatic = new Categorie($this->db);
387 
388  foreach ($lists as $list) {
389  // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
390  $tmp = explode(':', $list);
391  if (!empty($tmp[2])) {
392  $list = $tmp[2];
393  if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) { // Filter on member type label
394  dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
395  continue;
396  }
397  if ($object->element == 'member' && $tmp[0] == 'CATEG' && !in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) { // Filter on member category
398  dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
399  continue;
400  }
401  }
402 
403  //We call Mailman to unsubscribe the user
404  $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
405 
406  if ($result === false) {
407  $this->mlremoved_ko[$list] = $object->email;
408  return -2;
409  } else {
410  $this->mlremoved_ok[$list] = $object->email;
411  }
412  }
413  return count($lists);
414  } else {
415  $this->error = "ADHERENT_MAILMAN_UNSUB_URL not defined";
416  return -1;
417  }
418  }
419  }
420 }
db
$conf db
API class for accounts.
Definition: inc.php:41
MailmanSpip\callMailman
callMailman($object, $url, $list)
Function used to connect to Mailman.
Definition: mailmanspip.class.php:129
makesalt
if(!function_exists('dol_loginfunction')) makesalt($type=CRYPT_SALT_LENGTH)
Fonction pour initialiser un salt pour la fonction crypt.
Definition: security2.lib.php:327
MailmanSpip\checkSpipConfig
checkSpipConfig()
Function used to check if the SPIP config is correct.
Definition: mailmanspip.class.php:92
MailmanSpip\is_in_spip
is_in_spip($object)
Fonction qui dit si cet utilisateur est un redacteur existant dans spip.
Definition: mailmanspip.class.php:247
MailmanSpip
Class to manage mailman and spip.
Definition: mailmanspip.class.php:40
MailmanSpip\add_to_spip
add_to_spip($object)
Fonction qui donne les droits redacteurs dans spip.
Definition: mailmanspip.class.php:162
MailmanSpip\add_to_mailman
add_to_mailman($object, $listes='')
Subscribe an email to all mailing-lists.
Definition: mailmanspip.class.php:294
Categorie
Class to manage categories.
Definition: categorie.class.php:47
getDoliDBInstance
getDoliDBInstance($type, $host, $user, $pass, $name, $port)
Return a DoliDB instance (database handler).
Definition: functions.lib.php:122
getURLContent
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
dol_hash
dol_hash($chain, $type='0')
Returns a hash of a string.
Definition: security.lib.php:104
MailmanSpip\del_to_spip
del_to_spip($object)
Fonction qui enleve les droits redacteurs dans spip.
Definition: mailmanspip.class.php:206
MailmanSpip\del_to_mailman
del_to_mailman($object, $listes='')
Unsubscribe an email from all mailing-lists Used when a user is resiliated.
Definition: mailmanspip.class.php:362
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
MailmanSpip\__construct
__construct($db)
Constructor.
Definition: mailmanspip.class.php:68
MailmanSpip\isSpipEnabled
isSpipEnabled()
Function used to check if SPIP is enabled on the system.
Definition: mailmanspip.class.php:78
MailmanSpip\connectSpip
connectSpip()
Function used to connect to SPIP.
Definition: mailmanspip.class.php:108