LSaddon::ftp: Remove PEAR Net_FTP dependency

This commit is contained in:
Benjamin Renard 2023-10-13 19:04:22 +02:00
parent 9384d2e312
commit 7a4f0fac69
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
8 changed files with 134 additions and 128 deletions

9
.phpstan/compat.php Normal file
View file

@ -0,0 +1,9 @@
<?php
$config = [];
if (PHP_VERSION_ID < 80100) {
$config['parameters']['typeAliases']['FTP\Connection'] = 'resource';
}
return $config;

View file

@ -1,3 +1,6 @@
includes:
- compat.php
parameters: parameters:
level: 3 level: 3
paths: paths:

View file

@ -1,7 +1,6 @@
<?php <?php
require 'Net/LDAP2.php'; require 'Net/LDAP2.php';
require 'Net/FTP.php';
require 'Console/Table.php'; require 'Console/Table.php';
require 'Zxcvbn/autoload.php'; require 'Zxcvbn/autoload.php';
require '/usr/share/php/phpseclib/autoload.php'; require '/usr/share/php/phpseclib/autoload.php';

View file

@ -23,8 +23,8 @@
* La librairie File_CSV_DataSource (paquet php-file-csv-datasource dans Debian) * La librairie File_CSV_DataSource (paquet php-file-csv-datasource dans Debian)
* La librairie Console_Table (nécessaire pour le fonctionnement de l'outil CLI, paquet php-console-table dans Debian) * La librairie Console_Table (nécessaire pour le fonctionnement de l'outil CLI, paquet php-console-table dans Debian)
* Les librairies Mail et Mail_Mime (nécessaire pour l'envoi de courriels, paquets php-mail et php-mail-mime dans Debian) * Les librairies Mail et Mail_Mime (nécessaire pour l'envoi de courriels, paquets php-mail et php-mail-mime dans Debian)
* La librairie Net_FTP (nécessaire pour le fonctionnement du LSaddon FTP, paquet php-console-table dans Debian) * L'extension PHP ftp (nécessaire pour le fonctionnement du LSaddon FTP, paquet php-ftp dans Debian)
* La librairie PhpSecLib (nécessaire pour le fonctionnement du LSaddon SSH, paquet php-console-table dans Debian) * La librairie PhpSecLib (nécessaire pour le fonctionnement du LSaddon SSH, paquet php-phpseclib dans Debian)
################# #################
# Avertissement # # Avertissement #

4
debian/control vendored
View file

@ -6,8 +6,8 @@ Maintainer: Benjamin Renard <brenard@easter-eggs.com>
Package: ldapsaisie Package: ldapsaisie
Architecture: all Architecture: all
Depends: apache2 | httpd, php-ldap | php5-ldap, php-fpm | libapache2-mod-php5 | libapache2-mod-php | php5-cli | php-cli, smarty | smarty3, php-net-ldap2, php-net-ftp, php-mail, php-mail-mime, php-console-table Depends: apache2 | httpd, php-ldap | php5-ldap, php-fpm | libapache2-mod-php5 | libapache2-mod-php | php5-cli | php-cli, smarty | smarty3, php-net-ldap2, php-console-table
Recommends: php-mbstring, php-phpseclib, php-unidecode, php-zxcvbn Recommends: php-mbstring, php-phpseclib, php-unidecode, php-zxcvbn, php-ftp, php-mail, php-mail-mime
Description: web based interface for managing LDAP servers content Description: web based interface for managing LDAP servers content
LdapSaisie is a Web application developed to manage LDAP directory. LdapSaisie is a Web application developed to manage LDAP directory.
It has been written in PHP / JavaScript and is published under the It has been written in PHP / JavaScript and is published under the

View file

