Example #1
0
function mobile_api_json_reply($arr)
{
    header('Access-Control-Allow-Origin: *');
    echo "(" . json_encode($arr) . ")";
    perf_to_log();
    exit;
}
Example #2
0
function mobile_api_json_reply($arr)
{
    header('Content-type: application/json');
    header('Pragma: no-cache');
    echo json_encode($arr);
    perf_to_log();
    exit;
}
/**
 * Dwoo {mahara_performance_info} function plugin
 *
 * Type:     function<br>
 * Name:     mahara_performance_info<br>
 * Date:     June 22, 2006<br>
 * Purpose:  Fetch internationalized strings
 * @author   Catalyst IT Ltd
 * @version  1.0
 * @return html to display in the footer.
 */
function Dwoo_Plugin_mahara_performance_info(Dwoo $dwoo)
{
    if (!get_config('perftofoot') && !get_config('perftolog')) {
        return;
    }
    $info = get_performance_info();
    $dwoo = smarty_core();
    foreach ($info as $key => $value) {
        if ($key == 'realtime') {
            $value = round($value, 3);
        }
        $dwoo->assign('perf_' . $key, $value);
    }
    // extras
    $dwoo->assign('perf_memory_total_display', display_size($info['memory_total']));
    $dwoo->assign('perf_memory_growth_display', display_size($info['memory_growth']));
    if (get_config('perftolog')) {
        perf_to_log($info);
    }
    if (get_config('perftofoot')) {
        return $dwoo->fetch('performancefooter.tpl');
    }
}
/**
 * This function sends a JSON message, and ends the script.
 *
 * Scripts receiving replies will recieve a JSON array with two fields:
 *
 *  - error: True or false depending on whether the request was successful
 *  - message: JSON data representing a message sent back from the script
 *
 * @param boolean $error   Whether the script ended in an error or not
 * @param string  $message A message to pass back to the user, can be an
 *                         array of JSON data
 */
function json_reply($error, $message, $returncode = 0)
{
    json_headers();
    echo json_encode(array('error' => $error, 'message' => $message, 'returnCode' => $returncode));
    perf_to_log();
    exit;
}
Example #5
0
/**
 * Redirects the browser to a new location. The path to redirect to can take
 * two forms:
 *
 * - http[something]: will redirect the user to that exact URL
 * - /[something]: will redirect to WWWROOT/[something]
 *
 * Any other form is illegal and will cause an error.
 *
 * @param string $location The location to redirect the user to. Defaults to
 *                         the application home page.
 */
function redirect($location = '/')
{
    $file = $line = null;
    if (headers_sent($file, $line)) {
        throw new SystemException("Headers already sent when redirect() was called (output started in {$file} on line {$line}");
    }
    if (substr($location, 0, 4) != 'http') {
        if (substr($location, 0, 1) != '/') {
            throw new SystemException('redirect() should be called with either' . ' /[something] for local redirects or http[something] for' . ' absolute redirects');
        }
        $location = get_config('wwwroot') . substr($location, 1);
    }
    header('HTTP/1.1 303 See Other');
    header('Location:' . $location);
    perf_to_log();
    exit;
}
Example #6
0
/**
 * Serves a file from dataroot.
 *
 * This function checks that the file is inside dataroot, but does not perform
 * any other checks. Authors using this function should make sure that their
 * scripts perform appropriate authentication.
 *
 * As an example: If the file is an artefact, you could ask for an artefact and
 * view ID, and check that the artefact is in the view and that the user can
 * view the view.
 *
 * @param string $path     The file to send. Must include the dataroot path.
 * @param string $filename The name of the file as the browser should use to
 *                         serve it.
 * @param string $mimetype Mime type to be sent in header
 * @param array  $options  Any options to use when serving the file. Currently
 *                         lifetime = 0 for no cache
 *                         forcedownload - force application rather than inline
 *                         overridecontenttype - send this instead of the mimetype
 *                         there are none.
 */
