File: imagecache.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (169 lines) | stat: -rwxr-xr-x 5,701 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php rcs_id('$Id: imagecache.php,v 1.13 2006/03/19 15:01:00 rurban Exp $');
/*
 Copyright (C) 2002 Johannes Groe (Johannes Gro&szlig;e)

 This file is part of PhpWiki.

 PhpWiki is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 PhpWiki is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with PhpWiki; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 
/**
 * Gets an image from the cache and prints it to the browser.
 * This file belongs to WikiPluginCached.
 * @author  Johannes Groe
 * @version 0.8
 */

include_once("lib/config.php");
require_once(dirname(__FILE__)."/stdlib.php");
require_once('lib/Request.php');
if (ENABLE_USER_NEW) require_once("lib/WikiUserNew.php");
else                 require_once("lib/WikiUser.php");
require_once('lib/WikiDB.php');

require_once "lib/WikiPluginCached.php";

// -----------------------------------------------------------------------

function deducePagename ($request) {
    if ($request->getArg('pagename'))
        return $request->getArg('pagename');

    if (USE_PATH_INFO) {
        $pathinfo = $request->get('PATH_INFO');
        $tail = substr($pathinfo, strlen(PATH_INFO_PREFIX));
        if ($tail != '' and $pathinfo == PATH_INFO_PREFIX . $tail) {
            return $tail;
        }
    }
    elseif ($this->isPost()) {
        global $HTTP_GET_VARS;
        if (isset($HTTP_GET_VARS['pagename'])) { 
            return $HTTP_GET_VARS['pagename'];
        }
    }

    $query_string = $request->get('QUERY_STRING');
    if (preg_match('/^[^&=]+$/', $query_string))
        return urldecode($query_string);
    
    return HOME_PAGE;
}

function deduceUsername() {
    global $request, $HTTP_SERVER_VARS, $HTTP_ENV_VARS;
    if (!empty($request->args['auth']) and !empty($request->args['auth']['userid']))
        return $request->args['auth']['userid'];
    if (!empty($HTTP_SERVER_VARS['PHP_AUTH_USER']))
        return $HTTP_SERVER_VARS['PHP_AUTH_USER'];
    if (!empty($HTTP_ENV_VARS['REMOTE_USER']))
        return $HTTP_ENV_VARS['REMOTE_USER'];
    
    if ($user = $request->getSessionVar('wiki_user')) {
        $request->_user = $user;
        $request->_user->_authhow = 'session';
        return ENABLE_USER_NEW ? $user->UserName() : $request->_user;
    }
    if ($userid = $request->getCookieVar(getCookieName())) {
        if (!empty($userid) and substr($userid,0,2) != 's:') {
            $request->_user->authhow = 'cookie';
            return $userid;
        }
    }
    return false;
}

/**
 * Initializes PhpWiki and calls the plugin specified in the url to
 * produce an image. Furthermore, allow the usage of Apache's
 * ErrorDocument mechanism in order to make this file only called when 
 * image could not be found in the cache.
 * (see doc/README.phpwiki-cache for further information).
 */
function mainImageCache() {
    $request = new Request;   
    // normalize pagename
    $request->setArg('pagename', deducePagename($request));
    $pagename = $request->getArg('pagename');
    $request->_dbi = WikiDB::open($GLOBALS['DBParams']);
    if (ENABLE_USER_NEW) {
        $request->_user = new _AnonUser();
        $request->_prefs =& $request->_user->_prefs;
    } else {
    	$request->_user = new WikiUser($request);
        $request->_prefs = new UserPreferences();
    }
    
    // Enable the output of most of the warning messages.
    // The warnings will screw up zip files and setpref though.
    // They will also screw up my images... But I think 
    // we should keep them.
    global $ErrorManager;
    $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE);

    $id = $request->getArg('id');
    $args = $request->getArg('args');
    $request->setArg('action', 'imagecache');
    $cache = new WikiPluginCached;

    if ($id) {
        // this indicates a direct call (script wasn't called as
        // 404 ErrorDocument)
    } else {
        // deduce image id or image args (plugincall) from
        // refering URL

        $uri = $request->get('REDIRECT_URL');
        $query = $request->get('REDIRECT_QUERY_STRING');
        $uri .= $query ? '?'.$query : '';        

        if (!$uri) {
            $uri = $request->get('REQUEST_URI');
        }
        if (!$uri) {
            $cache->printError( 'png', 
                'Could not deduce image identifier or creation'
                . ' parameters. (Neither REQUEST nor REDIRECT'
                . ' obtained.)' ); 
            return;
        }    
        //$cacheparams = $GLOBALS['CacheParams'];
        if (!preg_match(':^(.*/)?'.PLUGIN_CACHED_FILENAME_PREFIX.'([^\?/]+)\.img(\?args=([^\?&]*))?$:', $uri, $matches)) {
            $cache->printError('png', "I do not understand this URL: $uri");
            return;
        }        
        
        $request->setArg('id', $matches[2]);
        if ($matches[4]) {
            // md5 args?
           $request->setArg('args', rawurldecode($matches[4]));
        }
        $request->setStatus(200); // No, we do _not_ have an Error 404 :->
    }

    $cache->fetchImageFromCache($request->_dbi, $request, 'png');
}


mainImageCache();


// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:   
?>