From 58a411afb156c962d2b62d0a01e23e769a3aacf3 Mon Sep 17 00:00:00 2001 From: petko Date: Fri, 26 Jul 2024 16:04:48 +0000 Subject: Add $EnableUploadDrop based on Cookbook:DragDropMultiUpload. git-svn-id: svn://pmwiki.org/pmwiki/trunk@4739 524c5546-5005-0410-9a3e-e25e191bd360 --- pmwiki.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'pmwiki.php') diff --git a/pmwiki.php b/pmwiki.php index 1bcba98b..5a897739 100644 --- a/pmwiki.php +++ b/pmwiki.php @@ -846,6 +846,47 @@ function PRCB($pat, $repl, $subj, $vars=null, $limit=-1, &$count=null, $flags=0) return preg_replace_callback($pat, $repl, $subj, $limit, $count, $flags); return preg_replace_callback($pat, $repl, $subj, $limit, $count); } + +## This is a replacement for json_encode+PHSC, but only for arrays that +## are used by the PmWiki core. It may or may not work in other cases. +## This may fail with international characters if UTF-8 is not enabled. +function pm_json_encode($x, $encodespecial=false) { + if (!isset($x) || is_null($x)) return 'null'; + if (is_bool($x)) return $x? "true" : "false"; + if (is_int($x) || is_float($x)) return strval($x); + + if (function_exists('json_encode')) + $out = json_encode($x); + + elseif (is_string($x)) ## escape controls and specials per RFC:8259 + $out = '"'.preg_replace_callback("/[\x00-\x1f\\/\\\\\"]/",'cb_rfc8259',$x).'"'; + + elseif (is_array($x)) { + $a = array(); + if (array_values($x) === $x) { # numeric sequential array + foreach($x as $v) + $a[] = pm_json_encode($v); + + $out = "[".implode(',', $a)."]"; + } + else { # associative array -> json object + foreach($x as $k=>$v) { + $jk = is_int($k)? "\"$k\"" : pm_json_encode($k); + $jv = pm_json_encode($v); + $a[] = "$jk:$jv"; + } + $out = "{".implode(',', $a)."}"; + } + } + else return 'null'; # other types not yet supported + + return $encodespecial? PHSC($out, ENT_QUOTES) : $out; +} +function cb_rfc8259($m) { + return sprintf('\\u00%02x', ord($m[0])); +} + + ## callback functions class PPRC { # PmWiki preg replace callbacks + pass local vars var $vars; @@ -1813,6 +1854,13 @@ function Redirect($pagename, $urlfmt='$PageUrl', $redirecturl=null) { exit; } +function PrintJSON($pagename, $out) { + $json = pm_json_encode($out); + header('Content-Type: application/json'); + print $json; + exit; +} + function PrintFmt($pagename,$fmt) { global $EnablePrePrintFmt; if (IsEnabled($EnablePrePrintFmt, 1)) PrePrintFmt($pagename,$fmt); -- cgit v1.2.3