web.php 152 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

/**
 * 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")
 */
function setpageicon($smarty, $icon){
    $smarty->assign('pageicon', 'icon ' . $icon);
}



/**
 * Helper function to determine what css to include
 * podclass app, setting up some variables.
 *
 * @param $strings    A list of language strings required by the javascript code.
 * @return array
 */

function getstylesheets($stylesheets){

    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
    if(isset($THEME->overrideparentcss) && $THEME->overrideparentcss && $THEME->parent){
        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)));
            }
        }
    }
    if ($sheets = $THEME->additional_stylesheets()) {
        $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;
}

112
113
114
115
/**
 * This function creates a Smarty object and sets it up for use within our
 * podclass app, setting up some variables.
 *
116
117
 * WARNING: If you are using pieforms, set them up BEFORE calling this function.
 *
118
119
 * The variables that it sets up are:
 *
120
 * - WWWROOT: The base url for the Mahara system
121
122
123
124
125
 * - 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).
126
127
 * - PUBLIC: Set true if this page is a public page
 * - MAINNAV: Array defining the main navigation
128
 *
129
 * @param $javascript A list of javascript includes.  Each include should be just
130
 *                    the name of a file, and reside in js/{filename}
131
132
133
 * @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.
134
135
 * @return Smarty
 */
136

137
138


139
function smarty($javascript = array(), $headers = array(), $pagestrings = array(), $extraconfig = array()) {
140
    global $USER, $SESSION, $THEME, $HEADDATA, $langselectform;
141
142
143
144
145
146
147
148
149
150
151

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

152
    $sideblocks = array();
153
154
155
156
157
    // 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();
    }
158
159
    $smarty = smarty_core();

160
    $wwwroot = get_config('wwwroot');
Aaron Wells's avatar
Aaron Wells committed
161
    // NOTE: not using jswwwroot - it seems to wreck image paths if you
162
    // drag them around the wysiwyg editor
163
    $jswwwroot = json_encode($wwwroot);
Martyn Smith's avatar
Martyn Smith committed
164

165
166
167
168
169
170
171
172
173
174
    // 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 . '/';
175
                $headers[] = '<script type="application/javascript">var fakewwwroot = ' . json_encode($fakewwwroot) . ';</script>';
176
177
178
179
            }
        }
    }

Martyn Smith's avatar
Martyn Smith committed
180
    $theme_list = array();
181
    $adminsection = in_admin_section();
Aaron Wells's avatar
Aaron Wells committed
182

183
184
    if (function_exists('pieform_get_headdata')) {
        $headers = array_merge($headers, pieform_get_headdata());
185
186
187
        if (!defined('PIEFORM_GOT_HEADDATA')) {
          define('PIEFORM_GOT_HEADDATA', 1);
        }
188
    }
189

190
191
192
    // Define the stylesheets array early so that javascript modules can add extras
    $stylesheets = array();

Aaron Wells's avatar
Aaron Wells committed
193
    // Insert the appropriate javascript tags
194
    $javascript_array = array();
195
    $jsroot = $wwwroot . 'js/';
196

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

199
200
    // Make jQuery accessible with $j (Mochikit has $)
    $javascript_array[] = $jsroot . 'jquery/jquery.js';
201
    $javascript_array[] = $jsroot . 'jquery/deprecated_jquery.js';
202
    $headers[] = '<script type="application/javascript">$j=jQuery;</script>';
203

Richard Mansfield's avatar
Richard Mansfield committed
204
    // TinyMCE must be included first for some reason we're not sure about
205
206
207
208
    //
    // 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
209
    if ($SESSION->get('handheld_device') == false) {
210
211
212
213
214
215
        $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];
216
                    $javascript_array[] = $wwwroot . 'artefact/file/js/filebrowser.js';
217
218
                    $javascript_array[] = $jsroot . 'tinymce/tinymce.js';
                    $stylesheets = array_merge($stylesheets, array_reverse(array_values($THEME->get_url('style/tinymceskin.css', true))));
