index.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. <?php
  2. /* Copyright (C) 2008-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/ftp/index.php
  21. * \ingroup ftp
  22. * \brief Main page for FTP section area
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
  28. // Load translation files required by the page
  29. $langs->loadLangs(array('companies', 'other'));
  30. // Security check
  31. if ($user->socid) {
  32. $socid = $user->socid;
  33. }
  34. $result = restrictedArea($user, 'ftp', '');
  35. // Get parameters
  36. $action = GETPOST('action', 'aZ09');
  37. $section = GETPOST('section');
  38. if (!$section) {
  39. $section = '/';
  40. }
  41. $numero_ftp = GETPOST("numero_ftp");
  42. /* if (! $numero_ftp) $numero_ftp=1; */
  43. $file = GETPOST("file");
  44. $confirm = GETPOST('confirm');
  45. $upload_dir = $conf->ftp->dir_temp;
  46. $download_dir = $conf->ftp->dir_temp;
  47. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  48. $sortfield = GETPOST('sortfield', 'aZ09comma');
  49. $sortorder = GETPOST('sortorder', 'aZ09comma');
  50. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  51. if (empty($page) || $page == -1) {
  52. $page = 0;
  53. } // If $page is not defined, or '' or -1
  54. $offset = $limit * $page;
  55. $pageprev = $page - 1;
  56. $pagenext = $page + 1;
  57. if (!$sortorder) {
  58. $sortorder = "ASC";
  59. }
  60. if (!$sortfield) {
  61. $sortfield = "label";
  62. }
  63. $s_ftp_name = 'FTP_NAME_'.$numero_ftp;
  64. $s_ftp_server = 'FTP_SERVER_'.$numero_ftp;
  65. $s_ftp_port = 'FTP_PORT_'.$numero_ftp;
  66. $s_ftp_user = 'FTP_USER_'.$numero_ftp;
  67. $s_ftp_password = 'FTP_PASSWORD_'.$numero_ftp;
  68. $s_ftp_passive = 'FTP_PASSIVE_'.$numero_ftp;
  69. $ftp_name = getDolGlobalString($s_ftp_name);
  70. $ftp_server = getDolGlobalString($s_ftp_server);
  71. $ftp_port = getDolGlobalString($s_ftp_port);
  72. if (empty($ftp_port)) {
  73. $ftp_port = 21;
  74. }
  75. $ftp_user = getDolGlobalString($s_ftp_user);
  76. $ftp_password = getDolGlobalString($s_ftp_password);
  77. $ftp_passive = getDolGlobalString($s_ftp_passive);
  78. // For result on connection
  79. $ok = 0;
  80. $conn_id = null; // FTP connection ID
  81. $mesg = '';
  82. /*
  83. * ACTIONS
  84. */
  85. // Submit file
  86. if (GETPOST("sendit") && !empty($conf->global->MAIN_UPLOAD_DOC)) {
  87. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  88. $result = $ecmdir->fetch(GETPOST("section", 'int'));
  89. if (!($result > 0)) {
  90. dol_print_error($db, $ecmdir->error);
  91. exit;
  92. }
  93. $relativepath = $ecmdir->getRelativePath();
  94. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  95. if (dol_mkdir($upload_dir) >= 0) {
  96. $resupload = dol_move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_dir."/".dol_unescapefile($_FILES['userfile']['name']), 0);
  97. if (is_numeric($resupload) && $resupload > 0) {
  98. $result = $ecmdir->changeNbOfFiles('+');
  99. } else {
  100. $langs->load("errors");
  101. if ($resupload < 0) { // Unknown error
  102. setEventMessages($langs->trans("ErrorFileNotUploaded"), null, 'errors');
  103. } elseif (preg_match('/ErrorFileIsInfectedWithAVirus/', $resupload)) {
  104. // Files infected by a virus
  105. setEventMessages($langs->trans("ErrorFileIsInfectedWithAVirus"), null, 'errors');
  106. } else // Known error
  107. {
  108. setEventMessages($langs->trans($resupload), null, 'errors');
  109. }
  110. }
  111. } else {
  112. // Transfer failure (file exceeding the limit ?)
  113. $langs->load("errors");
  114. setEventMessages($langs->trans("ErrorFailToCreateDir", $upload_dir), null, 'errors');
  115. }
  116. }
  117. // Action ajout d'un rep
  118. if ($action == 'add' && $user->rights->ftp->setup) {
  119. $ecmdir->ref = GETPOST("ref");
  120. $ecmdir->label = GETPOST("label");
  121. $ecmdir->description = GETPOST("desc");
  122. $id = $ecmdir->create($user);
  123. if ($id > 0) {
  124. header("Location: ".$_SERVER["PHP_SELF"]);
  125. exit;
  126. } else {
  127. setEventMessages($langs->trans("ErrorFailToCreateDir"), null, 'errors');
  128. $action = "create";
  129. }
  130. }
  131. // Remove 1 file
  132. if ($action == 'confirm_deletefile' && GETPOST('confirm') == 'yes') {
  133. // set up a connection or die
  134. if (!$conn_id) {
  135. $newsectioniso = utf8_decode($section);
  136. $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
  137. $conn_id = $resultarray['conn_id'];
  138. $ok = $resultarray['ok'];
  139. $mesg = $resultarray['mesg'];
  140. }
  141. if ($conn_id && $ok && !$mesg) {
  142. $newsection = $section;
  143. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  144. $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  145. }
  146. $langs->load("other");
  147. // Remote file
  148. $filename = $file;
  149. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  150. $newremotefileiso = utf8_decode($remotefile);
  151. //print "x".$newremotefileiso;
  152. dol_syslog("ftp/index.php ftp_delete ".$newremotefileiso);
  153. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  154. $result = ssh2_sftp_unlink($conn_id, $newremotefileiso);
  155. } else {
  156. $result = @ftp_delete($conn_id, $newremotefileiso);
  157. }
  158. if ($result) {
  159. setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs');
  160. } else {
  161. dol_syslog("ftp/index.php ftp_delete", LOG_ERR);
  162. setEventMessages($langs->trans("FTPFailedToRemoveFile", $file), null, 'errors');
  163. }
  164. //ftp_close($conn_id); Close later
  165. $action = '';
  166. } else {
  167. dol_print_error('', $mesg);
  168. }
  169. }
  170. // Delete several lines at once
  171. if (GETPOST("const", 'array') && GETPOST("delete") && GETPOST("delete") == $langs->trans("Delete")) {
  172. // set up a connection or die
  173. if (!$conn_id) {
  174. $newsectioniso = utf8_decode($section);
  175. $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
  176. $conn_id = $resultarray['conn_id'];
  177. $ok = $resultarray['ok'];
  178. $mesg = $resultarray['mesg'];
  179. }
  180. if ($conn_id && $ok && !$mesg) {
  181. foreach (GETPOST('const', 'array') as $const) {
  182. if ($const["check"]) { // Is checkbox checked
  183. $langs->load("other");
  184. // Remote file
  185. $file = $const["file"];
  186. $newsection = $const["section"];
  187. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  188. $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  189. }
  190. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  191. $newremotefileiso = utf8_decode($remotefile);
  192. //print "x".$newremotefileiso;
  193. dol_syslog("ftp/index.php ftp_delete n files for ".$newremotefileiso);
  194. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  195. $result = ssh2_sftp_unlink($conn_id, $newremotefileiso);
  196. } else {
  197. $result = @ftp_delete($conn_id, $newremotefileiso);
  198. }
  199. if ($result) {
  200. setEventMessages($langs->trans("FileWasRemoved", $file), null, 'mesgs');
  201. } else {
  202. dol_syslog("ftp/index.php ftp_delete n files", LOG_ERR);
  203. setEventMessages($langs->trans("FTPFailedToRemoveFile", $file), null, 'errors');
  204. }
  205. //ftp_close($conn_id); Close later
  206. $action = '';
  207. }
  208. }
  209. } else {
  210. dol_print_error('', $mesg);
  211. }
  212. }
  213. // Remove directory
  214. if ($action == 'confirm_deletesection' && $confirm == 'yes') {
  215. // set up a connection or die
  216. if (!$conn_id) {
  217. $newsectioniso = utf8_decode($section);
  218. $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
  219. $conn_id = $resultarray['conn_id'];
  220. $ok = $resultarray['ok'];
  221. $mesg = $resultarray['mesg'];
  222. }
  223. if ($conn_id && $ok && !$mesg) {
  224. $newsection = $section;
  225. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  226. $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  227. }
  228. // Remote file
  229. $filename = $file;
  230. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  231. $newremotefileiso = utf8_decode($remotefile);
  232. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  233. $result = ssh2_sftp_rmdir($conn_id, $newremotefileiso);
  234. } else {
  235. $result = @ftp_rmdir($conn_id, $newremotefileiso);
  236. }
  237. if ($result) {
  238. setEventMessages($langs->trans("DirWasRemoved", $file), null, 'mesgs');
  239. } else {
  240. setEventMessages($langs->trans("FTPFailedToRemoveDir", $file), null, 'errors');
  241. }
  242. //ftp_close($conn_id); Close later
  243. $action = '';
  244. } else {
  245. dol_print_error('', $mesg);
  246. }
  247. }
  248. // Download directory
  249. if ($action == 'download') {
  250. // set up a connection or die
  251. if (!$conn_id) {
  252. $newsectioniso = utf8_decode($section);
  253. $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $newsectioniso, $ftp_passive);
  254. $conn_id = $resultarray['conn_id'];
  255. $ok = $resultarray['ok'];
  256. $mesg = $resultarray['mesg'];
  257. }
  258. if ($conn_id && $ok && !$mesg) {
  259. // Local file
  260. $localfile = tempnam($download_dir, 'dol_');
  261. $newsection = $section;
  262. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  263. $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  264. }
  265. // Remote file
  266. $filename = $file;
  267. $remotefile = $newsection.(preg_match('@[\\\/]$@', $newsection) ? '' : '/').$file;
  268. $newremotefileiso = utf8_decode($remotefile);
  269. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  270. $result = fopen('ssh2.sftp://'.intval($conn_id).$newremotefileiso, 'r');
  271. } else {
  272. $result = ftp_get($conn_id, $localfile, $newremotefileiso, FTP_BINARY);
  273. }
  274. if ($result) {
  275. if (!empty($conf->global->MAIN_UMASK)) {
  276. @chmod($localfile, octdec($conf->global->MAIN_UMASK));
  277. }
  278. // Define mime type
  279. $type = 'application/octet-stream';
  280. if (GETPOSTISSET("type")) {
  281. $type = GETPOST("type");
  282. } else {
  283. $type = dol_mimetype($file);
  284. }
  285. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  286. $attachment = true;
  287. //if ($encoding) header('Content-Encoding: '.$encoding);
  288. if ($type) {
  289. header('Content-Type: '.$type);
  290. }
  291. if ($attachment) {
  292. header('Content-Disposition: attachment; filename="'.$filename.'"');
  293. } else {
  294. header('Content-Disposition: inline; filename="'.$filename.'"');
  295. }
  296. // Ajout directives pour resoudre bug IE
  297. header('Cache-Control: Public, must-revalidate');
  298. header('Pragma: public');
  299. readfile($localfile);
  300. ftp_close($conn_id);
  301. exit;
  302. } else {
  303. setEventMessages($langs->transnoentitiesnoconv('FailedToGetFile', $remotefile), null, 'errors');
  304. }
  305. } else {
  306. dol_print_error('', $mesg);
  307. }
  308. //ftp_close($conn_id); Close later
  309. }
  310. /*
  311. * View
  312. */
  313. llxHeader();
  314. // Add logic to shoow/hide buttons
  315. if ($conf->use_javascript_ajax) {
  316. ?>
  317. <script type="text/javascript">
  318. jQuery(document).ready(function() {
  319. jQuery("#delconst").hide();
  320. jQuery(".checkboxfordelete").click(function() {
  321. jQuery("#delconst").show();
  322. });
  323. $("#checkall").click(function() {
  324. $(".checkboxfordelete").prop('checked', true);
  325. jQuery("#delconst").show();
  326. });
  327. $("#checknone").click(function() {
  328. $(".checkboxfordelete").prop('checked', false);
  329. jQuery("#delconst").hide();
  330. });
  331. });
  332. </script>
  333. <?php
  334. }
  335. $form = new Form($db);
  336. $formfile = new FormFile($db);
  337. $userstatic = new User($db);
  338. // List
  339. print load_fiche_titre($langs->trans("FTPArea"));
  340. print $langs->trans("FTPAreaDesc")."<br>";
  341. if (!function_exists('ftp_connect')) {
  342. print $langs->trans("FTPFeatureNotSupportedByYourPHP");
  343. } else {
  344. if (!empty($ftp_server)) {
  345. // Confirm remove file
  346. if ($action == 'delete') {
  347. print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'&section='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile'), 'confirm_deletefile', '', '', 1);
  348. }
  349. // Confirmation de la suppression d'une ligne categorie
  350. if ($action == 'delete_section') {
  351. print $form->formconfirm($_SERVER["PHP_SELF"].'?numero_ftp='.$numero_ftp.'&section='.urlencode(GETPOST('section')).'&file='.urlencode(GETPOST('file')), $langs->trans('DeleteSection'), $langs->trans('ConfirmDeleteSection', $ecmdir->label), 'confirm_deletesection', '', '', 1);
  352. }
  353. print $langs->trans("Server").': <b>'.$ftp_server.'</b><br>';
  354. print $langs->trans("Port").': <b>'.$ftp_port.'</b> '.($ftp_passive ? "(Passive)" : "(Active)").'<br>';
  355. print $langs->trans("User").': <b>'.$ftp_user.'</b><br>';
  356. print $langs->trans("FTPs (FTP over SSH)").': <b>'.yn($conf->global->FTP_CONNECT_WITH_SSL).'</b><br>';
  357. print $langs->trans("SFTP (FTP as a subsytem of SSH)").': <b>'.yn($conf->global->FTP_CONNECT_WITH_SFTP).'</b><br>';
  358. print $langs->trans("Directory").': ';
  359. $sectionarray = preg_split('|[\/]|', $section);
  360. // For /
  361. $newsection = '/';
  362. print '<a href="'.$_SERVER["PHP_SELF"].'?action=refreshmanual&numero_ftp='.$numero_ftp.($newsection ? '&section='.urlencode($newsection) : '').'">';
  363. print '/';
  364. print '</a> ';
  365. // For other directories
  366. $i = 0;
  367. foreach ($sectionarray as $val) {
  368. if (empty($val)) {
  369. continue; // Discard first and last entry that should be empty as section start/end with /
  370. }
  371. if ($i > 0) {
  372. print ' / ';
  373. $newsection .= '/';
  374. }
  375. $newsection .= $val;
  376. print '<a href="'.$_SERVER["PHP_SELF"].'?action=refreshmanual&numero_ftp='.$numero_ftp.($newsection ? '&section='.urlencode($newsection) : '').'">';
  377. print $val;
  378. print '</a>';
  379. $i++;
  380. }
  381. print '<br>';
  382. print "<br>\n";
  383. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  384. print '<input type="hidden" name="numero_ftp" value="'.$numero_ftp.'">';
  385. print '<input type="hidden" name="token" value="'.newToken().'">';
  386. // Construit liste des repertoires
  387. print '<table width="100%" class="noborder">'."\n";
  388. print '<tr class="liste_titre">'."\n";
  389. print '<td class="liste_titre left">'.$langs->trans("Content").'</td>'."\n";
  390. print '<td class="liste_titre center">'.$langs->trans("Size").'</td>'."\n";
  391. print '<td class="liste_titre center">'.$langs->trans("Date").'</td>'."\n";
  392. print '<td class="liste_titre center">'.$langs->trans("Owner").'</td>'."\n";
  393. print '<td class="liste_titre center">'.$langs->trans("Group").'</td>'."\n";
  394. print '<td class="liste_titre center">'.$langs->trans("Permissions").'</td>'."\n";
  395. print '<td class="liste_titre nowrap right">';
  396. if ($conf->use_javascript_ajax) {
  397. print '<a href="#" id="checkall">'.$langs->trans("All").'</a> / <a href="#" id="checknone">'.$langs->trans("None").'</a> ';
  398. }
  399. print '<a href="'.$_SERVER["PHP_SELF"].'?action=refreshmanual&numero_ftp='.$numero_ftp.($section ? '&section='.urlencode($section) : '').'">'.img_picto($langs->trans("Refresh"), 'refresh').'</a>&nbsp;';
  400. print '</td>'."\n";
  401. print '</tr>'."\n";
  402. // set up a connection or die
  403. if (empty($conn_id)) {
  404. $resultarray = dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive);
  405. $conn_id = $resultarray['conn_id'];
  406. $ok = $resultarray['ok'];
  407. $mesg = $resultarray['mesg'];
  408. }
  409. if ($ok) {
  410. //$type = ftp_systype($conn_id);
  411. $newsection = $section;
  412. $newsectioniso = utf8_decode($section);
  413. //$newsection='/home';
  414. // List content of directory ($newsection = '/', '/home', ...)
  415. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  416. if ($newsection == '/') {
  417. //$newsection = '/./';
  418. $newsection = ssh2_sftp_realpath($conn_id, ".").'/./'; // workaround for bug https://bugs.php.net/bug.php?id=64169
  419. }
  420. //$newsection='/';
  421. //$dirHandle = opendir("ssh2.sftp://$conn_id".$newsection);
  422. //$dirHandle = opendir("ssh2.sftp://".intval($conn_id).ssh2_sftp_realpath($conn_id, ".").'/./');
  423. $contents = scandir('ssh2.sftp://'.intval($conn_id).$newsection);
  424. $buff = array();
  425. foreach ($contents as $i => $key) {
  426. $buff[$i] = "---------- - root root 1234 Aug 01 2000 ".$key;
  427. }
  428. //$i = 0;
  429. //$handle = opendir('ssh2.sftp://'.intval($conn_id).$newsection);
  430. //$buff=array();
  431. //while (false !== ($file = readdir($handle))) {
  432. // if (substr("$file", 0, 1) != "."){
  433. // if (is_dir($file)) {
  434. // $buff[$i]="d--------- - root root 1234 Aug 01 2000 ".$file;
  435. // } else {
  436. // $buff[$i]="---------- - root root 1234 Aug 01 2000 ".$file;
  437. // }
  438. // }
  439. // $i++;
  440. //}
  441. } else {
  442. $buff = ftp_rawlist($conn_id, $newsectioniso);
  443. $contents = ftp_nlist($conn_id, $newsectioniso); // Sometimes rawlist fails but never nlist
  444. //var_dump($contents);
  445. //var_dump($buff);
  446. }
  447. $nboflines = count($contents);
  448. $rawlisthasfailed = false;
  449. $i = 0;
  450. $nbofentries = 0;
  451. while ($i < $nboflines && $i < 1000) {
  452. $vals = preg_split('@ +@', utf8_encode($buff[$i]), 9);
  453. //$vals=preg_split('@ +@','drwxr-xr-x 2 root root 4096 Aug 30 2008 backup_apollon1',9);
  454. //var_dump($vals);
  455. $file = $vals[8];
  456. if (empty($file)) {
  457. $rawlisthasfailed = true;
  458. $file = utf8_encode($contents[$i]);
  459. }
  460. if ($file == '.' || ($file == '..' && $section == '/')) {
  461. $i++;
  462. continue;
  463. }
  464. // Is it a directory ?
  465. $is_directory = 0;
  466. $is_link = 0;
  467. if ($file == '..') {
  468. $is_directory = 1;
  469. } elseif (!$rawlisthasfailed) {
  470. if (preg_match('/^d/', $vals[0])) {
  471. $is_directory = 1;
  472. }
  473. if (preg_match('/^l/', $vals[0])) {
  474. $is_link = 1;
  475. }
  476. } else {
  477. // Remote file
  478. $filename = $file;
  479. //print "section=".$section.' file='.$file.'X';
  480. //print preg_match('@[\/]$@','aaa/').'Y';
  481. //print preg_match('@[\\\/]$@',"aaa\\").'Y';
  482. $remotefile = $section.(preg_match('@[\\\/]$@', $section) ? '' : '/').preg_replace('@^[\\\/]@', '', $file);
  483. //print 'A'.$remotefile.'A';
  484. $newremotefileiso = utf8_decode($remotefile);
  485. //print 'Z'.$newremotefileiso.'Z';
  486. $is_directory = ftp_isdir($conn_id, $newremotefileiso);
  487. }
  488. print '<tr class="oddeven" height="18">';
  489. // Name
  490. print '<td>';
  491. $newsection = $section.(preg_match('@[\\\/]$@', $section) ? '' : '/').$file;
  492. $newsection = preg_replace('@[\\\/][^\\\/]+[\\\/]\.\.$@', '/', $newsection); // Change aaa/xxx/.. to new aaa
  493. if ($is_directory) {
  494. print '<a href="'.$_SERVER["PHP_SELF"].'?section='.urlencode($newsection).'&numero_ftp='.$numero_ftp.'">';
  495. }
  496. print dol_escape_htmltag($file);
  497. if ($is_directory) {
  498. print '</a>';
  499. }
  500. print '</td>';
  501. // Size
  502. print '<td class="center nowrap">';
  503. if (!$is_directory && !$is_link) {
  504. print $vals[4];
  505. } else {
  506. print '&nbsp;';
  507. }
  508. print '</td>';
  509. // Date
  510. print '<td class="center nowrap">';
  511. print $vals[5].' '.$vals[6].' '.$vals[7];
  512. print '</td>';
  513. // User
  514. print '<td class="center nowrap">';
  515. print $vals[2];
  516. print '</td>';
  517. // Group
  518. print '<td class="center nowrap">';
  519. print $vals[3];
  520. print '</td>';
  521. // Permissions
  522. print '<td class="center nowrap">';
  523. print $vals[0];
  524. print '</td>';
  525. // Action
  526. print '<td class="right nowrap" width="64">';
  527. if ($is_directory) {
  528. if ($file != '..') {
  529. print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete_section&token='.newToken().'&numero_ftp='.$numero_ftp.'&section='.urlencode($section).'&file='.urlencode($file).'">'.img_delete().'</a>';
  530. } else {
  531. print '&nbsp;';
  532. }
  533. } elseif ($is_link) {
  534. $newfile = $file;
  535. $newfile = preg_replace('/ ->.*/', '', $newfile);
  536. print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&numero_ftp='.$numero_ftp.'&section='.urlencode($section).'&file='.urlencode($newfile).'">'.img_delete().'</a>';
  537. } else {
  538. print '<a href="'.$_SERVER["PHP_SELF"].'?action=download&token='.newToken().'&numero_ftp='.$numero_ftp.'&section='.urlencode($section).'&file='.urlencode($file).'">'.img_picto('', 'file').'</a>';
  539. print ' &nbsp; ';
  540. print '<input type="checkbox" class="flat checkboxfordelete" id="check_'.$i.'" name="const['.$i.'][check]" value="1">';
  541. print ' &nbsp; ';
  542. print '<a href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&numero_ftp='.$numero_ftp.'&section='.urlencode($section).'&file='.urlencode($file).'">'.img_delete().'</a>';
  543. print '<input type="hidden" name="const['.$i.'][section]" value="'.$section.'">';
  544. print '<input type="hidden" name="const['.$i.'][file]" value="'.$file.'">';
  545. }
  546. print '</td>';
  547. print '</tr>'."\n";
  548. $i++;
  549. $nbofentries++;
  550. }
  551. }
  552. print "</table>";
  553. if (!$ok) {
  554. print $mesg.'<br>'."\n";
  555. setEventMessages($mesg, null, 'errors');
  556. }
  557. // Actions
  558. /*
  559. if ($user->rights->ftp->write && ! empty($section))
  560. {
  561. $formfile->form_attach_new_file(DOL_URL_ROOT.'/ftp/index.php','',0,$section,1);
  562. }
  563. else print '&nbsp;';
  564. */
  565. print '<br>';
  566. print '<div id="delconst" class="right">';
  567. print '<input type="submit" name="delete" class="button" value="'.$langs->trans("Delete").'">';
  568. print '</div>';
  569. print "</form>";
  570. } else {
  571. $foundsetup = false;
  572. $MAXFTP = 20;
  573. $i = 1;
  574. while ($i <= $MAXFTP) {
  575. $paramkey = 'FTP_NAME_'.$i;
  576. //print $paramkey;
  577. if (!empty($conf->global->$paramkey)) {
  578. $foundsetup = true;
  579. break;
  580. }
  581. $i++;
  582. }
  583. if (!$foundsetup) {
  584. print $langs->trans("SetupOfFTPClientModuleNotComplete");
  585. } else {
  586. print $langs->trans("ChooseAFTPEntryIntoMenu");
  587. }
  588. }
  589. }
  590. print '<br>';
  591. // Close FTP connection
  592. if ($conn_id) {
  593. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  594. } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
  595. ftp_close($conn_id);
  596. } else {
  597. ftp_close($conn_id);
  598. }
  599. }
  600. // End of page
  601. llxFooter();
  602. $db->close();
  603. /**
  604. * Connect to FTP server
  605. *
  606. * @param string $ftp_server Server name
  607. * @param string $ftp_port Server port
  608. * @param string $ftp_user FTP user
  609. * @param string $ftp_password FTP password
  610. * @param string $section Directory
  611. * @param integer $ftp_passive Use a passive mode
  612. * @return int <0 if OK, >0 if KO
  613. */
  614. function dol_ftp_connect($ftp_server, $ftp_port, $ftp_user, $ftp_password, $section, $ftp_passive = 0)
  615. {
  616. global $langs, $conf;
  617. $ok = 1;
  618. $error = 0;
  619. $conn_id = null;
  620. $newsectioniso = '';
  621. $mesg="";
  622. if (!is_numeric($ftp_port)) {
  623. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
  624. $ok = 0;
  625. }
  626. if ($ok) {
  627. $connecttimeout = (empty($conf->global->FTP_CONNECT_TIMEOUT) ? 40 : $conf->global->FTP_CONNECT_TIMEOUT);
  628. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  629. dol_syslog('Try to connect with ssh2_connect');
  630. $tmp_conn_id = ssh2_connect($ftp_server, $ftp_port);
  631. } elseif (!empty($conf->global->FTP_CONNECT_WITH_SSL)) {
  632. dol_syslog('Try to connect with ftp_ssl_connect');
  633. $conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $connecttimeout);
  634. } else {
  635. dol_syslog('Try to connect with ftp_connect');
  636. $conn_id = ftp_connect($ftp_server, $ftp_port, $connecttimeout);
  637. }
  638. if (!empty($conn_id) || !empty($tmp_conn_id)) {
  639. if ($ftp_user) {
  640. if (!empty($conf->global->FTP_CONNECT_WITH_SFTP)) {
  641. dol_syslog('Try to authenticate with ssh2_auth_password');
  642. if (ssh2_auth_password($tmp_conn_id, $ftp_user, $ftp_password)) {
  643. // Turn on passive mode transfers (must be after a successful login
  644. //if ($ftp_passive) ftp_pasv($conn_id, true);
  645. // Change the dir
  646. $newsectioniso = utf8_decode($section);
  647. //ftp_chdir($conn_id, $newsectioniso);
  648. $conn_id = ssh2_sftp($tmp_conn_id);
  649. if (!$conn_id) {
  650. dol_syslog('Failed to connect to SFTP after sssh authentication', LOG_DEBUG);
  651. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToSFTPAfterSSHAuthentication");
  652. $ok = 0;
  653. $error++;
  654. }
  655. } else {
  656. dol_syslog('Failed to connect to FTP with login '.$ftp_user, LOG_DEBUG);
  657. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
  658. $ok = 0;
  659. $error++;
  660. }
  661. } else {
  662. if (ftp_login($conn_id, $ftp_user, $ftp_password)) {
  663. // Turn on passive mode transfers (must be after a successful login
  664. if ($ftp_passive) {
  665. ftp_pasv($conn_id, true);
  666. }
  667. // Change the dir
  668. $newsectioniso = utf8_decode($section);
  669. ftp_chdir($conn_id, $newsectioniso);
  670. } else {
  671. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServerWithCredentials");
  672. $ok = 0;
  673. $error++;
  674. }
  675. }
  676. }
  677. } else {
  678. dol_syslog('FailedToConnectToFTPServer '.$ftp_server.' '.$ftp_port, LOG_ERR);
  679. $mesg = $langs->transnoentitiesnoconv("FailedToConnectToFTPServer", $ftp_server, $ftp_port);
  680. $ok = 0;
  681. }
  682. }
  683. $arrayresult = array('conn_id'=>$conn_id, 'ok'=>$ok, 'mesg'=>$mesg, 'curdir'=>$section, 'curdiriso'=>$newsectioniso);
  684. return $arrayresult;
  685. }
  686. /**
  687. * Tell if an entry is a FTP directory
  688. *
  689. * @param resource $connect_id Connection handler
  690. * @param string $dir Directory
  691. * @return int 1=directory, 0=not a directory
  692. */
  693. function ftp_isdir($connect_id, $dir)
  694. {
  695. if (@ftp_chdir($connect_id, $dir)) {
  696. ftp_cdup($connect_id);
  697. return 1;
  698. } else {
  699. return 0;
  700. }
  701. }