Dotclear

source: inc/admin/lib.dc.page.php @ 3619:1b4fdf28e548

Revision 3619:1b4fdf28e548, 36.3 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Add Referrer-Policy header in admin pages (thanks Nicolas Hoffmann →  https://openweb.eu.org/articles/referrer-policy)

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

Sites map