web.php 175 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
 * @copyright  (C) portions from Moodle, (C) Martin Dougiamas http://dougiamas.com
 */

12
use Mahara\Dwoo_Mahara as Dwoo_Mahara;
13 14 15
defined('INTERNAL') || die();


16
function smarty_core() {
17 18
    require_once(__DIR__ . '/dwoo/vendor/autoload.php');
    require_once(__DIR__ . '/dwoo/mahara/Dwoo_Mahara.php');
19

20
    return new Dwoo_Mahara();
21 22 23
}


24 25 26 27 28 29 30 31 32

/**
 * 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
33
function setpageicon($smarty, $icon) {
34 35 36 37 38 39
    $smarty->assign('pageicon', 'icon ' . $icon);
}



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

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

    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
56
    if (isset($THEME->overrideparentcss) && $THEME->overrideparentcss && $THEME->parent) {
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 85
        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)));
            }
        }
    }
86 87 88 89

    // Only add additional stylesheets when configurable theme is set.
    if ($THEME->basename == 'custom') {
        $sheets = $THEME->additional_stylesheets();
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;
}

116 117 118 119 120 121 122 123 124
/**
* True if we are not in admin, institution or admin section
*/
function user_personal_section() {
    $usersection = !defined('ADMIN') && !defined('STAFF') && !defined('INSTITUTIONALADMIN') &&
        !defined('INSTITUTIONALSTAFF') && !defined('GROUP') && !defined('CREATEGROUP');

    return $usersection ? 1 : 0;
}
Pat Kira's avatar
Pat Kira committed
125

126 127 128 129
/**
 * This function creates a Smarty object and sets it up for use within our
 * podclass app, setting up some variables.
 *
130 131
 * WARNING: If you are using pieforms, set them up BEFORE calling this function.
 *
132 133
 * The variables that it sets up are:
 *
134
 * - WWWROOT: The base url for the Mahara system
135 136 137 138 139
 * - 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).
140 141
 * - PUBLIC: Set true if this page is a public page
 * - MAINNAV: Array defining the main navigation
142
 *
143
 * @param $javascript A list of javascript includes.  Each include should be just
144
 *                    the name of a file, and reside in js/{filename}
145 146 147
 * @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.
148
 * @return Dwoo_Mahara
149
 */
150

151 152


153
function smarty($javascript = array(), $headers = array(), $pagestrings = array(), $extraconfig = array()) {
154
    global $USER, $SESSION, $THEME, $HEADDATA, $langselectform, $CFG, $viewid;
155 156 157 158 159 160 161 162 163 164 165

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

166
    $sideblocks = array();
167 168 169 170 171
    // 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();
    }
Gregor Anzelj's avatar
Gregor Anzelj committed
172 173 174 175 176 177
    // Now that password element can set headdata we need to call the login form before smarty_core()
    $isloginblockvisible = !$USER->is_logged_in() && !get_config('siteclosedforupgrade')
        && get_config('showloginsideblock');
    if ($isloginblockvisible) {
        $authgenerateloginform = auth_generate_login_form();
    }
178 179
    $smarty = smarty_core();

180
    $wwwroot = get_config('wwwroot');
Aaron Wells's avatar
Aaron Wells committed
181
    // NOTE: not using jswwwroot - it seems to wreck image paths if you
182
    // drag them around the wysiwyg editor
183
    $jswwwroot = json_encode($wwwroot);
Martyn Smith's avatar
Martyn Smith committed
184

185 186 187 188 189 190 191 192 193 194
    // 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 . '/';
195
                $headers[] = '<script type="application/javascript">var fakewwwroot = ' . json_encode($fakewwwroot) . ';</script>';
196 197 198 199
            }
        }
    }

Martyn Smith's avatar
Martyn Smith committed
200
    $theme_list = array();
201
    $adminsection = in_admin_section();
Aaron Wells's avatar
Aaron Wells committed
202

