mailmanspip.class.php 12 KB

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