web.php 165 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
14
 * @copyright  (C) portions from Moodle, (C) Martin Dougiamas http://dougiamas.com
 */

defined('INTERNAL') || die();


15
16
17
function smarty_core() {
    require_once 'dwoo/dwoo/dwooAutoload.php';
    require_once 'dwoo/mahara/Dwoo_Mahara.php';
18

19
    return new Dwoo_Mahara();
20
21
22
}


23
24
25
26
27
28
29
30
31

/**
 * Function to set an optional page icon. Mahara uses fontawesome for icons by default,
 * (http://fortawesome.github.io/Font-Awesome/icons/) but this can be overridden at the theme
 * level by supplying a different icon font + css.
 *
 * @param Smarty | an initialized smarty object
 * @param String | the name of the icon to include (eg "icon-university")
 */
Pat Kira's avatar
Pat Kira committed
32
function setpageicon($smarty, $icon) {
33
34
35
36
37
38
    $smarty->assign('pageicon', 'icon ' . $icon);
}



/**
39
40
 * Helper function (called by smarty()) to determine what stylesheets to include
 * on the page (based on constants, global variables, and $extraconfig)
41
 *
42
43
 * @param $stylesheets Stylesheets we already know we're going to need
 * @param $extraconfig Extra configuration passed to smarty()
44
45
46
 * @return array
 */

Pat Kira's avatar
Pat Kira committed
47
function get_stylesheets_for_current_page($stylesheets, $extraconfig) {
48
49
50
51
52
53
54

    global $USER, $SESSION, $THEME, $HEADDATA, $langselectform;

    // stylesheet set up - if we're in a plugin also get its stylesheet
    $allstylesheets = $THEME->get_url('style/style.css', true);

    // determine if we want to include the parent css
Pat Kira's avatar
Pat Kira committed
55
    if (isset($THEME->overrideparentcss) && $THEME->overrideparentcss && $THEME->parent) {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        unset($allstylesheets[$THEME->parent]);
    }

    $stylesheets = array_merge($stylesheets, array_reverse(array_values($allstylesheets)));

    if (defined('SECTION_PLUGINTYPE') && defined('SECTION_PLUGINNAME') && SECTION_PLUGINTYPE != 'core') {
        if ($pluginsheets = $THEME->get_url('style/style.css', true, SECTION_PLUGINTYPE . '/' . SECTION_PLUGINNAME)) {
            $stylesheets = array_merge($stylesheets, array_reverse($pluginsheets));
        }
    }

    if ($adminsection = in_admin_section()) {
        if ($adminsheets = $THEME->get_url('style/admin.css', true)) {
            $stylesheets = array_merge($stylesheets, array_reverse($adminsheets));
        }
    }

    if (get_config('developermode') & DEVMODE_DEBUGCSS) {
        $stylesheets[] = get_config('wwwroot') . 'theme/debug.css';
    }

    // look for extra stylesheets
    if (isset($extraconfig['stylesheets']) && is_array($extraconfig['stylesheets'])) {
        foreach ($extraconfig['stylesheets'] as $extrasheet) {
            if ($sheets = $THEME->get_url($extrasheet, true)) {
                $stylesheets = array_merge($stylesheets, array_reverse(array_values($sheets)));
            }
        }
    }
85
86
87
88

    // Only add additional stylesheets when configurable theme is set.
    if ($THEME->basename == 'custom') {
        $sheets = $THEME->additional_stylesheets();
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        $stylesheets = array_merge($stylesheets, $sheets);
    }

    // Give the skin a chance to affect the page
    if (!empty($extraconfig['skin'])) {
        require_once(get_config('docroot').'/lib/skin.php');
        $skinobj = new Skin($extraconfig['skin']['skinid']);
        $viewid = isset($extraconfig['skin']['viewid']) ? $extraconfig['skin']['viewid'] : null;
        $stylesheets = array_merge($stylesheets, $skinobj->get_stylesheets($viewid));
    }

    $langdirection = get_string('thisdirection', 'langconfig');

    // Include rtl.css for right-to-left langs
    if ($langdirection == 'rtl') {
        $smarty->assign('LANGDIRECTION', 'rtl');
        if ($rtlsheets = $THEME->get_url('style/rtl.css', true)) {
            $stylesheets = array_merge($stylesheets, array_reverse($rtlsheets));
        }
    }


    $stylesheets = append_version_number($stylesheets);

    return $stylesheets;
}

Pat Kira's avatar
Pat Kira committed
116

117
118
119
120
/**
 * This function creates a Smarty object and sets it up for use within our
 * podclass app, setting up some variables.
 *
121
122
 * WARNING: If you are using pieforms, set them up BEFORE calling this function.
 *
123
124
 * The variables that it sets up are:
 *
125
 * - WWWROOT: The base url for the Mahara system
126
127
128
129
130
 * - USER: The user object
 * - JAVASCRIPT: A list of javascript files to include in the header.  This
 *   list is passed into this function (see below).
 * - HEADERS: An array of any further headers to set.  Each header is just
 *   straight HTML (see below).
131
132
 * - PUBLIC: Set true if this page is a public page
 * - MAINNAV: Array defining the main navigation
133
 *
134
 * @param $javascript A list of javascript includes.  Each include should be just
135
 *                    the name of a file, and reside in js/{filename}
136
137
138
 * @param $headers    A list of additional headers.  These are to be specified as
 *                    actual HTML.
 * @param $strings    A list of language strings required by the javascript code.
139
 * @return Dwoo_Mahara
140
 */
141

142
143


