index.php 23 KB

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