Пример #1
0
/**
 * Set headers and send the file to the client
 *
 * @author Andreas Gohr <*****@*****.**>
 * @author Ben Coburn <*****@*****.**>
 */
function sendFile($file, $mime, $dl, $cache)
{
    global $conf;
    $fmtime = @filemtime($file);
    // send headers
    header("Content-Type: {$mime}");
    // smart http caching headers
    if ($cache == -1) {
        // cache
        // cachetime or one hour
        header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
        header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
        header('Pragma: public');
    } else {
        if ($cache > 0) {
            // recache
            // remaining cachetime + 10 seconds so the newly recached media is used
            header('Expires: ' . gmdate("D, d M Y H:i:s", $fmtime + $conf['cachetime'] + 10) . ' GMT');
            header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($fmtime - time() + $conf['cachetime'] + 10, 0));
            header('Pragma: public');
        } else {
            if ($cache == 0) {
                // nocache
                header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
                header('Pragma: public');
            }
        }
    }
    //send important headers first, script stops here if '304 Not Modified' response
    http_conditionalRequest($fmtime);
    //download or display?
    if ($dl) {
        header('Content-Disposition: attachment; filename="' . basename($file) . '";');
    } else {
        header('Content-Disposition: inline; filename="' . basename($file) . '";');
    }
    //use x-sendfile header to pass the delivery to compatible webservers
    if (http_sendfile($file)) {
        exit;
    }
    // send file contents
    $fp = @fopen($file, "rb");
    if ($fp) {
        http_rangeRequest($fp, filesize($file), $mime);
    } else {
        header("HTTP/1.0 500 Internal Server Error");
        print "Could not read {$file} - bad permissions?";
    }
}
Пример #2
0
/**
 * Set headers and send the file to the client
 *
 * The $cache parameter influences how long files may be kept in caches, the $public parameter
 * influences if this caching may happen in public proxis or in the browser cache only FS#2734
 *
 * This function will abort the current script when a 304 is sent or file sending is handled
 * through x-sendfile
 *
 * @author Andreas Gohr <*****@*****.**>
 * @author Ben Coburn <*****@*****.**>
 * @author Gerry Weissbach <*****@*****.**>
 *
 * @param string $file   local file to send
 * @param string $mime   mime type of the file
 * @param bool   $dl     set to true to force a browser download
 * @param int    $cache  remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
 * @param bool   $public is this a public ressource or a private one?
 * @param string $orig   original file to send - the file name will be used for the Content-Disposition
 */
