mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-22 09:59:06 +01:00
LSaddons: Code cleaning
This commit is contained in:
parent
425df3da3a
commit
0eb0143921
8 changed files with 1093 additions and 1078 deletions
|
@ -39,88 +39,88 @@ LSerror :: defineError('ASTERISK_04',
|
||||||
___("Asterisk: The LDAP attribute type of the attribute %{attr} is incorrect: its must be password.")
|
___("Asterisk: The LDAP attribute type of the attribute %{attr} is incorrect: its must be password.")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check support of Asterisk by LdapSaisie
|
* Check support of Asterisk by LdapSaisie
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true if Asterisk is totally supported, false in other case
|
* @return boolean true if Asterisk is totally supported, false in other case
|
||||||
*/
|
*/
|
||||||
function LSaddon_asterisk_support() {
|
function LSaddon_asterisk_support() {
|
||||||
$retval=true;
|
$retval=true;
|
||||||
|
|
||||||
$MUST_DEFINE_CONST= array(
|
$MUST_DEFINE_CONST= array(
|
||||||
'LS_ASTERISK_HASH_PWD_FORMAT',
|
'LS_ASTERISK_HASH_PWD_FORMAT',
|
||||||
'LS_ASTERISK_USERPASSWORD_ATTR',
|
'LS_ASTERISK_USERPASSWORD_ATTR',
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach($MUST_DEFINE_CONST as $const) {
|
foreach($MUST_DEFINE_CONST as $const) {
|
||||||
if ( (!defined($const)) || (constant($const) == "")) {
|
if ( (!defined($const)) || (constant($const) == "")) {
|
||||||
LSerror :: addErrorCode('ASTERISK_SUPPORT_01',$const);
|
LSerror :: addErrorCode('ASTERISK_SUPPORT_01',$const);
|
||||||
$retval=false;
|
$retval=false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make asterisk password hash
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* Hash password in MD5 respecting the LSformat LS_ASTERISK_HASH_PWD_FORMAT.
|
|
||||||
*
|
|
||||||
* This function can be used as encode_function of LSattr_ldap :: password.
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject The LSldapObject use to build the hashed password
|
|
||||||
* @param string $clearPassword The password in clear text
|
|
||||||
*
|
|
||||||
* @return string|false The hashed password, or false
|
|
||||||
*/
|
|
||||||
function hashAsteriskPassword($ldapObject,$clearPassword) {
|
|
||||||
if (!is_a($ldapObject,'LSldapObject')) {
|
|
||||||
LSerror :: addErrorCode('ASTERISK_01',array('function' => 'hashAsteriskPassword', 'objectName' => 'LSldapObject'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!is_string($clearPassword)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$ldapObject -> registerOtherValue('clearPassword',$clearPassword);
|
|
||||||
return md5($ldapObject->getFData(LS_ASTERISK_HASH_PWD_FORMAT));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate asterisk MD5 hashed password
|
* Make asterisk password hash
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @param LSldapObject $ldapObject The LSldapObject
|
* Hash password in MD5 respecting the LSformat LS_ASTERISK_HASH_PWD_FORMAT.
|
||||||
*
|
*
|
||||||
* @return string|false asterisk MD5 hashed password or False
|
* This function can be used as encode_function of LSattr_ldap :: password.
|
||||||
*/
|
*
|
||||||
function generate_asteriskMD5HashedPassword($ldapObject) {
|
* @param LSldapObject $ldapObject The LSldapObject use to build the hashed password
|
||||||
if ( get_class($ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
* @param string $clearPassword The password in clear text
|
||||||
LSerror :: addErrorCode('ASTERISK_02',array(LS_ASTERISK_USERPASSWORD_ATTR));
|
*
|
||||||
return false;
|
* @return string|false The hashed password, or false
|
||||||
}
|
*/
|
||||||
|
function hashAsteriskPassword($ldapObject,$clearPassword) {
|
||||||
if (
|
if (!is_a($ldapObject,'LSldapObject')) {
|
||||||
!is_a(
|
LSerror :: addErrorCode('ASTERISK_01',array('function' => 'hashAsteriskPassword', 'objectName' => 'LSldapObject'));
|
||||||
$ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ] -> ldap,
|
return false;
|
||||||
'LSattr_ldap_password'
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
LSerror :: addErrorCode('ASTERISK_04', LS_ASTERISK_USERPASSWORD_ATTR);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$password = $ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
|
||||||
if (!$password) {
|
|
||||||
LSerror :: addErrorCode('ASTERISK_03');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return hashAsteriskPassword($ldapObject,(string)$password);
|
|
||||||
}
|
}
|
||||||
|
if (!is_string($clearPassword)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$ldapObject -> registerOtherValue('clearPassword',$clearPassword);
|
||||||
|
return md5($ldapObject->getFData(LS_ASTERISK_HASH_PWD_FORMAT));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate asterisk MD5 hashed password
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject The LSldapObject
|
||||||
|
*
|
||||||
|
* @return string|false asterisk MD5 hashed password or False
|
||||||
|
*/
|
||||||
|
function generate_asteriskMD5HashedPassword($ldapObject) {
|
||||||
|
if ( get_class($ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
||||||
|
LSerror :: addErrorCode('ASTERISK_02',array(LS_ASTERISK_USERPASSWORD_ATTR));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!is_a(
|
||||||
|
$ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ] -> ldap,
|
||||||
|
'LSattr_ldap_password'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
LSerror :: addErrorCode('ASTERISK_04', LS_ASTERISK_USERPASSWORD_ATTR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$password = $ldapObject -> attrs[ LS_ASTERISK_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
||||||
|
if (!$password) {
|
||||||
|
LSerror :: addErrorCode('ASTERISK_03');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return hashAsteriskPassword($ldapObject,(string)$password);
|
||||||
|
}
|
||||||
|
|
|
@ -45,110 +45,110 @@ LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_03',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check support of exportSearchResultAsCSV
|
* Check support of exportSearchResultAsCSV
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true if exportSearchResultAsCSV is fully supported, false in other case
|
* @return boolean true if exportSearchResultAsCSV is fully supported, false in other case
|
||||||
*/
|
*/
|
||||||
function LSaddon_exportSearchResultAsCSV_support() {
|
function LSaddon_exportSearchResultAsCSV_support() {
|
||||||
$retval=true;
|
$retval=true;
|
||||||
|
|
||||||
// Check fputcsv function
|
// Check fputcsv function
|
||||||
if (!function_exists('fputcsv')) {
|
if (!function_exists('fputcsv')) {
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_01');
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_01');
|
||||||
|
}
|
||||||
|
|
||||||
|
$MUST_DEFINE_CONST= array(
|
||||||
|
'LS_EXPORTSEARCHRESULTASCSV_DELIMITER',
|
||||||
|
'LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE',
|
||||||
|
'LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR',
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($MUST_DEFINE_CONST as $const) {
|
||||||
|
if ( (!defined($const)) || (constant($const) == "")) {
|
||||||
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_02',$const);
|
||||||
|
$retval=false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$MUST_DEFINE_CONST= array(
|
return $retval;
|
||||||
'LS_EXPORTSEARCHRESULTASCSV_DELIMITER',
|
}
|
||||||
'LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE',
|
|
||||||
'LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR',
|
/**
|
||||||
|
* Write LSsearch result as CSV and force download of it.
|
||||||
|
*
|
||||||
|
* @param LSsearch $LSsearch The LSsearch object
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @return boolean Void if CSV file is successfully generated and upload, false in other case
|
||||||
|
*/
|
||||||
|
function exportSearchResultAsCSV($LSsearch) {
|
||||||
|
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
|
||||||
|
|
||||||
|
if ($csv === false) {
|
||||||
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_00');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$LSsearch -> run()) {
|
||||||
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_01');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$headers=array($LSsearch->label_objectName, 'DN');
|
||||||
|
if ($LSsearch->displaySubDn) $headers[]='Sub DN';
|
||||||
|
if ($LSsearch->visibleExtraDisplayedColumns) {
|
||||||
|
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf) {
|
||||||
|
$headers[] = __($conf['label']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!writeRowInCSV($csv, $headers)) {
|
||||||
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_02');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
foreach ($LSsearch -> getSearchEntries() as $e) {
|
||||||
|
$row = array(
|
||||||
|
$e -> displayName,
|
||||||
|
$e -> dn
|
||||||
);
|
);
|
||||||
|
if ($LSsearch->displaySubDn) $row[] = $e -> subDn;
|
||||||
|
if ($LSsearch->visibleExtraDisplayedColumns)
|
||||||
|
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf)
|
||||||
|
$row[] = $e -> $cid;
|
||||||
|
|
||||||
foreach($MUST_DEFINE_CONST as $const) {
|
if (!writeRowInCSV($csv, $row)) {
|
||||||
if ( (!defined($const)) || (constant($const) == "")) {
|
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_03');
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_02',$const);
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write LSsearch result as CSV and force download of it.
|
|
||||||
*
|
|
||||||
* @param LSsearch $LSsearch The LSsearch object
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @return boolean Void if CSV file is successfully generated and upload, false in other case
|
|
||||||
*/
|
|
||||||
function exportSearchResultAsCSV($LSsearch) {
|
|
||||||
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');
|
|
||||||
|
|
||||||
if ($csv === false) {
|
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_00');
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$LSsearch -> run()) {
|
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_01');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$headers=array($LSsearch->label_objectName, 'DN');
|
|
||||||
if ($LSsearch->displaySubDn) $headers[]='Sub DN';
|
|
||||||
if ($LSsearch->visibleExtraDisplayedColumns) {
|
|
||||||
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf) {
|
|
||||||
$headers[] = __($conf['label']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!writeRowInCSV($csv, $headers)) {
|
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_02');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
foreach ($LSsearch -> getSearchEntries() as $e) {
|
|
||||||
$row = array(
|
|
||||||
$e -> displayName,
|
|
||||||
$e -> dn
|
|
||||||
);
|
|
||||||
if ($LSsearch->displaySubDn) $row[] = $e -> subDn;
|
|
||||||
if ($LSsearch->visibleExtraDisplayedColumns)
|
|
||||||
foreach ($LSsearch->visibleExtraDisplayedColumns as $cid => $conf)
|
|
||||||
$row[] = $e -> $cid;
|
|
||||||
|
|
||||||
if (!writeRowInCSV($csv, $row)) {
|
|
||||||
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_03');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
header("Content-disposition: attachment; filename=export.csv");
|
|
||||||
header("Content-type: text/csv");
|
|
||||||
rewind($csv);
|
|
||||||
print stream_get_contents($csv);
|
|
||||||
@fclose($csv);
|
|
||||||
exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
header("Content-disposition: attachment; filename=export.csv");
|
||||||
* Write CSV row in file
|
header("Content-type: text/csv");
|
||||||
*
|
rewind($csv);
|
||||||
* @param resource &$csv The CSV file description reference
|
print stream_get_contents($csv);
|
||||||
* @param array<string> $row An array of a CSV row fields to write
|
@fclose($csv);
|
||||||
*
|
exit();
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
}
|
||||||
*
|
|
||||||
* @return boolean True if CSV row is successfully writed, false in other case
|
/**
|
||||||
*/
|
* Write CSV row in file
|
||||||
function writeRowInCSV(&$csv, &$row) {
|
*
|
||||||
if (!defined('PHP_VERSION_ID') or PHP_VERSION_ID < 50504) {
|
* @param resource &$csv The CSV file description reference
|
||||||
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE) !== false);
|
* @param array<string> $row An array of a CSV row fields to write
|
||||||
}
|
*
|
||||||
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE, LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR) !== false);
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @return boolean True if CSV row is successfully writed, false in other case
|
||||||
|
*/
|
||||||
|
function writeRowInCSV(&$csv, &$row) {
|
||||||
|
if (!defined('PHP_VERSION_ID') or PHP_VERSION_ID < 50504) {
|
||||||
|
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE) !== false);
|
||||||
}
|
}
|
||||||
|
return (fputcsv($csv, $row, LS_EXPORTSEARCHRESULTASCSV_DELIMITER, LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE, LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR) !== false);
|
||||||
|
}
|
||||||
|
|
|
@ -53,165 +53,170 @@ LSerror :: defineError('FTP_05',
|
||||||
___("FTP Support : Unable to rename folder from %{old} to %{new} on the remote server.")
|
___("FTP Support : Unable to rename folder from %{old} to %{new} on the remote server.")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verification du support FTP par ldapSaisie
|
* Check support of FTP addon
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true si FTP est pleinement supporté, false sinon
|
* @return boolean true if FTP addon is fully supported, false in other case
|
||||||
*/
|
*/
|
||||||
function LSaddon_ftp_support() {
|
function LSaddon_ftp_support() {
|
||||||
$retval=true;
|
$retval = true;
|
||||||
|
|
||||||
// Dependance de librairie
|
// Net_FTP lib dependency
|
||||||
if (!class_exists('Net_FTP')) {
|
if (!class_exists('Net_FTP')) {
|
||||||
if (!defined('NET_FTP')) {
|
if (!defined('NET_FTP')) {
|
||||||
LSerror :: addErrorCode('FTP_SUPPORT_02','NET_FTP');
|
LSerror :: addErrorCode('FTP_SUPPORT_02', 'NET_FTP');
|
||||||
$retval=false;
|
$retval = false;
|
||||||
} else if(!LSsession::includeFile(NET_FTP, true)) {
|
} else if(!LSsession::includeFile(NET_FTP, true)) {
|
||||||
LSerror :: addErrorCode('FTP_SUPPORT_01');
|
LSerror :: addErrorCode('FTP_SUPPORT_01');
|
||||||
$retval=false;
|
$retval = false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connexion a un serveur FTP
|
/**
|
||||||
*
|
* Connect to FTP server
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
*
|
||||||
*
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
* @param string $host Le nom ou l'IP du serveur FTP
|
*
|
||||||
* @param string $port Le port de connexion au serveur ftp
|
* @param string $host FTP server FQDN or IP address
|
||||||
* @param string $user Le nom d'utilidateur de connexion
|
* @param string $port The TCP port of the FTP server
|
||||||
* @param string $pwd Le mot de passe de connexion
|
* @param string $user The username
|
||||||
*
|
* @param string $pwd The password
|
||||||
* @return Net_FTP|false Net_FTP object en cas de succès, false sinon
|
*
|
||||||
*/
|
* @return Net_FTP|false Net_FTP object in case of success, false otherwise
|
||||||
function connectToFTP($host,$port,$user,$pwd) {
|
*/
|
||||||
$cnx = new Net_FTP();
|
function connectToFTP($host, $port, $user, $pwd) {
|
||||||
$do = $cnx -> connect($host,$port);
|
$cnx = new Net_FTP();
|
||||||
if (! $do instanceof PEAR_Error){
|
$do = $cnx -> connect($host, $port);
|
||||||
$do = $cnx -> login($user,$pwd);
|
if (!$do instanceof PEAR_Error){
|
||||||
if (! $do instanceof PEAR_Error) {
|
$do = $cnx -> login($user, $pwd);
|
||||||
return $cnx;
|
if (!$do instanceof PEAR_Error) {
|
||||||
}
|
return $cnx;
|
||||||
else {
|
|
||||||
LSerror :: addErrorCode('FTP_01',"2");
|
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
LSerror :: addErrorCode('FTP_01',"1");
|
LSerror :: addErrorCode('FTP_01', "2");
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
/**
|
LSerror :: addErrorCode('FTP_01', "1");
|
||||||
* Creation d'un ou plusieurs dossiers via FTP
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
*
|
return false;
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param string $host Le nom ou l'IP du serveur FTP
|
|
||||||
* @param string $port Le port de connexion au serveur ftp
|
|
||||||
* @param string $user Le nom d'utilidateur de connexion
|
|
||||||
* @param string $pwd Le mot de passe de connexion
|
|
||||||
* @param array $dirs ou string Le(s) dossier(s) à ajouter
|
|
||||||
*
|
|
||||||
* @return bool True ou false si il y a un problème durant la création du/des dossier(s)
|
|
||||||
*/
|
|
||||||
function createDirsByFTP($host,$port,$user,$pwd,$dirs,$chmod=NULL) {
|
|
||||||
$cnx = connectToFTP($host,$port,$user,$pwd);
|
|
||||||
if (!$cnx) return false;
|
|
||||||
if (!is_array($dirs)) {
|
|
||||||
$dirs = array($dirs);
|
|
||||||
}
|
|
||||||
foreach($dirs as $dir) {
|
|
||||||
$do = $cnx -> mkdir($dir,true);
|
|
||||||
if ($do instanceof PEAR_Error) {
|
|
||||||
LSerror :: addErrorCode('FTP_02',$dir);
|
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if ($chmod) {
|
|
||||||
$do = $cnx -> chmod($dir,$chmod);
|
|
||||||
if ($do instanceof PEAR_Error) {
|
|
||||||
LSerror :: addErrorCode('FTP_04',$dir);
|
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suppression d'un ou plusieurs dossiers via FTP
|
* Creation d'un ou plusieurs dossiers via FTP
|
||||||
*
|
*
|
||||||
* Note : Attention : suppression récursive. Cela veut dire que les sous-dossiers
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
* lister par un LS FTP seront supprimé d'abord. Attention : Si votre serveur
|
*
|
||||||
* FTP est configuré pour caché certains fichiers ou dossiers (dont le nom
|
* @param string $host Le nom ou l'IP du serveur FTP
|
||||||
* commence par un '.' par exempl), ces fichiers ne seront pas supprimés et la
|
* @param string $port Le port de connexion au serveur ftp
|
||||||
* suppression du dossier parent échoura.
|
* @param string $user Le nom d'utilidateur de connexion
|
||||||
*
|
* @param string $pwd Le mot de passe de connexion
|
||||||
* Pour VsFTPd : Ajouter force_dot_files=1 dans la configuration.
|
* @param array $dirs ou string Le(s) dossier(s) à ajouter
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @return bool True ou false si il y a un problème durant la création du/des dossier(s)
|
||||||
*
|
|
||||||
* @param string $host Le nom ou l'IP du serveur FTP
|
|
||||||
* @param string $port Le port de connexion au serveur ftp
|
|
||||||
* @param string $user Le nom d'utilidateur de connexion
|
|
||||||
* @param string $pwd Le mot de passe de connexion
|
|
||||||
* @param array $dirs ou string Le(s) dossier(s) à supprimer
|
|
||||||
*
|
|
||||||
* @return bool True ou false si il y a un problème durant la suppression du/des dossier(s)
|
|
||||||
*/
|
|
||||||
function removeDirsByFTP($host,$port,$user,$pwd,$dirs) {
|
|
||||||
$cnx = connectToFTP($host,$port,$user,$pwd);
|
|
||||||
if (!$cnx) return false;
|
|
||||||
if (!is_array($dirs)) {
|
|
||||||
$dirs = array($dirs);
|
|
||||||
}
|
|
||||||
foreach($dirs as $dir) {
|
|
||||||
if ($dir[strlen($dir)-1]!='/') {
|
|
||||||
$dir.='/';
|
|
||||||
}
|
|
||||||
$do = $cnx -> rm($dir,true);
|
|
||||||
if ($do instanceof PEAR_Error) {
|
|
||||||
LSerror :: addErrorCode('FTP_03',$dir);
|
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
* Create one or more directories throught FTP
|
||||||
* Renomage d'un dossier via FTP
|
*
|
||||||
*
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
*
|
||||||
*
|
* @param string $host FTP server FQDN or IP address
|
||||||
* @param string $host Le nom ou l'IP du serveur FTP
|
* @param string $port The TCP port of the FTP server
|
||||||
* @param string $port Le port de connexion au serveur ftp
|
* @param string $user The username
|
||||||
* @param string $user Le nom d'utilidateur de connexion
|
* @param string $pwd The password
|
||||||
* @param string $pwd Le mot de passe de connexion
|
* @param array|string $dirs The directory/ies to add
|
||||||
* @param string $old Le dossier à renomer
|
* @param int $chmod The directory/ies mode as an octal number (do not forget leading zero,
|
||||||
* @param string $new Le nouveau nom du dossier à renomer
|
* example: 0755 or 02755, default : default umask on the SSH server)
|
||||||
*
|
*
|
||||||
* @return bool True ou false si il y a un problème durant le renomage du/des dossier(s)
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
function renameDirByFTP($host,$port,$user,$pwd,$old,$new) {
|
function createDirsByFTP($host, $port, $user, $pwd, $dirs, $chmod=NULL) {
|
||||||
$cnx = connectToFTP($host,$port,$user,$pwd);
|
$cnx = connectToFTP($host, $port, $user, $pwd);
|
||||||
if (!$cnx) return false;
|
if (!$cnx) return false;
|
||||||
$do = $cnx -> rename($old,$new);
|
foreach(ensureIsArray($dirs) as $dir) {
|
||||||
|
$do = $cnx -> mkdir($dir, true);
|
||||||
if ($do instanceof PEAR_Error) {
|
if ($do instanceof PEAR_Error) {
|
||||||
LSerror :: addErrorCode('FTP_05',array('old' => $old,'new' => $new));
|
LSerror :: addErrorCode('FTP_02', $dir);
|
||||||
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
if ($chmod) {
|
||||||
|
$do = $cnx -> chmod($dir, $chmod);
|
||||||
|
if ($do instanceof PEAR_Error) {
|
||||||
|
LSerror :: addErrorCode('FTP_04', $dir);
|
||||||
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete one or more directories throught FTP
|
||||||
|
*
|
||||||
|
* Caution : recursive deletion ! The content of the directory will be listed and deleted before the
|
||||||
|
* directory. If your FTP server is configured to mask some files or directories (for instance,
|
||||||
|
* starting by '.'), they will not be listed and deleted and this may cause the deletion to fail.
|
||||||
|
* With VsFTPd, you have to add force_dot_files=1 in configuration.
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param string $host FTP server FQDN or IP address
|
||||||
|
* @param string $port The TCP port of the FTP server
|
||||||
|
* @param string $user The username
|
||||||
|
* @param string $pwd The password
|
||||||
|
* @param array|string $dirs The directory/ies to remove
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
function removeDirsByFTP($host, $port, $user, $pwd, $dirs) {
|
||||||
|
$cnx = connectToFTP($host, $port, $user, $pwd);
|
||||||
|
if (!$cnx) return false;
|
||||||
|
foreach(ensureIsArray($dirs) as $dir) {
|
||||||
|
if ($dir[strlen($dir)-1] != '/') {
|
||||||
|
$dir .= '/';
|
||||||
|
}
|
||||||
|
$do = $cnx -> rm($dir,true);
|
||||||
|
if ($do instanceof PEAR_Error) {
|
||||||
|
LSerror :: addErrorCode('FTP_03', $dir);
|
||||||
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rename a directory throught FTP
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param string $host FTP server FQDN or IP address
|
||||||
|
* @param string $port The TCP port of the FTP server
|
||||||
|
* @param string $user The username
|
||||||
|
* @param string $pwd The password
|
||||||
|
* @param string $old The actual directory path to rename
|
||||||
|
* @param string $new The new directory path
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
function renameDirByFTP($host, $port, $user, $pwd, $old, $new) {
|
||||||
|
$cnx = connectToFTP($host, $port, $user, $pwd);
|
||||||
|
if (!$cnx) return false;
|
||||||
|
$do = $cnx -> rename($old, $new);
|
||||||
|
if ($do instanceof PEAR_Error) {
|
||||||
|
LSerror :: addErrorCode('FTP_05', array('old' => $old, 'new' => $new));
|
||||||
|
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -39,172 +39,172 @@ LSerror :: defineError('MAIL_01',
|
||||||
___("MAIL : Error sending your email")
|
___("MAIL : Error sending your email")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check support of this LSaddon
|
* Check support of this LSaddon
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true if this LSaddon is fully supported, false otherwise
|
* @return boolean true if this LSaddon is fully supported, false otherwise
|
||||||
*/
|
*/
|
||||||
function LSaddon_mail_support() {
|
function LSaddon_mail_support() {
|
||||||
$retval=true;
|
$retval=true;
|
||||||
|
|
||||||
// Lib dependencies (check/load)
|
// Lib dependencies (check/load)
|
||||||
if (!class_exists('Mail')) {
|
if (!class_exists('Mail')) {
|
||||||
if(!LSsession::includeFile(PEAR_MAIL, true)) {
|
if(!LSsession::includeFile(PEAR_MAIL, true)) {
|
||||||
LSerror :: addErrorCode('MAIL_SUPPORT_01');
|
LSerror :: addErrorCode('MAIL_SUPPORT_01');
|
||||||
$retval=false;
|
$retval=false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!class_exists('Mail_mime')) {
|
|
||||||
if(!LSsession::includeFile(PEAR_MAIL_MIME, true)) {
|
|
||||||
LSerror :: addErrorCode('MAIL_SUPPORT_02');
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($retval)
|
|
||||||
LScli :: add_command(
|
|
||||||
'test_send_mail',
|
|
||||||
'cli_test_send_mail',
|
|
||||||
'Send a test email',
|
|
||||||
"[-s subject] [-b body] [recipient]",
|
|
||||||
array (
|
|
||||||
" -s/--subject The test email subject (optional)",
|
|
||||||
" -b/--body The test email body (optional)",
|
|
||||||
" recipient The test email recipient (required)",
|
|
||||||
),
|
|
||||||
false, // This command does not need LDAP connection
|
|
||||||
'cli_test_send_mail_autocompleter'
|
|
||||||
);
|
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (!class_exists('Mail_mime')) {
|
||||||
* Send an email
|
if(!LSsession::includeFile(PEAR_MAIL_MIME, true)) {
|
||||||
*
|
LSerror :: addErrorCode('MAIL_SUPPORT_02');
|
||||||
* @param string|array<string> $to Email recipient(s)
|
$retval=false;
|
||||||
* @param string $subject Email subject
|
|
||||||
* @param string $msg Email body
|
|
||||||
* @param array<string,string>|null $headers Email headers
|
|
||||||
* @param array<string,string>|null $attachments Email attachments as an array with
|
|
||||||
* filepath as key and filename as value
|
|
||||||
* @param string|null $eol End of line string (default : \n)
|
|
||||||
* @param string|null $encoding Email encoding (default: utf8)
|
|
||||||
* @param boolean $html Set to true to send an HTML email (default: false)
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @return boolean true si MAIL est pleinement supporté, false sinon
|
|
||||||
*/
|
|
||||||
function sendMail($to, $subject, $msg, $headers=null, $attachments=null,
|
|
||||||
$eol=null, $encoding=null, $html=false) {
|
|
||||||
global $MAIL_SEND_PARAMS, $MAIL_HEARDERS, $MAIL_CATCH_ALL;
|
|
||||||
$mail_obj = Mail::factory(MAIL_SEND_METHOD, (isset($MAIL_SEND_PARAMS)?$MAIL_SEND_PARAMS:null));
|
|
||||||
$logger = LSlog :: get_logger('LSaddon_mail');
|
|
||||||
|
|
||||||
if (!$headers) $headers = array();
|
|
||||||
if (isset($MAIL_HEARDERS) && is_array($MAIL_HEARDERS)) {
|
|
||||||
$headers = array_merge($headers,$MAIL_HEARDERS);
|
|
||||||
}
|
}
|
||||||
$logger -> trace(
|
|
||||||
'Mail catch all: '.(
|
|
||||||
isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL?
|
|
||||||
varDump($MAIL_CATCH_ALL):'not set')
|
|
||||||
);
|
|
||||||
if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
|
|
||||||
$logger -> debug(
|
|
||||||
'Mail catch to '.
|
|
||||||
(is_array($MAIL_CATCH_ALL)?implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL)
|
|
||||||
);
|
|
||||||
$msg .= sprintf(
|
|
||||||
(
|
|
||||||
$html?
|
|
||||||
_("</hr><p><small>Mail initialy intended for %s.</small></p>"):
|
|
||||||
_("\n\n\nMail initialy intended for %s.")
|
|
||||||
),
|
|
||||||
(is_array($to)?implode(',', $to):$to));
|
|
||||||
$to = (
|
|
||||||
is_array($MAIL_CATCH_ALL)?
|
|
||||||
implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($headers['From'])) {
|
|
||||||
$from = $headers['From'];
|
|
||||||
unset($headers['From']);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$from = LSsession :: getEmailSender();
|
|
||||||
}
|
|
||||||
|
|
||||||
$headers["To"] = $to;
|
|
||||||
|
|
||||||
$to = array (
|
|
||||||
'To' => $to
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach(array_keys($headers) as $header) {
|
|
||||||
if(in_array(strtoupper($header), array('BCC', 'CC'))) {
|
|
||||||
if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
|
|
||||||
$logger -> debug("Mail catched: remove $header header");
|
|
||||||
$msg .= sprintf(
|
|
||||||
(
|
|
||||||
$html?
|
|
||||||
_("<p><small>%s: %s</small></p>"):
|
|
||||||
_("\n%s: %s")
|
|
||||||
),
|
|
||||||
strtoupper($header),
|
|
||||||
(is_array($headers[$header])?implode(',', $headers[$header]):$headers[$header]));
|
|
||||||
unset($headers[$header]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$to[strtoupper($header)] = $headers[$header];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$encoding) $encoding = "utf8";
|
|
||||||
$mime = new Mail_mime(
|
|
||||||
array(
|
|
||||||
'eol' => ($eol?$eol:"\n"),
|
|
||||||
($html?'html_charset':'text_charset') => $encoding,
|
|
||||||
'head_charset' => $encoding,
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($from)
|
|
||||||
$mime->setFrom($from);
|
|
||||||
|
|
||||||
if ($subject)
|
|
||||||
$mime->setSubject($subject);
|
|
||||||
|
|
||||||
if ($html)
|
|
||||||
$mime->setHTMLBody($msg);
|
|
||||||
else
|
|
||||||
$mime->setTXTBody($msg);
|
|
||||||
|
|
||||||
if (is_array($attachments) && !empty($attachments)) {
|
|
||||||
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
||||||
foreach ($attachments as $file => $filename) {
|
|
||||||
$mime->addAttachment($file, $finfo->file($file), $filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$body = $mime->get();
|
|
||||||
$headers = $mime->headers($headers);
|
|
||||||
|
|
||||||
$ret = $mail_obj -> send($to, $headers, $body);
|
|
||||||
|
|
||||||
if ($ret instanceof PEAR_Error) {
|
|
||||||
LSerror :: addErrorCode('MAIL_01');
|
|
||||||
LSerror :: addErrorCode('MAIL_00', $ret -> getMessage());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($retval)
|
||||||
|
LScli :: add_command(
|
||||||
|
'test_send_mail',
|
||||||
|
'cli_test_send_mail',
|
||||||
|
'Send a test email',
|
||||||
|
"[-s subject] [-b body] [recipient]",
|
||||||
|
array (
|
||||||
|
" -s/--subject The test email subject (optional)",
|
||||||
|
" -b/--body The test email body (optional)",
|
||||||
|
" recipient The test email recipient (required)",
|
||||||
|
),
|
||||||
|
false, // This command does not need LDAP connection
|
||||||
|
'cli_test_send_mail_autocompleter'
|
||||||
|
);
|
||||||
|
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send an email
|
||||||
|
*
|
||||||
|
* @param string|array<string> $to Email recipient(s)
|
||||||
|
* @param string $subject Email subject
|
||||||
|
* @param string $msg Email body
|
||||||
|
* @param array<string,string>|null $headers Email headers
|
||||||
|
* @param array<string,string>|null $attachments Email attachments as an array with
|
||||||
|
* filepath as key and filename as value
|
||||||
|
* @param string|null $eol End of line string (default : \n)
|
||||||
|
* @param string|null $encoding Email encoding (default: utf8)
|
||||||
|
* @param boolean $html Set to true to send an HTML email (default: false)
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @return boolean true si MAIL est pleinement supporté, false sinon
|
||||||
|
*/
|
||||||
|
function sendMail($to, $subject, $msg, $headers=null, $attachments=null,
|
||||||
|
$eol=null, $encoding=null, $html=false) {
|
||||||
|
global $MAIL_SEND_PARAMS, $MAIL_HEARDERS, $MAIL_CATCH_ALL;
|
||||||
|
$mail_obj = Mail::factory(MAIL_SEND_METHOD, (isset($MAIL_SEND_PARAMS)?$MAIL_SEND_PARAMS:null));
|
||||||
|
$logger = LSlog :: get_logger('LSaddon_mail');
|
||||||
|
|
||||||
|
if (!$headers) $headers = array();
|
||||||
|
if (isset($MAIL_HEARDERS) && is_array($MAIL_HEARDERS)) {
|
||||||
|
$headers = array_merge($headers,$MAIL_HEARDERS);
|
||||||
|
}
|
||||||
|
$logger -> trace(
|
||||||
|
'Mail catch all: '.(
|
||||||
|
isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL?
|
||||||
|
varDump($MAIL_CATCH_ALL):'not set')
|
||||||
|
);
|
||||||
|
if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
|
||||||
|
$logger -> debug(
|
||||||
|
'Mail catch to '.
|
||||||
|
(is_array($MAIL_CATCH_ALL)?implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL)
|
||||||
|
);
|
||||||
|
$msg .= sprintf(
|
||||||
|
(
|
||||||
|
$html?
|
||||||
|
_("</hr><p><small>Mail initialy intended for %s.</small></p>"):
|
||||||
|
_("\n\n\nMail initialy intended for %s.")
|
||||||
|
),
|
||||||
|
(is_array($to)?implode(',', $to):$to));
|
||||||
|
$to = (
|
||||||
|
is_array($MAIL_CATCH_ALL)?
|
||||||
|
implode(',', $MAIL_CATCH_ALL):$MAIL_CATCH_ALL
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($headers['From'])) {
|
||||||
|
$from = $headers['From'];
|
||||||
|
unset($headers['From']);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$from = LSsession :: getEmailSender();
|
||||||
|
}
|
||||||
|
|
||||||
|
$headers["To"] = $to;
|
||||||
|
|
||||||
|
$to = array (
|
||||||
|
'To' => $to
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach(array_keys($headers) as $header) {
|
||||||
|
if(in_array(strtoupper($header), array('BCC', 'CC'))) {
|
||||||
|
if (isset($MAIL_CATCH_ALL) && $MAIL_CATCH_ALL) {
|
||||||
|
$logger -> debug("Mail catched: remove $header header");
|
||||||
|
$msg .= sprintf(
|
||||||
|
(
|
||||||
|
$html?
|
||||||
|
_("<p><small>%s: %s</small></p>"):
|
||||||
|
_("\n%s: %s")
|
||||||
|
),
|
||||||
|
strtoupper($header),
|
||||||
|
(is_array($headers[$header])?implode(',', $headers[$header]):$headers[$header]));
|
||||||
|
unset($headers[$header]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$to[strtoupper($header)] = $headers[$header];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$encoding) $encoding = "utf8";
|
||||||
|
$mime = new Mail_mime(
|
||||||
|
array(
|
||||||
|
'eol' => ($eol?$eol:"\n"),
|
||||||
|
($html?'html_charset':'text_charset') => $encoding,
|
||||||
|
'head_charset' => $encoding,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($from)
|
||||||
|
$mime->setFrom($from);
|
||||||
|
|
||||||
|
if ($subject)
|
||||||
|
$mime->setSubject($subject);
|
||||||
|
|
||||||
|
if ($html)
|
||||||
|
$mime->setHTMLBody($msg);
|
||||||
|
else
|
||||||
|
$mime->setTXTBody($msg);
|
||||||
|
|
||||||
|
if (is_array($attachments) && !empty($attachments)) {
|
||||||
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
||||||
|
foreach ($attachments as $file => $filename) {
|
||||||
|
$mime->addAttachment($file, $finfo->file($file), $filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$body = $mime->get();
|
||||||
|
$headers = $mime->headers($headers);
|
||||||
|
|
||||||
|
$ret = $mail_obj -> send($to, $headers, $body);
|
||||||
|
|
||||||
|
if ($ret instanceof PEAR_Error) {
|
||||||
|
LSerror :: addErrorCode('MAIL_01');
|
||||||
|
LSerror :: addErrorCode('MAIL_00', $ret -> getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (php_sapi_name() != 'cli')
|
if (php_sapi_name() != 'cli')
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -44,138 +44,152 @@ LSerror :: defineError('MAILDIR_04',
|
||||||
___("MAILDIR : Error retrieving remote path of the maildir.")
|
___("MAILDIR : Error retrieving remote path of the maildir.")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verification du support Maildir par ldapSaisie
|
* Verification du support Maildir par ldapSaisie
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true si Maildir est pleinement supporté, false sinon
|
* @return boolean true si Maildir est pleinement supporté, false sinon
|
||||||
*/
|
*/
|
||||||
function LSaddon_maildir_support() {
|
function LSaddon_maildir_support() {
|
||||||
$retval=true;
|
$retval = true;
|
||||||
|
|
||||||
// Dependance de librairie
|
// Dependance de librairie
|
||||||
if (!function_exists('createDirsByFTP')) {
|
if (!function_exists('createDirsByFTP')) {
|
||||||
if(!LSsession :: loadLSaddon('ftp')) {
|
if(!LSsession :: loadLSaddon('ftp')) {
|
||||||
LSerror :: addErrorCode('MAILDIR_SUPPORT_01');
|
LSerror :: addErrorCode('MAILDIR_SUPPORT_01');
|
||||||
$retval=false;
|
$retval = false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$MUST_DEFINE_CONST= array(
|
$MUST_DEFINE_CONST = array(
|
||||||
'LS_MAILDIR_FTP_HOST',
|
'LS_MAILDIR_FTP_HOST',
|
||||||
'LS_MAILDIR_FTP_USER',
|
'LS_MAILDIR_FTP_USER',
|
||||||
'LS_MAILDIR_FTP_MAILDIR_PATH',
|
'LS_MAILDIR_FTP_MAILDIR_PATH',
|
||||||
'LS_MAILDIR_FTP_MAILDIR_PATH_REGEX'
|
'LS_MAILDIR_FTP_MAILDIR_PATH_REGEX'
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($MUST_DEFINE_CONST as $const) {
|
||||||
|
if ( (!defined($const)) || (constant($const) == "")) {
|
||||||
|
LSerror :: addErrorCode('MAILDIR_SUPPORT_02', $const);
|
||||||
|
$retval = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creation d'une Maildir via FTP
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
|
* @param string $dir Le chemin de la maildir. Si défini, la valeur ne sera pas
|
||||||
|
* récupérée dans le ldapObject
|
||||||
|
*
|
||||||
|
* @return bool True ou false si il y a un problème durant la création de la Maildir
|
||||||
|
*/
|
||||||
|
function createMaildirByFTP($ldapObject,$dir=null) {
|
||||||
|
if (!$dir) {
|
||||||
|
$dir = getMaildirPath($ldapObject);
|
||||||
|
if (!$dir)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$dirs = array(
|
||||||
|
$dir.'/cur',
|
||||||
|
$dir.'/new',
|
||||||
|
$dir.'/tmp'
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
!createDirsByFTP(
|
||||||
|
LS_MAILDIR_FTP_HOST, LS_MAILDIR_FTP_PORT,
|
||||||
|
LS_MAILDIR_FTP_USER, LS_MAILDIR_FTP_PWD,
|
||||||
|
$dirs, LS_MAILDIR_FTP_MAILDIR_CHMOD
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
LSerror :: addErrorCode('MAILDIR_01');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suppression d'une Maildir via FTP
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
|
* @param string $dir Le chemin de la maildir. Si défini, la valeur ne sera pas
|
||||||
|
* récupérée dans le ldapObject
|
||||||
|
*
|
||||||
|
* @return bool True ou false si il y a un problème durant la suppression de la Maildir
|
||||||
|
*/
|
||||||
|
function removeMaildirByFTP($ldapObject,$dir=null) {
|
||||||
|
if (!$dir) {
|
||||||
|
$dir = getMaildirPath($ldapObject);
|
||||||
|
if (!$dir)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
!removeDirsByFTP(
|
||||||
|
LS_MAILDIR_FTP_HOST, LS_MAILDIR_FTP_PORT,
|
||||||
|
LS_MAILDIR_FTP_USER, LS_MAILDIR_FTP_PWD,
|
||||||
|
$dir
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
LSerror :: addErrorCode('MAILDIR_02');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne le chemin distant de la maildir
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
|
*
|
||||||
|
* @return string|false Le chemin distant de la maildir ou false si il y a un problème
|
||||||
|
*/
|
||||||
|
function getMaildirPath($ldapObject) {
|
||||||
|
$dir = getFData(LS_MAILDIR_FTP_MAILDIR_PATH, $ldapObject, 'getValue');
|
||||||
|
|
||||||
|
if (constant("LS_MAILDIR_FTP_MAILDIR_PATH_REGEX")) {
|
||||||
|
$dir = (
|
||||||
|
preg_match(LS_MAILDIR_FTP_MAILDIR_PATH_REGEX, $dir, $regs)?
|
||||||
|
$regs[1]:false
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach($MUST_DEFINE_CONST as $const) {
|
|
||||||
if ( (!defined($const)) || (constant($const) == "")) {
|
|
||||||
LSerror :: addErrorCode('MAILDIR_SUPPORT_02',$const);
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (!$dir) {
|
||||||
* Creation d'une Maildir via FTP
|
LSerror :: addErrorCode('MAILDIR_04');
|
||||||
*
|
return false;
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
|
||||||
* @param string $dir Le chemin de la maildir. Si défini, la valeur ne sera pas
|
|
||||||
* récupérée dans le ldapObject
|
|
||||||
*
|
|
||||||
* @return bool True ou false si il y a un problème durant la création de la Maildir
|
|
||||||
*/
|
|
||||||
function createMaildirByFTP($ldapObject,$dir=null) {
|
|
||||||
if (!$dir) {
|
|
||||||
$dir = getMaildirPath($ldapObject);
|
|
||||||
if (!$dir) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$dirs = array(
|
|
||||||
$dir.'/cur',
|
|
||||||
$dir.'/new',
|
|
||||||
$dir.'/tmp'
|
|
||||||
);
|
|
||||||
if (!createDirsByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$dirs,LS_MAILDIR_FTP_MAILDIR_CHMOD)) {
|
|
||||||
LSerror :: addErrorCode('MAILDIR_01');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return $dir;
|
||||||
* Suppression d'une Maildir via FTP
|
}
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
/**
|
||||||
*
|
* Rename Maildir via FTP
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
*
|
||||||
* @param string $dir Le chemin de la maildir. Si défini, la valeur ne sera pas
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
* récupérée dans le ldapObject
|
*
|
||||||
*
|
* @param string $old L'ancien chemin de la maildir
|
||||||
* @return bool True ou false si il y a un problème durant la suppression de la Maildir
|
* @param string $new Le nouveau chemin de la maildir
|
||||||
*/
|
*
|
||||||
function removeMaildirByFTP($ldapObject,$dir=null) {
|
* @return bool True ou false si il y a un problème durant le renomage de la Maildir
|
||||||
if (!$dir) {
|
*/
|
||||||
$dir = getMaildirPath($ldapObject);
|
function renameMaildirByFTP($old, $new) {
|
||||||
if (!$dir) {
|
if (
|
||||||
return false;
|
!renameDirByFTP(
|
||||||
}
|
LS_MAILDIR_FTP_HOST, LS_MAILDIR_FTP_PORT,
|
||||||
}
|
LS_MAILDIR_FTP_USER, LS_MAILDIR_FTP_PWD,
|
||||||
if (!removeDirsByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$dir)) {
|
$old, $new
|
||||||
LSerror :: addErrorCode('MAILDIR_02');
|
)
|
||||||
return false;
|
) {
|
||||||
}
|
LSerror :: addErrorCode('MAILDIR_03');
|
||||||
return true;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retourne le chemin distant de la maildir
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
|
||||||
*
|
|
||||||
* @return string|false Le chemin distant de la maildir ou false si il y a un problème
|
|
||||||
*/
|
|
||||||
function getMaildirPath($ldapObject) {
|
|
||||||
$dir = getFData(LS_MAILDIR_FTP_MAILDIR_PATH,$ldapObject,'getValue');
|
|
||||||
|
|
||||||
if (LS_MAILDIR_FTP_MAILDIR_PATH_REGEX != "") {
|
|
||||||
if (preg_match(LS_MAILDIR_FTP_MAILDIR_PATH_REGEX,$dir,$regs)) {
|
|
||||||
$dir = $regs[1];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$dir = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($dir=="") {
|
|
||||||
LSerror :: addErrorCode('MAILDIR_04');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Rename Maildir via FTP
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param string $old L'ancien chemin de la maildir
|
|
||||||
* @param string $new Le nouveau chemin de la maildir
|
|
||||||
*
|
|
||||||
* @return bool True ou false si il y a un problème durant le renomage de la Maildir
|
|
||||||
*/
|
|
||||||
function renameMaildirByFTP($old,$new) {
|
|
||||||
if (!renameDirByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$old,$new)) {
|
|
||||||
LSerror :: addErrorCode('MAILDIR_03');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
|
@ -36,232 +36,237 @@ LSerror :: defineError('POSIX_01',
|
||||||
___("POSIX : The attribute %{dependency} is missing. Unable to forge the attribute %{attr}.")
|
___("POSIX : The attribute %{dependency} is missing. Unable to forge the attribute %{attr}.")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verification du support POSIX par ldapSaisie
|
* Verification du support POSIX par ldapSaisie
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true si POSIX est pleinement supporté, false sinon
|
* @return boolean true si POSIX est pleinement supporté, false sinon
|
||||||
*/
|
*/
|
||||||
function LSaddon_posix_support() {
|
function LSaddon_posix_support() {
|
||||||
|
$retval = true;
|
||||||
|
|
||||||
$retval=true;
|
// Dependance de librairie
|
||||||
|
if (!function_exists('createDirsByFTP')) {
|
||||||
// Dependance de librairie
|
if(!LSsession :: loadLSaddon('ftp')) {
|
||||||
if (!function_exists('createDirsByFTP')) {
|
LSerror :: addErrorCode('POSIX_SUPPORT_02');
|
||||||
if(!LSsession :: loadLSaddon('ftp')) {
|
$retval = false;
|
||||||
LSerror :: addErrorCode('POSIX_SUPPORT_02');
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$MUST_DEFINE_CONST= array(
|
|
||||||
'LS_POSIX_UID_ATTR',
|
|
||||||
'LS_POSIX_UIDNUMBER_ATTR',
|
|
||||||
'LS_POSIX_GIDNUMBER_ATTR',
|
|
||||||
'LS_POSIX_UIDNUMBER_MIN_VAL',
|
|
||||||
'LS_POSIX_GIDNUMBER_MIN_VAL',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY_FTP_HOST',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY_FTP_PORT',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY_FTP_USER',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY_FTP_PWD',
|
|
||||||
'LS_POSIX_HOMEDIRECTORY_FTP_PATH',
|
|
||||||
'LS_POSIX_DN_TO_UID_PATTERN',
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach($MUST_DEFINE_CONST as $const) {
|
|
||||||
if ( (!defined($const)) || (constant($const) == "")) {
|
|
||||||
LSerror :: addErrorCode('POSIX_SUPPORT_O1',$const);
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$MUST_DEFINE_CONST = array(
|
||||||
* Generation de uidNumber
|
'LS_POSIX_UID_ATTR',
|
||||||
*
|
'LS_POSIX_UIDNUMBER_ATTR',
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
'LS_POSIX_GIDNUMBER_ATTR',
|
||||||
*
|
'LS_POSIX_UIDNUMBER_MIN_VAL',
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
'LS_POSIX_GIDNUMBER_MIN_VAL',
|
||||||
*
|
'LS_POSIX_HOMEDIRECTORY',
|
||||||
* @return integer|false uidNumber ou false si il y a un problème durant la génération
|
'LS_POSIX_HOMEDIRECTORY_FTP_HOST',
|
||||||
*/
|
'LS_POSIX_HOMEDIRECTORY_FTP_PORT',
|
||||||
function generate_uidNumber($ldapObject) {
|
'LS_POSIX_HOMEDIRECTORY_FTP_USER',
|
||||||
|
'LS_POSIX_HOMEDIRECTORY_FTP_PWD',
|
||||||
|
'LS_POSIX_HOMEDIRECTORY_FTP_PATH',
|
||||||
|
'LS_POSIX_DN_TO_UID_PATTERN',
|
||||||
|
);
|
||||||
|
|
||||||
$objects = LSldap :: search (
|
foreach($MUST_DEFINE_CONST as $const) {
|
||||||
LS_POSIX_UIDNUMBER_ATTR.'=*',
|
if ( (!defined($const)) || (constant($const) == "")) {
|
||||||
NULL,
|
LSerror :: addErrorCode('POSIX_SUPPORT_O1', $const);
|
||||||
array(
|
$retval = false;
|
||||||
'attributes' => array(
|
}
|
||||||
LS_POSIX_UIDNUMBER_ATTR
|
}
|
||||||
)
|
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generation de uidNumber
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
|
*
|
||||||
|
* @return integer|false uidNumber ou false si il y a un problème durant la génération
|
||||||
|
*/
|
||||||
|
function generate_uidNumber($ldapObject) {
|
||||||
|
$objects = LSldap :: search (
|
||||||
|
LS_POSIX_UIDNUMBER_ATTR.'=*',
|
||||||
|
NULL,
|
||||||
|
array(
|
||||||
|
'attributes' => array(
|
||||||
|
LS_POSIX_UIDNUMBER_ATTR
|
||||||
)
|
)
|
||||||
);
|
)
|
||||||
$uidNumber = LS_POSIX_UIDNUMBER_MIN_VAL;
|
);
|
||||||
|
$uidNumber = LS_POSIX_UIDNUMBER_MIN_VAL;
|
||||||
|
|
||||||
if (!is_array($objects))
|
if (!is_array($objects))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
foreach($objects as $object) {
|
|
||||||
if($object['attrs'][LS_POSIX_UIDNUMBER_ATTR] > $uidNumber)
|
|
||||||
$uidNumber = $object['attrs'][LS_POSIX_UIDNUMBER_ATTR];
|
|
||||||
}
|
|
||||||
|
|
||||||
$uidNumber++;
|
|
||||||
return $uidNumber;
|
|
||||||
|
|
||||||
|
foreach($objects as $object) {
|
||||||
|
if($object['attrs'][LS_POSIX_UIDNUMBER_ATTR] > $uidNumber)
|
||||||
|
$uidNumber = $object['attrs'][LS_POSIX_UIDNUMBER_ATTR];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$uidNumber++;
|
||||||
* Generation de gidNumber
|
return $uidNumber;
|
||||||
*
|
}
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
|
||||||
*
|
|
||||||
* @return integer|false gidNumber ou false si il y a un problème durant la génération
|
|
||||||
*/
|
|
||||||
function generate_gidNumber($ldapObject) {
|
|
||||||
|
|
||||||
$objects = LSldap :: search (
|
/**
|
||||||
LS_POSIX_GIDNUMBER_ATTR.'=*',
|
* Generation de gidNumber
|
||||||
NULL,
|
*
|
||||||
array(
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
'attributes' => array(
|
*
|
||||||
LS_POSIX_GIDNUMBER_ATTR
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
)
|
*
|
||||||
|
* @return integer|false gidNumber ou false si il y a un problème durant la génération
|
||||||
|
*/
|
||||||
|
function generate_gidNumber($ldapObject) {
|
||||||
|
$objects = LSldap :: search (
|
||||||
|
LS_POSIX_GIDNUMBER_ATTR . '=*',
|
||||||
|
NULL,
|
||||||
|
array(
|
||||||
|
'attributes' => array(
|
||||||
|
LS_POSIX_GIDNUMBER_ATTR
|
||||||
)
|
)
|
||||||
);
|
)
|
||||||
$gidNumber = LS_POSIX_GIDNUMBER_MIN_VAL;
|
);
|
||||||
|
$gidNumber = LS_POSIX_GIDNUMBER_MIN_VAL;
|
||||||
|
|
||||||
if (!is_array($objects))
|
if (!is_array($objects))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
foreach($objects as $object) {
|
|
||||||
if($object['attrs'][LS_POSIX_GIDNUMBER_ATTR] > $gidNumber)
|
|
||||||
$gidNumber = $object['attrs'][LS_POSIX_GIDNUMBER_ATTR];
|
|
||||||
}
|
|
||||||
|
|
||||||
$gidNumber++;
|
|
||||||
return $gidNumber;
|
|
||||||
|
|
||||||
|
foreach($objects as $object) {
|
||||||
|
if($object['attrs'][LS_POSIX_GIDNUMBER_ATTR] > $gidNumber)
|
||||||
|
$gidNumber = $object['attrs'][LS_POSIX_GIDNUMBER_ATTR];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$gidNumber++;
|
||||||
* Generation de homeDirectory
|
return $gidNumber;
|
||||||
*
|
}
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
|
||||||
*
|
|
||||||
* @return string|false homeDirectory ou false si il y a un problème durant la génération
|
|
||||||
*/
|
|
||||||
function generate_homeDirectory($ldapObject) {
|
|
||||||
if ( get_class($ldapObject -> attrs[ LS_POSIX_UID_ATTR ]) != 'LSattribute' ) {
|
|
||||||
LSerror :: addErrorCode('POSIX_01',array('dependency' => 'uid', 'attr' => 'homeDirectory'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue();
|
|
||||||
$home = LS_POSIX_HOMEDIRECTORY . $uid[0];
|
|
||||||
return $home;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generation de homeDirectory
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
|
*
|
||||||
|
* @return string|false homeDirectory ou false si il y a un problème durant la génération
|
||||||
|
*/
|
||||||
|
function generate_homeDirectory($ldapObject) {
|
||||||
|
if ( get_class($ldapObject -> attrs[ LS_POSIX_UID_ATTR ]) != 'LSattribute' ) {
|
||||||
|
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'uid', 'attr' => 'homeDirectory'));
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
$uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue();
|
||||||
* Create home directory by FTP
|
$home = LS_POSIX_HOMEDIRECTORY . $uid[0];
|
||||||
*
|
return $home;
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
}
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject L'objet ldap
|
/**
|
||||||
*
|
* Create home directory by FTP
|
||||||
* @return bool True on success, false otherwise
|
*
|
||||||
*/
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
function createHomeDirectoryByFTP($ldapObject) {
|
*
|
||||||
$dir = getFData(LS_POSIX_HOMEDIRECTORY_FTP_PATH,$ldapObject,'getValue');
|
* @param LSldapObject $ldapObject L'objet ldap
|
||||||
if (!createDirsByFTP(LS_POSIX_HOMEDIRECTORY_FTP_HOST,LS_POSIX_HOMEDIRECTORY_FTP_PORT,LS_POSIX_HOMEDIRECTORY_FTP_USER,LS_POSIX_HOMEDIRECTORY_FTP_PWD,$dir)) {
|
*
|
||||||
LSerror :: addErrorCode('POSIX_02');
|
* @return bool True on success, false otherwise
|
||||||
return false;
|
*/
|
||||||
}
|
function createHomeDirectoryByFTP($ldapObject) {
|
||||||
return true;
|
$dir = getFData(LS_POSIX_HOMEDIRECTORY_FTP_PATH, $ldapObject, 'getValue');
|
||||||
|
if (
|
||||||
|
!createDirsByFTP(
|
||||||
|
LS_POSIX_HOMEDIRECTORY_FTP_HOST, LS_POSIX_HOMEDIRECTORY_FTP_PORT,
|
||||||
|
LS_POSIX_HOMEDIRECTORY_FTP_USER, LS_POSIX_HOMEDIRECTORY_FTP_PWD,
|
||||||
|
$dir
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
LSerror :: addErrorCode('POSIX_02');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate member attribute value from memberUid
|
||||||
|
*
|
||||||
|
* IMPORTANT : The attribute memberUid must be define in configuration
|
||||||
|
* of the same object and must use HTML type select_object.
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject The LSldapObject
|
||||||
|
*
|
||||||
|
* @return array|false array of member attribute values or false in case of error
|
||||||
|
*/
|
||||||
|
function generateMemberFromMemberUid($ldapObject) {
|
||||||
|
if ( get_class($ldapObject -> attrs[ 'memberUid' ]) != 'LSattribute' ) {
|
||||||
|
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'memberUid', 'attr' => 'member'));
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if ( get_class($ldapObject -> attrs[ 'member' ]) != 'LSattribute' ) {
|
||||||
* Generate member attribute value from memberUid
|
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'member', 'attr' => 'member'));
|
||||||
*
|
return false;
|
||||||
* IMPORTANT : The attribute memberUid must be define in configuration
|
}
|
||||||
* of the same object and must use HTML type select_object.
|
|
||||||
*
|
$obj_type = LSconfig::get(
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
'LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type'
|
||||||
*
|
);
|
||||||
* @param LSldapObject $ldapObject The LSldapObject
|
if (empty($obj_type))
|
||||||
*
|
|
||||||
* @return array|false array of member attribute values or false in case of error
|
|
||||||
*/
|
|
||||||
function generateMemberFromMemberUid($ldapObject) {
|
|
||||||
if ( get_class($ldapObject -> attrs[ 'memberUid' ]) != 'LSattribute' ) {
|
|
||||||
LSerror :: addErrorCode('POSIX_01',array('dependency' => 'memberUid', 'attr' => 'member'));
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
$uids = $ldapObject -> attrs[ 'memberUid' ] -> getValue();
|
||||||
|
$member = array();
|
||||||
|
if (is_array($uids)) {
|
||||||
|
foreach ( $uids as $uid ) {
|
||||||
|
$member[] = sprintf(
|
||||||
|
'uid=%s,%s,%s',
|
||||||
|
$uid,
|
||||||
|
LSconfig::get('LSobjects.'.$obj_type.'.container_dn'),
|
||||||
|
LSsession::getTopDn()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return $member;
|
||||||
|
}
|
||||||
|
|
||||||
if ( get_class($ldapObject -> attrs[ 'member' ]) != 'LSattribute' ) {
|
/**
|
||||||
LSerror :: addErrorCode('POSIX_01',array('dependency' => 'member', 'attr' => 'member'));
|
* Generate memberUid attribute value from uniqueMember
|
||||||
return false;
|
*
|
||||||
}
|
* IMPORTANT : The attribute uniqueMember must be define in configuration
|
||||||
|
* of the same object.
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject The LSldapObject
|
||||||
|
*
|
||||||
|
* @return array|false array of memberUid values or false in case of error
|
||||||
|
*/
|
||||||
|
function generate_memberUidFromUniqueMember($ldapObject) {
|
||||||
|
if (get_class($ldapObject -> attrs[ 'memberUid' ]) != 'LSattribute') {
|
||||||
|
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'memberUid', 'attr' => 'memberUid'));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$obj_type=LSconfig::get('LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type');
|
if (get_class($ldapObject -> attrs[ 'uniqueMember' ]) != 'LSattribute') {
|
||||||
if (empty($obj_type))
|
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'uniqueMember', 'attr' => 'memberUid'));
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$uids = $ldapObject -> attrs[ 'memberUid' ] -> getValue();
|
$dns = $ldapObject -> attrs[ 'uniqueMember' ] -> getValue();
|
||||||
$member = array();
|
$uids = array();
|
||||||
if (is_array($uids)) {
|
if (is_array($dns)) {
|
||||||
foreach ( $uids as $uid ) {
|
foreach($dns as $dn) {
|
||||||
$member[]='uid='.$uid.','.LSconfig::get('LSobjects.'.$obj_type.'.container_dn').','.LSsession::getTopDn();
|
if(preg_match(LS_POSIX_DN_TO_UID_PATTERN, $dn, $matches)) {
|
||||||
|
$uids[] = $matches[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $member;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
return $uids;
|
||||||
/**
|
}
|
||||||
* Generate memberUid attribute value from uniqueMember
|
|
||||||
*
|
|
||||||
* IMPORTANT : The attribute uniqueMember must be define in configuration
|
|
||||||
* of the same object.
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param LSldapObject $ldapObject The LSldapObject
|
|
||||||
*
|
|
||||||
* @return array|false array of memberUid values or false in case of error
|
|
||||||
*/
|
|
||||||
function generate_memberUidFromUniqueMember($ldapObject) {
|
|
||||||
if ( get_class($ldapObject -> attrs[ 'memberUid' ]) != 'LSattribute' ) {
|
|
||||||
LSerror :: addErrorCode('POSIX_01',array('dependency' => 'memberUid', 'attr' => 'memberUid'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( get_class($ldapObject -> attrs[ 'uniqueMember' ]) != 'LSattribute' ) {
|
|
||||||
LSerror :: addErrorCode('POSIX_01',array('dependency' => 'uniqueMember', 'attr' => 'memberUid'));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$dns = $ldapObject -> attrs[ 'uniqueMember' ] -> getValue();
|
|
||||||
$uids = array();
|
|
||||||
if (is_array($dns)) {
|
|
||||||
foreach($dns as $dn) {
|
|
||||||
if(preg_match(LS_POSIX_DN_TO_UID_PATTERN,$dn,$matches)) {
|
|
||||||
$uids[]=$matches[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $uids;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate shadowLastChange attribute value
|
* Generate shadowLastChange attribute value
|
||||||
|
|
|
@ -229,78 +229,71 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
|
||||||
return generate_sambaSID($ldapObject, LS_SAMBA_GIDNUMBER_ATTR, LS_SAMBA_SID_BASE_GROUP);
|
return generate_sambaSID($ldapObject, LS_SAMBA_GIDNUMBER_ATTR, LS_SAMBA_SID_BASE_GROUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* Generation de sambaNTPassword
|
||||||
* Generation de sambaNTPassword
|
*
|
||||||
*
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
*
|
||||||
*
|
* @param LSldapObject $ldapObject The user LSldapObject object
|
||||||
* @param LSldapObject $ldapObject The user LSldapObject object
|
*
|
||||||
*
|
* @return string|false sambaNTPassword value on success, false otherwise
|
||||||
* @return string|false sambaNTPassword value on success, false otherwise
|
*/
|
||||||
*/
|
function generate_sambaNTPassword($ldapObject) {
|
||||||
function generate_sambaNTPassword($ldapObject) {
|
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
||||||
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
LSerror :: addErrorCode('SAMBA_01',array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaNTPassword'));
|
||||||
LSerror :: addErrorCode('SAMBA_01',array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaNTPassword'));
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!is_a(
|
|
||||||
$ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap,
|
|
||||||
'LSattr_ldap_password'
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
LSerror :: addErrorCode('SAMBA_05', LS_SAMBA_USERPASSWORD_ATTR);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$password = $ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
|
||||||
$sambapassword = new smbHash;
|
|
||||||
$sambaNTPassword = $sambapassword -> nthash($password);
|
|
||||||
|
|
||||||
if($sambaNTPassword == '') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $sambaNTPassword;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (
|
||||||
* Generation de sambaLMPassword
|
!is_a(
|
||||||
*
|
$ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap,
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
'LSattr_ldap_password'
|
||||||
*
|
)
|
||||||
* @param LSldapObject $ldapObject The user LSldapObject object
|
) {
|
||||||
*
|
LSerror :: addErrorCode('SAMBA_05', LS_SAMBA_USERPASSWORD_ATTR);
|
||||||
* @return string|false sambaLMPassword value on success, false otherwise
|
|
||||||
*/
|
|
||||||
function generate_sambaLMPassword($ldapObject) {
|
|
||||||
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
|
||||||
LSerror :: addErrorCode(
|
|
||||||
'SAMBA_01',
|
|
||||||
array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaLMPassword')
|
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!is_a(
|
|
||||||
$ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap,
|
|
||||||
'LSattr_ldap_password'
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
LSerror :: addErrorCode('SAMBA_05', LS_SAMBA_USERPASSWORD_ATTR);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$password = $ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
|
||||||
$sambapassword = new smbHash;
|
|
||||||
$sambaLMPassword = $sambapassword -> lmhash($password);
|
|
||||||
|
|
||||||
if($sambaLMPassword == '') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return $sambaLMPassword;
|
|
||||||
}
|
}
|
||||||
|
$password = $ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
||||||
|
$sambapassword = new smbHash;
|
||||||
|
$sambaNTPassword = $sambapassword -> nthash($password);
|
||||||
|
|
||||||
|
return $sambaNTPassword?$sambaNTPassword:false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generation de sambaLMPassword
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param LSldapObject $ldapObject The user LSldapObject object
|
||||||
|
*
|
||||||
|
* @return string|false sambaLMPassword value on success, false otherwise
|
||||||
|
*/
|
||||||
|
function generate_sambaLMPassword($ldapObject) {
|
||||||
|
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
|
||||||
|
LSerror :: addErrorCode(
|
||||||
|
'SAMBA_01',
|
||||||
|
array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaLMPassword')
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!is_a(
|
||||||
|
$ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap,
|
||||||
|
'LSattr_ldap_password'
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
LSerror :: addErrorCode('SAMBA_05', LS_SAMBA_USERPASSWORD_ATTR);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$password = $ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap -> getClearPassword();
|
||||||
|
$sambapassword = new smbHash;
|
||||||
|
$sambaLMPassword = $sambapassword -> lmhash($password);
|
||||||
|
|
||||||
|
return $sambaLMPassword?$sambaLMPassword:false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate UNIX ID value from sambaUnixIdPool object
|
* Generate UNIX ID value from sambaUnixIdPool object
|
||||||
|
|
|
@ -59,225 +59,223 @@ LSerror :: defineError('SSH_07',
|
||||||
___("SSH : Unable to rename folder from %{old} to %{new} on the remote server.")
|
___("SSH : Unable to rename folder from %{old} to %{new} on the remote server.")
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check LdapSaisie SSH support
|
* Check LdapSaisie SSH support
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @return boolean true if SSH is fully supported, false otherwise
|
* @return boolean true if SSH is fully supported, false otherwise
|
||||||
*/
|
*/
|
||||||
function LSaddon_ssh_support() {
|
function LSaddon_ssh_support() {
|
||||||
$retval=true;
|
$retval=true;
|
||||||
|
|
||||||
// Check PhpSecLib library
|
// Check PhpSecLib library
|
||||||
if (!defined('PHPSECLIB_AUTOLOAD')) {
|
if (!defined('PHPSECLIB_AUTOLOAD')) {
|
||||||
LSerror :: addErrorCode('SSH_SUPPORT_02','PHPSECLIB_AUTOLOAD');
|
LSerror :: addErrorCode('SSH_SUPPORT_02','PHPSECLIB_AUTOLOAD');
|
||||||
$retval=false;
|
$retval=false;
|
||||||
} else if(!LSsession::includeFile(PHPSECLIB_AUTOLOAD, true)) {
|
} else if(!LSsession::includeFile(PHPSECLIB_AUTOLOAD, true)) {
|
||||||
LSerror :: addErrorCode('SSH_SUPPORT_01');
|
LSerror :: addErrorCode('SSH_SUPPORT_01');
|
||||||
|
$retval=false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to an SFTP server
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param array $params The SSH connexion parameters :
|
||||||
|
* array (
|
||||||
|
* 'host' => '[SSH server hostname/IP]', // required
|
||||||
|
* 'port' => [SSH port], // optional, default : 22
|
||||||
|
* 'timeout' => [SSH connection timeout], // optional, default : 10
|
||||||
|
*
|
||||||
|
* // Authentication
|
||||||
|
* 'user' => '[SSH remote user]', // required
|
||||||
|
*
|
||||||
|
* // Auth method :
|
||||||
|
* // One of the following method configuration is required.
|
||||||
|
*
|
||||||
|
* // Auth using simple password
|
||||||
|
* 'password' => '[secret password]'
|
||||||
|
*
|
||||||
|
* // Auth using a key
|
||||||
|
* 'auth_key' => array (
|
||||||
|
* 'file_path' => '[SSH private key file path]',
|
||||||
|
* 'password' => '[SSH private key file password]' // Only if need
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* @param boolean $sftp Enable SFTP mode (default : false)
|
||||||
|
*
|
||||||
|
* @return SSH2|SFTP|false SSH2/SFTP object or false
|
||||||
|
*/
|
||||||
|
function connectToSSH($params, $sftp=false) {
|
||||||
|
$logger = LSlog :: get_logger('LSaddon_ssh');
|
||||||
|
if (!isset($params['host'])) {
|
||||||
|
LSerror :: addErrorCode('SSH_01',"host");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$host = $params['host'];
|
||||||
|
|
||||||
|
if (!isset($params['user'])) {
|
||||||
|
LSerror :: addErrorCode('SSH_01',"user");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$user = $params['user'];
|
||||||
|
|
||||||
|
$port = (isset($params['port'])?$params['port']:22);
|
||||||
|
$timeout = (isset($params['timeout'])?$params['timeout']:10);
|
||||||
|
|
||||||
|
if (isset($params['auth_key'])) {
|
||||||
|
if (!isset($params['auth_key']['file_path'])) {
|
||||||
|
LSerror :: addErrorCode('SSH_01',"auth_key -> file_path");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$key_file_path = $params['auth_key']['file_path'];
|
||||||
|
if (!is_file($key_file_path) || !is_readable($key_file_path)) {
|
||||||
|
LSerror :: addErrorCode('SSH_02', $key_file_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$password = new RSA();
|
||||||
|
|
||||||
|
if (isset($params['auth_key']['password'])) {
|
||||||
|
$password -> setPassword($params['auth_key']['password']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$key_content = file_get_contents($key_file_path);
|
||||||
|
if (!$password -> loadKey($key_content)) {
|
||||||
|
LSerror :: addErrorCode('SSH_03', $key_file_path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$logger -> debug("Connect on $user@$host:$port (timeout: $timeout sec) with key authentication using file '$key_file_path'.");
|
||||||
|
}
|
||||||
|
elseif (isset($params['password'])) {
|
||||||
|
$logger -> debug("Connect on $user@$host:$port (timeout: $timeout sec) with password authentication.");
|
||||||
|
$password = $params['password'];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LSerror :: addErrorCode('SSH_01',"authentication");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($sftp)
|
||||||
|
$cnx = new SFTP($host, $port, $timeout);
|
||||||
|
else
|
||||||
|
$cnx = new SSH2($host, $port, $timeout);
|
||||||
|
|
||||||
|
if (!$cnx->login($user, $password)) {
|
||||||
|
LSerror :: addErrorCode('SSH_04', array('host' => $host, 'port' => $port));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$logger -> debug("Connected.");
|
||||||
|
|
||||||
|
return $cnx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create one or more directories throught SFTP
|
||||||
|
*
|
||||||
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
|
*
|
||||||
|
* @param array $connection_params Connection parameters
|
||||||
|
* @param array|string $dirs The directory/ies to add
|
||||||
|
* @param int $chmod The directory/ies mode as an octal number (do not forget leading zero,
|
||||||
|
* example: 0755 or 02755, default : default umask on the SSH server)
|
||||||
|
* @param boolean $recursive Enable recursive mode (default : false)
|
||||||
|
* @param boolean $continue Enable continue mode : do not on error (default : false)
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
function createDirsBySFTP($connection_params, $dirs, $chmod=-1, $recursive=false, $continue=false) {
|
||||||
|
$cnx = connectToSSH($connection_params, true);
|
||||||
|
if (!$cnx instanceof SFTP) return false;
|
||||||
|
if (!is_array($dirs)) {
|
||||||
|
$dirs = array($dirs);
|
||||||
|
}
|
||||||
|
$retval=true;
|
||||||
|
foreach($dirs as $dir) {
|
||||||
|
LSlog :: get_logger('LSaddon_ssh') -> debug("mkdir($dir) with chmod=$chmod and recursie ".($recursive?'enabled':'disabled'));
|
||||||
|
if (!$cnx -> mkdir($dir, $chmod, $recursive)) {
|
||||||
|
LSerror :: addErrorCode('SSH_05',$dir);
|
||||||
|
if (!$continue) return false;
|
||||||
$retval=false;
|
$retval=false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete one or more directories throught SFTP
|
||||||
/**
|
*
|
||||||
* Connect to an SFTP server
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @param array $connection_params Connection parameters
|
||||||
*
|
* @param array|string $dirs The directory/ies to remove
|
||||||
* @param array $params The SSH connexion parameters :
|
* @param boolean $recursive Enable recursive mode (default : false)
|
||||||
* array (
|
* @param boolean $continue Enable continue mode : do not on error (default : false)
|
||||||
* 'host' => '[SSH server hostname/IP]', // required
|
*
|
||||||
* 'port' => [SSH port], // optional, default : 22
|
* @return boolean
|
||||||
* 'timeout' => [SSH connection timeout], // optional, default : 10
|
*/
|
||||||
*
|
function removeDirsBySFTP($connection_params, $dirs, $recursive=false, $continue=false) {
|
||||||
* // Authentication
|
$cnx = connectToSSH($connection_params, true);
|
||||||
* 'user' => '[SSH remote user]', // required
|
if (!$cnx instanceof SFTP) return false;
|
||||||
*
|
if (!is_array($dirs)) {
|
||||||
* // Auth method :
|
$dirs = array($dirs);
|
||||||
* // One of the following method configuration is required.
|
|
||||||
*
|
|
||||||
* // Auth using simple password
|
|
||||||
* 'password' => '[secret password]'
|
|
||||||
*
|
|
||||||
* // Auth using a key
|
|
||||||
* 'auth_key' => array (
|
|
||||||
* 'file_path' => '[SSH private key file path]',
|
|
||||||
* 'password' => '[SSH private key file password]' // Only if need
|
|
||||||
* )
|
|
||||||
* )
|
|
||||||
* @param boolean $sftp Enable SFTP mode (default : false)
|
|
||||||
*
|
|
||||||
* @return SSH2|SFTP|false SSH2/SFTP object or false
|
|
||||||
*/
|
|
||||||
function connectToSSH($params, $sftp=false) {
|
|
||||||
$logger = LSlog :: get_logger('LSaddon_ssh');
|
|
||||||
if (!isset($params['host'])) {
|
|
||||||
LSerror :: addErrorCode('SSH_01',"host");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$host = $params['host'];
|
|
||||||
|
|
||||||
if (!isset($params['user'])) {
|
|
||||||
LSerror :: addErrorCode('SSH_01',"user");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$user = $params['user'];
|
|
||||||
|
|
||||||
$port = (isset($params['port'])?$params['port']:22);
|
|
||||||
$timeout = (isset($params['timeout'])?$params['timeout']:10);
|
|
||||||
|
|
||||||
if (isset($params['auth_key'])) {
|
|
||||||
if (!isset($params['auth_key']['file_path'])) {
|
|
||||||
LSerror :: addErrorCode('SSH_01',"auth_key -> file_path");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$key_file_path = $params['auth_key']['file_path'];
|
|
||||||
if (!is_file($key_file_path) || !is_readable($key_file_path)) {
|
|
||||||
LSerror :: addErrorCode('SSH_02', $key_file_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$password = new RSA();
|
|
||||||
|
|
||||||
if (isset($params['auth_key']['password'])) {
|
|
||||||
$password -> setPassword($params['auth_key']['password']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$key_content = file_get_contents($key_file_path);
|
|
||||||
if (!$password -> loadKey($key_content)) {
|
|
||||||
LSerror :: addErrorCode('SSH_03', $key_file_path);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$logger -> debug("Connect on $user@$host:$port (timeout: $timeout sec) with key authentication using file '$key_file_path'.");
|
|
||||||
}
|
|
||||||
elseif (isset($params['password'])) {
|
|
||||||
$logger -> debug("Connect on $user@$host:$port (timeout: $timeout sec) with password authentication.");
|
|
||||||
$password = $params['password'];
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
LSerror :: addErrorCode('SSH_01',"authentication");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($sftp)
|
|
||||||
$cnx = new SFTP($host, $port, $timeout);
|
|
||||||
else
|
|
||||||
$cnx = new SSH2($host, $port, $timeout);
|
|
||||||
|
|
||||||
if (!$cnx->login($user, $password)) {
|
|
||||||
LSerror :: addErrorCode('SSH_04', array('host' => $host, 'port' => $port));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$logger -> debug("Connected.");
|
|
||||||
|
|
||||||
return $cnx;
|
|
||||||
}
|
}
|
||||||
|
$retval=true;
|
||||||
/**
|
foreach($dirs as $dir) {
|
||||||
* Create one or more directories throught SFTP
|
LSlog :: get_logger('LSaddon_ssh') -> debug("delete($dir) with recursive ".($recursive?'enabled':'disabled'));
|
||||||
*
|
if (!$cnx -> delete($dir, $recursive)) {
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
LSerror :: addErrorCode('SSH_06',$dir);
|
||||||
*
|
if (!$continue) return false;
|
||||||
* @param array $connection_params Connection parameters
|
$retval=false;
|
||||||
* @param array|string $dirs The directory/ies to add
|
|
||||||
* @param int $chmod The directory/ies mode as an octal number (do not forget leading zero,
|
|
||||||
* example: 0755 or 02755, default : default umask on the SSH server)
|
|
||||||
* @param boolean $recursive Enable recursive mode (default : false)
|
|
||||||
* @param boolean $continue Enable continue mode : do not on error (default : false)
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
function createDirsBySFTP($connection_params, $dirs, $chmod=-1, $recursive=false, $continue=false) {
|
|
||||||
$cnx = connectToSSH($connection_params, true);
|
|
||||||
if (!$cnx instanceof SFTP) return false;
|
|
||||||
if (!is_array($dirs)) {
|
|
||||||
$dirs = array($dirs);
|
|
||||||
}
|
}
|
||||||
$retval=true;
|
|
||||||
foreach($dirs as $dir) {
|
|
||||||
LSlog :: get_logger('LSaddon_ssh') -> debug("mkdir($dir) with chmod=$chmod and recursie ".($recursive?'enabled':'disabled'));
|
|
||||||
if (!$cnx -> mkdir($dir, $chmod, $recursive)) {
|
|
||||||
LSerror :: addErrorCode('SSH_05',$dir);
|
|
||||||
if (!$continue) return false;
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
return $retval;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete one or more directories throught SFTP
|
* Rename a directory throught SFTP
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @param array $connection_params Connection parameters
|
* @param array $connection_params Connection parameters
|
||||||
* @param array|string $dirs The directory/ies to remove
|
* @param string $old The actual directory path to rename
|
||||||
* @param boolean $recursive Enable recursive mode (default : false)
|
* @param string $new The new directory path
|
||||||
* @param boolean $continue Enable continue mode : do not on error (default : false)
|
*
|
||||||
*
|
* @return boolean
|
||||||
* @return boolean
|
*/
|
||||||
*/
|
function renameDirBySFTP($connection_params, $old, $new) {
|
||||||
function removeDirsBySFTP($connection_params, $dirs, $recursive=false, $continue=false) {
|
$cnx = connectToSSH($connection_params, true);
|
||||||
$cnx = connectToSSH($connection_params, true);
|
if (!$cnx instanceof SFTP) return false;
|
||||||
if (!$cnx instanceof SFTP) return false;
|
LSlog :: get_logger('LSaddon_ssh') -> debug("rename($old, $new)");
|
||||||
if (!is_array($dirs)) {
|
if (!$cnx -> rename($old, $new)) {
|
||||||
$dirs = array($dirs);
|
LSerror :: addErrorCode('SSH_07',array('old' => $old,'new' => $new));
|
||||||
}
|
return false;
|
||||||
$retval=true;
|
|
||||||
foreach($dirs as $dir) {
|
|
||||||
LSlog :: get_logger('LSaddon_ssh') -> debug("delete($dir) with recursive ".($recursive?'enabled':'disabled'));
|
|
||||||
if (!$cnx -> delete($dir, $recursive)) {
|
|
||||||
LSerror :: addErrorCode('SSH_06',$dir);
|
|
||||||
if (!$continue) return false;
|
|
||||||
$retval=false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $retval;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rename a directory throught SFTP
|
* Exec a command throught SSH
|
||||||
*
|
*
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
||||||
*
|
*
|
||||||
* @param array $connection_params Connection parameters
|
* @param array $connection_params Connection parameters
|
||||||
* @param string $old The actual directory path to rename
|
* @param string $cmd The command to run on remote server
|
||||||
* @param string $new The new directory path
|
*
|
||||||
*
|
* @return array|false False if connection fail and an array otherwise, with
|
||||||
* @return boolean
|
* exit code as first value and the command outup as second
|
||||||
*/
|
* one (stdout + stderr).
|
||||||
function renameDirBySFTP($connection_params, $old, $new) {
|
*/
|
||||||
$cnx = connectToSSH($connection_params, true);
|
function execBySSH($connection_params, $cmd) {
|
||||||
if (!$cnx instanceof SFTP) return false;
|
$cnx = connectToSSH($connection_params);
|
||||||
LSlog :: get_logger('LSaddon_ssh') -> debug("rename($old, $new)");
|
if (!$cnx instanceof SSH2) return false;
|
||||||
if (!$cnx -> rename($old, $new)) {
|
LSlog :: get_logger('LSaddon_ssh') -> debug("exec($cmd)");
|
||||||
LSerror :: addErrorCode('SSH_07',array('old' => $old,'new' => $new));
|
$result = $cnx -> exec($cmd);
|
||||||
return false;
|
$exit_status = $cnx->getExitStatus();
|
||||||
}
|
return array($exit_status, $result);
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exec a command throught SSH
|
|
||||||
*
|
|
||||||
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
||||||
*
|
|
||||||
* @param array $connection_params Connection parameters
|
|
||||||
* @param string $cmd The command to run on remote server
|
|
||||||
*
|
|
||||||
* @return array|false False if connection fail and an array otherwise, with
|
|
||||||
* exit code as first value and the command outup as second
|
|
||||||
* one (stdout + stderr).
|
|
||||||
*/
|
|
||||||
function execBySSH($connection_params, $cmd) {
|
|
||||||
$cnx = connectToSSH($connection_params);
|
|
||||||
if (!$cnx instanceof SSH2) return false;
|
|
||||||
LSlog :: get_logger('LSaddon_ssh') -> debug("exec($cmd)");
|
|
||||||
$result = $cnx -> exec($cmd);
|
|
||||||
$exit_status = $cnx->getExitStatus();
|
|
||||||
return array($exit_status, $result);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue