PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/snippets/jot/includes/phx.parser.class.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 433 lines | 336 code | 33 blank | 64 comment | 35 complexity | 7f756ac4c9914d61985a37d269aca015 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /*####
  3. #
  4. # Name: PHx (Placeholders Xtended)
  5. # Version: 2.1.3
  6. # Modified by Nick to include external files
  7. # Modified by Anton Kuzmin for using of modx snippets cache
  8. # Author: Armand "bS" Pondman (apondman@zerobarrier.nl)
  9. # Date: July 13, 2007
  10. #
  11. ####*/
  12. class PHxParser {
  13. var $placeholders = array();
  14. function PHxParser($debug=0,$maxpass=50) {
  15. global $modx;
  16. $this->name = "PHx";
  17. $this->version = "2.1.3";
  18. $this->user["mgrid"] = intval($_SESSION['mgrInternalKey']);
  19. $this->user["usrid"] = intval($_SESSION['webInternalKey']);
  20. $this->user["id"] = ($this->user["usrid"] > 0 ) ? (-$this->user["usrid"]) : $this->user["mgrid"];
  21. $this->cache["cm"] = array();
  22. $this->cache["ui"] = array();
  23. $this->cache["mo"] = array();
  24. $this->safetags[0][0] = '~(?<![\[]|^\^)\[(?=[^\+\*\(\[]|$)~s';
  25. $this->safetags[0][1] = '~(?<=[^\+\*\)\]]|^)\](?=[^\]]|$)~s';
  26. $this->safetags[1][0] = '&_PHX_INTERNAL_091_&';
  27. $this->safetags[1][1] = '&_PHX_INTERNAL_093_&';
  28. $this->safetags[2][0] = '[';
  29. $this->safetags[2][1] = ']';
  30. $this->console = array();
  31. $this->debug = ($debug!='') ? $debug : 0;
  32. $this->debugLog = false;
  33. $this->curPass = 0;
  34. $this->maxPasses = ($maxpass!='') ? $maxpass : 50;
  35. $this->swapSnippetCache = array();
  36. $modx->setPlaceholder("phx", "&_PHX_INTERNAL_&");
  37. }
  38. // Plugin event hook for MODx
  39. function OnParseDocument() {
  40. global $modx;
  41. // Get document output from MODx
  42. $template = $modx->documentOutput;
  43. // To the parse cave .. let's go! *insert batman tune here*
  44. $template = $this->Parse($template);
  45. // Set processed document output in MODx
  46. $modx->documentOutput = $template;
  47. }
  48. // Parser: Preparation, cleaning and checkup
  49. function Parse($template='') {
  50. global $modx;
  51. // If we already reached max passes don't get at it again.
  52. if ($this->curPass == $this->maxPasses) return $template;
  53. // Set template pre-process hash
  54. $st = md5($template);
  55. // Replace non-call characters in the template: [, ]
  56. $template = preg_replace($this->safetags[0],$this->safetags[1],$template);
  57. // To the parse mobile.. let's go! *insert batman tune here*
  58. $template = $this->ParseValues($template);
  59. // clean up unused placeholders that have modifiers attached (MODx can't clean them)
  60. preg_match_all('~\[(\+|\*|\()([^:\+\[\]]+)([^\[\]]*?)(\1|\))\]~s', $template, $matches);
  61. if ($matches[0]) {
  62. $template = str_replace($matches[0], '', $template);
  63. $this->Log("Cleaning unsolved tags: \n" . implode("\n",$matches[2]) );
  64. }
  65. // Restore non-call characters in the template: [, ]
  66. $template = str_replace($this->safetags[1],$this->safetags[2],$template);
  67. // Set template post-process hash
  68. $et = md5($template);
  69. // If template has changed, parse it once more...
  70. if ($st!=$et) $template = $this->Parse($template);
  71. // Write an event log if debugging is enabled and there is something to log
  72. if ($this->debug && $this->debugLog) {
  73. $modx->logEvent($this->curPass,1,$this->createEventLog(), $this->name.' '.$this->version);
  74. $this->debugLog = false;
  75. }
  76. // Return the processed template
  77. return $template;
  78. }
  79. // Parser: Tag detection and replacements
  80. function ParseValues($template='') {
  81. global $modx;
  82. $this->curPass = $this->curPass + 1;
  83. $st = md5($template);
  84. //$this->LogSource($template);
  85. $this->LogPass();
  86. // MODx Chunks
  87. $this->Log("MODx Chunks -> Merging all chunk tags");
  88. $template = $modx->mergeChunkContent($template);
  89. // MODx Snippets
  90. //if ( preg_match_all('~\[(\[|!)([^\[]*?)(!|\])\]~s',$template, $matches)) {
  91. if ( preg_match_all('~\[(\[)([^\[]*?)(\])\]~s',$template, $matches)) {
  92. $count = count($matches[0]);
  93. $var_search = array();
  94. $var_replace = array();
  95. // for each detected snippet
  96. for($i=0; $i<$count; $i++) {
  97. $snippet = $matches[2][$i]; // snippet call
  98. $this->Log("MODx Snippet -> ".$snippet);
  99. // Let MODx evaluate snippet
  100. $replace = $modx->evalSnippets("[[".$snippet."]]");
  101. $this->LogSnippet($replace);
  102. // Replace values
  103. $var_search[] = $matches[0][$i];
  104. $var_replace[] = $replace;
  105. }
  106. $template = str_replace($var_search, $var_replace, $template);
  107. }
  108. // PHx / MODx Tags
  109. if ( preg_match_all('~\[(\+|\*|\()([^:\+\[\]]+)([^\[\]]*?)(\1|\))\]~s',$template, $matches)) {
  110. //$matches[0] // Complete string that's need to be replaced
  111. //$matches[1] // Type
  112. //$matches[2] // The placeholder(s)
  113. //$matches[3] // The modifiers
  114. //$matches[4] // Type (end character)
  115. $count = count($matches[0]);
  116. $var_search = array();
  117. $var_replace = array();
  118. for($i=0; $i<$count; $i++) {
  119. $replace = NULL;
  120. $match = $matches[0][$i];
  121. $type = $matches[1][$i];
  122. $type_end = $matches[4][$i];
  123. $input = $matches[2][$i];
  124. $modifiers = $matches[3][$i];
  125. $var_search[] = $match;
  126. switch($type) {
  127. // Document / Template Variable eXtended
  128. case "*":
  129. $this->Log("MODx TV/DV: " . $input);
  130. $input = $modx->mergeDocumentContent("[*".$input."*]");
  131. $replace = $this->Filter($input,$modifiers);
  132. break;
  133. // MODx Setting eXtended
  134. case "(":
  135. $this->Log("MODx Setting variable: " . $input);
  136. $input = $modx->mergeSettingsContent("[(".$input.")]");
  137. $replace = $this->Filter($input,$modifiers);
  138. break;
  139. // MODx Placeholder eXtended
  140. default:
  141. $this->Log("MODx / PHx placeholder variable: " . $input);
  142. // Check if placeholder is set
  143. if ( !array_key_exists($input, $this->placeholders) && !array_key_exists($input, $modx->placeholders) ) {
  144. // not set so try again later.
  145. $replace = $match;
  146. $this->Log(" |--- Skipping - hasn't been set yet.");
  147. }
  148. else {
  149. // is set, get value and run filter
  150. $input = $this->getPHxVariable($input);
  151. $replace = $this->Filter($input,$modifiers);
  152. }
  153. break;
  154. }
  155. $var_replace[] = $replace;
  156. }
  157. $template = str_replace($var_search, $var_replace, $template);
  158. }
  159. $et = md5($template); // Post-process template hash
  160. // Log an event if this was the maximum pass
  161. if ($this->curPass == $this->maxPasses) $this->Log("Max passes reached. infinite loop protection so exiting.\n If you need the extra passes set the max passes to the highest count of nested tags in your template.");
  162. // If this pass is not at maximum passes and the template hash is not the same, get at it again.
  163. if (($this->curPass < $this->maxPasses) && ($st!=$et)) $template = $this->ParseValues($template);
  164. return $template;
  165. }
  166. // Parser: modifier detection and eXtended processing if needed
  167. function Filter($input, $modifiers) {
  168. global $modx;
  169. $output = $input;
  170. $this->Log(" |--- Input = '". $output ."'");
  171. if (preg_match_all('~:([^:=]+)(?:=`(.*?)`(?=:[^:=]+|$))?~s',$modifiers, $matches)) {
  172. $modifier_cmd = $matches[1]; // modifier command
  173. $modifier_value = $matches[2]; // modifier value
  174. $count = count($modifier_cmd);
  175. $condition = array();
  176. for($i=0; $i<$count; $i++) {
  177. $output = trim($output);
  178. $this->Log(" |--- Modifier = '". $modifier_cmd[$i] ."'");
  179. if ($modifier_value[$i] != '') $this->Log(" |--- Options = '". $modifier_value[$i] ."'");
  180. switch ($modifier_cmd[$i]) {
  181. ##### Conditional Modifiers
  182. case "input": case "if": $output = $modifier_value[$i]; break;
  183. case "equals": case "is": case "eq": $condition[] = intval(($output==$modifier_value[$i])); break;
  184. case "notequals": case "isnot": case "isnt": case "ne":$condition[] = intval(($output!=$modifier_value[$i]));break;
  185. case "isgreaterthan": case "isgt": case "eg": $condition[] = intval(($output>=$modifier_value[$i]));break;
  186. case "islowerthan": case "islt": case "el": $condition[] = intval(($output<=$modifier_value[$i]));break;
  187. case "greaterthan": case "gt": $condition[] = intval(($output>$modifier_value[$i]));break;
  188. case "lowerthan": case "lt":$condition[] = intval(($output<$modifier_value[$i]));break;
  189. case "isinrole": case "ir": case "memberof": case "mo": // Is Member Of (same as inrole but this one can be stringed as a conditional)
  190. if ($output == "&_PHX_INTERNAL_&") $output = $this->user["id"];
  191. $grps = (strlen($modifier_value) > 0 ) ? explode(",",$modifier_value[$i]) :array();
  192. $condition[] = intval($this->isMemberOfWebGroupByUserId($output,$grps));
  193. break;
  194. case "or":$condition[] = "||";break;
  195. case "and": $condition[] = "&&";break;
  196. case "show":
  197. $conditional = implode(' ',$condition);
  198. $isvalid = intval(eval("return (". $conditional. ");"));
  199. if (!$isvalid) { $output = NULL;}
  200. case "then":
  201. $conditional = implode(' ',$condition);
  202. $isvalid = intval(eval("return (". $conditional. ");"));
  203. if ($isvalid) { $output = $modifier_value[$i]; }
  204. else { $output = NULL; }
  205. break;
  206. case "else":
  207. $conditional = implode(' ',$condition);
  208. $isvalid = intval(eval("return (". $conditional. ");"));
  209. if (!$isvalid) { $output = $modifier_value[$i]; }
  210. break;
  211. case "select":
  212. $raw = explode("&",$modifier_value[$i]);
  213. $map = array();
  214. for($m=0; $m<(count($raw)); $m++) {
  215. $mi = explode("=",$raw[$m]);
  216. $map[$mi[0]] = $mi[1];
  217. }
  218. $output = $map[$output];
  219. break;
  220. ##### End of Conditional Modifiers
  221. ##### String Modifiers
  222. case "lcase": $output = strtolower($output); break;
  223. case "ucase": $output = strtoupper($output); break;
  224. case "ucfirst": $output = ucfirst($output); break;
  225. case "htmlent": $output = htmlentities($output,ENT_QUOTES,$modx->config['etomite_charset']); break;
  226. case "esc":
  227. $output = preg_replace("/&amp;(#[0-9]+|[a-z]+);/i", "&$1;", htmlspecialchars($output));
  228. $output = str_replace(array("[","]","`"),array("&#91;","&#93;","&#96;"),$output);
  229. break;
  230. case "strip": $output = preg_replace("~([\n\r\t\s]+)~"," ",$output); break;
  231. case "notags": $output = strip_tags($output); break;
  232. case "length": case "len": $output = strlen($output); break;
  233. case "reverse": $output = strrev($output); break;
  234. case "wordwrap": // default: 70
  235. $wrapat = intval($modifier_value[$i]) ? intval($modifier_value[$i]) : 70;
  236. $output = preg_replace("~(\b\w+\b)~e","wordwrap('\\1',\$wrapat,' ',1)",$output);
  237. break;
  238. case "limit": // default: 100
  239. $limit = intval($modifier_value[$i]) ? intval($modifier_value[$i]) : 100;
  240. $output = substr($output,0,$limit);
  241. break;
  242. ##### Special functions
  243. case "math":
  244. $filter = preg_replace("~([a-zA-Z\n\r\t\s])~","",$modifier_value[$i]);
  245. $filter = str_replace("?",$output,$filter);
  246. $output = eval("return ".$filter.";");
  247. break;
  248. case "ifempty": if (empty($output)) $output = $modifier_value[$i]; break;
  249. case "nl2br": $output = nl2br($output); break;
  250. case "date": $output = strftime($modifier_value[$i],0+$output); break;
  251. case "set":
  252. $c = $i+1;
  253. if ($count>$c&&$modifier_cmd[$c]=="value") $output = preg_replace("~([^a-zA-Z0-9])~","",$modifier_value[$i]);
  254. break;
  255. case "value":
  256. if ($i>0&&$modifier_cmd[$i-1]=="set") { $modx->SetPlaceholder("phx.".$output,$modifier_value[$i]); }
  257. $output = NULL;
  258. break;
  259. case "md5": $output = md5($output); break;
  260. case "userinfo":
  261. if ($output == "&_PHX_INTERNAL_&") $output = $this->user["id"];
  262. $output = $this->ModUser($output,$modifier_value[$i]);
  263. break;
  264. case "inrole": // deprecated
  265. if ($output == "&_PHX_INTERNAL_&") $output = $this->user["id"];
  266. $grps = (strlen($modifier_value) > 0 ) ? explode(",",$modifier_value[$i]) :array();
  267. $output = intval($this->isMemberOfWebGroupByUserId($output,$grps));
  268. break;
  269. default:
  270. // modified by Anton Kuzmin (23.06.2010) //
  271. $snippetName = 'phx:'.$modifier_cmd[$i];
  272. if( isset($modx->snippetCache[$snippetName]) ) {
  273. $snippet = $modx->snippetCache[$snippetName];
  274. } else { // not in cache so let's check the db
  275. $sql= "SELECT snippet FROM " . $modx->getFullTableName("site_snippets") . " WHERE " . $modx->getFullTableName("site_snippets") . ".name='" . $modx->db->escape($snippetName) . "';";
  276. $result= $modx->dbQuery($sql);
  277. if ($modx->recordCount($result) == 1) {
  278. $row= $modx->fetchRow($result);
  279. $snippet= $modx->snippetCache[$row['name']]= $row['snippet'];
  280. $this->Log(" |--- DB -> Custom Modifier");
  281. } else if ($modx->recordCount($result) == 0){ // If snippet not found, look in the modifiers folder
  282. $filename = $modx->config['rb_base_dir'] . 'plugins/phx/modifiers/'.$modifier_cmd[$i].'.phx.php';
  283. if (@file_exists($filename)) {
  284. $file_contents = @file_get_contents($filename);
  285. $file_contents = str_replace('<'.'?php', '', $file_contents);
  286. $file_contents = str_replace('?'.'>', '', $file_contents);
  287. $file_contents = str_replace('<?', '', $file_contents);
  288. $snippet = $modx->snippetCache[$snippetName] = $file_contents;
  289. $modx->snippetCache[$snippetName.'Props'] = '';
  290. $this->Log(" |--- File ($filename) -> Custom Modifier");
  291. }
  292. }
  293. }
  294. $cm = $snippet;
  295. // end //
  296. ob_start();
  297. $options = $modifier_value[$i];
  298. $custom = eval($cm);
  299. $msg = ob_get_contents();
  300. $output = $msg.$custom;
  301. ob_end_clean();
  302. break;
  303. }
  304. if (count($condition)) $this->Log(" |--- Condition = '". $condition[count($condition)-1] ."'");
  305. $this->Log(" |--- Output = '". $output ."'");
  306. }
  307. }
  308. return $output;
  309. }
  310. // Event logging (debug)
  311. function createEventLog() {
  312. if($this->console) {
  313. $console = implode("\n",$this->console);
  314. $this->console = array();
  315. return '<pre style="overflow: auto;">' . $console . '</pre>';
  316. }
  317. }
  318. // Returns a cleaned string escaping the HTML and special MODx characters
  319. function LogClean($string) {
  320. $string = preg_replace("/&amp;(#[0-9]+|[a-z]+);/i", "&$1;", htmlspecialchars($string));
  321. $string = str_replace(array("[","]","`"),array("&#91;","&#93;","&#96;"),$string);
  322. return $string;
  323. }
  324. // Simple log entry
  325. function Log($string) {
  326. if ($this->debug) {$this->debugLog = true; $this->console[] = (count($this->console)+1-$this->curPass). " [". strftime("%H:%M:%S",time()). "] " . $this->LogClean($string);}
  327. }
  328. // Log snippet output
  329. function LogSnippet($string) {
  330. if ($this->debug) {$this->debugLog = true; $this->console[] = (count($this->console)+1-$this->curPass). " [". strftime("%H:%M:%S",time()). "] " . " |--- Returns: <div style='margin: 10px;'>".$this->LogClean($string)."</div>";}
  331. }
  332. // Log pass
  333. function LogPass() {
  334. $this->console[] = "<div style='margin: 2px;margin-top: 5px;border-bottom: 1px solid black;'>Pass " . $this->curPass . "</div>";
  335. }
  336. // Log pass
  337. function LogSource($string) {
  338. $this->console[] = "<div style='margin: 2px;margin-top: 5px;border-bottom: 1px solid black;'>Source:</div>" . $this->LogClean($string);
  339. }
  340. // Returns the specified field from the user record
  341. // positive userid = manager, negative integer = webuser
  342. function ModUser($userid,$field) {
  343. global $modx;
  344. if (!array_key_exists($userid, $this->cache["ui"])) {
  345. if (intval($userid) < 0) {
  346. $user = $modx->getWebUserInfo(-($userid));
  347. } else {
  348. $user = $modx->getUserInfo($userid);
  349. }
  350. $this->cache["ui"][$userid] = $user;
  351. } else {
  352. $user = $this->cache["ui"][$userid];
  353. }
  354. return $user[$field];
  355. }
  356. // Returns true if the user id is in one the specified webgroups
  357. function isMemberOfWebGroupByUserId($userid=0,$groupNames=array()) {
  358. global $modx;
  359. // if $groupNames is not an array return false
  360. if(!is_array($groupNames)) return false;
  361. // if the user id is a negative number make it positive
  362. if (intval($userid) < 0) { $userid = -($userid); }
  363. // Creates an array with all webgroups the user id is in
  364. if (!array_key_exists($userid, $this->cache["mo"])) {
  365. $tbl = $modx->getFullTableName("webgroup_names");
  366. $tbl2 = $modx->getFullTableName("web_groups");
  367. $sql = "SELECT wgn.name FROM $tbl wgn INNER JOIN $tbl2 wg ON wg.webgroup=wgn.id AND wg.webuser='".$userid."'";
  368. $this->cache["mo"][$userid] = $grpNames = $modx->db->getColumn("name",$sql);
  369. } else {
  370. $grpNames = $this->cache["mo"][$userid];
  371. }
  372. // Check if a supplied group matches a webgroup from the array we just created
  373. foreach($groupNames as $k=>$v)
  374. if(in_array(trim($v),$grpNames)) return true;
  375. // If we get here the above logic did not find a match, so return false
  376. return false;
  377. }
  378. // Returns the value of a PHx/MODx placeholder.
  379. function getPHxVariable($name) {
  380. global $modx;
  381. // Check if this variable is created by PHx
  382. if (array_key_exists($name, $this->placeholders)) {
  383. // Return the value from PHx
  384. return $this->placeholders[$name];
  385. } else {
  386. // Return the value from MODx
  387. return $modx->getPlaceholder($name);
  388. }
  389. }
  390. // Sets a placeholder variable which can only be access by PHx
  391. function setPHxVariable($name, $value) {
  392. if ($name != "phx") $this->placeholders[$name] = $value;
  393. }
  394. }
  395. ?>