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>
*
* @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() {
$retval = true;
// Dependance de librairie
// Net_FTP lib dependency
if (!class_exists('Net_FTP')) {
if (!defined('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>
*
* @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 $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 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();
@ -124,14 +124,25 @@ LSerror :: defineError('FTP_05',
* @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)
* 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;
if (!is_array($dirs)) {
$dirs = array($dirs);
}
foreach($dirs as $dir) {
foreach(ensureIsArray($dirs) as $dir) {
$do = $cnx -> mkdir($dir, true);
if ($do instanceof PEAR_Error) {
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
* 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.
* 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 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
* @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 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) {
$cnx = connectToFTP($host, $port, $user, $pwd);
if (!$cnx) return false;
if (!is_array($dirs)) {
$dirs = array($dirs);
}
foreach($dirs as $dir) {
foreach(ensureIsArray($dirs) as $dir) {
if ($dir[strlen($dir)-1] != '/') {
$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>
*
* @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
* @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 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) {
$cnx = connectToFTP($host, $port, $user, $pwd);

View file

@ -92,16 +92,21 @@ LSerror :: defineError('MAILDIR_04',
function createMaildirByFTP($ldapObject,$dir=null) {
if (!$dir) {
$dir = getMaildirPath($ldapObject);
if (!$dir) {
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)) {
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;
}
@ -122,11 +127,16 @@ LSerror :: defineError('MAILDIR_04',
function removeMaildirByFTP($ldapObject,$dir=null) {
if (!$dir) {
$dir = getMaildirPath($ldapObject);
if (!$dir) {
if (!$dir)
return false;
}
}
if (!removeDirsByFTP(LS_MAILDIR_FTP_HOST,LS_MAILDIR_FTP_PORT,LS_MAILDIR_FTP_USER,LS_MAILDIR_FTP_PWD,$dir)) {
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;
}
@ -145,16 +155,14 @@ LSerror :: defineError('MAILDIR_04',
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 (constant("LS_MAILDIR_FTP_MAILDIR_PATH_REGEX")) {
$dir = (
preg_match(LS_MAILDIR_FTP_MAILDIR_PATH_REGEX, $dir, $regs)?
$regs[1]:false
);
}
if ($dir=="") {
if (!$dir) {
LSerror :: addErrorCode('MAILDIR_04');
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
*/
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');
return false;
}

View file

@ -44,7 +44,6 @@ LSerror :: defineError('POSIX_01',
* @return boolean true si POSIX est pleinement supporté, false sinon
*/
function LSaddon_posix_support() {
$retval = true;
// 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
*/
function generate_uidNumber($ldapObject) {
$objects = LSldap :: search (
LS_POSIX_UIDNUMBER_ATTR.'=*',
NULL,
@ -112,7 +110,6 @@ LSerror :: defineError('POSIX_01',
$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
*/
function generate_gidNumber($ldapObject) {
$objects = LSldap :: search (
LS_POSIX_GIDNUMBER_ATTR . '=*',
NULL,
@ -147,7 +143,6 @@ LSerror :: defineError('POSIX_01',
$gidNumber++;
return $gidNumber;
}
/**
@ -168,7 +163,6 @@ LSerror :: defineError('POSIX_01',
$uid = $ldapObject -> attrs[ LS_POSIX_UID_ATTR ] -> getValue();
$home = LS_POSIX_HOMEDIRECTORY . $uid[0];
return $home;
}
/**
@ -182,7 +176,13 @@ LSerror :: defineError('POSIX_01',
*/
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)) {
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;
}
@ -212,7 +212,9 @@ LSerror :: defineError('POSIX_01',
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))
return false;
@ -220,11 +222,15 @@ LSerror :: defineError('POSIX_01',
$member = array();
if (is_array($uids)) {
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;
}
/**
@ -262,7 +268,6 @@ LSerror :: defineError('POSIX_01',
return $uids;
}
/**
* 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);
}
/**
* Generation de sambaNTPassword
*
@ -258,10 +257,7 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
$sambapassword = new smbHash;
$sambaNTPassword = $sambapassword -> nthash($password);
if($sambaNTPassword == '') {
return false;
}
return $sambaNTPassword;
return $sambaNTPassword?$sambaNTPassword:false;
}
/**
@ -296,10 +292,7 @@ function generate_sambaPrimaryGroupSID($ldapObject) {
$sambapassword = new smbHash;
$sambaLMPassword = $sambapassword -> lmhash($password);
if($sambaLMPassword == '') {
return false;
}
return $sambaLMPassword;
return $sambaLMPassword?$sambaLMPassword:false;
}
/**

View file

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