web.php 158 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
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
112
        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;
}

Pat Kira's avatar
Pat Kira committed
113

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

139
140


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

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

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

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

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

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

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

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

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

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

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

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

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

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

266
267
268
269
                    // Language strings required for TinyMCE
                    $pagestrings['mahara'] = isset($pagestrings['mahara']) ? $pagestrings['mahara'] : array();
                    $pagestrings['mahara'][] = 'attachedimage';

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

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

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

348
function custom_urlconvert (u, n, e) {
349
350
    // Don't convert the url on the skype status buttons.
    if (u.indexOf('skype:') == 0) {
351
      return u;
352
353
    }
    var t = tinyMCE.activeEditor, s = t.settings;
354

355
356
    // 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)
357
358
      return u;

359
360
    // Convert to relative
    if (s.relative_urls)
361
362
      return t.documentBaseURI.toRelative(u);

363
364
    // Convert to absolute
    u = t.documentBaseURI.toAbsolute(u, s.remove_script_host);
365

366
    return u;
367
}
368
369
370
</script>

EOF;
371
372
373
374
375
376
377
                    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]);
378
                }
379
            }
380

381
382
383
384
            // If any page adds jquery explicitly, remove it from the list
            if (($key = array_search('jquery', $check)) !== false) {
                unset($check[$key]);
            }
385
        }
386
    }
387
388
389
390
391
392
393
394
    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]);
        }
    }
395

396
    if (get_config('developermode') & DEVMODE_UNPACKEDJS) {
397
        $javascript_array[] = $jsroot . 'MochiKit/MochiKit.js';
398
399
400
401
        $javascript_array[] = $jsroot . 'MochiKit/Position.js';
        $javascript_array[] = $jsroot . 'MochiKit/Color.js';
        $javascript_array[] = $jsroot . 'MochiKit/Visual.js';
        $javascript_array[] = $jsroot . 'MochiKit/DragAndDrop.js';
402
        $javascript_array[] = $jsroot . 'MochiKit/Format.js';
403
404
405
406
    }
    else {
        $javascript_array[] = $jsroot . 'MochiKit/Packed.js';
    }
Martyn Smith's avatar
Martyn Smith committed
407
    $javascript_array[] = $jsroot . 'keyboardNavigation.js';
408

409
410
411
412
413
    //If necessary, load MathJax path
    if (get_config('mathjax')) {
        $javascript_array[] = get_config('mathjaxpath');
    }

414
    $strings = array();
415
416
417
418
419
420
421
422
423
    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);
        }
424
425
    }

426
    $jsstrings = jsstrings();
Martyn Smith's avatar
Martyn Smith committed
427
    $themepaths = themepaths();
428

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

    $javascript_array[] = $jsroot . 'mahara.js';
508
    $javascript_array[] = $jsroot . 'formchangechecker.js';
509

510
511
512
    foreach ($jsstrings['mahara'] as $section => $tags) {
        foreach ($tags as $tag) {
            $strings[$tag] = get_raw_string($tag, $section);
513
514
        }
    }
515
516
    if (isset($extraconfig['themepaths']) && is_array($extraconfig['themepaths'])) {
        foreach ($extraconfig['themepaths'] as $themepath) {
Nigel McNie's avatar
Nigel McNie committed
517
            $theme_list[$themepath] = $THEME->get_url($themepath);
518
519
        }
    }
520

521
    $stringjs = '<script type="application/javascript">';
522
    $stringjs .= 'var strings = ' . json_encode($strings) . ';';
523
    $stringjs .= "\nfunction plural(n) { return " . get_raw_string('pluralrule', 'langconfig') . "; }\n";
524
525
    $stringjs .= '</script>';

526

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

530
    $smarty->assign('STRINGJS', $stringjs);
531

532
    $stylesheets = get_stylesheets_for_current_page($stylesheets, $extraconfig);
533

534
    $smarty->assign('STYLESHEETLIST', $stylesheets);
535
536
    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
537
        $smarty->assign('THEMELIST', json_encode(array_merge((array)json_decode($smarty->get_template_vars('THEMELIST')),  $theme_list)));
538
    }
539

540
541
542
543
544
545
546
547
548
549
550
551
552
    $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)
553
554
                    || (empty($USER->accountprefs) && $i->theme == $THEME->basename && $USER->institutiontheme->institutionname == $i->institution)) {
                    $dropdownmenu = $i->dropdownmenu;
555
556
557
558
559
560
561
562
                }
            }
        }
    }

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

