diff --git a/roles/copr/backend/files/lighttpd/lighttpd.conf b/roles/copr/backend/files/lighttpd/lighttpd.conf index 60c50d82ee..a235bc5b76 100644 --- a/roles/copr/backend/files/lighttpd/lighttpd.conf +++ b/roles/copr/backend/files/lighttpd/lighttpd.conf @@ -80,8 +80,17 @@ server.modules = ( "mod_access", "mod_setenv", "mod_redirect", + "mod_indexfile", + "mod_cgi" ) +cgi.assign = ( ".pl" => "/usr/bin/perl", + ".cgi" => "/usr/bin/perl", + ".rb" => "/usr/bin/ruby", + ".erb" => "/usr/bin/eruby", + ".py" => "/usr/bin/python", + ".php" => "/usr/bin/php" ) + ## ####################################################################### @@ -302,8 +311,8 @@ server.max-connections = 1024 ## index-file.names = ( "index.php", "index.rb", "index.html", ## "index.htm", "default.htm" ) ## -index-file.names += ( - "index.xhtml", "index.html", "index.htm", "default.htm", "index.php" +index-file.names = ( + "/dir-generator.php" ) ## diff --git a/roles/copr/backend/files/lighttpd/lighttpd_dev.conf b/roles/copr/backend/files/lighttpd/lighttpd_dev.conf index 2c19f67fbc..ba3d6b7482 100644 --- a/roles/copr/backend/files/lighttpd/lighttpd_dev.conf +++ b/roles/copr/backend/files/lighttpd/lighttpd_dev.conf @@ -80,8 +80,17 @@ server.modules = ( "mod_access", "mod_setenv", "mod_redirect", + "mod_indexfile", + "mod_cgi" ) +cgi.assign = ( ".pl" => "/usr/bin/perl", + ".cgi" => "/usr/bin/perl", + ".rb" => "/usr/bin/ruby", + ".erb" => "/usr/bin/eruby", + ".py" => "/usr/bin/python", + ".php" => "/usr/bin/php" ) + ## ####################################################################### @@ -302,8 +311,8 @@ server.max-connections = 1024 ## index-file.names = ( "index.php", "index.rb", "index.html", ## "index.htm", "default.htm" ) ## -index-file.names += ( - "index.xhtml", "index.html", "index.htm", "default.htm", "index.php" +index-file.names = ( + "/dir-generator.php" ) ## diff --git a/roles/copr/backend/tasks/main.yml b/roles/copr/backend/tasks/main.yml index 1214cc4525..67df17ef79 100644 --- a/roles/copr/backend/tasks/main.yml +++ b/roles/copr/backend/tasks/main.yml @@ -22,6 +22,7 @@ - python-glanceclient - python-neutronclient - python-keystoneclient + - php-cli - name: make copr dirs file: state=directory path={{ item }} @@ -98,6 +99,9 @@ notify: - restart lighttpd +- name: install custom lighttpd template for directory listings + template: src="lighttpd/dir-generator.php.j2" dest="/var/lib/copr/public_html/dir-generator.php" owner=copr group=copr mode=0755 + - name: start webserver service: state=started enabled=yes name=lighttpd diff --git a/roles/copr/backend/templates/lighttpd/dir-generator.php.j2 b/roles/copr/backend/templates/lighttpd/dir-generator.php.j2 new file mode 100755 index 0000000000..53f2964ddb --- /dev/null +++ b/roles/copr/backend/templates/lighttpd/dir-generator.php.j2 @@ -0,0 +1,449 @@ +" . $path . " is not a valid path."); +} + + +// +// Get the size in bytes of a folder +// +function foldersize($path) { + $size = 0; + if($handle = @opendir($path)){ + while(($file = readdir($handle)) !== false) { + if(is_file($path."/".$file)){ + $size += filesize($path."/".$file); + } + + if(is_dir($path."/".$file)){ + if($file != "." && $file != "..") { + $size += foldersize($path."/".$file); + } + } + } + } + + return $size; +} + + +// +// This function returns the file size of a specified $file. +// +function format_bytes($size, $precision=0) { + $sizes = array('B', 'B', 'E', 'P', 'B', 'G', 'M', 'K', 'B'); + $total = count($sizes); + + while($total-- && $size > 1024) $size /= 1024; + return sprintf('%.'.$precision.'f', $size).$sizes[$total]; +} + + +// +// This function returns the mime type of $file. +// +function get_file_type($file) { + global $image_types, $movie_types; + + $pos = strrpos($file, "."); + if ($pos === false) { + return "Text File"; + } + + $ext = rtrim(substr($file, $pos+1), "~"); + if(in_array($ext, $image_types)) { + $type = "Image File"; + + } elseif(in_array($ext, $movie_types)) { + $type = "Video File"; + + } elseif(in_array($ext, $archive_types)) { + $type = "Compressed Archive"; + + } elseif(in_array($ext, $document_types)) { + $type = "Type Document"; + + } elseif(in_array($ext, $font_types)) { + $type = "Type Font"; + + } else { + $type = "File"; + } + + return(strtoupper($ext) . " " . $type); +} + + + +// Print the heading stuff +$vpath = ($path != "./")?$path:""; +print " + + Index of /" .$vpath. " + + + +

