index.php 25 KB

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