function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null)
{
    global $conf;
    // send mime headers
    header("Content-Type: {$mime}");
    // calculate cache times
    if ($cache == -1) {
        $maxage = max($conf['cachetime'], 3600);
        // cachetime or one hour
        $expires = time() + $maxage;
    } else {
        if ($cache > 0) {
            $maxage = $cache;
            // given time
            $expires = time() + $maxage;
        } else {
            // $cache == 0
            $maxage = 0;
            $expires = 0;
            // 1970-01-01
        }
    }
    // smart http caching headers
    if ($maxage) {
        if ($public) {
            // cache publically
            header('Expires: ' . gmdate("D, d M Y H:i:s", $expires) . ' GMT');
            header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . $maxage);
            header('Pragma: public');
        } else {
            // cache in browser
            header('Expires: ' . gmdate("D, d M Y H:i:s", $expires) . ' GMT');
            header('Cache-Control: private, no-transform, max-age=' . $maxage);
            header('Pragma: no-cache');
        }
    } else {
        // no cache at all
        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
        header('Cache-Control: no-cache, no-transform');
        header('Pragma: no-cache');
    }
    //send important headers first, script stops here if '304 Not Modified' response
    $fmtime = @filemtime($file);
    http_conditionalRequest($fmtime);
    // Use the current $file if is $orig is not set.
    if ($orig == null) {
        $orig = $file;
    }
    //download or display?
    if ($dl) {
        header('Content-Disposition: attachment;' . rfc2231_encode('filename', utf8_basename($orig)) . ';');
    } else {
        header('Content-Disposition: inline;' . rfc2231_encode('filename', utf8_basename($orig)) . ';');
    }
    //use x-sendfile header to pass the delivery to compatible webservers
    http_sendfile($file);
    // send file contents
    $fp = @fopen($file, "rb");
    if ($fp) {
        http_rangeRequest($fp, filesize($file), $mime);
    } else {
        http_status(500);
        print "Could not read {$file} - bad permissions?";
    }
}
Пример #3
0
$depends['purge'] = $_REQUEST['purge'] ? true : false;
// check cacheage and deliver if nothing has changed since last
// time or the update interval has not passed, also handles conditional requests
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');
if ($cache->useCache($depends)) {
    http_conditionalRequest($cache->_time);
    if ($conf['allowdebug']) {
        header("X-CacheUsed: {$cache->cache}");
    }
    print $cache->retrieveCache();
    exit;
} else {
    http_conditionalRequest(time());
}
// create new feed
$rss = new DokuWikiFeedCreator();
$rss->title = $conf['title'] . ($opt['namespace'] ? ' ' . $opt['namespace'] : '');
$rss->link = DOKU_URL;
$rss->syndicationURL = DOKU_URL . 'feed.php';
$rss->cssStyleSheet = DOKU_URL . 'lib/exe/css.php?s=feed';
$image = new FeedImage();
$image->title = $conf['title'];
$image->url = DOKU_URL . "lib/images/favicon.ico";
$image->link = DOKU_URL;
$rss->image = $image;
$data = null;
if ($opt['feed_mode'] == 'list') {
    $data = rssListNamespace($opt);
Пример #4
0
 /**
  * @param string $cachefile
  * @param string $title
  */
 protected function sendODTFile($cachefile, $title)
 {
     header('Content-Type: application/vnd.oasis.opendocument.text');
     header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
     header('Pragma: public');
     http_conditionalRequest(filemtime($cachefile));
     $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
     if ($this->getConf('output') == 'file') {
         header('Content-Disposition: attachment; filename="' . $filename . '.odt";');
     } else {
         header('Content-Disposition: inline; filename="' . $filename . '.odt";');
     }
     //try to send file, and exit if done
     http_sendfile($cachefile);
     $fp = @fopen($cachefile, "rb");
     if ($fp) {
         http_rangeRequest($fp, filesize($cachefile), 'application/vnd.oasis.opendocument.text');
     } else {
         header("HTTP/1.0 500 Internal Server Error");
         print "Could not read file - bad permissions?";
     }
     exit;
 }
Пример #5
0
    $pl->gfx_error('blank');
}
// cache times
$data['cache'] = getCacheName($data['file'], '.pv.' . $data['zoom'] . '-' . $data['col'] . '-' . $data['row'] . '.jpg');
$data['cachet'] = @filemtime($data['cache']);
// (re)generate
if ($data['cachet'] < $data['mtime']) {
    $pl->tile_lock($data);
    if ($conf['im_convert']) {
        $pl->tile_im($data);
    } else {
        $pl->tile_gd($data);
    }
    $pl->tile_unlock($data);
}
// send
header('Content-type: image/jpeg');
http_conditionalRequest(max($data['mtime'], $data['selft']));
//use x-sendfile header to pass the delivery to compatible webservers
if (http_sendfile($data['cache'])) {
    exit;
}
// send file contents
$fp = @fopen($data['cache'], "rb");
if ($fp) {
    http_rangeRequest($fp, filesize($data['cache']), 'image/jpeg');
} else {
    header("HTTP/1.0 500 Internal Server Error");
    print "Could not read tile - bad permissions?";
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
Пример #6
0
/**
 * Output all needed JavaScript
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function js_out()
{
    global $conf;
    global $lang;
    $edit = (bool) $_REQUEST['edit'];
    // edit or preview mode?
    $write = (bool) $_REQUEST['write'];
    // writable?
    // The generated script depends on some dynamic options
    $cache = getCacheName('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . $edit . 'x' . $write, '.js');
    // Array of needed files
    $files = array(DOKU_INC . 'lib/scripts/helpers.js', DOKU_INC . 'lib/scripts/events.js', DOKU_INC . 'lib/scripts/cookie.js', DOKU_INC . 'lib/scripts/script.js', DOKU_INC . 'lib/scripts/tw-sack.js', DOKU_INC . 'lib/scripts/ajax.js', DOKU_INC . 'lib/scripts/index.js');
    if ($edit) {
        if ($write) {
            $files[] = DOKU_INC . 'lib/scripts/edit.js';
        }
        $files[] = DOKU_INC . 'lib/scripts/media.js';
    }
    $files[] = DOKU_TPLINC . 'script.js';
    // get possible plugin scripts
    $plugins = js_pluginscripts();
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (js_cacheok($cache, array_merge($files, $plugins))) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the script
    ob_start();
    // add some global variables
    print "var DOKU_BASE   = '" . DOKU_BASE . "';";
    print "var DOKU_TPL    = '" . DOKU_TPL . "';";
    //FIXME: move thes into LANG
    print "var alertText   = '" . js_escape($lang['qb_alert']) . "';";
    print "var notSavedYet = '" . js_escape($lang['notsavedyet']) . "';";
    print "var reallyDel   = '" . js_escape($lang['del_confirm']) . "';";
    // load JS strings form plugins
    $lang['js']['plugins'] = js_pluginstrings();
    // load JS specific translations
    $json = new JSON();
    echo 'LANG = ' . $json->encode($lang['js']) . ";\n";
    // load files
    foreach ($files as $file) {
        echo "\n\n/* XXXXXXXXXX begin of {$file} XXXXXXXXXX */\n\n";
        js_load($file);
        echo "\n\n/* XXXXXXXXXX end of {$file} XXXXXXXXXX */\n\n";
    }
    // init stuff
    js_runonstart("ajax_qsearch.init('qsearch__in','qsearch__out')");
    js_runonstart("addEvent(document,'click',closePopups)");
    js_runonstart('addTocToggle()');
    if ($edit) {
        // size controls
        js_runonstart("initSizeCtl('size__ctl','wiki__text')");
        if ($write) {
            require_once DOKU_INC . 'inc/toolbar.php';
            toolbar_JSdefines('toolbar');
            js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)");
            // add pageleave check
            js_runonstart("initChangeCheck('" . js_escape($lang['notsavedyet']) . "')");
            // add lock timer
            js_runonstart("locktimer.init(" . ($conf['locktime'] - 60) . ",'" . js_escape($lang['willexpire']) . "'," . $conf['usedraft'] . ")");
        }
    }
    // load plugin scripts (suppress warnings for missing ones)
    foreach ($plugins as $plugin) {
        if (@file_exists($plugin)) {
            echo "\n\n/* XXXXXXXXXX begin of {$plugin} XXXXXXXXXX */\n\n";
            js_load($plugin);
            echo "\n\n/* XXXXXXXXXX end of {$plugin} XXXXXXXXXX */\n\n";
        }
    }
    // load user script
    @readfile(DOKU_CONF . 'userscript.js');
    // add scroll event and tooltip rewriting
    js_runonstart('scrollToMarker()');
    js_runonstart('focusMarker()');
    // end output buffering and get contents
    $js = ob_get_contents();
    ob_end_clean();
    // compress whitespace and comments
    if ($conf['compress']) {
        $js = js_compress($js);
    }
    $js .= "\n";
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    // save cache file
    io_saveFile($cache, $js);
    copy($cache, "compress.zlib://{$cache}.gz");
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($js, 9, FORCE_GZIP);
    } else {
        print $js;
    }
}
Пример #7
0
/**
 * Set HTTP headers and echo cachefile, if useable
 *
 * This function handles output of cacheable resource files. It ses the needed
 * HTTP headers. If a useable cache is present, it is passed to the web server
 * and the script is terminated.
 *
 * @param string $cache cache file name
 * @param bool   $cache_ok    if cache can be used
 */