144
function smarty($javascript = array(), $headers = array(), $pagestrings = array(), $extraconfig = array()) {
Aaron Wells's avatar
Aaron Wells committed
145
    global $USER, $SESSION, $THEME, $HEADDATA, $langselectform, $CFG;
146
147
148
149
150
151
152
153
154
155
156

    if (!is_array($headers)) {
        $headers = array();
    }
    if (!is_array($pagestrings)) {
        $pagestrings = array();
    }
    if (!is_array($extraconfig)) {
        $extraconfig = array();
    }

157
    $sideblocks = array();
158
159
160
161
162
    // Some things like die_info() will try and create a smarty() call when we are already in one, which causes
    // language_select_form() to throw headdata error as it is called twice.
    if (!isset($langselectform)) {
        $langselectform = language_select_form();
    }
163
164
    $smarty = smarty_core();

165
    $wwwroot = get_config('wwwroot');
Aaron Wells's avatar
Aaron Wells committed
166
    // NOTE: not using jswwwroot - it seems to wreck image paths if you
167
    // drag them around the wysiwyg editor
168
    $jswwwroot = json_encode($wwwroot);
Martyn Smith's avatar
Martyn Smith committed
169

170
171
172
173
174
175
176
177
178
179
    // Workaround for $cfg->cleanurlusersubdomains.
    // When cleanurlusersubdomains is on, ajax requests might come from somewhere other than
    // the wwwroot.  To avoid cross-domain requests, set a js variable when this page is on a
    // different subdomain, and let the ajax wrapper function sendjsonrequest rewrite its url
    // if necessary.
    if (get_config('cleanurls') && get_config('cleanurlusersubdomains')) {
        if ($requesthost = get_requested_host_name()) {
            $wwwrootparts = parse_url($wwwroot);
            if ($wwwrootparts['host'] != $requesthost) {
                $fakewwwroot = $wwwrootparts['scheme'] . '://' . $requesthost . '/';
180
                $headers[] = '<script type="application/javascript">var fakewwwroot = ' . json_encode($fakewwwroot) . ';</script>';
181
182
183
184
            }
        }
    }

Martyn Smith's avatar
Martyn Smith committed
185
    $theme_list = array();
186
    $adminsection = in_admin_section();
Aaron Wells's avatar
Aaron Wells committed
187

188
189
    if (function_exists('pieform_get_headdata')) {
        $headers = array_merge($headers, pieform_get_headdata());
190
191
192
        if (!defined('PIEFORM_GOT_HEADDATA')) {
          define('PIEFORM_GOT_HEADDATA', 1);
        }
193
    }
194

195
196
197
    // Define the stylesheets array early so that javascript modules can add extras
    $stylesheets = array();

Aaron Wells's avatar
Aaron Wells committed
198
    // Insert the appropriate javascript tags
199
    $javascript_array = array();
200
    $jsroot = $wwwroot . 'js/';
201

202
203
    $langdirection = get_string('thisdirection', 'langconfig');

204
205
    // Make jQuery accessible with $j (Mochikit has $)
    $javascript_array[] = $jsroot . 'jquery/jquery.js';
206
    $headers[] = '<script type="application/javascript">$j=jQuery;</script>';
207

208
209
210
211
212
    // If necessary, load MathJax configuration
    if (get_config('mathjax')) {
        $headers[] = '<script type="application/javascript">'.get_config('mathjaxconfig').'</script>';
    }

Richard Mansfield's avatar
Richard Mansfield committed
213
    // TinyMCE must be included first for some reason we're not sure about
214
215
216
217
    //
    // Note: we do not display tinyMCE for mobile devices
    // as it doesn't work on some of them and can
    // disable the editing of a textarea field
218
    if (is_html_editor_enabled()) {
219
220
221
222
223
224
        $checkarray = array(&$javascript, &$headers);
        $found_tinymce = false;
        foreach ($checkarray as &$check) {
            if (($key = array_search('tinymce', $check)) !== false || ($key = array_search('tinytinymce', $check)) !== false) {
                if (!$found_tinymce) {
                    $found_tinymce = $check[$key];
225
                    $javascript_array[] = $wwwroot . 'artefact/file/js/filebrowser.js';
226
                    $javascript_array[] = $jsroot . 'switchbox.js';
227
228
                    $javascript_array[] = $jsroot . 'tinymce/tinymce.js';
                    $stylesheets = array_merge($stylesheets, array_reverse(array_values($THEME->get_url('style/tinymceskin.css', true))));
229
                    $content_css = json_encode($THEME->get_url('style/tinymce.css'));
230
231
                    $language = current_language();
                    $language = substr($language, 0, ((substr_count($language, '_') > 0) ? 5 : 2));
232
                    if ($language != 'en' && !file_exists(get_config('docroot') . 'js/tinymce/langs/' . $language . '.js')) {
233
234
235
236
237
238
239
240
                        // In case the language file exists as a string with both lower and upper case, eg fr_FR we test for this
                        $language = substr($language, 0, 2) . '_' . strtoupper(substr($language, 0, 2));
                        if (!file_exists(get_config('docroot') . 'js/tinymce/langs/' . $language . '.js')) {
                            // In case we fail to find a language of 5 chars, eg pt_BR (Portugese, Brazil) we try the 'parent' pt (Portugese)
                            $language = substr($language, 0, 2);
                            if ($language != 'en' && !file_exists(get_config('docroot') . 'js/tinymce/langs/' . $language . '.js')) {
                                $language = 'en';
                            }
241
                        }
242
243
                    }
                    $extrasetup = isset($extraconfig['tinymcesetup']) ? $extraconfig['tinymcesetup'] : '';
244
                    $extramceconfig = isset($extraconfig['tinymceconfig']) ? $extraconfig['tinymceconfig'] : '';
245

246
                    // Check whether to make the spellchecker available
247
                    if (get_config('tinymcespellcheckerengine')) {
248
                        $spellchecker = ',spellchecker';
249
                        $spellchecker_toolbar = '| spellchecker';
250
                        $spellchecker_config = "gecko_spellcheck : false, spellchecker_rpc_url : \"{$jsroot}tinymce/plugins/spellchecker/spellchecker.php\",";
251
252
                    }
                    else {
253
                        $spellchecker = $spellchecker_toolbar = '';
254
255
                        $spellchecker_config = 'gecko_spellcheck : true,';
                    }
256
257
                    $mathslate = (get_config('mathjax')) ? 'mathslate' : '';
                    $mathslateplugin = !empty($mathslate) ? ',' . $mathslate : '';
258
                    $toolbar = array(
259
                        null,
260
                        '"toolbar_toggle | formatselect | bold italic | bullist numlist | link unlink | imagebrowser | undo redo"',
261
                        '"underline strikethrough subscript superscript | alignleft aligncenter alignright alignjustify | outdent indent | forecolor backcolor | ltr rtl | fullscreen"',
262
                        '"fontselect | fontsizeselect | emoticons nonbreaking charmap ' . $mathslate . ' ' . $spellchecker_toolbar . ' | table | removeformat pastetext | code"',
263
264
265
266
267
268
                    );

                    // For right-to-left langs, reverse button order & align controls right.
                    $tinymce_langdir = $langdirection == 'rtl' ? 'rtl' : 'ltr';
                    $toolbar_align = 'left';

269
270
271
272
                    // Language strings required for TinyMCE
                    $pagestrings['mahara'] = isset($pagestrings['mahara']) ? $pagestrings['mahara'] : array();
                    $pagestrings['mahara'][] = 'attachedimage';

273
                    if ($check[$key] == 'tinymce') {
274
275
                        $tinymceconfig = <<<EOF
    theme: "modern",
276
    plugins: "tooltoggle,textcolor,visualblocks,wordcount,link,lists,imagebrowser,table,emoticons{$spellchecker},paste,code,fullscreen,directionality,searchreplace,nonbreaking,charmap{$mathslateplugin}",
277
    skin: 'light',
278
279
    toolbar1: {$toolbar[1]},
    toolbar2: {$toolbar[2]},
280
    toolbar3: {$toolbar[3]},
281
    menubar: false,
282
    fix_list_elements: true,
283
    image_advtab: true,
284
    table_style_by_css: true,
285
    {$spellchecker_config}
286
EOF;
287
288
                    }
                    else {
289
290
291
                        $tinymceconfig = <<<EOF
    selector: "textarea.tinywysiwyg",
    theme: "modern",
292
    skin: 'light',
293
294
    plugins: "fullscreen,autoresize",
    toolbar: {$toolbar[0]},
295
EOF;
296
                    }
297

298
                    $headers[] = <<<EOF
299
<script type="application/javascript">
300
tinyMCE.init({
301
302
    {$tinymceconfig}
    schema: 'html4',
303
304
305
306
307
308
309
310
311
    extended_valid_elements:
        "object[width|height|classid|codebase]"
        + ",param[name|value]"
        + ",embed[src|type|width|height|flashvars|wmode]"
        + ",script[src,type,language]"
        + ",ul[id|type|compact]"
        + ",iframe[src|width|height|name|scrolling|frameborder|allowfullscreen|webkitallowfullscreen|mozallowfullscreen|longdesc|marginheight|marginwidth|align|title|class|type]"
        + ",a[id|class|title|href|name]"
    ,urlconverter_callback : "custom_urlconvert",
312
    language: '{$language}',
313
    directionality: "{$tinymce_langdir}",
314
    content_css : {$content_css},
315
    font_formats: 'Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Open Sans=Open Sans;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;',
316
    remove_script_host: false,
317
    relative_urls: false,
318
    target_list: false,
Aaron Wells's avatar
Aaron Wells committed
319
    cache_suffix: '?v={$CFG->cacheversion}',
320
    {$extramceconfig}
321
    setup: function(ed) {
322
        ed.on('init', function(ed) {
323
            if (typeof(editor_to_focus) == 'string' && ed.editorId == editor_to_focus) {
324
325
326
                ed.focus();
            }
        });
327
328
329
        ed.on('keyup change', function (e) {
            checkTextareaMaxLength(ed.settings.id);
        });
330
331
        ed.on('LoadContent', function(e) {
            // Hide all the 2nd/3rd row menu buttons
332
            jQuery('.mce-toolbar.mce-first').siblings().addClass('hidden');
333
334
335
336
337
338
339
340
341
342
343
            // The tinymce fullscreen mode does not work properly in a transformed container div
            // such as div.vertcentre
            // and IE doesn't like a preset z-index
            // This work-around will remove/add classes: .vertcenter .configure .blockinstane
            // of the configure block div
            // when toggling fullscreen
            jQuery('div[aria-label="Fullscreen"]').on('click', function(e) {
                jQuery('div#configureblock').toggleClass('vertcentre');
                jQuery('div#configureblock').toggleClass('blockinstance');
                jQuery('div#configureblock').toggleClass('configure');
            });
344
        });
345
        {$extrasetup}
346
    }
347
});
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

function imageBrowserConfigSuccess(form, data) {
    // handle updates to file browser
    // final form submission handled by tinymce plugin
    if (data.formelementsuccess) {
        eval(data.formelementsuccess + '(form, data)');
        return;
    }
}

function imageBrowserConfigError(form, data) {
    if (data.formelementerror) {
        eval(data.formelementerror + '(form, data)');
        return;
    }
}

365
function custom_urlconvert (u, n, e) {
366
367
    // Don't convert the url on the skype status buttons.
    if (u.indexOf('skype:') == 0) {
368
      return u;
369
370
    }
    var t = tinyMCE.activeEditor, s = t.settings;
371

372
373
    // Don't convert link href since thats the CSS files that gets loaded into the editor also skip local file URLs
    if (!s.convert_urls || (e && e.nodeName == 'LINK') || u.indexOf('file:') === 0)
374
375
      return u;

376
377
    // Convert to relative
    if (s.relative_urls)
378
379
      return t.documentBaseURI.toRelative(u);

380
381
    // Convert to absolute
    u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
382

383
    return u;
384
}
385
386
387
</script>

EOF;
388
389
390
391
392
393
394
                    unset($check[$key]);
                }
                else {
                    if ($check[$key] != $found_tinymce) {
                        log_warn('Two differently configured tinyMCE instances have been asked for on this page! This is not possible');
                    }
                    unset($check[$key]);
395
                }
396
            }
397

398
399
400
401
            // If any page adds jquery explicitly, remove it from the list
            if (($key = array_search('jquery', $check)) !== false) {
                unset($check[$key]);
            }
402
        }
403
    }
404
405
406
407
408
409
410
411
    else {
        if (($key = array_search('tinymce', $javascript)) !== false || ($key = array_search('tinytinymce', $javascript)) !== false) {
            unset($javascript[$key]);
        }
        if (($key = array_search('tinymce', $headers)) !== false || ($key = array_search('tinytinymce', $headers)) !== false) {
            unset($headers[$key]);
        }
    }
Martyn Smith's avatar
Martyn Smith committed
412
    $javascript_array[] = $jsroot . 'keyboardNavigation.js';
413

414
415
416
417
418
    //If necessary, load MathJax path
    if (get_config('mathjax')) {
        $javascript_array[] = get_config('mathjaxpath');
    }

419
    $strings = array();
420
421
422
423
424
425
426
427
428
    foreach ($pagestrings as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $tag) {
                $strings[$tag] = get_raw_string($tag, $k);
            }
        }
        else {
            $strings[$k] = get_raw_string($k, $v);
        }