203 204
    if (function_exists('pieform_get_headdata')) {
        $headers = array_merge($headers, pieform_get_headdata());
205 206 207
        if (!defined('PIEFORM_GOT_HEADDATA')) {
          define('PIEFORM_GOT_HEADDATA', 1);
        }
208
    }
209

210 211 212
    // Define the stylesheets array early so that javascript modules can add extras
    $stylesheets = array();

Aaron Wells's avatar
Aaron Wells committed
213
    // Insert the appropriate javascript tags
214
    $javascript_array = array();
215
    $jsroot = $wwwroot . 'js/';
216

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

219 220
    // Make jQuery accessible with $j (Mochikit has $)
    $javascript_array[] = $jsroot . 'jquery/jquery.js';
221
    $headers[] = '<script type="application/javascript">$j=jQuery;</script>';
222

223 224 225 226 227
    // 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
228
    // TinyMCE must be included first for some reason we're not sure about
229 230 231 232
    //
    // 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
233
    if (is_html_editor_enabled()) {
234 235
        $checkarray = array(&$javascript, &$headers);
        $found_tinymce = false;
236 237 238 239 240 241 242 243 244
        $tinymceviewid = 'null';
        if ($inpersonalarea = user_personal_section()) {
            if (defined('SECTION_PAGE') && (SECTION_PAGE == 'view' || SECTION_PAGE == 'blocks' || SECTION_PAGE == 'editlayout')) {
                if (isset($viewid) && $viewid > 0) {
                    $tinymceviewid = $viewid;
                }
            }
        }

245 246 247 248
        foreach ($checkarray as &$check) {
            if (($key = array_search('tinymce', $check)) !== false || ($key = array_search('tinytinymce', $check)) !== false) {
                if (!$found_tinymce) {
                    $found_tinymce = $check[$key];
249
                    $javascript_array[] = $wwwroot . 'artefact/file/js/filebrowser.js';
250
                    $javascript_array[] = $jsroot . 'switchbox.js';
251 252
                    $javascript_array[] = $jsroot . 'tinymce/tinymce.js';
                    $stylesheets = array_merge($stylesheets, array_reverse(array_values($THEME->get_url('style/tinymceskin.css', true))));
253
                    $content_css = json_encode($THEME->get_url('style/tinymce.css'));
254 255
                    $language = current_language();
                    $language = substr($language, 0, ((substr_count($language, '_') > 0) ? 5 : 2));
256
                    if ($language != 'en' && !file_exists(get_config('docroot') . 'js/tinymce/langs/' . $language . '.js')) {
257 258 259 260 261 262 263 264
                        // 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';
                            }
265
                        }
266 267
                    }
                    $extrasetup = isset($extraconfig['tinymcesetup']) ? $extraconfig['tinymcesetup'] : '';
268
                    $extramceconfig = isset($extraconfig['tinymceconfig']) ? $extraconfig['tinymceconfig'] : '';
269

270
                    // Check whether to make the spellchecker available
271
                    if (get_config('tinymcespellcheckerengine')) {
272
                        $spellchecker = ',spellchecker';
273
                        $spellchecker_toolbar = '| spellchecker';
274
                        $spellchecker_config = "gecko_spellcheck : false, spellchecker_rpc_url : \"{$jsroot}tinymce/plugins/spellchecker/spellchecker.php\",";
275 276
                    }
                    else {
277
                        $spellchecker = $spellchecker_toolbar = '';
278 279
                        $spellchecker_config = 'gecko_spellcheck : true,';
                    }
280 281
                    $mathslate = (get_config('mathjax')) ? 'mathslate' : '';
                    $mathslateplugin = !empty($mathslate) ? ',' . $mathslate : '';
282
                    $toolbar = array(
283
                        null,
284
                        '"toolbar_toggle | formatselect | bold italic | bullist numlist | link unlink | imagebrowser | undo redo"',
285
                        '"underline strikethrough subscript superscript | alignleft aligncenter alignright alignjustify | outdent indent | forecolor backcolor | ltr rtl | fullscreen"',
286
                        '"fontselect | fontsizeselect | emoticons nonbreaking charmap ' . $mathslate . ' ' . $spellchecker_toolbar . ' | table | removeformat pastetext | anchor | code"',
287 288 289 290 291 292
                    );

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

293 294 295 296
                    // Language strings required for TinyMCE
                    $pagestrings['mahara'] = isset($pagestrings['mahara']) ? $pagestrings['mahara'] : array();
                    $pagestrings['mahara'][] = 'attachedimage';

297 298 299 300 301 302 303 304 305 306 307
                    $tinymceinitbehatsetup = '';
                    $tinymcebehatsetup = '';
                    if (defined('BEHAT_TEST')) {
                        $tinymceinitbehatsetup = 'window.isEditorInitializing = false;';
                        $tinymcebehatsetup = <<<EOF
        ed.on('PreInit', function(ed) {
            window.isEditorInitializing = true;
        });
EOF;
                    }

308
                    if ($check[$key] == 'tinymce') {
309 310
                        $tinymceconfig = <<<EOF
    theme: "modern",
311
    plugins: "tooltoggle,textcolor,visualblocks,wordcount,link,lists,imagebrowser,table,emoticons{$spellchecker},paste,code,fullscreen,directionality,searchreplace,nonbreaking,charmap{$mathslateplugin},anchor",
312
    skin: 'light',
313 314
    toolbar1: {$toolbar[1]},
    toolbar2: {$toolbar[2]},
315
    toolbar3: {$toolbar[3]},
316
    menubar: false,
317
    fix_list_elements: true,
318
    image_advtab: true,
319
    table_style_by_css: true,
320
    {$spellchecker_config}
321
EOF;
322 323
                    }
                    else {
324 325 326
                        $tinymceconfig = <<<EOF
    selector: "textarea.tinywysiwyg",
    theme: "modern",
327
    skin: 'light',
328 329
    plugins: "fullscreen,autoresize",
    toolbar: {$toolbar[0]},
330
EOF;
331
                    }
332

333
                    $headers[] = <<<EOF
334
<script type="application/javascript">
335
tinyMCE.init({
336 337
    {$tinymceconfig}
    schema: 'html4',
338 339 340 341 342 343 344 345
    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]"
346
        + ",button[id|class|title]"
347
    ,urlconverter_callback : "custom_urlconvert",
348
    language: '{$language}',
349
    directionality: "{$tinymce_langdir}",
350
    content_css : {$content_css},
351
    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;',
352
    remove_script_host: false,
353
    relative_urls: false,
354
    target_list: false,
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    link_list: function(success) {
        // Only show the list of links in the normal user section
        if ({$inpersonalarea}) {
            var params = {
                'viewid': {$tinymceviewid}
            }
            sendjsonrequest(config['wwwroot'] + 'json/tinymceviewlist.json.php',  params, 'POST', function(data) {
                if (data.count > 0) {
                    success(JSON.parse(data.data));
                }
                else {
                    success(''); // stop showing list with only option being 'none'
                }
            });
        }
        else {
            success(''); // stop showing list with only option being 'none'
        }
    },

Aaron Wells's avatar
Aaron Wells committed
375
    cache_suffix: '?v={$CFG->cacheversion}',
376
    {$extramceconfig}
377
    setup: function(ed) {
378
        {$tinymcebehatsetup}
379
        ed.on('init', function(ed) {
380
        {$tinymceinitbehatsetup}
381
            if (typeof(editor_to_focus) == 'string' && ed.editorId == editor_to_focus) {
382
                ed.trigger("focus");
383 384
            }
        });
385 386 387
        ed.on('keyup change', function (e) {
            checkTextareaMaxLength(ed.settings.id);
        });
388 389
        ed.on('LoadContent', function(e) {
            // Hide all the 2nd/3rd row menu buttons
390
            jQuery('.mce-toolbar.mce-first').siblings().addClass('hidden');
391 392 393 394 395 396 397 398 399 400 401
            // 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');
            });
402
        });
403
        {$extrasetup}
404
    }
405
});
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

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;
    }
}

