]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/FileInfo.php
Add missing return
[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 getName()
40     {
41         return _("FileInfo");
42     }
43
44     function getDescription()
45     {
46         return _("Display file information like version, size, date... of uploaded files.");
47     }
48
49     function getDefaultArguments()
50     {
51         return array(
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)
57         );
58     }
59
60     function run($dbi, $argstr, &$request, $basepage)
61     {
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         }
67         if (!$display) {
68             return $this->error(sprintf(_("A required argument “%s” is missing."), 'display'));
69         }
70         if (string_starts_with($file, "Upload:")) {
71             $file = preg_replace("/^Upload:(.*)$/", getUploadFilePath() . "\\1", $file);
72             $is_Upload = 1;
73         }
74         $dir = getcwd();
75         if (defined('PHPWIKI_DIR')) {
76             chdir(PHPWIKI_DIR);
77         }
78         if (!file_exists($file)) {
79             if ($quiet) {
80                 return HTML::raw('');
81             } else {
82                 return $this->error(sprintf(_("File “%s” not found."), $file));
83             }
84         }
85         // sanify $file name
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()))) {
89             $isuploaded = 1;
90         } else {
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")));
98                 else
99                     return $this->error("Invalid path \"$file\". Only ADMIN can allow local paths, and the page must be locked.");
100             }
101         }
102         $s = array();
103         $modes = explode(",", $display);
104         foreach ($modes as $mode) {
105             switch ($mode) {
106                 case 'version':
107                     $s[] = $this->exeversion($file);
108                     break;
109                 case 'size':
110                     $s[] = filesize($file);
111                     break;
112                 case 'phonysize':
113                     $s[] = $this->phonysize(filesize($file));
114                     break;
115                 case 'date':
116                     $s[] = strftime("%x %X", filemtime($file));
117                     break;
118                 case 'mtime':
119                     $s[] = filemtime($file);
120                     break;
121                 case 'owner':
122                     $o = posix_getpwuid(fileowner($file));
123                     $s[] = $o['name'];
124                     break;
125                 case 'group':
126                     $o = posix_getgrgid(filegroup($file));
127                     $s[] = $o['name'];
128                     break;
129                 case 'name':
130                     $s[] = basename($file);
131                     break;
132                 case 'path':
133                     $s[] = $file;
134                     break;
135                 case 'dirname':
136                     $s[] = dirname($file);
137                     break;
138                 case 'magic':
139                     $s[] = $this->magic($file);
140                     break;
141                 case 'mime-typ':
142                     $s[] = $this->mime_type($file);
143                     break;
144                 case 'link':
145                     if ($is_Upload) {
146                         $s[] = " [" . $args['file'] . "]";
147                     } elseif ($isuploaded) {
148                         // will fail with user uploads
149                         $s[] = " [Upload:" . basename($file) . "]";
150                     } else {
151                         $s[] = " [" . basename($file) . "] ";
152                     }
153                     break;
154                 default:
155                     if (!$quiet) {
156                         return $this->error(sprintf(_("Unsupported argument: %s=%s"), 'display', $mode));
157                     } else {
158                         return HTML::raw('');
159                     }
160                     break;
161             }
162         }
163         chdir($dir);
164         if (!$format) {
165             $format = '';
166             foreach ($s as $x) {
167                 $format .= " %s";
168             }
169         }
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);
176         } else {
177             return HTML::raw($result);
178         }
179     }
180
181     function magic($file)
182     {
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));
189             finfo_close($res);
190             return $result;
191         }
192         return '';
193     }
194
195     function mime_type($file)
196     {
197         return '';
198     }
199
200     private function _formatsize($n, $factor, $suffix = '')
201     {
202         if ($n > $factor) {
203             $b = $n / $factor;
204             $n -= floor($factor * $b);
205             return number_format($b, $n ? 3 : 0) . $suffix;
206         }
207         return '';
208     }
209
210     function phonysize($a)
211     {
212         $factor = 1024 * 1024 * 1000;
213         if ($a > $factor)
214             return $this->_formatsize($a, $factor, ' GB');
215         $factor = 1024 * 1000;
216         if ($a > $factor)
217             return $this->_formatsize($a, $factor, ' MB');
218         $factor = 1024;
219         if ($a > $factor)
220             return $this->_formatsize($a, $factor, ' KB');
221         if ($a > 1)
222             return $this->_formatsize($a, 1, ' byte');
223         else
224             return $a;
225     }
226
227     function exeversion($file)
228     {
229         if (!isWindows()) return "?";
230         if (class_exists('ffi') or loadPhpExtension('ffi'))
231             return $this->exeversion_ffi($file);
232         if (function_exists('res_list_type') or loadPhpExtension('win32std'))
233             return $this->exeversion_resopen($file);
234         return exeversion_showver($file);
235     }
236
237     // http://www.codeproject.com/dll/showver.asp
238     function exeversion_showver($file)
239     {
240         $path = realpath($file);
241         $result = `showver $path`;
242         return "?";
243     }
244
245     function exeversion_ffi($file)
246     {
247         if (!DEBUG)
248             return "?"; // not yet stable
249
250         if (function_exists('ffi') or loadPhpExtension('ffi')) {
251             $win32_idl = "
252 struct VS_FIXEDFILEINFO {
253         DWORD dwSignature;
254         DWORD dwStrucVersion;
255         DWORD dwFileVersionMS;
256         DWORD dwFileVersionLS;
257         DWORD dwProductVersionMS;
258         DWORD dwProductVersionLS;
259         DWORD dwFileFlagsMask;
260         DWORD dwFileFlags;
261         DWORD dwFileOS;
262         DWORD dwFileType;
263         DWORD dwFileSubtype;
264         DWORD dwFileDateMS;
265         DWORD dwFileDateLS;
266 };
267 struct VS_VERSIONINFO { struct VS_VERSIONINFO
268   WORD  wLength;
269   WORD  wValueLength;
270   WORD  wType;
271   WCHAR szKey[1];
272   WORD  Padding1[1];
273   VS_FIXEDFILEINFO Value;
274   WORD  Padding2[1];
275   WORD  Children[1];
276 };
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);
279 ";
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
287             ) {
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);
296             }
297         }
298     }
299
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)
303     {
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'));
307             if ($v) {
308                 // This is really a binary VERSIONINFO block, with lots of
309                 // nul bytes (widechar) which cannot be transported as string.
310                 return "$v";
311             } else {
312                 $h = res_open(realpath($file));
313                 $v = res_get($h, 'RT_VERSION', 'FileVersion');
314                 res_close($h);
315                 if ($v) return $v;
316
317                 $h = res_open(realpath($file));
318                 $v = res_get($h, '#1', 'RT_VERSION', 1);
319                 res_close($h);
320                 if ($v) return $v;
321             }
322
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. */
326             /*
327                     $h = res_open(realpath($file));
328
329                     echo "Res list of '$file': \n";
330                     $list= res_list_type($h, true);
331                     if( $list===FALSE ) err( "Can't list type" );
332
333                     for( $i= 0; $i<count($list); $i++ ) {
334                             echo $list[$i]."\n";
335                             $res= res_list($h, $list[$i]);
336                             for( $j= 0; $j<count($res); $j++ ) {
337                                     echo "\t".$res[$j]."\n";
338                             }
339                     }
340                     echo "Res get: ".res_get( $h, 'A_TYPE', 'A_RC_NAME' )."\n\n";
341                     res_close( $h );
342             */
343             if ($v)
344                 return "$v";
345             else
346                 return "";
347         } else {
348             return "";
349         }
350
351     }
352 }
353
354 // Local Variables:
355 // mode: php
356 // tab-width: 8
357 // c-basic-offset: 4
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil
360 // End: