rename DiogenesIcons to Diogenes_Icons
[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
23
24 /** This plugin allows you to insert a directory listing with icons
25 * and modification date.
26 *
27 * To make use of this plugin, insert {FileList} in your page where
28 * the file list should appear.
29 */
30 class FileList extends Diogenes_Plugin_Filter {
31 /** Plugin name */
32 var $name = "FileList";
33
34 /** Plugin description */
35 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.";
36
37 /** Plugin parameters */
38 var $params = array('dirbase' => "", 'urlbase' => "", 'match' => "");
39
40
41 /** Prepare the output for a single file of the list.
42 *
43 * @param $file
44 * @param $title
45 * @param $url
46 */
47 function list_file($file,$title,$url="") {
48 if (empty($url)) $url = $file;
49 global $globals;
50
51 /* get modification date / time */
52 $modified = date ("d M Y H:m:s", filemtime($file));
53
54 /* get file icon */
55 $icon = $globals->icons->get_mime_icon($file);
56
57 /* calculate file size */
58 $size = filesize($file);
59 if ($size < 1000)
60 $size = "$size B";
61 elseif ($size < 1000000)
62 $size = floor($size/1000)." kB";
63 else
64 $size = floor($size/1000000)." MB";
65
66 /* figure out description */
67 $show = 1;
68 if (preg_match("/(i386\.changes|\.dsc)$/",$file))
69 $show = 0;
70 elseif (preg_match("/\.tar\.gz$/",$file))
71 $desc = "source tarball";
72 elseif (preg_match("/_(all|i386)\.deb$/",$file,$matches))
73 {
74 $type = $matches[1];
75 $desc = "Debian package";
76 if ($type == "all")
77 $desc .= " [platform independant]";
78 else
79 $desc .= " [$type specific]";
80 }
81 elseif (preg_match("/\.diff\.gz$/",$file))
82 $desc = "Debian diff";
83 else $desc = "&nbsp;";
84
85 /* display the link */
86 if ($show)
87 return "<tr><td><img alt=\"file\" class=\"fileicon\" src=\"$icon\" /><a href=\"$url\">$title</a></td><td><small>$modified</small></td><td>$size</td><td>$desc</td></tr>\n";
88 }
89
90
91 /** Show an instance of the FileList plugin.
92 *
93 * @param $args
94 */
95 function show($args)
96 {
97 global $page;
98 $bbarel = $page->barrel;
99
100 // process arguments
101 $params = array();
102 foreach($this->params as $key => $val) {
103 $params[$key] = isset($args[$key]) ? $args[$key] : $this->params[$key];
104 }
105
106 //print_r($params);
107 if (empty($params['dirbase'])) {
108 $params['dirbase'] = $bbarel->spool->spoolPath($page->curpage->props['PID']);
109 }
110
111 // process parameters
112 $output = '';
113 $dir = $params['dirbase'];
114 if (is_dir($dir) && ($dh = opendir($dir))) {
115 $output .=
116 '<table class="light">
117 <tr>
118 <th>'.__("file").'</th>
119 <th>'.__("date").'</th>
120 <th>'.__("size").'</th>
121 <th>'.__("description").'</th>
122 </tr>';
123
124 /* get the matching files */
125 while (($fname = readdir($dh)) !== false) {
126 if ( is_file("$dir/$fname")
127 && preg_match('/^'.$params['match'].'/',$fname) )
128 $filelist[] = $fname;
129 }
130 closedir($dh);
131
132 /* order and display */
133 if (is_array($filelist)) {
134 rsort($filelist);
135 while(list ($key,$val) = each($filelist)) {
136 $output .= $this->list_file("$dir/$val",$val,$params['urlbase'].$val);
137 }
138 } else {
139 $output .= '<tr><td colspan="4"><i>'.__("no files").'</i></td></tr>';
140 }
141 $output .= '</table>';
142 }
143 return $output;
144 }
145 }
146
147 ?>