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