423
function custom_urlconvert (u, n, e) {
424 425
    // Don't convert the url on the skype status buttons.
    if (u.indexOf('skype:') == 0) {
426
      return u;
427 428
    }
    var t = tinyMCE.activeEditor, s = t.settings;
429

430 431
    // 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)
432 433
      return u;

434 435
    // Convert to relative
    if (s.relative_urls)
436 437
      return t.documentBaseURI.toRelative(u);

438 439
    // Convert to absolute
    u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
440

441
    return u;
442
}
443 444 445
</script>

EOF;
446 447 448 449 450 451 452
                    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]);
453
                }
454
            }
455

456 457 458 459
            // If any page adds jquery explicitly, remove it from the list
            if (($key = array_search('jquery', $check)) !== false) {
                unset($check[$key]);
            }
460
        }
461
    }
462 463 464 465 466 467 468 469
    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
470
    $javascript_array[] = $jsroot . 'keyboardNavigation.js';
471

472 473 474 475 476
    //If necessary, load MathJax path
    if (get_config('mathjax')) {
        $javascript_array[] = get_config('mathjaxpath');
    }

477
    $strings = array();
478 479 480 481 482 483 484 485 486
    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);
        }
487 488
    }

489
    $jsstrings = jsstrings();
Martyn Smith's avatar
Martyn Smith committed
490
    $themepaths = themepaths();