function http_cached($cache, $cache_ok)
{
    global $conf;
    // check cache age & handle conditional request
    // since the resource files are timestamped, we can use a long max age: 1 year
    header('Cache-Control: public, max-age=31536000');
    header('Pragma: public');
    if ($cache_ok) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            http_sendfile($cache);
            readfile($cache);
        }
        exit;
    }
    http_conditionalRequest(time());
}
Пример #8
0
 /**
  * Do the HTML to PDF conversion work
  *
  * @param Doku_Event $event
  * @param array      $param
  * @return bool
  */
 public function convert(&$event, $param)
 {
     global $ACT;
     global $REV;
     global $ID;
     // our event?
     if ($ACT != 'export_pdfbook' && $ACT != 'export_pdf') {
         return false;
     }
     // check user's rights
     if (auth_quickaclcheck($ID) < AUTH_READ) {
         return false;
     }
     // one or multiple pages?
     $list = array();
     if ($ACT == 'export_pdf') {
         $list[0] = $ID;
         $title = p_get_first_heading($ID);
     } elseif (isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
         //is in Bookmanager of bookcreator plugin title given
         if (!($title = $_GET['pdfbook_title'])) {
             //TODO when title is changed, the cached file contains the old title
             /** @var $bookcreator action_plugin_bookcreator */
             $bookcreator = plugin_load('action', 'bookcreator');
             msg($bookcreator->getLang('needtitle'), -1);
             $event->data = 'show';
             $_SERVER['REQUEST_METHOD'] = 'POST';
             //clears url
             return false;
         }
         $list = explode("|", $_COOKIE['list-pagelist']);
     } else {
         /** @var $bookcreator action_plugin_bookcreator */
         $bookcreator = plugin_load('action', 'bookcreator');
         msg($bookcreator->getLang('empty'), -1);
         $event->data = 'show';
         $_SERVER['REQUEST_METHOD'] = 'POST';
         //clears url
         return false;
     }
     // it's ours, no one else's
     $event->preventDefault();
     // prepare cache
     $cache = new cache(join(',', $list) . $REV . $this->tpl, '.dw2.pdf');
     $depends['files'] = array_map('wikiFN', $list);
     $depends['files'][] = __FILE__;
     $depends['files'][] = dirname(__FILE__) . '/renderer.php';
     $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php';
     $depends['files'] = array_merge($depends['files'], getConfigFiles('main'));
     // hard work only when no cache available
     if (!$this->getConf('usecache') || !$cache->useCache($depends)) {
         // initialize PDF library
         require_once dirname(__FILE__) . "/DokuPDF.class.php";
         $mpdf = new DokuPDF();
         // let mpdf fix local links
         $self = parse_url(DOKU_URL);
         $url = $self['scheme'] . '://' . $self['host'];
         if ($self['port']) {
             $url .= ':' . $self['port'];
         }
         $mpdf->setBasePath($url);
         // Set the title
         $mpdf->SetTitle($title);
         // some default settings
         $mpdf->mirrorMargins = 1;
         $mpdf->useOddEven = 1;
         $mpdf->setAutoTopMargin = 'stretch';
         $mpdf->setAutoBottomMargin = 'stretch';
         // load the template
         $template = $this->load_template($title);
         // prepare HTML header styles
         $html = '<html><head>';
         $html .= '<style type="text/css">';
         $html .= $this->load_css();
         $html .= '@page { size:auto; ' . $template['page'] . '}';
         $html .= '@page :first {' . $template['first'] . '}';
         $html .= '</style>';
         $html .= '</head><body>';
         $html .= $template['html'];
         $html .= '<div class="dokuwiki">';
         // loop over all pages
         $cnt = count($list);
         for ($n = 0; $n < $cnt; $n++) {
             $page = $list[$n];
             $html .= p_cached_output(wikiFN($page, $REV), 'dw2pdf', $page);
             $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
             if ($n < $cnt - 1) {
                 $html .= '<pagebreak />';
             }
         }
         $html .= '</div>';
         $mpdf->WriteHTML($html);
         // write to cache file
         $mpdf->Output($cache->cache, 'F');
     }
     // deliver the file
     header('Content-Type: application/pdf');
     header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
     header('Pragma: public');
     http_conditionalRequest(filemtime($cache->cache));
     $filename = rawurlencode(cleanID(strtr($title, ':/;"', '    ')));
     if ($this->getConf('output') == 'file') {
         header('Content-Disposition: attachment; filename="' . $filename . '.pdf";');
     } else {
         header('Content-Disposition: inline; filename="' . $filename . '.pdf";');
     }
     if (http_sendfile($cache->cache)) {
         exit;
     }
     $fp = @fopen($cache->cache, "rb");
     if ($fp) {
         http_rangeRequest($fp, filesize($cache->cache), 'application/pdf');
     } else {
         header("HTTP/1.0 500 Internal Server Error");
         print "Could not read file - bad permissions?";
     }
     exit;
 }
