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