index.php 23 KB

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