563
564
    if ($dropdownmenu) {
        $smarty->assign('DROPDOWNMENU', $dropdownmenu);
565
        $javascript_array[] = $jsroot . 'dropdown-nav.js';
566
    }
567

568
    $smarty->assign('MOBILE', $SESSION->get('mobile'));
569
    $smarty->assign('HANDHELD_DEVICE', $SESSION->get('handheld_device'));
570

571
572
573
574
    $sitename = get_config('sitename');
    if (!$sitename) {
       $sitename = 'Mahara';
    }
575
    $smarty->assign('sitename', $sitename);
576
    $sitelogo = $THEME->header_logo();
577
    $sitelogo = append_version_number($sitelogo);
578
    $smarty->assign('sitelogo', $sitelogo);
579
580
    $smarty->assign('sitelogo4facebook', $THEME->facebook_logo());
    $smarty->assign('sitedescription4facebook', get_string('facebookdescription', 'mahara'));
581

Martyn Smith's avatar
Martyn Smith committed
582
    if (defined('TITLE')) {
583
        $smarty->assign('PAGETITLE', TITLE . ' - ' . $sitename);
584
        $smarty->assign('heading', TITLE);
Martyn Smith's avatar
Martyn Smith committed
585
586
    }
    else {
587
        $smarty->assign('PAGETITLE', $sitename);
Martyn Smith's avatar
Martyn Smith committed
588
589
    }

590
    $smarty->assign('PRODUCTIONMODE', get_config('productionmode'));
591
    if (function_exists('local_header_top_content')) {
592
        $sitetop = (isset($sitetop) ? $sitetop : '') . local_header_top_content();
593
594
595
    }
    if (isset($sitetop)) {
        $smarty->assign('SITETOP', $sitetop);
596
    }
597
598
599
    if (defined('PUBLIC')) {
        $smarty->assign('PUBLIC', true);
    }
600
601
602
    if (defined('ADMIN')) {
        $smarty->assign('ADMIN', true);
    }
603
604
605
    if (defined('INSTITUTIONALADMIN')) {
        $smarty->assign('INSTITUTIONALADMIN', true);
    }
606
607
608
609
610
611
    if (defined('STAFF')) {
        $smarty->assign('STAFF', true);
    }
    if (defined('INSTITUTIONALSTAFF')) {
        $smarty->assign('INSTITUTIONALSTAFF', true);
    }
612

613
    $smarty->assign('LOGGEDIN', $USER->is_logged_in());
614
615
616
617
618
619
620
    $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);
621
    if ($USER->is_logged_in()) {
622
        global $SELECTEDSUBNAV; // It's evil, but rightnav & mainnav stuff are now in different templates.
623
        $smarty->assign('MAINNAV', main_nav());
624
        $mainnavsubnav = $SELECTEDSUBNAV;
625
        $smarty->assign('RIGHTNAV', right_nav());
626
627
628
629
630
631
632
633
634
        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);
        }
635
    }
636
    else {
637
        $smarty->assign('languageform', $langselectform);
638
    }
639
    $smarty->assign('FOOTERMENU', footer_menu());
640

641
    $smarty->assign_by_ref('USER', $USER);
642
    $smarty->assign('SESSKEY', $USER->get('sesskey'));
643
    $smarty->assign('CC_ENABLED', get_config('cookieconsent_enabled'));
644
    $javascript_array = append_version_number($javascript_array);
645
    $smarty->assign_by_ref('JAVASCRIPT', $javascript_array);
646
    $smarty->assign('RELEASE', get_config('release'));
647
    $smarty->assign('SERIES', get_config('series'));
648
    $smarty->assign('CACHEVERSION', get_config('cacheversion'));
649
650
    $siteclosedforupgrade = get_config('siteclosed');
    if ($siteclosedforupgrade && get_config('disablelogin')) {
651
        $smarty->assign('SITECLOSED', 'logindisabled');
652
653
    }
    else if ($siteclosedforupgrade || get_config('siteclosedbyadmin')) {
654
        $smarty->assign('SITECLOSED', 'loginallowed');
655
    }
656

