PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/mailmanspip/class/mailmanspip.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 400 lines | 250 code | 53 blank | 97 comment | 62 complexity | 47f50f0782935e9f8cac4c6c2e8aaa5e MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  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-2011 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 members of a foundation
  27. */
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  31. /**
  32. * Class to manage mailman and spip
  33. */
  34. class MailmanSpip
  35. {
  36. var $db;
  37. var $error;
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. function __construct($db = '')
  44. {
  45. $this->db = $db;
  46. }
  47. /**
  48. * Function used to check if SPIP is enabled on the system
  49. *
  50. * @return boolean
  51. */
  52. function isSpipEnabled()
  53. {
  54. if (defined("ADHERENT_USE_SPIP") && (ADHERENT_USE_SPIP == 1))
  55. {
  56. return true;
  57. }
  58. return false;
  59. }
  60. /**
  61. * Function used to check if the SPIP config is correct
  62. *
  63. * @return boolean
  64. */
  65. function checkSpipConfig()
  66. {
  67. if (defined('ADHERENT_SPIP_SERVEUR') && defined('ADHERENT_SPIP_USER') && defined('ADHERENT_SPIP_PASS') && defined('ADHERENT_SPIP_DB'))
  68. {
  69. if (ADHERENT_SPIP_SERVEUR != '' && ADHERENT_SPIP_USER != '' && ADHERENT_SPIP_PASS != '' && ADHERENT_SPIP_DB != '')
  70. {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. /**
  77. * Function used to connect to SPIP
  78. *
  79. * @return boolean|DoliDB Boolean of DoliDB
  80. */
  81. function connectSpip()
  82. {
  83. $resource = getDoliDBInstance('mysql', ADHERENT_SPIP_SERVEUR, ADHERENT_SPIP_USER, ADHERENT_SPIP_PASS, ADHERENT_SPIP_DB, ADHERENT_SPIP_PORT);
  84. if ($resource->ok)
  85. {
  86. return $resource;
  87. }
  88. dol_syslog('Error when connecting to SPIP '.ADHERENT_SPIP_SERVEUR.' '.ADHERENT_SPIP_USER.' '.ADHERENT_SPIP_PASS.' '.ADHERENT_SPIP_DB, LOG_ERR);
  89. return false;
  90. }
  91. /**
  92. * Function used to connect to Mailman
  93. *
  94. * @param object $object Object with the data
  95. * @param string $url Mailman URL to be called with patterns
  96. * @return mixed Boolean or string
  97. */
  98. function callMailman($object, $url)
  99. {
  100. global $conf;
  101. //Patterns that are going to be replaced with their original value
  102. $patterns = array(
  103. '%LISTE%',
  104. '%EMAIL%',
  105. '%PASSWORD%',
  106. '%MAILMAN_ADMINPW%'
  107. );
  108. $replace = array(
  109. $list,
  110. $object->email,
  111. $object->pass,
  112. $conf->global->ADHERENT_MAILMAN_ADMINPW
  113. );
  114. $curl_url = str_replace($patterns, $replace, $url);
  115. dol_syslog('Calling Mailman: '.$curl_url);
  116. $ch = curl_init($curl_url);
  117. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  118. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  119. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  120. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  121. $result = curl_exec($ch);
  122. dol_syslog('result curl_exec='.$result);
  123. //An error was found, we store it in $this->error for later
  124. if ($result === false || curl_errno($ch) > 0)
  125. {
  126. $this->error = curl_errno($ch).' '.curl_error($ch);
  127. dol_syslog('Error using curl '.$this->error, LOG_ERR);
  128. }
  129. curl_close($ch);
  130. return $result;
  131. }
  132. /**
  133. * Fonction qui donne les droits redacteurs dans spip
  134. *
  135. * @param Object $object Object with data (->firstname, ->lastname, ->email and ->login)
  136. * @return int =0 if KO, >0 if OK
  137. */
  138. function add_to_spip($object)
  139. {
  140. dol_syslog(get_class($this)."::add_to_spip");
  141. if ($this->isSpipEnabled())
  142. {
  143. if ($this->checkSpipConfig())
  144. {
  145. $mydb = $this->connectSpip();
  146. if ($mydb)
  147. {
  148. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  149. $mdpass=dol_hash($object->pass);
  150. $htpass=crypt($object->pass,makesalt());
  151. $query = "INSERT INTO spip_auteurs (nom, email, login, pass, htpass, alea_futur, statut) VALUES(\"".$object->firstname." ".$object->lastname."\",\"".$object->email."\",\"".$object->login."\",\"$mdpass\",\"$htpass\",FLOOR(32000*RAND()),\"1comite\")";
  152. $result = $mydb->query($query);
  153. $mydb->close();
  154. if ($result)
  155. {
  156. return 1;
  157. }
  158. else $this->error = $mydb->lasterror();
  159. }
  160. else $this->error = 'Failed to connect to SPIP';
  161. }
  162. else $this->error = 'BadSPIPConfiguration';
  163. }
  164. else $this->error = 'SPIPNotEnabled';
  165. return 0;
  166. }
  167. /**
  168. * Fonction qui enleve les droits redacteurs dans spip
  169. *
  170. * @param Object $object Object with data (->login)
  171. * @return int =0 if KO, >0 if OK
  172. */
  173. function del_to_spip($object)
  174. {
  175. dol_syslog(get_class($this)."::del_to_spip");
  176. if ($this->isSpipEnabled())
  177. {
  178. if ($this->checkSpipConfig())
  179. {
  180. $mydb = $this->connectSpip();
  181. if ($mydb)
  182. {
  183. $query = "DELETE FROM spip_auteurs WHERE login='".$object->login."'";
  184. $result = $mydb->query($query);
  185. $mydb->close();
  186. if ($result)
  187. {
  188. return 1;
  189. }
  190. else $this->error = $mydb->lasterror();
  191. }
  192. else $this->error = 'Failed to connect to SPIP';
  193. }
  194. else $this->error = 'BadSPIPConfiguration';
  195. }
  196. else $this->error = 'SPIPNotEnabled';
  197. return 0;
  198. }
  199. /**
  200. * Fonction qui dit si cet utilisateur est un redacteur existant dans spip
  201. *
  202. * @param Object $object Object with data (->login)
  203. * @return int 1=exists, 0=does not exists, -1=error
  204. */
  205. function is_in_spip($object)
  206. {
  207. if ($this->isSpipEnabled())
  208. {
  209. if ($this->checkSpipConfig())
  210. {
  211. $mydb = $this->connectSpip();
  212. if ($mydb)
  213. {
  214. $query = "SELECT login FROM spip_auteurs WHERE login='".$object->login."'";
  215. $result = $mydb->query($query);
  216. if ($result)
  217. {
  218. if ($mydb->num_rows($result))
  219. {
  220. // nous avons au moins une reponse
  221. $mydb->close($result);
  222. return 1;
  223. }
  224. else
  225. {
  226. // nous n'avons pas de reponse => n'existe pas
  227. $mydb->close($result);
  228. return 0;
  229. }
  230. }
  231. else
  232. {
  233. $this->error = $mydb->lasterror();
  234. $mydb->close();
  235. }
  236. }
  237. else $this->error = 'Failed to connect to SPIP';
  238. }
  239. else $this->error = 'BadSPIPConfiguration';
  240. }
  241. else $this->error = 'SPIPNotEnabled';
  242. return -1;
  243. }
  244. /**
  245. * Subscribe an email to all mailing-lists
  246. *
  247. * @param Object $object Object with data (->email, ->pass, ->element, ->type)
  248. * @param array $listes To force mailing-list (string separated with ,)
  249. * @return int <0 if KO, >=0 if OK
  250. */
  251. function add_to_mailman($object,$listes='')
  252. {
  253. global $conf,$langs,$user;
  254. dol_syslog(get_class($this)."::add_to_mailman");
  255. if (! function_exists("curl_init"))
  256. {
  257. $langs->load("errors");
  258. $this->error=$langs->trans("ErrorFunctionNotAvailableInPHP","curl_init");
  259. return -1;
  260. }
  261. if (! empty($conf->global->ADHERENT_MAILMAN_URL))
  262. {
  263. if ($listes == '' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS))
  264. {
  265. $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  266. }
  267. else
  268. {
  269. $lists=explode(',',$listes);
  270. }
  271. foreach ($lists as $list)
  272. {
  273. // Filter on type something (ADHERENT_MAILMAN_LISTS = "filtervalue:mailinglist1,filtervalue2:mailinglist2,mailinglist3")
  274. $tmp=explode(':',$list);
  275. if (! empty($tmp[1]))
  276. {
  277. $list=$tmp[1];
  278. if ($object->element == 'member' && $object->type != $tmp[0]) // Filter on member type label
  279. {
  280. dol_syslog("We ignore list ".$list." because object member type ".$object->type." does not match ".$tmp[0], LOG_DEBUG);
  281. continue;
  282. }
  283. }
  284. //We call Mailman to subscribe the user
  285. $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_URL);
  286. if ($result === false)
  287. {
  288. return -2;
  289. }
  290. }
  291. return count($lists);
  292. }
  293. else
  294. {
  295. $this->error="ADHERENT_MAILMAN_URL not defined";
  296. return -1;
  297. }
  298. }
  299. /**
  300. * Unsubscribe an email from all mailing-lists
  301. * Used when a user is resiliated
  302. *
  303. * @param Object $object Object with data (->email, ->pass, ->element, ->type)
  304. * @param array $listes To force mailing-list (string separated with ,)
  305. * @return int <0 if KO, >=0 if OK
  306. */
  307. function del_to_mailman($object,$listes='')
  308. {
  309. global $conf,$langs,$user;
  310. if (! empty($conf->global->ADHERENT_MAILMAN_UNSUB_URL))
  311. {
  312. if ($listes=='' && ! empty($conf->global->ADHERENT_MAILMAN_LISTS))
  313. {
  314. $lists=explode(',',$conf->global->ADHERENT_MAILMAN_LISTS);
  315. }
  316. else
  317. {
  318. $lists=explode(',',$listes);
  319. }
  320. foreach ($lists as $list)
  321. {
  322. // Filter on type something (ADHERENT_MAILMAN_LISTS = "filtervalue:mailinglist1,filtervalue2:mailinglist2,mailinglist3")
  323. $tmp=explode(':',$list);
  324. if (! empty($tmp[1]))
  325. {
  326. if ($object->element == 'member' && $object->type != $tmp[1]) // Filter on member type label
  327. {
  328. continue;
  329. }
  330. }
  331. //We call Mailman to unsubscribe the user
  332. $result = $this->callMailman($object, $conf->global->ADHERENT_MAILMAN_UNSUB_URL);
  333. if ($result === false)
  334. {
  335. return -2;
  336. }
  337. }
  338. return count($lists);
  339. }
  340. else
  341. {
  342. $this->error="ADHERENT_MAILMAN_UNSUB_URL not defined";
  343. return -1;
  344. }
  345. }
  346. }
  347. ?>