mailmanspip.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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@capnetworks.com>
  8. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/mailmanspip/class/mailmanspip.class.php
  25. * \ingroup member
  26. * \brief File of class to manage mailman and spip actions
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  32. /**
  33. * Class to manage mailman and spip
  34. */
  35. class MailmanSpip
  36. {
  37. var $db;
  38. var $error;
  39. var $mladded_ok;
  40. var $mladded_ko;
  41. var $mlremoved_ok;
  42. var $mlremoved_ko;
  43. /**
  44. * Constructor
  45. *
  46. * @param DoliDB $db Database handler
  47. */
  48. function __construct($db)
  49. {
  50. $this->db = $db;
  51. }
  52. /**
  53. * Function used to check if SPIP is enabled on the system
  54. *
  55. * @return boolean
  56. */
  57. function isSpipEnabled()
  58. {
  59. if (defined("ADHERENT_USE_SPIP") && (ADHERENT_USE_SPIP == 1))
  60. {
  61. return true;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Function used to check if the SPIP config is correct
  67. *
  68. * @return boolean
  69. */
  70. function checkSpipConfig()
  71. {
  72. if (defined('ADHERENT_SPIP_SERVEUR') && defined('ADHERENT_SPIP_USER') && defined('ADHERENT_SPIP_PASS') && defined('ADHERENT_SPIP_DB'))
  73. {
  74. if (ADHERENT_SPIP_SERVEUR != '' && ADHERENT_SPIP_USER != '' && ADHERENT_SPIP_PASS != '' && ADHERENT_SPIP_DB != '')
  75. {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. * Function used to connect to SPIP
  83. *
  84. * @return boolean|DoliDB Boolean of DoliDB
  85. */
  86. function connectSpip()
  87. {
  88. $resource = getDoliDBInstance('mysql', ADHERENT_SPIP_SERVEUR, ADHERENT_SPIP_USER, ADHERENT_SPIP_PASS, ADHERENT_SPIP_DB, ADHERENT_SPIP_PORT);
  89. if ($resource->ok)
  90. {
  91. return $resource;
  92. }
  93. dol_syslog('Error when connecting to SPIP '.ADHERENT_SPIP_SERVEUR.' '.ADHERENT_SPIP_USER.' '.ADHERENT_SPIP_PASS.' '.ADHERENT_SPIP_DB, LOG_ERR);
  94. return false;
  95. }
  96. /**
  97. * Function used to connect to Mailman
  98. *
  99. * @param object $object Object with the data
  100. * @param string $url Mailman URL to be called with patterns
  101. * @param string $list Name of mailing-list
  102. * @return mixed Boolean or string
  103. */
  104. private function callMailman($object, $url, $list)
  105. {
  106. global $conf;
  107. //Patterns that are going to be replaced with their original value
  108. $patterns = array(
  109. '%LISTE%',
  110. '%EMAIL%',
  111. '%PASSWORD%',
  112. '%MAILMAN_ADMINPW%'
  113. );
  114. $replace = array(
  115. $list,
  116. $object->email,
  117. $object->pass,
  118. $conf->global->ADHERENT_MAILMAN_ADMINPW
  119. );
  120. $curl_url = str_replace($patterns, $replace, $url);
  121. dol_syslog('Calling Mailman: '.$curl_url);
  122. $ch = curl_init($curl_url);
  123. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  124. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  125. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  126. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?5:$conf->global->MAIN_USE_CONNECT_TIMEOUT);
  127. curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT);
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  129. $result = curl_exec($ch);
  130. dol_syslog('result curl_exec='.$result);
  131. //An error was found, we store it in $this->error for later
  132. if ($result === false || curl_errno($ch) > 0)
  133. {
  134. $this->error = curl_errno($ch).' '.curl_error($ch);
  135. dol_syslog('Error using curl '.$this->error, LOG_ERR);
  136. }
  137. curl_close($ch);
  138. return $result;
  139. }
  140. /**
  141. * Fonction qui donne les droits redacteurs dans spip
  142. *
  143. * @param Object $object Object with data (->firstname, ->lastname, ->email and ->login)
  144. * @return int =0 if KO, >0 if OK
  145. */
  146. function add_to_spip($object)
  147. {
  148. dol_syslog(get_class($this)."::add_to_spip");
  149. if ($this->isSpipEnabled())
  150. {
  151. if ($this->checkSpipConfig())
  152. {
  153. $mydb = $this->connectSpip();
  154. if ($mydb)
  155. {
  156. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  157. $mdpass=dol_hash($object->pass);
  158. $htpass=crypt($object->pass,makesalt());
  159. $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\")";
  160. $result = $mydb->query($query);
  161. $mydb->close();
  162. if ($result)
  163. {
  164. return 1;
  165. }
  166. else $this->error = $mydb->lasterror();
  167. }
  168. else $this->error = 'Failed to connect to SPIP';
  169. }
  170. else $this->error = 'BadSPIPConfiguration';
  171. }
  172. else $this->error = 'SPIPNotEnabled';
  173. return 0;
  174. }
  175. /**
  176. * Fonction qui enleve les droits redacteurs dans spip
  177. *
  178. * @param Object $object Object with data (->login)
  179. * @return int =0 if KO, >0 if OK
  180. */
  181. function del_to_spip($object)
  182. {
  183. dol_syslog(get_class($this)."::del_to_spip");
  184. if ($this->isSpipEnabled())
  185. {
  186. if ($this->checkSpipConfig())
  187. {
  188. $mydb = $this->connectSpip();
  189. if ($mydb)
  190. {
  191. $query = "DELETE FROM spip_auteurs WHERE login='".$object->login."'";
  192. $result = $mydb->query($query);
  193. $mydb->close();
  194. if ($result)
  195. {
  196. return 1;
  197. }
  198. else $this->error = $mydb->lasterror();
  199. }
  200. else $this->error = 'Failed to connect to SPIP';
  201. }
  202. else $this->error = 'BadSPIPConfiguration';
  203. }
  204. else $this->error = 'SPIPNotEnabled';
  205. return 0;
  206. }
  207. /**
  208. * Fonction qui dit si cet utilisateur est un redacteur existant dans spip
  209. *
  210. * @param Object $object Object with data (->login)
  211. * @return int 1=exists, 0=does not exists, -1=error
  212. */
  213. function is_in_spip($object)
  214. {
  215. if ($this->isSpipEnabled())
  216. {
  217. if ($this->checkSpipConfig())
  218. {
  219. $mydb = $this->connectSpip();
  220. if ($mydb)
  221. {
  222. $query = "SELECT login FROM spip_auteurs WHERE login='".$object->login."'";
  223. $result = $mydb->query($query);
  224. if ($result)
  225. {
  226. if ($mydb->num_rows($result))
  227. {
  228. // nous avons au moins une reponse
  229. $mydb->close($result);
  230. return 1;
  231. }
  232. else
  233. {
  234. // nous n'avons pas de reponse => n'existe pas
  235. $mydb->close($result);
  236. return 0;
  237. }
  238. }
  239. else
  240. {
  241. $this->error = $mydb->lasterror();
  242. $mydb->close();
  243. }
  244. }
  245. else $this->error = 'Failed to connect to SPIP';
  246. }
  247. else $this->error = 'BadSPIPConfiguration';
  248. }
  249. else $this->error = 'SPIPNotEnabled';
  250. return -1;
  251. }
  252. /**
  253. * Subscribe an email to all mailing-lists
  254. *
  255. * @param Object $object Object with data (->email, ->pass, ->element, ->type)
  256. * @param array $listes To force mailing-list (string separated with ,)
  257. * @return int <0 if KO, >=0 if OK
  258. */
  259. function add_to_mailman($object,$listes='')
  260. {
  261. global $conf,$langs,$user;
  262. dol_syslog(get_class($this)."::add_to_mailman");
  263. $this->mladded_ok=array();
  264. $this->mladded_ko=array();
  265. if (! function_exists("curl_init"))
  266. {
  267. $langs->load("errors");
  268. $this->error=$langs->trans("ErrorFunctionNotAvailableInPHP","curl_init");
  269. return -1;
  270. }
  271. if ($conf->adherent->enabled) // Synchro for members
  272. {
  273. if (! empty($conf->global->ADHERENT_MAILMAN_URL))
  274. {
  275. if ($listes == '' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS)) $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  276. else $lists=explode(',',$listes);
  277. $categstatic=new Categorie($this->db);
  278. foreach ($lists as $list)
  279. {
  280. // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
  281. $tmp=explode(':',$list);
  282. if (! empty($tmp[2]))
  283. {
  284. $list=$tmp[2];
  285. if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) // Filter on member type label
  286. {
  287. dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
  288. continue;
  289. }
  290. if ($object->element == 'member' && $tmp[0] == 'CATEG' && ! in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) // Filter on member category
  291. {
  292. dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
  293. continue;
  294. }
  295. }
  296. //We call Mailman to subscribe the user
  297. $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL, $list);
  298. if ($result === false)
  299. {
  300. $this->mladded_ko[$list]=$object->email;
  301. return -2;
  302. }
  303. else $this->mladded_ok[$list]=$object->email;
  304. }
  305. return count($lists);
  306. }
  307. else
  308. {
  309. $this->error="ADHERENT_MAILMAN_URL not defined";
  310. return -1;
  311. }
  312. }
  313. }
  314. /**
  315. * Unsubscribe an email from all mailing-lists
  316. * Used when a user is resiliated
  317. *
  318. * @param Object $object Object with data (->email, ->pass, ->element, ->type)
  319. * @param array $listes To force mailing-list (string separated with ,)
  320. * @return int <0 if KO, >=0 if OK
  321. */
  322. function del_to_mailman($object,$listes='')
  323. {
  324. global $conf,$langs,$user;
  325. dol_syslog(get_class($this)."::del_to_mailman");
  326. $this->mlremoved_ok=array();
  327. $this->mlremoved_ko=array();
  328. if (! function_exists("curl_init"))
  329. {
  330. $langs->load("errors");
  331. $this->error=$langs->trans("ErrorFunctionNotAvailableInPHP","curl_init");
  332. return -1;
  333. }
  334. if ($conf->adherent->enabled) // Synchro for members
  335. {
  336. if (! empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL))
  337. {
  338. if ($listes=='' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS)) $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  339. else $lists=explode(',',$listes);
  340. foreach ($lists as $list)
  341. {
  342. // Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
  343. $tmp=explode(':',$list);
  344. if (! empty($tmp[2]))
  345. {
  346. $list=$tmp[2];
  347. if ($object->element == 'member' && $tmp[0] == 'TYPE' && $object->type != $tmp[1]) // Filter on member type label
  348. {
  349. dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[1], LOG_DEBUG);
  350. continue;
  351. }
  352. if ($object->element == 'member' && $tmp[0] == 'CATEG' && ! in_array($tmp[1], $categstatic->containing($object->id, 'member', 'label'))) // Filter on member category
  353. {
  354. dol_syslog("We ignore list ".$list." because object member is not into category ".$tmp[1], LOG_DEBUG);
  355. continue;
  356. }
  357. }
  358. //We call Mailman to unsubscribe the user
  359. $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL, $list);
  360. if ($result === false)
  361. {
  362. $this->mlremoved_ko[$list]=$object->email;
  363. return -2;
  364. }
  365. else $this->mlremoved_ok[$list]=$object->email;
  366. }
  367. return count($lists);
  368. }
  369. else
  370. {
  371. $this->error="ADHERENT_MAILMAN_UNSUB_URL not defined";
  372. return -1;
  373. }
  374. }
  375. }
  376. }
  377. ?>