moodle/lib/ajax/getsiteadminbranch.php
Andrew Nicols 0bb56f2e31 MDL-42989 Admin: Clean output when building site administration tree for JS
Previously, any inappropriate whitespace found whilst building the admin
tree caused a JSON error and for the site administration tree retrieval to
fail.

This change ensures that access to the site administration tree is unbroken
for non-developers, whilst still alerting developers that there is an
issue.
2013-12-02 14:28:37 +08:00

63 lines
2.1 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file is used to deliver a branch from the site administration
* in XML format back to a page from an AJAX call
*
* @since 2.6
* @package core
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(dirname(__FILE__) . '/../../config.php');
// This should be accessed by only valid logged in user.
if (!isloggedin() or isguestuser()) {
die('Invalid access.');
}
// This identifies the type of the branch we want to get. Make sure it's SITE_ADMIN.
$branchtype = required_param('type', PARAM_INT);
if ($branchtype !== navigation_node::TYPE_SITE_ADMIN) {
die('Wrong node type passed.');
}
// Start capturing output in case of broken plugins.
ob_start();
$PAGE->set_context(context_system::instance());
$PAGE->set_url('/lib/ajax/getsiteadminbranch.php', array('type'=>$branchtype));
$sitenavigation = new settings_navigation_ajax($PAGE);
// Convert and output the branch as JSON.
$converter = new navigation_json();
$branch = $sitenavigation->get('root');
$output = ob_get_contents();
ob_end_clean();
if ($CFG->debugdeveloper && !empty($output)) {
throw new coding_exception('Unexpected output whilst building the administration tree. ' .
'This could be caused by trailing whitespace. Output received: ' .
var_export($output, true));
} else {
echo $converter->convert($branch);
}