Start to introduce sftp

This commit is contained in:
Laurent Destailleur 2015-09-07 11:44:45 +02:00
parent 91ddca6ab0
commit e85921c82f
2 changed files with 151 additions and 60 deletions

View File

@ -38,12 +38,13 @@ if ($user->societe_id) $socid=$user->societe_id;
$result = restrictedArea($user, 'ftp','');
// Get parameters
$action = isset($_GET["action"])?$_GET["action"]:$_POST['action'];
$section=isset($_GET["section"])?$_GET["section"]:$_POST['section'];
$action=GETPOST('action');
$section=GETPOST('section');
if (! $section) $section='/';
$numero_ftp = GETPOST("numero_ftp");
if (! $numero_ftp) $numero_ftp=1;
$file=isset($_GET["file"])?$_GET["file"]:$_POST['file'];
/* if (! $numero_ftp) $numero_ftp=1; */
$file=GETPOST("file");
$confirm=GETPOST('confirm');
$upload_dir = $conf->ftp->dir_temp;
$download_dir = $conf->ftp->dir_temp;
@ -71,18 +72,19 @@ $ftp_user=$conf->global->$s_ftp_user;
$ftp_password=$conf->global->$s_ftp_password;
$ftp_passive=$conf->global->$s_ftp_passive;
// For result on connection
$ok=0;
$conn_id=null; // FTP connection ID
$mesg='';
/*******************************************************************
/*
* ACTIONS
*
* Put here all code to do according to value of "action" parameter
********************************************************************/
*/
// Envoie fichier
if ( $_POST["sendit"] && ! empty($conf->global->MAIN_UPLOAD_DOC))
// Submit file
if (GETPOST("sendit") && ! empty($conf->global->MAIN_UPLOAD_DOC))
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -127,7 +129,7 @@ if ( $_POST["sendit"] && ! empty($conf->global->MAIN_UPLOAD_DOC))
}
// Action ajout d'un rep
if ($_POST["action"] == 'add' && $user->rights->ftp->setup)
if ($action == 'add' && $user->rights->ftp->setup)
{
$ecmdir->ref = $_POST["ref"];
$ecmdir->label = $_POST["label"];
@ -148,7 +150,7 @@ if ($_POST["action"] == 'add' && $user->rights->ftp->setup)
}
// Remove file
if ($_REQUEST['action'] == 'confirm_deletefile' && $_REQUEST['confirm'] == 'yes')
if ($action == 'confirm_deletefile' && $_REQUEST['confirm'] == 'yes')
{
// set up a connection or die
if (! $conn_id)
@ -246,7 +248,7 @@ if ($_POST["const"] && $_POST["delete"] && $_POST["delete"] == $langs->trans("De
}
// Remove directory
if ($_REQUEST['action'] == 'confirm_deletesection' && $_REQUEST['confirm'] == 'yes')
if ($action == 'confirm_deletesection' && $confirm == 'yes')
{
// set up a connection or die
if (! $conn_id)
@ -286,7 +288,7 @@ if ($_REQUEST['action'] == 'confirm_deletesection' && $_REQUEST['confirm'] == 'y
}
// Download directory
if ($_REQUEST['action'] == 'download')
if ($action == 'download')
{
// set up a connection or die
if (! $conn_id)
@ -339,8 +341,7 @@ if ($_REQUEST['action'] == 'download')
}
else
{
//TODO: Translate
setEventMessage('Failed to get file '.$remotefile, 'errors');
setEventMessages($langs->trans('FailedToGetFile',$remotefile), null, 'errors');
}
}
@ -355,11 +356,9 @@ if ($_REQUEST['action'] == 'download')
/*******************************************************************
* PAGE
*
* Put here all code to do according to value of "action" parameter
********************************************************************/
/*
* View
*/
llxHeader();
@ -383,10 +382,7 @@ $formfile=new FormFile($db);
$userstatic = new User($db);
//***********************
// List
//***********************
print_fiche_titre($langs->trans("FTPArea"));
print $langs->trans("FTPAreaDesc")."<br>";
@ -397,18 +393,17 @@ if (! function_exists('ftp_connect'))
}
else
{
if (! empty($ftp_server))
if (! empty($ftp_server))
{
// Confirm remove file
if ($_GET['action'] == 'delete')
if ($action == 'delete')
{
print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'&section='.urlencode($_REQUEST["section"]).'&file='.urlencode($_GET["file"]), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile','','',1);
}
// Confirmation de la suppression d'une ligne categorie
if ($_GET['action'] == 'delete_section')
if ($action == 'delete_section')
{
print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'&section='.urlencode($_REQUEST["section"]).'&file='.urlencode($_GET["file"]), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection',$ecmdir->label), 'confirm_deletesection','','',1);
@ -417,8 +412,9 @@ else
print $langs->trans("Server").': <b>'.$ftp_server.'</b><br>';
print $langs->trans("Port").': <b>'.$ftp_port.'</b> '.($ftp_passive?"(Passive)":"(Active)").'<br>';
print $langs->trans("User").': <b>'.$ftp_user.'</b><br>';
print $langs->trans("Directory").': ';
print $langs->trans("FTPs (FTP over SSH)").': <b>'.yn($conf->global->FTP_CONNECT_WITH_SSL).'</b><br>';
print $langs->trans("SFTP (FTP as a subsytem of SSH)").': <b>'.yn($conf->global->FTP_CONNECT_WITH_SFTP).'</b><br>';
print $langs->trans("Directory").': ';
$sectionarray=preg_split('|[\/]|',$section);
// For /
$newsection='/';
@ -465,24 +461,43 @@ else
print '</tr>'."\n";
// set up a connection or die
if (! $conn_id)
if (empty($conn_id))
{
$resultarray=dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive);
$conn_id=$resultarray['conn_id'];
$ok=$resultarray['ok'];
$mesg=$resultarray['mesg'];
}
if ($ok)
{
//$type = ftp_systype($conn_id);
$newsectioniso=utf8_decode($section);
$buff = ftp_rawlist($conn_id, $newsectioniso);
$contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist
//var_dump($contents);
//var_dump($buff);
$newsection=$section;
$newsectioniso=utf8_decode($section);
//$newsection='/home';
// List content of directory ($newsection = '/', '/home', ...)
if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
{
if ($newsection == '/') $newsection='/./'; # workaround for bug https://bugs.php.net/bug.php?id=64169
//$dirHandle = opendir("ssh2.sftp://$conn_id".$newsection);
//var_dump($dirHandle);
$contents = scandir('ssh2.sftp://' . $conn_id . $newsection);
$buff=array();
foreach($contents as $i => $key)
{
$buff[$i]="---------- - root root 1234 Aug 01 2000 ".$key;
}
}
else
{
$buff = ftp_rawlist($conn_id, $newsectioniso);
$contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist
//var_dump($contents);
//var_dump($buff);
}
$nboflines=count($contents);
$var=true;
@ -590,9 +605,16 @@ else
}
print "</table>";
if (! $ok)
{
print $mesg.'<br>'."\n";
setEventMessages($mesg, null, 'errors');
}
// Actions
/*
if ($user->rights->ftp->write && ! empty($section))
@ -611,20 +633,43 @@ else
}
else
{
print $langs->trans("SetupOfFTPClientModuleNotComplete");
$s_ftp_server='FTP_SERVER_1';
if (empty($s_ftp_server))
{
print $langs->trans("SetupOfFTPClientModuleNotComplete");
}
else
{
print $langs->trans("ChooseAFTPEntryIntoMenu");
}
}
}
print '<br>';
// Close FTP connection
if ($conn_id) ftp_close($conn_id);
// End of page
$db->close();
if ($conn_id)
{
if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
{
}
else if (! empty($conf->global->FTP_CONNECT_WITH_SSL))
{
ftp_close($conn_id);
}
else
{
ftp_close($conn_id);
}
}
llxFooter();
$db->close();
/**
* Connect to FTP server
@ -642,46 +687,90 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect
global $langs, $conf;
$ok=1;
$conn_id=null;
if (! is_numeric($ftp_port))
{
$mesg=$langs->trans("FailedToConnectToFTPServer",$ftp_server,$ftp_port);
$mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServer",$ftp_server,$ftp_port);
$ok=0;
}
if ($ok)
{
$connecttimeout=(empty($conf->global->FTP_CONNECT_TIMEOUT)?40:$conf->global->FTP_CONNECT_TIMEOUT);
if (! empty($conf->global->FTP_CONNECT_WITH_SSL)) $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
else $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
if ($conn_id)
if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
{
dol_syslog('Try to connect with ssh2_ftp');
$tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
}
else if (! empty($conf->global->FTP_CONNECT_WITH_SSL))
{
dol_syslog('Try to connect with ftp_ssl_connect');
$conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
}
else
{
dol_syslog('Try to connect with ftp_connect');
$conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
}
if ($conn_id || $tmp_conn_id)
{
if ($ftp_user)
{
if (ftp_login($conn_id, $ftp_user, $ftp_password))
if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
{
// Turn on passive mode transfers (must be after a successful login
if ($ftp_passive) ftp_pasv($conn_id, true);
// Change the dir
$newsectioniso=utf8_decode($section);
ftp_chdir($conn_id, $newsectioniso);
}
if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password))
{
// Turn on passive mode transfers (must be after a successful login
//if ($ftp_passive) ftp_pasv($conn_id, true);
// Change the dir
$newsectioniso=utf8_decode($section);
//ftp_chdir($conn_id, $newsectioniso);
$conn_id = ssh2_sftp($tmp_conn_id);
if (! $conn_id)
{
$mesg=$langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
$ok=0;
$error++;
}
}
else
{
$mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
$ok=0;
$error++;
}
}
else
{
$mesg=$langs->trans("FailedToConnectToFTPServerWithCredentials");
$ok=0;
if (ftp_login($conn_id, $ftp_user, $ftp_password))
{
// Turn on passive mode transfers (must be after a successful login
if ($ftp_passive) ftp_pasv($conn_id, true);
// Change the dir
$newsectioniso=utf8_decode($section);
ftp_chdir($conn_id, $newsectioniso);
}
else
{
$mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
$ok=0;
$error++;
}
}
}
}
else
{
$mesg=$langs->trans("FailedToConnectToFTPServer",$ftp_server,$ftp_port);
dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
$mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServer",$ftp_server,$ftp_port);
$ok=0;
}
}
$arrayresult=array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg);
$arrayresult=array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
return $arrayresult;
}

View File

@ -10,3 +10,5 @@ FailedToConnectToFTPServerWithCredentials=Failed to login to FTP server with def
FTPFailedToRemoveFile=Failed to remove file <b>%s</b>.
FTPFailedToRemoveDir=Failed to remove directory <b>%s</b> (Check permissions and that directory is empty).
FTPPassiveMode=Passive mode
ChooseAFTPEntryIntoMenu=Choose a FTP entry into menu...
FailedToGetFile=Failed to get files %s