browse.inc.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. // -------------------------------------------------------------------------------
  3. // | net2ftp: a web based FTP client |
  4. // | Copyright (c) 2003-2013 by David Gartner |
  5. // | |
  6. // | This program is free software; you can redistribute it and/or |
  7. // | modify it under the terms of the GNU General Public License |
  8. // | as published by the Free Software Foundation; either version 2 |
  9. // | of the License, or (at your option) any later version. |
  10. // | |
  11. // -------------------------------------------------------------------------------
  12. // **************************************************************************************
  13. // **************************************************************************************
  14. // ** **
  15. // ** **
  16. function ftp_getlist($conn_id, $directory) {
  17. // --------------
  18. // This function connects to the FTP server and returns an array with a list of directories and files.
  19. // One row per directory or file, with rows from index 1 to n
  20. //
  21. // Step 1: send ftp_rawlist request to the FTP server; this returns a string
  22. // Step 2: parse that string and get a first array ($templist)
  23. // Step 3: move the rows to another array, to index 1 to n ($list)
  24. //
  25. // This function is used in all functions which process directories recursively.
  26. // --------------
  27. // -------------------------------------------------------------------------
  28. // Global variables
  29. // -------------------------------------------------------------------------
  30. global $net2ftp_globals, $net2ftp_settings;
  31. // -------------------------------------------------------------------------
  32. // Initialization
  33. // -------------------------------------------------------------------------
  34. $warnings = "";
  35. // -------------------------------------------------------------------------
  36. // Step 1: Chdir to the directory and get the current directory
  37. // -------------------------------------------------------------------------
  38. // ----------------------------------------------
  39. // Step 1a - Directory is "/"
  40. // Chdir to the directory because otherwise the ftp_rawlist does not work on some FTP servers
  41. // ----------------------------------------------
  42. if ($directory == "/") {
  43. $result1a = ftp_chdir($conn_id, $directory);
  44. }
  45. // ----------------------------------------------
  46. // Step 1b - Directory is ""
  47. // If the directory is "" then the user can be directed to his home directory
  48. // We don't know which directory it is, so we request it from the FTP server
  49. // ----------------------------------------------
  50. elseif ($directory == "") {
  51. $result1b = @ftp_chdir($conn_id, $directory);
  52. $directory = ftp_pwd($conn_id);
  53. }
  54. // ----------------------------------------------
  55. // Step 1c - Directory is not "/" or ""
  56. // ----------------------------------------------
  57. else {
  58. // 1c1 - Replace \' by \\' to be able to delete directories with names containing \'
  59. $directory1 = str_replace("\'", "\\\'", $directory);
  60. // 1c2 - Chdir to the directory
  61. // This is to check if the directory exists, but also because otherwise
  62. // the ftp_rawlist does not work on some FTP servers.
  63. $result1c = ftp_chdir($conn_id, $directory1);
  64. // 1c3 - If the first ftp_chdir returns false, try a second time without the leading /
  65. // Some Windows FTP servers do not work when you use a leading /
  66. if ($result1c == false) {
  67. $directory2 = stripDirectory($directory1);
  68. $result2 = ftp_chdir($conn_id, $directory2);
  69. // 1c3 - If the second ftp_chdir also does not work:
  70. // For the Browse screen ==> go to the user's root directory
  71. // For all other screens ==> return error
  72. if ($result2 == false) {
  73. if ($net2ftp_globals["state"] == "browse") {
  74. $rootdirectory = getRootdirectory();
  75. // User's root directory is different from the current directory, so switch to it
  76. if ($directory != $rootdirectory) {
  77. $warnings .= __("The directory <b>%1\$s</b> does not exist or could not be selected, so the directory <b>%2\$s</b> is shown instead.", $directory, $rootdirectory);
  78. $directory = $rootdirectory;
  79. $result3 = ftp_chdir($conn_id, $directory);
  80. }
  81. // The current directory *is* the user's root directory!
  82. // We cannot display any other directory (like /), so print an error message.
  83. else {
  84. $errormessage = __("Your root directory <b>%1\$s</b> does not exist or could not be selected.", $directory);
  85. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  86. }
  87. }
  88. else {
  89. $errormessage = __("The directory <b>%1\$s</b> could not be selected - you may not have sufficient rights to view this directory, or it may not exist.", $directory);
  90. setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
  91. }
  92. } // end if result2
  93. } // end if result1
  94. } // end if / or "" or else
  95. // -------------------------------------------------------------------------
  96. // Step 2 - Get list of directories and files
  97. // The -a option is used to show the hidden files as well on some FTP servers
  98. // Some servers do not return anything when using -a, so in that case try again without the -a option
  99. // -------------------------------------------------------------------------
  100. $rawlist = ftp_rawlist($conn_id, "-a");
  101. if (sizeof($rawlist) <= 1) { $rawlist = ftp_rawlist($conn_id, ""); }
  102. // -------------------------------------------------------------------------
  103. // Step 3 - Parse the raw list
  104. // -------------------------------------------------------------------------
  105. // ----------------------------------------------
  106. // Initialize variables
  107. // ----------------------------------------------
  108. $list["directories"] = array();
  109. $list["files"] = array();
  110. $list["symlinks"] = array();
  111. $list["unrecognized"] = array();
  112. $directory_index = 1;
  113. $file_index = 1;
  114. $symlink_index = 1;
  115. $unrecognized_index = 1;
  116. $list["stats"]["directories"]["total_number"] = 0;
  117. $list["stats"]["directories"]["total_size"] = 0;
  118. $list["stats"]["directories"]["total_skipped"] = 0;
  119. $list["stats"]["files"]["total_number"] = 0;
  120. $list["stats"]["files"]["total_size"] = 0;
  121. $list["stats"]["files"]["total_skipped"] = 0;
  122. $list["stats"]["symlinks"]["total_number"] = 0;
  123. $list["stats"]["symlinks"]["total_size"] = 0;
  124. $list["stats"]["symlinks"]["total_skipped"] = 0;
  125. $list["stats"]["unrecognized"]["total_number"] = 0;
  126. $list["stats"]["unrecognized"]["total_size"] = 0;
  127. $list["stats"]["unrecognized"]["total_skipped"] = 0;
  128. // ----------------------------------------------
  129. // Loop over the raw list lines
  130. // ----------------------------------------------
  131. $nr_entries_banned_keyword = 0;
  132. $nr_entries_too_big = 0;
  133. for($i=0; $i<sizeof($rawlist); $i++) {
  134. // ----------------------------------------------
  135. // Scan each line
  136. // ----------------------------------------------
  137. $listline = ftp_scanline($directory, $rawlist[$i]);
  138. // If $listline is empty (e.g. if it contained ".."), continue to the next line
  139. if ($listline == "") { continue; }
  140. // Encode the name for HTML and Javascript
  141. if (isset($listline["dirfilename"])) {
  142. $listline["dirfilename_html"] = htmlEncode2($listline["dirfilename"]);
  143. $listline["dirfilename_url"] = urlEncode2($listline["dirfilename"]);
  144. $listline["dirfilename_js"] = javascriptEncode2($listline["dirfilename"]);
  145. }
  146. // Check if the filename contains a forbidden keyword
  147. // If it does, then this line will not be selectable on the Browse screen
  148. // Note: even if "selectable" is set to true here, it can still be set to false just below if the filesize is too big
  149. if (checkAuthorizedName($listline["dirfilename"]) == true) { $listline["selectable"] = "ok"; }
  150. else { $listline["selectable"] = "banned_keyword"; $nr_entries_banned_keyword++; }
  151. // Check if the filesize is bigger than the maximum authorized filesize
  152. if ($listline["dirorfile"] == "-" && isset($listline["size"]) && is_numeric($listline["size"])) {
  153. if ($listline["selectable"] == "ok" && $listline["size"] > $net2ftp_settings["max_filesize"]) { $listline["selectable"] = "too_big"; $nr_entries_too_big++; }
  154. }
  155. // Form the new directory filename and encode it too
  156. if ($listline["dirorfile"] == "d") {
  157. $listline["newdir"] = glueDirectories($directory, $listline["dirfilename"]);
  158. $listline["newdir_html"] = htmlEncode2($listline["newdir"]);
  159. $listline["newdir_url"] = urlEncode2($listline["newdir"]);
  160. $listline["newdir_js"] = javascriptEncode2($listline["newdir"]);
  161. }
  162. // ----------------------------------------------
  163. // Depending on if the line contained a directory/file/symlink/unrecognized
  164. // row, store the result in different variables
  165. // ----------------------------------------------
  166. if ($listline["dirorfile"] == "d") {
  167. $list["directories"][$directory_index] = $listline;
  168. $directory_index++;
  169. if (isset($listline["size"]) && is_numeric($listline["size"])) {
  170. $list["stats"]["directories"]["total_size"] = $list["stats"]["directories"]["total_size"] + $listline["size"];
  171. }
  172. else {
  173. $list["stats"]["directories"]["total_skipped"] = $list["stats"]["directories"]["total_skipped"] + 1;
  174. }
  175. } // end if
  176. elseif ($listline["dirorfile"] == "-") {
  177. $list["files"][$file_index] = $listline;
  178. $file_index++;
  179. if (isset($listline["size"]) && is_numeric($listline["size"])) {
  180. $list["stats"]["files"]["total_size"] = $list["stats"]["files"]["total_size"] + $listline["size"];
  181. }
  182. else {
  183. $list["stats"]["files"]["total_skipped"] = $list["stats"]["files"]["total_skipped"] + 1;
  184. }
  185. } // end elseif
  186. elseif ($listline["dirorfile"] == "l") {
  187. $list["symlinks"][$symlink_index] = $listline;
  188. $symlink_index++;
  189. } // end elseif
  190. elseif ($listline["dirorfile"] == "u") {
  191. $list["unrecognized"][$unrecognized_index] = $listline;
  192. $unrecognized_index++;
  193. } // end elseif
  194. } // end for
  195. // Print a warning message if some directories, files or symlinks contain a banned keyword or if a file is
  196. // too big to be downloaded
  197. if ($nr_entries_banned_keyword > 0) {
  198. $warnings .= __("Entries which contain banned keywords can't be managed using net2ftp. This is to avoid Paypal or Ebay scams from being uploaded through net2ftp.");
  199. $warnings .= "<br />\n";
  200. }
  201. if ($nr_entries_too_big > 0) {
  202. $warnings .= __("Files which are too big can't be downloaded, uploaded, copied, moved, searched, zipped, unzipped, viewed or edited; they can only be renamed, chmodded or deleted.");
  203. $warnings .= "<br />\n";
  204. }
  205. // Store the warnings and new directory in $list["stats"]
  206. if (isset($warnings) == true) { $list["stats"]["warnings"] = $warnings; }
  207. else { $list["stats"]["warnings"] = ""; }
  208. $list["stats"]["newdirectory"] = $directory;
  209. // Store the statistics
  210. $list["stats"]["directories"]["total_size_formated"] = formatFilesize($list["stats"]["directories"]["total_size"]);
  211. $list["stats"]["files"]["total_size_formated"] = formatFilesize($list["stats"]["files"]["total_size"]);
  212. $list["stats"]["directories"]["total_number"] = $directory_index - 1;
  213. $list["stats"]["files"]["total_number"] = $file_index - 1;
  214. $list["stats"]["symlinks"]["total_number"] = $symlink_index - 1;
  215. $list["stats"]["unrecognized"]["total_number"] = $unrecognized_index - 1;
  216. // Put everything together in $list["all"]
  217. $list["all"] = $list["directories"] + $list["files"] + $list["symlinks"] + $list["unrecognized"];
  218. // -------------------------------------------------------------------------
  219. // Step 4 - Return the result
  220. // -------------------------------------------------------------------------
  221. return $list;
  222. // -------------------------------------------------------------------------
  223. // Some documentation:
  224. // 1 - Some FTP servers return a total on the first line
  225. // 2 - Some FTP servers return . and .. in their list of directories
  226. // ftp_scanline does not return those entries.
  227. // -------------------------------------------------------------------------
  228. // 1 - After doing some tests on different public FTP servers, it appears that
  229. // they reply differently to the ftp_rawlist request:
  230. // - some FTP servers, like ftp.belnet.be, start with a line summarizing how
  231. // many subdirectories and files there are in the current directory. The
  232. // real list of subdirectories and files starts on the second line.
  233. // [0] => total 15
  234. // [1] => drwxr-xr-x 11 BELNET Archive 512 Feb 6 2000 BELNET
  235. // [2] => drwxr-xr-x 2 BELNET Archive 512 Oct 29 2001 FVD-SFI
  236. // - some other FTP servers, like ftp.redhat.com/pub, start directly with the
  237. // list of subdirectories and files.
  238. // [0] => drwxr-xr-x 9 ftp ftp 4096 Jan 11 06:34 contrib
  239. // [1] => drwxr-xr-x 13 ftp ftp 4096 Jan 29 21:59 redhat
  240. // [2] => drwxrwsr-x 6 ftp ftp 4096 Jun 05 2002 up2date
  241. // 2 - Some FTP servers return "." and ".." as directories. These fake entries
  242. // have to be eliminated!
  243. // They would cause infinite loops in the copy/move/delete functions.
  244. // [0] => drwxr-xr-x 5 80 www 512 Apr 10 09:39 .
  245. // [1] => drwxr-xr-x 16 80 www 512 Apr 9 08:51 ..
  246. // [2] => -rw-r--r-- 1 80 www 5647 Apr 9 08:12 _CHANGES_v0.5
  247. // [3] => -rw-r--r-- 1 80 www 1239 Apr 9 08:12 _CREDITS_v0.5
  248. } // End function ftp_getlist
  249. // ** **
  250. // ** **
  251. // **************************************************************************************
  252. // **************************************************************************************
  253. // **************************************************************************************
  254. // **************************************************************************************
  255. // ** **
  256. // ** **
  257. function ftp_scanline($directory, $rawlistline) {
  258. // --------------
  259. // This function scans an ftp_rawlist line string and returns its parts (directory/file, name, size,...) using preg_match()
  260. //
  261. // !!! Documentation about preg_match and FTP server's outputs are now at the end of the function !!!
  262. // --------------
  263. // -------------------------------------------------------------------------
  264. // Global variables
  265. // -------------------------------------------------------------------------
  266. global $net2ftp_settings, $net2ftp_messages;
  267. // -------------------------------------------------------------------------
  268. // Scanning:
  269. // 1. first scan with strict rules
  270. // 2. if that does not match, scan with less strict rules
  271. // 3. if that does not match, scan with rules for specific FTP servers (AS400)
  272. // 4. and if that does not match, return the raw line
  273. // -------------------------------------------------------------------------
  274. // ----------------------------------------------
  275. // 1. Strict rules
  276. // ----------------------------------------------
  277. if (preg_match("/([-dl])([rwxsStT-]{9})[ ]+([0-9]+)[ ]+([^ ]+)[ ]+(.+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ]+(.*)/", $rawlistline, $regs) == true) {
  278. // permissions number owner group size month day year/hour filename
  279. $listline["scanrule"] = "rule-1";
  280. $listline["dirorfile"] = "$regs[1]"; // Directory ==> d, File ==> -
  281. $listline["dirfilename"] = "$regs[9]"; // Filename
  282. $listline["size"] = "$regs[6]"; // Size
  283. $listline["owner"] = "$regs[4]"; // Owner
  284. $listline["group"] = trim($regs[5]); // Group
  285. $listline["permissions"] = "$regs[2]"; // Permissions
  286. $listline["mtime"] = "$regs[7] $regs[8]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  287. }
  288. // ----------------------------------------------
  289. // 2. Less strict rules
  290. // ----------------------------------------------
  291. elseif (preg_match("/([-dl])([rwxsStT-]{9})[ ]+(.*)[ ]+([a-zA-Z0-9 ]+)[ ]+([0-9:]+)[ ]+(.*)/", $rawlistline, $regs) == true) {
  292. // permissions number/owner/group/size
  293. // month-day year/hour filename
  294. $listline["scanrule"] = "rule-2";
  295. $listline["dirorfile"] = "$regs[1]"; // Directory ==> d, File ==> -
  296. $listline["dirfilename"] = "$regs[6]"; // Filename
  297. $listline["size"] = "$regs[3]"; // Number/Owner/Group/Size
  298. $listline["permissions"] = "$regs[2]"; // Permissions
  299. $listline["mtime"] = "$regs[4] $regs[5]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  300. }
  301. // ----------------------------------------------
  302. // 3. Specific FTP server rules
  303. // ----------------------------------------------
  304. // ---------------
  305. // 3.1 Windows
  306. // ---------------
  307. elseif (preg_match("/([0-9\\/-]+)[ ]+([0-9:AMP]+)[ ]+([0-9]*|<DIR>)[ ]+(.*)/", $rawlistline, $regs) == true) {
  308. // date time size filename
  309. $listline["scanrule"] = "rule-3.1";
  310. if ($regs[3] == "<DIR>") { $listline["size"] = ""; }
  311. else { $listline["size"] = "$regs[3]"; } // Size
  312. $listline["dirfilename"] = "$regs[4]"; // Filename
  313. $listline["owner"] = ""; // Owner
  314. $listline["group"] = ""; // Group
  315. $listline["permissions"] = ""; // Permissions
  316. $listline["mtime"] = "$regs[1] $regs[2]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  317. if ($listline["size"] != "") { $listline["dirorfile"] = "-"; }
  318. else { $listline["dirorfile"] = "d"; }
  319. }
  320. // ---------------
  321. // 3.2 Netware
  322. // Thanks to Danny!
  323. // ---------------
  324. elseif (preg_match("/([-]|[d])[ ]+(.{10})[ ]+([^ ]+)[ ]+([0-9]*)[ ]+([a-zA-Z]*[ ]+[0-9]*)[ ]+([0-9:]*)[ ]+(.*)/", $rawlistline, $regs) == true) {
  325. // dir/file perms owner size month day hour filename
  326. $listline["scanrule"] = "rule-3.2";
  327. $listline["dirorfile"] = "$regs[1]"; // Directory ==> d, File ==> -
  328. $listline["dirfilename"] = "$regs[7]"; // Filename
  329. $listline["size"] = "$regs[4]"; // Size
  330. $listline["owner"] = "$regs[3]"; // Owner
  331. $listline["group"] = ""; // Group
  332. $listline["permissions"] = "$regs[2]"; // Permissions
  333. $listline["mtime"] = "$regs[5] $regs6"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  334. }
  335. // ---------------
  336. // 3.3 AS400
  337. // ---------------
  338. elseif (preg_match("/([a-zA-Z0-9_-]+)[ ]+([0-9]+)[ ]+([0-9\\/-]+)[ ]+([0-9:]+)[ ]+([a-zA-Z0-9_ -\*]+)[ \\/]+([^\\/]+)/", $rawlistline, $regs) == true) {
  339. // owner size date time type filename
  340. if ($regs[5] != "*STMF") { $directory_or_file = "d"; }
  341. elseif ($regs[5] == "*STMF") { $directory_or_file = "-"; }
  342. $listline["scanrule"] = "rule-3.3";
  343. $listline["dirorfile"] = "$directory_or_file";// Directory ==> d, File ==> -
  344. $listline["dirfilename"] = "$regs[6]"; // Filename
  345. $listline["size"] = "$regs[2]"; // Size
  346. $listline["owner"] = "$regs[1]"; // Owner
  347. $listline["group"] = ""; // Group
  348. $listline["permissions"] = ""; // Permissions
  349. $listline["mtime"] = "$regs[3] $regs[4]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  350. }
  351. // ---------------
  352. // 3.4 Titan
  353. // Owner, group are modified compared to rule 1
  354. // TO DO: integrate this rule in rule 1 itself
  355. // ---------------
  356. elseif (preg_match("/([-dl])([rwxsStT-]{9})[ ]+([0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([a-zA-Z0-9]+)[ ]+([0-9]+)[ ]+([a-zA-Z]+[ ]+[0-9]+)[ ]+([0-9:]+)[ ](.*)/", $rawlistline, $regs) == true) {
  357. // dir/file permissions number owner group size month date time file
  358. $listline["scanrule"] = "rule-3.4";
  359. $listline["dirorfile"] = "$regs[1]"; // Directory ==> d, File ==> -
  360. $listline["dirfilename"] = "$regs[9]"; // Filename
  361. $listline["size"] = "$regs[6]"; // Size
  362. $listline["owner"] = "$regs[4]"; // Owner
  363. $listline["group"] = "$regs[5]"; // Group
  364. $listline["permissions"] = "$regs[2]"; // Permissions
  365. $listline["mtime"] = "$regs[7] $regs[8]"; // Mtime -- format depends on what FTP server returns (year, month, day, hour, minutes... see above)
  366. }
  367. // ----------------------------------------------
  368. // 4. If nothing matchs, return the raw line
  369. // ----------------------------------------------
  370. else {
  371. $listline["scanrule"] = "rule-4";
  372. $listline["dirorfile"] = "u";
  373. $listline["dirfilename"] = $rawlistline;
  374. }
  375. // -------------------------------------------------------------------------
  376. // Remove the . and .. entries
  377. // Remove the total line that some servers return
  378. // -------------------------------------------------------------------------
  379. if ($listline["dirfilename"] == "." || $listline["dirfilename"] == "..") { return ""; }
  380. elseif (substr($rawlistline,0,5) == "total") { return ""; }
  381. // -------------------------------------------------------------------------
  382. // And finally... return the nice list!
  383. // -------------------------------------------------------------------------
  384. return $listline;
  385. // -------------------------------------------------------------------------
  386. // Documentation
  387. // -------------------------------------------------------------------------
  388. /*
  389. [email protected]
  390. 11-Jan-2002 11:51
  391. ^ Start of String
  392. $ End of string
  393. n* Zero or more of 'n'
  394. n+ One or more of 'n'
  395. n? A possible 'n'
  396. n{2} Exactly two of 'n'
  397. n{2,} At least 2 or more of 'n'
  398. n{2,4} From 2 to 4 of 'n'
  399. () Parenthesis to group expressions
  400. (n|a) Either 'n' or 'a'
  401. . Any single character
  402. [1-6] A number between 1 and 6
  403. [c-h] A lower case character between c and h
  404. [D-M] An upper case character between D and M
  405. [^a-z] Absence of lower case a to z
  406. [_a-zA-Z] An underscore or any letter of the alphabet
  407. ^.{2}[a-z]{1,2}_?[0-9]*([1-6]|[a-f])[^1-9]{2}a+$
  408. A string beginning with any two characters
  409. Followed by either 1 or 2 lower case alphabet letters
  410. Followed by an optional underscore
  411. Followed by zero or more digits
  412. Followed by either a number between 1 and 6 or a character between a and f (Lowercase)
  413. Followed by a two characters which are not digits between 1 and 9
  414. Followed by one or more n characters at the end of a string
  415. // $regs can contain a maximum of 10 elements !! (regs[0] to regs[9])
  416. // To specify what you really want back from ereg, use (). Only what is within () will be returned. See below.
  417. */
  418. // ----------------------------------------------
  419. // Sample FTP server's output
  420. // ----------------------------------------------
  421. // ---------------
  422. // 1. "Standard" FTP servers output
  423. // ---------------
  424. // ftp.redhat.com
  425. //drwxr-xr-x 6 0 0 4096 Aug 21 2001 pub (one or more spaces between entries)
  426. //
  427. // ftp.suse.com
  428. //drwxr-xr-x 2 root root 4096 Jan 9 2001 bin
  429. //-rw-r--r-- 1 suse susewww 664 May 23 16:24 README.txt
  430. //
  431. // ftp.belnet.be
  432. //-rw-r--r-- 1 BELNET Mirror 162 Aug 6 2000 HEADER.html
  433. //drwxr-xr-x 53 BELNET Archive 2048 Nov 13 12:03 mirror
  434. //
  435. // ftp.microsoft.com
  436. //-r-xr-xr-x 1 owner group 0 Nov 27 2000 dirmap.htm
  437. //
  438. // ftp.sourceforge.net
  439. //-rw-r--r-- 1 root staff 29136068 Apr 21 22:07 ls-lR.gz
  440. //
  441. // ftp.nec.com
  442. //dr-xr-xr-x 12 other 512 Apr 3 2002 pub
  443. //
  444. // ftp.intel.com
  445. //drwxr-sr-x 11 root ftp 4096 Sep 23 16:36 pub
  446. // ---------------
  447. // 3.1 Windows
  448. // ---------------
  449. //06-10-04 07:56PM 8175 garantie.html
  450. //04-09-04 04:27PM <DIR> images
  451. //05-25-04 09:18AM 9505 index.html
  452. // ---------------
  453. // 3.2 Netware
  454. // ---------------
  455. // total 0
  456. // - [RWCEAFMS] USER 12 Mar 08 10:48 check.txt
  457. // d [RWCEAFMS] USER 512 Mar 18 17:55 latest
  458. // ---------------
  459. // 3.3 AS400
  460. // ---------------
  461. // RGOVINDAN 932 03/29/01 14:59:53 *STMF /cert.txt
  462. // QSYS 77824 12/17/01 15:33:14 *DIR /QOpenSys/
  463. // QDOC 24576 12/31/69 20:00:00 *FLR /QDLS/
  464. // QSYS 12832768 04/14/03 16:47:25 *LIB /QSYS.LIB/
  465. // QDFTOWN 2147483647 12/31/69 20:00:00 *DDIR /QOPT/
  466. // QSYS 2144 04/12/03 12:49:00 *DDIR /QFileSvr.400/
  467. // QDFTOWN 1136 04/12/03 12:49:01 *DDIR /QNTC/
  468. // ---------------
  469. // 3.4 Titan FTP server
  470. // ---------------
  471. // total 6
  472. // drwxrwx--- 1 owner group 512 Apr 19 11:44 .
  473. // drwxrwx--- 1 owner group 512 Apr 19 11:44 ..
  474. // -rw-rw---- 1 owner group 13171 Apr 15 13:50 default.asp
  475. // drwxrwx--- 1 owner group 512 Apr 19 11:44 forum
  476. // drwxrwx--- 1 owner group 512 Apr 15 13:32 images
  477. // -rw-rw---- 1 owner group 764 Apr 15 11:07 styles.css
  478. } // End function ftp_scanline
  479. // ** **
  480. // ** **
  481. // **************************************************************************************
  482. // **************************************************************************************
  483. // **************************************************************************************
  484. // **************************************************************************************
  485. // ** **
  486. // ** **
  487. function ftp2http($directory, $list_files, $htmltags) {
  488. // --------------
  489. // This function calculates the HTTP URL based on the FTP URL
  490. //
  491. // Given the FTP server (ftp.name.com),
  492. // the directory and file (/directory/file.php)
  493. // It has to return
  494. // http://www.name.com/directory/file.php
  495. //
  496. // $htmltags indicates whether the url should be returned enclosed in HTML tags or not
  497. //
  498. // For efficiency reasons, this function processes a list of files
  499. // --------------
  500. // -------------------------------------------------------------------------
  501. // Global variables
  502. // -------------------------------------------------------------------------
  503. global $net2ftp_globals;
  504. // -------------------------------------------------------------------------
  505. // If no list is supplied, return ""
  506. // -------------------------------------------------------------------------
  507. if (sizeof($list_files) == 0) { return ""; }
  508. // -------------------------------------------------------------------------
  509. // Prepare the variables
  510. // -------------------------------------------------------------------------
  511. // Directory
  512. if ($directory == "/") { $directory = ""; }
  513. // Convert single quotes from ' to &#039;
  514. if ($htmltags == "no") { $directory = javascriptEncode2($directory); }
  515. else { $directory = urlEncode2($directory); }
  516. // Filenames
  517. if ($htmltags == "no") { $encoding = "dirfilename_js"; }
  518. else { $encoding = "dirfilename_url"; }
  519. // Username
  520. if ($htmltags == "no") { $username = javascriptEncode2($net2ftp_globals["username"]); }
  521. else { $username = htmlEncode2($net2ftp_globals["username"]); }
  522. // -------------------------------------------------------------------------
  523. // "ftp.t35.com" -----> "http://username" (username = username.t35.com)
  524. // "ftp.t35.net" -----> "http://username" (username = username.t35.net)
  525. // -------------------------------------------------------------------------
  526. if (strpos($net2ftp_globals["ftpserver"], "ftp.t35") !== false) {
  527. for ($i=1; $i<=sizeof($list_files); $i++) {
  528. $URL = "http://" . $username . $directory . "/" . $list_files[$i][$encoding];
  529. if ($htmltags == "no") { $list_links[$i] = $URL; }
  530. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  531. } // end for
  532. }
  533. // -------------------------------------------------------------------------
  534. // "ftp-www.earthlink.net/webdocs/directory" -----> "http://home.earthlink.net/~username/directory"
  535. // -------------------------------------------------------------------------
  536. elseif (strpos($net2ftp_globals["ftpserver"], "ftp-www.earthlink.net") !== false) {
  537. if (strlen($directory) < 8) {
  538. for ($i=1; $i<=sizeof($list_files); $i++) {
  539. if ($htmltags == "no") { $list_links[$i] = "javascript:alert('" . __("This file is not accessible from the web") . "');"; }
  540. else { $list_links[$i] = "<a title=\"" . __("This file is not accessible from the web") . "\" onclick=\"alert('" . __("This file is not accessible from the web") . "');\">" . $list_files[$i][$encoding] . "</a>"; }
  541. } // end for
  542. }
  543. else {
  544. // Transform directory from /webdocs/dir to /dir --> remove the first 4 characters
  545. $directory = substr($directory, 8);
  546. for ($i=1; $i<=sizeof($list_files); $i++) {
  547. $URL = "http://home.earthlink.net/~" . $username . $directory . "/" . $list_files[$i][$encoding];
  548. if ($htmltags == "no") { $list_links[$i] = $URL; }
  549. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  550. } // end for
  551. } // end if else strlen
  552. }
  553. // -------------------------------------------------------------------------
  554. // "ftpperso.free.fr" -----> "http://username.free.fr"
  555. // -------------------------------------------------------------------------
  556. elseif (strpos($net2ftp_globals["ftpserver"], "ftpperso.free.fr") !== false) {
  557. for ($i=1; $i<=sizeof($list_files); $i++) {
  558. $URL = "http://" . $username . ".free.fr" . $directory . "/" . $list_files[$i][$encoding];
  559. if ($htmltags == "no") { $list_links[$i] = $URL; }
  560. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  561. } // end for
  562. }
  563. // -------------------------------------------------------------------------
  564. // "ftp.membres.lycos.fr" -----> "http://membres.lycos.fr/username"
  565. // -------------------------------------------------------------------------
  566. elseif (strpos($net2ftp_globals["ftpserver"],"ftp.membres.lycos.fr") !== false) {
  567. for ($i=1; $i<=sizeof($list_files); $i++) {
  568. $URL = "http://membres.lycos.fr/" . $username . $directory . "/" . $list_files[$i][$encoding];
  569. if ($htmltags == "no") { $list_links[$i] = $URL; }
  570. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  571. } // end for
  572. }
  573. // -------------------------------------------------------------------------
  574. // "home.planetinternet.be" -----> "http://home.planetinternet.be/~username"
  575. // -------------------------------------------------------------------------
  576. elseif (strpos($net2ftp_globals["ftpserver"], "home.planetinternet.be") !== false) {
  577. for ($i=1; $i<=sizeof($list_files); $i++) {
  578. $URL = "http://home.planetinternet.be/~" . $username . $directory . "/" . $list_files[$i][$encoding];
  579. if ($htmltags == "no") { $list_links[$i] = $URL; }
  580. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  581. } // end for
  582. }
  583. // -------------------------------------------------------------------------
  584. // "home.planet.nl" -----> "http://home.planet.nl/~username"
  585. // -------------------------------------------------------------------------
  586. elseif (strpos($net2ftp_globals["ftpserver"], "home.planet.nl") !== false) {
  587. for ($i=1; $i<=sizeof($list_files); $i++) {
  588. $URL = "http://home.planet.nl/~" . $username . $directory . "/" . $list_files[$i][$encoding];
  589. if ($htmltags == "no") { $list_links[$i] = $URL; }
  590. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  591. } // end for
  592. }
  593. // -------------------------------------------------------------------------
  594. // "users.skynet.be" -----> "http://users.skynet.be/username"
  595. // -------------------------------------------------------------------------
  596. elseif (strpos($net2ftp_globals["ftpserver"], "users.skynet.be") !== false) {
  597. for ($i=1; $i<=sizeof($list_files); $i++) {
  598. $URL = "http://users.skynet.be/" . $username . $directory . "/" . $list_files[$i][$encoding];
  599. if ($htmltags == "no") { $list_links[$i] = $URL; }
  600. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  601. } // end for
  602. }
  603. // -------------------------------------------------------------------------
  604. // "ftp.tripod.com" -----> "http://username.tripod.com"
  605. // -------------------------------------------------------------------------
  606. elseif (strpos($net2ftp_globals["ftpserver"], "ftp.tripod.com") !== false) {
  607. for ($i=1; $i<=sizeof($list_files); $i++) {
  608. $URL = "http://" . $username . ".tripod.com" . $directory . "/" . $list_files[$i][$encoding];
  609. if ($htmltags == "no") { $list_links[$i] = $URL; }
  610. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  611. } // end for
  612. }
  613. // -------------------------------------------------------------------------
  614. // "ftp.wanadoo.es" -----> "http://perso.wanadoo.es/username"
  615. // -------------------------------------------------------------------------
  616. elseif (strpos($net2ftp_globals["ftpserver"], "ftp.wanadoo.es") !== false) {
  617. for ($i=1; $i<=sizeof($list_files); $i++) {
  618. $URL = "http://perso.wanadoo.es/" . $username . $directory . "/" . $list_files[$i][$encoding];
  619. if ($htmltags == "no") { $list_links[$i] = $URL; }
  620. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  621. } // end for
  622. }
  623. // -------------------------------------------------------------------------
  624. // "perso-ftp.wanadoo.fr" -----> "http://perso.wanadoo.fr/username"
  625. // -------------------------------------------------------------------------
  626. elseif (strpos($net2ftp_globals["ftpserver"], "perso-ftp.wanadoo.fr") !== false) {
  627. for ($i=1; $i<=sizeof($list_files); $i++) {
  628. $URL = "http://perso.wanadoo.fr/" . $username . $directory . "/" . $list_files[$i][$encoding];
  629. if ($htmltags == "no") { $list_links[$i] = $URL; }
  630. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  631. } // end for
  632. }
  633. // -------------------------------------------------------------------------
  634. // "home.wanadoo.nl" -----> "http://home.wanadoo.nl/username"
  635. // -------------------------------------------------------------------------
  636. elseif (strpos($net2ftp_globals["ftpserver"], "home.wanadoo.nl") !== false) {
  637. for ($i=1; $i<=sizeof($list_files); $i++) {
  638. $URL = "http://home.wanadoo.nl/" . $username . $directory . "/" . $list_files[$i][$encoding];
  639. if ($htmltags == "no") { $list_links[$i] = $URL; }
  640. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  641. } // end for
  642. }
  643. // -------------------------------------------------------------------------
  644. // wanadoo uk
  645. // "uploads.webspace.freeserve.net" -----> "http://www.username.freeserve.co.uk"
  646. // -------------------------------------------------------------------------
  647. elseif (strpos($net2ftp_globals["ftpserver"], "uploads.webspace.freeserve.net") !== false) {
  648. for ($i=1; $i<=sizeof($list_files); $i++) {
  649. $URL = "http://www." . $username . ".freeserve.co.uk" . $directory . "/" . $list_files[$i][$encoding];
  650. if ($htmltags == "no") { $list_links[$i] = $URL; }
  651. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  652. } // end for
  653. }
  654. // -------------------------------------------------------------------------
  655. // "ftp.xs4all.nl/WWW/directory" -----> "http://www.xs4all.nl/~username/directory"
  656. // -------------------------------------------------------------------------
  657. elseif (strpos($net2ftp_globals["ftpserver"], "ftp.xs4all.nl") !== false) {
  658. if (strlen($directory) < 4) {
  659. for ($i=1; $i<=sizeof($list_files); $i++) {
  660. if ($htmltags == "no") { $list_links[$i] = "javascript:alert('" . __("This file is not accessible from the web") . "');"; }
  661. else { $list_links[$i] = "<a title=\"" . __("This file is not accessible from the web") . "\" onclick=\"alert('" . __("This file is not accessible from the web") . "');\">" . $list_files[$i][$encoding] . "</a>"; }
  662. } // end for
  663. }
  664. else {
  665. // Transform directory from /WWW/dir to /dir --> remove the first 4 characters
  666. $directory = substr($directory, 4);
  667. for ($i=1; $i<=sizeof($list_files); $i++) {
  668. $URL = "http://www.xs4all.nl/~" . $username . $directory . "/" . $list_files[$i][$encoding];
  669. if ($htmltags == "no") { $list_links[$i] = $URL; }
  670. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  671. } // end for
  672. }
  673. }
  674. // -------------------------------------------------------------------------
  675. // "ftp.server.com/directory/file" -----> "http://www.server.com/directory/file"
  676. // -------------------------------------------------------------------------
  677. elseif (preg_match("/ftp.(.+)(.{2,4})/", $net2ftp_globals["ftpserver"], $regs)) {
  678. // Check if the FTP directory contains "htdocs", "httpdocs" or "public_html"
  679. // If it does, the HTTP directory root starts from there on
  680. // Example: /srv/www/htdocs/directory1 ==> /directory1
  681. $specialdirectories[1] = "htdocs";
  682. $specialdirectories[2] = "httpdocs";
  683. $specialdirectories[3] = "public_html";
  684. for ($i=1; $i<=sizeof($specialdirectories); $i++) {
  685. $pos = strpos($directory, $specialdirectories[$i]);
  686. if ($pos !== false) {
  687. $directory = substr($directory, $pos + strlen($specialdirectories[$i]));
  688. break;
  689. }
  690. }
  691. // Calculate all the URLs on the Browse screen
  692. for ($i=1; $i<=sizeof($list_files); $i++) {
  693. $URL = "http://www." . $regs[1] . $regs[2] . $directory . "/" . $list_files[$i][$encoding];
  694. if ($htmltags == "no") { $list_links[$i] = $URL; }
  695. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  696. } // end for
  697. }
  698. // -------------------------------------------------------------------------
  699. // "http://192.168.0.1/directory/file" can be determined using "192.168.0.1/directory/file":
  700. // -------------------------------------------------------------------------
  701. else {
  702. // Check if the FTP directory contains "htdocs", "httpdocs" or "public_html"
  703. // If it does, the HTTP directory root starts from there on
  704. // Example: /srv/www/htdocs/directory1 ==> /directory1
  705. $specialdirectories[1] = "htdocs";
  706. $specialdirectories[2] = "httpdocs";
  707. $specialdirectories[3] = "public_html";
  708. for ($i=1; $i<=sizeof($specialdirectories); $i++) {
  709. $pos = strpos($directory, $specialdirectories[$i]);
  710. if ($pos !== false) {
  711. $directory = substr($directory, $pos + strlen($specialdirectories[$i]));
  712. break;
  713. }
  714. }
  715. // Calculate all the URLs on the Browse screen
  716. for ($i=1; $i<=sizeof($list_files); $i++) {
  717. $URL = "http://" . $net2ftp_globals["ftpserver"]. $directory . "/" . @(string)$list_files[$i][$encoding];
  718. if ($htmltags == "no") { $list_links[$i] = $URL; }
  719. else { $list_links[$i] = "<a href=\"" . $URL . "\" target=\"_blank\" title=\"" . __("Execute %1\$s in a new window", $list_files[$i][$encoding]) . "\">" . $list_files[$i][$encoding] . "</a>"; }
  720. } // end for
  721. }
  722. return $list_links;
  723. } // end function ftp2http
  724. // ** **
  725. // ** **
  726. // **************************************************************************************
  727. // **************************************************************************************
  728. ?>