function serve_file($path, $filename, $mimetype, $options = array())
{
    $dataroot = realpath(get_config('dataroot'));
    $path = realpath($path);
    $options = array_merge(array('lifetime' => 86400), $options);
    if (!get_config('insecuredataroot') && substr($path, 0, strlen($dataroot)) != $dataroot) {
        throw new AccessDeniedException();
    }
    if (!file_exists($path)) {
        throw new NotFoundException();
    }
    session_write_close();
    // unlock session during fileserving
    $lastmodified = filemtime($path);
    $filesize = filesize($path);
    if ($mimetype == 'text/html' || $mimetype == 'text/xml' || $mimetype == 'application/xml' || $mimetype == 'application/xhtml+xml' || $mimetype == 'image/svg+xml') {
        if (isset($options['downloadurl']) && $filesize < 1024 * 1024) {
            display_cleaned_html(file_get_contents($path), $filename, $options);
            exit;
        }
        $options['forcedownload'] = true;
        $mimetype = 'application/octet-stream';
    }
    if (!$mimetype) {
        $mimetype = 'application/forcedownload';
    }
    if (ini_get('zlib.output_compression')) {
        ini_set('zlib.output_compression', 'Off');
    }
    // Try to disable automatic sid rewrite in cookieless mode
    @ini_set('session.use_trans_sid', 'false');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $lastmodified) . ' GMT');
    // @todo possibly need addslashes on the filename, but I'm unsure on exactly
    // how the browsers will handle it.
    if ($mimetype == 'application/forcedownload' || isset($options['forcedownload'])) {
        header('Content-Disposition: attachment; filename="' . $filename . '"');
    } else {
        header('Content-Disposition: inline; filename="' . $filename . '"');
    }
    header('X-Content-Type-Options: nosniff');
    if ($options['lifetime'] > 0 && !get_config('nocache')) {
        header('Cache-Control: max-age=' . $options['lifetime']);
        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $options['lifetime']) . ' GMT');
        header('Pragma: ');
        if ($mimetype != 'text/plain' && $mimetype != 'text/html' && !isset($fileoutput)) {
            @header('Accept-Ranges: bytes');
            if (!empty($_SERVER['HTTP_RANGE']) && strpos($_SERVER['HTTP_RANGE'], 'bytes=') !== FALSE) {
                // Byteserving stuff - for Acrobat Reader and download accelerators
                // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
                // inspired by: http://www.coneural.org/florian/papers/04_byteserving.php
                $ranges = false;
                if (preg_match_all('/(\\d*)-(\\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) {
                    foreach ($ranges as $key => $value) {
                        if ($ranges[$key][1] == '') {
                            // Suffix case
                            $ranges[$key][1] = $filesize - $ranges[$key][2];
                            $ranges[$key][2] = $filesize - 1;
                        } else {
                            if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) {
                                // Fix range length
                                $ranges[$key][2] = $filesize - 1;
                            }
                        }
                        if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) {
                            // Invalid byte-range ==> ignore header
                            $ranges = false;
                            break;
                        }
                        // Prepare multipart header
                        $ranges[$key][0] = "\r\n--" . BYTESERVING_BOUNDARY . "\r\nContent-Type: {$mimetype}\r\n";
                        $ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/{$filesize}\r\n\r\n";
                    }
                } else {
                    $ranges = false;
                }
                if ($ranges) {
                    byteserving_send_file($path, $mimetype, $ranges);
                }
            }
        } else {
            // Do not byteserve (disabled, strings, text and html files).
            header('Accept-Ranges: none');
        }
    } else {
        // Do not cache files in proxies and browsers
        if (is_https() === true) {
            //https sites - watch out for IE! KB812935 and KB316431
            header('Cache-Control: max-age=10');
            header('Expires: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
            header('Pragma: ');
        } else {
            //normal http - prevent caching at all cost
            header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
            header('Expires: ' . gmdate('D, d M Y H:i:s', 0) . ' GMT');
            header('Pragma: no-cache');
        }
        header('Accept-Ranges: none');
        // Do not allow byteserving when caching disabled
    }
    if ($mimetype == 'text/plain') {
        // Add encoding
        header('Content-Type: Text/plain; charset=utf-8');
    } else {
        if (isset($options['overridecontenttype'])) {
            header('Content-Type: ' . $options['overridecontenttype']);
        } else {
            header('Content-Type: ' . $mimetype);
        }
    }
    header('Content-Length: ' . $filesize);
    while (@ob_end_flush()) {
    }
    //flush the buffers - save memory and disable sid rewrite
    readfile_chunked($path);
    perf_to_log();
    exit;
}
Example #7
0
function readfile_exit($path)
{
    readfile($path);
    perf_to_log();
    exit;
}
Example #8
0
<?php

/**
 *
 * @package    mahara
 * @subpackage core
 * @author     Richard Mansfield
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */
define('INTERNAL', 1);
define('PUBLIC', 1);
define('NOCHECKREQUIREDFIELDS', 1);
require 'init.php';
header('Content-type: text/css');
if (!get_config('nocache')) {
    $maxage = 604800;
    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $maxage) . ' GMT');
    header('Cache-Control: max-age=' . $maxage);
    header('Pragma: public');
}
if ($style = param_integer('id', null)) {
    echo get_field('style', 'css', 'id', $style);
}
perf_to_log();