429
430
    }

431
    $jsstrings = jsstrings();
Martyn Smith's avatar
Martyn Smith committed
432
    $themepaths = themepaths();
433

Richard Mansfield's avatar
Richard Mansfield committed
434
    foreach ($javascript as $jsfile) {
435
436
437
438
        // For now, if there's no path in the js file, assume it's in
        // $jsroot and append '.js' to the name.  Later we may want to
        // ensure all smarty() calls include the full path to the js
        // file, with the proper extension.
439
        if (strpos($jsfile, '/') === false) {
440
            $javascript_array[] = $jsroot . $jsfile . '.js';
441
            if (isset($jsstrings[$jsfile])) {
442
443
444
                foreach ($jsstrings[$jsfile] as $section => $tags) {
                    foreach ($tags as $tag) {
                        $strings[$tag] = get_raw_string($tag, $section);
445
446
447
                    }
                }
            }
Martyn Smith's avatar
Martyn Smith committed
448
449
            if (isset($themepaths[$jsfile])) {
                foreach ($themepaths[$jsfile] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
450
                    $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
451
452
                }
            }
453
        }
454
        else if (stripos($jsfile, 'http://') === false && stripos($jsfile, 'https://') === false) {
455
            // A local .js file with a fully specified path
456
            $javascript_array[] = $wwwroot . $jsfile;
457
458
459
460
461
462
            // If $jsfile is from a plugin or plugin's block, i.e.:
            // - plugintype/pluginname/js/foo.js
            // - plugintype/pluginname/blocktype/pluginname/js/foo.js
            // Then get js strings from static function jsstrings in:
            // - plugintype/pluginname/lib.php, or
            // - plugintype/pluginname/blocktype/pluginname/lib.php
463
            $bits = explode('/', $jsfile);
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
            $pluginname = false;
            $plugintype = false;
            $jsfilename = false;
            if (count($bits) == 4 && $bits[2] == 'js' && in_array($bits[0], plugin_types())) {
                $plugintype = $bits[0];
                $pluginname = $bits[1];
                $jsfilename = $bits[3];
            }
            if (count($bits) == 6 && $bits[0] == 'artefact' && $bits[2] == 'blocktype' && $bits[4] == 'js') {
                $plugintype = 'blocktype';
                $pluginname = $bits[3];
                $jsfilename = $bits[5];
            }
            if ($pluginname) {
                safe_require($plugintype, $pluginname);
                $pluginclass = generate_class_name($plugintype, $pluginname);
                $name = substr($jsfilename, 0, strpos($jsfilename, '.js'));
481
482
                if (is_callable(array($pluginclass, 'jsstrings'))) {
                    $tempstrings = call_static_method($pluginclass, 'jsstrings', $name);
483
484
485
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
                            $strings[$tag] = get_raw_string($tag, $section);
486
487
                        }
                    }
Richard Mansfield's avatar
Richard Mansfield committed
488
                }
489
490
491
492
                if (is_callable(array($pluginclass, 'jshelp'))) {
                    $tempstrings = call_static_method($pluginclass, 'jshelp', $name);
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
493
                            $strings[$tag . '.help'] = get_help_icon($plugintype, $pluginname, null, null,
494
495
496
497
                                                                     null, $tag);
                        }
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
498
499
500
                if (is_callable(array($pluginclass, 'themepaths'))) {
                    $tmpthemepaths = call_static_method($pluginclass, 'themepaths', $name);
                    foreach ($tmpthemepaths as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
501
                        $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
502
503
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
504
            }
Martyn Smith's avatar
Martyn Smith committed
505
        }
506
507
508
509
        else {
            // A remote .js file
            $javascript_array[] = $jsfile;
        }
510
    }
