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

@ -54,16 +54,16 @@ LSerror :: defineError('FTP_05',
); );
/** /**
* 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');
@ -80,16 +80,16 @@ LSerror :: defineError('FTP_05',
/** /**
* 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 $host FTP server FQDN or IP address
* @param string $port Le port de connexion au serveur ftp * @param string $port The TCP port of the FTP server
* @param string $user Le nom d'utilidateur de connexion * @param string $user The username
* @param string $pwd Le mot de passe de connexion * @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) { function connectToFTP($host, $port, $user, $pwd) {
$cnx = new Net_FTP(); $cnx = new Net_FTP();
@ -124,14 +124,25 @@ LSerror :: defineError('FTP_05',
* @param array $dirs ou string Le(s) dossier(s) à ajouter * @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) * @return bool True ou false si il y a un problème durant la création du/des dossier(s)
* 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) { 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;
if (!is_array($dirs)) { foreach(ensureIsArray($dirs) as $dir) {
$dirs = array($dirs);
}
foreach($dirs as $dir) {
$do = $cnx -> mkdir($dir, true); $do = $cnx -> mkdir($dir, true);
if ($do instanceof PEAR_Error) { if ($do instanceof PEAR_Error) {
LSerror :: addErrorCode('FTP_02', $dir); LSerror :: addErrorCode('FTP_02', $dir);
@ -150,33 +161,27 @@ LSerror :: defineError('FTP_05',
} }
/** /**
* Suppression d'un ou plusieurs dossiers via FTP * Delete one or more directories throught FTP
* *
* Note : Attention : suppression récursive. Cela veut dire que les sous-dossiers * Caution : recursive deletion ! The content of the directory will be listed and deleted before the
* lister par un LS FTP seront supprimé d'abord. Attention : Si votre serveur * directory. If your FTP server is configured to mask some files or directories (for instance,
* FTP est configuré pour caché certains fichiers ou dossiers (dont le nom * starting by '.'), they will not be listed and deleted and this may cause the deletion to fail.
* commence par un '.' par exempl), ces fichiers ne seront pas supprimés et la * With VsFTPd, you have to add force_dot_files=1 in configuration.
* suppression du dossier parent échoura.
*
* Pour VsFTPd : Ajouter force_dot_files=1 dans la configuration.
* *
* @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 $host FTP server FQDN or IP address
* @param string $port Le port de connexion au serveur ftp * @param string $port The TCP port of the FTP server
* @param string $user Le nom d'utilidateur de connexion * @param string $user The username
* @param string $pwd Le mot de passe de connexion * @param string $pwd The password
* @param array $dirs ou string Le(s) dossier(s) à supprimer * @param array|string $dirs The directory/ies to remove
* *
* @return bool True ou false si il y a un problème durant la suppression du/des dossier(s) * @return boolean
*/ */
function removeDirsByFTP($host, $port, $user, $pwd, $dirs) { function removeDirsByFTP($host, $port, $user, $pwd, $dirs) {
$cnx = connectToFTP($host, $port, $user, $pwd); $cnx = connectToFTP($host, $port, $user, $pwd);
if (!$cnx) return false; if (!$cnx) return false;
if (!is_array($dirs)) { foreach(ensureIsArray($dirs) as $dir) {
$dirs = array($dirs);
}
foreach($dirs as $dir) {
if ($dir[strlen($dir)-1] != '/') { if ($dir[strlen($dir)-1] != '/') {
$dir .= '/'; $dir .= '/';
} }
@ -191,18 +196,18 @@ LSerror :: defineError('FTP_05',
} }
/** /**
* Renomage d'un dossier via FTP * Rename a directory throught FTP
* *
* @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 $host FTP server FQDN or IP address
* @param string $port Le port de connexion au serveur ftp * @param string $port The TCP port of the FTP server
* @param string $user Le nom d'utilidateur de connexion * @param string $user The username
* @param string $pwd Le mot de passe de connexion * @param string $pwd The password
* @param string $old Le dossier à renomer * @param string $old The actual directory path to rename
* @param string $new Le nouveau nom du dossier à renomer * @param string $new The new directory path
* *
* @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 renameDirByFTP($host, $port, $user, $pwd, $old, $new) {
$cnx = connectToFTP($host, $port, $user, $pwd); $cnx = connectToFTP($host, $port, $user, $pwd);

View file

@ -92,16 +92,21 @@ LSerror :: defineError('MAILDIR_04',
function createMaildirByFTP($ldapObject,$dir=null) { function createMaildirByFTP($ldapObject,$dir=null) {
if (!$dir) { if (!$dir) {
$dir = getMaildirPath($ldapObject); $dir = getMaildirPath($ldapObject);
if (!$dir) { if (!$dir)
return false; return false;
} }
}
$dirs = array( $dirs = array(
$dir.'/cur', $dir.'/cur',
$dir.'/new', $dir.'/new',
$dir.'/tmp' $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)) { 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'); LSerror :: addErrorCode('MAILDIR_01');
return false; return false;
} }
@ -122,11 +127,16 @@ LSerror :: defineError('MAILDIR_04',
function removeMaildirByFTP($ldapObject,$dir=null) { function removeMaildirByFTP($ldapObject,$dir=null) {
if (!$dir) { if (!$dir) {
$dir = getMaildirPath($ldapObject); $dir = getMaildirPath($ldapObject);
if (!$dir) { if (!$dir)
return false; return false;
} }
} if (
if (!removeDirsByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$dir)) { !removeDirsByFTP(
LS_MAILDIR_FTP_HOST, LS_MAILDIR_FTP_PORT,
LS_MAILDIR_FTP_USER, LS_MAILDIR_FTP_PWD,
$dir
)
) {
LSerror :: addErrorCode('MAILDIR_02'); LSerror :: addErrorCode('MAILDIR_02');
return false; return false;
} }
@ -145,16 +155,14 @@ LSerror :: defineError('MAILDIR_04',
function getMaildirPath($ldapObject) { function getMaildirPath($ldapObject) {
$dir = getFData(LS_MAILDIR_FTP_MAILDIR_PATH, $ldapObject, 'getValue'); $dir = getFData(LS_MAILDIR_FTP_MAILDIR_PATH, $ldapObject, 'getValue');
if (LS_MAILDIR_FTP_MAILDIR_PATH_REGEX != "") { if (constant("LS_MAILDIR_FTP_MAILDIR_PATH_REGEX")) {
if (preg_match(LS_MAILDIR_FTP_MAILDIR_PATH_REGEX,$dir,$regs)) { $dir = (
$dir = $regs[1]; preg_match(LS_MAILDIR_FTP_MAILDIR_PATH_REGEX, $dir, $regs)?
} $regs[1]:false
else { );
$dir = "";
}
} }
if ($dir=="") { if (!$dir) {
LSerror :: addErrorCode('MAILDIR_04'); LSerror :: addErrorCode('MAILDIR_04');
return false; return false;
} }
@ -173,7 +181,13 @@ LSerror :: defineError('MAILDIR_04',
* @return bool True ou false si il y a un problème durant le renomage 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) { function renameMaildirByFTP($old, $new) {
if (!renameDirByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$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'); LSerror :: addErrorCode('MAILDIR_03');
return false; return false;
} }

View file

@ -44,7 +44,6 @@ LSerror :: defineError('POSIX_01',
* @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 // Dependance de librairie
@ -90,7 +89,6 @@ LSerror :: defineError('POSIX_01',
* @return integer|false uidNumber ou false si il y a un problème durant la génération * @return integer|false uidNumber ou false si il y a un problème durant la génération
*/ */
function generate_uidNumber($ldapObject) { function generate_uidNumber($ldapObject) {
$objects = LSldap :: search ( $objects = LSldap :: search (
LS_POSIX_UIDNUMBER_ATTR.'=*', LS_POSIX_UIDNUMBER_ATTR.'=*',
NULL, NULL,
@ -112,7 +110,6 @@ LSerror :: defineError('POSIX_01',
$uidNumber++; $uidNumber++;
return $uidNumber; return $uidNumber;
} }
/** /**
@ -125,7 +122,6 @@ LSerror :: defineError('POSIX_01',
* @return integer|false gidNumber ou false si il y a un problème durant la génération * @return integer|false gidNumber ou false si il y a un problème durant la génération
*/ */
function generate_gidNumber($ldapObject) { function generate_gidNumber($ldapObject) {
$objects = LSldap :: search ( $objects = LSldap :: search (
LS_POSIX_GIDNUMBER_ATTR . '=*', LS_POSIX_GIDNUMBER_ATTR . '=*',
NULL, NULL,
@ -147,7 +143,6 @@ LSerror :: defineError('POSIX_01',
$gidNumber++; $gidNumber++;
return $gidNumber; return $gidNumber;
} }
/** /**
@ -168,7 +163,6 @@ LSerror :: defineError('POSIX_01',
$uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue(); $uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue();
$home = LS_POSIX_HOMEDIRECTORY . $uid[0]; $home = LS_POSIX_HOMEDIRECTORY . $uid[0];
return $home; return $home;
} }
/** /**
@ -182,7 +176,13 @@ LSerror :: defineError('POSIX_01',
*/ */
function createHomeDirectoryByFTP($ldapObject) { function createHomeDirectoryByFTP($ldapObject) {
$dir = getFData(LS_POSIX_HOMEDIRECTORY_FTP_PATH, $ldapObject, 'getValue'); $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)) { 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'); LSerror :: addErrorCode('POSIX_02');
return false; return false;
} }
@ -212,7 +212,9 @@ LSerror :: defineError('POSIX_01',
return false; return false;
} }
$obj_type=LSconfig::get('LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type'); $obj_type = LSconfig::get(
'LSobjects.'.get_class($ldapObject).'.attrs.memberUid.html_options.selectable_object.object_type'
);
if (empty($obj_type)) if (empty($obj_type))
return false; return false;
@ -220,11 +222,15 @@ LSerror :: defineError('POSIX_01',
$member = array(); $member = array();
if (is_array($uids)) { if (is_array($uids)) {
foreach ( $uids as $uid ) { foreach ( $uids as $uid ) {
$member[]='uid='.$uid.','.LSconfig::get('LSobjects.'.$obj_type.'.container_dn').','.LSsession::getTopDn(); $member[] = sprintf(
'uid=%s,%s,%s',
$uid,
LSconfig::get('LSobjects.'.$obj_type.'.container_dn'),
LSsession::getTopDn()
);
} }
} }
return $member; return $member;
} }
/** /**
@ -262,7 +268,6 @@ LSerror :: defineError('POSIX_01',
return $uids; return $uids;
} }
/** /**
* Generate shadowLastChange attribute value * Generate shadowLastChange attribute value
* *

View file

@ -229,7 +229,6 @@ 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
* *
@ -258,10 +257,7 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
$sambapassword = new smbHash; $sambapassword = new smbHash;
$sambaNTPassword = $sambapassword -> nthash($password); $sambaNTPassword = $sambapassword -> nthash($password);
if($sambaNTPassword == '') { return $sambaNTPassword?$sambaNTPassword:false;
return false;
}
return $sambaNTPassword;
} }
/** /**
@ -296,10 +292,7 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
$sambapassword = new smbHash; $sambapassword = new smbHash;
$sambaLMPassword = $sambapassword -> lmhash($password); $sambaLMPassword = $sambapassword -> lmhash($password);
if($sambaLMPassword == '') { return $sambaLMPassword?$sambaLMPassword:false;
return false;
}
return $sambaLMPassword;
} }
/** /**

View file

@ -81,8 +81,6 @@ LSerror :: defineError('SSH_07',
return $retval; return $retval;
} }
/** /**
* Connect to an SFTP server * Connect to an SFTP server
* *