4 * Copyright 2005,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
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.
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.
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.
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.
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)" >>
36 class WikiPlugin_FileInfo
44 function getDescription()
46 return _("Display file information like version, size, date... of uploaded files.");
49 function getDefaultArguments()
52 'file' => false, // relative path from PHPWIKI_DIR. (required)
53 'display' => false, // version,phonysize,size,date,mtime,owner,name,path,dirname,link. (required)
54 'format' => false, // printf format string with %s only, all display modes
55 'quiet' => false // print no error if file not found
56 // from above vars return strings (optional)
60 function run($dbi, $argstr, &$request, $basepage)
62 $args = $this->getArgs($argstr, $request);
65 return $this->error(sprintf(_("A required argument “%s” is missing."), 'file'));
68 return $this->error(sprintf(_("A required argument “%s” is missing."), 'display'));
70 if (string_starts_with($file, "Upload:")) {
71 $file = preg_replace("/^Upload:(.*)$/", getUploadFilePath() . "\\1", $file);
75 if (defined('PHPWIKI_DIR')) {
78 if (!file_exists($file)) {
82 return $this->error(sprintf(_("File “%s” not found."), $file));
86 $realfile = realpath($file);
87 // Hmm, allow ADMIN to check a local file? Only if its locked
88 if (string_starts_with($realfile, realpath(getUploadDataPath()))) {
91 $page = $dbi->getPage($basepage);
92 $user = $request->getUser();
93 if ($page->getOwner() != ADMIN_USER or !$page->get('locked')) {
94 // For convenience we warn the admin
95 if ($quiet and $user->isAdmin())
96 return HTML::span(array('title' => _("Output suppressed. FileInfoPlugin with local files require a locked page.")),
97 HTML::em(_("page not locked")));
99 return $this->error("Invalid path \"$file\". Only ADMIN can allow local paths, and the page must be locked.");
103 $modes = explode(",", $display);
104 foreach ($modes as $mode) {
107 $s[] = $this->exeversion($file);
110 $s[] = filesize($file);
113 $s[] = $this->phonysize(filesize($file));
116 $s[] = strftime("%x %X", filemtime($file));
119 $s[] = filemtime($file);
122 $o = posix_getpwuid(fileowner($file));
126 $o = posix_getgrgid(filegroup($file));
130 $s[] = basename($file);
136 $s[] = dirname($file);
139 $s[] = $this->magic($file);
142 $s[] = $this->mime_type($file);
146 $s[] = " [" . $args['file'] . "]";
147 } elseif ($isuploaded) {
148 // will fail with user uploads
149 $s[] = " [Upload:" . basename($file) . "]";
151 $s[] = " [" . basename($file) . "] ";
156 return $this->error(sprintf(_("Unsupported argument: %s=%s"), 'display', $mode));
158 return HTML::raw('');
170 array_unshift($s, $format);
171 // $x, array($i,$j) => sprintf($x, $i, $j)
172 $result = call_user_func_array("sprintf", $s);
173 if (in_array('link', $modes)) {
174 require_once 'lib/InlineParser.php';
175 return TransformInline($result, 2, $basepage);
177 return HTML::raw($result);
181 function magic($file)
183 if (function_exists('finfo_file') or loadPhpExtension('fileinfo')) {
184 // Valid finfo_open (i.e. libmagic) options:
185 // FILEINFO_NONE | FILEINFO_SYMLINK | FILEINFO_MIME | FILEINFO_COMPRESS | FILEINFO_DEVICES |
186 // FILEINFO_CONTINUE | FILEINFO_PRESERVE_ATIME | FILEINFO_RAW
187 $f = finfo_open( /*FILEINFO_MIME*/);
188 $result = finfo_file(realpath($file));
195 function mime_type($file)
200 function _formatsize($n, $factor, $suffix = '')
204 $n -= floor($factor * $b);
205 return number_format($b, $n ? 3 : 0) . $suffix;
209 function phonysize($a)
211 $factor = 1024 * 1024 * 1000;
213 return $this->_formatsize($a, $factor, ' GB');
214 $factor = 1024 * 1000;
216 return $this->_formatsize($a, $factor, ' MB');
219 return $this->_formatsize($a, $factor, ' KB');
221 return $this->_formatsize($a, 1, ' byte');
226 function exeversion($file)
228 if (!isWindows()) return "?";
229 if (class_exists('ffi') or loadPhpExtension('ffi'))
230 return $this->exeversion_ffi($file);
231 if (function_exists('res_list_type') or loadPhpExtension('win32std'))
232 return $this->exeversion_resopen($file);
233 return exeversion_showver($file);
237 // http://www.codeproject.com/dll/showver.asp
238 function exeversion_showver($file)
240 $path = realpath($file);
241 $result = `showver $path`;
245 function exeversion_ffi($file)
248 return "?"; // not yet stable
250 if (function_exists('ffi') or loadPhpExtension('ffi')) {
252 struct VS_FIXEDFILEINFO {
254 DWORD dwStrucVersion;
255 DWORD dwFileVersionMS;
256 DWORD dwFileVersionLS;
257 DWORD dwProductVersionMS;
258 DWORD dwProductVersionLS;
259 DWORD dwFileFlagsMask;
267 struct VS_VERSIONINFO { struct VS_VERSIONINFO
273 VS_FIXEDFILEINFO Value;
277 [lib='kernel32.dll'] DWORD GetFileVersionInfoSizeA(char *szFileName, DWORD *dwVerHnd);
278 [lib='kernel32.dll'] int GetFileVersionInfoA(char *sfnFile, DWORD dummy, DWORD size, struct VS_VERSIONINFO *pVer);
280 $ffi = new ffi($win32_idl);
281 $dummy = 0; // &DWORD
282 $size = $ffi->GetFileVersionInfoSizeA($file, $dummy);
283 //$pVer = str_repeat($size+1);
284 $pVer = new ffi_struct($ffi, "VS_VERSIONINFO");
285 if ($ffi->GetFileVersionInfoA($file, 0, $size, $pVer)
286 and $pVer->wValueLength
288 // analyze the VS_FIXEDFILEINFO(Value);
289 // $pValue = new ffi_struct($ffi, "VS_FIXEDFILEINFO");
290 $pValue =& $pVer->Value;
291 return sprintf("%d.%d.%d.%d",
292 $pValue->dwFileVersionMS >> 16,
293 $pValue->dwFileVersionMS & 0xFFFF,
294 $pValue->dwFileVersionLS >> 16,
295 $pValue->dwFileVersionLS & 0xFFFF);
300 // Read "RT_VERSION/VERSIONINFO" exe/dll resource info for MSWin32 binaries
301 // The "win32std" extension is not ready yet to pass back a VERSIONINFO struct
302 function exeversion_resopen($file)
304 if (function_exists('res_list_type') or loadPhpExtension('win32std')) {
305 // See http://msdn.microsoft.com/workshop/networking/predefined/res.asp
306 $v = file_get_contents('res://' . realpath($file) . urlencode('/RT_VERSION/#1'));
308 // This is really a binary VERSIONINFO block, with lots of
309 // nul bytes (widechar) which cannot be transported as string.
312 $h = res_open(realpath($file));
313 $v = res_get($h, 'RT_VERSION', 'FileVersion');
317 $h = res_open(realpath($file));
318 $v = res_get($h, '#1', 'RT_VERSION', 1);
323 /* The version consists of two 32-bit integers, defined by four 16-bit integers.
324 For example, "FILEVERSION 3,10,0,61" is translated into two doublewords:
325 0x0003000a and 0x0000003d, in that order. */
327 $h = res_open(realpath($file));
329 echo "Res list of '$file': \n";
330 $list= res_list_type($h, true);
331 if( $list===FALSE ) err( "Can't list type" );
333 for( $i= 0; $i<count($list); $i++ ) {
335 $res= res_list($h, $list[$i]);
336 for( $j= 0; $j<count($res); $j++ ) {
337 echo "\t".$res[$j]."\n";
340 echo "Res get: ".res_get( $h, 'A_TYPE', 'A_RC_NAME' )."\n\n";
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil