Merge branch 'MDL-52136-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2016-03-02 05:25:57 +01:00
commit ce878562f3
12 changed files with 176 additions and 29 deletions

View File

@ -222,6 +222,27 @@ define([ 'core/mustache',
return '{{_s' + index + '}}';
};
/**
* Quote helper used to wrap content in quotes, and escape all quotes present in the content.
*
* @method quoteHelper
* @private
* @param {string} sectionText The text to parse the arguments from.
* @param {function} helper Used to render subsections of the text.
* @return {string}
*/
var quoteHelper = function(sectionText, helper) {
var content = helper(sectionText.trim(), this);
// Escape the {{ and the ".
// This involves wrapping {{, and }} in change delimeter tags.
content = content
.replace('"', '\\"')
.replace(/([\{\}]{2,3})/g, '{{=<% %>=}}$1<%={{ }}=%>')
;
return '"' + content + '"';
};
/**
* Add some common helper functions to all context objects passed to templates.
* These helpers match exactly the helpers available in php.
@ -239,6 +260,7 @@ define([ 'core/mustache',
context.str = function() { return stringHelper; };
context.pix = function() { return pixHelper; };
context.js = function() { return jsHelper; };
context.quote = function() { return quoteHelper; };
context.globals = { config : config };
context.currentTheme = themeName;
};

View File

@ -0,0 +1,55 @@
<?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/>.
/**
* Wrap content in quotes, and escape all quotes used.
*
* @package core
* @category output
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\output;
/**
* Wrap content in quotes, and escape all quotes used.
*
* @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mustache_quote_helper {
/**
* Wrap content in quotes, and escape all quotes used.
*
* Note: This helper is only compatible with the standard {{ }} delimeters.
*
* @param string $text The text to parse for arguments.
* @param Mustache_LambdaHelper $helper Used to render nested mustache variables.
* @return string
*/
public function quote($text, \Mustache_LambdaHelper $helper) {
// Split the text into an array of variables.
$content = trim($text);
$content = $helper->render($content);
// Escape the {{ and the ".
$content = str_replace('"', '\\"', $content);
$content = preg_replace('([{}]{2,3})', '{{=<% %>=}}${0}<%={{ }}=%>', $content);
return '"' . $content . '"';
}
}

View File

@ -76,4 +76,3 @@ class mustache_string_helper {
return get_string($key, $component, $a);
}
}

View File

