jqueryFileTree.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. # jQuery File Tree Ruby Connector
  3. #
  4. # Version 1.01
  5. #
  6. # Erik Lax
  7. # http://datahack.se
  8. # 13 July 2008
  9. #
  10. # History
  11. #
  12. # 1.01 Initial Release
  13. #
  14. # Output a list of files for jQuery File Tree
  15. #
  16. #<settings>
  17. #root = "/absolute/path/"
  18. # or
  19. root = File.expand_path(".")
  20. #</settings>
  21. #<code>
  22. require "cgi"
  23. cgi = CGI.new
  24. cgi.header("type" => "text/html")
  25. dir = cgi.params["dir"].to_s
  26. puts "<ul class=\"jqueryFileTree\" style=\"display: none;\">"
  27. begin
  28. path = root + "/" + dir
  29. # chdir() to user requested dir (root + "/" + dir)
  30. Dir.chdir(File.expand_path(path).untaint);
  31. # check that our base path still begins with root path
  32. if Dir.pwd[0,root.length] == root then
  33. #loop through all directories
  34. Dir.glob("*") {
  35. |x|
  36. if not File.directory?(x.untaint) then next end
  37. puts "<li class=\"directory collapsed\"><a href=\"#\" rel=\"#{dir}#{x}/\">#{x}</a></li>";
  38. }
  39. #loop through all files
  40. Dir.glob("*") {
  41. |x|
  42. if not File.file?(x.untaint) then next end
  43. ext = File.extname(x)[1..-1]
  44. puts "<li class=\"file ext_#{ext}\"><a href=\"#\" rel=\"#{dir}#{x}\">#{x}</a></li>"
  45. }
  46. else
  47. #only happens when someone tries to go outside your root directory...
  48. puts "You are way out of your league"
  49. end
  50. rescue
  51. puts "Internal Error"
  52. end
  53. puts "</ul>"
  54. #</code>