Dotclear

source: inc/admin/lib.dc.page.php @ 3769:12b632aaedc1

Revision 3769:12b632aaedc1, 44.8 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Some modifications were missing from previous commit, my bad!

Line 
1<?php
2/**
3 * @package Dotclear
4 * @subpackage Backend
5 *
6 * @copyright Olivier Meunier & Association Dotclear
7 * @copyright GPL-2.0-only
8 */
9
10if (!defined('DC_RC_PATH')) {return;}
11
12define('DC_AUTH_PAGE', 'auth.php');
13
14class dcPage
15{
16    private static $loaded_js     = array();
17    private static $loaded_css    = array();
18    private static $xframe_loaded = false;
19    private static $N_TYPES       = array(
20        "success" => "success",
21        "warning" => "warning-msg",
22        "error"   => "error",
23        "message" => "message",
24        "static"  => "static-msg");
25
26    # Auth check
27    public static function check($permissions)
28    {
29        global $core;
30
31        if ($core->blog && $core->auth->check($permissions, $core->blog->id)) {
32            return;
33        }
34
35        if (session_id()) {
36            $core->session->destroy();
37        }
38        http::redirect(DC_AUTH_PAGE);
39    }
40
41    # Check super admin
42    public static function checkSuper()
43    {
44        global $core;
45
46        if (!$core->auth->isSuperAdmin()) {
47            if (session_id()) {
48                $core->session->destroy();
49            }
50            http::redirect(DC_AUTH_PAGE);
51        }
52    }
53
54    # Top of admin page
55    public static function open($title = '', $head = '', $breadcrumb = '', $options = array())
56    {
57        global $core;
58
59        # List of user's blogs
60        if ($core->auth->getBlogCount() == 1 || $core->auth->getBlogCount() > 20) {
61            $blog_box =
62            '<p>' . __('Blog:') . ' <strong title="' . html::escapeHTML($core->blog->url) . '">' .
63            html::escapeHTML($core->blog->name) . '</strong>';
64
65            if ($core->auth->getBlogCount() > 20) {
66                $blog_box .= ' - <a href="' . $core->adminurl->get("admin.blogs") . '">' . __('Change blog') . '</a>';
67            }
68            $blog_box .= '</p>';
69        } else {
70            $rs_blogs = $core->getBlogs(array('order' => 'LOWER(blog_name)', 'limit' => 20));
71            $blogs    = array();
72            while ($rs_blogs->fetch()) {
73                $blogs[html::escapeHTML($rs_blogs->blog_name . ' - ' . $rs_blogs->blog_url)] = $rs_blogs->blog_id;
74            }
75            $blog_box =
76            '<p><label for="switchblog" class="classic">' .
77            __('Blogs:') . '</label> ' .
78            $core->formNonce() .
79            form::combo('switchblog', $blogs, $core->blog->id) .
80            '<input type="submit" value="' . __('ok') . '" class="hidden-if-js" /></p>';
81        }
82
83        $safe_mode = isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'];
84
85        # Display
86        $headers = new ArrayObject(array());
87
88        # Content-Type
89        $headers['content-type'] = 'Content-Type: text/html; charset=UTF-8';
90
91        # Referrer Policy for admin pages
92        $headers['referrer'] = 'Referrer-Policy: strict-origin';
93
94        # Prevents Clickjacking as far as possible
95        if (isset($options['x-frame-allow'])) {
96            self::setXFrameOptions($headers, $options['x-frame-allow']);
97        } else {
98            self::setXFrameOptions($headers);
99        }
100
101        # Content-Security-Policy (only if safe mode if not active, it may help)
102        if (!$safe_mode && $core->blog->settings->system->csp_admin_on) {
103            // Get directives from settings if exist, else set defaults
104            $csp = new ArrayObject(array());
105
106                                                                                // SQlite Clearbricks driver does not allow using single quote at beginning or end of a field value
107                                                                                // so we have to use neutral values (localhost and 127.0.0.1) for some CSP directives
108            $csp_prefix = $core->con->driver() == 'sqlite' ? 'localhost ' : ''; // Hack for SQlite Clearbricks driver
109            $csp_suffix = $core->con->driver() == 'sqlite' ? ' 127.0.0.1' : ''; // Hack for SQlite Clearbricks driver
110
111            $csp['default-src'] = $core->blog->settings->system->csp_admin_default ?:
112            $csp_prefix . "'self'" . $csp_suffix;
113            $csp['script-src'] = $core->blog->settings->system->csp_admin_script ?:
114            $csp_prefix . "'self' 'unsafe-inline' 'unsafe-eval'" . $csp_suffix;
115            $csp['style-src'] = $core->blog->settings->system->csp_admin_style ?:
116            $csp_prefix . "'self' 'unsafe-inline'" . $csp_suffix;
117            $csp['img-src'] = $core->blog->settings->system->csp_admin_img ?:
118            $csp_prefix . "'self' data: http://media.dotaddict.org blob:";
119
120            # Cope with blog post preview (via public URL in iframe)
121            if (!is_null($core->blog->host)) {
122                $csp['default-src'] .= ' ' . parse_url($core->blog->host, PHP_URL_HOST);
123                $csp['script-src'] .= ' ' . parse_url($core->blog->host, PHP_URL_HOST);
124                $csp['style-src'] .= ' ' . parse_url($core->blog->host, PHP_URL_HOST);
125            }
126            # Cope with media display in media manager (via public URL)
127            if (!is_null($core->media)) {
128                $csp['img-src'] .= ' ' . parse_url($core->media->root_url, PHP_URL_HOST);
129            }
130            # Allow everything in iframe (used by editors to preview public content)
131            $csp['child-src'] = "*";
132
133            # --BEHAVIOR-- adminPageHTTPHeaderCSP
134            $core->callBehavior('adminPageHTTPHeaderCSP', $csp);
135
136            // Construct CSP header
137            $directives = array();
138            foreach ($csp as $key => $value) {
139                if ($value) {
140                    $directives[] = $key . ' ' . $value;
141                }
142            }
143            if (count($directives)) {
144                if (version_compare(phpversion(), '5.4', '>=')) {
145                    // csp_report.php needs PHP ≥ 5.4
146                    $directives[] = "report-uri " . DC_ADMIN_URL . "csp_report.php";
147                }
148                $report_only    = ($core->blog->settings->system->csp_admin_report_only) ? '-Report-Only' : '';
149                $headers['csp'] = "Content-Security-Policy" . $report_only . ": " . implode(" ; ", $directives);
150            }
151        }
152
153        # --BEHAVIOR-- adminPageHTTPHeaders
154        $core->callBehavior('adminPageHTTPHeaders', $headers);
155        foreach ($headers as $key => $value) {
156            header($value);
157        }
158
159        echo
160        '<!DOCTYPE html>' .
161        '<html lang="' . $core->auth->getInfo('user_lang') . '">' . "\n" .
162        "<head>\n" .
163        '  <meta charset="UTF-8" />' . "\n" .
164        '  <meta name="ROBOTS" content="NOARCHIVE,NOINDEX,NOFOLLOW" />' . "\n" .
165        '  <meta name="GOOGLEBOT" content="NOSNIPPET" />' . "\n" .
166        '  <meta name="viewport" content="width=device-width, initial-scale=1.0" />' . "\n" .
167        '  <title>' . $title . ' - ' . html::escapeHTML($core->blog->name) . ' - ' . html::escapeHTML(DC_VENDOR_NAME) . ' - ' . DC_VERSION . '</title>' . "\n" .
168
169        self::cssLoad('style/default.css');
170        if (l10n::getTextDirection($GLOBALS['_lang']) == 'rtl') {
171            echo self::cssLoad('style/default-rtl.css');
172        }
173
174        $core->auth->user_prefs->addWorkspace('interface');
175        if (!$core->auth->user_prefs->interface->hide_std_favicon) {
176            echo
177                '<link rel="icon" type="image/png" href="images/favicon96-login.png" />' . "\n" .
178                '<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />' . "\n";
179        }
180
181        if ($core->auth->user_prefs->interface->htmlfontsize) {
182            echo
183            '<script type="text/javascript">' . "\n" .
184            self::jsVar('dotclear_htmlFontSize', $core->auth->user_prefs->interface->htmlfontsize) . "\n" .
185                "</script>\n";
186        }
187
188        echo
189        self::jsCommon() .
190        self::jsToggles() .
191            $head;
192
193        if ($core->auth->user_prefs->interface->hidemoreinfo) {
194            echo
195                '<script type="text/javascript">' . "\n" .
196                'dotclear.hideMoreInfo = true;' . "\n" .
197                "</script>\n";
198        }
199        if ($core->auth->user_prefs->interface->showajaxloader) {
200            echo
201                '<script type="text/javascript">' . "\n" .
202                'dotclear.showAjaxLoader = true;' . "\n" .
203                "</script>\n";
204        }
205
206        # --BEHAVIOR-- adminPageHTMLHead
207        $core->callBehavior('adminPageHTMLHead');
208
209        echo
210        "</head>\n" .
211        '<body id="dotclear-admin' .
212        ($safe_mode ? ' safe-mode' : '') . '" class="no-js' .
213        ($core->auth->user_prefs->interface->dynfontsize ? ' responsive-font' : '') . '">' . "\n" .
214
215        '<ul id="prelude">' .
216        '<li><a href="#content">' . __('Go to the content') . '</a></li>' .
217        '<li><a href="#main-menu">' . __('Go to the menu') . '</a></li>' .
218        '<li><a href="#qx">' . __('Go to search') . '</a></li>' .
219        '<li><a href="#help">' . __('Go to help') . '</a></li>' .
220        '</ul>' . "\n" .
221        '<div id="header" role="banner">' .
222        '<h1><a href="' . $core->adminurl->get("admin.home") . '"><span class="hidden">' . DC_VENDOR_NAME . '</span></a></h1>' . "\n";
223
224        echo
225        '<form action="' . $core->adminurl->get("admin.home") . '" method="post" id="top-info-blog">' .
226        $blog_box .
227        '<p><a href="' . $core->blog->url . '" class="outgoing" title="' . __('Go to site') .
228        '">' . __('Go to site') . '<img src="images/outgoing.png" alt="" /></a>' .
229        '</p></form>' .
230        '<ul id="top-info-user">' .
231        '<li><a class="' . (preg_match('/' . preg_quote($core->adminurl->get('admin.home')) . '$/', $_SERVER['REQUEST_URI']) ? ' active' : '') . '" href="' . $core->adminurl->get("admin.home") . '">' . __('My dashboard') . '</a></li>' .
232        '<li><a class="smallscreen' . (preg_match('/' . preg_quote($core->adminurl->get('admin.user.preferences')) . '(\?.*)?$/', $_SERVER['REQUEST_URI']) ? ' active' : '') .
233        '" href="' . $core->adminurl->get("admin.user.preferences") . '">' . __('My preferences') . '</a></li>' .
234        '<li><a href="' . $core->adminurl->get("admin.home", array('logout' => 1)) . '" class="logout"><span class="nomobile">' . sprintf(__('Logout %s'), $core->auth->userID()) .
235            '</span><img src="images/logout.png" alt="" /></a></li>' .
236            '</ul>' .
237            '</div>'; // end header
238
239        echo
240        '<div id="wrapper" class="clearfix">' . "\n" .
241        '<div class="hidden-if-no-js collapser-box"><button type="button" id="collapser" class="void-btn">' .
242        '<img class="collapse-mm visually-hidden" src="images/collapser-hide.png" alt="' . __('Hide main menu') . '" />' .
243        '<img class="expand-mm visually-hidden" src="images/collapser-show.png" alt="' . __('Show main menu') . '" />' .
244            '</button></div>' .
245            '<div id="main" role="main">' . "\n" .
246            '<div id="content" class="clearfix">' . "\n";
247
248        # Safe mode
249        if ($safe_mode) {
250            echo
251            '<div class="warning" role="alert"><h3>' . __('Safe mode') . '</h3>' .
252            '<p>' . __('You are in safe mode. All plugins have been temporarily disabled. Remind to log out then log in again normally to get back all functionalities') . '</p>' .
253                '</div>';
254        }
255
256        // Display breadcrumb (if given) before any error messages
257        echo $breadcrumb;
258
259        // Display notices and errors
260        echo self::notices();
261    }
262
263    public static function notices()
264    {
265        global $core;
266        static $error_displayed = false;
267
268        $res = '';
269
270        // return error messages if any
271        if ($core->error->flag() && !$error_displayed) {
272
273            # --BEHAVIOR-- adminPageNotificationError
274            $notice_error = $core->callBehavior('adminPageNotificationError', $core, $core->error);
275
276            if (isset($notice_error) && !empty($notice_error)) {
277                $res .= $notice_error;
278            } else {
279                $res .= '<div class="error"><p>' .
280                '<strong>' . (count($core->error->getErrors()) > 1 ? __('Errors:') : __('Error:')) . '</strong>' .
281                '</p>' . $core->error->toHTML() . '</div>';
282            }
283            $error_displayed = true;
284        }
285
286        // return notices if any
287        if (isset($_SESSION['notifications'])) {
288            foreach ($_SESSION['notifications'] as $notification) {
289
290                # --BEHAVIOR-- adminPageNotification
291                $notice = $core->callBehavior('adminPageNotification', $core, $notification);
292
293                $res .= (isset($notice) && !empty($notice) ? $notice : self::getNotification($notification));
294            }
295            unset($_SESSION['notifications']);
296        }
297        return $res;
298    }
299
300    public static function addNotice($type, $message, $options = array())
301    {
302        if (isset(self::$N_TYPES[$type])) {
303            $class = self::$N_TYPES[$type];
304        } else {
305            $class = $type;
306        }
307        if (isset($_SESSION['notifications']) && is_array($_SESSION['notifications'])) {
308            $notifications = $_SESSION['notifications'];
309        } else {
310            $notifications = array();
311        }
312
313        $n = array_merge($options, array('class' => $class, 'ts' => time(), 'text' => $message));
314        if ($type != "static") {
315            $notifications[] = $n;
316        } else {
317            array_unshift($notifications, $n);
318        }
319        $_SESSION['notifications'] = $notifications;
320    }
321
322    public static function addSuccessNotice($message, $options = array())
323    {
324        self::addNotice("success", $message, $options);
325    }
326
327    public static function addWarningNotice($message, $options = array())
328    {
329        self::addNotice("warning", $message, $options);
330    }
331
332    public static function addErrorNotice($message, $options = array())
333    {
334        self::addNotice("error", $message, $options);
335    }
336
337    protected static function getNotification($n)
338    {
339        global $core;
340        $tag = (isset($n['divtag']) && $n['divtag']) ? 'div' : 'p';
341        $ts  = '';
342        if (!isset($n['with_ts']) || ($n['with_ts'] == true)) {
343            $ts = dt::str(__('[%H:%M:%S]'), $n['ts'], $core->auth->getInfo('user_tz')) . ' ';
344        }
345        $res = '<' . $tag . ' class="' . $n['class'] . '" role="alert">' . $ts . $n['text'] . '</' . $tag . '>';
346        return $res;
347    }
348
349    public static function close()
350    {
351        global $core;
352
353        if (!$GLOBALS['__resources']['ctxhelp']) {
354            if (!$core->auth->user_prefs->interface->hidehelpbutton) {
355                echo
356                '<p id="help-button"><a href="' . $core->adminurl->get("admin.help") . '" class="outgoing" title="' .
357                __('Global help') . '">' . __('Global help') . '</a></p>';
358            }
359        }
360
361        $menu = &$GLOBALS['_menu'];
362
363        echo
364        "</div>\n" . // End of #content
365        "</div>\n" . // End of #main
366
367        '<div id="main-menu" role="navigation">' . "\n" .
368
369        '<form id="search-menu" action="' . $core->adminurl->get("admin.search") . '" method="get" role="search">' .
370        '<p><label for="qx" class="hidden">' . __('Search:') . ' </label>' . form::field('qx', 30, 255, '') .
371        '<input type="submit" value="' . __('OK') . '" /></p>' .
372            '</form>';
373
374        foreach ($menu as $k => $v) {
375            echo $menu[$k]->draw();
376        }
377
378        $text = sprintf(__('Thank you for using %s.'), 'Dotclear ' . DC_VERSION);
379
380        # --BEHAVIOR-- adminPageFooter
381        $textAlt = $core->callBehavior('adminPageFooter', $core, $text);
382        if ($textAlt != '') {
383            $text = $textAlt;
384        }
385        $text = html::escapeHTML($text);
386
387        echo
388        '</div>' . "\n" . // End of #main-menu
389        "</div>\n";       // End of #wrapper
390
391        echo '<p id="gototop"><a href="#wrapper">' . __('Page top') . '</a></p>' . "\n";
392
393        $figure = "
394  ♥‿♥
395  ";
396
397        echo
398            '<div id="footer" role="contentinfo">' .
399            '<a href="http://dotclear.org/" title="' . $text . '">' .
400            '<img src="style/dc_logos/w-dotclear90.png" alt="' . $text . '" /></a></div>' . "\n" .
401            "<!-- " . "\n" .
402            $figure .
403            " -->" . "\n";
404
405        if (defined('DC_DEV') && DC_DEV === true) {
406            echo self::debugInfo();
407        }
408
409        echo
410            '</body></html>';
411    }
412
413    public static function openPopup($title = '', $head = '', $breadcrumb = '')
414    {
415        global $core;
416
417        # Display
418        header('Content-Type: text/html; charset=UTF-8');
419
420        # Referrer Policy for admin pages
421        header('Referrer-Policy: strict-origin');
422
423        # Prevents Clickjacking as far as possible
424        header('X-Frame-Options: SAMEORIGIN'); // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
425
426        echo
427        '<!DOCTYPE html>' .
428        '<html lang="' . $core->auth->getInfo('user_lang') . '">' . "\n" .
429        "<head>\n" .
430        '  <meta charset="UTF-8" />' . "\n" .
431        '  <meta name="viewport" content="width=device-width, initial-scale=1.0" />' . "\n" .
432        '  <title>' . $title . ' - ' . html::escapeHTML($core->blog->name) . ' - ' . html::escapeHTML(DC_VENDOR_NAME) . ' - ' . DC_VERSION . '</title>' . "\n" .
433
434        '  <meta name="ROBOTS" content="NOARCHIVE,NOINDEX,NOFOLLOW" />' . "\n" .
435        '  <meta name="GOOGLEBOT" content="NOSNIPPET" />' . "\n" .
436
437        self::cssLoad('style/default.css');
438        if (l10n::getTextDirection($GLOBALS['_lang']) == 'rtl') {
439            echo self::cssLoad('style/default-rtl.css');
440        }
441
442        $core->auth->user_prefs->addWorkspace('interface');
443        if ($core->auth->user_prefs->interface->htmlfontsize) {
444            echo
445            '<script type="text/javascript">' . "\n" .
446            self::jsVar('dotclear_htmlFontSize', $core->auth->user_prefs->interface->htmlfontsize) . "\n" .
447                "</script>\n";
448        }
449
450        echo
451        self::jsCommon() .
452        self::jsToggles() .
453            $head;
454
455        if ($core->auth->user_prefs->interface->hidemoreinfo) {
456            echo
457                '<script type="text/javascript">' . "\n" .
458                'dotclear.hideMoreInfo = true;' . "\n" .
459                "</script>\n";
460        }
461        if ($core->auth->user_prefs->interface->showajaxloader) {
462            echo
463                '<script type="text/javascript">' . "\n" .
464                'dotclear.showAjaxLoader = true;' . "\n" .
465                "</script>\n";
466        }
467
468        # --BEHAVIOR-- adminPageHTMLHead
469        $core->callBehavior('adminPageHTMLHead');
470
471        echo
472            "</head>\n" .
473            '<body id="dotclear-admin" class="popup' .
474            ($core->auth->user_prefs->interface->dynfontsize ? ' responsive-font' : '') . '">' . "\n" .
475
476            '<h1>' . DC_VENDOR_NAME . '</h1>' . "\n";
477
478        echo
479            '<div id="wrapper">' . "\n" .
480            '<div id="main" role="main">' . "\n" .
481            '<div id="content">' . "\n";
482
483        // display breadcrumb if given
484        echo $breadcrumb;
485
486        // Display notices and errors
487        echo self::notices();
488    }
489
490    public static function closePopup()
491    {
492        echo
493        "</div>\n" . // End of #content
494        "</div>\n" . // End of #main
495        "</div>\n" . // End of #wrapper
496
497        '<p id="gototop"><a href="#wrapper">' . __('Page top') . '</a></p>' . "\n" .
498
499            '<div id="footer" role="contentinfo"><p>&nbsp;</p></div>' . "\n" .
500            '</body></html>';
501    }
502
503    public static function breadcrumb($elements = null, $options = array())
504    {
505        global $core;
506        $with_home_link = isset($options['home_link']) ? $options['home_link'] : true;
507        $hl             = isset($options['hl']) ? $options['hl'] : true;
508        $hl_pos         = isset($options['hl_pos']) ? $options['hl_pos'] : -1;
509        // First item of array elements should be blog's name, System or Plugins
510        $res = '<h2>' . ($with_home_link ?
511            '<a class="go_home" href="' . $core->adminurl->get("admin.home") . '"><img src="style/dashboard.png" alt="' . __('Go to dashboard') . '" /></a>' :
512            '<img src="style/dashboard-alt.png" alt="" />');
513        $index = 0;
514        if ($hl_pos < 0) {
515            $hl_pos = count($elements) + $hl_pos;
516        }
517        foreach ($elements as $element => $url) {
518            if ($hl && $index == $hl_pos) {
519                $element = sprintf('<span class="page-title">%s</span>', $element);
520            }
521            $res .= ($with_home_link ? ($index == 1 ? ' : ' : ' &rsaquo; ') : ($index == 0 ? ' ' : ' &rsaquo; ')) .
522                ($url ? '<a href="' . $url . '">' : '') . $element . ($url ? '</a>' : '');
523            $index++;
524        }
525        $res .= '</h2>';
526        return $res;
527    }
528
529    public static function message($msg, $timestamp = true, $div = false, $echo = true, $class = 'message')
530    {
531        global $core;
532
533        $res = '';
534        if ($msg != '') {
535            $res = ($div ? '<div class="' . $class . '">' : '') . '<p' . ($div ? '' : ' class="' . $class . '"') . '>' .
536                ($timestamp ? dt::str(__('[%H:%M:%S]'), null, $core->auth->getInfo('user_tz')) . ' ' : '') . $msg .
537                '</p>' . ($div ? '</div>' : '');
538            if ($echo) {
539                echo $res;
540            }
541        }
542        return $res;
543    }
544
545    public static function success($msg, $timestamp = true, $div = false, $echo = true)
546    {
547        return self::message($msg, $timestamp, $div, $echo, "success");
548    }
549
550    public static function warning($msg, $timestamp = true, $div = false, $echo = true)
551    {
552        return self::message($msg, $timestamp, $div, $echo, "warning-msg");
553    }
554
555    private static function debugInfo()
556    {
557        $global_vars = implode(', ', array_keys($GLOBALS));
558
559        $res =
560        '<div id="debug"><div>' .
561        '<p>memory usage: ' . memory_get_usage() . ' (' . files::size(memory_get_usage()) . ')</p>';
562
563        if (function_exists('xdebug_get_profiler_filename')) {
564            $res .= '<p>Elapsed time: ' . xdebug_time_index() . ' seconds</p>';
565
566            $prof_file = xdebug_get_profiler_filename();
567            if ($prof_file) {
568                $res .= '<p>Profiler file : ' . xdebug_get_profiler_filename() . '</p>';
569            } else {
570                $prof_url = http::getSelfURI();
571                $prof_url .= (strpos($prof_url, '?') === false) ? '?' : '&';
572                $prof_url .= 'XDEBUG_PROFILE';
573                $res .= '<p><a href="' . html::escapeURL($prof_url) . '">Trigger profiler</a></p>';
574            }
575
576            /* xdebug configuration:
577        zend_extension = /.../xdebug.so
578        xdebug.auto_trace = On
579        xdebug.trace_format = 0
580        xdebug.trace_options = 1
581        xdebug.show_mem_delta = On
582        xdebug.profiler_enable = 0
583        xdebug.profiler_enable_trigger = 1
584        xdebug.profiler_output_dir = /tmp
585        xdebug.profiler_append = 0
586        xdebug.profiler_output_name = timestamp
587         */
588        }
589
590        $res .=
591            '<p>Global vars: ' . $global_vars . '</p>' .
592            '</div></div>';
593
594        return $res;
595    }
596
597    public static function help($page, $index = '')
598    {
599        # Deprecated but we keep this for plugins.
600    }
601
602    public static function helpBlock()
603    {
604        global $core;
605
606        if ($core->auth->user_prefs->interface->hidehelpbutton) {
607            return;
608        }
609
610        $args = func_get_args();
611        $args = new ArrayObject($args);
612
613        # --BEHAVIOR-- adminPageHelpBlock
614        $GLOBALS['core']->callBehavior('adminPageHelpBlock', $args);
615
616        if (empty($args)) {
617            return;
618        };
619
620        global $__resources;
621        if (empty($__resources['help'])) {
622            return;
623        }
624
625        $content = '';
626        foreach ($args as $v) {
627            if (is_object($v) && isset($v->content)) {
628                $content .= $v->content;
629                continue;
630            }
631
632            if (!isset($__resources['help'][$v])) {
633                continue;
634            }
635            $f = $__resources['help'][$v];
636            if (!file_exists($f) || !is_readable($f)) {
637                continue;
638            }
639
640            $fc = file_get_contents($f);
641            if (preg_match('|<body[^>]*?>(.*?)</body>|ms', $fc, $matches)) {
642                $content .= $matches[1];
643            } else {
644                $content .= $fc;
645            }
646        }
647
648        if (trim($content) == '') {
649            return;
650        }
651
652        // Set contextual help global flag
653        $GLOBALS['__resources']['ctxhelp'] = true;
654
655        echo
656        '<div id="help"><hr /><div class="help-content clear"><h3>' . __('Help about this page') . '</h3>' .
657        $content .
658        '</div>' .
659        '<div id="helplink"><hr />' .
660        '<p>' .
661        sprintf(__('See also %s'), sprintf('<a href="' . $core->adminurl->get("admin.help") . '">%s</a>', __('the global help'))) .
662            '.</p>' .
663            '</div></div>';
664    }
665
666    public static function cssLoad($src, $media = 'screen', $v = '')
667    {
668        $escaped_src = html::escapeHTML($src);
669        if (!isset(self::$loaded_css[$escaped_src])) {
670            self::$loaded_css[$escaped_src] = true;
671            $escaped_src                    = self::appendVersion($escaped_src, $v);
672
673            return '<link rel="stylesheet" href="' . $escaped_src . '" type="text/css" media="' . $media . '" />' . "\n";
674        }
675    }
676
677    public static function jsLoad($src, $v = '')
678    {
679        $escaped_src = html::escapeHTML($src);
680        if (!isset(self::$loaded_js[$escaped_src])) {
681            self::$loaded_js[$escaped_src] = true;
682            $escaped_src                   = self::appendVersion($escaped_src, $v);
683            return '<script type="text/javascript" src="' . $escaped_src . '"></script>' . "\n";
684        }
685    }
686
687    private static function appendVersion($src, $v = '')
688    {
689        $src .= (strpos($src, '?') === false ? '?' : '&amp;') . 'v=';
690        if (defined('DC_DEV') && DC_DEV === true) {
691            $src .= md5(uniqid());
692        } else {
693            $src .= ($v === '' ? DC_VERSION : $v);
694        }
695        return $src;
696    }
697
698    public static function jsVar($n, $v)
699    {
700        return $n . " = '" . html::escapeJS($v) . "';\n";
701    }
702
703    public static function jsVars($vars)
704    {
705        $ret = '<script type="text/javascript">' . "\n";
706        foreach ($vars as $var => $value) {
707            $ret .= $var . ' = ' . (is_string($value) ? "'" . html::escapeJS($value) . "'" : $value) . ';' . "\n";
708        }
709        $ret .= "</script>\n";
710
711        return $ret;
712    }
713
714    public static function jsToggles()
715    {
716        if ($GLOBALS['core']->auth->user_prefs->toggles) {
717            $unfolded_sections = explode(',', $GLOBALS['core']->auth->user_prefs->toggles->unfolded_sections);
718            foreach ($unfolded_sections as $k => &$v) {
719                if ($v == '') {
720                    unset($unfolded_sections[$k]);
721                } else {
722                    $v = "'" . html::escapeJS($v) . "':true";
723                }
724            }
725        } else {
726            $unfolded_sections = array();
727        }
728        return '<script type="text/javascript">' . "\n" .
729        'dotclear.unfolded_sections = {' . join(",", $unfolded_sections) . "};\n" .
730            "</script>\n";
731    }
732
733    public static function jsCommon()
734    {
735        $mute_or_no = '';
736        if (empty($GLOBALS['core']->blog) || $GLOBALS['core']->blog->settings->system->jquery_migrate_mute) {
737            $mute_or_no .=
738                '<script type="text/javascript">' . "\n" .
739                'jQuery.migrateMute = true;' . "\n" .
740                "</script>\n";
741        }
742
743        return
744        self::jsLoad('js/jquery/jquery.js') .
745        $mute_or_no .
746        self::jsLoad('js/jquery/jquery-migrate.js') .
747        self::jsLoad('js/jquery/jquery.biscuit.js') .
748        self::jsLoad('js/common.js') .
749        self::jsLoad('js/prelude.js') .
750
751        '<script type="text/javascript">' . "\n" .
752        'jsToolBar = {}, jsToolBar.prototype = { elements : {} };' . "\n" .
753
754        self::jsVar('dotclear.nonce', $GLOBALS['core']->getNonce()) .
755
756        self::jsVar('dotclear.img_plus_src', 'images/expand.png') .
757        self::jsVar('dotclear.img_plus_txt', '►') .
758        self::jsVar('dotclear.img_plus_alt', __('uncover')) .
759        self::jsVar('dotclear.img_minus_src', 'images/hide.png') .
760        self::jsVar('dotclear.img_minus_txt', '▼') .
761        self::jsVar('dotclear.img_minus_alt', __('hide')) .
762        self::jsVar('dotclear.img_menu_on', 'images/menu_on.png') .
763        self::jsVar('dotclear.img_menu_off', 'images/menu_off.png') .
764
765        self::jsVar('dotclear.img_plus_theme_src', 'images/plus-theme.png') .
766        self::jsVar('dotclear.img_plus_theme_txt', '►') .
767        self::jsVar('dotclear.img_plus_theme_alt', __('uncover')) .
768        self::jsVar('dotclear.img_minus_theme_src', 'images/minus-theme.png') .
769        self::jsVar('dotclear.img_minus_theme_txt', '▼') .
770        self::jsVar('dotclear.img_minus_theme_alt', __('hide')) .
771
772        self::jsVar('dotclear.msg.help',
773            __('Need help?')) .
774        self::jsVar('dotclear.msg.new_window',
775            __('new window')) .
776        self::jsVar('dotclear.msg.help_hide',
777            __('Hide')) .
778        self::jsVar('dotclear.msg.to_select',
779            __('Select:')) .
780        self::jsVar('dotclear.msg.no_selection',
781            __('no selection')) .
782        self::jsVar('dotclear.msg.select_all',
783            __('select all')) .
784        self::jsVar('dotclear.msg.invert_sel',
785            __('Invert selection')) .
786        self::jsVar('dotclear.msg.website',
787            __('Web site:')) .
788        self::jsVar('dotclear.msg.email',
789            __('Email:')) .
790        self::jsVar('dotclear.msg.ip_address',
791            __('IP address:')) .
792        self::jsVar('dotclear.msg.error',
793            __('Error:')) .
794        self::jsVar('dotclear.msg.entry_created',
795            __('Entry has been successfully created.')) .
796        self::jsVar('dotclear.msg.edit_entry',
797            __('Edit entry')) .
798        self::jsVar('dotclear.msg.view_entry',
799            __('view entry')) .
800        self::jsVar('dotclear.msg.confirm_delete_posts',
801            __("Are you sure you want to delete selected entries (%s)?")) .
802        self::jsVar('dotclear.msg.confirm_delete_medias',
803            __("Are you sure you want to delete selected medias (%d)?")) .
804        self::jsVar('dotclear.msg.confirm_delete_categories',
805            __("Are you sure you want to delete selected categories (%s)?")) .
806        self::jsVar('dotclear.msg.confirm_delete_post',
807            __("Are you sure you want to delete this entry?")) .
808        self::jsVar('dotclear.msg.click_to_unlock',
809            __("Click here to unlock the field")) .
810        self::jsVar('dotclear.msg.confirm_spam_delete',
811            __('Are you sure you want to delete all spams?')) .
812        self::jsVar('dotclear.msg.confirm_delete_comments',
813            __('Are you sure you want to delete selected comments (%s)?')) .
814        self::jsVar('dotclear.msg.confirm_delete_comment',
815            __('Are you sure you want to delete this comment?')) .
816        self::jsVar('dotclear.msg.cannot_delete_users',
817            __('Users with posts cannot be deleted.')) .
818        self::jsVar('dotclear.msg.confirm_delete_user',
819            __('Are you sure you want to delete selected users (%s)?')) .
820        self::jsVar('dotclear.msg.confirm_delete_blog',
821            __('Are you sure you want to delete selected blogs (%s)?')) .
822        self::jsVar('dotclear.msg.confirm_delete_category',
823            __('Are you sure you want to delete category "%s"?')) .
824        self::jsVar('dotclear.msg.confirm_reorder_categories',
825            __('Are you sure you want to reorder all categories?')) .
826        self::jsVar('dotclear.msg.confirm_delete_media',
827            __('Are you sure you want to remove media "%s"?')) .
828        self::jsVar('dotclear.msg.confirm_delete_directory',
829            __('Are you sure you want to remove directory "%s"?')) .
830        self::jsVar('dotclear.msg.confirm_extract_current',
831            __('Are you sure you want to extract archive in current directory?')) .
832        self::jsVar('dotclear.msg.confirm_remove_attachment',
833            __('Are you sure you want to remove attachment "%s"?')) .
834        self::jsVar('dotclear.msg.confirm_delete_lang',
835            __('Are you sure you want to delete "%s" language?')) .
836        self::jsVar('dotclear.msg.confirm_delete_plugin',
837            __('Are you sure you want to delete "%s" plugin?')) .
838        self::jsVar('dotclear.msg.confirm_delete_plugins',
839            __('Are you sure you want to delete selected plugins?')) .
840        self::jsVar('dotclear.msg.use_this_theme',
841            __('Use this theme')) .
842        self::jsVar('dotclear.msg.remove_this_theme',
843            __('Remove this theme')) .
844        self::jsVar('dotclear.msg.confirm_delete_theme',
845            __('Are you sure you want to delete "%s" theme?')) .
846        self::jsVar('dotclear.msg.confirm_delete_themes',
847            __('Are you sure you want to delete selected themes?')) .
848        self::jsVar('dotclear.msg.confirm_delete_backup',
849            __('Are you sure you want to delete this backup?')) .
850        self::jsVar('dotclear.msg.confirm_revert_backup',
851            __('Are you sure you want to revert to this backup?')) .
852        self::jsVar('dotclear.msg.zip_file_content',
853            __('Zip file content')) .
854        self::jsVar('dotclear.msg.xhtml_validator',
855            __('XHTML markup validator')) .
856        self::jsVar('dotclear.msg.xhtml_valid',
857            __('XHTML content is valid.')) .
858        self::jsVar('dotclear.msg.xhtml_not_valid',
859            __('There are XHTML markup errors.')) .
860        self::jsVar('dotclear.msg.warning_validate_no_save_content',
861            __('Attention: an audit of a content not yet registered.')) .
862        self::jsVar('dotclear.msg.confirm_change_post_format',
863            __('You have unsaved changes. Switch post format will loose these changes. Proceed anyway?')) .
864        self::jsVar('dotclear.msg.confirm_change_post_format_noconvert',
865            __("Warning: post format change will not convert existing content. You will need to apply new format by yourself. Proceed anyway?")) .
866        self::jsVar('dotclear.msg.load_enhanced_uploader',
867            __('Loading enhanced uploader, please wait.')) .
868
869        self::jsVar('dotclear.msg.module_author',
870            __('Author:')) .
871        self::jsVar('dotclear.msg.module_details',
872            __('Details')) .
873        self::jsVar('dotclear.msg.module_support',
874            __('Support')) .
875        self::jsVar('dotclear.msg.module_help',
876            __('Help:')) .
877        self::jsVar('dotclear.msg.module_section',
878            __('Section:')) .
879        self::jsVar('dotclear.msg.module_tags',
880            __('Tags:')) .
881
882        self::jsVar('dotclear.msg.close_notice',
883            __('Hide this notice')) .
884
885            "\n" .
886            "</script>\n";
887    }
888
889    /**
890    @deprecated since version 2.11
891     */
892    public static function jsLoadIE7()
893    {
894        return '';
895    }
896
897    public static function jsConfirmClose()
898    {
899        $args = func_get_args();
900        if (count($args) > 0) {
901            foreach ($args as $k => $v) {
902                $args[$k] = "'" . html::escapeJS($v) . "'";
903            }
904            $args = implode(',', $args);
905        } else {
906            $args = '';
907        }
908
909        return
910        self::jsLoad('js/confirm-close.js') .
911        '<script type="text/javascript">' . "\n" .
912        "confirmClosePage = new confirmClose(" . $args . "); " .
913        "confirmClose.prototype.prompt = '" . html::escapeJS(__('You have unsaved changes.')) . "'; " .
914            "</script>\n";
915    }
916
917    public static function jsPageTabs($default = null)
918    {
919        if ($default) {
920            $default = "'" . html::escapeJS($default) . "'";
921        }
922
923        return
924        self::jsLoad('js/jquery/jquery.pageTabs.js') .
925            '<script type="text/javascript">' . "\n" .
926            '$(function() {' . "\n" .
927            '   $.pageTabs(' . $default . ');' . "\n" .
928            '});' .
929            "</script>\n";
930    }
931
932    public static function jsModal()
933    {
934        return
935        self::jsLoad('js/jquery/jquery.magnific-popup.js');
936    }
937
938    public static function jsColorPicker()
939    {
940        return
941        self::cssLoad('style/farbtastic/farbtastic.css') .
942        self::jsLoad('js/jquery/jquery.farbtastic.js') .
943        self::jsLoad('js/color-picker.js');
944    }
945
946    public static function jsDatePicker()
947    {
948        return
949        self::cssLoad('style/date-picker.css') .
950        self::jsLoad('js/date-picker.js') .
951        '<script type="text/javascript">' . "\n" .
952
953        "datePicker.prototype.months[0] = '" . html::escapeJS(__('January')) . "'; " .
954        "datePicker.prototype.months[1] = '" . html::escapeJS(__('February')) . "'; " .
955        "datePicker.prototype.months[2] = '" . html::escapeJS(__('March')) . "'; " .
956        "datePicker.prototype.months[3] = '" . html::escapeJS(__('April')) . "'; " .
957        "datePicker.prototype.months[4] = '" . html::escapeJS(__('May')) . "'; " .
958        "datePicker.prototype.months[5] = '" . html::escapeJS(__('June')) . "'; " .
959        "datePicker.prototype.months[6] = '" . html::escapeJS(__('July')) . "'; " .
960        "datePicker.prototype.months[7] = '" . html::escapeJS(__('August')) . "'; " .
961        "datePicker.prototype.months[8] = '" . html::escapeJS(__('September')) . "'; " .
962        "datePicker.prototype.months[9] = '" . html::escapeJS(__('October')) . "'; " .
963        "datePicker.prototype.months[10] = '" . html::escapeJS(__('November')) . "'; " .
964        "datePicker.prototype.months[11] = '" . html::escapeJS(__('December')) . "'; " .
965
966        "datePicker.prototype.days[0] = '" . html::escapeJS(__('Monday')) . "'; " .
967        "datePicker.prototype.days[1] = '" . html::escapeJS(__('Tuesday')) . "'; " .
968        "datePicker.prototype.days[2] = '" . html::escapeJS(__('Wednesday')) . "'; " .
969        "datePicker.prototype.days[3] = '" . html::escapeJS(__('Thursday')) . "'; " .
970        "datePicker.prototype.days[4] = '" . html::escapeJS(__('Friday')) . "'; " .
971        "datePicker.prototype.days[5] = '" . html::escapeJS(__('Saturday')) . "'; " .
972        "datePicker.prototype.days[6] = '" . html::escapeJS(__('Sunday')) . "'; " .
973
974        "datePicker.prototype.img_src = 'images/date-picker.png'; " .
975        "datePicker.prototype.img_alt = '" . html::escapeJS(__('Choose date')) . "'; " .
976
977        "datePicker.prototype.close_msg = '" . html::escapeJS(__('close')) . "'; " .
978        "datePicker.prototype.now_msg = '" . html::escapeJS(__('now')) . "'; " .
979
980            "</script>\n";
981    }
982
983    public static function jsToolBar()
984    {
985        # Deprecated but we keep this for plugins.
986    }
987
988    public static function jsUpload($params = array(), $base_url = null)
989    {
990        if (!$base_url) {
991            $base_url = path::clean(dirname(preg_replace('/(\?.*$)?/', '', $_SERVER['REQUEST_URI']))) . '/';
992        }
993
994        $params = array_merge($params, array(
995            'sess_id=' . session_id(),
996            'sess_uid=' . $_SESSION['sess_browser_uid'],
997            'xd_check=' . $GLOBALS['core']->getNonce()
998        ));
999
1000        return
1001        '<script type="text/javascript">' . "\n" .
1002        "dotclear.jsUpload = {};\n" .
1003        "dotclear.jsUpload.msg = {};\n" .
1004        self::jsVar('dotclear.msg.enhanced_uploader_activate', __('Temporarily activate enhanced uploader')) .
1005        self::jsVar('dotclear.msg.enhanced_uploader_disable', __('Temporarily disable enhanced uploader')) .
1006        self::jsVar('dotclear.jsUpload.msg.limit_exceeded', __('Limit exceeded.')) .
1007        self::jsVar('dotclear.jsUpload.msg.size_limit_exceeded', __('File size exceeds allowed limit.')) .
1008        self::jsVar('dotclear.jsUpload.msg.canceled', __('Canceled.')) .
1009        self::jsVar('dotclear.jsUpload.msg.http_error', __('HTTP Error:')) .
1010        self::jsVar('dotclear.jsUpload.msg.error', __('Error:')) .
1011        self::jsVar('dotclear.jsUpload.msg.choose_file', __('Choose file')) .
1012        self::jsVar('dotclear.jsUpload.msg.choose_files', __('Choose files')) .
1013        self::jsVar('dotclear.jsUpload.msg.cancel', __('Cancel')) .
1014        self::jsVar('dotclear.jsUpload.msg.clean', __('Clean')) .
1015        self::jsVar('dotclear.jsUpload.msg.upload', __('Upload')) .
1016        self::jsVar('dotclear.jsUpload.msg.send', __('Send')) .
1017        self::jsVar('dotclear.jsUpload.msg.file_successfully_uploaded', __('File successfully uploaded.')) .
1018        self::jsVar('dotclear.jsUpload.msg.no_file_in_queue', __('No file in queue.')) .
1019        self::jsVar('dotclear.jsUpload.msg.file_in_queue', __('1 file in queue.')) .
1020        self::jsVar('dotclear.jsUpload.msg.files_in_queue', __('%d files in queue.')) .
1021        self::jsVar('dotclear.jsUpload.msg.queue_error', __('Queue error:')) .
1022        self::jsVar('dotclear.jsUpload.base_url', $base_url) .
1023        "</script>\n" .
1024
1025        self::jsLoad('js/jquery/jquery-ui.custom.js') .
1026        self::jsLoad('js/jsUpload/tmpl.js') .
1027        self::jsLoad('js/jsUpload/template-upload.js') .
1028        self::jsLoad('js/jsUpload/template-download.js') .
1029        self::jsLoad('js/jsUpload/load-image.js') .
1030        self::jsLoad('js/jsUpload/jquery.iframe-transport.js') .
1031        self::jsLoad('js/jsUpload/jquery.fileupload.js') .
1032        self::jsLoad('js/jsUpload/jquery.fileupload-process.js') .
1033        self::jsLoad('js/jsUpload/jquery.fileupload-resize.js') .
1034        self::jsLoad('js/jsUpload/jquery.fileupload-ui.js');
1035    }
1036
1037    public static function jsMetaEditor()
1038    {
1039        return self::jsLoad('js/meta-editor.js');
1040    }
1041
1042    public static function jsFilterControl($show = true)
1043    {
1044        return
1045        self::jsLoad('js/filter-controls.js') .
1046        '<script type="text/javascript">' . "\n" .
1047        self::jsVar('dotclear.msg.show_filters', $show ? 'true' : 'false') . "\n" .
1048        self::jsVar('dotclear.msg.filter_posts_list', __('Show filters and display options')) . "\n" .
1049        self::jsVar('dotclear.msg.cancel_the_filter', __('Cancel filters and display options')) . "\n" .
1050            "</script>";
1051    }
1052
1053    public static function jsLoadCodeMirror($theme = '', $multi = true, $modes = array('css', 'htmlmixed', 'javascript', 'php', 'xml'))
1054    {
1055        $ret =
1056        self::cssLoad('js/codemirror/lib/codemirror.css') .
1057        self::jsLoad('js/codemirror/lib/codemirror.js');
1058        if ($multi) {
1059            $ret .= self::jsLoad('js/codemirror/addon/mode/multiplex.js');
1060        }
1061        foreach ($modes as $mode) {
1062            $ret .= self::jsLoad('js/codemirror/mode/' . $mode . '/' . $mode . '.js');
1063        }
1064        $ret .=
1065        self::jsLoad('js/codemirror/addon/edit/closebrackets.js') .
1066        self::jsLoad('js/codemirror/addon/edit/matchbrackets.js') .
1067        self::cssLoad('js/codemirror/addon/display/fullscreen.css') .
1068        self::jsLoad('js/codemirror/addon/display/fullscreen.js');
1069        if ($theme != '') {
1070            $ret .= self::cssLoad('js/codemirror/theme/' . $theme . '.css');
1071        }
1072        return $ret;
1073    }
1074
1075    public static function jsRunCodeMirror($name, $id, $mode, $theme = '')
1076    {
1077        $ret =
1078            '<script type="text/javascript">' . "\n" .
1079            'var ' . $name . ' = CodeMirror.fromTextArea(' . $id . ',{' . "\n" .
1080            '   mode: "' . $mode . '",' . "\n" .
1081            '   tabMode: "indent",' . "\n" .
1082            '   lineWrapping: 1,' . "\n" .
1083            '   lineNumbers: 1,' . "\n" .
1084            '   matchBrackets: 1,' . "\n" .
1085            '   autoCloseBrackets: 1,' . "\n" .
1086            '   extraKeys: {"F11": function(cm) {cm.setOption("fullScreen",!cm.getOption("fullScreen"));}}';
1087        if ($theme) {
1088            $ret .=
1089                ',' . "\n" .
1090                '   theme: "' . $theme . '"';
1091        }
1092        $ret .= "\n" .
1093            '});' . "\n" .
1094            '</script>';
1095        return $ret;
1096    }
1097
1098    public static function getCodeMirrorThemes()
1099    {
1100        $themes      = array();
1101        $themes_root = dirname(__FILE__) . '/../../admin' . '/js/codemirror/theme/';
1102        if (is_dir($themes_root) && is_readable($themes_root)) {
1103            if (($d = @dir($themes_root)) !== false) {
1104                while (($entry = $d->read()) !== false) {
1105                    if ($entry != '.' && $entry != '..' && substr($entry, 0, 1) != '.' && is_readable($themes_root . '/' . $entry)) {
1106                        $themes[] = substr($entry, 0, -4); // remove .css extension
1107                    }
1108                }
1109                sort($themes);
1110            }
1111        }
1112        return $themes;
1113    }
1114
1115    public static function getPF($file)
1116    {
1117        return $GLOBALS['core']->adminurl->get('load.plugin.file', array('pf' => $file));
1118    }
1119
1120    public static function getVF($file)
1121    {
1122        return $GLOBALS['core']->adminurl->get('load.var.file', array('vf' => $file));
1123    }
1124
1125    public static function setXFrameOptions($headers, $origin = null)
1126    {
1127        if (self::$xframe_loaded) {
1128            return;
1129        }
1130
1131        if ($origin !== null) {
1132            $url                        = parse_url($origin);
1133            $headers['x-frame-options'] = sprintf('X-Frame-Options: %s', is_array($url) && isset($url['host']) ?
1134                ("ALLOW-FROM " . (isset($url['scheme']) ? $url['scheme'] . ':' : '') . '//' . $url['host']) :
1135                'SAMEORIGIN');
1136        } else {
1137            $headers['x-frame-options'] = 'X-Frame-Options: SAMEORIGIN'; // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
1138        }
1139        self::$xframe_loaded = true;
1140    }
1141}
Note: See TracBrowser for help on using the repository browser.

Sites map