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',''); $result = restrictedArea($user, 'ftp','');
// Get parameters // Get parameters
$action = isset($_GET["action"])?$_GET["action"]:$_POST['action']; $action=GETPOST('action');
$section=isset($_GET["section"])?$_GET["section"]:$_POST['section']; $section=GETPOST('section');
if (! $section) $section='/'; if (! $section) $section='/';
$numero_ftp = GETPOST("numero_ftp"); $numero_ftp = GETPOST("numero_ftp");
if (! $numero_ftp) $numero_ftp=1; /* if (! $numero_ftp) $numero_ftp=1; */
$file=isset($_GET["file"])?$_GET["file"]:$_POST['file']; $file=GETPOST("file");
$confirm=GETPOST('confirm');
$upload_dir = $conf->ftp->dir_temp; $upload_dir = $conf->ftp->dir_temp;
$download_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_password=$conf->global->$s_ftp_password;
$ftp_passive=$conf->global->$s_ftp_passive; $ftp_passive=$conf->global->$s_ftp_passive;
// For result on connection
$ok=0;
$conn_id=null; // FTP connection ID $conn_id=null; // FTP connection ID
$mesg='';
/******************************************************************* /*
* ACTIONS * ACTIONS
* */
* Put here all code to do according to value of "action" parameter
********************************************************************/
// Envoie fichier // Submit file
if ( $_POST["sendit"] && ! empty($conf->global->MAIN_UPLOAD_DOC)) if (GETPOST("sendit") && ! empty($conf->global->MAIN_UPLOAD_DOC))
{ {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; 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 // 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->ref = $_POST["ref"];
$ecmdir->label = $_POST["label"]; $ecmdir->label = $_POST["label"];
@ -148,7 +150,7 @@ if ($_POST["action"] == 'add' && $user->rights->ftp->setup)
} }
// Remove file // Remove file
if ($_REQUEST['action'] == 'confirm_deletefile' && $_REQUEST['confirm'] == 'yes') if ($action == 'confirm_deletefile' && $_REQUEST['confirm'] == 'yes')
{ {
// set up a connection or die // set up a connection or die
if (! $conn_id) if (! $conn_id)
@ -246,7 +248,7 @@ if ($_POST["const"] && $_POST["delete"] && $_POST["delete"] == $langs->trans("De
} }
// Remove directory // Remove directory
if ($_REQUEST['action'] == 'confirm_deletesection' && $_REQUEST['confirm'] == 'yes') if ($action == 'confirm_deletesection' && $confirm == 'yes')
{ {
// set up a connection or die // set up a connection or die
if (! $conn_id) if (! $conn_id)
@ -286,7 +288,7 @@ if ($_REQUEST['action'] == 'confirm_deletesection' && $_REQUEST['confirm'] == 'y
} }
// Download directory // Download directory
if ($_REQUEST['action'] == 'download') if ($action == 'download')
{ {
// set up a connection or die // set up a connection or die
if (! $conn_id) if (! $conn_id)
@ -339,8 +341,7 @@ if ($_REQUEST['action'] == 'download')
} }
else else
{ {
//TODO: Translate setEventMessages($langs->trans('FailedToGetFile',$remotefile), null, 'errors');
setEventMessage('Failed to get file '.$remotefile, 'errors');
} }
} }
@ -355,11 +356,9 @@ if ($_REQUEST['action'] == 'download')
/******************************************************************* /*
* PAGE * View
* */
* Put here all code to do according to value of "action" parameter
********************************************************************/
llxHeader(); llxHeader();
@ -383,10 +382,7 @@ $formfile=new FormFile($db);
$userstatic = new User($db); $userstatic = new User($db);
//***********************
// List // List
//***********************
print_fiche_titre($langs->trans("FTPArea")); print_fiche_titre($langs->trans("FTPArea"));
print $langs->trans("FTPAreaDesc")."<br>"; print $langs->trans("FTPAreaDesc")."<br>";
@ -399,16 +395,15 @@ else
{ {
if (! empty($ftp_server)) if (! empty($ftp_server))
{ {
// Confirm remove file // 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); 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 // 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); 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,7 +412,8 @@ else
print $langs->trans("Server").': <b>'.$ftp_server.'</b><br>'; 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("Port").': <b>'.$ftp_port.'</b> '.($ftp_passive?"(Passive)":"(Active)").'<br>';
print $langs->trans("User").': <b>'.$ftp_user.'</b><br>'; print $langs->trans("User").': <b>'.$ftp_user.'</b><br>';
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").': '; print $langs->trans("Directory").': ';
$sectionarray=preg_split('|[\/]|',$section); $sectionarray=preg_split('|[\/]|',$section);
// For / // For /
@ -465,24 +461,43 @@ else
print '</tr>'."\n"; print '</tr>'."\n";
// set up a connection or die // 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); $resultarray=dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive);
$conn_id=$resultarray['conn_id']; $conn_id=$resultarray['conn_id'];
$ok=$resultarray['ok']; $ok=$resultarray['ok'];
$mesg=$resultarray['mesg']; $mesg=$resultarray['mesg'];
} }
if ($ok) if ($ok)
{ {
//$type = ftp_systype($conn_id); //$type = ftp_systype($conn_id);
$newsection=$section;
$newsectioniso=utf8_decode($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); $buff = ftp_rawlist($conn_id, $newsectioniso);
$contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist $contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist
//var_dump($contents); //var_dump($contents);
//var_dump($buff); //var_dump($buff);
}
$nboflines=count($contents); $nboflines=count($contents);
$var=true; $var=true;
@ -590,9 +605,16 @@ else
} }
print "</table>"; print "</table>";
if (! $ok)
{
print $mesg.'<br>'."\n";
setEventMessages($mesg, null, 'errors');
}
// Actions // Actions
/* /*
if ($user->rights->ftp->write && ! empty($section)) if ($user->rights->ftp->write && ! empty($section))
@ -610,21 +632,44 @@ else
print "</form>"; print "</form>";
} }
else else
{
$s_ftp_server='FTP_SERVER_1';
if (empty($s_ftp_server))
{ {
print $langs->trans("SetupOfFTPClientModuleNotComplete"); print $langs->trans("SetupOfFTPClientModuleNotComplete");
} }
else
{
print $langs->trans("ChooseAFTPEntryIntoMenu");
}
}
} }
print '<br>'; print '<br>';
// Close FTP connection // Close FTP connection
if ($conn_id) ftp_close($conn_id); 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);
}
}
// End of page
$db->close();
llxFooter(); llxFooter();
$db->close();
/** /**
* Connect to FTP server * Connect to FTP server
@ -642,21 +687,62 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect
global $langs, $conf; global $langs, $conf;
$ok=1; $ok=1;
$conn_id=null;
if (! is_numeric($ftp_port)) if (! is_numeric($ftp_port))
{ {
$mesg=$langs->trans("FailedToConnectToFTPServer",$ftp_server,$ftp_port); $mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServer",$ftp_server,$ftp_port);
$ok=0; $ok=0;
} }
if ($ok) if ($ok)
{ {
$connecttimeout=(empty($conf->global->FTP_CONNECT_TIMEOUT)?40:$conf->global->FTP_CONNECT_TIMEOUT); $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); if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
else $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout); {
if ($conn_id) 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_user)
{
if (! empty($conf->global->FTP_CONNECT_WITH_SFTP))
{
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
{ {
if (ftp_login($conn_id, $ftp_user, $ftp_password)) if (ftp_login($conn_id, $ftp_user, $ftp_password))
{ {
@ -669,19 +755,22 @@ function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $sect
} }
else else
{ {
$mesg=$langs->trans("FailedToConnectToFTPServerWithCredentials"); $mesg=$langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
$ok=0; $ok=0;
$error++;
}
} }
} }
} }
else 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; $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; 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>. FTPFailedToRemoveFile=Failed to remove file <b>%s</b>.
FTPFailedToRemoveDir=Failed to remove directory <b>%s</b> (Check permissions and that directory is empty). FTPFailedToRemoveDir=Failed to remove directory <b>%s</b> (Check permissions and that directory is empty).
FTPPassiveMode=Passive mode FTPPassiveMode=Passive mode
ChooseAFTPEntryIntoMenu=Choose a FTP entry into menu...
FailedToGetFile=Failed to get files %s