Dotclear

source: inc/admin/lib.dc.page.php @ 3454:7e4a964280fb

Revision 3454:7e4a964280fb, 35.2 KB checked in by franck <carnet.franck.paul@…>, 9 years ago (diff)

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

Sites map