511
512

    $javascript_array[] = $jsroot . 'mahara.js';
513
    $javascript_array[] = $jsroot . 'formchangechecker.js';
514
    $javascript_array[] = $jsroot . 'textareamaxlengthchecker.js';
515

516
517
518
    foreach ($jsstrings['mahara'] as $section => $tags) {
        foreach ($tags as $tag) {
            $strings[$tag] = get_raw_string($tag, $section);
519
520
        }
    }
521
522
    if (isset($extraconfig['themepaths']) && is_array($extraconfig['themepaths'])) {
        foreach ($extraconfig['themepaths'] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
523
            $theme_list[$themepath] = $THEME->get_url($themepath);
524
525
        }
    }
526

527
    $stringjs = '<script type="application/javascript">';
528
    $stringjs .= 'var strings = ' . json_encode($strings) . ';';
529
    $stringjs .= "\nfunction plural(n) { return " . get_raw_string('pluralrule', 'langconfig') . "; }\n";
530
531
    $stringjs .= '</script>';

532

533
534
535
    // Allow us to set the HTML lang attribute
    $smarty->assign('LANGUAGE', substr(current_language(), 0, 2));

536
    $smarty->assign('STRINGJS', $stringjs);
537

538
    $stylesheets = get_stylesheets_for_current_page($stylesheets, $extraconfig);
539

Son Nguyen's avatar
Son Nguyen committed
540
541
542
543
    // Disable CSS transforms, transitions, and animations when running behat tests
    if (defined('BEHAT_TEST')) {
        $stylesheets[] = get_config('wwwroot') . 'testing/frameworks/behat/no_transitions.css';
    }
544
    $smarty->assign('STYLESHEETLIST', $stylesheets);
545
546
    if (!empty($theme_list)) {
        // this gets assigned in smarty_core, but do it again here if it's changed locally
Aaron Wells's avatar
Aaron Wells committed
547
        $smarty->assign('THEMELIST', json_encode(array_merge((array)json_decode($smarty->get_template_vars('THEMELIST')),  $theme_list)));
548
    }
549

550
551
552
553
554
    $dropdownmenu = get_config('dropdownmenu');
    // disable drop-downs if overridden at institution level
    $sitethemeprefs = get_config('sitethemeprefs');
    $institutions = $USER->institutions;
    if (!empty($institutions)) {
555
556
557
558
        if (count($institutions) == 1) {
            $i = reset($institutions);
            if ($i->theme == $THEME->basename && $USER->institutiontheme->institutionname == $i->institution) {
                $dropdownmenu = $i->dropdownmenu;
559
            }
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
        }
        else {
            foreach ($institutions as $i) {
                if (!empty($sitethemeprefs)) {
                    if (!empty($USER->accountprefs['theme']) && $USER->accountprefs['theme'] == $THEME->basename . '/' . $i->institution) {
                        $dropdownmenu = $i->dropdownmenu;
                        break;
                    }
                }
                else {
                    if ((!empty($USER->accountprefs['theme']) && $USER->accountprefs['theme'] == $THEME->basename . '/' . $i->institution)
                        || (empty($USER->accountprefs) && $i->theme == $THEME->basename && $USER->institutiontheme->institutionname == $i->institution)) {
                        $dropdownmenu = $i->dropdownmenu;
                        break;
                    }
575
576
577
578
579
580
                }
            }
        }
    }

    // and/or disable drop-downs if a handheld device detected
581
    $dropdownmenu = $SESSION->get('handheld_device') ? false : $dropdownmenu && get_config('dropdownmenuenabled');
582

583
584
    if ($dropdownmenu) {
        $smarty->assign('DROPDOWNMENU', $dropdownmenu);
585
        $javascript_array[] = $jsroot . 'dropdown-nav.js';
586
    }
587

588
    $smarty->assign('MOBILE', $SESSION->get('mobile'));
589
    $smarty->assign('HANDHELD_DEVICE', $SESSION->get('handheld_device'));
590
591
592
593
594
595
596
597
598
    if (defined('FILEBROWSERS') ||
        (defined('SECTION_PAGE') && SECTION_PAGE == 'blocks')) {
        // Need to add the headers for select2 here so filebrowser has correct language
        require_once(get_config('libroot') . 'form/elements/autocomplete.php');
        $select2lang = pieform_element_autocomplete_language();
        $select2headdata = pieform_element_autocomplete_get_headdata();
        $headers = array_merge($headers, $select2headdata);
        $smarty->assign('select2_language', $select2lang);
    }
599

600
601
602
603
    $sitename = get_config('sitename');
    if (!$sitename) {
       $sitename = 'Mahara';
    }
604
    $smarty->assign('sitename', $sitename);
605
    $sitelogo = $THEME->header_logo();
606
    $sitelogo = append_version_number($sitelogo);
607
    $smarty->assign('sitelogo', $sitelogo);
608
609
    $smarty->assign('sitelogo4facebook', $THEME->facebook_logo());
    $smarty->assign('sitedescription4facebook', get_string('facebookdescription', 'mahara'));
610

Martyn Smith's avatar
Martyn Smith committed
611
    if (defined('TITLE')) {
612
        $smarty->assign('PAGETITLE', TITLE . ' - ' . $sitename);
Martyn Smith's avatar
Martyn Smith committed
613
614
    }
    else {
615
        $smarty->assign('PAGETITLE', $sitename);
Martyn Smith's avatar
Martyn Smith committed
616
    }
