--- pmwiki-2.2.106/pmwiki.php	2017-11-08 10:39:13.000000000 -0600
+++ pmwiki-2.2.106/pmwiki-cg.php	2017-12-01 20:21:21.166334652 -0600
@@ -465,29 +465,73 @@
     $z['#'][] = $v;
   }
   return $z;
 }
 function PHSC($x, $flags=ENT_COMPAT, $enc=null, $dbl_enc=true) { # for PHP 5.4
   if (is_null($enc)) $enc = "ISO-8859-1"; # single-byte charset
   if (! is_array($x)) return @htmlspecialchars($x, $flags, $enc, $dbl_enc);
   foreach($x as $k=>$v) $x[$k] = PHSC($v, $flags, $enc, $dbl_enc);
   return $x;
 }
+function PCF($args, $code) {
+  global $EnablePCF;
+  global $MessagesFmt;
+  if (IsEnabled($EnablePCF, 0)) {
+    static $plambdaCnt = 0;
+    $fn = "plambda_" . ++$plambdaCnt;
+    eval ("function {$fn} ({$args}) {{$code}}");
+  } else {
+    $fn = create_function($args, $code);
+   #$msg = "PCF:  create_function args='{$args}', code='{$code}'.";
+   #$MessagesFmt[] = "<div class='wikimessage'>{$msg}</div>";
+  }
+  PCFWarn(' (function created)');
+  return $fn;
+}
+function PCFWarn($sfx = '') {
+  global $EnablePCFWarn, $PCFRootPat;
+  global $MessagesFmt;
+  if (IsEnabled($EnablePCFWarn, 0)) {
+    if (function_exists('debug_backtrace')) {
+      $dbg = debug_backtrace();
+      for ($i=1, $ilen=count($dbg); $i<$ilen; $i++) {
+        $func = $dbg[$i]['function'];
+        if (! ($func == 'PCF' || $func == 'PCCF' || $func == 'PPRE' || $func == 'Markup_e')) {
+          break;
+        }
+      }
+      $dbginfo = $dbg[$i-1];
+      $file = $dbginfo['file'];
+      if (isset($PCFRootPat)) {
+        $file = preg_replace($PCFRootPat, '$1', $file);
+      }
+      $msgkey = "PCF_" . $dbginfo['function'] . '_' . $file . "_" . $dbginfo['line'];
+      if (! isset($MessagesFmt[$msgkey])) {
+        $msg = "PCF: {$dbginfo['function']} was called from {$file} line {$dbginfo['line']}!";
+        $MessagesFmt[$msgkey] = "<div class='wikimessage'>{$msg}{$sfx}</div>";
+      }
+    }
+    else 
+      $MessagesFmt['PCF_unknown'] = "<div class='wikimessage'>PCF was Called!</div>";
+  }
+}
 function PCCF($code, $template = 'default', $args = '$m') {
   global $CallbackFnTemplates, $CallbackFunctions;
   if (!isset($CallbackFnTemplates[$template]))
     Abort("No \$CallbackFnTemplates[$template]).");
   $code = sprintf($CallbackFnTemplates[$template], $code);
   if (!isset($CallbackFunctions[$code])) {
-    $fn = create_function($args, $code);
+    $fn = PCF($args, $code);
     if ($fn) $CallbackFunctions[$code] = $fn;
     else StopWatch("Failed to create callback function: ".PHSC($code));
+  } else {
+    PCFWarn();
   }
   return $CallbackFunctions[$code];
 }
 function PPRE($pat, $rep, $x) {
   $lambda = PCCF("return $rep;");
   return preg_replace_callback($pat, $lambda, $x);
 }
 function PPRA($array, $x) {
   foreach($array as $pat => $rep) {
     $fmt = $x; # for $FmtP
@@ -1676,53 +1720,55 @@
     if ($m[1]=='<:page>' && trim($txt) == '+' && $suffix>'') { # PITS:01392
       $txt = array(trim($txt), $suffix);
     }
     else $txt .= $suffix;
   }
   if (@$LinkTitleFunction) $m[4] = $LinkTitleFunction($pagename,$m,$txt);
   $out = $LinkFunctions[$m[1]]($pagename,$m[1],$m[2],@$m[4],$txt,$fmt);
   return preg_replace('/(<[^>]+) title=(""|\'\')/', '$1', $out);
 }
 
-function Markup($id, $when, $pat=NULL, $rep=NULL) {
+function Markup($id, $when, $pat=NULL, $rep=NULL, $tracelev=0) {
+  global $EnableMarkupDiag;
   global $MarkupTable;
   unset($GLOBALS['MarkupRules']);
   if (preg_match('/^([<>])?(.+)$/', $when, $m)) {
     $MarkupTable[$id]['cmd'] = $when;
     $MarkupTable[$m[2]]['dep'][$id] = $m[1];
     if (!$m[1]) $m[1] = '=';
     if (@$MarkupTable[$m[2]]['seq']) {
       $MarkupTable[$id]['seq'] = $MarkupTable[$m[2]]['seq'].$m[1];
       foreach((array)@$MarkupTable[$id]['dep'] as $i=>$m)
         Markup($i,"$m$id");
       unset($GLOBALS['MarkupTable'][$id]['dep']);
     }
   }
   if ($pat && !isset($MarkupTable[$id]['pat'])) {
     $MarkupTable[$id]['pat'] = $pat;
     $MarkupTable[$id]['rep'] = $rep;
     
-    if (preg_match('!/[^/]*e[^/]*$!', $pat)) {
+    if ($EnableMarkupDiag || preg_match('!/[^/]*e[^/]*$!', $pat)) {
       if (function_exists('debug_backtrace')) {
         $dbg = debug_backtrace();
-        $MarkupTable[$id]['dbg'] = "! file: {$dbg['0']['file']}, "
-          . "line: {$dbg['0']['line']}, pat: {$dbg['0']['args'][2]}";
+        $dbginfo = $dbg[$tracelev];
+        $MarkupTable[$id]['dbg'] = "! file: {$dbginfo['file']}, "
+          . "line: {$dbginfo['line']}, pat: {$dbginfo['args'][2]}";
       }
       else 
         $MarkupTable[$id]['dbg'] = "! id: '$id', pat: '$pat'";
     }
   }
 }
 
 function Markup_e($id, $when, $pat, $rep, $template = 'markup_e') {
   if (!is_callable($rep)) $rep = PCCF($rep, $template);
-  Markup($id, $when, $pat, $rep);
+  Markup($id, $when, $pat, $rep, 1);  #CGG
 }
 
 function DisableMarkup() {
   global $MarkupTable;
   $idlist = func_get_args();
   unset($GLOBALS['MarkupRules']);
   while (count($idlist)>0) {
     $id = array_shift($idlist);
     if (is_array($id)) { $idlist = array_merge($idlist, $id); continue; }
     $MarkupTable[$id] = array('cmd' => 'none', 'pat'=>'');
