Dotclear

source: inc/admin/lib.dc.page.php @ 3637:11396143671b

Revision 3637:11396143671b, 36.7 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Ajax activity indicator option, just for fun ;-)

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

Sites map