Пример #9
0
/**
 * Handle sitemap delivery
 *
 * @author Michael Hamann <*****@*****.**>
 */
function act_sitemap($act)
{
    global $conf;
    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
        header("HTTP/1.0 404 Not Found");
        print "Sitemap generation is disabled.";
        exit;
    }
    $sitemap = Sitemapper::getFilePath();
    if (strrchr($sitemap, '.') === '.gz') {
        $mime = 'application/x-gzip';
    } else {
        $mime = 'application/xml; charset=utf-8';
    }
    // Check if sitemap file exists, otherwise create it
    if (!is_readable($sitemap)) {
        Sitemapper::generate();
    }
    if (is_readable($sitemap)) {
        // Send headers
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename=' . basename($sitemap));
        http_conditionalRequest(filemtime($sitemap));
        // Send file
        //use x-sendfile header to pass the delivery to compatible webservers
        if (http_sendfile($sitemap)) {
            exit;
        }
        readfile($sitemap);
        exit;
    }
    header("HTTP/1.0 500 Internal Server Error");
    print "Could not read the sitemap file - bad permissions?";
    exit;
}
Пример #10
0
 /**
  * Send a graphical error message and stop script
  */
 function gfx_error($type)
 {
     $file = dirname(__FILE__) . '/gfx/' . $type . '.gif';
     $time = filemtime($file);
     header('Content-type: image/gif');
     http_conditionalRequest($time);
     http_sendfile($file);
     readfile($file);
     exit;
 }