617
618
619
620
621
622
623
624
625
    if (defined('PAGEHEADING')) {
        $smarty->assign('PAGEHEADING', PAGEHEADING);
    }
    else {
        if (defined('TITLE')) {
            $smarty->assign('PAGEHEADING', TITLE);
        }
    }

626
627
628
    if (defined('SUBSECTIONHEADING')) {
        $smarty->assign('SUBSECTIONHEADING', SUBSECTIONHEADING);
    }
Martyn Smith's avatar
Martyn Smith committed
629

630
    $smarty->assign('PRODUCTIONMODE', get_config('productionmode'));
631
    if (function_exists('local_header_top_content')) {
632
        $sitetop = (isset($sitetop) ? $sitetop : '') . local_header_top_content();
633
634
635
    }
    if (isset($sitetop)) {
        $smarty->assign('SITETOP', $sitetop);
636
    }
637
638
639
    if (defined('PUBLIC')) {
        $smarty->assign('PUBLIC', true);
    }
640
641
642
    if (defined('ADMIN')) {
        $smarty->assign('ADMIN', true);
    }
643
644
645
    if (defined('INSTITUTIONALADMIN')) {
        $smarty->assign('INSTITUTIONALADMIN', true);
    }
646
647
648
649
650
651
    if (defined('STAFF')) {
        $smarty->assign('STAFF', true);
    }
    if (defined('INSTITUTIONALSTAFF')) {
        $smarty->assign('INSTITUTIONALSTAFF', true);
    }
652

653
    $smarty->assign('LOGGEDIN', $USER->is_logged_in());
654
655
656
657
658
659
660
    $publicsearchallowed = false;
    $searchplugin = get_config('searchplugin');
    if ($searchplugin) {
        safe_require('search', $searchplugin);
        $publicsearchallowed = (call_static_method(generate_class_name('search', $searchplugin), 'publicform_allowed') && get_config('publicsearchallowed'));
    }
    $smarty->assign('publicsearchallowed', $publicsearchallowed);
661
    if ($USER->is_logged_in()) {
662
        global $SELECTEDSUBNAV; // It's evil, but rightnav & mainnav stuff are now in different templates.
663
664
665
666
667
668
669
670
671
672
673
        if (in_array('raw_old', $THEME->inheritance)) {
            $menutype = (in_admin_section() ? 'adminnav' : null);
            $smarty->assign('MAINNAV', main_nav($menutype));
        }
        else {
            $smarty->assign('MAINNAV', main_nav());
            $is_admin = $USER->get('admin') || $USER->is_institutional_admin() || $USER->get('staff') || $USER->is_institutional_staff();
            if ($is_admin) {
                $smarty->assign('MAINNAVADMIN', main_nav('adminnav'));
            }
        }
674
        $mainnavsubnav = $SELECTEDSUBNAV;
675
        $smarty->assign('RIGHTNAV', right_nav());
676
677
678
679
680
681
682
683
684
        if (!$mainnavsubnav && $dropdownmenu) {
            // In drop-down navigation, the submenu is only usable if its parent is one of the top-level menu
            // items.  But if the submenu comes from something in right_nav (settings), it's unreachable.
            // Turning the submenu into SUBPAGENAV group-style tabs makes it usable.
            $smarty->assign('SUBPAGENAV', $SELECTEDSUBNAV);
        }
        else {
            $smarty->assign('SELECTEDSUBNAV', $SELECTEDSUBNAV);
        }
685
    }
686
    else {
687
        $smarty->assign('languageform', $langselectform);
688
    }
689
    $smarty->assign('FOOTERMENU', footer_menu());
690

691
    $smarty->assign('USER', $USER);
692
    $smarty->assign('SESSKEY', $USER->get('sesskey'));
693
    $smarty->assign('CC_ENABLED', get_config('cookieconsent_enabled'));
694
    $javascript_array = append_version_number($javascript_array);
695
    $smarty->assign('JAVASCRIPT', $javascript_array);
696
    $smarty->assign('RELEASE', get_config('release'));
697
    $smarty->assign('SERIES', get_config('series'));
698
    $smarty->assign('CACHEVERSION', get_config('cacheversion', 0));
699
    if (get_config('siteclosedforupgrade')) {
700
        $smarty->assign('SITECLOSED', 'logindisabled');
701
    }
702
    else if (get_config('siteclosedbyadmin')) {
703
        $smarty->assign('SITECLOSED', 'loginallowed');
704
    }
705

706
707
    if ((!isset($extraconfig['pagehelp']) || $extraconfig['pagehelp'] !== false)
        and $help = has_page_help()) {
708
709
710
        $smarty->assign('PAGEHELPNAME', $help[0]);
        $smarty->assign('PAGEHELPICON', $help[1]);
    }
711
    if (defined('GROUP')) {
712
        require_once('group.php');
713
714
715
716
717
718
        if ($group = group_current_group()) {
            $smarty->assign('GROUP', $group);
            if (!defined('NOGROUPMENU')) {
                $smarty->assign('SUBPAGENAV', group_get_menu_tabs());
                $smarty->assign('PAGEHEADING', $group->name);
            }
719
        }
720
    }
721

Martyn Smith's avatar
Martyn Smith committed
722
    // ---------- sideblock stuff ----------
