Advertisement
Guest User

Untitled

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