Пример #11
0
    $subdir = $subdirs[$i];
    $dir = realpath($base_dir . '/' . $subdir);
    $file = $dir . '/' . basename($file);
    if (!file_exists($file) || !stristr($dir, $base_dir) && !stristr($dir, "/usr/share/javascript") && !stristr($dir, "/usr/share/yui")) {
        if ($countFiles == 1) {
            header("HTTP/1.0 404 Not Found");
            echo 'Not Found';
            exit;
        }
        continue;
    }
    $newest_mdate = max(filemtime($file), $newest_mdate);
}
// This function quits the page load if the browser has a cached version of the requested script.
// It then returns a 304 Not Modified header
http_conditionalRequest($newest_mdate);
// here we need to send the script or stylesheet
$processed_files = 0;
for ($i = 0; $i < $countFiles; $i++) {
    $file = $files[$i];
    $subdir = $subdirs[$i];
    $dir = realpath($base_dir . '/' . $subdir);
    $file = $dir . '/' . basename($file);
    if (!file_exists($file) || !stristr($dir, $base_dir) && !stristr($dir, "/usr/share/javascript") && !stristr($dir, "/usr/share/yui") || !is_readable($file)) {
        continue;
    }
    $processed_files++;
    $fileinfo = pathinfo($file);
    switch ($fileinfo['extension']) {
        case 'css':
            $mime_type = 'text/css';
Пример #12
0
/**
 * Set headers and send the file to the client
 *
 * @author Andreas Gohr <*****@*****.**>
 * @author Ben Coburn <*****@*****.**>
 */
function sendFile($file, $mime, $dl, $cache)
{
    global $conf;
    $fmtime = @filemtime($file);
    // send headers
    header("Content-Type: {$mime}");
    // smart http caching headers
    if ($cache == -1) {
        // cache
        // cachetime or one hour
        header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
        header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
        header('Pragma: public');
    } else {
        if ($cache > 0) {
            // recache
            // remaining cachetime + 10 seconds so the newly recached media is used
            header('Expires: ' . gmdate("D, d M Y H:i:s", $fmtime + $conf['cachetime'] + 10) . ' GMT');
            header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($fmtime - time() + $conf['cachetime'] + 10, 0));
            header('Pragma: public');
        } else {
            if ($cache == 0) {
                // nocache
                header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
                header('Pragma: public');
            }
        }
    }
    //send important headers first, script stops here if '304 Not Modified' response
    http_conditionalRequest($fmtime);
    //download or display?
    if ($dl) {
        header('Content-Disposition: attachment; filename="' . basename($file) . '";');
    } else {
        header('Content-Disposition: inline; filename="' . basename($file) . '";');
    }
    //use x-sendfile header to pass the delivery to compatible webservers
    if (http_sendfile($file)) {
        exit;
    }
    //support download continueing
    header('Accept-Ranges: bytes');
    list($start, $len) = http_rangeRequest(filesize($file));
    // send file contents
    $fp = @fopen($file, "rb");
    if ($fp) {
        fseek($fp, $start);
        //seek to start of range
        $chunk = $len > CHUNK_SIZE ? CHUNK_SIZE : $len;
        while (!feof($fp) && $chunk > 0) {
            @set_time_limit(30);
            // large files can take a lot of time
            print fread($fp, $chunk);
            flush();
            $len -= $chunk;
            $chunk = $len > CHUNK_SIZE ? CHUNK_SIZE : $len;
        }
        fclose($fp);
    } else {
        header("HTTP/1.0 500 Internal Server Error");
        print "Could not read {$file} - bad permissions?";
    }
}
 /**
  * Sends a media file with its original filename
  * 
  * @see sendFile() in lib/exe/fetch.php
  */
 function _sendFile(&$event)
 {
     global $conf;
     global $MEDIA;
     $d = $event->data;
     $event->preventDefault();
     list($file, $mime, $dl, $cache) = array($d['file'], $d['mime'], $d['download'], $d['cache']);
     $fmtime = @filemtime($file);
     // send headers
     header("Content-Type: {$mime}");
     // smart http caching headers
     if ($cache == -1) {
         // cache
         // cachetime or one hour
         header('Expires: ' . gmdate('D, d M Y H:i:s', time() + max($conf['cachetime'], 3600)) . ' GMT');
         header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
         header('Pragma: public');
     } elseif ($cache > 0) {
         // recache
         // remaining cachetime + 10 seconds so the newly recached media is used
         header('Expires: ' . gmdate("D, d M Y H:i:s", $fmtime + $conf['cachetime'] + 10) . ' GMT');
         header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($fmtime - time() + $conf['cachetime'] + 10, 0));
         header('Pragma: public');
     } elseif ($cache == 0) {
         // nocache
         header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
         header('Pragma: public');
     }
     // send important headers first, script stops here if '304 Not Modified' response
     http_conditionalRequest($fmtime);
     // retrieve original filename and send Content-Disposition header
     $filename = $this->_getOriginalFileName($MEDIA);
     if ($filename === false) {
         $filename = utf8_decodeFN($this->common->_correctBasename($d['file']));
     }
     header($this->common->_buildContentDispositionHeader($dl, $filename));
     // use x-sendfile header to pass the delivery to compatible webservers
     if (http_sendfile($file)) {
         exit;
     }
     // send file contents
     $fp = @fopen($file, 'rb');
     if ($fp) {
         http_rangeRequest($fp, filesize($file), $mime);
     } else {
         header('HTTP/1.0 500 Internal Server Error');
         print "Could not read {$file} - bad permissions?";
     }
 }