723
724
    $sidebars = !isset($extraconfig['sidebars']) || $extraconfig['sidebars'] !== false;
    if ($sidebars && !defined('INSTALLER') && (!defined('MENUITEM') || substr(MENUITEM, 0, 5) != 'admin')) {
725
        if (get_config('installed') && !$adminsection) {
726
727
728
            $data = site_menu();
            if (!empty($data)) {
                $smarty->assign('SITEMENU', site_menu());
729
                $sideblocks[] = array(
730
                    'name'   => 'linksandresources',
731
732
733
734
735
736
                    'weight' => 10,
                    'data'   => $data,
                );
            }
        }

737
738
        if ($USER->is_logged_in() && defined('MENUITEM') &&
            (substr(MENUITEM, 0, 11) == 'myportfolio' || substr(MENUITEM, 0, 7) == 'content')) {
739
            if (get_config('showselfsearchsideblock')) {
740
                $sideblocks[] = array(
741
742
743
744
745
746
                    'name'   => 'selfsearch',
                    'weight' => 0,
                    'data'   => array(),
                );
            }
            if (get_config('showtagssideblock')) {
747
                $sideblocks[] = array(
748
749
750
751
752
753
                    'name'   => 'tags',
                    'id'     => 'sb-tags',
                    'weight' => 0,
                    'data'   => tags_sideblock(),
                );
            }
Clare Lenihan's avatar
Clare Lenihan committed
754
        }
Clare Lenihan's avatar
Clare Lenihan committed
755

756
        if ($USER->is_logged_in() && !$adminsection) {
757
            $sideblocks[] = array(
758
                'name'   => 'profile',
759
                'id'     => 'sb-profile',
760
                'class' => 'user-panel',
761
762
763
                'weight' => -20,
                'data'   => profile_sideblock()
            );
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
            $showusers = 2;
            $institutions = $USER->institutions;
            if (!empty($institutions)) {
                $showusers = 0;
                foreach ($institutions as $i) {
                    if ($i->showonlineusers == 2) {
                        $showusers = 2;
                        break;
                    }
                    if ($i->showonlineusers == 1) {
                        $showusers = 1;
                    }
                }
            }
            if (get_config('showonlineuserssideblock') && $showusers > 0) {
779
                $sideblocks[] = array(
780
                    'name'   => 'onlineusers',
781
                    'id'     => 'sb-onlineusers',
782
783
784
785
                    'weight' => -10,
                    'data'   => onlineusers_sideblock(),
                );
            }
786
            if (get_config('showprogressbar') && $USER->get_account_preference('showprogressbar')) {
787
                $sideblocks[] = array(
788
789
                    'name'   => 'progressbar',
                    'id'     => 'sb-progressbar',
790
                    'class'  => 'progressbar',
791
                    'weight' => -8,
792
793
794
795
796
797
                    'data'   => progressbar_sideblock(),
                );
            }
        }

        if ($USER->is_logged_in() && $adminsection && defined('SECTION_PAGE') && SECTION_PAGE == 'progressbar') {
798
            $sideblocks[] = array(
799
800
                'name'   => 'progressbar',
                'id'     => 'sb-progressbar',
801
                'class'  => 'progressbar',
802
                'weight' => -8,
803
804
                'data'   => progressbar_sideblock(true),
            );
805
        }
Martyn Smith's avatar
Martyn Smith committed
806

807
        $isloginblockvisible = !$USER->is_logged_in() && !get_config('siteclosedforupgrade')
808
809
                && get_config('showloginsideblock');
        if ($isloginblockvisible) {
810
            $sideblocks[] = array(
811
812
                'name'   => 'login',
                'weight' => -10,
813
                'id'     => 'sb-loginbox',
814
815
816
                'data'   => array(
                    'loginform' => auth_generate_login_form(),
                ),
817
818
            );
        }
819
        $smarty->assign('SHOWLOGINBLOCK', $isloginblockvisible);
820

821
822
823
        if (get_config('enablenetworking')) {
            require_once(get_config('docroot') .'api/xmlrpc/lib.php');
            if ($USER->is_logged_in() && $ssopeers = get_service_providers($USER->authinstance)) {
824
                $sideblocks[] = array(
825
826
827
828
829
                    'name'   => 'ssopeers',
                    'weight' => 1,
                    'data'   => $ssopeers,
                );
            }
Martyn Smith's avatar
Martyn Smith committed
830
831
        }

832
833
        if (isset($extraconfig['sideblocks']) && is_array($extraconfig['sideblocks'])) {
            foreach ($extraconfig['sideblocks'] as $sideblock) {
834
                $sideblocks[] = $sideblock;
835
836
            }
        }
Martyn Smith's avatar
Martyn Smith committed
837

838
        // local_sideblocks_update allows sites to customise the sideblocks by munging the $sideblocks array.
839
        if (function_exists('local_sideblocks_update')) {
840
            local_sideblocks_update($sideblocks);
841
842
        }

843
        usort($sideblocks, create_function('$a,$b', 'if ($a["weight"] == $b["weight"]) return 0; return ($a["weight"] < $b["weight"]) ? -1 : 1;'));
844

Aaron Wells's avatar
Aaron Wells committed
845
846
        // Place all sideblocks on the right. If this structure is munged
        // appropriately, you can put blocks on the left. In future versions of
847
        // Mahara, we'll make it easy to do this.
848
849
        $sidebars = $sidebars && !empty($sideblocks);
        $sideblocks = array('left' => array(), 'right' => $sideblocks);
850

851
        $smarty->assign('userauthinstance', $SESSION->get('authinstance'));
852
        $smarty->assign('MNETUSER', $SESSION->get('mnetuser'));
853
        $smarty->assign('SIDEBLOCKS', $sideblocks);
854
        $smarty->assign('SIDEBARS', $sidebars);
855

856
857
    }

858
859
860
    if (is_array($HEADDATA) && !empty($HEADDATA)) {
        $headers = array_merge($HEADDATA, $headers);
    }
861
    $smarty->assign('HEADERS', $headers);
862

