]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/imagecache.php
get db params from config/config.ini
[SourceForge/phpwiki.git] / lib / imagecache.php
1 <?php rcs_id('$Id: imagecache.php,v 1.8 2004-06-19 10:06:37 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
119     if ($id) {
120         // this indicates a direct call (script wasn't called as
121         // 404 ErrorDocument)
122     } else {
123         // deduce image id or image args (plugincall) from
124         // refering URL
125
126         $uri = $request->get('REDIRECT_URL');
127         $query = $request->get('REDIRECT_QUERY_STRING');
128         $uri .= $query ? '?'.$query : '';        
129
130         if (!$uri) {
131             $uri = $request->get('REQUEST_URI');
132         }
133         if (!$uri) {
134             WikiPluginCached::printError( 'png', 
135                 'Could not deduce image identifier or creation'
136                 . ' parameters. (Neither REQUEST nor REDIRECT'
137                 . ' obtained.)' ); 
138             return;
139         }    
140         //$cacheparams = $GLOBALS['CacheParams'];
141         if (!preg_match(':^(.*/)?'.PLUGIN_CACHED_FILENAME_PREFIX.'([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
142             WikiPluginCached::printError('png', "I do not understand this URL: $uri");
143             return;
144         }        
145         
146         $request->setArg('id',$matches[2]);
147         if ($matches[4]) {
148            $request->setArg('args',rawurldecode($matches[4]));
149         }
150         $request->setStatus(200); // No, we do _not_ have an Error 404 :->
151     }
152
153     WikiPluginCached::fetchImageFromCache($request->_dbi,$request,'png');
154 }
155
156
157 mainImageCache();
158
159
160 // Local Variables:
161 // mode: php
162 // tab-width: 8
163 // c-basic-offset: 4
164 // c-hanging-comment-ender-p: nil
165 // indent-tabs-mode: nil
166 // End:   
167 ?>