Blob


1 <?php if (!defined('PmWiki')) exit();
2 /* Copyright 2019-2022 Petko Yotov www.pmwiki.org/petko
3 This file is part of PmWiki; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published
5 by the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version. See pmwiki.php for full details.
8 This script includes and configures one or more JavaScript utilities,
9 when they are enabled by the wiki administrator, notably:
11 * Tables of contents
12 * Sortable tables
13 * Localized time stamps
14 * Improved recent changes
15 * Syntax highlighting (PmWiki markup)
16 * Syntax highlighting (external)
17 * Collapsible sections
18 * Email obfuscation
20 To disable all these functions, add to config.php:
21 $EnablePmUtils = 0;
22 */
24 function PmUtilsJS() {
25 global $PmTOC, $EnableSortable, $EnableHighlight, $EnableLocalTimes, $ToggleNextSelector,
26 $LinkFunctions, $FarmD, $HTMLHeaderFmt, $EnablePmSyntax, $CustomSyntax;
28 $utils = "$FarmD/pub/pmwiki-utils.js";
29 if (( IsEnabled($PmTOC['Enable'], 0)
30 || IsEnabled($EnableSortable, 0)
31 || $LinkFunctions['mailto:'] == 'ObfuscateLinkIMap'
32 || IsEnabled($EnableHighlight, 0)
33 || IsEnabled($ToggleNextSelector, 0)
34 || IsEnabled($EnableLocalTimes, 0)
35 ) && file_exists($utils)) {
36 $mtime = filemtime($utils);
37 SDVA($HTMLHeaderFmt, array('pmwiki-utils' =>
38 "<script type='text/javascript' src='\$FarmPubDirUrl/pmwiki-utils.js?st=$mtime'
39 data-sortable='".@$EnableSortable."' data-highlight='".@$EnableHighlight."'
40 data-pmtoc='".pm_json_encode(@$PmTOC, true)."'
41 data-toggle='".PHSC(@$ToggleNextSelector, ENT_QUOTES)."'
42 data-localtimes='".@$EnableLocalTimes."' data-fullname='{\$FullName}'></script>"
43 ));
44 }
46 if (IsEnabled($EnablePmSyntax, 0)) { # inject before skins and local.css
47 $cs = is_array(@$CustomSyntax) ?
48 pm_json_encode(array_values($CustomSyntax), true) : '';
49 array_unshift($HTMLHeaderFmt, "<link rel='stylesheet'
50 href='\$FarmPubDirUrl/guiedit/pmwiki.syntax.css'>
51 <script src='\$FarmPubDirUrl/guiedit/pmwiki.syntax.js' data-imap='{\$EnabledIMap}'
52 data-label=\"$[Highlight]\" data-mode='$EnablePmSyntax'
53 data-custom=\"$cs\"></script>");
54 }
55 }
56 PmUtilsJS();
58 ## This is a replacement for json_encode+PHSC, but only for arrays that
59 ## are used by the PmWiki core. It may or may not work in other cases.
60 ## This may fail with international characters if UTF-8 is not enabled.
61 function pm_json_encode($x, $encodespecial=false) {
62 if (!isset($x) || is_null($x)) return 'null';
63 if (is_bool($x)) return $x? "true" : "false";
64 if (is_int($x) || is_float($x)) return strval($x);
66 if (function_exists('json_encode'))
67 $out = json_encode($x);
69 elseif (is_string($x)) ## escape controls and specials per RFC:8259
70 $out = '"'.preg_replace_callback("/[\x00-\x1f\\/\\\\\"]/",'cb_rfc8259',$x).'"';
72 elseif (is_array($x)) {
73 $a = array();
74 if (array_values($x) === $x) { # numeric sequential array
75 foreach($x as $v)
76 $a[] = pm_json_encode($v);
78 $out = "[".implode(',', $a)."]";
79 }
80 else { # associative array -> json object
81 foreach($x as $k=>$v) {
82 $jk = is_int($k)? "\"$k\"" : pm_json_encode($k);
83 $jv = pm_json_encode($v);
84 $a[] = "$jk:$jv";
85 }
86 $out = "{".implode(',', $a)."}";
87 }
88 }
90 else return 'null'; # other types not yet supported
92 return $encodespecial? PHSC($out, ENT_QUOTES) : $out;
93 }
94 function cb_rfc8259($m) {
95 return sprintf('\\u00%02x', ord($m[0]));
96 }