863
864
    if ($USER->get('parentuser')) {
        $smarty->assign('USERMASQUERADING', true);
865
        $smarty->assign('masqueradedetails', get_string('youaremasqueradingas', 'mahara', display_name($USER)));
866
867
        $smarty->assign('becomeyoulink', hsc($wwwroot) . 'admin/users/changeuser.php?restore=1');
        $smarty->assign('becomeyouagain', get_string('becomeadminagain', 'admin', hsc($USER->get('parentuser')->name)));
868
    }
Martyn Smith's avatar
Martyn Smith committed
869

870
871
    // Define additional html content
    if (get_config('installed')) {
872
873
874
875
        $additionalhtmlitems = array(
            'ADDITIONALHTMLHEAD'      => get_config('additionalhtmlhead'),
            'ADDITIONALHTMLTOPOFBODY' => get_config('additionalhtmltopofbody'),
            'ADDITIONALHTMLFOOTER'    => get_config('additionalhtmlfooter')
876
877
        );
        if ($additionalhtmlitems) {
878
879
            foreach ($additionalhtmlitems as $name=>$content) {
                $smarty->assign($name, $content);
880
881
882
            }
        }
    }
883
884
885
886
887
888

    // If Cookie Consent is enabled, than define conent
    if (get_config('cookieconsent_enabled')) {
        require_once('cookieconsent.php');
        $smarty->assign('COOKIECONSENTCODE', get_cookieconsent_code());
    }
889
890
891
    return $smarty;
}

892
893
894
895

/**
 * Manages theme configuration.
 *
Aaron Wells's avatar
Aaron Wells committed
896
 * Does its best to give the user _a_ theme, even if it's not the theme they
897
898
899
900
901
902
903
904
905
 * want to use (e.g. the theme they want has been uninstalled)
 */
class Theme {

    /**
     * The base name of the theme (the name of the directory in which it lives)
     */
    public $basename = '';

906
907
908
909
910
    /**
     * A user may have had the header logo overridden by an institution
     */
    public $headerlogo;

911
912
913
914
915
    /**
     * Additional stylesheets to display after the basename theme's stylesheets
     */
    public $addedstylesheets;

916
917
918
919
920
921
922
923
924
925
    /**
     * A human-readable version of the theme name
     */
    public $displayname = '';

    /**
     * Which pieform renderer to use by default for all forms
     */
    public $formrenderer = '';

926
927
928
929
930
931
932
933
934
935
    /**
     * Directories where to look for templates by default
     */
    public $templatedirs = array();

    /**
     * Theme inheritance path from this theme to 'raw'
     */
    public $inheritance = array();

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
    /**
     * What unit the left/center/right column widths are in. 'pixels' or 'percent'
     */
    public $columnwidthunits    = '';

    /**
     * Width of the left column. Integer - see $columnwidthunits
     */
    public $leftcolumnwidth     = 256;

    /**
     * Background colour for the left column
     */
    public $leftcolumnbgcolor   = '#fff';

    /**
     * Background colour for the center column
     */
    public $centercolumnbgcolor = '#fff';

    /**
     * Width of the right column. Integer - see $columnwidthunits
     */
    public $rightcolumnwidth    = 256;

    /**
     * Background colour for the right column
     */
    public $rightcolumnbgcolor  = '#fff';

966
967
968
969
    /**
     * If the theme can use the svg image file format.
     */
    public $usesvg  = false;
970
971
972
973

    /**
     * Initialises a theme object based on the theme 'hint' passed.
     *
Aaron Wells's avatar
Aaron Wells committed
974
975
     * If arg is a string, it's taken to be a theme name. If it's a user
     * object, we ask it for a theme name. If it's an integer, we pretend
976
977
     * that's a user ID and ask for the theme for that user.
     *
Aaron Wells's avatar
Aaron Wells committed
978
     * If the theme they want doesn't exist, the object is initialised for the
979
980
981
982
983
984
985
986
     * default theme. This means you can initialise one of these for a user
     * and then use it without worrying if the theme exists.
     *
     * @param mixed $arg Theme name, user object or user ID
     */
    public function __construct($arg) {
        if (is_string($arg)) {
            $themename = $arg;
987
            $themedata = null;
988
989
        }
        else if ($arg instanceof User) {
990
            $themedata = $arg->get_themedata();
991
        }
992
993
994
995
996
997
998
999
1000
1001
1002
1003
        else if ($arg instanceof View) {
            $themename = $arg->get('theme');
            $themedata = null;
            $userid = $arg->get('owner');
            if ($userid) {
                $user = new User();
                $user->find_by_id($userid);
                $themedata = $user->get_themedata();
                $themedata->viewbasename = $themedata->basename;
                unset($themedata->basename);
            }
        }
1004
1005
1006
        else if (is_int($arg)) {
            $user = new User();
            $user->find_by_id($arg);
1007
            $themedata = $user->get_themedata();
1008
1009
1010
1011
1012
        }
        else {
            throw new SystemException("Argument to Theme::__construct was not a theme name, user object or user ID");
        }

1013
        if (isset($themedata) && isset($themedata->basename)) {
1014
1015
1016
            $themename = $themedata->basename;
        }

1017
        if (empty($themename)) {
1018
            // Theme to show to when no theme has been suggested
1019
1020
1021
            if (!$themename = get_config('theme')) {
                $themename = 'raw';
            }
1022
        }
1023
1024

        // check the validity of the name
1025
        if (!$this->name_is_valid($themename)) {
1026
1027
            throw new SystemException("Theme name is in invalid form: '$themename'");
        }
1028
1029

        $this->init_theme($themename, $themedata);
1030
1031
1032
1033