Set HTTP status code 500 if database connection fails. Should avoid pages
[diogenes.git] / plugins / FileList.php
1 <?php
2 /*
3 * Copyright (C) 2003-2005 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 require_once 'Plugin/Filter.php';
22 require_once 'diogenes.icons.inc.php';
23
24
25 /** This plugin allows you to insert a directory listing with icons
26 * and modification date.
27 *
28 * To make use of this plugin, insert {FileList} in your page where
29 * the file list should appear.
30 */
31 class FileList extends Diogenes_Plugin_Filter {
32 /** Plugin name */
33 var $name = "FileList";
34
35 /** Plugin description */
36 var $description = "This plugin allows you to insert a directory listing with icons and modification date. To make use of this plugin, insert <b>{FileList}</b> in your page where the file list should appear.";
37
38 /** Plugin parameters */
39 var $params = array('dirbase' => "", 'urlbase' => "", 'match' => "");
40
41
42 /** Prepare the output for a single file of the list.
43 *
44 * @param $file
45 * @param $title
46 * @param $url
47 */
48 function list_file($file,$title,$url="") {
49 if (empty($url)) $url = $file;
50 global $globals;
51
52 /* get modification date / time */
53 $modified = date ("d M Y H:m:s", filemtime($file));
54
55 /* get file icon */
56 $icon = $globals->icons->get_mime_icon($file);
57
58 /* calculate file size */
59 $size = filesize($file);
60 if ($size < 1000)
61 $size = "$size B";
62 elseif ($size < 1000000)
63 $size = floor($size/1000)." kB";
64 else
65 $size = floor($size/1000000)." MB";
66
67 /* figure out description */
68 $show = 1;
69 if (preg_match("/(i386\.changes|\.dsc)$/",$file))
70 $show = 0;
71 elseif (preg_match("/\.tar\.gz$/",$file))
72 $desc = "source tarball";
73 elseif (preg_match("/_(all|i386)\.deb$/",$file,$matches))
74 {
75 $type = $matches[1];
76 $desc = "Debian package";
77 if ($type == "all")
78 $desc .= " [platform independant]";
79 else
80 $desc .= " [$type specific]";
81 }
82 elseif (preg_match("/\.diff\.gz$/",$file))
83 $desc = "Debian diff";
84 else $desc = "&nbsp;";
85
86 /* display the link */
87 if ($show)
88 return "<tr><td><img class=\"fileicon\" src=\"$icon\" /><a href=\"$url\">$title</a></td><td><small>$modified</small></td><td>$size</td><td>$desc</td></tr>\n";
89 }
90
91
92 /** Show an instance of the FileList plugin.
93 *
94 * @param $args
95 */
96 function show($args)
97 {
98 global $page;
99 $bbarel = $page->barrel;
100
101 // process arguments
102 $params = array();
103 foreach($this->params as $key => $val) {
104 $params[$key] = isset($args[$key]) ? $args[$key] : $this->params[$key];
105 }
106
107 //print_r($params);
108 if (empty($params['dirbase'])) {
109 $params['dirbase'] = $bbarel->spool->spoolPath($page->curpage->props['PID']);
110 }
111
112 // process parameters
113 $output = '';
114 $dir = $params['dirbase'];
115 if (is_dir($dir) && ($dh = opendir($dir))) {
116 $output .=
117 '<table class="light">
118 <tr>
119 <th>'.__("file").'</th>
120 <th>'.__("date").'</th>
121 <th>'.__("size").'</th>
122 <th>'.__("description").'</th>
123 </tr>';
124
125 /* get the matching files */
126 while (($fname = readdir($dh)) !== false) {
127 if ( is_file("$dir/$fname")
128 && preg_match('/^'.$params['match'].'/',$fname) )
129 $filelist[] = $fname;
130 }
131 closedir($dh);
132
133 /* order and display */
134 if (is_array($filelist)) {
135 rsort($filelist);
136 while(list ($key,$val) = each($filelist)) {
137 $output .= $this->list_file("$dir/$val",$val,$params['urlbase'].$val);
138 }
139 } else {
140 $output .= '<tr><td colspan="4"><i>'.__("no files").'</i></td></tr>';
141 }
142 $output .= '</table>';
143 }
144 return $output;
145 }
146 }
147
148 ?>