]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/imagecache.php
remove final \n to be ob_cache independent
[SourceForge/phpwiki.git] / lib / imagecache.php
1 <?php rcs_id('$Id: imagecache.php,v 1.12 2004-11-21 11:59:20 rurban Exp $');
2 /*
3  Copyright (C) 2002 Johannes Große (Johannes Gro&szlig;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  * Gets an image from the cache and prints it to the browser.
23  * This file belongs to WikiPluginCached.
24  * @author  Johannes Große
25  * @version 0.8
26  */
27
28 include_once("lib/config.php");
29 require_once(dirname(__FILE__)."/stdlib.php");
30 require_once('lib/Request.php');
31 if (ENABLE_USER_NEW) require_once("lib/WikiUserNew.php");
32 else                 require_once("lib/WikiUser.php");
33 require_once('lib/WikiDB.php');
34
35 require_once "lib/WikiPluginCached.php";
36
37 // -----------------------------------------------------------------------
38
39 function deducePagename ($request) {
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     }
50     elseif ($this->isPost()) {
51         global $HTTP_GET_VARS;
52         if (isset($HTTP_GET_VARS['pagename'])) { 
53             return $HTTP_GET_VARS['pagename'];
54         }
55     }
56
57     $query_string = $request->get('QUERY_STRING');
58     if (preg_match('/^[^&=]+$/', $query_string))
59         return urldecode($query_string);
60     
61     return HOME_PAGE;
62 }
63
64 function deduceUsername() {
65     global $request, $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
66     if (!empty($request->args['auth']) and !empty($request->args['auth']['userid']))
67         return $request->args['auth']['userid'];
68     if (!empty($HTTP_SERVER_VARS['PHP_AUTH_USER']))
69         return $HTTP_SERVER_VARS['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 ENABLE_USER_NEW ? $user->UserName() : $request->_user;
77     }
78     if ($userid = $request->getCookieVar('WIKI_ID')) {
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     $request = new Request;   
96     // normalize pagename
97     $request->setArg('pagename', deducePagename($request));
98     $pagename = $request->getArg('pagename');
99     $request->_dbi = WikiDB::open($GLOBALS['DBParams']);
100     if (ENABLE_USER_NEW) {
101         $request->_user = new _AnonUser();
102         $request->_prefs =& $request->_user->_prefs;
103     } else {
104         $request->_user = new WikiUser($request);
105         $request->_prefs = new UserPreferences();
106     }
107     
108     // Enable the output of most of the warning messages.
109     // The warnings will screw up zip files and setpref though.
110     // They will also screw up my images... But I think 
111     // we should keep them.
112     global $ErrorManager;
113     $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);
114
115     $id = $request->getArg('id');
116     $args = $request->getArg('args');
117     $request->setArg('action', 'imagecache');
118     $cache = new WikiPluginCached;
119
120     if ($id) {
121         // this indicates a direct call (script wasn't called as
122         // 404 ErrorDocument)
123     } else {
124         // deduce image id or image args (plugincall) from
125         // refering URL
126
127         $uri = $request->get('REDIRECT_URL');
128         $query = $request->get('REDIRECT_QUERY_STRING');
129         $uri .= $query ? '?'.$query : '';        
130
131         if (!$uri) {
132             $uri = $request->get('REQUEST_URI');
133         }
134         if (!$uri) {
135             $cache->printError( 'png', 
136                 'Could not deduce image identifier or creation'
137                 . ' parameters. (Neither REQUEST nor REDIRECT'
138                 . ' obtained.)' ); 
139             return;
140         }    
141         //$cacheparams = $GLOBALS['CacheParams'];
142         if (!preg_match(':^(.*/)?'.PLUGIN_CACHED_FILENAME_PREFIX.'([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
143             $cache->printError('png', "I do not understand this URL: $uri");
144             return;
145         }        
146         
147         $request->setArg('id', $matches[2]);
148         if ($matches[4]) {
149             // md5 args?
150            $request->setArg('args', rawurldecode($matches[4]));
151         }
152         $request->setStatus(200); // No, we do _not_ have an Error 404 :->
153     }
154
155     $cache->fetchImageFromCache($request->_dbi, $request, 'png');
156 }
157
158
159 mainImageCache();
160
161
162 // Local Variables:
163 // mode: php
164 // tab-width: 8
165 // c-basic-offset: 4
166 // c-hanging-comment-ender-p: nil
167 // indent-tabs-mode: nil
168 // End:   
169 ?>