Ejemplo n.º 1
0
function send_file_to_client($real_filename, $filename, $disposition = null, $send_name = false, $delete = false)
{
    if (!file_exists($real_filename)) {
        return false;
    }
    $content_type = get_mime_type($filename);
    if ($content_type == 'text/html') {
        $charset = '; charset=' . html_charset($real_filename);
    } elseif ($content_type == 'text/plain') {
        $charset = '; charset=' . text_charset($real_filename);
    } else {
        $charset = '';
    }
    if ($send_name) {
        if (preg_match('/[^\\x20-\\x7E]/', $filename) and strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
            $filename = urlencode($filename);
        }
        // Add quotes to filename if it contains spaces
        if (strpos($filename, ' ') !== false) {
            $filename = '"' . $filename . '"';
        }
        $filenameattr = '; filename=' . $filename;
        if (!isset($disposition)) {
            $disposition = 'attachment';
        }
    } else {
        $filenameattr = '';
    }
    header("Content-type: {$content_type}{$charset}");
    if (isset($disposition)) {
        header("Content-Disposition: {$disposition}{$filenameattr}");
    }
    header('Pragma:');
    header('Cache-Control: public');
    header('Content-length: ' . filesize($real_filename));
    $mtime = filemtime($real_filename);
    $mdate = gmdate('D, d M Y H:i:s', $mtime);
    $etag = md5($real_filename . $mdate . $filename . filesize($real_filename));
    header('Last-Modified: ' . $mdate . ' GMT');
    header("Etag: {$etag}");
    if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) and strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) >= $mtime or array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) and trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
        header("HTTP/1.0 304 Not Modified");
    } else {
        stop_output_buffering();
        if ($delete) {
            register_shutdown_function('unlink', $real_filename);
        }
        readfile($real_filename);
    }
    return true;
}
Ejemplo n.º 2
0
 * be useful (without any warranty), under the terms of the GNU (General
 * Public License) as published by the Free Software Foundation.
 * The full license can be read in "/info/license/license_gpl.txt".
 *
 * Contact address: GUnet Asynchronous eLearning Group,
 *                  Network Operations Center, University of Athens,
 *                  Panepistimiopolis Ilissia, 15784, Athens, Greece
 *                  e-mail: info@openeclass.org
 * ======================================================================== */
define('UPGRADE', true);
require '../include/baseTheme.php';
require_once 'include/lib/fileUploadLib.inc.php';
require_once 'include/lib/forcedownload.php';
require_once 'include/phpass/PasswordHash.php';
require_once 'upgradeHelper.php';
stop_output_buffering();
// set default storage engine
Database::get()->query("SET storage_engine = InnoDB");
require_once 'upgrade/functions.php';
set_time_limit(0);
if (php_sapi_name() == 'cli' and !isset($_SERVER['REMOTE_ADDR'])) {
    $command_line = true;
} else {
    $command_line = false;
}
load_global_messages();
if ($urlAppend[strlen($urlAppend) - 1] != '/') {
    $urlAppend .= '/';
}
// include_messages
require "lang/{$language}/common.inc.php";
Ejemplo n.º 3
0
function download_assignments($id)
{
    global $workPath, $course_code;
    $counter = Database::get()->querySingle('SELECT COUNT(*) AS count FROM assignment_submit WHERE assignment_id = ?d', $id)->count;
    if ($counter > 0) {
        $secret = work_secret($id);
        $filename = "{$course_code}_work_{$id}.zip";
        chdir($workPath);
        create_zip_index("{$secret}/index.html", $id);
        $zip = new PclZip($filename);
        $flag = $zip->create($secret, "work_{$id}", $secret);
        header("Content-Type: application/x-zip");
        header("Content-Disposition: attachment; filename={$filename}");
        stop_output_buffering();
        @readfile($filename);
        @unlink($filename);
        exit;
    } else {
        return false;
    }
}
Ejemplo n.º 4
0
function send_file_to_client($real_filename, $filename, $disposition = null, $send_name = false, $delete = false)
{
    if (!file_exists($real_filename)) {
        return false;
    }
    $content_type = get_mime_type($filename);
    if ($content_type == 'text/html') {
        $charset = '; charset=' . html_charset($real_filename);
    } elseif ($content_type == 'text/plain') {
        $charset = '; charset=' . text_charset($real_filename);
    } else {
        $charset = '';
    }
    if ($send_name) {
        if (preg_match('/[^\\x20-\\x7E]/', $filename) and strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
            $filename = urlencode($filename);
        }
        // Add quotes to filename if it contains spaces
        if (strpos($filename, ' ') !== false) {
            $filename = '"' . $filename . '"';
        }
        $filenameattr = '; filename=' . $filename;
        if (!isset($disposition)) {
            $disposition = 'attachment';
        }
    } else {
        $filenameattr = '';
    }
    header("Content-type: {$content_type}{$charset}");
    if (isset($disposition)) {
        header("Content-Disposition: {$disposition}{$filenameattr}");
    }
    header('Pragma:');
    header('Cache-Control: public');
    $mtime = filemtime($real_filename);
    $mdate = gmdate('D, d M Y H:i:s', $mtime);
    $etag = md5($real_filename . $mdate . $filename . filesize($real_filename));
    header('Last-Modified: ' . $mdate . ' GMT');
    header("Etag: {$etag}");
    if (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER) and strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) >= $mtime or array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) and trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
        header("HTTP/1.0 304 Not Modified");
    } else {
        if ($delete) {
            register_shutdown_function('unlink', $real_filename);
        }
        $size = filesize($real_filename);
        if (isset($_SERVER['HTTP_RANGE'])) {
            // error_log('http range ON: ' . $_SERVER['HTTP_RANGE']); // debug output in apache error.log
            // Parse the range header to get the byte offset
            $ranges = array_map('intval', explode('-', substr($_SERVER['HTTP_RANGE'], 6)));
            if (!$ranges[1]) {
                // Second number missing, return from byte $range[0] to end
                $start = $ranges[0];
                $end = $size - 1;
            } else {
                // Both numbers present, return specific range
                $start = $ranges[0];
                $end = $ranges[1];
            }
            $length = $end - $start + 1;
            // Send the appropriate headers
            header('HTTP/1.1 206 Partial Content');
            header('Accept-Ranges: bytes');
            header('Content-Length: ' . $length);
            header(sprintf('Content-Range: bytes %d-%d/%d', $start, $end, $size));
            $f = fopen($real_filename, 'rb');
            // Open the file in binary mode
            $chunkSize = 8192;
            // The size of each chunk to output
            fseek($f, $start);
            // Seek to the requested start range
            stop_output_buffering();
            while ($length) {
                // Read in blocks of chunksize so we don't chew up memory on the server
                $read = $length > $chunkSize ? $chunkSize : $length;
                $length -= $read;
                echo fread($f, $read);
            }
            fclose($f);
        } else {
            // error_log('http range OFF'); // debug output in apache error.log
            header('Content-length: ' . $size);
            stop_output_buffering();
            readfile($real_filename);
        }
    }
    return true;
}