219
                    $content_css = json_encode($THEME->get_url('style/tinymce.css'));
220
221
                    $language = current_language();
                    $language = substr($language, 0, ((substr_count($language, '_') > 0) ? 5 : 2));
222
                    if ($language != 'en' && !file_exists(get_config('docroot') . 'js/tinymce/langs/' . $language . '.js')) {
223
224
225
226
227
228
229
230
                        // 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';
                            }
231
                        }
232
233
                    }
                    $extrasetup = isset($extraconfig['tinymcesetup']) ? $extraconfig['tinymcesetup'] : '';
234
                    $extramceconfig = isset($extraconfig['tinymceconfig']) ? $extraconfig['tinymceconfig'] : '';
235

236
                    // Check whether to make the spellchecker available
237
                    if (get_config('tinymcespellcheckerengine')) {
238
                        $spellchecker = ',spellchecker';
239
                        $spellchecker_toolbar = '| spellchecker';
240
                        $spellchecker_config = "gecko_spellcheck : false, spellchecker_rpc_url : \"{$jsroot}tinymce/plugins/spellchecker/spellchecker.php\",";
241
242
                    }
                    else {
243
                        $spellchecker = $spellchecker_toolbar = '';
244
245
246
                        $spellchecker_config = 'gecko_spellcheck : true,';
                    }

247
                    $toolbar = array(
248
                        null,
249
                        '"toolbar_toggle | formatselect | bold italic | bullist numlist | link unlink | imagebrowser | undo redo"',
250
                        '"underline strikethrough subscript superscript | alignleft aligncenter alignright alignjustify | outdent indent | forecolor backcolor | ltr rtl | fullscreen"',
251
                        '"fontselect | fontsizeselect | emoticons nonbreaking charmap ' . $spellchecker_toolbar . ' | table | removeformat pastetext | code"',
252
253
254
255
256
257
                    );

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

258
259
260
261
                    // Language strings required for TinyMCE
                    $pagestrings['mahara'] = isset($pagestrings['mahara']) ? $pagestrings['mahara'] : array();
                    $pagestrings['mahara'][] = 'attachedimage';

262
                    if ($check[$key] == 'tinymce') {
263
264
                        $tinymceconfig = <<<EOF
    theme: "modern",
265
    plugins: "tooltoggle,textcolor,visualblocks,wordcount,link,imagebrowser,table,emoticons{$spellchecker},paste,code,fullscreen,directionality,searchreplace,nonbreaking,charmap",
266
    skin: 'light',
267
268
    toolbar1: {$toolbar[1]},
    toolbar2: {$toolbar[2]},
269
    toolbar3: {$toolbar[3]},
270
    menubar: false,
271
    fix_list_elements: true,
272
    image_advtab: true,
273
    {$spellchecker_config}
274
EOF;
275
276
                    }
                    else {
277
278
279
                        $tinymceconfig = <<<EOF
    selector: "textarea.tinywysiwyg",
    theme: "modern",
280
    skin: 'light',
281
282
    plugins: "fullscreen,autoresize",
    toolbar: {$toolbar[0]},
283
EOF;
284
                    }
285

286
                    $headers[] = <<<EOF
287
<script type="application/javascript">
288
tinyMCE.init({
289
290
    {$tinymceconfig}
    schema: 'html4',
291
    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|align|title|class|type|frameborder|allowfullscreen]",
292
    urlconverter_callback : "custom_urlconvert",
293
    language: '{$language}',
294
    directionality: "{$tinymce_langdir}",
295
    content_css : {$content_css},
296
    remove_script_host: false,
297
    relative_urls: false,
298
    {$extramceconfig}
299
    setup: function(ed) {
300
        ed.on('init', function(ed) {
301
            if (typeof(editor_to_focus) == 'string' && ed.editorId == editor_to_focus) {
302
303
304
                ed.focus();
            }
        });
305
306
307
        ed.on('LoadContent', function(e) {
            // Hide all the 2nd/3rd row menu buttons
            jQuery('.mce-toolbar.mce-first').siblings().toggleClass('hidden');
308
309
310
311
312
313
314
315
316
317
318
            // 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');
            });
319
        });