491

Richard Mansfield's avatar
Richard Mansfield committed
492
    foreach ($javascript as $jsfile) {
493 494 495 496
        // 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.
497
        if (strpos($jsfile, '/') === false) {
498
            $javascript_array[] = $jsroot . $jsfile . '.js';
499
            if (isset($jsstrings[$jsfile])) {
500 501 502
                foreach ($jsstrings[$jsfile] as $section => $tags) {
                    foreach ($tags as $tag) {
                        $strings[$tag] = get_raw_string($tag, $section);
503 504 505
                    }
                }
            }
Martyn Smith's avatar
Martyn Smith committed
506 507
            if (isset($themepaths[$jsfile])) {
                foreach ($themepaths[$jsfile] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
508
                    $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
509 510
                }
            }
511
        }
512
        else if (stripos($jsfile, 'http://') === false && stripos($jsfile, 'https://') === false) {
513
            // A local .js file with a fully specified path
514
            $javascript_array[] = $wwwroot . $jsfile;
515 516 517 518 519 520
            // 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
521
            $bits = explode('/', $jsfile);
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
            $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'));
539 540
                if (is_callable(array($pluginclass, 'jsstrings'))) {
                    $tempstrings = call_static_method($pluginclass, 'jsstrings', $name);
541 542 543
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
                            $strings[$tag] = get_raw_string($tag, $section);
544 545
                        }
                    }
Richard Mansfield's avatar
Richard Mansfield committed
546
                }
547 548 549 550
                if (is_callable(array($pluginclass, 'jshelp'))) {
                    $tempstrings = call_static_method($pluginclass, 'jshelp', $name);
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
551
                            $strings[$tag . '.help'] = get_help_icon($plugintype, $pluginname, null, null,
552 553 554 555
                                                                     null, $tag);
                        }
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
556 557 558
                if (is_callable(array($pluginclass, 'themepaths'))) {
                    $tmpthemepaths = call_static_method($pluginclass, 'themepaths', $name);
                    foreach ($tmpthemepaths as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
559
                        $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
560 561
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
562
            }
Martyn Smith's avatar
Martyn Smith committed
563
        }
564 565 566 567
        else {
            // A remote .js file
            $javascript_array[] = $jsfile;
        }
568
    }
569 570

    $javascript_array[] = $jsroot . 'mahara.js';
571
    $javascript_array[] = $jsroot . 'formchangechecker.js';
572
    $javascript_array[] = $jsroot . 'textareamaxlengthchecker.js';
573

574 575 576 577 578 579
    // Load some event handler functions for checking if all AJAX requests have completed
    // when running behat tests
    if (defined('BEHAT_TEST')) {
        $javascript_array[] = get_config('wwwroot') . 'testing/frameworks/behat/page_status.js';
    }

580 581 582
    foreach ($jsstrings['mahara'] as $section => $tags) {
        foreach ($tags as $tag) {
            $strings[$tag] = get_raw_string($tag, $section);
583 584
        }
    }
585 586
    if (isset($extraconfig['themepaths']) && is_array($extraconfig['themepaths'])) {
        foreach ($extraconfig['themepaths'] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
587
            $theme_list[$themepath] = $THEME->get_url($themepath);
588 589
        }
    }
590

591
    $stringjs = '<script type="application/javascript">';
592
    $stringjs .= 'var strings = ' . json_encode($strings) . ';';
593
    $stringjs .= "\nfunction plural(n) { return " . get_raw_string('pluralrule', 'langconfig') . "; }\n";
594 595
    $stringjs .= '</script>';

596

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

600
    $smarty->assign('STRINGJS', $stringjs);
601

602
    $stylesheets = get_stylesheets_for_current_page($stylesheets, $extraconfig);
603

Son Nguyen's avatar
Son Nguyen committed
604 605 606 607
    // Disable CSS transforms, transitions, and animations when running behat tests
    if (defined('BEHAT_TEST')) {
        $stylesheets[] = get_config('wwwroot') . 'testing/frameworks/behat/no_transitions.css';
    }
608
    $smarty->assign('STYLESHEETLIST', $stylesheets);
609 610
    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
611
        $smarty->assign('THEMELIST', json_encode(array_merge((array)json_decode($smarty->get_template_vars('THEMELIST')),  $theme_list)));
612
    }
613

614 615 616 617 618
    $dropdownmenu = get_config('dropdownmenu');
    // disable drop-downs if overridden at institution level
    $sitethemeprefs = get_config('sitethemeprefs');
    $institutions = $USER->institutions;
    if (!empty($institutions)) {
619 620 621 622
        if (count($institutions) == 1) {
            $i = reset($institutions);
            if ($i->theme == $THEME->basename && $USER->institutiontheme->institutionname == $i->institution) {
                $dropdownmenu = $i->dropdownmenu;
623
            }
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        }
        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;
                    }
639 640 641 642 643 644
                }
            }
        }
    }

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

647 648
    if ($dropdownmenu) {
        $smarty->assign('DROPDOWNMENU', $dropdownmenu);
649
        $javascript_array[] = $jsroot . 'dropdown-nav.js';
650
    }
651

652
    $smarty->assign('MOBILE', $SESSION->get('mobile'));
653
    $smarty->assign('HANDHELD_DEVICE', $SESSION->get('handheld_device'));
654 655 656 657 658 659 660 661 662
    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);
    }
663

664 665 666 667
    $sitename = get_config('sitename');
    if (!$sitename) {
       $sitename = 'Mahara';
    }
668
    $smarty->assign('sitename', $sitename);
669

670 671 672 673
    $sitelogocustom = false;
    if (get_config('installed')) {
        $sitelogocustom = (int) (get_field('institution', 'logo', 'name', 'mahara') || $THEME->headerlogo);
    }
674
    $smarty->assign('sitelogocustom', $sitelogocustom);
675
    $sitelogo = $THEME->header_logo();
676
    $sitelogo = append_version_number($sitelogo);
677 678
    $sitelogosmall = $THEME->header_logo_small();
    $sitelogosmall = ($sitelogosmall ? append_version_number($sitelogosmall) : null);
679
    $smarty->assign('sitelogo', $sitelogo);
680
    $smarty->assign('sitelogosmall', $sitelogosmall);
681 682
    $smarty->assign('sitelogo4facebook', $THEME->facebook_logo());
    $smarty->assign('sitedescription4facebook', get_string('facebookdescription', 'mahara'));
683

Martyn Smith's avatar
Martyn Smith committed
684
    if (defined('TITLE')) {
685
        $smarty->assign('PAGETITLE', TITLE . ' - ' . $sitename);
Martyn Smith's avatar
Martyn Smith committed
686 687
    }
    else {
688
        $smarty->assign('PAGETITLE', $sitename);
Martyn Smith's avatar
Martyn Smith committed
689
    }