@ -91,6 +91,7 @@ class renderer_base {
$loader = new \core\output\mustache_filesystem_loader();
$stringhelper = new \core\output\mustache_string_helper();
$quotehelper = new \core\output\mustache_quote_helper();
$jshelper = new \core\output\mustache_javascript_helper($this->page->requires);
$pixhelper = new \core\output\mustache_pix_helper($this);
@ -99,6 +100,7 @@ class renderer_base {
$helpers = array('config' => $safeconfig,
'str' => array($stringhelper, 'str'),
'quote' => array($quotehelper, 'quote'),
'js' => array($jshelper, 'help'),
'pix' => array($pixhelper, 'pix'));

View File

@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die();
*
* @property boolean $viewfullnames Whether to override fullname()
*/
class forum_post implements \renderable {
class forum_post implements \renderable, \templatable {
/**
* The course that the forum post is in.
@ -134,9 +134,65 @@ class forum_post implements \renderable {
* Export this data so it can be used as the context for a mustache template.
*
* @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments
* @param bool $plaintext Whethe the target is a plaintext target
* @return stdClass Data ready for use in a mustache template
*/
public function export_for_template(\mod_forum_renderer $renderer) {
public function export_for_template(\renderer_base $renderer, $plaintext = false) {
if ($plaintext) {
return $this->export_for_template_text($renderer);
} else {
return $this->export_for_template_html($renderer);
}
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments
* @return stdClass Data ready for use in a mustache template
*/
protected function export_for_template_text(\mod_forum_renderer $renderer) {
return array(
'id' => html_entity_decode($this->post->id),
'coursename' => html_entity_decode($this->get_coursename()),
'courselink' => html_entity_decode($this->get_courselink()),
'forumname' => html_entity_decode($this->get_forumname()),
'showdiscussionname' => html_entity_decode($this->get_showdiscussionname()),
'discussionname' => html_entity_decode($this->get_discussionname()),
'subject' => html_entity_decode($this->get_subject()),
'authorfullname' => html_entity_decode($this->get_author_fullname()),
'postdate' => html_entity_decode($this->get_postdate()),
// Format some components according to the renderer.
'message' => html_entity_decode($renderer->format_message_text($this->cm, $this->post)),
'attachments' => html_entity_decode($renderer->format_message_attachments($this->cm, $this->post)),
'canreply' => $this->canreply,
'permalink' => $this->get_permalink(),
'firstpost' => $this->get_is_firstpost(),
'replylink' => $this->get_replylink(),
'unsubscribediscussionlink' => $this->get_unsubscribediscussionlink(),
'unsubscribeforumlink' => $this->get_unsubscribeforumlink(),
'parentpostlink' => $this->get_parentpostlink(),
'forumindexlink' => $this->get_forumindexlink(),
'forumviewlink' => $this->get_forumviewlink(),
'discussionlink' => $this->get_discussionlink(),
'authorlink' => $this->get_authorlink(),
'authorpicture' => $this->get_author_picture(),
'grouppicture' => $this->get_group_picture(),
);
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \mod_forum_renderer $renderer The render to be used for formatting the message and attachments
* @return stdClass Data ready for use in a mustache template
*/
protected function export_for_template_html(\mod_forum_renderer $renderer) {
return array(
'id' => $this->post->id,
'coursename' => $this->get_coursename(),

View File

@ -191,7 +191,7 @@ class mod_forum_renderer extends plugin_renderer_base {
* @return string
*/
public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
$data = $post->export_for_template($this);
$data = $post->export_for_template($this, $this->target === RENDERER_TARGET_TEXTEMAIL);
return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
}

View File

@ -87,7 +87,10 @@
{{{ subject }}}
</div>
<div class="author">
{{# str }} bynameondate, forum, { "name": "<a target='_blank' href='{{{ authorlink }}}'>{{ authorfullname }}</a>", "date": "{{ postdate }}" } {{/ str }}
{{# str }} bynameondate, forum, {
"name": {{# quote }}<a target='_blank' href='{{{ authorlink }}}'>{{ authorfullname }}</a>{{/ quote }},
"date": {{# quote }}{{ postdate }}{{/ quote }}
} {{/ str }}
</div>
</td>
</tr>

View File

@ -45,14 +45,20 @@
{{{ coursename }}} -> {{# str }} forums, forum {{/ str }} -> {{{ forumname }}}{{# showdiscussionname }} -> {{{ discussionname }}} {{/ showdiscussionname }}
{{ permalink }}
{{{ subject }}}
{{# str }} bynameondate, forum, { "name": "{{{ authorfullname }}}", "date": "{{ postdate }}" } {{/ str }}
{{# str }} bynameondate, forum, {
"name": {{# quote }}{{{ authorfullname }}}{{/ quote }},
"date": {{# quote}}{{ postdate }}{{/ quote }}
} {{/ str }}
---------------------------------------------------------------------
{{{ message }}}
{{{ attachments }}}
---------------------------------------------------------------------
{{# canreply }}
{{# str }} postmailinfolink, forum, { "coursename": "{{{ coursename }}}", "replylink": "{{ replylink }}" } {{/ str }}
{{# str }} postmailinfolink, forum, {
"coursename": {{# quote }}{{{ coursename }}}{{/ quote }},
"replylink": {{# quote }}{{ replylink }}{{/ quote }}
} {{/ str }}
{{/ canreply }}
{{# unsubscribeforumlink }}
{{# str }} unsubscribelink, forum, {{{ unsubscribeforumlink }}} {{/ str }}

View File

@ -44,7 +44,7 @@
<div>
<a target="_blank" href="{{{ permalink }}}">{{{ subject }}}</a>
{{# str }} bynameondate, forum, {
"name": "<a target=\"_blank\" href=\"{{{ authorlink }}}\">{{ authorfullname }}</a>",
"date": "{{ postdate }}"
} {{/ str }}
"name": {{# quote }}<a target=\"_blank\" href=\"{{{ authorlink }}}\">{{ authorfullname }}</a>{{/ quote }},
"date": {{# quote }}{{ postdate }}{{/ quote }}
} {{/ str }}
</div>

View File

@ -33,5 +33,8 @@
}}
{{ discussionlink }}
{{{ subject }}} {{# str }} bynameondate, forum, { "name": "{{{ authorfullname }}}", "date": "{{ postdate }}" } {{/ str }}
{{{ subject }}} {{# str }} bynameondate, forum, {
"name": {{# quote }}{{{ authorfullname }}}{{/ quote }},
"date": {{# quote }}{{ postdate }}{{/ quote }}
} {{/ str }}
---------------------------------------------------------------------

View File

@ -38,7 +38,10 @@
{{ discussionlink }}
{{{ subject }}} ({{{ permalink }}})
{{# str }} bynameondate, forum, { "name": "{{{ authorfullname }}}", "date": "{{ postdate }}" } {{/ str }}
{{# str }} bynameondate, forum, {
"name": {{# quote }}{{{ authorfullname }}}{{/ quote }},
"date": {{# quote }}{{ postdate }}{{/ quote }}
} {{/ str }}
---------------------------------------------------------------------
{{{ message }}}

View File

@ -879,10 +879,8 @@ class mod_forum_mail_testcase extends advanced_testcase {
// Single and double quotes everywhere.
$newcase = $base;
$newcase['user']['lastname'] = 'Moodle\'';
// $newcase['user']['lastname'] = 'Moodle\'"'; // TODO: This breaks badly. See MDL-52136.
$newcase['course']['shortname'] = '101\'';
// $newcase['course']['shortname'] = '101\'"'; // TODO: This breaks badly. See MDL-52136.
$newcase['user']['lastname'] = 'Moodle\'"';
$newcase['course']['shortname'] = '101\'"';
$newcase['forums'][0]['name'] = 'Moodle Forum\'"';
$newcase['forums'][0]['forumposts'][0]['name'] = 'Hello Moodle\'"';
$newcase['forums'][0]['forumposts'][0]['message'] = 'Welcome to Moodle\'"';
@ -901,8 +899,8 @@ class mod_forum_mail_testcase extends advanced_testcase {
$newcase['forums'][0]['forumposts'][0]['name'] = 'Hello Moodle>';
$newcase['forums'][0]['forumposts'][0]['message'] = 'Welcome to Moodle>';
$newcase['expectations'][0]['contents'] = array(
'Attachment example.txt:', '~{\$a', '~&amp;gt;', 'Love Moodle>', '101&gt;', 'Moodle Forum&gt;',
'Hello Moodle&gt;', 'Welcome to Moodle>');
'Attachment example.txt:', '~{\$a', '~&amp;gt;', 'Love Moodle>', '101>', 'Moodle Forum>',
'Hello Moodle>', 'Welcome to Moodle>');
$textcases['Text mail with gt and lt everywhere'] = array('data' => $newcase);
// Ampersands everywhere. This case is completely borked because format_string()
@ -914,8 +912,8 @@ class mod_forum_mail_testcase extends advanced_testcase {
$newcase['forums'][0]['forumposts'][0]['name'] = 'Hello Moodle&';
$newcase['forums'][0]['forumposts'][0]['message'] = 'Welcome to Moodle&';
$newcase['expectations'][0]['contents'] = array(
'Attachment example.txt:', '~{\$a', '~&amp;amp;', 'Love Moodle&', '101&amp;', 'Moodle Forum&amp;',
'Hello Moodle&amp;', 'Welcome to Moodle&');
'Attachment example.txt:', '~{\$a', '~&amp;amp;', 'Love Moodle&', '101&', 'Moodle Forum&',
'Hello Moodle&', 'Welcome to Moodle&');
$textcases['Text mail with ampersands everywhere'] = array('data' => $newcase);
// Now the html cases.
@ -927,25 +925,23 @@ class mod_forum_mail_testcase extends advanced_testcase {
$htmlbase['expectations'][0]['contents'] = array(
'~{\$a',
'~&(amp|lt|gt|quot|\#039);(?!course)',
'<div class=3D"attachments">( *\n *)?<a href',
'<div class=3D"subject">\n.*Hello Moodle', '>Moodle Forum', '>Welcome.*Moodle', '>Love Moodle', '>1\d1');
'<div class="attachments">( *\n *)?<a href',
'<div class="subject">\n.*Hello Moodle', '>Moodle Forum', '>Welcome.*Moodle', '>Love Moodle', '>1\d1');
$htmlcases['HTML mail without ampersands, quotes or lt/gt'] = array('data' => $htmlbase);
// Single and double quotes, lt and gt, ampersands everywhere.
$newcase = $htmlbase;
$newcase['user']['lastname'] = 'Moodle\'>&';
// $newcase['user']['lastname'] = 'Moodle\'">&'; // TODO: This breaks badly. See MDL-52136.
$newcase['course']['shortname'] = '101\'>&';
// $newcase['course']['shortname'] = '101\'">&'; // TODO: This breaks badly. See MDL-52136.
$newcase['user']['lastname'] = 'Moodle\'">&';
$newcase['course']['shortname'] = '101\'">&';
$newcase['forums'][0]['name'] = 'Moodle Forum\'">&';
$newcase['forums'][0]['forumposts'][0]['name'] = 'Hello Moodle\'">&';
$newcase['forums'][0]['forumposts'][0]['message'] = 'Welcome to Moodle\'">&';
$newcase['expectations'][0]['contents'] = array(
'~{\$a',
'~&amp;(amp|lt|gt|quot|\#039);',
'<div class=3D"attachments">( *\n *)?<a href',
'<div class=3D"subject">\n.*Hello Moodle\'"&gt;&amp;', '>Moodle Forum\'"&gt;&amp;',
'>Welcome.*Moodle\'"&gt;&amp;', '>Love Moodle&\#039;&gt;&amp;', '>1\d1\'&gt;&amp');
'<div class="attachments">( *\n *)?<a href',
'<div class="subject">\n.*Hello Moodle\'"&gt;&amp;', '>Moodle Forum\'"&gt;&amp;',
'>Welcome.*Moodle\'"&gt;&amp;', '>Love Moodle&\#039;&quot;&gt;&amp;', '>101\'"&gt;&amp');
$htmlcases['HTML mail with quotes, gt, lt and ampersand everywhere'] = array('data' => $newcase);
return $textcases + $htmlcases;
@ -1062,6 +1058,7 @@ class mod_forum_mail_testcase extends advanced_testcase {
// If we have found the expectation and have contents to match, let's do it.
if (isset($foundexpectation) and isset($foundexpectation['contents'])) {
$mail->body = quoted_printable_decode($mail->body);
if (!is_array($foundexpectation['contents'])) { // Accept both string and array.
$foundexpectation['contents'] = array($foundexpectation['contents']);
}
@ -1069,6 +1066,7 @@ class mod_forum_mail_testcase extends advanced_testcase {
if (strpos($content, '~') !== 0) {
$this->assertRegexp('#' . $content . '#m', $mail->body);
} else {
preg_match('#' . substr($content, 1) . '#m', $mail->body, $matches);
$this->assertNotRegexp('#' . substr($content, 1) . '#m', $mail->body);
}
}