Example #1
0
/**
 * 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');
}