657
658
    if ((!isset($extraconfig['pagehelp']) || $extraconfig['pagehelp'] !== false)
        and $help = has_page_help()) {
659
660
661
        $smarty->assign('PAGEHELPNAME', $help[0]);
        $smarty->assign('PAGEHELPICON', $help[1]);
    }
662
    if (defined('GROUP')) {
663
        require_once('group.php');
664
665
666
667
668
669
        if ($group = group_current_group()) {
            $smarty->assign('GROUP', $group);
            if (!defined('NOGROUPMENU')) {
                $smarty->assign('SUBPAGENAV', group_get_menu_tabs());
                $smarty->assign('PAGEHEADING', $group->name);
            }
670
        }
671
    }
672

Martyn Smith's avatar
Martyn Smith committed
673
    // ---------- sideblock stuff ----------
674
675
    $sidebars = !isset($extraconfig['sidebars']) || $extraconfig['sidebars'] !== false;
    if ($sidebars && !defined('INSTALLER') && (!defined('MENUITEM') || substr(MENUITEM, 0, 5) != 'admin')) {
676
        if (get_config('installed') && !$adminsection) {
677
678
679
            $data = site_menu();
            if (!empty($data)) {
                $smarty->assign('SITEMENU', site_menu());
680
                $sideblocks[] = array(
681
                    'name'   => 'linksandresources',
682
683
684
685
686
687
                    'weight' => 10,
                    'data'   => $data,
                );
            }
        }

688
689
        if ($USER->is_logged_in() && defined('MENUITEM') &&
            (substr(MENUITEM, 0, 11) == 'myportfolio' || substr(MENUITEM, 0, 7) == 'content')) {
690
            if (get_config('showselfsearchsideblock')) {
691
                $sideblocks[] = array(
692
693
694
695
696
697
                    'name'   => 'selfsearch',
                    'weight' => 0,
                    'data'   => array(),
                );
            }
            if (get_config('showtagssideblock')) {
698
                $sideblocks[] = array(
699
700
701
702
703
704
                    'name'   => 'tags',
                    'id'     => 'sb-tags',
                    'weight' => 0,
                    'data'   => tags_sideblock(),
                );
            }
Clare Lenihan's avatar
Clare Lenihan committed
705
        }
Clare Lenihan's avatar
Clare Lenihan committed
706

707
        if ($USER->is_logged_in() && !$adminsection) {
708
            $sideblocks[] = array(
709
                'name'   => 'profile',
710
                'id'     => 'sb-profile',
711
                'class' => 'user-panel',
712
713
714
                'weight' => -20,
                'data'   => profile_sideblock()
            );
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
            $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) {
730
                $sideblocks[] = array(
731
                    'name'   => 'onlineusers',
732
                    'id'     => 'sb-onlineusers',
733
734
735
736
                    'weight' => -10,
                    'data'   => onlineusers_sideblock(),
                );
            }
737
            if (get_config('showprogressbar') && $USER->get_account_preference('showprogressbar')) {
738
                $sideblocks[] = array(
739
740
                    'name'   => 'progressbar',
                    'id'     => 'sb-progressbar',
741
                    'class'  => 'progressbar',
742
                    'weight' => -8,
743
744
745
746
747
748
                    'data'   => progressbar_sideblock(),
                );
            }
        }

        if ($USER->is_logged_in() && $adminsection && defined('SECTION_PAGE') && SECTION_PAGE == 'progressbar') {
749
            $sideblocks[] = array(
750
751
                'name'   => 'progressbar',
                'id'     => 'sb-progressbar',
752
                'class'  => 'progressbar',
753
                'weight' => -8,
754
755
                'data'   => progressbar_sideblock(true),
            );
756
        }
Martyn Smith's avatar
Martyn Smith committed
757

758
759
760
        $isloginblockvisible = !$USER->is_logged_in() && !(get_config('siteclosed') && get_config('disablelogin'))
                && get_config('showloginsideblock');
        if ($isloginblockvisible) {
761
            $sideblocks[] = array(
762
763
                'name'   => 'login',
                'weight' => -10,
764
                'id'     => 'sb-loginbox',
765
766
767
                'data'   => array(
                    'loginform' => auth_generate_login_form(),
                ),
768
769
            );
        }
770