690 691 692 693 694 695 696 697 698
    if (defined('PAGEHEADING')) {
        $smarty->assign('PAGEHEADING', PAGEHEADING);
    }
    else {
        if (defined('TITLE')) {
            $smarty->assign('PAGEHEADING', TITLE);
        }
    }

699 700 701
    if (defined('SUBSECTIONHEADING')) {
        $smarty->assign('SUBSECTIONHEADING', SUBSECTIONHEADING);
    }
Martyn Smith's avatar
Martyn Smith committed
702

703
    $smarty->assign('PRODUCTIONMODE', get_config('productionmode'));
704 705 706
    if (defined('SITEOUTOFSYNC')) {
        $smarty->assign('SITEOUTOFSYNC', SITEOUTOFSYNC);
    }
707
    if (function_exists('local_header_top_content')) {
708
        $sitetop = (isset($sitetop) ? $sitetop : '') . local_header_top_content();
709 710 711
    }
    if (isset($sitetop)) {
        $smarty->assign('SITETOP', $sitetop);
712
    }
713 714 715
    if (defined('PUBLIC')) {
        $smarty->assign('PUBLIC', true);
    }
716 717 718
    if (defined('ADMIN')) {
        $smarty->assign('ADMIN', true);
    }
719 720 721
    if (defined('INSTITUTIONALADMIN')) {
        $smarty->assign('INSTITUTIONALADMIN', true);
    }
722 723 724 725 726 727
    if (defined('STAFF')) {
        $smarty->assign('STAFF', true);
    }
    if (defined('INSTITUTIONALSTAFF')) {
        $smarty->assign('INSTITUTIONALSTAFF', true);
    }
728

729
    $smarty->assign('LOGGEDIN', $USER->is_logged_in());
730
    $smarty->assign('loggedout', !$USER->is_logged_in());
731 732 733 734 735 736 737
    $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);
738
    if ($USER->is_logged_in()) {
739
        global $SELECTEDSUBNAV; // It's evil, but rightnav & mainnav stuff are now in different templates.
740 741 742 743
        $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'));
744
        }
745
        $mainnavsubnav = $SELECTEDSUBNAV;
746
        $smarty->assign('RIGHTNAV', right_nav());
747
        $smarty->assign('MESSAGEBOX', message_nav());
748 749 750 751 752 753 754 755 756
        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);
        }
757
    }
758
    else {
759
        $smarty->assign('languageform', $langselectform);
760
    }
761
    $smarty->assign('FOOTERMENU', footer_menu());
762

763
    $smarty->assign('USER', $USER);
764
    $smarty->assign('SESSKEY', $USER->get('sesskey'));
765
    $smarty->assign('CC_ENABLED', get_config('cookieconsent_enabled'));
766
    $javascript_array = append_version_number($javascript_array);
767
    $smarty->assign('JAVASCRIPT', $javascript_array);
768
    $smarty->assign('RELEASE', get_config('release'));
769
    $smarty->assign('SERIES', get_config('series'));
770
    $smarty->assign('CACHEVERSION', get_config('cacheversion', 0));
771
    if (get_config('siteclosedforupgrade')) {
772
        $smarty->assign('SITECLOSED', 'logindisabled');
773
    }
774
    else if (get_config('siteclosedbyadmin')) {
775
        $smarty->assign('SITECLOSED', 'loginallowed');
776
    }
777

778 779
    if ((!isset($extraconfig['pagehelp']) || $extraconfig['pagehelp'] !== false)
        and $help = has_page_help()) {
780 781 782
        $smarty->assign('PAGEHELPNAME', $help[0]);
        $smarty->assign('PAGEHELPICON', $help[1]);
    }
783
    if (defined('GROUP')) {
784
        require_once('group.php');
785 786 787 788 789 790
        if ($group = group_current_group()) {
            $smarty->assign('GROUP', $group);
            if (!defined('NOGROUPMENU')) {
                $smarty->assign('SUBPAGENAV', group_get_menu_tabs());
                $smarty->assign('PAGEHEADING', $group->name);
            }
791
        }
792
    }
