]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/imagecache.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / imagecache.php
1 <?php
2 /*
3  * Copyright (C) 2002 Johannes Große
4  *
5  * This file is part of PhpWiki.
6  *
7  * PhpWiki is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * PhpWiki is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 /**
23  * Gets an image from the cache and prints it to the browser.
24  * This file belongs to WikiPluginCached.
25  * @author  Johannes Große
26  * @version 0.8
27  */
28
29 include_once 'lib/config.php';
30 require_once(dirname(__FILE__) . "/stdlib.php");
31 require_once 'lib/Request.php';
32 if (ENABLE_USER_NEW) require_once("lib/WikiUserNew.php");
33 else                 require_once("lib/WikiUser.php");
34 require_once 'lib/WikiDB.php';
35
36 require_once 'lib/WikiPluginCached.php';
37
38 // -----------------------------------------------------------------------
39
40 function deducePagename($request)
41 {
42     if ($request->getArg('pagename'))
43         return $request->getArg('pagename');
44
45     if (USE_PATH_INFO) {
46         $pathinfo = $request->get('PATH_INFO');
47         $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
48         if ($tail != '' and $pathinfo == PATH_INFO_PREFIX . $tail) {
49             return $tail;
50         }
51     } elseif ($this->isPost()) {
52         global $HTTP_GET_VARS;
53         if (isset($HTTP_GET_VARS['pagename'])) {
54             return $HTTP_GET_VARS['pagename'];
55         }
56     }
57
58     $query_string = $request->get('QUERY_STRING');
59     if (preg_match('/^[^&=]+$/', $query_string))
60         return urldecode($query_string);
61
62     return HOME_PAGE;
63 }
64
65 function deduceUsername()
66 {
67     global $request, $HTTP_ENV_VARS;
68     if (!empty($request->args['auth']) and !empty($request->args['auth']['userid']))
69         return $request->args['auth']['userid'];
70     if (!empty($_SERVER['PHP_AUTH_USER']))
71         return $_SERVER['PHP_AUTH_USER'];
72     if (!empty($HTTP_ENV_VARS['REMOTE_USER']))
73         return $HTTP_ENV_VARS['REMOTE_USER'];
74
75     if ($user = $request->getSessionVar('wiki_user')) {
76         $request->_user = $user;
77         $request->_user->_authhow = 'session';
78         return ENABLE_USER_NEW ? $user->UserName() : $request->_user;
79     }
80     if ($userid = $request->getCookieVar(getCookieName())) {
81         if (!empty($userid) and substr($userid, 0, 2) != 's:') {
82             $request->_user->authhow = 'cookie';
83             return $userid;
84         }
85     }
86     return false;
87 }
88
89 /**
90  * Initializes PhpWiki and calls the plugin specified in the url to
91  * produce an image. Furthermore, allow the usage of Apache's
92  * ErrorDocument mechanism in order to make this file only called when
93  * image could not be found in the cache.
94  * (see doc/README.phpwiki-cache for further information).
95  */
96 function mainImageCache()
97 {
98     $request = new Request;
99     // normalize pagename
100     $request->setArg('pagename', deducePagename($request));
101     $pagename = $request->getArg('pagename');
102     $request->_dbi = WikiDB::open($GLOBALS['DBParams']);
103     if (ENABLE_USER_NEW) {
104         $request->_user = new _AnonUser();
105         $request->_prefs =& $request->_user->_prefs;
106     } else {
107         $request->_user = new WikiUser($request);
108         $request->_prefs = new UserPreferences();
109     }
110
111     // Enable the output of most of the warning messages.
112     // The warnings will screw up zip files and setpref though.
113     // They will also screw up my images... But I think
114     // we should keep them.
115     global $ErrorManager;
116     $ErrorManager->setPostponedErrorMask(E_NOTICE | E_USER_NOTICE);
117
118     $id = $request->getArg('id');
119     $args = $request->getArg('args');
120     $request->setArg('action', 'imagecache');
121     $cache = new WikiPluginCached;
122
123     if ($id) {
124         // this indicates a direct call (script wasn't called as
125         // 404 ErrorDocument)
126     } else {
127         // deduce image id or image args (plugincall) from
128         // refering URL
129
130         $uri = $request->get('REDIRECT_URL');
131         $query = $request->get('REDIRECT_QUERY_STRING');
132         $uri .= $query ? '?' . $query : '';
133
134         if (!$uri) {
135             $uri = $request->get('REQUEST_URI');
136         }
137         if (!$uri) {
138             $cache->printError('png',
139                 'Could not deduce image identifier or creation'
140                     . ' parameters. (Neither REQUEST nor REDIRECT'
141                     . ' obtained.)');
142             return;
143         }
144         //$cacheparams = $GLOBALS['CacheParams'];
145         if (!preg_match(':^(.*/)?' . PLUGIN_CACHED_FILENAME_PREFIX . '([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
146             $cache->printError('png', "I do not understand this URL: $uri");
147             return;
148         }
149
150         $request->setArg('id', $matches[2]);
151         if ($matches[4]) {
152             // md5 args?
153             $request->setArg('args', rawurldecode($matches[4]));
154         }
155         $request->setStatus(200); // No, we do _not_ have an Error 404 :->
156     }
157
158     $cache->fetchImageFromCache($request->_dbi, $request, 'png');
159 }
160
161 mainImageCache();
162
163 // Local Variables:
164 // mode: php
165 // tab-width: 8
166 // c-basic-offset: 4
167 // c-hanging-comment-ender-p: nil
168 // indent-tabs-mode: nil
169 // End: