Dotclear

source: inc/admin/lib.dc.page.php @ 3731:3770620079d4

Revision 3731:3770620079d4, 44.6 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Simplify licence block at the beginning of each file

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
398        echo
399            '<div id="footer" role="contentinfo">' .
400            '<a href="http://dotclear.org/" title="' . $text . '">' .
401            '<img src="style/dc_logos/w-dotclear90.png" alt="' . $text . '" /></a></div>' . "\n" .
402            "<!-- " . "\n" .
403            $figure .
404            " -->" . "\n";
405
406        if (defined('DC_DEV') && DC_DEV === true) {
407            echo self::debugInfo();
408        }
409
410        echo
411            '</body></html>';
412    }
413
414    public static function openPopup($title = '', $head = '', $breadcrumb = '')
415    {
416        global $core;
417
418        # Display
419        header('Content-Type: text/html; charset=UTF-8');
420
421        # Referrer Policy for admin pages
422        header('Referrer-Policy: strict-origin');
423
424        # Prevents Clickjacking as far as possible
425        header('X-Frame-Options: SAMEORIGIN'); // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
426
427        echo
428        '<!DOCTYPE html>' .
429        '<html lang="' . $core->auth->getInfo('user_lang') . '">' . "\n" .
430        "<head>\n" .
431        '  <meta charset="UTF-8" />' . "\n" .
432        '  <meta name="viewport" content="width=device-width, initial-scale=1.0" />' . "\n" .
433        '  <title>' . $title . ' - ' . html::escapeHTML($core->blog->name) . ' - ' . html::escapeHTML(DC_VENDOR_NAME) . ' - ' . DC_VERSION . '</title>' . "\n" .
434
435        '  <meta name="ROBOTS" content="NOARCHIVE,NOINDEX,NOFOLLOW" />' . "\n" .
436        '  <meta name="GOOGLEBOT" content="NOSNIPPET" />' . "\n" .
437
438        self::cssLoad('style/default.css');
439        if (l10n::getTextDirection($GLOBALS['_lang']) == 'rtl') {
440            echo self::cssLoad('style/default-rtl.css');
441        }
442
443        $core->auth->user_prefs->addWorkspace('interface');
444        if ($core->auth->user_prefs->interface->htmlfontsize) {
445            echo
446            '<script type="text/javascript">' . "\n" .
447            self::jsVar('dotclear_htmlFontSize', $core->auth->user_prefs->interface->htmlfontsize) . "\n" .
448                "</script>\n";
449        }
450
451        echo
452        self::jsCommon() .
453        self::jsToggles() .
454            $head;
455
456        if ($core->auth->user_prefs->interface->hidemoreinfo) {
457            echo
458                '<script type="text/javascript">' . "\n" .
459                'dotclear.hideMoreInfo = true;' . "\n" .
460                "</script>\n";
461        }
462        if ($core->auth->user_prefs->interface->showajaxloader) {
463            echo
464                '<script type="text/javascript">' . "\n" .
465                'dotclear.showAjaxLoader = true;' . "\n" .
466                "</script>\n";
467        }
468
469        # --BEHAVIOR-- adminPageHTMLHead
470        $core->callBehavior('adminPageHTMLHead');
471
472        echo
473            "</head>\n" .
474            '<body id="dotclear-admin" class="popup' .
475            ($core->auth->user_prefs->interface->dynfontsize ? ' responsive-font' : '') . '">' . "\n" .
476
477            '<h1>' . DC_VENDOR_NAME . '</h1>' . "\n";
478
479        echo
480            '<div id="wrapper">' . "\n" .
481            '<div id="main" role="main">' . "\n" .
482            '<div id="content">' . "\n";
483
484        // display breadcrumb if given
485        echo $breadcrumb;
486
487        // Display notices and errors
488        echo self::notices();
489    }
490
491    public static function closePopup()
492    {
493        echo
494        "</div>\n" . // End of #content
495        "</div>\n" . // End of #main
496        "</div>\n" . // End of #wrapper
497
498        '<p id="gototop"><a href="#wrapper">' . __('Page top') . '</a></p>' . "\n" .
499
500            '<div id="footer" role="contentinfo"><p>&nbsp;</p></div>' . "\n" .
501            '</body></html>';
502    }
503
504    public static function breadcrumb($elements = null, $options = array())
505    {
506        global $core;
507        $with_home_link = isset($options['home_link']) ? $options['home_link'] : true;
508        $hl             = isset($options['hl']) ? $options['hl'] : true;
509        $hl_pos         = isset($options['hl_pos']) ? $options['hl_pos'] : -1;
510        // First item of array elements should be blog's name, System or Plugins
511        $res = '<h2>' . ($with_home_link ?
512            '<a class="go_home" href="' . $core->adminurl->get("admin.home") . '"><img src="style/dashboard.png" alt="' . __('Go to dashboard') . '" /></a>' :
513            '<img src="style/dashboard-alt.png" alt="" />');
514        $index = 0;
515        if ($hl_pos < 0) {
516            $hl_pos = count($elements) + $hl_pos;
517        }
518        foreach ($elements as $element => $url) {
519            if ($hl && $index == $hl_pos) {
520                $element = sprintf('<span class="page-title">%s</span>', $element);
521            }
522            $res .= ($with_home_link ? ($index == 1 ? ' : ' : ' &rsaquo; ') : ($index == 0 ? ' ' : ' &rsaquo; ')) .
523                ($url ? '<a href="' . $url . '">' : '') . $element . ($url ? '</a>' : '');
524            $index++;
525        }
526        $res .= '</h2>';
527        return $res;
528    }
529
530    public static function message($msg, $timestamp = true, $div = false, $echo = true, $class = 'message')
531    {
532        global $core;
533
534        $res = '';
535        if ($msg != '') {
536            $res = ($div ? '<div class="' . $class . '">' : '') . '<p' . ($div ? '' : ' class="' . $class . '"') . '>' .
537                ($timestamp ? dt::str(__('[%H:%M:%S]'), null, $core->auth->getInfo('user_tz')) . ' ' : '') . $msg .
538                '</p>' . ($div ? '</div>' : '');
539            if ($echo) {
540                echo $res;
541            }
542        }
543        return $res;
544    }
545
546    public static function success($msg, $timestamp = true, $div = false, $echo = true)
547    {
548        return self::message($msg, $timestamp, $div, $echo, "success");
549    }
550
551    public static function warning($msg, $timestamp = true, $div = false, $echo = true)
552    {
553        return self::message($msg, $timestamp, $div, $echo, "warning-msg");
554    }
555
556    private static function debugInfo()
557    {
558        $global_vars = implode(', ', array_keys($GLOBALS));
559
560        $res =
561        '<div id="debug"><div>' .
562        '<p>memory usage: ' . memory_get_usage() . ' (' . files::size(memory_get_usage()) . ')</p>';
563
564        if (function_exists('xdebug_get_profiler_filename')) {
565            $res .= '<p>Elapsed time: ' . xdebug_time_index() . ' seconds</p>';
566
567            $prof_file = xdebug_get_profiler_filename();
568            if ($prof_file) {
569                $res .= '<p>Profiler file : ' . xdebug_get_profiler_filename() . '</p>';
570            } else {
571                $prof_url = http::getSelfURI();
572                $prof_url .= (strpos($prof_url, '?') === false) ? '?' : '&';
573                $prof_url .= 'XDEBUG_PROFILE';
574                $res .= '<p><a href="' . html::escapeURL($prof_url) . '">Trigger profiler</a></p>';
575            }
576
577            /* xdebug configuration:
578        zend_extension = /.../xdebug.so
579        xdebug.auto_trace = On
580        xdebug.trace_format = 0
581        xdebug.trace_options = 1
582        xdebug.show_mem_delta = On
583        xdebug.profiler_enable = 0
584        xdebug.profiler_enable_trigger = 1
585        xdebug.profiler_output_dir = /tmp
586        xdebug.profiler_append = 0
587        xdebug.profiler_output_name = timestamp
588         */
589        }
590
591        $res .=
592            '<p>Global vars: ' . $global_vars . '</p>' .
593            '</div></div>';
594
595        return $res;
596    }
597
598    public static function help($page, $index = '')
599    {
600        # Deprecated but we keep this for plugins.
601    }
602
603    public static function helpBlock()
604    {
605        global $core;
606
607        if ($core->auth->user_prefs->interface->hidehelpbutton) {
608            return;
609        }
610
611        $args = func_get_args();
612        $args = new ArrayObject($args);
613
614        # --BEHAVIOR-- adminPageHelpBlock
615        $GLOBALS['core']->callBehavior('adminPageHelpBlock', $args);
616
617        if (empty($args)) {
618            return;
619        };
620
621        global $__resources;
622        if (empty($__resources['help'])) {
623            return;
624        }
625
626        $content = '';
627        foreach ($args as $v) {
628            if (is_object($v) && isset($v->content)) {
629                $content .= $v->content;
630                continue;
631            }
632
633            if (!isset($__resources['help'][$v])) {
634                continue;
635            }
636            $f = $__resources['help'][$v];
637            if (!file_exists($f) || !is_readable($f)) {
638                continue;
639            }
640
641            $fc = file_get_contents($f);
642            if (preg_match('|<body[^>]*?>(.*?)</body>|ms', $fc, $matches)) {
643                $content .= $matches[1];
644            } else {
645                $content .= $fc;
646            }
647        }
648
649        if (trim($content) == '') {
650            return;
651        }
652
653        // Set contextual help global flag
654        $GLOBALS['__resources']['ctxhelp'] = true;
655
656        echo
657        '<div id="help"><hr /><div class="help-content clear"><h3>' . __('Help about this page') . '</h3>' .
658        $content .
659        '</div>' .
660        '<div id="helplink"><hr />' .
661        '<p>' .
662        sprintf(__('See also %s'), sprintf('<a href="' . $core->adminurl->get("admin.help") . '">%s</a>', __('the global help'))) .
663            '.</p>' .
664            '</div></div>';
665    }
666
667    public static function cssLoad($src, $media = 'screen', $v = '')
668    {
669        $escaped_src = html::escapeHTML($src);
670        if (!isset(self::$loaded_css[$escaped_src])) {
671            self::$loaded_css[$escaped_src] = true;
672            $escaped_src                    = self::appendVersion($escaped_src, $v);
673
674            return '<link rel="stylesheet" href="' . $escaped_src . '" type="text/css" media="' . $media . '" />' . "\n";
675        }
676    }
677
678    public static function jsLoad($src, $v = '')
679    {
680        $escaped_src = html::escapeHTML($src);
681        if (!isset(self::$loaded_js[$escaped_src])) {
682            self::$loaded_js[$escaped_src] = true;
683            $escaped_src                   = self::appendVersion($escaped_src, $v);
684            return '<script type="text/javascript" src="' . $escaped_src . '"></script>' . "\n";
685        }
686    }
687
688    private static function appendVersion($src, $v = '')
689    {
690        $src .= (strpos($src, '?') === false ? '?' : '&amp;') . 'v=';
691        if (defined('DC_DEV') && DC_DEV === true) {
692            $src .= md5(uniqid());
693        } else {
694            $src .= ($v === '' ? DC_VERSION : $v);
695        }
696        return $src;
697    }
698
699    public static function jsVar($n, $v)
700    {
701        return $n . " = '" . html::escapeJS($v) . "';\n";
702    }
703
704    public static function jsVars($vars)
705    {
706        $ret = '<script type="text/javascript">' . "\n";
707        foreach ($vars as $var => $value) {
708            $ret .= $var . ' = ' . (is_string($value) ? "'" . html::escapeJS($value) . "'" : $value) . ';' . "\n";
709        }
710        $ret .= "</script>\n";
711
712        return $ret;
713    }
714
715    public static function jsToggles()
716    {
717        if ($GLOBALS['core']->auth->user_prefs->toggles) {
718            $unfolded_sections = explode(',', $GLOBALS['core']->auth->user_prefs->toggles->unfolded_sections);
719            foreach ($unfolded_sections as $k => &$v) {
720                if ($v == '') {
721                    unset($unfolded_sections[$k]);
722                } else {
723                    $v = "'" . html::escapeJS($v) . "':true";
724                }
725            }
726        } else {
727            $unfolded_sections = array();
728        }
729        return '<script type="text/javascript">' . "\n" .
730        'dotclear.unfolded_sections = {' . join(",", $unfolded_sections) . "};\n" .
731            "</script>\n";
732    }
733
734    public static function jsCommon()
735    {
736        $mute_or_no = '';
737        if (empty($GLOBALS['core']->blog) || $GLOBALS['core']->blog->settings->system->jquery_migrate_mute) {
738            $mute_or_no .=
739                '<script type="text/javascript">' . "\n" .
740                'jQuery.migrateMute = true;' . "\n" .
741                "</script>\n";
742        }
743
744        return
745        self::jsLoad('js/jquery/jquery.js') .
746        $mute_or_no .
747        self::jsLoad('js/jquery/jquery-migrate.js') .
748        self::jsLoad('js/jquery/jquery.biscuit.js') .
749        self::jsLoad('js/common.js') .
750        self::jsLoad('js/prelude.js') .
751
752        '<script type="text/javascript">' . "\n" .
753        'jsToolBar = {}, jsToolBar.prototype = { elements : {} };' . "\n" .
754
755        self::jsVar('dotclear.nonce', $GLOBALS['core']->getNonce()) .
756
757        self::jsVar('dotclear.img_plus_src', 'images/expand.png') .
758        self::jsVar('dotclear.img_plus_alt', __('uncover')) .
759        self::jsVar('dotclear.img_minus_src', 'images/hide.png') .
760        self::jsVar('dotclear.img_minus_alt', __('hide')) .
761        self::jsVar('dotclear.img_menu_on', 'images/menu_on.png') .
762        self::jsVar('dotclear.img_menu_off', 'images/menu_off.png') .
763
764        self::jsVar('dotclear.img_plus_theme_src', 'images/plus-theme.png') .
765        self::jsVar('dotclear.img_plus_theme_alt', __('uncover')) .
766        self::jsVar('dotclear.img_minus_theme_src', 'images/minus-theme.png') .
767        self::jsVar('dotclear.img_minus_theme_alt', __('hide')) .
768
769        self::jsVar('dotclear.msg.help',
770            __('Need help?')) .
771        self::jsVar('dotclear.msg.new_window',
772            __('new window')) .
773        self::jsVar('dotclear.msg.help_hide',
774            __('Hide')) .
775        self::jsVar('dotclear.msg.to_select',
776            __('Select:')) .
777        self::jsVar('dotclear.msg.no_selection',
778            __('no selection')) .
779        self::jsVar('dotclear.msg.select_all',
780            __('select all')) .
781        self::jsVar('dotclear.msg.invert_sel',
782            __('Invert selection')) .
783        self::jsVar('dotclear.msg.website',
784            __('Web site:')) .
785        self::jsVar('dotclear.msg.email',
786            __('Email:')) .
787        self::jsVar('dotclear.msg.ip_address',
788            __('IP address:')) .
789        self::jsVar('dotclear.msg.error',
790            __('Error:')) .
791        self::jsVar('dotclear.msg.entry_created',
792            __('Entry has been successfully created.')) .
793        self::jsVar('dotclear.msg.edit_entry',
794            __('Edit entry')) .
795        self::jsVar('dotclear.msg.view_entry',
796            __('view entry')) .
797        self::jsVar('dotclear.msg.confirm_delete_posts',
798            __("Are you sure you want to delete selected entries (%s)?")) .
799        self::jsVar('dotclear.msg.confirm_delete_medias',
800            __("Are you sure you want to delete selected medias (%d)?")) .
801        self::jsVar('dotclear.msg.confirm_delete_categories',
802            __("Are you sure you want to delete selected categories (%s)?")) .
803        self::jsVar('dotclear.msg.confirm_delete_post',
804            __("Are you sure you want to delete this entry?")) .
805        self::jsVar('dotclear.msg.click_to_unlock',
806            __("Click here to unlock the field")) .
807        self::jsVar('dotclear.msg.confirm_spam_delete',
808            __('Are you sure you want to delete all spams?')) .
809        self::jsVar('dotclear.msg.confirm_delete_comments',
810            __('Are you sure you want to delete selected comments (%s)?')) .
811        self::jsVar('dotclear.msg.confirm_delete_comment',
812            __('Are you sure you want to delete this comment?')) .
813        self::jsVar('dotclear.msg.cannot_delete_users',
814            __('Users with posts cannot be deleted.')) .
815        self::jsVar('dotclear.msg.confirm_delete_user',
816            __('Are you sure you want to delete selected users (%s)?')) .
817        self::jsVar('dotclear.msg.confirm_delete_blog',
818            __('Are you sure you want to delete selected blogs (%s)?')) .
819        self::jsVar('dotclear.msg.confirm_delete_category',
820            __('Are you sure you want to delete category "%s"?')) .
821        self::jsVar('dotclear.msg.confirm_reorder_categories',
822            __('Are you sure you want to reorder all categories?')) .
823        self::jsVar('dotclear.msg.confirm_delete_media',
824            __('Are you sure you want to remove media "%s"?')) .
825        self::jsVar('dotclear.msg.confirm_delete_directory',
826            __('Are you sure you want to remove directory "%s"?')) .
827        self::jsVar('dotclear.msg.confirm_extract_current',
828            __('Are you sure you want to extract archive in current directory?')) .
829        self::jsVar('dotclear.msg.confirm_remove_attachment',
830            __('Are you sure you want to remove attachment "%s"?')) .
831        self::jsVar('dotclear.msg.confirm_delete_lang',
832            __('Are you sure you want to delete "%s" language?')) .
833        self::jsVar('dotclear.msg.confirm_delete_plugin',
834            __('Are you sure you want to delete "%s" plugin?')) .
835        self::jsVar('dotclear.msg.confirm_delete_plugins',
836            __('Are you sure you want to delete selected plugins?')) .
837        self::jsVar('dotclear.msg.use_this_theme',
838            __('Use this theme')) .
839        self::jsVar('dotclear.msg.remove_this_theme',
840            __('Remove this theme')) .
841        self::jsVar('dotclear.msg.confirm_delete_theme',
842            __('Are you sure you want to delete "%s" theme?')) .
843        self::jsVar('dotclear.msg.confirm_delete_themes',
844            __('Are you sure you want to delete selected themes?')) .
845        self::jsVar('dotclear.msg.confirm_delete_backup',
846            __('Are you sure you want to delete this backup?')) .
847        self::jsVar('dotclear.msg.confirm_revert_backup',
848            __('Are you sure you want to revert to this backup?')) .
849        self::jsVar('dotclear.msg.zip_file_content',
850            __('Zip file content')) .
851        self::jsVar('dotclear.msg.xhtml_validator',
852            __('XHTML markup validator')) .
853        self::jsVar('dotclear.msg.xhtml_valid',
854            __('XHTML content is valid.')) .
855        self::jsVar('dotclear.msg.xhtml_not_valid',
856            __('There are XHTML markup errors.')) .
857        self::jsVar('dotclear.msg.warning_validate_no_save_content',
858            __('Attention: an audit of a content not yet registered.')) .
859        self::jsVar('dotclear.msg.confirm_change_post_format',
860            __('You have unsaved changes. Switch post format will loose these changes. Proceed anyway?')) .
861        self::jsVar('dotclear.msg.confirm_change_post_format_noconvert',
862            __("Warning: post format change will not convert existing content. You will need to apply new format by yourself. Proceed anyway?")) .
863        self::jsVar('dotclear.msg.load_enhanced_uploader',
864            __('Loading enhanced uploader, please wait.')) .
865
866        self::jsVar('dotclear.msg.module_author',
867            __('Author:')) .
868        self::jsVar('dotclear.msg.module_details',
869            __('Details')) .
870        self::jsVar('dotclear.msg.module_support',
871            __('Support')) .
872        self::jsVar('dotclear.msg.module_help',
873            __('Help:')) .
874        self::jsVar('dotclear.msg.module_section',
875            __('Section:')) .
876        self::jsVar('dotclear.msg.module_tags',
877            __('Tags:')) .
878
879        self::jsVar('dotclear.msg.close_notice',
880            __('Hide this notice')) .
881
882            "\n" .
883            "</script>\n";
884    }
885
886    /**
887    @deprecated since version 2.11
888     */
889    public static function jsLoadIE7()
890    {
891        return '';
892    }
893
894    public static function jsConfirmClose()
895    {
896        $args = func_get_args();
897        if (count($args) > 0) {
898            foreach ($args as $k => $v) {
899                $args[$k] = "'" . html::escapeJS($v) . "'";
900            }
901            $args = implode(',', $args);
902        } else {
903            $args = '';
904        }
905
906        return
907        self::jsLoad('js/confirm-close.js') .
908        '<script type="text/javascript">' . "\n" .
909        "confirmClosePage = new confirmClose(" . $args . "); " .
910        "confirmClose.prototype.prompt = '" . html::escapeJS(__('You have unsaved changes.')) . "'; " .
911            "</script>\n";
912    }
913
914    public static function jsPageTabs($default = null)
915    {
916        if ($default) {
917            $default = "'" . html::escapeJS($default) . "'";
918        }
919
920        return
921        self::jsLoad('js/jquery/jquery.pageTabs.js') .
922            '<script type="text/javascript">' . "\n" .
923            '$(function() {' . "\n" .
924            '   $.pageTabs(' . $default . ');' . "\n" .
925            '});' .
926            "</script>\n";
927    }
928
929    public static function jsModal()
930    {
931        return
932        self::jsLoad('js/jquery/jquery.magnific-popup.js');
933    }
934
935    public static function jsColorPicker()
936    {
937        return
938        self::cssLoad('style/farbtastic/farbtastic.css') .
939        self::jsLoad('js/jquery/jquery.farbtastic.js') .
940        self::jsLoad('js/color-picker.js');
941    }
942
943    public static function jsDatePicker()
944    {
945        return
946        self::cssLoad('style/date-picker.css') .
947        self::jsLoad('js/date-picker.js') .
948        '<script type="text/javascript">' . "\n" .
949
950        "datePicker.prototype.months[0] = '" . html::escapeJS(__('January')) . "'; " .
951        "datePicker.prototype.months[1] = '" . html::escapeJS(__('February')) . "'; " .
952        "datePicker.prototype.months[2] = '" . html::escapeJS(__('March')) . "'; " .
953        "datePicker.prototype.months[3] = '" . html::escapeJS(__('April')) . "'; " .
954        "datePicker.prototype.months[4] = '" . html::escapeJS(__('May')) . "'; " .
955        "datePicker.prototype.months[5] = '" . html::escapeJS(__('June')) . "'; " .
956        "datePicker.prototype.months[6] = '" . html::escapeJS(__('July')) . "'; " .
957        "datePicker.prototype.months[7] = '" . html::escapeJS(__('August')) . "'; " .
958        "datePicker.prototype.months[8] = '" . html::escapeJS(__('September')) . "'; " .
959        "datePicker.prototype.months[9] = '" . html::escapeJS(__('October')) . "'; " .
960        "datePicker.prototype.months[10] = '" . html::escapeJS(__('November')) . "'; " .
961        "datePicker.prototype.months[11] = '" . html::escapeJS(__('December')) . "'; " .
962
963        "datePicker.prototype.days[0] = '" . html::escapeJS(__('Monday')) . "'; " .
964        "datePicker.prototype.days[1] = '" . html::escapeJS(__('Tuesday')) . "'; " .
965        "datePicker.prototype.days[2] = '" . html::escapeJS(__('Wednesday')) . "'; " .
966        "datePicker.prototype.days[3] = '" . html::escapeJS(__('Thursday')) . "'; " .
967        "datePicker.prototype.days[4] = '" . html::escapeJS(__('Friday')) . "'; " .
968        "datePicker.prototype.days[5] = '" . html::escapeJS(__('Saturday')) . "'; " .
969        "datePicker.prototype.days[6] = '" . html::escapeJS(__('Sunday')) . "'; " .
970
971        "datePicker.prototype.img_src = 'images/date-picker.png'; " .
972        "datePicker.prototype.img_alt = '" . html::escapeJS(__('Choose date')) . "'; " .
973
974        "datePicker.prototype.close_msg = '" . html::escapeJS(__('close')) . "'; " .
975        "datePicker.prototype.now_msg = '" . html::escapeJS(__('now')) . "'; " .
976
977            "</script>\n";
978    }
979
980    public static function jsToolBar()
981    {
982        # Deprecated but we keep this for plugins.
983    }
984
985    public static function jsUpload($params = array(), $base_url = null)
986    {
987        if (!$base_url) {
988            $base_url = path::clean(dirname(preg_replace('/(\?.*$)?/', '', $_SERVER['REQUEST_URI']))) . '/';
989        }
990
991        $params = array_merge($params, array(
992            'sess_id=' . session_id(),
993            'sess_uid=' . $_SESSION['sess_browser_uid'],
994            'xd_check=' . $GLOBALS['core']->getNonce()
995        ));
996
997        return
998        '<script type="text/javascript">' . "\n" .
999        "dotclear.jsUpload = {};\n" .
1000        "dotclear.jsUpload.msg = {};\n" .
1001        self::jsVar('dotclear.msg.enhanced_uploader_activate', __('Temporarily activate enhanced uploader')) .
1002        self::jsVar('dotclear.msg.enhanced_uploader_disable', __('Temporarily disable enhanced uploader')) .
1003        self::jsVar('dotclear.jsUpload.msg.limit_exceeded', __('Limit exceeded.')) .
1004        self::jsVar('dotclear.jsUpload.msg.size_limit_exceeded', __('File size exceeds allowed limit.')) .
1005        self::jsVar('dotclear.jsUpload.msg.canceled', __('Canceled.')) .
1006        self::jsVar('dotclear.jsUpload.msg.http_error', __('HTTP Error:')) .
1007        self::jsVar('dotclear.jsUpload.msg.error', __('Error:')) .
1008        self::jsVar('dotclear.jsUpload.msg.choose_file', __('Choose file')) .
1009        self::jsVar('dotclear.jsUpload.msg.choose_files', __('Choose files')) .
1010        self::jsVar('dotclear.jsUpload.msg.cancel', __('Cancel')) .
1011        self::jsVar('dotclear.jsUpload.msg.clean', __('Clean')) .
1012        self::jsVar('dotclear.jsUpload.msg.upload', __('Upload')) .
1013        self::jsVar('dotclear.jsUpload.msg.send', __('Send')) .
1014        self::jsVar('dotclear.jsUpload.msg.file_successfully_uploaded', __('File successfully uploaded.')) .
1015        self::jsVar('dotclear.jsUpload.msg.no_file_in_queue', __('No file in queue.')) .
1016        self::jsVar('dotclear.jsUpload.msg.file_in_queue', __('1 file in queue.')) .
1017        self::jsVar('dotclear.jsUpload.msg.files_in_queue', __('%d files in queue.')) .
1018        self::jsVar('dotclear.jsUpload.msg.queue_error', __('Queue error:')) .
1019        self::jsVar('dotclear.jsUpload.base_url', $base_url) .
1020        "</script>\n" .
1021
1022        self::jsLoad('js/jquery/jquery-ui.custom.js') .
1023        self::jsLoad('js/jsUpload/tmpl.js') .
1024        self::jsLoad('js/jsUpload/template-upload.js') .
1025        self::jsLoad('js/jsUpload/template-download.js') .
1026        self::jsLoad('js/jsUpload/load-image.js') .
1027        self::jsLoad('js/jsUpload/jquery.iframe-transport.js') .
1028        self::jsLoad('js/jsUpload/jquery.fileupload.js') .
1029        self::jsLoad('js/jsUpload/jquery.fileupload-process.js') .
1030        self::jsLoad('js/jsUpload/jquery.fileupload-resize.js') .
1031        self::jsLoad('js/jsUpload/jquery.fileupload-ui.js');
1032    }
1033
1034    public static function jsMetaEditor()
1035    {
1036        return self::jsLoad('js/meta-editor.js');
1037    }
1038
1039    public static function jsFilterControl($show = true)
1040    {
1041        return
1042        self::jsLoad('js/filter-controls.js') .
1043        '<script type="text/javascript">' . "\n" .
1044        self::jsVar('dotclear.msg.show_filters', $show ? 'true' : 'false') . "\n" .
1045        self::jsVar('dotclear.msg.filter_posts_list', __('Show filters and display options')) . "\n" .
1046        self::jsVar('dotclear.msg.cancel_the_filter', __('Cancel filters and display options')) . "\n" .
1047            "</script>";
1048    }
1049
1050    public static function jsLoadCodeMirror($theme = '', $multi = true, $modes = array('css', 'htmlmixed', 'javascript', 'php', 'xml'))
1051    {
1052        $ret =
1053        self::cssLoad('js/codemirror/lib/codemirror.css') .
1054        self::jsLoad('js/codemirror/lib/codemirror.js');
1055        if ($multi) {
1056            $ret .= self::jsLoad('js/codemirror/addon/mode/multiplex.js');
1057        }
1058        foreach ($modes as $mode) {
1059            $ret .= self::jsLoad('js/codemirror/mode/' . $mode . '/' . $mode . '.js');
1060        }
1061        $ret .=
1062        self::jsLoad('js/codemirror/addon/edit/closebrackets.js') .
1063        self::jsLoad('js/codemirror/addon/edit/matchbrackets.js') .
1064        self::cssLoad('js/codemirror/addon/display/fullscreen.css') .
1065        self::jsLoad('js/codemirror/addon/display/fullscreen.js');
1066        if ($theme != '') {
1067            $ret .= self::cssLoad('js/codemirror/theme/' . $theme . '.css');
1068        }
1069        return $ret;
1070    }
1071
1072    public static function jsRunCodeMirror($name, $id, $mode, $theme = '')
1073    {
1074        $ret =
1075            '<script type="text/javascript">' . "\n" .
1076            'var ' . $name . ' = CodeMirror.fromTextArea(' . $id . ',{' . "\n" .
1077            '   mode: "' . $mode . '",' . "\n" .
1078            '   tabMode: "indent",' . "\n" .
1079            '   lineWrapping: 1,' . "\n" .
1080            '   lineNumbers: 1,' . "\n" .
1081            '   matchBrackets: 1,' . "\n" .
1082            '   autoCloseBrackets: 1,' . "\n" .
1083            '   extraKeys: {"F11": function(cm) {cm.setOption("fullScreen",!cm.getOption("fullScreen"));}}';
1084        if ($theme) {
1085            $ret .=
1086                ',' . "\n" .
1087                '   theme: "' . $theme . '"';
1088        }
1089        $ret .= "\n" .
1090            '});' . "\n" .
1091            '</script>';
1092        return $ret;
1093    }
1094
1095    public static function getCodeMirrorThemes()
1096    {
1097        $themes      = array();
1098        $themes_root = dirname(__FILE__) . '/../../admin' . '/js/codemirror/theme/';
1099        if (is_dir($themes_root) && is_readable($themes_root)) {
1100            if (($d = @dir($themes_root)) !== false) {
1101                while (($entry = $d->read()) !== false) {
1102                    if ($entry != '.' && $entry != '..' && substr($entry, 0, 1) != '.' && is_readable($themes_root . '/' . $entry)) {
1103                        $themes[] = substr($entry, 0, -4); // remove .css extension
1104                    }
1105                }
1106                sort($themes);
1107            }
1108        }
1109        return $themes;
1110    }
1111
1112    public static function getPF($file)
1113    {
1114        return $GLOBALS['core']->adminurl->get('load.plugin.file', array('pf' => $file));
1115    }
1116
1117    public static function getVF($file)
1118    {
1119        return $GLOBALS['core']->adminurl->get('load.var.file', array('vf' => $file));
1120    }
1121
1122    public static function setXFrameOptions($headers, $origin = null)
1123    {
1124        if (self::$xframe_loaded) {
1125            return;
1126        }
1127
1128        if ($origin !== null) {
1129            $url                        = parse_url($origin);
1130            $headers['x-frame-options'] = sprintf('X-Frame-Options: %s', is_array($url) && isset($url['host']) ?
1131                ("ALLOW-FROM " . (isset($url['scheme']) ? $url['scheme'] . ':' : '') . '//' . $url['host']) :
1132                'SAMEORIGIN');
1133        } else {
1134            $headers['x-frame-options'] = 'X-Frame-Options: SAMEORIGIN'; // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
1135        }
1136        self::$xframe_loaded = true;
1137    }
1138}
Note: See TracBrowser for help on using the repository browser.

Sites map