793

Martyn Smith's avatar
Martyn Smith committed
794
    // ---------- sideblock stuff ----------
795 796
    $sidebars = !isset($extraconfig['sidebars']) || $extraconfig['sidebars'] !== false;
    if ($sidebars && !defined('INSTALLER') && (!defined('MENUITEM') || substr(MENUITEM, 0, 5) != 'admin')) {
797
        if (get_config('installed') && !$adminsection) {
798 799 800
            $data = site_menu();
            if (!empty($data)) {
                $smarty->assign('SITEMENU', site_menu());
801
                $sideblocks[] = array(
802
                    'name'   => 'linksandresources',
803 804 805 806 807 808
                    'weight' => 10,
                    'data'   => $data,
                );
            }
        }

809
        if ($USER->is_logged_in() && defined('MENUITEM') &&
810 811
            (
                substr(MENUITEM, 0, 7) == 'profile' ||
812
                in_array(substr(MENUITEM, 0, 7), array('create/', 'engage/', 'manage/'))
813
            )) {
814
            if (get_config('showselfsearchsideblock')) {
815
                $sideblocks[] = array(
816 817 818 819 820 821
                    'name'   => 'selfsearch',
                    'weight' => 0,
                    'data'   => array(),
                );
            }
            if (get_config('showtagssideblock')) {
822
                $sideblocks[] = array(
823 824 825 826 827 828
                    'name'   => 'tags',
                    'id'     => 'sb-tags',
                    'weight' => 0,
                    'data'   => tags_sideblock(),
                );
            }
Clare Lenihan's avatar
Clare Lenihan committed
829
        }
Clare Lenihan's avatar
Clare Lenihan committed
830

831
        if ($USER->is_logged_in() && !$adminsection) {
832
            $sideblocks[] = array(
833
                'name'   => 'profile',
834
                'id'     => 'sb-profile',
835
                'class' => 'user-panel',
836 837 838
                'weight' => -20,
                'data'   => profile_sideblock()
            );
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
            $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) {
854
                $sideblocks[] = array(
855
                    'name'   => 'onlineusers',
856
                    'id'     => 'sb-onlineusers',
857 858 859 860
                    'weight' => -10,
                    'data'   => onlineusers_sideblock(),
                );
            }
861
            if (get_config('showprogressbar') && $USER->get_account_preference('showprogressbar')) {
862
                $sideblocks[] = array(
863 864
                    'name'   => 'progressbar',
                    'id'     => 'sb-progressbar',
865
                    'class'  => 'progressbar',
866
                    'weight' => -8,
867 868 869 870 871 872
                    'data'   => progressbar_sideblock(),
                );
            }
        }

        if ($USER->is_logged_in() && $adminsection && defined('SECTION_PAGE') && SECTION_PAGE == 'progressbar') {
873
            $sideblocks[] = array(
874 875
                'name'   => 'progressbar',
                'id'     => 'sb-progressbar',
876
                'class'  => 'progressbar',
877
                'weight' => -8,
878 879
                'data'   => progressbar_sideblock(true),
            );
880
        }
Martyn Smith's avatar
Martyn Smith committed
881

882
        if ($isloginblockvisible) {
883
            $sideblocks[] = array(
884 885
                'name'   => 'login',
                'weight' => -10,
886
                'id'     => 'sb-loginbox',
887
                'data'   => array(
Gregor Anzelj's avatar
Gregor Anzelj committed
888
                    'loginform' => $authgenerateloginform,
889
                ),
890 891
            );
        }
892
        $smarty->assign('SHOWLOGINBLOCK', $isloginblockvisible);
893

894 895 896
        if (get_config('enablenetworking')) {
            require_once(get_config('docroot') .'api/xmlrpc/lib.php');
            if ($USER->is_logged_in() && $ssopeers = get_service_providers($USER->authinstance)) {