public function test_external_format_string() { $settings = external_settings::get_instance(); $currentraw = $settings->get_raw(); $currentfilter = $settings->get_filter(); $settings->set_raw(true); $context = context_system::instance(); $test = '$$ \pi $$ <script>hi</script> <h3>there</h3>'; $correct = $test; $this->assertSame(external_format_string($test, $context->id), $correct); $settings->set_raw(false); $test = '$$ \pi $$<script>hi</script> <h3>there</h3>'; $correct = '$$ \pi $$hi there'; $this->assertSame(external_format_string($test, $context->id), $correct); $settings->set_raw($currentraw); $settings->set_filter($currentfilter); }
public function test_external_format_string() { $this->resetAfterTest(); $settings = external_settings::get_instance(); $currentraw = $settings->get_raw(); $currentfilter = $settings->get_filter(); // Enable multilang filter to on content and heading. filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_applies_to_strings('multilang', 1); $filtermanager = filter_manager::instance(); $filtermanager->reset_caches(); $settings->set_raw(true); $settings->set_filter(true); $context = context_system::instance(); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>!'; $correct = $test; $this->assertSame($correct, external_format_string($test, $context->id)); $settings->set_raw(false); $settings->set_filter(false); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>?'; $correct = 'ENFR hi there?'; $this->assertSame($correct, external_format_string($test, $context->id)); $settings->set_filter(true); $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>@'; $correct = 'EN hi there@'; $this->assertSame($correct, external_format_string($test, $context->id)); // Filters can be opted out. $test = '<span lang="en" class="multilang">EN</span><span lang="fr" class="multilang">FR</span> ' . '<script>hi</script> <h3>there</h3>%'; $correct = 'ENFR hi there%'; $this->assertSame($correct, external_format_string($test, $context->id, false, ['filter' => false])); $settings->set_raw($currentraw); $settings->set_filter($currentfilter); }
/** * Intercept some moodlewssettingXXX $_GET and $_POST parameter * that are related to the web service call and are not the function parameters */ protected function set_web_service_call_settings() { global $CFG; // Default web service settings. // Must be the same XXX key name as the external_settings::set_XXX function. // Must be the same XXX ws parameter name as 'moodlewssettingXXX'. $externalsettings = array('raw' => false, 'fileurl' => true, 'filter' => false); // Load the external settings with the web service settings. $settings = external_settings::get_instance(); foreach ($externalsettings as $name => $default) { $wsparamname = 'moodlewssetting' . $name; // Retrieve and remove the setting parameter from the request. $value = optional_param($wsparamname, $default, PARAM_BOOL); unset($_GET[$wsparamname]); unset($_POST[$wsparamname]); $functioname = 'set_' . $name; $settings->{$functioname}($value); } }
/** * Format the text to be returned properly as requested by the either the web service server, * either by an internally call. * The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton * All web service servers must set this singleton when parsing the $_GET and $_POST. * * <pre> * Options are the same that in {@link format_text()} with some changes in defaults to provide backwards compatibility: * trusted : If true the string won't be cleaned. Default false. * noclean : If true the string won't be cleaned only if trusted is also true. Default false. * nocache : If true the string will not be cached and will be formatted every call. Default false. * filter : Can be set to false to force filters off, else observes {@link external_settings}. * para : If true then the returned string will be wrapped in div tags. Default (different from format_text) false. * Default changed because div tags are not commonly needed. * newlines : If true then lines newline breaks will be converted to HTML newline breaks. Default true. * context : Not used! Using contextid parameter instead. * overflowdiv : If set to true the formatted text will be encased in a div with the class no-overflow before being * returned. Default false. * allowid : If true then id attributes will not be removed, even when using htmlpurifier. Default (different from * format_text) true. Default changed id attributes are commonly needed. * </pre> * * @param string $text The content that may contain ULRs in need of rewriting. * @param int $textformat The text format. * @param int $contextid This parameter and the next two identify the file area to use. * @param string $component * @param string $filearea helps identify the file area. * @param int $itemid helps identify the file area. * @param object/array $options text formatting options * @return array text + textformat * @since Moodle 2.3 */ function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid, $options = null) { global $CFG; // Get settings (singleton). $settings = external_settings::get_instance(); if ($settings->get_fileurl()) { require_once $CFG->libdir . "/filelib.php"; $text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid); } if (!$settings->get_raw()) { $options = (array) $options; // If context is passed in options, check that is the same to show a debug message. if (isset($options['context'])) { if (is_object($options['context']) && $options['context']->id != $contextid || !is_object($options['context']) && $options['context'] != $contextid) { debugging('Different contexts found in external_format_text parameters. $options[\'context\'] not allowed. Using $contextid parameter...', DEBUG_DEVELOPER); } } $options['filter'] = isset($options['filter']) && !$options['filter'] ? false : $settings->get_filter(); $options['para'] = isset($options['para']) ? $options['para'] : false; $options['context'] = context::instance_by_id($contextid); $options['allowid'] = isset($options['allowid']) ? $options['allowid'] : true; $text = format_text($text, $textformat, $options); $textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML). } return array($text, $textformat); }
/** * Format the text to be returned properly as requested by the either the web service server, * either by an internally call. * The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton * All web service servers must set this singleton when parsing the $_GET and $_POST. * * @param string $text The content that may contain ULRs in need of rewriting. * @param int $textformat The text format, by default FORMAT_HTML. * @param int $contextid This parameter and the next two identify the file area to use. * @param string $component * @param string $filearea helps identify the file area. * @param int $itemid helps identify the file area. * @return array text + textformat * @since Moodle 2.3 */ function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid) { global $CFG; // Get settings (singleton). $settings = external_settings::get_instance(); if ($settings->get_fileurl()) { require_once $CFG->libdir . "/filelib.php"; $text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid); } if (!$settings->get_raw()) { $textformat = FORMAT_HTML; // Force format to HTML when not raw. $text = format_text($text, $textformat, array('noclean' => true, 'para' => false, 'filter' => $settings->get_filter())); } return array($text, $textformat); }
/** * Format the text to be returned properly as requested by the either the web service server, * either by an internally call. * The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton * All web service servers must set this singleton when parsing the $_GET and $_POST. * * @param string $text The content that may contain ULRs in need of rewriting. * @param int $textformat The text format. * @param int $contextid This parameter and the next two identify the file area to use. * @param string $component * @param string $filearea helps identify the file area. * @param int $itemid helps identify the file area. * @return array text + textformat * @since Moodle 2.3 */ function external_format_text($text, $textformat, $contextid, $component, $filearea, $itemid) { global $CFG; // Get settings (singleton). $settings = external_settings::get_instance(); if ($settings->get_fileurl()) { require_once $CFG->libdir . "/filelib.php"; $text = file_rewrite_pluginfile_urls($text, $settings->get_file(), $contextid, $component, $filearea, $itemid); } if (!$settings->get_raw()) { $text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter())); $textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML). } return array($text, $textformat); }
require_once(__DIR__ . '/../../config.php'); require_once($CFG->libdir . '/externallib.php'); define('PREFERRED_RENDERER_TARGET', RENDERER_TARGET_GENERAL); $rawjson = file_get_contents('php://input'); $requests = json_decode($rawjson, true); if ($requests === null) { $lasterror = json_last_error_msg(); throw new coding_exception('Invalid json in request: ' . $lasterror); } $responses = array(); // Defines the external settings required for Ajax processing. $settings = external_settings::get_instance(); $settings->set_file('pluginfile.php'); $settings->set_fileurl(true); $settings->set_filter(true); $settings->set_raw(false); foreach ($requests as $request) { $response = array(); $methodname = clean_param($request['methodname'], PARAM_ALPHANUMEXT); $index = clean_param($request['index'], PARAM_INT); $args = $request['args']; $response = external_api::call_external_function($methodname, $args, true); $responses[$index] = $response; if ($response['error']) { // Do not process the remaining requests.