@ -36,12 +36,13 @@
[PEAR_Mail_Mime](https://pear.php.net/package/Mail_Mime) (nécessaire pour l'envoi de courriels, [PEAR_Mail_Mime](https://pear.php.net/package/Mail_Mime) (nécessaire pour l'envoi de courriels,
paquets `php-mail` et `php-mail-mime` dans Debian) paquets `php-mail` et `php-mail-mime` dans Debian)
- La librairie [Net_FTP](https://pear.php.net/package/Net_FTP) (nécessaire pour le fonctionnement du - L'[extension PHP `ftp`](https://www.php.net/manual/fr/intro.ftp.php) (nécessaire pour le
[LSaddon](../conf/index.md#configuration-des-lsaddons) FTP, paquet `php-console-table` dans Debian) fonctionnement du [LSaddon](../conf/index.md#configuration-des-lsaddons) FTP, paquet `php-ftp`
dans Debian)
- La librairie [PhpSecLib](https://github.com/phpseclib/phpseclib) (nécessaire pour le - La librairie [PhpSecLib](https://github.com/phpseclib/phpseclib) (nécessaire pour le
fonctionnement du [LSaddon](../conf/index.md#configuration-des-lsaddons) SSH, paquet `php-console-table` fonctionnement du [LSaddon](../conf/index.md#configuration-des-lsaddons) SSH, paquet
dans Debian) `php-phpseclib` dans Debian)
!!! warning !!! warning

View file

@ -1,30 +0,0 @@
<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* https://ldapsaisie.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
/*
************************************************
* Données de configuration pour le support FTP *
************************************************
*/
// Pear :: NET_FTP
define('NET_FTP','/usr/share/php/Net/FTP.php');

View file

@ -20,37 +20,38 @@
******************************************************************************/ ******************************************************************************/
// Messages d'erreur // Error messages
// Support // Support
LSerror :: defineError('FTP_SUPPORT_01', LSerror :: defineError('FTP_SUPPORT_01',
___("FTP Support : Pear::Net_FTP is missing.") ___("FTP support: PHP ftp extension is missing.")
); );
LSerror :: defineError('FTP_SUPPORT_02', // Other errors
___("FTP Support : The constant %{const} is not defined.")
);
// Autres erreurs
LSerror :: defineError('FTP_00', LSerror :: defineError('FTP_00',
___("Net_FTP Error : %{msg}") ___("FTP error: %{msg}")
); );
LSerror :: defineError('FTP_01', LSerror :: defineError('FTP_01',
___("FTP Support : Unable to connect to FTP Server (Step : %{step}).") ___("FTP: Unable to connect to FTP Server (Step : %{step}).")
); );
LSerror :: defineError('FTP_02', LSerror :: defineError('FTP_02',
___("FTP Support : Unable to make directory %{dir} on the remote server.") ___("FTP: Unable to make directory %{dir} on the remote server.")
); );
LSerror :: defineError('FTP_03', LSerror :: defineError('FTP_03',
___("FTP Support : Unable to delete directory %{dir} on the remote server.") ___("FTP: Unable to list directory %{dir} content on the remote server.")
); );
LSerror :: defineError('FTP_04', LSerror :: defineError('FTP_04',
___("FTP Support : Unable to modify rights on the directory %{dir} on the remote server.") ___("FTP: Unable to delete directory %{dir} on the remote server.")
); );
LSerror :: defineError('FTP_05', LSerror :: defineError('FTP_05',
___("FTP Support : Unable to rename folder from %{old} to %{new} on the remote server.") ___("FTP: Unable to delete file %{file} on the remote server.")
);
LSerror :: defineError('FTP_06',
___("FTP: Unable to modify rights on the directory %{dir} on the remote server.")
);
LSerror :: defineError('FTP_07',
___("FTP: Unable to rename folder from %{old} to %{new} on the remote server.")
); );
/** /**
@ -63,68 +64,52 @@ LSerror :: defineError('FTP_05',
function LSaddon_ftp_support() { function LSaddon_ftp_support() {
$retval = true; $retval = true;
// Net_FTP lib dependency // PHP ftp extension dependency
if (!class_exists('Net_FTP')) { if (!function_exists('ftp_connect')) {
if (!defined('NET_FTP')) { LSerror :: addErrorCode('FTP_SUPPORT_01');
LSerror :: addErrorCode('FTP_SUPPORT_02', 'NET_FTP'); $retval = false;
$retval = false;
} else if(!LSsession::includeFile(NET_FTP, true)) {
LSerror :: addErrorCode('FTP_SUPPORT_01');
$retval = false;
}
} }
return $retval; return $retval;
} }
/** /**
* Connect to FTP server * Log last FTP error
* * @return void
* @author Benjamin Renard <brenard@easter-eggs.com> */
* function _logLastFtpError() {
* @param string $host FTP server FQDN or IP address $error = error_get_last();
* @param string $port The TCP port of the FTP server if ($error) LSerror :: addErrorCode('FTP_00', $error['message']);
* @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', "2");
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false;
}
}
else {
LSerror :: addErrorCode('FTP_01', "1");
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false;
}
} }
/** /**
* Creation d'un ou plusieurs dossiers via 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
* @param array $dirs ou string Le(s) dossier(s) à ajouter *
* * @return FTP\Connection|resource|false FTP\Connection (or resource with PHP <= 8.1.0) in case of
* @return bool True ou false si il y a un problème durant la création du/des dossier(s) * success, false otherwise
*/
function connectToFTP($host, $port, $user, $pwd) {
$cnx = @ftp_connect($host, $port);
if ($cnx === false) {
_logLastFtpError();
LSerror :: addErrorCode('FTP_01', "1");
return false;
}
if (@ftp_login($cnx, $user, $pwd))
return $cnx;
_logLastFtpError();
LSerror :: addErrorCode('FTP_01', "2");
return false;
}
/**
* Create one or more directories throught FTP * Create one or more directories throught FTP
* *
* @author Benjamin Renard <brenard@easter-eggs.com> * @author Benjamin Renard <brenard@easter-eggs.com>
@ -135,7 +120,7 @@ function connectToFTP($host, $port, $user, $pwd) {
* @param string $pwd The password * @param string $pwd The password
* @param array|string $dirs The directory/ies to add * @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, * @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) * example: 0755 or 02755, default : default umask on the SSH server)
* *
* @return boolean * @return boolean
*/ */
@ -143,20 +128,68 @@ 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;
foreach(ensureIsArray($dirs) as $dir) { foreach(ensureIsArray($dirs) as $dir) {
$do = $cnx -> mkdir($dir, true); if (@ftp_mkdir($cnx, $dir) === false) {
if ($do instanceof PEAR_Error) { _logLastFtpError();
LSerror :: addErrorCode('FTP_02', $dir); LSerror :: addErrorCode('FTP_02', $dir);
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false; return false;
} }
if ($chmod) { if ($chmod && !@ftp_chmod($cnx, $chmod, $dir)) {
$do = $cnx -> chmod($dir, $chmod); _logLastFtpError();
if ($do instanceof PEAR_Error) { LSerror :: addErrorCode('FTP_06', $dir);
LSerror :: addErrorCode('FTP_04', $dir); }
LSerror :: addErrorCode('FTP_00', $do -> getMessage()); }
return true;
}
/**
* Internal function call recursively to remove a directory and its content
* @param FTP\Connection|resource $cnx The FTP\Connection object (or resource with PHP < 8.1.0)
* @param string $dir The directory path to remove
* @return boolean
*/
function _removeDirByFTP(&$cnx, $dir) {
if ($dir[strlen($dir)-1] == '/')
$dir = substr($dir, 0, strlen($dir)-1);
$items = @ftp_nlist($cnx, $dir);
LSlog :: get_logger('LSaddon_ftp') -> trace(
"_removeDirByFTP($dir): directory content: ".varDump($items));
if (!is_array($items)) {
_logLastFtpError();
LSerror :: addErrorCode('FTP_03', $dir);
return false;
}
foreach($items as $item) {
if (@ftp_chdir($cnx, $item)) {
LSlog :: get_logger('LSaddon_ftp') -> trace(
"_removeDirByFTP($dir): item '$item' is a directory, delete it recursively");
if (!_removeDirByFTP($cnx, $item))
return false;
}
else {
LSlog :: get_logger('LSaddon_ftp') -> trace(
"_removeDirByFTP($dir): item '$item' is a file, delete it");
if (!@ftp_delete($cnx, $item)) {
_logLastFtpError();
LSerror :: addErrorCode('FTP_05', $item);
return false;
}
else {
LSlog :: get_logger('LSaddon_ftp') -> trace(
"_removeDirByFTP($dir): file '$item' removed");
} }
} }
} }
if (@!ftp_rmdir($cnx, $dir)) {
_logLastFtpError();
LSerror :: addErrorCode('FTP_04', $dir);
return false;
}
else {
LSlog :: get_logger('LSaddon_ftp') -> trace(
"_removeDirByFTP($dir): directory '$dir' removed");
}
return true; return true;
} }
@ -181,17 +214,9 @@ function createDirsByFTP($host, $port, $user, $pwd, $dirs, $chmod=NULL) {
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;
foreach(ensureIsArray($dirs) as $dir) { foreach(ensureIsArray($dirs) as $dir)
if ($dir[strlen($dir)-1] != '/') { if (!_removeDirByFTP($cnx, $dir))
$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 false;
}
}
return true; return true;
} }
@ -212,10 +237,9 @@ function removeDirsByFTP($host, $port, $user, $pwd, $dirs) {
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);
if (!$cnx) return false; if (!$cnx) return false;
$do = $cnx -> rename($old, $new); if (!@ftp_rename($cnx, $old, $new)) {
if ($do instanceof PEAR_Error) { _logLastFtpError();
LSerror :: addErrorCode('FTP_05', array('old' => $old, 'new' => $new)); LSerror :: addErrorCode('FTP_07', array('old' => $old, 'new' => $new));
LSerror :: addErrorCode('FTP_00', $do -> getMessage());
return false; return false;
} }
return true; return true;