From c530a78a1c4591f52d3f08a9ac5465740696949e Mon Sep 17 00:00:00 2001 From: stronk7 Date: Thu, 25 Jun 2009 23:54:22 +0000 Subject: [PATCH] MDL-19579 code coverage - add file.php-like script to serve static reports from dataroot/codecoverage/[dbtest|unittest]. Requires login + site:config cap --- admin/report/unittest/coveragefile.php | 75 ++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 admin/report/unittest/coveragefile.php diff --git a/admin/report/unittest/coveragefile.php b/admin/report/unittest/coveragefile.php new file mode 100755 index 0000000000..982d128b21 --- /dev/null +++ b/admin/report/unittest/coveragefile.php @@ -0,0 +1,75 @@ +. + +/** + * This script serves files from dataroot/codecoverage + * + * You should use the get_file_url() function, available in lib/filelib.php, to link to file.php. + * This ensures proper formatting and offers useful options. + * Syntax: coveragefile.php/path/to/file/file.html + * coveragefile.php?file=path/to/file/file.html + * + * @package moodlecore + * @subpackage simpletestcoverage + * @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(__FILE__) . '/../../../config.php'); +require_once($CFG->libdir . '/filelib.php'); + +// basic security, require login + require site config cap +require_login(); +require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); + +// disable moodle specific debug messages +disable_debugging(); + +// get file requested +$relativepath = get_file_argument(); + +// basic check, start by slash +if (!$relativepath) { + print_error('invalidargorconf'); +} else if ($relativepath{0} != '/') { + print_error('pathdoesnotstartslash'); +} + +// determine which disk file is going to be served +// and how it's going to be named +$filepath = $CFG->dataroot . '/codecoverage' . $relativepath; +$filename = basename($filepath); + +// extract relative path components +$args = explode('/', ltrim($relativepath, '/')); + +// only serve from some controlled subdirs +$alloweddirs = array('dbtest', 'unittest'); +if (!isset($args[0]) || !in_array($args[0], $alloweddirs)) { + print_error('invalidarguments'); +} + +// only serve some controlled extensions +$allowedextensions = array('text/html', 'text/css', 'image/gif', 'application/x-javascript'); +if (!in_array(mimeinfo('type', $filepath), $allowedextensions)) { + print_error('invalidarguments'); +} + +// arrived here, send the file +session_get_instance()->write_close(); // unlock session during fileserving +send_file($filepath, $filename, 0, false); + -- 2.39.5