Index of /" . $vpath ."

+
+ "; + + + + +// Get all of the folders and files. +$folderlist = array(); +$filelist = array(); +if($handle = @opendir($path)) { + while(($item = readdir($handle)) !== false) { + if(is_dir($path.'/'.$item) and $item != '.' and $item != '..') { + if( $show_hidden_files == "false" ) { + if(substr($item, 0, 1) == "." or substr($item, -1) == "~") { + continue; + } + } + $folderlist[] = array( + 'name' => $item, + 'size' => (($calculate_folder_size)?foldersize($path.'/'.$item):0), + 'modtime'=> filemtime($path.'/'.$item), + 'file_type' => "Directory" + ); + } + + elseif(is_file($path.'/'.$item)) { + if( $show_hidden_files == "false" ) { + if(substr($item, 0, 1) == "." or substr($item, -1) == "~") { + continue; + } + } + $filelist[] = array( + 'name'=> $item, + 'size'=> filesize($path.'/'.$item), + 'modtime'=> filemtime($path.'/'.$item), + 'file_type' => get_file_type($path.'/'.$item) + ); + } + } + fclose($handle); +} + +// Show sort methods +print ""; + +$header = array(); +$header['name'] = "Name"; +$header['modtime'] = "Last Modified"; +$header['size'] = "Size"; +$header['file_type'] = "Type"; + +foreach($header as $key=>$item) { + if($_GET['sort'] == $key) { + print ""; + } else { + print ""; + } +} +print ""; + + + +// Parent directory link +if($path != "./") { + print ""; + print ""; + print ""; + print ""; +} + + + +// Print folder information +foreach($folderlist as $folder) { + print ""; + print ""; + print ""; + print ""; +} + + + + +// Print file information +foreach($filelist as $file) { + print ""; + print ""; + print ""; + print ""; +} + + +$frontend_baseurl = '{{ frontend_base_url }}'; +$path = explode('/', $_SERVER['REQUEST_URI']); +if (count($path) >= 7) { + $owner = preg_replace('/^(@|%40)(.*)$/', 'g/$2', $path[2]); + $project = $path[3]; + $buildid = ltrim(explode('-', $path[5])[0], '0'); + $frontend_url = implode('/', [$frontend_baseurl, 'coprs', $owner, $project, 'build', $buildid]); + $frontend_url_text = 'Go to COPR frontend build'; +} else if (count($path) >= 5) { + $owner = preg_replace('/^(@|%40)(.*)$/', 'g/$2', $path[2]); + $project = $path[3]; + $frontend_url = implode('/', [$frontend_baseurl, 'coprs', $owner, $project]); + $frontend_url_text = 'Go to COPR frontend project'; +} else { + $frontend_url = $frontend_baseurl; + $frontend_url_text = 'Go to COPR frontend'; +} + +// Print ending stuff +print " +
".$item."".$item."
../  Directory
" .htmlentities($folder['name']). "/" . date('Y-M-d H:m:s', $folder['modtime']) . "" . (($calculate_folder_size)?format_bytes($folder['size'], 2):'--') . " " . $folder['file_type'] . "
" .htmlentities($file['name']). "" . date('Y-M-d H:m:s', $file['modtime']) . "" . format_bytes($file['size'],2) . " " . $file['file_type'] . "
+
+
Lighttpd Enhanced Directory Listing Script
+
".$frontend_url_text."
+
". $_ENV['SERVER_SOFTWARE'] . "
+ + + + "; + + +/* -------------------------------------------------------------------------------- *\ + I hope you enjoyed my take on the enhanced directory listing script! + If you have any questions, feel free to contact me. + + Regards, + Evan Fosmark < me@evanfosmark.com > +\* -------------------------------------------------------------------------------- */ +?>