mailmanspip.class.php 12 KB

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