771
772
773
        if (get_config('enablenetworking')) {
            require_once(get_config('docroot') .'api/xmlrpc/lib.php');
            if ($USER->is_logged_in() && $ssopeers = get_service_providers($USER->authinstance)) {
774
                $sideblocks[] = array(
775
776
777
778
779
                    'name'   => 'ssopeers',
                    'weight' => 1,
                    'data'   => $ssopeers,
                );
            }
Martyn Smith's avatar
Martyn Smith committed
780
781
        }

782
783
        if (isset($extraconfig['sideblocks']) && is_array($extraconfig['sideblocks'])) {
            foreach ($extraconfig['sideblocks'] as $sideblock) {
784
                $sideblocks[] = $sideblock;
785
786
            }
        }
Martyn Smith's avatar
Martyn Smith committed
787

788
        // local_sideblocks_update allows sites to customise the sideblocks by munging the $sideblocks array.
789
        if (function_exists('local_sideblocks_update')) {
790
            local_sideblocks_update($sideblocks);
791
792
        }

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

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

801
        $smarty->assign('userauthinstance', $SESSION->get('authinstance'));
802
        $smarty->assign('MNETUSER', $SESSION->get('mnetuser'));
803
        $smarty->assign('SIDEBLOCKS', $sideblocks);
804
        $smarty->assign('SIDEBARS', $sidebars);
805

806
807
    }

808
809
810
811
812
    if (is_array($HEADDATA) && !empty($HEADDATA)) {
        $headers = array_merge($HEADDATA, $headers);
    }
    $smarty->assign_by_ref('HEADERS', $headers);

813
814
    if ($USER->get('parentuser')) {
        $smarty->assign('USERMASQUERADING', true);
815
        $smarty->assign('masqueradedetails', get_string('youaremasqueradingas', 'mahara', display_name($USER)));
816
817
        $smarty->assign('becomeyoulink', hsc($wwwroot) . 'admin/users/changeuser.php?restore=1');
        $smarty->assign('becomeyouagain', get_string('becomeadminagain', 'admin', hsc($USER->get('parentuser')->name)));
818
    }
Martyn Smith's avatar
Martyn Smith committed
819

820
821
    // Define additional html content
    if (get_config('installed')) {
822
823
824
825
        $additionalhtmlitems = array(
            'ADDITIONALHTMLHEAD'      => get_config('additionalhtmlhead'),
            'ADDITIONALHTMLTOPOFBODY' => get_config('additionalhtmltopofbody'),
            'ADDITIONALHTMLFOOTER'    => get_config('additionalhtmlfooter')
826
827
        );
        if ($additionalhtmlitems) {
828
829
            foreach ($additionalhtmlitems as $name=>$content) {
                $smarty->assign($name, $content);
830
831
832
            }
        }
    }
833
834
835
836
837
838

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

842
843
844
845

/**
 * Manages theme configuration.
 *
Aaron Wells's avatar
Aaron Wells committed
846
 * Does its best to give the user _a_ theme, even if it's not the theme they
847
848
849
850
851
852
853
854
855
 * 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 = '';

856
857
858
859
860
    /**
     * A user may have had the header logo overridden by an institution
     */
    public $headerlogo;

861
862
863
864
865
    /**
     * Additional stylesheets to display after the basename theme's stylesheets
     */
    public $addedstylesheets;

866
867
868
869
870
871
872
873
874
875
    /**
     * A human-readable version of the theme name
     */
    public $displayname = '';

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

876
877
878
879
880
881
882
883
884
885
    /**
     * Directories where to look for templates by default
     */
    public $templatedirs = array();

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

886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    /**
     * 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';

916
917
918
919
    /**
     * If the theme can use the svg image file format.
     */
    public $usesvg  = false;
920
921
922
923

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

951
952
953
954
        if (isset($themedata)) {
            $themename = $themedata->basename;
        }

955
        if (empty($themename)) {
956
            // Theme to show to when no theme has been suggested
957
958
959
            if (!$themename = get_config('theme')) {
                $themename = 'raw';
            }
960
        }
961
962

        // check the validity of the name
963
        if (!$this->name_is_valid($themename)) {
964
965
            throw new SystemException("Theme name is in invalid form: '$themename'");
        }
966
967

        $this->init_theme($themename, $themedata);
968
969
970
971
972
973
974
975
    }

    /**
     * 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);
976
977
978
979
980
    }

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

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
        // 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
999
            // We can safely assume that the default theme is installed, users
1000
            // should never be able to remove it
1001
1002
            $themename ='default';
            $themeconfig = $getthemeconfig($themename);