Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
/**
-
 
18
 * Custom Moodle helper collection for mustache.
-
 
19
 *
-
 
20
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
-
 
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
22
 */
-
 
23
 
16
 
Línea 24... Línea 17...
24
namespace core\output;
17
namespace core\output;
25
 
18
 
26
/**
19
/**
-
 
20
 * Custom Moodle helper collection for mustache.
27
 * Custom Moodle helper collection for mustache.
21
 *
28
 *
22
 * @package core
29
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
23
 * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
-
 
32
class mustache_helper_collection extends \Mustache_HelperCollection {
25
 */
33
 
26
class mustache_helper_collection extends \Mustache_HelperCollection {
34
    /**
27
    /**
35
     * @var string[] Names of helpers that aren't allowed to be called within other helpers.
28
     * @var string[] Names of helpers that aren't allowed to be called within other helpers.
Línea 71... Línea 64...
71
    public function add($name, $helper) {
64
    public function add($name, $helper) {
Línea 72... Línea 65...
72
 
65
 
Línea 73... Línea 66...
73
        $disallowedlist = $this->disallowednestedhelpers;
66
        $disallowedlist = $this->disallowednestedhelpers;
74
 
67
 
Línea 75... Línea 68...
75
        if (is_callable($helper) && !empty($disallowedlist)) {
68
        if (is_callable($helper) && !empty($disallowedlist)) {
76
            $helper = function($source, \Mustache_LambdaHelper $lambdahelper) use ($helper, $disallowedlist) {
69
            $helper = function ($source, \Mustache_LambdaHelper $lambdahelper) use ($helper, $disallowedlist) {
77
 
70
 
78
                // Temporarily override the disallowed helpers to return nothing
71
                // Temporarily override the disallowed helpers to return nothing
Línea 109... Línea 102...
109
        foreach ($names as $name) {
102
        foreach ($names as $name) {
110
            if ($this->has($name)) {
103
            if ($this->has($name)) {
111
                $function = $this->get($name);
104
                $function = $this->get($name);
112
                // Null out the helper. Must call parent::add here to avoid
105
                // Null out the helper. Must call parent::add here to avoid
113
                // a recursion problem.
106
                // a recursion problem.
114
                parent::add($name, function() {
107
                parent::add($name, function () {
115
                    return '';
108
                    return '';
116
                });
109
                });
Línea 117... Línea 110...
117
 
110
 
118
                $disabledhelpers[$name] = $function;
111
                $disabledhelpers[$name] = $function;
Línea 153... Línea 146...
153
        $endtoken = \Mustache_Tokenizer::T_END_SECTION;
146
        $endtoken = \Mustache_Tokenizer::T_END_SECTION;
154
        if ($endtoken == '/') {
147
        if ($endtoken == '/') {
155
            $endtoken = '\/';
148
            $endtoken = '\/';
156
        }
149
        }
Línea 157... Línea 150...
157
 
150
 
158
        $regexes = array_map(function($name) use ($starttoken, $endtoken) {
151
        $regexes = array_map(function ($name) use ($starttoken, $endtoken) {
159
            // We only strip out the name of the helper (excluding delimiters)
152
            // We only strip out the name of the helper (excluding delimiters)
160
            // the user is able to change the delimeters on a per template
153
            // the user is able to change the delimeters on a per template
161
            // basis so they may not be curly braces.
154
            // basis so they may not be curly braces.
162
            return '/\s*' . $starttoken . '\s*'. $name . '\W+.*' . $endtoken . '\s*' . $name . '\s*/';
155
            return '/\s*' . $starttoken . '\s*' . $name . '\W+.*' . $endtoken . '\s*' . $name . '\s*/';
Línea 163... Línea 156...
163
        }, $disallowedlist);
156
        }, $disallowedlist);
164
 
157
 
165
        // This will strip out unwanted helpers from the $source string
158
        // This will strip out unwanted helpers from the $source string
166
        // before providing it to the original helper function.
159
        // before providing it to the original helper function.
167
        // E.g.
160
        // E.g.
168
        // Before:
161
        // Before:
169
        // "core, move, {{#js}} some nasty JS hack {{/js}}"
162
        // "core, move, {{#js}} some nasty JS hack {{/js}}"
170
        // After:
163
        // After:
171
        // "core, move, {{}}".
164
        // "core, move, {{}}".
172
        return preg_replace_callback($regexes, function() {
165
        return preg_replace_callback($regexes, function () {
173
            return '';
166
            return '';
Línea 174... Línea 167...
174
        }, $string);
167
        }, $string);