]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/imagecache.php
Remove ENABLE_USER_NEW (always true), remove lib/WikiUser.php
[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 require_once("lib/WikiUserNew.php");
33 require_once 'lib/WikiDB.php';
34 require_once 'lib/WikiPluginCached.php';
35
36 // -----------------------------------------------------------------------
37
38 function deducePagename($request)
39 {
40     if ($request->getArg('pagename'))
41         return $request->getArg('pagename');
42
43     if (USE_PATH_INFO) {
44         $pathinfo = $request->get('PATH_INFO');
45         $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
46         if ($tail != '' and $pathinfo == PATH_INFO_PREFIX . $tail) {
47             return $tail;
48         }
49     } elseif ($this->isPost()) {
50         global $HTTP_GET_VARS;
51         if (isset($HTTP_GET_VARS['pagename'])) {
52             return $HTTP_GET_VARS['pagename'];
53         }
54     }
55
56     $query_string = $request->get('QUERY_STRING');
57     if (preg_match('/^[^&=]+$/', $query_string))
58         return urldecode($query_string);
59
60     return HOME_PAGE;
61 }
62
63 function deduceUsername()
64 {
65     global $request, $HTTP_ENV_VARS;
66     if (!empty($request->args['auth']) and !empty($request->args['auth']['userid']))
67         return $request->args['auth']['userid'];
68     if (!empty($_SERVER['PHP_AUTH_USER']))
69         return $_SERVER['PHP_AUTH_USER'];
70     if (!empty($HTTP_ENV_VARS['REMOTE_USER']))
71         return $HTTP_ENV_VARS['REMOTE_USER'];
72
73     if ($user = $request->getSessionVar('wiki_user')) {
74         $request->_user = $user;
75         $request->_user->_authhow = 'session';
76         return $user->UserName();
77     }
78     if ($userid = $request->getCookieVar(getCookieName())) {
79         if (!empty($userid) and substr($userid, 0, 2) != 's:') {
80             $request->_user->authhow = 'cookie';
81             return $userid;
82         }
83     }
84     return false;
85 }
86
87 /**
88  * Initializes PhpWiki and calls the plugin specified in the url to
89  * produce an image. Furthermore, allow the usage of Apache's
90  * ErrorDocument mechanism in order to make this file only called when
91  * image could not be found in the cache.
92  * (see doc/README.phpwiki-cache for further information).
93  */
94 function mainImageCache()
95 {
96     $request = new Request;
97     // normalize pagename
98     $request->setArg('pagename', deducePagename($request));
99     $pagename = $request->getArg('pagename');
100     $request->_dbi = WikiDB::open($GLOBALS['DBParams']);
101     $request->_user = new _AnonUser();
102     $request->_prefs =& $request->_user->_prefs;
103
104     // Enable the output of most of the warning messages.
105     // The warnings will screw up zip files and setpref though.
106     // They will also screw up my images... But I think
107     // we should keep them.
108     global $ErrorManager;
109     $ErrorManager->setPostponedErrorMask(E_NOTICE | E_USER_NOTICE);
110
111     $id = $request->getArg('id');
112     $args = $request->getArg('args');
113     $request->setArg('action', 'imagecache');
114     $cache = new WikiPluginCached;
115
116     if ($id) {
117         // this indicates a direct call (script wasn't called as
118         // 404 ErrorDocument)
119     } else {
120         // deduce image id or image args (plugincall) from
121         // refering URL
122
123         $uri = $request->get('REDIRECT_URL');
124         $query = $request->get('REDIRECT_QUERY_STRING');
125         $uri .= $query ? '?' . $query : '';
126
127         if (!$uri) {
128             $uri = $request->get('REQUEST_URI');
129         }
130         if (!$uri) {
131             $cache->printError('png',
132                 'Could not deduce image identifier or creation'
133                     . ' parameters. (Neither REQUEST nor REDIRECT'
134                     . ' obtained.)');
135             return;
136         }
137         //$cacheparams = $GLOBALS['CacheParams'];
138         if (!preg_match(':^(.*/)?' . PLUGIN_CACHED_FILENAME_PREFIX . '([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
139             $cache->printError('png', "I do not understand this URL: $uri");
140             return;
141         }
142
143         $request->setArg('id', $matches[2]);
144         if ($matches[4]) {
145             // md5 args?
146             $request->setArg('args', rawurldecode($matches[4]));
147         }
148         $request->setStatus(200); // No, we do _not_ have an Error 404 :->
149     }
150
151     $cache->fetchImageFromCache($request->_dbi, $request, 'png');
152 }
153
154 mainImageCache();
155
156 // Local Variables:
157 // mode: php
158 // tab-width: 8
159 // c-basic-offset: 4
160 // c-hanging-comment-ender-p: nil
161 // indent-tabs-mode: nil
162 // End: