jqueryFileTree.rb 1.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#
# jQuery File Tree Ruby Connector
#
# Version 1.01
#
# Erik Lax
# http://datahack.se
# 13 July 2008
#
# History
#
# 1.01 Initial Release
#
# Output a list of files for jQuery File Tree
#

#<settings>
#root = "/absolute/path/"
# or
root = File.expand_path(".")
#</settings>

#<code>
require "cgi"
cgi = CGI.new
cgi.header("type" => "text/html")
dir = cgi.params["dir"].to_s

puts "<ul class=\"jqueryFileTree\" style=\"display: none;\">"
begin
	path = root + "/" + dir 

	# chdir() to user requested dir (root + "/" + dir) 
	Dir.chdir(File.expand_path(path).untaint);
	
	# check that our base path still begins with root path
	if Dir.pwd[0,root.length] == root then

		#loop through all directories
		Dir.glob("*") {
			|x|
			if not File.directory?(x.untaint) then next end 
			puts "<li class=\"directory collapsed\"><a href=\"#\" rel=\"#{dir}#{x}/\">#{x}</a></li>";
		}

		#loop through all files
		Dir.glob("*") {
			|x|
			if not File.file?(x.untaint) then next end 
			ext = File.extname(x)[1..-1]
			puts "<li class=\"file ext_#{ext}\"><a href=\"#\" rel=\"#{dir}#{x}\">#{x}</a></li>"
		}
	else
		#only happens when someone tries to go outside your root directory...
		puts "You are way out of your league"
	end 
rescue 
	puts "Internal Error"
end
puts "</ul>"
#</code>