]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/imagecache.php
No tabs
[SourceForge/phpwiki.git] / lib / imagecache.php
1 <?php // $Id$
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
18  * along with PhpWiki; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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     if ($request->getArg('pagename'))
42         return $request->getArg('pagename');
43
44     if (USE_PATH_INFO) {
45         $pathinfo = $request->get('PATH_INFO');
46         $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
47         if ($tail != '' and $pathinfo == PATH_INFO_PREFIX . $tail) {
48             return $tail;
49         }
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     global $request, $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
67     if (!empty($request->args['auth']) and !empty($request->args['auth']['userid']))
68         return $request->args['auth']['userid'];
69     if (!empty($HTTP_SERVER_VARS['PHP_AUTH_USER']))
70         return $HTTP_SERVER_VARS['PHP_AUTH_USER'];
71     if (!empty($HTTP_ENV_VARS['REMOTE_USER']))
72         return $HTTP_ENV_VARS['REMOTE_USER'];
73
74     if ($user = $request->getSessionVar('wiki_user')) {
75         $request->_user = $user;
76         $request->_user->_authhow = 'session';
77         return ENABLE_USER_NEW ? $user->UserName() : $request->_user;
78     }
79     if ($userid = $request->getCookieVar(getCookieName())) {
80         if (!empty($userid) and substr($userid,0,2) != 's:') {
81             $request->_user->authhow = 'cookie';
82             return $userid;
83         }
84     }
85     return false;
86 }
87
88 /**
89  * Initializes PhpWiki and calls the plugin specified in the url to
90  * produce an image. Furthermore, allow the usage of Apache's
91  * ErrorDocument mechanism in order to make this file only called when
92  * image could not be found in the cache.
93  * (see doc/README.phpwiki-cache for further information).
94  */
95 function mainImageCache() {
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     if (ENABLE_USER_NEW) {
102         $request->_user = new _AnonUser();
103         $request->_prefs =& $request->_user->_prefs;
104     } else {
105         $request->_user = new WikiUser($request);
106         $request->_prefs = new UserPreferences();
107     }
108
109     // Enable the output of most of the warning messages.
110     // The warnings will screw up zip files and setpref though.
111     // They will also screw up my images... But I think
112     // we should keep them.
113     global $ErrorManager;
114     $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
115
116     $id = $request->getArg('id');
117     $args = $request->getArg('args');
118     $request->setArg('action', 'imagecache');
119     $cache = new WikiPluginCached;
120
121     if ($id) {
122         // this indicates a direct call (script wasn't called as
123         // 404 ErrorDocument)
124     } else {
125         // deduce image id or image args (plugincall) from
126         // refering URL
127
128         $uri = $request->get('REDIRECT_URL');
129         $query = $request->get('REDIRECT_QUERY_STRING');
130         $uri .= $query ? '?'.$query : '';
131
132         if (!$uri) {
133             $uri = $request->get('REQUEST_URI');
134         }
135         if (!$uri) {
136             $cache->printError( 'png',
137                 'Could not deduce image identifier or creation'
138                 . ' parameters. (Neither REQUEST nor REDIRECT'
139                 . ' obtained.)' );
140             return;
141         }
142         //$cacheparams = $GLOBALS['CacheParams'];
143         if (!preg_match(':^(.*/)?'.PLUGIN_CACHED_FILENAME_PREFIX.'([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
144             $cache->printError('png', "I do not understand this URL: $uri");
145             return;
146         }
147
148         $request->setArg('id', $matches[2]);
149         if ($matches[4]) {
150             // md5 args?
151            $request->setArg('args', rawurldecode($matches[4]));
152         }
153         $request->setStatus(200); // No, we do _not_ have an Error 404 :->
154     }
155
156     $cache->fetchImageFromCache($request->_dbi, $request, 'png');
157 }
158
159
160 mainImageCache();
161
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:
170 ?>