320
        {$extrasetup}
321
    }
322
});
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

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

340
function custom_urlconvert (u, n, e) {
341
342
    // Don't convert the url on the skype status buttons.
    if (u.indexOf('skype:') == 0) {
343
      return u;
344
345
    }
    var t = tinyMCE.activeEditor, s = t.settings;
346

347
348
    // 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)
349
350
      return u;

351
352
    // Convert to relative
    if (s.relative_urls)
353
354
      return t.documentBaseURI.toRelative(u);

355
356
    // Convert to absolute
    u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
357

358
    return u;
359
}
360
361
362
</script>

EOF;
363
364
365
366
367
368
369
                    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]);
370
                }
371
            }
372

373
374
375
376
            // If any page adds jquery explicitly, remove it from the list
            if (($key = array_search('jquery', $check)) !== false) {
                unset($check[$key]);
            }
377
        }
378
    }
379
380
381
382
383
384
385
386
    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]);
        }
    }
387

388
    if (get_config('developermode') & DEVMODE_UNPACKEDJS) {
389
        $javascript_array[] = $jsroot . 'MochiKit/MochiKit.js';
390
391
392
393
        $javascript_array[] = $jsroot . 'MochiKit/Position.js';
        $javascript_array[] = $jsroot . 'MochiKit/Color.js';
        $javascript_array[] = $jsroot . 'MochiKit/Visual.js';
        $javascript_array[] = $jsroot . 'MochiKit/DragAndDrop.js';
394
        $javascript_array[] = $jsroot . 'MochiKit/Format.js';
395
396
397
398
    }
    else {
        $javascript_array[] = $jsroot . 'MochiKit/Packed.js';
    }
Martyn Smith's avatar
Martyn Smith committed
399
    $javascript_array[] = $jsroot . 'keyboardNavigation.js';
400

401
    $strings = array();
402
403
404
405
406
407
408
409
410
    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);
        }
411
412
    }

413
    $jsstrings = jsstrings();
Martyn Smith's avatar
Martyn Smith committed
414
    $themepaths = themepaths();
415

Richard Mansfield's avatar
Richard Mansfield committed
416
    foreach ($javascript as $jsfile) {
417
418
419
420
        // 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.
421
        if (strpos($jsfile, '/') === false) {
422
            $javascript_array[] = $jsroot . $jsfile . '.js';
423
            if (isset($jsstrings[$jsfile])) {
424
425
426
                foreach ($jsstrings[$jsfile] as $section => $tags) {
                    foreach ($tags as $tag) {
                        $strings[$tag] = get_raw_string($tag, $section);
427
428
429
                    }
                }
            }
Martyn Smith's avatar
Martyn Smith committed
430
431
            if (isset($themepaths[$jsfile])) {
                foreach ($themepaths[$jsfile] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
432
                    $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
433
434
                }
            }
435
        }
436
        else if (stripos($jsfile, 'http://') === false && stripos($jsfile, 'https://') === false) {
437
            // A local .js file with a fully specified path
438
            $javascript_array[] = $wwwroot . $jsfile;
439
440
441
442
443
444
            // 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
445
            $bits = explode('/', $jsfile);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
            $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'));
463
464
                if (is_callable(array($pluginclass, 'jsstrings'))) {
                    $tempstrings = call_static_method($pluginclass, 'jsstrings', $name);
465
466
467
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
                            $strings[$tag] = get_raw_string($tag, $section);
468
469
                        }
                    }
Richard Mansfield's avatar
Richard Mansfield committed
470
                }
