]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FileInfo.php
Add quiet argument. Allow local files if owner == ADMIN and page == locked.
[SourceForge/phpwiki.git] / lib / plugin / FileInfo.php
1 <?php // -*-php-*-
2 rcs_id('$Id: FileInfo.php,v 1.6 2007-01-04 16:42:31 rurban Exp $');
3 /*
4  Copyright 2005 $ThePhpWikiProgrammingTeam
5  
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * This plugin displays the version, date, size, perms of an uploaded file.
25  * Only files relative and below to the uploads path can be handled.
26  *
27  * Usage:
28  *   <?plugin FileInfo file=Upload:setup.exe display=version,date ?>
29  *   <?plugin FileInfo file=Upload:setup.exe display=name,version,date 
30  *                     format="%s (version: %s, date: %s)" ?>
31  *
32  * @author: ReiniUrban
33  */
34
35 class WikiPlugin_FileInfo
36 extends WikiPlugin
37 {
38     function getName () {
39         return _("FileInfo");
40     }
41
42     function getDescription () {
43         return _("Display file information like version,size,date,... of uploaded files.");
44     }
45
46     function getVersion() {
47         return preg_replace("/[Revision: $]/", '',
48                             "\$Revision: 1.6 $");
49     }
50
51     function getDefaultArguments() {
52         return array(
53                      'file'      => false, // relative path from PHPWIKI_DIR. (required)
54                      'display'   => false, // version,size,date,mtime,owner,name,path,dirname,link.  (required)
55                      'format'    => false, // printf format string with %s only, all display modes 
56                      'quiet'     => false  // print no error if file not found
57                                            // from above vars return strings (optional)
58                     );
59     }
60
61     function run($dbi, $argstr, &$request, $basepage) {
62         extract($this->getArgs($argstr, $request));
63         if (!$file)
64             return $this->error(sprintf(_("A required argument '%s' is missing."), 'file'));
65         if (!$display)
66             return $this->error(sprintf(_("A required argument '%s' is missing."), 'display'));
67
68         $dir = getcwd();
69         chdir(PHPWIKI_DIR);
70         if (!file_exists($file)) {
71             if ($quiet)
72                 return '';
73             else
74                 trigger_error("file \"$file\" not found", E_USER_WARNING);
75         }
76         // sanify $file name
77         $realfile = realpath($file);
78         // Hmm, allow ADMIN to check a local file? Only if its locked
79         if (!string_starts_with($realfile, realpath(getUploadDataPath()))) {
80             $page = $dbi->getPage($basepage);
81             $user = $request->getUser();
82             if ($page->getOwner() != ADMIN_USER or !$page->get('locked')) {
83                 // For convenience we warn the admin
84                 if ($quiet and $user->isAdmin())
85                     return HTML::span(array('title' => _("Output suppressed. FileInfoPlugin with local files require a locked page.")),
86                                       HTML::em(_("page not locked")));
87                 else
88                     return $this->error("Invalid path \"$file\". Only ADMIN can allow local paths, and the page must be locked.");
89             }
90         }
91         else 
92             $isuploaded = 1;
93         $s = array();
94         $modes = explode(",", $display);
95         foreach ($modes as $mode) {
96             switch ($mode) {
97             case 'version':  $s[] = $this->exeversion($file); break;
98             case 'size':     $s[] = filesize($file); break;
99             case 'phonysize':$s[] = $this->phonysize(filesize($file)); break;
100             case 'date':     $s[] = strftime("%x %X", filemtime($file)); break;
101             case 'mtime':    $s[] = filemtime($file); break;
102             case 'name':     $s[] = basename($file); break;
103             case 'path':     $s[] = $file; break;
104             case 'dirname':  $s[] = dirname($file); break;
105             case 'magic':    $s[] = $this->magic($file); break;
106             case 'mime-typ': $s[] = $this->mime_type($file); break;
107             case 'link':    
108                 if ($isuploaded) {
109                     $s[] = "[Upload:".basename($file)."]"; 
110                 } else {
111                     $s[] = "[".basename($file)."]"; 
112                 }
113                 break;
114             default:
115                 if (!$quiet)
116                     return $this->error(sprintf(_("Unsupported argument: %s=%s"), 'display', $mode)); 
117                 else return '';
118                 break;
119             }
120         }
121         chdir($dir);
122         if (!$format) {
123             $format = '';
124             foreach ($s as $x) { $format .= " %s"; }
125         }
126         array_unshift($s, $format);
127         // $x, array($i,$j) => sprintf($x, $i, $j)
128         $result = call_user_func_array("sprintf", $s);
129         if (in_array('link', $modes)) {
130             require_once("lib/InlineParser.php");
131             return TransformInline($result);
132         } else {
133             return $result;
134         }
135     }
136
137     function magic($file) {
138         if (function_exists('finfo_file') or loadPhpExtension('fileinfo')) {
139             // Valid finfo_open (i.e. libmagic) options:
140             // FILEINFO_NONE | FILEINFO_SYMLINK | FILEINFO_MIME | FILEINFO_COMPRESS | FILEINFO_DEVICES |
141             // FILEINFO_CONTINUE | FILEINFO_PRESERVE_ATIME | FILEINFO_RAW
142             $f = finfo_open(/*FILEINFO_MIME*/);
143             $result = finfo_file(realpath($file));
144             finfo_close($res);
145             return $result;
146         }
147         return '';
148     }
149
150     function mime_type($file) {
151         return '';
152     }
153
154     function _formatsize ($n, $factor, $suffix = '') {
155         if ($n > $factor) {
156             $b = $n / $factor;
157             $n -= floor($factor * $b);
158             return number_format($b, $n ? 3 : 0). $suffix;
159         }
160     }
161     function phonysize ($a) {
162         $factor = 1024 * 1024 * 1000;
163         if ($a > $factor)
164             return $this->_formatsize($a, $factor, ' GB');
165         $factor = 1024 * 1000;
166         if ($a > $factor)
167             return $this->_formatsize($a, $factor, ' MB');
168         $factor = 1024;
169         if ($a > $factor)
170             return $this->_formatsize($a, $factor, ' KB');
171         if ($a > 1)
172             return $this->_formatsize($a, 1, ' byte');
173         else
174             return $a;
175     }
176
177     function exeversion($file) {
178         if (!isWindows()) return "?";
179         if (class_exists('ffi') or loadPhpExtension('ffi'))
180             return $this->exeversion_ffi($file);
181         if (function_exists('res_list_type') or loadPhpExtension('win32std'))
182             return $this->exeversion_resopen($file);
183         return exeversion_showver($file);
184         return '';
185     }
186
187     // http://www.codeproject.com/dll/showver.asp
188     function exeversion_showver($file) {
189         $path = realpath($file);
190         $result = `showver $path`; 
191         return "?";
192     }
193
194     function exeversion_ffi($file) {
195         if (!DEBUG)
196             return "?"; // not yet stable
197         
198         if (function_exists('ffi') or loadPhpExtension('ffi')) {
199             $win32_idl = "
200 struct VS_FIXEDFILEINFO {
201         DWORD dwSignature;
202         DWORD dwStrucVersion;
203         DWORD dwFileVersionMS;
204         DWORD dwFileVersionLS;
205         DWORD dwProductVersionMS;
206         DWORD dwProductVersionLS;
207         DWORD dwFileFlagsMask;
208         DWORD dwFileFlags;
209         DWORD dwFileOS;
210         DWORD dwFileType;
211         DWORD dwFileSubtype;
212         DWORD dwFileDateMS;
213         DWORD dwFileDateLS;
214 };
215 struct VS_VERSIONINFO { struct VS_VERSIONINFO
216   WORD  wLength; 
217   WORD  wValueLength; 
218   WORD  wType; 
219   WCHAR szKey[1]; 
220   WORD  Padding1[1]; 
221   VS_FIXEDFILEINFO Value; 
222   WORD  Padding2[1]; 
223   WORD  Children[1]; 
224 };
225 [lib='kernel32.dll'] DWORD GetFileVersionInfoSizeA(char *szFileName, DWORD *dwVerHnd);
226 [lib='kernel32.dll'] int GetFileVersionInfoA(char *sfnFile, DWORD dummy, DWORD size, struct VS_VERSIONINFO *pVer);
227 ";
228             $ffi = new ffi($win32_idl);
229             $dummy = 0; // &DWORD
230             $size = $ffi->GetFileVersionInfoSizeA($file, $dummy);
231             //$pVer = str_repeat($size+1);
232             $pVer = new ffi_struct($ffi, "VS_VERSIONINFO");
233             if ($ffi->GetFileVersionInfoA($file, 0, $size, $pVer) 
234                 and $pVer->wValueLength) {
235                 // analyze the VS_FIXEDFILEINFO(Value);
236                 // $pValue = new ffi_struct($ffi, "VS_FIXEDFILEINFO");
237                 $pValue =& $pVer->Value;
238                 return sprintf("%d.%d.%d.%d",
239                                $pValue->dwFileVersionMS >> 16,
240                                $pValue->dwFileVersionMS & 0xFFFF,
241                                $pValue->dwFileVersionLS >> 16, 
242                                $pValue->dwFileVersionLS & 0xFFFF);
243             }
244         }
245     }
246
247     // Read "RT_VERSION/VERSIONINFO" exe/dll resource info for MSWin32 binaries
248     // The "win32std" extension is not ready yet to pass back a VERSIONINFO struct
249     function exeversion_resopen($file) {
250         if (function_exists('res_list_type') or loadPhpExtension('win32std')) {
251             // See http://msdn.microsoft.com/workshop/networking/predefined/res.asp
252             $v = file_get_contents('res://'.realpath($file).urlencode('/RT_VERSION/#1'));
253             if ($v) {
254                 // This is really a binary VERSIONINFO block, with lots of
255                 // nul bytes (widechar) which cannot be transported as string.
256                 return "$v";
257             }
258             else {
259                 $h = res_open(realpath($file));
260                 $v = res_get($h, 'RT_VERSION', 'FileVersion');
261                 res_close($h);
262                 if ($v) return $v;
263
264                 $h = res_open(realpath($file));
265                 $v = res_get($h, '#1', 'RT_VERSION', 1);
266                 res_close($h);
267                 if ($v) return $v;
268             }
269               
270             /* The version consists of two 32-bit integers, defined by four 16-bit integers. 
271                For example, "FILEVERSION 3,10,0,61" is translated into two doublewords: 
272                0x0003000a and 0x0000003d, in that order. */
273 /*          
274         $h = res_open(realpath($file));
275             
276         echo "Res list of '$file': \n";
277         $list= res_list_type($h, true);
278         if( $list===FALSE ) err( "Can't list type" );
279         
280         for( $i= 0; $i<count($list); $i++ ) {
281                 echo $list[$i]."\n";
282                 $res= res_list($h, $list[$i]);
283                 for( $j= 0; $j<count($res); $j++ ) {
284                         echo "\t".$res[$j]."\n";
285                 }
286         }
287         echo "Res get: ".res_get( $h, 'A_TYPE', 'A_RC_NAME' )."\n\n";
288         res_close( $h );            
289 */
290             if ($v)
291                 return "$v";
292             else 
293                 return "";
294         } else {
295             return "";
296         }
297         
298     }
299 };
300
301 /* 
302  $Log: not supported by cvs2svn $
303  Revision 1.5  2006/08/25 22:10:16  rurban
304  fix docs: FileVersion => FileInfo
305
306  Revision 1.4  2005/10/29 14:18:47  rurban
307  add display=phonysize
308
309  Revision 1.3  2005/10/29 13:35:00  rurban
310  fix Log:, add chdir() if not in PHPWIKI_DIR, fix ->warning
311
312
313 */
314
315 // For emacs users
316 // Local Variables:
317 // mode: php
318 // tab-width: 8
319 // c-basic-offset: 4
320 // c-hanging-comment-ender-p: nil
321 // indent-tabs-mode: nil
322 // End:
323 ?>