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