471
472
473
474
                if (is_callable(array($pluginclass, 'jshelp'))) {
                    $tempstrings = call_static_method($pluginclass, 'jshelp', $name);
                    foreach ($tempstrings as $section => $tags) {
                        foreach ($tags as $tag) {
475
                            $strings[$tag . '.help'] = get_help_icon($plugintype, $pluginname, null, null,
476
477
478
479
                                                                     null, $tag);
                        }
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
480
481
482
                if (is_callable(array($pluginclass, 'themepaths'))) {
                    $tmpthemepaths = call_static_method($pluginclass, 'themepaths', $name);
                    foreach ($tmpthemepaths as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
483
                        $theme_list[$themepath] = $THEME->get_url($themepath);
Martyn Smith's avatar
Martyn Smith committed
484
485
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
486
            }
Martyn Smith's avatar
Martyn Smith committed
487
        }
488
489
490
491
        else {
            // A remote .js file
            $javascript_array[] = $jsfile;
        }
492
    }
493
494

    $javascript_array[] = $jsroot . 'mahara.js';
495
    $javascript_array[] = $jsroot . 'formchangechecker.js';
496
    if (get_config('developermode') & DEVMODE_DEBUGJS) {
497
        $javascript_array[] = $jsroot . 'debug.js';
498
    }
499

500
501
502
    foreach ($jsstrings['mahara'] as $section => $tags) {
        foreach ($tags as $tag) {
            $strings[$tag] = get_raw_string($tag, $section);
503
504
        }
    }
505
506
    if (isset($extraconfig['themepaths']) && is_array($extraconfig['themepaths'])) {
        foreach ($extraconfig['themepaths'] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
507
            $theme_list[$themepath] = $THEME->get_url($themepath);
508
509
        }
    }
510

511
    $stringjs = '<script type="application/javascript">';
512
    $stringjs .= 'var strings = ' . json_encode($strings) . ';';
513
    $stringjs .= "\nfunction plural(n) { return " . get_raw_string('pluralrule', 'langconfig') . "; }\n";
514
515
    $stringjs .= '</script>';

516

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

520
    $smarty->assign('STRINGJS', $stringjs);
521

522
    $stylesheets = getstylesheets($stylesheets);
523

524
    $smarty->assign('STYLESHEETLIST', $stylesheets);
525
526
    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
527
        $smarty->assign('THEMELIST', json_encode(array_merge((array)json_decode($smarty->get_template_vars('THEMELIST')),  $theme_list)));
528
    }
529

530
531
532
533
534
535
536
537
538
539
540
541
542
    $dropdownmenu = get_config('dropdownmenu');
    // disable drop-downs if overridden at institution level
    $sitethemeprefs = get_config('sitethemeprefs');
    $institutions = $USER->institutions;
    if (!empty($institutions)) {
        foreach ($institutions as $i) {
            if (!empty($sitethemeprefs)) {
                if (!empty($USER->accountprefs['theme']) && $USER->accountprefs['theme'] == $THEME->basename . '/' . $i->institution) {
                    $dropdownmenu = $i->dropdownmenu;
                }
            }
            else {
                if ((!empty($USER->accountprefs['theme']) && $USER->accountprefs['theme'] == $THEME->basename . '/' . $i->institution)
543
544
                    || (empty($USER->accountprefs) && $i->theme == $THEME->basename && $USER->institutiontheme->institutionname == $i->institution)) {
                    $dropdownmenu = $i->dropdownmenu;
545
546
547
548
549
550
551
552
                }
            }
        }
    }

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

553
554
    if ($dropdownmenu) {
        $smarty->assign('DROPDOWNMENU', $dropdownmenu);
555
        $javascript_array[] = $jsroot . 'dropdown-nav.js';
556
    }
557

558
    $smarty->assign('MOBILE', $SESSION->get('mobile'));
559
    $smarty->assign('HANDHELD_DEVICE', $SESSION->get('handheld_device'));
560

561
562
563
564
    $sitename = get_config('sitename');
    if (!$sitename) {
       $sitename = 'Mahara';
    }
565
    $smarty->assign('sitename', $sitename);
566
    $sitelogo = $THEME->header_logo();
567
    $sitelogo = append_version_number($sitelogo);
568
    $smarty->assign('sitelogo', $sitelogo);
569
570
    $smarty->assign('sitelogo4facebook', $THEME->facebook_logo());
    $smarty->assign('sitedescription4facebook', get_string('facebookdescription', 'mahara'));
571

Martyn Smith's avatar
Martyn Smith committed
572
    if (defined('TITLE')) {
573
        $smarty->assign('PAGETITLE', TITLE . ' - ' . $sitename);
574
        $smarty->assign('heading', TITLE);
Martyn Smith's avatar
Martyn Smith committed
575
576
    }
    else {
577
        $smarty->assign('PAGETITLE', $sitename);
Martyn Smith's avatar
Martyn Smith committed
578
579
    }

580
    $smarty->assign('PRODUCTIONMODE', get_config('productionmode'));
581
    if (function_exists('local_header_top_content')) {
582
        $sitetop = (isset($sitetop) ? $sitetop : '') . local_header_top_content();
583
584
585
    }
    if (isset($sitetop)) {
        $smarty->assign('SITETOP', $sitetop);
586
    }
587
588
589
    if (defined('PUBLIC')) {
        $smarty->assign('PUBLIC', true);
    }
590
591
592
    if (defined('ADMIN')) {
        $smarty->assign('ADMIN', true);
    }
593
594
595
    if (defined('INSTITUTIONALADMIN')) {
        $smarty->assign('INSTITUTIONALADMIN', true);
    }
596
597
598
599
600
601
    if (defined('STAFF')) {
        $smarty->assign('STAFF', true);
    }
    if (defined('INSTITUTIONALSTAFF')) {
        $smarty->assign('INSTITUTIONALSTAFF', true);
    }
602

603
    $smarty->assign('LOGGEDIN', $USER->is_logged_in());
604
605
606
607
608
609
610
    $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);
611
    if ($USER->is_logged_in()) {
612
        global $SELECTEDSUBNAV; // It's evil, but rightnav & mainnav stuff are now in different templates.
613
        $smarty->assign('MAINNAV', main_nav());
614
        $mainnavsubnav = $SELECTEDSUBNAV;
615
        $smarty->assign('RIGHTNAV', right_nav());
616
617
618
619
620
621
622
623
624
        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);
        }
625
    }
626
    else {
627
        $smarty->assign('languageform', $langselectform);
628
    }
629
    $smarty->assign('FOOTERMENU', footer_menu());
630

631
    $smarty->assign_by_ref('USER', $USER);
632
    $smarty->assign('SESSKEY', $USER->get('sesskey'));
633
    $smarty->assign('CC_ENABLED', get_config('cookieconsent_enabled'));
634
    $javascript_array = append_version_number($javascript_array);
635
    $smarty->assign_by_ref('JAVASCRIPT', $javascript_array);
636
    $smarty->assign('RELEASE', get_config('release'));
637
    $smarty->assign('SERIES', get_config('series'));
638
    $smarty->assign('CACHEVERSION', get_config('cacheversion'));
639
640
    $siteclosedforupgrade = get_config('siteclosed');
    if ($siteclosedforupgrade && get_config('disablelogin')) {
641
        $smarty->assign('SITECLOSED', 'logindisabled');
642
643
    }
    else if ($siteclosedforupgrade || get_config('siteclosedbyadmin')) {
644
        $smarty->assign('SITECLOSED', 'loginallowed');
645
    }
646

647
648
    if ((!isset($extraconfig['pagehelp']) || $extraconfig['pagehelp'] !== false)
        and $help = has_page_help()) {
649
650
651
        $smarty->assign('PAGEHELPNAME', $help[0]);
        $smarty->assign('PAGEHELPICON', $help[1]);
    }
652
    if (defined('GROUP')) {
653
        require_once('group.php');
654
655
656
657
658
659
        if ($group = group_current_group()) {
            $smarty->assign('GROUP', $group);
            if (!defined('NOGROUPMENU')) {
                $smarty->assign('SUBPAGENAV', group_get_menu_tabs());
                $smarty->assign('PAGEHEADING', $group->name);
            }
660
        }
661
    }
662

Martyn Smith's avatar
Martyn Smith committed
663
    // ---------- sideblock stuff ----------
664
665
    $sidebars = !isset($extraconfig['sidebars']) || $extraconfig['sidebars'] !== false;
    if ($sidebars && !defined('INSTALLER') && (!defined('MENUITEM') || substr(MENUITEM, 0, 5) != 'admin')) {
666
        if (get_config('installed') && !$adminsection) {
667
668
669
            $data = site_menu();
            if (!empty($data)) {
                $smarty->assign('SITEMENU', site_menu());
670
                $sideblocks[] = array(
671
                    'name'   => 'linksandresources',
672
673
674
675
676
677
                    'weight' => 10,
                    'data'   => $data,
                );
            }
        }

678
679
        if ($USER->is_logged_in() && defined('MENUITEM') &&
            (substr(MENUITEM, 0, 11) == 'myportfolio' || substr(MENUITEM, 0, 7) == 'content')) {
680
            if (get_config('showselfsearchsideblock')) {
681
                $sideblocks[] = array(
682
683
684
685
686
687
                    'name'   => 'selfsearch',
                    'weight' => 0,
                    'data'   => array(),
                );
            }
            if (get_config('showtagssideblock')) {
688
                $sideblocks[] = array(
689
690
691
692
693
694
                    'name'   => 'tags',
                    'id'     => 'sb-tags',
                    'weight' => 0,
                    'data'   => tags_sideblock(),
                );
            }
Clare Lenihan's avatar
Clare Lenihan committed
695
        }
Clare Lenihan's avatar
Clare Lenihan committed
696

697
        if ($USER->is_logged_in() && !$adminsection) {
698
            $sideblocks[] = array(
699
                'name'   => 'profile',
700
                'id'     => 'sb-profile',
701
                'class' => 'user-panel',
702
703
704
                'weight' => -20,
                'data'   => profile_sideblock()
            );
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
            $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) {
720
                $sideblocks[] = array(
721
                    'name'   => 'onlineusers',
722
                    'id'     => 'sb-onlineusers',
723
724
725
726
                    'weight' => -10,
                    'data'   => onlineusers_sideblock(),
                );
            }
727
            if (get_config('showprogressbar') && $USER->get_account_preference('showprogressbar')) {
728
                $sideblocks[] = array(
729
730
                    'name'   => 'progressbar',
                    'id'     => 'sb-progressbar',
731
                    'class'  => 'progressbar',
732
                    'weight' => -8,
733
734
735
736
737
738
                    'data'   => progressbar_sideblock(),
                );
            }
        }

        if ($USER->is_logged_in() && $adminsection && defined('SECTION_PAGE') && SECTION_PAGE == 'progressbar') {
739
            $sideblocks[] = array(
740
741
                'name'   => 'progressbar',
                'id'     => 'sb-progressbar',
742
                'class'  => 'progressbar',
743
                'weight' => -8,
744
745
                'data'   => progressbar_sideblock(true),
            );
746
        }
Martyn Smith's avatar
Martyn Smith committed
747

748
749
750
        $isloginblockvisible = !$USER->is_logged_in() && !(get_config('siteclosed') && get_config('disablelogin'))
                && get_config('showloginsideblock');
        if ($isloginblockvisible) {
751
            $sideblocks[] = array(
752
753
                'name'   => 'login',
                'weight' => -10,
754
                'id'     => 'sb-loginbox',
755
756
757
                'data'   => array(
                    'loginform' => auth_generate_login_form(),
                ),
758
759
            );
        }
760

761
762
763
        if (get_config('enablenetworking')) {
            require_once(get_config('docroot') .'api/xmlrpc/lib.php');
            if ($USER->is_logged_in() && $ssopeers = get_service_providers($USER->authinstance)) {
764
                $sideblocks[] = array(
765
766
767
768
769
                    'name'   => 'ssopeers',
                    'weight' => 1,
                    'data'   => $ssopeers,
                );
            }
Martyn Smith's avatar
Martyn Smith committed
770
771
        }

