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
39 function getDescription()
41 return _("Display file information like version, size, date... of uploaded files.");
44 function getDefaultArguments()
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)
55 function run($dbi, $argstr, &$request, $basepage)
57 $args = $this->getArgs($argstr, $request);
60 return $this->error(sprintf(_("A required argument “%s” is missing."), 'file'));
63 return $this->error(sprintf(_("A required argument “%s” is missing."), 'display'));
65 if (string_starts_with($file, "Upload:")) {
66 $file = preg_replace("/^Upload:(.*)$/", getUploadFilePath() . "\\1", $file);
70 if (defined('PHPWIKI_DIR')) {
73 if (!file_exists($file)) {
77 return $this->error(sprintf(_("File “%s” not found."), $file));
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()))) {
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")));
94 return $this->error("Invalid path \"$file\". Only ADMIN can allow local paths, and the page must be locked.");
98 $modes = explode(",", $display);
99 foreach ($modes as $mode) {
102 $s[] = $this->exeversion($file);
105 $s[] = filesize($file);
108 $s[] = $this->phonysize(filesize($file));
111 $s[] = strftime("%x %X", filemtime($file));
114 $s[] = filemtime($file);
117 $o = posix_getpwuid(fileowner($file));
121 $o = posix_getgrgid(filegroup($file));
125 $s[] = basename($file);
131 $s[] = dirname($file);
134 $s[] = $this->magic($file);
137 $s[] = $this->mime_type($file);
141 $s[] = " [" . $args['file'] . "]";
142 } elseif ($isuploaded) {
143 // will fail with user uploads
144 $s[] = " [Upload:" . basename($file) . "]";
146 $s[] = " [" . basename($file) . "] ";
151 return $this->error(sprintf(_("Unsupported argument: %s=%s"), 'display', $mode));
153 return HTML::raw('');
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, $basepage);
172 return HTML::raw($result);
176 function magic($file)
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));
190 function mime_type($file)
195 private function _formatsize($n, $factor, $suffix = '')
199 $n -= floor($factor * $b);
200 return number_format($b, $n ? 3 : 0) . $suffix;
205 function phonysize($a)
207 $factor = 1024 * 1024 * 1000;
209 return $this->_formatsize($a, $factor, ' GB');
210 $factor = 1024 * 1000;
212 return $this->_formatsize($a, $factor, ' MB');
215 return $this->_formatsize($a, $factor, ' KB');
217 return $this->_formatsize($a, 1, ' byte');
222 function exeversion($file)
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);
232 // http://www.codeproject.com/dll/showver.asp
233 function exeversion_showver($file)
235 $path = realpath($file);
236 $result = `showver $path`;
240 function exeversion_ffi($file)
243 return "?"; // not yet stable
245 if (function_exists('ffi') or loadPhpExtension('ffi')) {
247 struct VS_FIXEDFILEINFO {
249 DWORD dwStrucVersion;
250 DWORD dwFileVersionMS;
251 DWORD dwFileVersionLS;
252 DWORD dwProductVersionMS;
253 DWORD dwProductVersionLS;
254 DWORD dwFileFlagsMask;
262 struct VS_VERSIONINFO { struct VS_VERSIONINFO
268 VS_FIXEDFILEINFO Value;
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);
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
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);
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)
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'));
304 // This is really a binary VERSIONINFO block, with lots of
305 // nul bytes (widechar) which cannot be transported as string.
308 $h = res_open(realpath($file));
309 $v = res_get($h, 'RT_VERSION', 'FileVersion');
313 $h = res_open(realpath($file));
314 $v = res_get($h, '#1', 'RT_VERSION', 1);
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. */
323 $h = res_open(realpath($file));
325 echo "Res list of '$file': \n";
326 $list= res_list_type($h, true);
327 if( $list===FALSE ) err( "Can't list type" );
329 for( $i= 0; $i<count($list); $i++ ) {
331 $res= res_list($h, $list[$i]);
332 for( $j= 0; $j<count($res); $j++ ) {
333 echo "\t".$res[$j]."\n";
336 echo "Res get: ".res_get( $h, 'A_TYPE', 'A_RC_NAME' )."\n\n";
354 // c-hanging-comment-ender-p: nil
355 // indent-tabs-mode: nil