Example #1
0
function MailifyPage($pagehash, $oldpagehash = false)
{
    global $SERVER_ADMIN, $ArchivePageStore;
    $from = isset($SERVER_ADMIN) ? $SERVER_ADMIN : 'foo@bar';
    $head = "From {$from}  " . ctime(time()) . "\r\n";
    $head .= "Subject: " . encode_pagename_for_wikizip($pagehash['pagename']) . "\r\n";
    $head .= "From: {$from} (PhpWiki)\r\n";
    $head .= "Date: " . rfc1123date($pagehash['lastmodified']) . "\r\n";
    $head .= "Mime-Version: 1.0 (Produced by PhpWiki 1.1.x)\r\n";
    if (is_array($oldpagehash)) {
        return $head . MimeMultipart(array(MimeifyPage($oldpagehash), MimeifyPage($pagehash)));
    }
    return $head . MimeifyPage($pagehash);
}
/** the designated file is sent to the visitor
 *
 * This transmits the file {$CFG->datadir}$file from
 * the data directory to the visitor's browser, suggesting
 * the name $name. The file is transmitted in chunks 
 * (see {@link readfile_chunked()}).
 *
 * Several different variations are possible.
 *
 *  - by specifying a Time To Live of 0 seconds, this routine
 *    tries hard to defeat any caching by proxies
 *
 *  - if the download flag is TRUE, this routine tries to
 *    prevent the visitor's browser to render the file in-line
 *    suggesting downloading instead
 *
 * Quirks
 *
 *  - There appears to be a problem with Internet Explorer and https://
 *    and caching which requires a specific workaround. We simply check
 *    for 'https:' or 'http'.
 *
 *  - Adobe Acrobat Reader has a bad track record of infecting
 *    user's computers with malware when PDF's are rendered in-line.
 *    Therefore we force download for that kind of files.
 *
 *  - It is not easy to determine the exact mime type of files
 *    without resorting to a complex shadow-filesystem or a metadata
 *    table in the database. Therefore we 'guess' the mime type, either
 *    based on the information provided by the fileinfo PHP-module, or
 *    simply based on the extension of $file (which is not very reliable,
 *    but we have to do _something_). See {@link get_mimetype()} for details.
 *
 * @param string $file name of the file to send relative to $CFG->datadir
 * @param string $name filename to suggest to the visitor/visitor's browser
 * @param string $mimetype the mime type of the file; if not specified we use an educated guess
 * @param int $ttl time to live (aka maximum age) in seconds, 0 implies file is not cacheable
 * @param bool $download if TRUE we try to force a download
 * @uses get_mimetype()
 */
function send_file_from_datadir($file, $name, $mimetype = '', $ttl = 86400, $download = FALSE)
{
    global $CFG;
    $path = $CFG->datadir . $file;
    $mtime = filemtime($path);
    $fsize = filesize($path);
    if (empty($mimetype)) {
        $mimetype = get_mimetype($path);
    }
    // Try to prevent inline rendering of PDF because of bugs in Adobe Reader
    $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
    if ($mimetype == 'application/pdf' || $ext == 'pdf') {
        $download = TRUE;
        $ttl = 0;
    }
    $headers = array();
    $headers['Last-Modified'] = rfc1123date($mtime);
    $headers['Content-Disposition'] = sprintf('%s; filename=%s', $download ? 'attachment' : 'inline', urlencode($name));
    $headers['Content-Type'] = $mimetype;
    $headers['Content-Length'] = $fsize;
    $headers['Accept-Ranges'] = 'none';
    if ($ttl > 0) {
        $headers['Cache-Control'] = sprintf('max-age=%d', $ttl);
        $headers['Expires'] = rfc1123date(time() + $ttl);
        $headers['Pragma'] = '';
    } else {
        if (strtolower(substr($CFG->www, 0, 6)) == 'https:') {
            $ttl = 10;
            $headers['Cache-Control'] = sprintf('max-age=%d', $ttl);
            $headers['Expires'] = rfc1123date(time() - 86400);
            // 24h in the past
            $headers['Pragma'] = '';
        } else {
            $headers['Cache-Control'] = 'private, must-revalidate, max-age=0';
            $headers['Expires'] = rfc1123date(time() - 86400);
            // 24h in the past
            $headers['Pragma'] = 'no-cache';
        }
    }
    foreach ($headers as $k => $v) {
        @header(trim($k . ': ' . $v));
    }
    $bytes = readfile_chunked($path);
    return $bytes;
}