772
773
        if (isset($extraconfig['sideblocks']) && is_array($extraconfig['sideblocks'])) {
            foreach ($extraconfig['sideblocks'] as $sideblock) {
774
                $sideblocks[] = $sideblock;
775
776
            }
        }
Martyn Smith's avatar
Martyn Smith committed
777

778
        // local_sideblocks_update allows sites to customise the sideblocks by munging the $sideblocks array.
779
        if (function_exists('local_sideblocks_update')) {
780
            local_sideblocks_update($sideblocks);
781
782
        }

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

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

791
        $smarty->assign('userauthinstance', $SESSION->get('authinstance'));
792
        $smarty->assign('MNETUSER', $SESSION->get('mnetuser'));
793
        $smarty->assign('SIDEBLOCKS', $sideblocks);
794
        $smarty->assign('SIDEBARS', $sidebars);
795

796
797
    }

798
799
800
801
802
    if (is_array($HEADDATA) && !empty($HEADDATA)) {
        $headers = array_merge($HEADDATA, $headers);
    }
    $smarty->assign_by_ref('HEADERS', $headers);

803
804
    if ($USER->get('parentuser')) {
        $smarty->assign('USERMASQUERADING', true);
805
        $smarty->assign('masqueradedetails', get_string('youaremasqueradingas', 'mahara', display_name($USER)));
806
807
        $smarty->assign('becomeyouagain',
            ' <a href="' . hsc($wwwroot) . 'admin/users/changeuser.php?restore=1">'
808
            . get_string('becomeadminagain', 'admin', hsc($USER->get('parentuser')->name))
809
            . '</a>');
810
    }
Martyn Smith's avatar
Martyn Smith committed
811

812
813
    // Define additional html content
    if (get_config('installed')) {
814
815
816
817
        $additionalhtmlitems = array(
            'ADDITIONALHTMLHEAD'      => get_config('additionalhtmlhead'),
            'ADDITIONALHTMLTOPOFBODY' => get_config('additionalhtmltopofbody'),
            'ADDITIONALHTMLFOOTER'    => get_config('additionalhtmlfooter')
818
819
        );
        if ($additionalhtmlitems) {
820
821
            foreach ($additionalhtmlitems as $name=>$content) {
                $smarty->assign($name, $content);
822
823
824
            }
        }
    }
825
826
827
828
829
830

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

834
835
836
837

/**
 * Manages theme configuration.
 *
Aaron Wells's avatar
Aaron Wells committed
838
 * Does its best to give the user _a_ theme, even if it's not the theme they
839
840
841
842
843
844
845
846
847
 * 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 = '';

848
849
850
851
852
    /**
     * A user may have had the header logo overridden by an institution
     */
    public $headerlogo;

853
854
855
856
857
    /**
     * Additional stylesheets to display after the basename theme's stylesheets
     */
    public $addedstylesheets;

858
859
860
861
862
863
864
865
866
867
    /**
     * A human-readable version of the theme name
     */
    public $displayname = '';

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

868
869
870
871
872
873
874
875
876
877
    /**
     * Directories where to look for templates by default
     */
    public $templatedirs = array();

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

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
    /**
     * 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';

908
909
910
911
    /**
     * If the theme can use the svg image file format.
     */
    public $usesvg  = false;
