2012-06-10 21:49:19 +02:00
< ? php
2018-10-15 21:30:57 +02:00
/* Copyright ( C ) 2002 - 2003 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2002 - 2003 Jean - Louis Bergamo < jlb @ j1b . org >
* Copyright ( C ) 2004 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2009 Regis Houssin < regis . houssin @ inodbox . com >
2018-10-15 21:30:57 +02:00
* Copyright ( C ) 2012 Marcos García < marcosgdf @ gmail . com >
* Copyright ( C ) 2018 Frédéric France < frederic . france @ netlogic . fr >
2012-06-10 21:49:19 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-06-10 21:49:19 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-06-10 21:49:19 +02:00
*/
/**
* \file htdocs / mailmanspip / class / mailmanspip . class . php
* \ingroup member
2013-06-09 12:56:20 +02:00
* \brief File of class to manage mailman and spip actions
2012-06-10 21:49:19 +02:00
*/
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2013-06-09 12:56:20 +02:00
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2012-06-10 21:49:19 +02:00
/**
* Class to manage mailman and spip
*/
class MailmanSpip
{
2018-08-22 11:11:59 +02:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-02 22:05:19 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2020-04-10 10:59:32 +02:00
public $error = '' ;
2012-06-10 21:49:19 +02:00
2018-10-15 21:30:57 +02:00
/**
2018-10-15 21:32:27 +02:00
* @ var string [] Array of error strings
*/
public $errors = array ();
2018-10-15 21:30:57 +02:00
2018-10-06 12:34:51 +02:00
public $mladded_ok ;
public $mladded_ko ;
public $mlremoved_ok ;
public $mlremoved_ko ;
2013-03-30 18:04:33 +01:00
2012-06-10 21:49:19 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
2019-03-02 00:14:22 +01:00
public function __construct ( $db )
2012-06-10 21:49:19 +02:00
{
$this -> db = $db ;
}
2012-08-28 07:02:00 +02:00
/**
* Function used to check if SPIP is enabled on the system
2012-09-08 02:17:08 +02:00
*
2012-08-28 07:02:00 +02:00
* @ return boolean
*/
2019-03-02 00:14:22 +01:00
public function isSpipEnabled ()
2012-08-28 07:02:00 +02:00
{
if ( defined ( " ADHERENT_USE_SPIP " ) && ( ADHERENT_USE_SPIP == 1 ))
{
return true ;
}
return false ;
}
/**
* Function used to check if the SPIP config is correct
2012-09-08 02:17:08 +02:00
*
2012-08-28 07:02:00 +02:00
* @ return boolean
*/
2019-03-02 00:14:22 +01:00
public function checkSpipConfig ()
2012-08-28 07:02:00 +02:00
{
if ( defined ( 'ADHERENT_SPIP_SERVEUR' ) && defined ( 'ADHERENT_SPIP_USER' ) && defined ( 'ADHERENT_SPIP_PASS' ) && defined ( 'ADHERENT_SPIP_DB' ))
{
if ( ADHERENT_SPIP_SERVEUR != '' && ADHERENT_SPIP_USER != '' && ADHERENT_SPIP_PASS != '' && ADHERENT_SPIP_DB != '' )
{
return true ;
}
}
return false ;
}
/**
* Function used to connect to SPIP
2012-09-08 02:17:08 +02:00
*
* @ return boolean | DoliDB Boolean of DoliDB
2012-08-28 07:02:00 +02:00
*/
2019-03-02 00:14:22 +01:00
public function connectSpip ()
2012-08-28 07:02:00 +02:00
{
$resource = getDoliDBInstance ( 'mysql' , ADHERENT_SPIP_SERVEUR , ADHERENT_SPIP_USER , ADHERENT_SPIP_PASS , ADHERENT_SPIP_DB , ADHERENT_SPIP_PORT );
if ( $resource -> ok )
{
return $resource ;
}
2012-09-08 02:17:08 +02:00
2012-08-28 07:02:00 +02:00
dol_syslog ( 'Error when connecting to SPIP ' . ADHERENT_SPIP_SERVEUR . ' ' . ADHERENT_SPIP_USER . ' ' . ADHERENT_SPIP_PASS . ' ' . ADHERENT_SPIP_DB , LOG_ERR );
return false ;
}
/**
* Function used to connect to Mailman
2012-09-08 02:17:08 +02:00
*
2014-07-21 18:42:17 +02:00
* @ param Adherent $object Object with the data
2012-10-22 01:16:29 +02:00
* @ param string $url Mailman URL to be called with patterns
* @ param string $list Name of mailing - list
* @ return mixed Boolean or string
2012-08-28 07:02:00 +02:00
*/
2012-10-22 01:16:29 +02:00
private function callMailman ( $object , $url , $list )
2012-08-28 07:02:00 +02:00
{
global $conf ;
//Patterns that are going to be replaced with their original value
$patterns = array (
'%LISTE%' ,
'%EMAIL%' ,
'%PASSWORD%' ,
'%MAILMAN_ADMINPW%'
);
$replace = array (
$list ,
$object -> email ,
$object -> pass ,
$conf -> global -> ADHERENT_MAILMAN_ADMINPW
);
$curl_url = str_replace ( $patterns , $replace , $url );
dol_syslog ( 'Calling Mailman: ' . $curl_url );
$ch = curl_init ( $curl_url );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt ( $ch , CURLOPT_FAILONERROR , true );
@ curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 );
2020-04-10 10:59:32 +02:00
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , empty ( $conf -> global -> MAIN_USE_CONNECT_TIMEOUT ) ? 5 : $conf -> global -> MAIN_USE_CONNECT_TIMEOUT );
curl_setopt ( $ch , CURLOPT_TIMEOUT , empty ( $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT ) ? 30 : $conf -> global -> MAIN_USE_RESPONSE_TIMEOUT );
2012-10-28 13:57:21 +01:00
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , false );
2014-07-19 16:47:51 +02:00
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , false );
2012-09-08 02:17:08 +02:00
2012-08-28 07:02:00 +02:00
$result = curl_exec ( $ch );
dol_syslog ( 'result curl_exec=' . $result );
2012-09-08 02:17:08 +02:00
//An error was found, we store it in $this->error for later
2012-08-28 07:02:00 +02:00
if ( $result === false || curl_errno ( $ch ) > 0 )
{
$this -> error = curl_errno ( $ch ) . ' ' . curl_error ( $ch );
dol_syslog ( 'Error using curl ' . $this -> error , LOG_ERR );
}
curl_close ( $ch );
return $result ;
}
2019-03-02 00:14:22 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-06-10 21:49:19 +02:00
/**
* Fonction qui donne les droits redacteurs dans spip
*
2015-03-17 00:21:17 +01:00
* @ param Adherent $object Object with data ( -> firstname , -> lastname , -> email and -> login )
2012-06-10 21:49:19 +02:00
* @ return int = 0 if KO , > 0 if OK
*/
2019-03-02 00:14:22 +01:00
public function add_to_spip ( $object )
2012-06-10 21:49:19 +02:00
{
2018-09-02 22:05:19 +02:00
// phpcs:enable
2012-06-10 21:49:19 +02:00
dol_syslog ( get_class ( $this ) . " ::add_to_spip " );
2012-08-28 07:02:00 +02:00
if ( $this -> isSpipEnabled ())
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
if ( $this -> checkSpipConfig ())
{
$mydb = $this -> connectSpip ();
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
if ( $mydb )
{
require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php' ;
2020-04-10 10:59:32 +02:00
$mdpass = dol_hash ( $object -> pass );
$htpass = crypt ( $object -> pass , makesalt ());
2019-01-27 11:55:16 +01:00
$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 \" ) " ;
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
$result = $mydb -> query ( $query );
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
$mydb -> close ();
if ( $result )
{
return 1 ;
2020-05-21 15:05:19 +02:00
} else $this -> error = $mydb -> lasterror ();
} else $this -> error = 'Failed to connect to SPIP' ;
} else $this -> error = 'BadSPIPConfiguration' ;
} else $this -> error = 'SPIPNotEnabled' ;
2012-09-08 02:17:08 +02:00
2012-08-28 07:02:00 +02:00
return 0 ;
2012-06-10 21:49:19 +02:00
}
2019-03-02 00:14:22 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-06-10 21:49:19 +02:00
/**
* Fonction qui enleve les droits redacteurs dans spip
*
2015-03-17 00:21:17 +01:00
* @ param Adherent $object Object with data ( -> login )
2012-06-10 21:49:19 +02:00
* @ return int = 0 if KO , > 0 if OK
*/
2019-03-02 00:14:22 +01:00
public function del_to_spip ( $object )
2012-06-10 21:49:19 +02:00
{
2018-09-02 22:05:19 +02:00
// phpcs:enable
2012-08-28 07:02:00 +02:00
dol_syslog ( get_class ( $this ) . " ::del_to_spip " );
if ( $this -> isSpipEnabled ())
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
if ( $this -> checkSpipConfig ())
{
$mydb = $this -> connectSpip ();
if ( $mydb )
{
$query = " DELETE FROM spip_auteurs WHERE login=' " . $object -> login . " ' " ;
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
$result = $mydb -> query ( $query );
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
$mydb -> close ();
if ( $result )
{
return 1 ;
2020-05-21 15:05:19 +02:00
} else $this -> error = $mydb -> lasterror ();
} else $this -> error = 'Failed to connect to SPIP' ;
} else $this -> error = 'BadSPIPConfiguration' ;
} else $this -> error = 'SPIPNotEnabled' ;
2012-09-08 02:17:08 +02:00
2012-08-28 07:02:00 +02:00
return 0 ;
2012-06-10 21:49:19 +02:00
}
2019-03-02 00:14:22 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-06-10 21:49:19 +02:00
/**
* Fonction qui dit si cet utilisateur est un redacteur existant dans spip
*
2015-02-10 10:45:48 +01:00
* @ param object $object Object with data ( -> login )
2012-06-10 21:49:19 +02:00
* @ return int 1 = exists , 0 = does not exists , - 1 = error
*/
2019-03-02 00:14:22 +01:00
public function is_in_spip ( $object )
2012-06-10 21:49:19 +02:00
{
2018-09-02 22:05:19 +02:00
// phpcs:enable
2012-08-28 07:02:00 +02:00
if ( $this -> isSpipEnabled ())
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
if ( $this -> checkSpipConfig ())
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
$mydb = $this -> connectSpip ();
2012-06-10 21:49:19 +02:00
2012-08-28 07:02:00 +02:00
if ( $mydb )
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
$query = " SELECT login FROM spip_auteurs WHERE login=' " . $object -> login . " ' " ;
$result = $mydb -> query ( $query );
if ( $result )
2012-06-10 21:49:19 +02:00
{
2012-08-28 07:02:00 +02:00
if ( $mydb -> num_rows ( $result ))
{
// nous avons au moins une reponse
$mydb -> close ( $result );
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2012-08-28 07:02:00 +02:00
// nous n'avons pas de reponse => n'existe pas
$mydb -> close ( $result );
return 0 ;
}
2020-05-21 15:05:19 +02:00
} else {
2012-08-28 07:02:00 +02:00
$this -> error = $mydb -> lasterror ();
$mydb -> close ();
2012-06-10 21:49:19 +02:00
}
2020-05-21 15:05:19 +02:00
} else $this -> error = 'Failed to connect to SPIP' ;
} else $this -> error = 'BadSPIPConfiguration' ;
} else $this -> error = 'SPIPNotEnabled' ;
2012-08-28 07:02:00 +02:00
return - 1 ;
2012-06-10 21:49:19 +02:00
}
2019-03-02 00:14:22 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-06-10 21:49:19 +02:00
/**
* Subscribe an email to all mailing - lists
*
2015-03-17 00:21:17 +01:00
* @ param Adherent $object Object with data ( -> email , -> pass , -> element , -> type )
2012-06-10 21:49:19 +02:00
* @ param array $listes To force mailing - list ( string separated with ,)
* @ return int < 0 if KO , >= 0 if OK
*/
2019-03-02 00:14:22 +01:00
public function add_to_mailman ( $object , $listes = '' )
2012-06-10 21:49:19 +02:00
{
2018-09-02 22:05:19 +02:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $conf , $langs , $user ;
2012-06-10 21:49:19 +02:00
dol_syslog ( get_class ( $this ) . " ::add_to_mailman " );
2020-04-10 10:59:32 +02:00
$this -> mladded_ok = array ();
$this -> mladded_ko = array ();
2013-03-30 18:04:33 +01:00
2020-04-10 10:59:32 +02:00
if ( ! function_exists ( " curl_init " ))
2012-06-10 21:49:19 +02:00
{
$langs -> load ( " errors " );
2020-04-10 10:59:32 +02:00
$this -> error = $langs -> trans ( " ErrorFunctionNotAvailableInPHP " , " curl_init " );
2012-06-10 21:49:19 +02:00
return - 1 ;
}
2013-06-05 15:44:42 +02:00
if ( $conf -> adherent -> enabled ) // Synchro for members
2012-06-10 21:49:19 +02:00
{
2020-04-10 10:59:32 +02:00
if ( ! empty ( $conf -> global -> ADHERENT_MAILMAN_URL ))
2013-06-05 15:44:42 +02:00
{
2020-04-10 10:59:32 +02:00
if ( $listes == '' && ! empty ( $conf -> global -> ADHERENT_MAILMAN_LISTS )) $lists = explode ( ',' , $conf -> global -> ADHERENT_MAILMAN_LISTS );
else $lists = explode ( ',' , $listes );
2013-06-05 15:44:42 +02:00
2020-04-10 10:59:32 +02:00
$categstatic = new Categorie ( $this -> db );
2013-06-05 15:44:42 +02:00
foreach ( $lists as $list )
{
// Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
2020-04-10 10:59:32 +02:00
$tmp = explode ( ':' , $list );
if ( ! empty ( $tmp [ 2 ]))
2013-06-05 15:44:42 +02:00
{
2020-04-10 10:59:32 +02:00
$list = $tmp [ 2 ];
2013-06-05 15:44:42 +02:00
if ( $object -> element == 'member' && $tmp [ 0 ] == 'TYPE' && $object -> type != $tmp [ 1 ]) // Filter on member type label
{
dol_syslog ( " We ignore list " . $list . " because object member type " . $object -> type . " does not match " . $tmp [ 1 ], LOG_DEBUG );
continue ;
}
2020-04-10 10:59:32 +02:00
if ( $object -> element == 'member' && $tmp [ 0 ] == 'CATEG' && ! in_array ( $tmp [ 1 ], $categstatic -> containing ( $object -> id , 'member' , 'label' ))) // Filter on member category
2013-06-05 15:44:42 +02:00
{
dol_syslog ( " We ignore list " . $list . " because object member is not into category " . $tmp [ 1 ], LOG_DEBUG );
continue ;
}
}
//We call Mailman to subscribe the user
$result = $this -> callMailman ( $object , $conf -> global -> ADHERENT_MAILMAN_URL , $list );
if ( $result === false )
{
2020-04-10 10:59:32 +02:00
$this -> mladded_ko [ $list ] = $object -> email ;
2013-06-05 15:44:42 +02:00
return - 2 ;
2020-05-21 15:05:19 +02:00
} else $this -> mladded_ok [ $list ] = $object -> email ;
2013-06-05 15:44:42 +02:00
}
return count ( $lists );
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = " ADHERENT_MAILMAN_URL not defined " ;
2013-06-05 15:44:42 +02:00
return - 1 ;
}
2012-06-10 21:49:19 +02:00
}
}
2019-03-02 00:14:22 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2012-06-10 21:49:19 +02:00
/**
* Unsubscribe an email from all mailing - lists
* Used when a user is resiliated
*
2015-03-17 00:21:17 +01:00
* @ param Adherent $object Object with data ( -> email , -> pass , -> element , -> type )
2012-06-10 21:49:19 +02:00
* @ param array $listes To force mailing - list ( string separated with ,)
* @ return int < 0 if KO , >= 0 if OK
*/
2019-03-02 00:14:22 +01:00
public function del_to_mailman ( $object , $listes = '' )
2012-06-10 21:49:19 +02:00
{
2018-09-02 22:05:19 +02:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $conf , $langs , $user ;
2012-06-10 21:49:19 +02:00
2013-03-30 18:04:33 +01:00
dol_syslog ( get_class ( $this ) . " ::del_to_mailman " );
2020-04-10 10:59:32 +02:00
$this -> mlremoved_ok = array ();
$this -> mlremoved_ko = array ();
2013-03-30 18:04:33 +01:00
2020-04-10 10:59:32 +02:00
if ( ! function_exists ( " curl_init " ))
2013-03-30 18:04:33 +01:00
{
$langs -> load ( " errors " );
2020-04-10 10:59:32 +02:00
$this -> error = $langs -> trans ( " ErrorFunctionNotAvailableInPHP " , " curl_init " );
2013-03-30 18:04:33 +01:00
return - 1 ;
}
2013-06-05 15:44:42 +02:00
if ( $conf -> adherent -> enabled ) // Synchro for members
2012-06-10 21:49:19 +02:00
{
2020-04-10 10:59:32 +02:00
if ( ! empty ( $conf -> global -> ADHERENT_MAILMAN_UNSUB_URL ))
2013-06-05 15:44:42 +02:00
{
2020-04-10 10:59:32 +02:00
if ( $listes == '' && ! empty ( $conf -> global -> ADHERENT_MAILMAN_LISTS )) $lists = explode ( ',' , $conf -> global -> ADHERENT_MAILMAN_LISTS );
else $lists = explode ( ',' , $listes );
2013-06-05 15:44:42 +02:00
2020-04-10 10:59:32 +02:00
$categstatic = new Categorie ( $this -> db );
2013-08-31 16:58:25 +02:00
2013-06-05 15:44:42 +02:00
foreach ( $lists as $list )
{
2013-06-06 18:31:33 +02:00
// Filter on type something (ADHERENT_MAILMAN_LISTS = "mailinglist0,TYPE:typevalue:mailinglist1,CATEG:categvalue:mailinglist2")
2020-04-10 10:59:32 +02:00
$tmp = explode ( ':' , $list );
if ( ! empty ( $tmp [ 2 ]))
2013-06-06 18:31:33 +02:00
{
2020-04-10 10:59:32 +02:00
$list = $tmp [ 2 ];
2013-06-06 18:31:33 +02:00
if ( $object -> element == 'member' && $tmp [ 0 ] == 'TYPE' && $object -> type != $tmp [ 1 ]) // Filter on member type label
{
dol_syslog ( " We ignore list " . $list . " because object member type " . $object -> type . " does not match " . $tmp [ 1 ], LOG_DEBUG );
continue ;
}
2020-04-10 10:59:32 +02:00
if ( $object -> element == 'member' && $tmp [ 0 ] == 'CATEG' && ! in_array ( $tmp [ 1 ], $categstatic -> containing ( $object -> id , 'member' , 'label' ))) // Filter on member category
2013-06-06 18:31:33 +02:00
{
dol_syslog ( " We ignore list " . $list . " because object member is not into category " . $tmp [ 1 ], LOG_DEBUG );
continue ;
}
2013-06-05 15:53:27 +02:00
}
2013-06-05 15:44:42 +02:00
//We call Mailman to unsubscribe the user
$result = $this -> callMailman ( $object , $conf -> global -> ADHERENT_MAILMAN_UNSUB_URL , $list );
if ( $result === false )
{
2020-04-10 10:59:32 +02:00
$this -> mlremoved_ko [ $list ] = $object -> email ;
2013-06-05 15:44:42 +02:00
return - 2 ;
2020-05-21 15:05:19 +02:00
} else $this -> mlremoved_ok [ $list ] = $object -> email ;
2013-06-05 15:44:42 +02:00
}
return count ( $lists );
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = " ADHERENT_MAILMAN_UNSUB_URL not defined " ;
2013-06-05 15:44:42 +02:00
return - 1 ;
}
2012-06-10 21:49:19 +02:00
}
}
}