26 require
'../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formfile.class.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/treeview.lib.php';
32 $langs->loadLangs(array(
'companies',
'other'));
36 $socid = $user->socid;
41 $action =
GETPOST(
'action',
'aZ09');
46 $numero_ftp =
GETPOST(
"numero_ftp");
51 $upload_dir = $conf->ftp->dir_temp;
52 $download_dir = $conf->ftp->dir_temp;
54 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
55 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
56 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
58 if (empty($page) || $page == -1) {
61 $offset = $limit * $page;
62 $pageprev = $page - 1;
63 $pagenext = $page + 1;
71 $s_ftp_name =
'FTP_NAME_'.$numero_ftp;
72 $s_ftp_server =
'FTP_SERVER_'.$numero_ftp;
73 $s_ftp_port =
'FTP_PORT_'.$numero_ftp;
74 $s_ftp_user =
'FTP_USER_'.$numero_ftp;
75 $s_ftp_password =
'FTP_PASSWORD_'.$numero_ftp;
76 $s_ftp_passive =
'FTP_PASSIVE_'.$numero_ftp;
80 if (empty($ftp_port)) {
99 if (
GETPOST(
"sendit") && !empty($conf->global->MAIN_UPLOAD_DOC)) {
100 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
102 $result = $ecmdir->fetch(
GETPOST(
"section",
'int'));
103 if (!($result > 0)) {
107 $relativepath = $ecmdir->getRelativePath();
108 $upload_dir = $conf->ecm->dir_output.
'/'.$relativepath;
112 if (is_numeric($resupload) && $resupload > 0) {
113 $result = $ecmdir->changeNbOfFiles(
'+');
115 $langs->load(
"errors");
116 if ($resupload < 0) {
118 } elseif (preg_match(
'/ErrorFileIsInfectedWithAVirus/', $resupload)) {
120 setEventMessages($langs->trans(
"ErrorFileIsInfectedWithAVirus"),
null,
'errors');
128 $langs->load(
"errors");
129 setEventMessages($langs->trans(
"ErrorFailToCreateDir", $upload_dir),
null,
'errors');
134 if ($action ==
'add' && $user->rights->ftp->setup) {
136 $ecmdir->label =
GETPOST(
"label");
137 $ecmdir->description =
GETPOST(
"desc");
139 $id = $ecmdir->create($user);
141 header(
"Location: ".$_SERVER[
"PHP_SELF"]);
150 if ($action ==
'confirm_deletefile' &&
GETPOST(
'confirm') ==
'yes') {
153 $newsectioniso = utf8_decode($section);
154 $resultarray =
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
155 $conn_id = $resultarray[
'conn_id'];
156 $ok = $resultarray[
'ok'];
157 $mesg = $resultarray[
'mesg'];
160 if ($conn_id && $ok && !$mesg) {
161 $newsection = $section;
162 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
163 $newsection = ssh2_sftp_realpath($conn_id,
".").
'/./';
166 $langs->load(
"other");
170 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
171 $newremotefileiso = utf8_decode($remotefile);
174 dol_syslog(
"ftp/index.php ftp_delete ".$newremotefileiso);
175 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
176 $result = ssh2_sftp_unlink($conn_id, $newremotefileiso);
178 $result = @ftp_delete($conn_id, $newremotefileiso);
183 dol_syslog(
"ftp/index.php ftp_delete", LOG_ERR);
184 setEventMessages($langs->trans(
"FTPFailedToRemoveFile", $file),
null,
'errors');
196 if (
GETPOST(
"const",
'array') &&
GETPOST(
"delete") &&
GETPOST(
"delete") == $langs->trans(
"Delete")) {
199 $newsectioniso = utf8_decode($section);
200 $resultarray =
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
201 $conn_id = $resultarray[
'conn_id'];
202 $ok = $resultarray[
'ok'];
203 $mesg = $resultarray[
'mesg'];
206 if ($conn_id && $ok && !$mesg) {
207 foreach (
GETPOST(
'const',
'array') as $const) {
208 if ($const[
"check"]) {
209 $langs->load(
"other");
212 $file = $const[
"file"];
213 $newsection = $const[
"section"];
214 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
215 $newsection = ssh2_sftp_realpath($conn_id,
".").
'/./';
217 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
218 $newremotefileiso = utf8_decode($remotefile);
221 dol_syslog(
"ftp/index.php ftp_delete n files for ".$newremotefileiso);
222 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
223 $result = ssh2_sftp_unlink($conn_id, $newremotefileiso);
225 $result = @ftp_delete($conn_id, $newremotefileiso);
230 dol_syslog(
"ftp/index.php ftp_delete n files", LOG_ERR);
231 setEventMessages($langs->trans(
"FTPFailedToRemoveFile", $file),
null,
'errors');
245 if ($action ==
'confirm_deletesection' && $confirm ==
'yes') {
248 $newsectioniso = utf8_decode($section);
249 $resultarray =
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
250 $conn_id = $resultarray[
'conn_id'];
251 $ok = $resultarray[
'ok'];
252 $mesg = $resultarray[
'mesg'];
255 if ($conn_id && $ok && !$mesg) {
256 $newsection = $section;
257 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
258 $newsection = ssh2_sftp_realpath($conn_id,
".").
'/./';
263 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
264 $newremotefileiso = utf8_decode($remotefile);
266 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
267 $result = ssh2_sftp_rmdir($conn_id, $newremotefileiso);
269 $result = @ftp_rmdir($conn_id, $newremotefileiso);
274 setEventMessages($langs->trans(
"FTPFailedToRemoveDir", $file),
null,
'errors');
286 if ($action ==
'download') {
289 $newsectioniso = utf8_decode($section);
290 $resultarray =
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
291 $conn_id = $resultarray[
'conn_id'];
292 $ok = $resultarray[
'ok'];
293 $mesg = $resultarray[
'mesg'];
296 if ($conn_id && $ok && !$mesg) {
298 $localfile = tempnam($download_dir,
'dol_');
300 $newsection = $section;
301 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
302 $newsection = ssh2_sftp_realpath($conn_id,
".").
'/./';
307 $remotefile = $newsection.(preg_match(
'@[\\\/]$@', $newsection) ?
'' :
'/').$file;
308 $newremotefileiso = utf8_decode($remotefile);
310 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
311 $result = fopen(
'ssh2.sftp://'.intval($conn_id).$newremotefileiso,
'r');
313 $result = ftp_get($conn_id, $localfile, $newremotefileiso, FTP_BINARY);
316 if (!empty($conf->global->MAIN_UMASK)) {
317 @chmod($localfile, octdec($conf->global->MAIN_UMASK));
321 $type =
'application/octet-stream';
333 header(
'Content-Type: '.$type);
336 header(
'Content-Disposition: attachment; filename="'.$filename.
'"');
338 header(
'Content-Disposition: inline; filename="'.$filename.
'"');
342 header(
'Cache-Control: Public, must-revalidate');
343 header(
'Pragma: public');
345 readfile($localfile);
351 setEventMessages($langs->transnoentitiesnoconv(
'FailedToGetFile', $remotefile),
null,
'errors');
370 if ($conf->use_javascript_ajax) {
372 <script
type=
"text/javascript">
373 jQuery(document).ready(
function() {
374 jQuery(
"#delconst").hide();
376 jQuery(
".checkboxfordelete").click(
function() {
377 jQuery(
"#delconst").show();
380 $(
"#checkall").click(
function() {
381 $(
".checkboxfordelete").prop(
'checked',
true);
382 jQuery(
"#delconst").show();
384 $(
"#checknone").click(
function() {
385 $(
".checkboxfordelete").prop(
'checked',
false);
386 jQuery(
"#delconst").hide();
398 $userstatic =
new User($db);
404 print $langs->trans(
"FTPAreaDesc").
"<br>";
406 if (!function_exists(
'ftp_connect')) {
407 print $langs->trans(
"FTPFeatureNotSupportedByYourPHP");
409 if (!empty($ftp_server)) {
411 if ($action ==
'delete') {
412 print
$form->formconfirm($_SERVER[
"PHP_SELF"].
'?numero_ftp='.$numero_ftp.
'§ion='.urlencode(
GETPOST(
'section')).
'&file='.urlencode(
GETPOST(
'file')), $langs->trans(
'DeleteFile'), $langs->trans(
'ConfirmDeleteFile'),
'confirm_deletefile',
'',
'', 1);
416 if ($action ==
'delete_section') {
417 print
$form->formconfirm($_SERVER[
"PHP_SELF"].
'?numero_ftp='.$numero_ftp.
'§ion='.urlencode(
GETPOST(
'section')).
'&file='.urlencode(
GETPOST(
'file')), $langs->trans(
'DeleteSection'), $langs->trans(
'ConfirmDeleteSection', $ecmdir->label),
'confirm_deletesection',
'',
'', 1);
420 print $langs->trans(
"Server").
': <b>'.$ftp_server.
'</b><br>';
421 print $langs->trans(
"Port").
': <b>'.$ftp_port.
'</b> '.($ftp_passive ?
"(Passive)" :
"(Active)").
'<br>';
422 print $langs->trans(
"User").
': <b>'.$ftp_user.
'</b><br>';
423 print $langs->trans(
"FTPs (FTP over SSH)").
': <b>'.
yn($conf->global->FTP_CONNECT_WITH_SSL).
'</b><br>';
424 print $langs->trans(
"SFTP (FTP as a subsytem of SSH)").
': <b>'.
yn($conf->global->FTP_CONNECT_WITH_SFTP).
'</b><br>';
425 print $langs->trans(
"Directory").
': ';
426 $sectionarray = preg_split(
'|[\/]|', $section);
429 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=refreshmanual&numero_ftp='.$numero_ftp.($newsection ?
'§ion='.urlencode($newsection) :
'').
'">';
434 foreach ($sectionarray as $val) {
443 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=refreshmanual&numero_ftp='.$numero_ftp.($newsection ?
'§ion='.urlencode($newsection) :
'').
'">';
451 print
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
452 print
'<input type="hidden" name="numero_ftp" value="'.$numero_ftp.
'">';
453 print
'<input type="hidden" name="token" value="'.newToken().
'">';
457 print
'<table width="100%" class="noborder">'.
"\n";
459 print
'<tr class="liste_titre">'.
"\n";
460 print
'<td class="liste_titre left">'.$langs->trans(
"Content").
'</td>'.
"\n";
461 print
'<td class="liste_titre center">'.$langs->trans(
"Size").
'</td>'.
"\n";
462 print
'<td class="liste_titre center">'.$langs->trans(
"Date").
'</td>'.
"\n";
463 print
'<td class="liste_titre center">'.$langs->trans(
"Owner").
'</td>'.
"\n";
464 print
'<td class="liste_titre center">'.$langs->trans(
"Group").
'</td>'.
"\n";
465 print
'<td class="liste_titre center">'.$langs->trans(
"Permissions").
'</td>'.
"\n";
466 print
'<td class="liste_titre nowrap right">';
467 if ($conf->use_javascript_ajax) {
468 print
'<a href="#" id="checkall">'.$langs->trans(
"All").
'</a> / <a href="#" id="checknone">'.$langs->trans(
"None").
'</a> ';
470 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=refreshmanual&numero_ftp='.$numero_ftp.($section ?
'§ion='.urlencode($section) :
'').
'">'.
img_picto($langs->trans(
"Refresh"),
'refresh').
'</a> ';
475 if (empty($conn_id)) {
476 $resultarray =
dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive);
478 $conn_id = $resultarray[
'conn_id'];
479 $ok = $resultarray[
'ok'];
480 $mesg = $resultarray[
'mesg'];
486 $newsection = $section;
487 $newsectioniso = utf8_decode($section);
491 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
492 if ($newsection ==
'/') {
494 $newsection = ssh2_sftp_realpath($conn_id,
".").
'/./';
501 $contents = scandir(
'ssh2.sftp://'.intval($conn_id).$newsection);
503 foreach ($contents as $i => $key) {
504 $buff[$i] =
"---------- - root root 1234 Aug 01 2000 ".$key;
521 $buff = ftp_rawlist($conn_id, $newsectioniso);
522 $contents = ftp_nlist($conn_id, $newsectioniso);
527 $nboflines = count($contents);
528 $rawlisthasfailed =
false;
531 while ($i < $nboflines && $i < 1000) {
532 $vals = preg_split(
'@ +@', utf8_encode($buff[$i]), 9);
537 $rawlisthasfailed =
true;
538 $file = utf8_encode($contents[$i]);
541 if ($file ==
'.' || ($file ==
'..' && $section ==
'/')) {
551 } elseif (!$rawlisthasfailed) {
552 if (preg_match(
'/^d/', $vals[0])) {
555 if (preg_match(
'/^l/', $vals[0])) {
564 $remotefile = $section.(preg_match(
'@[\\\/]$@', $section) ?
'' :
'/').preg_replace(
'@^[\\\/]@',
'', $file);
566 $newremotefileiso = utf8_decode($remotefile);
568 $is_directory =
ftp_isdir($conn_id, $newremotefileiso);
572 print
'<tr class="oddeven" height="18">';
575 $newsection = $section.(preg_match(
'@[\\\/]$@', $section) ?
'' :
'/').$file;
576 $newsection = preg_replace(
'@[\\\/][^\\\/]+[\\\/]\.\.$@',
'/', $newsection);
578 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?section='.urlencode($newsection).
'&numero_ftp='.$numero_ftp.
'">';
586 print
'<td class="center nowrap">';
587 if (!$is_directory && !$is_link) {
594 print
'<td class="center nowrap">';
595 print $vals[5].
' '.$vals[6].
' '.$vals[7];
598 print
'<td class="center nowrap">';
602 print
'<td class="center nowrap">';
606 print
'<td class="center nowrap">';
610 print
'<td class="right nowrap" width="64">';
613 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=delete_section&token='.
newToken().
'&numero_ftp='.$numero_ftp.
'§ion='.urlencode($section).
'&file='.urlencode($file).
'">'.
img_delete().
'</a>';
617 } elseif ($is_link) {
619 $newfile = preg_replace(
'/ ->.*/',
'', $newfile);
620 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=delete&token='.
newToken().
'&numero_ftp='.$numero_ftp.
'§ion='.urlencode($section).
'&file='.urlencode($newfile).
'">'.
img_delete().
'</a>';
622 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=download&token='.
newToken().
'&numero_ftp='.$numero_ftp.
'§ion='.urlencode($section).
'&file='.urlencode($file).
'">'.
img_picto(
'',
'file').
'</a>';
624 print
'<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.
'" name="const['.$i.
'][check]" value="1">';
626 print
'<a href="'.$_SERVER[
"PHP_SELF"].
'?action=delete&token='.
newToken().
'&numero_ftp='.$numero_ftp.
'§ion='.urlencode($section).
'&file='.urlencode($file).
'">'.
img_delete().
'</a>';
627 print
'<input type="hidden" name="const['.$i.
'][section]" value="'.$section.
'">';
628 print
'<input type="hidden" name="const['.$i.
'][file]" value="'.$file.
'">';
641 print $mesg.
'<br>'.
"\n";
656 print
'<div id="delconst" class="right">';
657 print
'<input type="submit" name="delete" class="button" value="'.$langs->trans(
"Delete").
'">';
665 while ($i <= $MAXFTP) {
666 $paramkey =
'FTP_NAME_'.$i;
668 if (!empty($conf->global->$paramkey)) {
675 print $langs->trans(
"SetupOfFTPClientModuleNotComplete");
677 print $langs->trans(
"ChooseAFTPEntryIntoMenu");
686 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
687 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
711 function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
713 global $langs, $conf;
721 if (!is_numeric($ftp_port)) {
722 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
727 $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
728 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
729 dol_syslog(
'Try to connect with ssh2_connect');
730 $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
731 } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
732 dol_syslog(
'Try to connect with ftp_ssl_connect');
733 $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
735 dol_syslog(
'Try to connect with ftp_connect');
736 $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
738 if (!empty($conn_id) || !empty($tmp_conn_id)) {
740 if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
741 dol_syslog(
'Try to authenticate with ssh2_auth_password');
742 if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
747 $newsectioniso = utf8_decode($section);
749 $conn_id = ssh2_sftp($tmp_conn_id);
751 dol_syslog(
'Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
752 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToSFTPAfterSSHAuthentication");
757 dol_syslog(
'Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
758 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
763 if (ftp_login($conn_id, $ftp_user, $ftp_password)) {
766 ftp_pasv($conn_id,
true);
770 $newsectioniso = utf8_decode($section);
771 ftp_chdir($conn_id, $newsectioniso);
773 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServerWithCredentials");
780 dol_syslog(
'FailedToConnectToFTPServer '.$ftp_server.
' '.$ftp_port, LOG_ERR);
781 $mesg = $langs->transnoentitiesnoconv(
"FailedToConnectToFTPServer", $ftp_server, $ftp_port);
786 $arrayresult = array(
'conn_id'=>$conn_id,
'ok'=>$ok,
'mesg'=>$mesg,
'curdir'=>$section,
'curdiriso'=>$newsectioniso);
800 if (@ftp_chdir($connect_id, $dir)) {
801 ftp_cdup($connect_id);