Esempio n. 1
0
            $fileinfo['filename'] .= '.' . $download["f_ext"];
        }
        // just count up a download
        if ($countonly) {
            $success = true;
            // just use built-in download
        } else {
            $fileinfo['path'] = PHPWCMS_ROOT . $phpwcms["file_path"];
            $fileinfo['filesize'] = $download['f_size'];
            $fileinfo['method'] = empty($phpwcms["inline_download"]) ? 'attachment' : 'inline';
            $fileinfo['mimetype'] = $download["f_type"];
            $fileinfo['file'] = $fileinfo['path'] . $fileinfo['filename'];
            $fileinfo['extension'] = $download["f_ext"];
            $fileinfo['realfname'] = $phpwcms['sanitize_dlname'] ? phpwcms_remove_accents($download["f_name"]) : $download["f_name"];
            // start download
            $success = dl_file_resume($fileinfo['file'], $fileinfo, true);
        }
    }
    // we hack in the stream.php here
} elseif ($file = isset($_GET['file']) ? clean_slweg($_GET['file'], 40) : '') {
    $filename = basename($file);
    $file = PHPWCMS_ROOT . '/' . PHPWCMS_FILES . $filename;
    if (is_file($file)) {
        $mime = empty($_GET['type']) ? '' : clean_slweg($_GET['type'], 100);
        if (!is_mimetype_format($mime)) {
            $mime = get_mimetype_by_extension(which_ext($file));
        }
        header('Content-Type: ' . $mime);
        if (BROWSER_OS == 'iOS') {
            require_once PHPWCMS_ROOT . '/include/inc_lib/functions.file.inc.php';
            rangeDownload($file);
Esempio n. 2
0
function do_download($formatter, $options)
{
    global $DBInfo;
    if (!$options['value']) {
        if (!function_exists('do_uploadedfiles')) {
            include_once dirname(__FILE__) . '/UploadedFiles.php';
        }
        do_uploadedfiles($formatter, $options);
        return;
    }
    $value =& $options['value'];
    $down_mode = (!empty($options['mode']) and $options['mode'][0] == 'a') ? 'attachment' : (!empty($DBInfo->download_mode) ? $DBInfo->download_mode : 'inline');
    // SubPage:foobar.png == SubPage/foobar.png
    // SubPage:thumbnails/foobar.png == SubPage/thumbnails/foobar.png
    // SubPage/FoobarPage:thumbnails/foobar.png == SubPage/FoobarPage/thumbnails/foobar.png
    // check acceptable subdirs
    $acceptable_subdirs = array('thumbnails');
    $tmp = explode('/', $value);
    $subdir = '';
    if (($c = count($tmp)) > 1) {
        if (in_array($tmp[$c - 2], $acceptable_subdirs)) {
            $subdir = $tmp[$c - 2] . '/';
            unset($tmp[$c - 2]);
            $value = implode('/', $tmp);
        }
    }
    if (($p = strpos($value, ':')) !== false or ($p = strrpos($value, '/')) !== false) {
        $subpage = substr($value, 0, $p);
        $file = substr($value, $p + 1);
        $value = $subpage . '/' . $file;
        # normalize page arg
        if ($subpage and $DBInfo->hasPage($subpage)) {
            $pagename =& $subpage;
            $key = $DBInfo->pageToKeyname($subpage);
        }
    }
    if (!isset($pagename[0])) {
        $pagename =& $formatter->page->name;
        $key = $DBInfo->pageToKeyname($formatter->page->name);
    }
    $prefix = '';
    if (isset($key[0])) {
        // for compatibility
        $dir = $DBInfo->upload_dir . '/' . $key;
        if (!is_dir($dir) and !empty($DBInfo->use_hashed_upload_dir)) {
            // support hashed upload_dir
            $prefix = get_hashed_prefix($key);
            $dir = $DBInfo->upload_dir . '/' . $prefix . $key;
        }
    }
    if ($value[0] == '/' or $key == 'UploadFile') {
        $dir = $DBInfo->upload_dir;
    }
    if (file_exists($dir)) {
        $handle = opendir($dir);
    } else {
        $dir = $DBInfo->upload_dir;
        $handle = opendir($dir);
    }
    $file = explode('/', $value);
    $file = $file[count($file) - 1];
    $params = $options;
    // copy request params
    /**
     * Thumbnail feature
     *
     * foo/bar/foo.png
     * - pagename = foo/bar
     * - attached image = foo.png
     * foo/bar/foo.png?thumb=1
     * - generate thumbnail with default width
     * foo/bar/foo.png?thumbwidth=320
     * - generate thumbnails/foo.w320.png
     *   if 320 is acceptable width
     * foo/bar/thumbnails/foo.w320.png
     * == foo/bar/foo.png?thumbwidth=320
     * foo/bar/foo.w320.png
     * == foo/bar/foo.png?thumbwidth=320
     * you can also upload foo.w320.png manually
     */
    // check thumbnail width from filename
    if (preg_match('@(\\.w(\\d+)\\.(png|jpe?g|gif))$@i', $file, $m)) {
        // drop w320 from given filename
        $orgfile = substr($file, 0, -strlen($m[1])) . '.' . $m[3];
        $params['thumbwidth'] = $m[2];
        unset($params['thumb']);
    }
    // check file exists
    $tmp = _l_filename($file);
    if (file_exists($dir . '/' . $subdir . $tmp)) {
        $_l_file = $subdir . $tmp;
        if (!empty($orgfile)) {
            unset($orgfile);
            // no need to generate thumbnails
            unset($params['thumbwidth']);
            $nothumb = true;
        }
    } else {
        $_l_file = !empty($orgfile) ? _l_filename($orgfile) : _l_filename($file);
        if (!file_exists("{$dir}/{$_l_file}")) {
            header("HTTP/1.1 404 Not Found");
            echo "File not found";
            return;
        }
    }
    $lines = @file($DBInfo->data_dir . '/mime.types');
    if ($lines) {
        foreach ($lines as $line) {
            rtrim($line);
            if (preg_match('/^\\#/', $line)) {
                continue;
            }
            $elms = preg_split('/\\s+/', $line);
            $type = array_shift($elms);
            foreach ($elms as $elm) {
                $mime[$elm] = $type;
            }
        }
    } else {
        $mime = array();
    }
    $realfile = $dir . '/' . $_l_file;
    # set filename
    if (preg_match("/\\.(.{1,4})\$/", $file, $match)) {
        $ext = strtolower($match[1]);
        $mimetype = !empty($mime[$ext]) ? $mime[$ext] : '';
        $ext = '.' . $ext;
    }
    // auto generate thumbnails
    if (empty($nothumb) and !empty($mimetype) and preg_match('@image/(png|jpe?g|gif)$@', $mimetype)) {
        list($w, $h) = getimagesize($realfile);
        $thumbfile = '';
        if (!empty($params['thumbwidth'])) {
            // check allowed thumb widths.
            $thumb_widths = isset($DBInfo->thumb_widths) ? $DBInfo->thumb_widths : array('120', '240', '320', '480', '600', '800', '1024');
            $width = 320;
            // default
            if (!empty($DBInfo->default_thumb_width)) {
                $width = $DBInfo->default_thumb_width;
            }
            if (!empty($thumb_widths)) {
                if (in_array($params['thumbwidth'], $thumb_widths)) {
                    $width = $params['thumbwidth'];
                } else {
                    header("HTTP/1.1 404 Not Found");
                    echo "Invalid thumbnail width", "<br />", "valid thumb widths are ", implode(', ', $thumb_widths);
                    return;
                }
            } else {
                $width = $params['thumbwidth'];
            }
            if ($w > $width) {
                $thumb_width = $width;
                $force_thumb = true;
            }
        } else {
            // automatically generate thumb images to support low-bandwidth mobile version
            if ($params['is_mobile']) {
                $force_thumb = (!isset($params['m']) or $params['m'] == 1);
            } else {
                if (!isset($params['thumb']) and !empty($DBInfo->max_image_width) and $w > $DBInfo->max_image_width) {
                    $force_thumb = true;
                    $thumb_width = $DBInfo->max_image_width;
                }
            }
        }
        while (!empty($params['thumb']) or $force_thumb) {
            if (empty($thumb_width)) {
                $thumb_width = 320;
                // default
                if (!empty($DBInfo->default_thumb_width)) {
                    $thumb_width = $DBInfo->default_thumb_width;
                }
            }
            $thumbfiles = array();
            $thumbname = preg_replace('@' . $ext . '$@i', '.w' . $thumb_width . $ext, $_l_file);
            $thumbfiles[] = $thumbname;
            $thumbfiles[] = 'thumbnails/' . $thumbname;
            foreach ($thumbfiles as $file) {
                $thumbfile = $dir . '/' . $file;
                if (file_exists($thumbfile)) {
                    $thumb_ok = true;
                    break;
                }
            }
            if ($thumb_ok) {
                break;
            }
            if ($w <= $thumb_width) {
                if (!empty($orgfile)) {
                    header("HTTP/1.1 404 Not Found");
                    echo "the thumbnail width have to smaller than original";
                    return;
                }
                $thumbfile = $realfile;
                break;
            }
            if (!file_exists($dir . "/thumbnails")) {
                @mkdir($dir . "/thumbnails", 0777);
            }
            require_once 'lib/mediautils.php';
            // generate thumbnail using the gd func or the ImageMagick(convert)
            resize_image($ext, $realfile, $thumbfile, $w, $h, $thumb_width);
            break;
        }
        if (!empty($thumbfile)) {
            $realfile = $thumbfile;
        }
    }
    if (empty($mimetype)) {
        $mimetype = "application/x-unknown";
    }
    if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        // IE: rawurlencode()
        $fn = preg_replace('/[:\\x5c\\/*?"<>|]/', '_', $file);
        $fname = 'filename="' . rawurlencode($fn) . '"';
        // fix IE bug
        $fname = preg_replace('/\\./', '%2e', $fname, substr_count($fname, '.') - 1);
        #header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        #header('Pragma: public');
    } else {
        if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mozilla')) {
            // Mozilla: RFC 2047
            $fname = 'filename="=?' . $DBInfo->charset . '?B?' . base64_encode($file) . '?="';
        } else {
            // etc. Safari, Opera 9: RFC 2231
            $fn = preg_replace('/[:\\x5c\\/{?]/', '_', $file);
            $fname = 'filename*=' . $DBInfo->charset . "''" . rawurlencode($fn) . '';
            //$fname='filename="'.$fn.'"';
        }
    }
    if (!empty($DBInfo->use_resume_download)) {
        $header = array("Content-Description: MoniWiki PHP Downloader");
        dl_file_resume($mimetype, $realfile, $fname, $down_mode, $header);
        return;
    }
    header("Content-Type: {$mimetype}\r\n");
    header("Content-Length: " . filesize($realfile));
    header("Content-Disposition: {$down_mode}; " . $fname);
    header("Content-Description: MoniWiki PHP Downloader");
    $mtime = filemtime($realfile);
    $lastmod = gmdate("D, d M Y H:i:s", $mtime) . ' GMT';
    $etag = md5($lastmod . $thumbfile);
    header("Last-Modified: " . $lastmod);
    header('ETag: "' . $etag . '"');
    header("Pragma:");
    $maxage = 60 * 60 * 24 * 7;
    header('Cache-Control: public, max-age=' . $maxage);
    $need = http_need_cond_request($mtime, $lastmod, $etag);
    if (!$need) {
        header('X-Cache-Debug: Cached OK');
        header('HTTP/1.0 304 Not Modified');
        @ob_end_clean();
        return;
    }
    $fp = readfile($realfile);
    return;
}