LSaddons: Code cleaning

This commit is contained in:
Benjamin Renard 2023-06-20 19:42:12 +02:00
parent 425df3da3a
commit 0eb0143921
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
8 changed files with 1093 additions and 1078 deletions

View file

@ -39,88 +39,88 @@ LSerror :: defineError('ASTERISK_04',
___("Asterisk: The LDAP attribute type of the attribute %{attr} is incorrect: its must be password.")
);
/**
* Check support of Asterisk by LdapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if Asterisk is totally supported, false in other case
*/
function LSaddon_asterisk_support() {
$retval=true;
/**
* Check support of Asterisk by LdapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if Asterisk is totally supported, false in other case
*/
function LSaddon_asterisk_support() {
$retval=true;
$MUST_DEFINE_CONST= array(
'LS_ASTERISK_HASH_PWD_FORMAT',
'LS_ASTERISK_USERPASSWORD_ATTR',
);
$MUST_DEFINE_CONST= array(
'LS_ASTERISK_HASH_PWD_FORMAT',
'LS_ASTERISK_USERPASSWORD_ATTR',
);
foreach($MUST_DEFINE_CONST as $const) {
if ( (!defined($const)) || (constant($const) == "")) {
LSerror :: addErrorCode('ASTERISK_SUPPORT_01',$const);
$retval=false;
}
foreach($MUST_DEFINE_CONST as $const) {
if ( (!defined($const)) || (constant($const) == "")) {
LSerror :: addErrorCode('ASTERISK_SUPPORT_01',$const);
$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
*
* @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);
/**
* 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
*
* @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);
}

View file

@ -45,110 +45,110 @@ LSerror :: defineError('LS_EXPORTSEARCHRESULTASCSV_03',
);
/**
* Check support of exportSearchResultAsCSV
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if exportSearchResultAsCSV is fully supported, false in other case
*/
function LSaddon_exportSearchResultAsCSV_support() {
$retval=true;
/**
* Check support of exportSearchResultAsCSV
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if exportSearchResultAsCSV is fully supported, false in other case
*/
function LSaddon_exportSearchResultAsCSV_support() {
$retval=true;
// Check fputcsv function
if (!function_exists('fputcsv')) {
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_SUPPORT_01');
// Check fputcsv function
if (!function_exists('fputcsv')) {
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(
'LS_EXPORTSEARCHRESULTASCSV_DELIMITER',
'LS_EXPORTSEARCHRESULTASCSV_ENCLOSURE',
'LS_EXPORTSEARCHRESULTASCSV_ESCAPE_CHAR',
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;
}
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 ( (!defined($const)) || (constant($const) == "")) {
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');
if (!writeRowInCSV($csv, $row)) {
LSerror :: addErrorCode('LS_EXPORTSEARCHRESULTASCSV_03');
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();
}
/**
* Write CSV row in file
*
* @param resource &$csv The CSV file description reference
* @param array<string> $row An array of a CSV row fields to write
*
* @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);
header("Content-disposition: attachment; filename=export.csv");
header("Content-type: text/csv");
rewind($csv);
print stream_get_contents($csv);
@fclose($csv);
exit();
}
/**
* Write CSV row in file
*
* @param resource &$csv The CSV file description reference
* @param array<string> $row An array of a CSV row fields to write
*
* @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);
}

View file

@ -53,165 +53,170 @@ LSerror :: defineError('FTP_05',
___("FTP Support : Unable to rename folder from %{old} to %{new} on the remote server.")
);
/**
* Verification du support FTP par ldapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true si FTP est pleinement supporté, false sinon
*/
function LSaddon_ftp_support() {
$retval=true;
/**
* Check support of FTP addon
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if FTP addon is fully supported, false in other case
*/
function LSaddon_ftp_support() {
$retval = true;
// Dependance de librairie
if (!class_exists('Net_FTP')) {
if (!defined('NET_FTP')) {
LSerror :: addErrorCode('FTP_SUPPORT_02','NET_FTP');
$retval=false;
} else if(!LSsession::includeFile(NET_FTP, true)) {
LSerror :: addErrorCode('FTP_SUPPORT_01');
$retval=false;
}
// Net_FTP lib dependency
if (!class_exists('Net_FTP')) {
if (!defined('NET_FTP')) {
LSerror :: addErrorCode('FTP_SUPPORT_02', 'NET_FTP');
$retval = false;
} else if(!LSsession::includeFile(NET_FTP, true)) {
LSerror :: addErrorCode('FTP_SUPPORT_01');
$retval = false;
}
return $retval;
}
return $retval;
}
/**
* Connexion a un serveur FTP
*
* @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
*
* @return Net_FTP|false Net_FTP object en cas de succès, false sinon
*/
function connectToFTP($host,$port,$user,$pwd) {
$cnx = new Net_FTP();
$do = $cnx -> connect($host,$port);
if (! $do instanceof PEAR_Error){
$do = $cnx -> login($user,$pwd);
if (! $do instanceof PEAR_Error) {
return $cnx;
}
else {
LSerror :: addErrorCode('FTP_01',"2");
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
return false;
}
/**
* Connect to FTP server
*
* @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
*
* @return Net_FTP|false Net_FTP object in case of success, false otherwise
*/
function connectToFTP($host, $port, $user, $pwd) {
$cnx = new Net_FTP();
$do = $cnx -> connect($host, $port);
if (!$do instanceof PEAR_Error){
$do = $cnx -> login($user, $pwd);
if (!$do instanceof PEAR_Error) {
return $cnx;
}
else {
LSerror :: addErrorCode('FTP_01',"1");
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
LSerror :: addErrorCode('FTP_01', "2");
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false;
}
}
/**
* Creation d'un ou plusieurs dossiers via FTP
*
* @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;
else {
LSerror :: addErrorCode('FTP_01', "1");
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false;
}
}
/**
* Suppression d'un ou plusieurs dossiers via FTP
*
* Note : Attention : suppression récursive. Cela veut dire que les sous-dossiers
* 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
* commence par un '.' par exempl), ces fichiers ne seront pas supprimés et la
* suppression du dossier parent échoura.
*
* Pour VsFTPd : Ajouter force_dot_files=1 dans la configuration.
*
* @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) à 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;
}
/**
* Creation d'un ou plusieurs dossiers via FTP
*
* @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)
/**
* Renomage d'un dossier via FTP
*
* @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 string $old Le dossier à renomer
* @param string $new Le nouveau nom du dossier à renomer
*
* @return bool True ou false si il y a un problème durant le renomage du/des dossier(s)
*/
function renameDirByFTP($host,$port,$user,$pwd,$old,$new) {
$cnx = connectToFTP($host,$port,$user,$pwd);
if (!$cnx) return false;
$do = $cnx -> rename($old,$new);
* Create one or more directories 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 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)
*
* @return boolean
*/
function createDirsByFTP($host, $port, $user, $pwd, $dirs, $chmod=NULL) {
$cnx = connectToFTP($host, $port, $user, $pwd);
if (!$cnx) return false;
foreach(ensureIsArray($dirs) as $dir) {
$do = $cnx -> mkdir($dir, true);
if ($do instanceof PEAR_Error) {
LSerror :: addErrorCode('FTP_05',array('old' => $old,'new' => $new));
LSerror :: addErrorCode('FTP_00',$do -> getMessage());
LSerror :: addErrorCode('FTP_02', $dir);
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
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;
}

View file

@ -39,172 +39,172 @@ LSerror :: defineError('MAIL_01',
___("MAIL : Error sending your email")
);
/**
* Check support of this LSaddon
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if this LSaddon is fully supported, false otherwise
*/
function LSaddon_mail_support() {
$retval=true;
/**
* Check support of this LSaddon
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if this LSaddon is fully supported, false otherwise
*/
function LSaddon_mail_support() {
$retval=true;
// Lib dependencies (check/load)
if (!class_exists('Mail')) {
if(!LSsession::includeFile(PEAR_MAIL, true)) {
LSerror :: addErrorCode('MAIL_SUPPORT_01');
$retval=false;
}
// Lib dependencies (check/load)
if (!class_exists('Mail')) {
if(!LSsession::includeFile(PEAR_MAIL, true)) {
LSerror :: addErrorCode('MAIL_SUPPORT_01');
$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;
}
/**
* 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);
if (!class_exists('Mail_mime')) {
if(!LSsession::includeFile(PEAR_MAIL_MIME, true)) {
LSerror :: addErrorCode('MAIL_SUPPORT_02');
$retval=false;
}
$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')
return true;

View file

@ -44,138 +44,152 @@ LSerror :: defineError('MAILDIR_04',
___("MAILDIR : Error retrieving remote path of the maildir.")
);
/**
* Verification du support Maildir par ldapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true si Maildir est pleinement supporté, false sinon
*/
function LSaddon_maildir_support() {
$retval=true;
/**
* Verification du support Maildir par ldapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true si Maildir est pleinement supporté, false sinon
*/
function LSaddon_maildir_support() {
$retval = true;
// Dependance de librairie
if (!function_exists('createDirsByFTP')) {
if(!LSsession :: loadLSaddon('ftp')) {
LSerror :: addErrorCode('MAILDIR_SUPPORT_01');
$retval=false;
}
// Dependance de librairie
if (!function_exists('createDirsByFTP')) {
if(!LSsession :: loadLSaddon('ftp')) {
LSerror :: addErrorCode('MAILDIR_SUPPORT_01');
$retval = false;
}
}
$MUST_DEFINE_CONST= array(
'LS_MAILDIR_FTP_HOST',
'LS_MAILDIR_FTP_USER',
'LS_MAILDIR_FTP_MAILDIR_PATH',
'LS_MAILDIR_FTP_MAILDIR_PATH_REGEX'
$MUST_DEFINE_CONST = array(
'LS_MAILDIR_FTP_HOST',
'LS_MAILDIR_FTP_USER',
'LS_MAILDIR_FTP_MAILDIR_PATH',
'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;
}
/**
* 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;
if (!$dir) {
LSerror :: addErrorCode('MAILDIR_04');
return false;
}
/**
* 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 (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 $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;
}

View file

@ -36,232 +36,237 @@ LSerror :: defineError('POSIX_01',
___("POSIX : The attribute %{dependency} is missing. Unable to forge the attribute %{attr}.")
);
/**
* Verification du support POSIX par ldapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true si POSIX est pleinement supporté, false sinon
*/
function LSaddon_posix_support() {
/**
* Verification du support POSIX par ldapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true si POSIX est pleinement supporté, false sinon
*/
function LSaddon_posix_support() {
$retval = true;
$retval=true;
// Dependance de librairie
if (!function_exists('createDirsByFTP')) {
if(!LSsession :: loadLSaddon('ftp')) {
LSerror :: addErrorCode('POSIX_SUPPORT_02');
$retval=false;
}
// Dependance de librairie
if (!function_exists('createDirsByFTP')) {
if(!LSsession :: loadLSaddon('ftp')) {
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;
}
/**
* 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) {
$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',
);
$objects = LSldap :: search (
LS_POSIX_UIDNUMBER_ATTR.'=*',
NULL,
array(
'attributes' => array(
LS_POSIX_UIDNUMBER_ATTR
)
foreach($MUST_DEFINE_CONST as $const) {
if ( (!defined($const)) || (constant($const) == "")) {
LSerror :: addErrorCode('POSIX_SUPPORT_O1', $const);
$retval = false;
}
}
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))
return false;
foreach($objects as $object) {
if($object['attrs'][LS_POSIX_UIDNUMBER_ATTR] > $uidNumber)
$uidNumber = $object['attrs'][LS_POSIX_UIDNUMBER_ATTR];
}
$uidNumber++;
return $uidNumber;
if (!is_array($objects))
return false;
foreach($objects as $object) {
if($object['attrs'][LS_POSIX_UIDNUMBER_ATTR] > $uidNumber)
$uidNumber = $object['attrs'][LS_POSIX_UIDNUMBER_ATTR];
}
/**
* Generation de gidNumber
*
* @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) {
$uidNumber++;
return $uidNumber;
}
$objects = LSldap :: search (
LS_POSIX_GIDNUMBER_ATTR.'=*',
NULL,
array(
'attributes' => array(
LS_POSIX_GIDNUMBER_ATTR
)
/**
* Generation de gidNumber
*
* @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 . '=*',
NULL,
array(
'attributes' => array(
LS_POSIX_GIDNUMBER_ATTR
)
);
$gidNumber = LS_POSIX_GIDNUMBER_MIN_VAL;
)
);
$gidNumber = LS_POSIX_GIDNUMBER_MIN_VAL;
if (!is_array($objects))
return false;
foreach($objects as $object) {
if($object['attrs'][LS_POSIX_GIDNUMBER_ATTR] > $gidNumber)
$gidNumber = $object['attrs'][LS_POSIX_GIDNUMBER_ATTR];
}
$gidNumber++;
return $gidNumber;
if (!is_array($objects))
return false;
foreach($objects as $object) {
if($object['attrs'][LS_POSIX_GIDNUMBER_ATTR] > $gidNumber)
$gidNumber = $object['attrs'][LS_POSIX_GIDNUMBER_ATTR];
}
/**
* 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();
$home = LS_POSIX_HOMEDIRECTORY . $uid[0];
return $home;
$gidNumber++;
return $gidNumber;
}
/**
* 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;
}
/**
* Create home directory by FTP
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param LSldapObject $ldapObject L'objet ldap
*
* @return bool True on success, false otherwise
*/
function createHomeDirectoryByFTP($ldapObject) {
$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;
$uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue();
$home = LS_POSIX_HOMEDIRECTORY . $uid[0];
return $home;
}
/**
* Create home directory by FTP
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param LSldapObject $ldapObject L'objet ldap
*
* @return bool True on success, false otherwise
*/
function createHomeDirectoryByFTP($ldapObject) {
$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;
}
/**
* 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'));
if ( get_class($ldapObject -> attrs[ 'member' ]) != 'LSattribute' ) {
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'member', 'attr' => 'member'));
return false;
}
$obj_type = LSconfig::get(
'LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type'
);
if (empty($obj_type))
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'));
return false;
}
/**
* 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;
}
$obj_type=LSconfig::get('LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type');
if (empty($obj_type))
return false;
if (get_class($ldapObject -> attrs[ 'uniqueMember' ]) != 'LSattribute') {
LSerror :: addErrorCode('POSIX_01', array('dependency' => 'uniqueMember', 'attr' => 'memberUid'));
return false;
}
$uids = $ldapObject -> attrs[ 'memberUid' ] -> getValue();
$member = array();
if (is_array($uids)) {
foreach ( $uids as $uid ) {
$member[]='uid='.$uid.','.LSconfig::get('LSobjects.'.$obj_type.'.container_dn').','.LSsession::getTopDn();
$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 $member;
}
/**
* 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;
}
return $uids;
}
/**
* Generate shadowLastChange attribute value

View file

@ -229,78 +229,71 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
return generate_sambaSID($ldapObject, LS_SAMBA_GIDNUMBER_ATTR, LS_SAMBA_SID_BASE_GROUP);
}
/**
* Generation de sambaNTPassword
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param LSldapObject $ldapObject The user LSldapObject object
*
* @return string|false sambaNTPassword value on success, false otherwise
*/
function generate_sambaNTPassword($ldapObject) {
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
LSerror :: addErrorCode('SAMBA_01',array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaNTPassword'));
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;
/**
* Generation de sambaNTPassword
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param LSldapObject $ldapObject The user LSldapObject object
*
* @return string|false sambaNTPassword value on success, false otherwise
*/
function generate_sambaNTPassword($ldapObject) {
if ( get_class($ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ]) != 'LSattribute' ) {
LSerror :: addErrorCode('SAMBA_01',array('dependency' => LS_SAMBA_USERPASSWORD_ATTR, 'attr' => 'sambaNTPassword'));
return 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')
);
if (
!is_a(
$ldapObject -> attrs[ LS_SAMBA_USERPASSWORD_ATTR ] -> ldap,
'LSattr_ldap_password'
)
) {
LSerror :: addErrorCode('SAMBA_05', LS_SAMBA_USERPASSWORD_ATTR);
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

View file

@ -59,225 +59,223 @@ LSerror :: defineError('SSH_07',
___("SSH : Unable to rename folder from %{old} to %{new} on the remote server.")
);
/**
* Check LdapSaisie SSH support
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if SSH is fully supported, false otherwise
*/
function LSaddon_ssh_support() {
$retval=true;
/**
* Check LdapSaisie SSH support
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @return boolean true if SSH is fully supported, false otherwise
*/
function LSaddon_ssh_support() {
$retval=true;
// Check PhpSecLib library
if (!defined('PHPSECLIB_AUTOLOAD')) {
LSerror :: addErrorCode('SSH_SUPPORT_02','PHPSECLIB_AUTOLOAD');
$retval=false;
} else if(!LSsession::includeFile(PHPSECLIB_AUTOLOAD, true)) {
LSerror :: addErrorCode('SSH_SUPPORT_01');
// Check PhpSecLib library
if (!defined('PHPSECLIB_AUTOLOAD')) {
LSerror :: addErrorCode('SSH_SUPPORT_02','PHPSECLIB_AUTOLOAD');
$retval=false;
} else if(!LSsession::includeFile(PHPSECLIB_AUTOLOAD, true)) {
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;
}
return $retval;
}
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;
/**
* Delete 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 remove
* @param boolean $recursive Enable recursive mode (default : false)
* @param boolean $continue Enable continue mode : do not on error (default : false)
*
* @return boolean
*/
function removeDirsBySFTP($connection_params, $dirs, $recursive=false, $continue=false) {
$cnx = connectToSSH($connection_params, true);
if (!$cnx instanceof SFTP) return false;
if (!is_array($dirs)) {
$dirs = array($dirs);
}
/**
* 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("delete($dir) with recursive ".($recursive?'enabled':'disabled'));
if (!$cnx -> delete($dir, $recursive)) {
LSerror :: addErrorCode('SSH_06',$dir);
if (!$continue) return false;
$retval=false;
}
$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
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param array $connection_params Connection parameters
* @param array|string $dirs The directory/ies to remove
* @param boolean $recursive Enable recursive mode (default : false)
* @param boolean $continue Enable continue mode : do not on error (default : false)
*
* @return boolean
*/
function removeDirsBySFTP($connection_params, $dirs, $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("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;
/**
* Rename a directory throught SFTP
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param array $connection_params Connection parameters
* @param string $old The actual directory path to rename
* @param string $new The new directory path
*
* @return boolean
*/
function renameDirBySFTP($connection_params, $old, $new) {
$cnx = connectToSSH($connection_params, true);
if (!$cnx instanceof SFTP) return false;
LSlog :: get_logger('LSaddon_ssh') -> debug("rename($old, $new)");
if (!$cnx -> rename($old, $new)) {
LSerror :: addErrorCode('SSH_07',array('old' => $old,'new' => $new));
return false;
}
return true;
}
/**
* Rename a directory throught SFTP
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*
* @param array $connection_params Connection parameters
* @param string $old The actual directory path to rename
* @param string $new The new directory path
*
* @return boolean
*/
function renameDirBySFTP($connection_params, $old, $new) {
$cnx = connectToSSH($connection_params, true);
if (!$cnx instanceof SFTP) return false;
LSlog :: get_logger('LSaddon_ssh') -> debug("rename($old, $new)");
if (!$cnx -> rename($old, $new)) {
LSerror :: addErrorCode('SSH_07',array('old' => $old,'new' => $new));
return false;
}
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);
}
/**
* 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);
}