912
913
914
915

    /**
     * Initialises a theme object based on the theme 'hint' passed.
     *
Aaron Wells's avatar
Aaron Wells committed
916
917
     * 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
918
919
     * that's a user ID and ask for the theme for that user.
     *
Aaron Wells's avatar
Aaron Wells committed
920
     * If the theme they want doesn't exist, the object is initialised for the
921
922
923
924
925
926
927
928
     * 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;
929
            $themedata = null;
930
931
        }
        else if ($arg instanceof User) {
932
            $themedata = $arg->get_themedata();
933
934
935
936
        }
        else if (is_int($arg)) {
            $user = new User();
            $user->find_by_id($arg);
937
            $themedata = $user->get_themedata();
938
939
940
941
942
        }
        else {
            throw new SystemException("Argument to Theme::__construct was not a theme name, user object or user ID");
        }

943
944
945
946
        if (isset($themedata)) {
            $themename = $themedata->basename;
        }

947
        if (empty($themename)) {
948
            // Theme to show to when no theme has been suggested
949
950
951
            if (!$themename = get_config('theme')) {
                $themename = 'raw';
            }
952
        }
953
954

        // check the validity of the name
955
        if (!$this->name_is_valid($themename)) {
956
957
            throw new SystemException("Theme name is in invalid form: '$themename'");
        }
958
959

        $this->init_theme($themename, $themedata);
960
961
962
963
964
965
966
967
    }

    /**
     * Given a theme name, check that it is valid
     */
    public static function name_is_valid($themename) {
        // preg_match returns 0 if invalid characters were found, 1 if not
        return (preg_match('/^[a-zA-Z0-9_-]+$/', $themename) == 1);
968
969
970
971
972
    }

    /**
     * Given a theme name, reads in all config and sets fields on this object
     */
973
    private function init_theme($themename, $themedata) {
974

975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
        // A little anonymous function to retrieve *only* the $theme variable from
        // the themeconfig.php file
        $getthemeconfig = function($themename) {
            $themeconfigfile = get_config('docroot') . 'theme/' . $themename . '/themeconfig.php';
            if (is_readable($themeconfigfile)) {
                require( get_config('docroot') . 'theme/' . $themename . '/themeconfig.php' );
                return $theme;
            }
            else {
                return false;
            }
        };

        $themeconfig = $getthemeconfig($themename);

        if (!$themeconfig) {
Aaron Wells's avatar
Aaron Wells committed
991
            // We can safely assume that the default theme is installed, users
992
            // should never be able to remove it
993
994
            $themename ='default';
            $themeconfig = $getthemeconfig($themename);
995
996
        }

997
        $this->basename = $themename;
998

999
        foreach (get_object_vars($themeconfig) as $key => $value) {
1000
1001
1002
1003
            $this->$key = $value;
        }

        if (!isset($this->displayname)) {
1004
            $this->displayname = $this->basename;
1005
1006
        }

1007
1008
1009
1010
        // Local theme overrides come first
        $this->templatedirs[] = get_config('docroot') . 'local/theme/templates/';

        // Then the current theme
1011
1012
1013
        $this->templatedirs[] = get_config('docroot') . 'theme/' . $this->basename . '/templates/';
        $this->inheritance[]  = $this->basename;

1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
        // 'raw' is the default parent theme
        // (If a theme has no parent, it should set $themeconfig->parent = false)
        if (!isset($themeconfig->parent)) {
            $themeconfig->parent = 'raw';
        }
        $currentthemename = $this->basename;
        while ($themeconfig->parent !== false) {
            // Now go through the theme hierarchy assigning variables from the
            // parent themes
            $parentthemename = $themeconfig->parent;
            $parentthemeconfig = $getthemeconfig($parentthemename);

            // If the parent theme is missing, short-circuit to the "raw" theme
            if (!$parentthemeconfig) {
                log_warn("Theme \"{$currentthemename}\" has missing parent theme \"{$parentthemename}\".");
                $parentthemename = 'raw';
                $parentthemeconfig = $getthemeconfig($parentthemename);
            }
            $currentthemename = $parentthemename;
            $themeconfig = $parentthemeconfig;
1034

1035
            foreach (get_object_vars($themeconfig) as $key => $value) {
1036
                if (!isset($this->$key) || !$this->$key) {
1037
1038
1039
                    $this->$key = $value;
                }
            }
1040
1041
1042
1043
1044
            $this->templatedirs[] = get_config('docroot') . 'theme/' . $currentthemename . '/templates/';
            $this->inheritance[]  = $currentthemename;
            if (!isset($themeconfig->parent)) {
                $themeconfig->parent = 'raw';
            }
1045
        }
Richard Mansfield's avatar