Пример #14
0
/**
 * Handle sitemap delivery
 *
 * @author Michael Hamann <*****@*****.**>
 *
 * @param string $act action command
 */
function act_sitemap($act)
{
    global $conf;
    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
        http_status(404);
        print "Sitemap generation is disabled.";
        exit;
    }
    $sitemap = Sitemapper::getFilePath();
    if (Sitemapper::sitemapIsCompressed()) {
        $mime = 'application/x-gzip';
    } else {
        $mime = 'application/xml; charset=utf-8';
    }
    // Check if sitemap file exists, otherwise create it
    if (!is_readable($sitemap)) {
        Sitemapper::generate();
    }
    if (is_readable($sitemap)) {
        // Send headers
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename=' . utf8_basename($sitemap));
        http_conditionalRequest(filemtime($sitemap));
        // Send file
        //use x-sendfile header to pass the delivery to compatible webservers
        http_sendfile($sitemap);
        readfile($sitemap);
        exit;
    }
    http_status(500);
    print "Could not read the sitemap file - bad permissions?";
    exit;
}
Пример #15
0
/**
 * Output all needed JavaScript
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function js_out()
{
    global $conf;
    global $lang;
    global $config_cascade;
    // The generated script depends on some dynamic options
    $cache = getCacheName('scripts' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'], '.js');
    // array of core files
    $files = array(DOKU_INC . 'lib/scripts/helpers.js', DOKU_INC . 'lib/scripts/events.js', DOKU_INC . 'lib/scripts/delay.js', DOKU_INC . 'lib/scripts/cookie.js', DOKU_INC . 'lib/scripts/script.js', DOKU_INC . 'lib/scripts/tw-sack.js', DOKU_INC . 'lib/scripts/ajax.js', DOKU_INC . 'lib/scripts/index.js', DOKU_INC . 'lib/scripts/drag.js', DOKU_INC . 'lib/scripts/textselection.js', DOKU_INC . 'lib/scripts/toolbar.js', DOKU_INC . 'lib/scripts/edit.js', DOKU_INC . 'lib/scripts/linkwiz.js', DOKU_INC . 'lib/scripts/media.js', DOKU_INC . 'lib/scripts/subscriptions.js', DOKU_TPLINC . 'script.js');
    // add possible plugin scripts and userscript
    $files = array_merge($files, js_pluginscripts());
    if (isset($config_cascade['userscript']['default'])) {
        $files[] = $config_cascade['userscript']['default'];
    }
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (js_cacheok($cache, $files)) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the script
    ob_start();
    // add some global variables
    print "var DOKU_BASE   = '" . DOKU_BASE . "';";
    print "var DOKU_TPL    = '" . DOKU_TPL . "';";
    print "var DOKU_UHN    = " . (int) useHeading('navigation') . ";";
    print "var DOKU_UHC    = " . (int) useHeading('content') . ";";
    // load JS specific translations
    $json = new JSON();
    $lang['js']['plugins'] = js_pluginstrings();
    echo 'LANG = ' . $json->encode($lang['js']) . ";\n";
    // load toolbar
    toolbar_JSdefines('toolbar');
    // load files
    foreach ($files as $file) {
        echo "\n\n/* XXXXXXXXXX begin of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
        js_load($file);
        echo "\n\n/* XXXXXXXXXX end of " . str_replace(DOKU_INC, '', $file) . " XXXXXXXXXX */\n\n";
    }
    // init stuff
    js_runonstart("addEvent(document,'click',closePopups)");
    js_runonstart('addTocToggle()');
    js_runonstart("initSizeCtl('size__ctl','wiki__text')");
    js_runonstart("initToolbar('tool__bar','wiki__text',toolbar)");
    if ($conf['locktime'] != 0) {
        js_runonstart("locktimer.init(" . ($conf['locktime'] - 60) . ",'" . js_escape($lang['willexpire']) . "'," . $conf['usedraft'] . ")");
    }
    js_runonstart('scrollToMarker()');
    js_runonstart('focusMarker()');
    // init hotkeys - must have been done after init of toolbar
    # disabled for FS#1958    js_runonstart('initializeHotkeys()');
    // end output buffering and get contents
    $js = ob_get_contents();
    ob_end_clean();
    // compress whitespace and comments
    if ($conf['compress']) {
        $js = js_compress($js);
    }
    $js .= "\n";
    // https://bugzilla.mozilla.org/show_bug.cgi?id=316033
    // save cache file
    io_saveFile($cache, $js);
    if (function_exists('gzopen')) {
        io_saveFile("{$cache}.gz", $js);
    }
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($js, 9, FORCE_GZIP);
    } else {
        print $js;
    }
}
Пример #16
0
/**
 * Output all needed Styles
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function css_out()
{
    global $conf;
    global $lang;
    global $config_cascade;
    $mediatype = 'screen';
    if (isset($_REQUEST['s']) && in_array($_REQUEST['s'], array('all', 'print', 'feed'))) {
        $mediatype = $_REQUEST['s'];
    }
    $tpl = trim(preg_replace('/[^\\w-]+/', '', $_REQUEST['t']));
    if ($tpl) {
        $tplinc = DOKU_INC . 'lib/tpl/' . $tpl . '/';
        $tpldir = DOKU_BASE . 'lib/tpl/' . $tpl . '/';
    } else {
        $tplinc = DOKU_TPLINC;
        $tpldir = DOKU_TPL;
    }
    // The generated script depends on some dynamic options
    $cache = getCacheName('styles' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . DOKU_BASE . $tplinc . $mediatype, '.css');
    // load template styles
    $tplstyles = array();
    if (@file_exists($tplinc . 'style.ini')) {
        $ini = parse_ini_file($tplinc . 'style.ini', true);
        foreach ($ini['stylesheets'] as $file => $mode) {
            $tplstyles[$mode][$tplinc . $file] = $tpldir;
        }
    }
    // Array of needed files and their web locations, the latter ones
    // are needed to fix relative paths in the stylesheets
    $files = array();
    // load core styles
    $files[DOKU_INC . 'lib/styles/' . $mediatype . '.css'] = DOKU_BASE . 'lib/styles/';
    // load plugin styles
    $files = array_merge($files, css_pluginstyles($mediatype));
    // load template styles
    if (isset($tplstyles[$mediatype])) {
        $files = array_merge($files, $tplstyles[$mediatype]);
    }
    // if old 'default' userstyle setting exists, make it 'screen' userstyle for backwards compatibility
    if (isset($config_cascade['userstyle']['default'])) {
        $config_cascade['userstyle']['screen'] = $config_cascade['userstyle']['default'];
    }
    // load user styles
    if (isset($config_cascade['userstyle'][$mediatype])) {
        $files[$config_cascade['userstyle'][$mediatype]] = DOKU_BASE;
    }
    // load rtl styles
    // @todo: this currently adds the rtl styles only to the 'screen' media type
    //        but 'print' and 'all' should also be supported
    if ($mediatype == 'screen') {
        if ($lang['direction'] == 'rtl') {
            if (isset($tplstyles['rtl'])) {
                $files = array_merge($files, $tplstyles['rtl']);
            }
        }
    }
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (css_cacheok($cache, array_keys($files), $tplinc)) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the stylesheet
    ob_start();
    // print the default classes for interwiki links and file downloads
    css_interwiki();
    css_filetypes();
    // load files
    foreach ($files as $file => $location) {
        print css_loadfile($file, $location);
    }
    // end output buffering and get contents
    $css = ob_get_contents();
    ob_end_clean();
    // apply style replacements
    $css = css_applystyle($css, $tplinc);
    // place all @import statements at the top of the file
    $css = css_moveimports($css);
    // compress whitespace and comments
    if ($conf['compress']) {
        $css = css_compress($css);
    }
    // save cache file
    io_saveFile($cache, $css);
    if (function_exists('gzopen')) {
        io_saveFile("{$cache}.gz", $css);
    }
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($css, 9, FORCE_GZIP);
    } else {
        print $css;
    }
}
Пример #17
0
/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <*****@*****.**>
 */
if (!defined('DOKU_INC')) {
    define('DOKU_INC', dirname(__FILE__) . '/../../../');
}
define('NOSESSION', true);
require_once DOKU_INC . 'inc/init.php';
// let the syntax plugin do the work
$data = $_REQUEST;
$plugin = plugin_load('syntax', 'ditaa');
$cache = $plugin->_imgfile($data);
if (!$cache) {
    _fail();
}
header('Content-Type: image/png;');
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + max($conf['cachetime'], 3600)) . ' GMT');
header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . max($conf['cachetime'], 3600));
header('Pragma: public');
http_conditionalRequest($time);
echo io_readFile($cache, false);
function _fail()
{
    header("HTTP/1.0 404 Not Found");
    header('Content-Type: image/png');
    echo io_readFile('broken.png', false);
    exit;
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
Пример #18
0
/**
 * Output all needed Styles
 *
 * @author Andreas Gohr <*****@*****.**>
 */
function css_out()
{
    global $conf;
    global $lang;
    switch ($_REQUEST['s']) {
        case 'all':
        case 'print':
        case 'feed':
            $style = $_REQUEST['s'];
            break;
        default:
            $style = '';
            break;
    }
    $tpl = trim(preg_replace('/[^\\w-]+/', '', $_REQUEST['t']));
    if ($tpl) {
        $tplinc = DOKU_INC . 'lib/tpl/' . $tpl . '/';
        $tpldir = DOKU_BASE . 'lib/tpl/' . $tpl . '/';
    } else {
        $tplinc = DOKU_TPLINC;
        $tpldir = DOKU_TPL;
    }
    // The generated script depends on some dynamic options
    $cache = getCacheName('styles' . $_SERVER['HTTP_HOST'] . $_SERVER['SERVER_PORT'] . DOKU_BASE . $tplinc . $style, '.css');
    // load template styles
    $tplstyles = array();
    if (@file_exists($tplinc . 'style.ini')) {
        $ini = parse_ini_file($tplinc . 'style.ini', true);
        foreach ($ini['stylesheets'] as $file => $mode) {
            $tplstyles[$mode][$tplinc . $file] = $tpldir;
        }
    }
    // Array of needed files and their web locations, the latter ones
    // are needed to fix relative paths in the stylesheets
    $files = array();
    //if (isset($tplstyles['all'])) $files = array_merge($files, $tplstyles['all']);
    if (!empty($style)) {
        $files[DOKU_INC . 'lib/styles/' . $style . '.css'] = DOKU_BASE . 'lib/styles/';
        // load plugin, template, user styles
        $files = array_merge($files, css_pluginstyles($style));
        if (isset($tplstyles[$style])) {
            $files = array_merge($files, $tplstyles[$style]);
        }
        $files[DOKU_CONF . 'user' . $style . '.css'] = DOKU_BASE;
    } else {
        $files[DOKU_INC . 'lib/styles/style.css'] = DOKU_BASE . 'lib/styles/';
        if ($conf['spellchecker']) {
            $files[DOKU_INC . 'lib/styles/spellcheck.css'] = DOKU_BASE . 'lib/styles/';
        }
        // load plugin, template, user styles
        $files = array_merge($files, css_pluginstyles('screen'));
        if (isset($tplstyles['screen'])) {
            $files = array_merge($files, $tplstyles['screen']);
        }
        if ($lang['direction'] == 'rtl') {
            if (isset($tplstyles['rtl'])) {
                $files = array_merge($files, $tplstyles['rtl']);
            }
        }
        $files[DOKU_CONF . 'userstyle.css'] = DOKU_BASE;
    }
    // check cache age & handle conditional request
    header('Cache-Control: public, max-age=3600');
    header('Pragma: public');
    if (css_cacheok($cache, array_keys($files), $tplinc)) {
        http_conditionalRequest(filemtime($cache));
        if ($conf['allowdebug']) {
            header("X-CacheUsed: {$cache}");
        }
        // finally send output
        if ($conf['gzip_output'] && http_gzip_valid($cache)) {
            header('Vary: Accept-Encoding');
            header('Content-Encoding: gzip');
            readfile($cache . ".gz");
        } else {
            if (!http_sendfile($cache)) {
                readfile($cache);
            }
        }
        return;
    } else {
        http_conditionalRequest(time());
    }
    // start output buffering and build the stylesheet
    ob_start();
    // print the default classes for interwiki links and file downloads
    css_interwiki();
    css_filetypes();
    // load files
    foreach ($files as $file => $location) {
        print css_loadfile($file, $location);
    }
    // end output buffering and get contents
    $css = ob_get_contents();
    ob_end_clean();
    // apply style replacements
    $css = css_applystyle($css, $tplinc);
    // compress whitespace and comments
    if ($conf['compress']) {
        $css = css_compress($css);
    }
    // save cache file
    io_saveFile($cache, $css);
    copy($cache, "compress.zlib://{$cache}.gz");
    // finally send output
    if ($conf['gzip_output']) {
        header('Vary: Accept-Encoding');
        header('Content-Encoding: gzip');
        print gzencode($css, 9, FORCE_GZIP);
    } else {
        print $css;
    }
}