Ejemplo n.º 1
0
 /**
  * load extenders
  *
  * @param string $ext name extender separated by ,
  * @return boolean status load extenders
  */
 public function loadExtender($ext = '')
 {
     $out = true;
     if ($ext != '') {
         $ext = explode(",", $ext);
         foreach ($ext as $item) {
             if ($item != '' && !$this->_loadExtender($item)) {
                 throw new Exception('Error load ' . APIHelpers::e($item) . ' extender');
             }
         }
     }
     return $out;
 }
Ejemplo n.º 2
0
 function Filter($input, $modifiers)
 {
     global $modx;
     $output = $input;
     $this->Log("  |--- Input = '" . $output . "'");
     if (preg_match_all('~:([^:=]+)(?:=`(.*?)`(?=:[^:=]+|$))?~s', $modifiers, $matches)) {
         $modifier_cmd = $matches[1];
         // modifier command
         $modifier_value = $matches[2];
         // modifier value
         $count = count($modifier_cmd);
         $condition = array();
         for ($i = 0; $i < $count; $i++) {
             $output = trim($output);
             $this->Log("  |--- Modifier = '" . $modifier_cmd[$i] . "'");
             if ($modifier_value[$i] != '') {
                 $this->Log("  |--- Options = '" . $modifier_value[$i] . "'");
             }
             switch ($modifier_cmd[$i]) {
                 #####  Conditional Modifiers
                 case "input":
                 case "if":
                     $output = $modifier_value[$i];
                     break;
                 case "equals":
                 case "is":
                 case "eq":
                     $condition[] = intval($output == $modifier_value[$i]);
                     break;
                 case "empty":
                     $condition[] = intval(empty($output));
                     break;
                 case "notequals":
                 case "isnot":
                 case "isnt":
                 case "ne":
                     $condition[] = intval($output != $modifier_value[$i]);
                     break;
                 case "isgreaterthan":
                 case "isgt":
                 case "eg":
                     $condition[] = intval($output >= $modifier_value[$i]);
                     break;
                 case "islowerthan":
                 case "islt":
                 case "el":
                     $condition[] = intval($output <= $modifier_value[$i]);
                     break;
                 case "greaterthan":
                 case "gt":
                     $condition[] = intval($output > $modifier_value[$i]);
                     break;
                 case "lowerthan":
                 case "lt":
                     $condition[] = intval($output < $modifier_value[$i]);
                     break;
                 case "isinrole":
                 case "ir":
                 case "memberof":
                 case "mo":
                     // Is Member Of  (same as inrole but this one can be stringed as a conditional)
                     if ($output == "&_PHX_INTERNAL_&") {
                         $output = $this->user["id"];
                     }
                     $grps = $this->strlen($modifier_value[$i]) > 0 ? explode(",", $modifier_value[$i]) : array();
                     $condition[] = intval($this->isMemberOfWebGroupByUserId($output, $grps));
                     break;
                 case "or":
                     $condition[] = "||";
                     break;
                 case "and":
                     $condition[] = "&&";
                     break;
                 case "show":
                     $conditional = implode(' ', $condition);
                     $isvalid = intval(eval("return (" . $conditional . ");"));
                     if (!$isvalid) {
                         $output = NULL;
                     }
                 case "then":
                     $conditional = implode(' ', $condition);
                     $isvalid = intval(eval("return (" . $conditional . ");"));
                     if ($isvalid) {
                         $output = $modifier_value[$i];
                     } else {
                         $output = NULL;
                     }
                     break;
                 case "else":
                     $conditional = implode(' ', $condition);
                     $isvalid = intval(eval("return (" . $conditional . ");"));
                     if (!$isvalid) {
                         $output = $modifier_value[$i];
                     }
                     break;
                 case "select":
                     $raw = explode("&", $modifier_value[$i]);
                     $map = array();
                     for ($m = 0; $m < count($raw); $m++) {
                         $mi = explode("=", $raw[$m]);
                         $map[$mi[0]] = $mi[1];
                     }
                     $output = $map[$output];
                     break;
                     ##### End of Conditional Modifiers
                     #####  String Modifiers
                 ##### End of Conditional Modifiers
                 #####  String Modifiers
                 case "default":
                     $output = $output === '' ? $modifier_value[0] : $output;
                     break;
                 case "lcase":
                 case "strtolower":
                     $output = $this->strtolower($output);
                     break;
                 case "ucase":
                 case "strtoupper":
                     $output = $this->strtoupper($output);
                     break;
                 case "ucfirst":
                     $output = $this->ucfirst($output);
                     break;
                 case "lcfirst":
                     $output = $this->lcfirst($output);
                     break;
                 case "ucwords":
                     $output = $this->ucwords($output);
                     break;
                 case "htmlent":
                 case "htmlentities":
                     $output = htmlentities($output, ENT_QUOTES, $modx->config['modx_charset']);
                     break;
                 case "html_entity_decode":
                     $output = html_entity_decode($output, ENT_QUOTES, $modx->config['modx_charset']);
                     break;
                 case "esc":
                     $output = preg_replace("/&amp;(#[0-9]+|[a-z]+);/i", "&\$1;", APIHelpers::e($output));
                     $output = str_replace(array("[", "]", "`"), array("&#91;", "&#93;", "&#96;"), $output);
                     break;
                 case "strip":
                     $output = preg_replace("~([\n\r\t\\s]+)~", " ", $output);
                     break;
                 case "notags":
                 case "strip_tags":
                     $output = strip_tags($output);
                     break;
                 case "length":
                 case "len":
                 case "strlen":
                     $output = $this->strlen($output);
                     break;
                 case "reverse":
                 case "strrev":
                     $output = $this->strrev($output);
                     break;
                 case "wordwrap":
                     // default: 70
                     $wrapat = intval($modifier_value[$i]) ? intval($modifier_value[$i]) : 70;
                     $output = preg_replace("~(\\b\\w+\\b)~e", "wordwrap('\\1',\$wrapat,' ',1)", $output);
                     break;
                 case "limit":
                     // default: 100
                     $limit = intval($modifier_value[$i]) ? intval($modifier_value[$i]) : 100;
                     $output = $this->substr($output, 0, $limit);
                     break;
                 case "str_shuffle":
                 case "shuffle":
                     $output = $this->str_shuffle($output);
                     break;
                 case "str_word_count":
                 case "word_count":
                 case "wordcount":
                     $output = $this->str_word_count($output);
                     break;
                     #####  Special functions
                 #####  Special functions
                 case "math":
                     $filter = preg_replace("~([a-zA-Z\n\r\t\\s])~", "", $modifier_value[$i]);
                     $filter = str_replace("?", $output, $filter);
                     $output = eval("return " . $filter . ";");
                     break;
                 case "isnotempty":
                     if (!empty($output)) {
                         $output = $modifier_value[$i];
                     }
                     break;
                 case "isempty":
                 case "ifempty":
                     if (empty($output)) {
                         $output = $modifier_value[$i];
                     }
                     break;
                 case "nl2br":
                     $output = nl2br($output);
                     break;
                 case "date":
                     $output = strftime($modifier_value[$i], 0 + $output);
                     break;
                 case "set":
                     $c = $i + 1;
                     if ($count > $c && $modifier_cmd[$c] == "value") {
                         $output = preg_replace("~([^a-zA-Z0-9])~", "", $modifier_value[$i]);
                     }
                     break;
                 case "value":
                     if ($i > 0 && $modifier_cmd[$i - 1] == "set") {
                         $modx->SetPlaceholder("phx." . $output, $modifier_value[$i]);
                     }
                     $output = NULL;
                     break;
                 case "md5":
                     $output = md5($output);
                     break;
                 case "userinfo":
                     if ($output == "&_PHX_INTERNAL_&") {
                         $output = $this->user["id"];
                     }
                     $output = $this->ModUser($output, $modifier_value[$i]);
                     break;
                 case "inrole":
                     // deprecated
                     if ($output == "&_PHX_INTERNAL_&") {
                         $output = $this->user["id"];
                     }
                     $grps = $this->strlen($modifier_value[$i]) > 0 ? explode(",", $modifier_value[$i]) : array();
                     $output = intval($this->isMemberOfWebGroupByUserId($output, $grps));
                     break;
                     // If we haven't yet found the modifier, let's look elsewhere
                 // If we haven't yet found the modifier, let's look elsewhere
                 default:
                     // modified by Anton Kuzmin (23.06.2010) //
                     $snippetName = 'phx:' . $modifier_cmd[$i];
                     if (isset($modx->snippetCache[$snippetName])) {
                         $snippet = $modx->snippetCache[$snippetName];
                     } else {
                         // not in cache so let's check the db
                         $sql = "SELECT snippet FROM " . $modx->getFullTableName("site_snippets") . " WHERE " . $modx->getFullTableName("site_snippets") . ".name='" . $modx->db->escape($snippetName) . "';";
                         $result = $modx->dbQuery($sql);
                         if ($modx->recordCount($result) == 1) {
                             $row = $modx->fetchRow($result);
                             $snippet = $modx->snippetCache[$row['name']] = $row['snippet'];
                             $this->Log("  |--- DB -> Custom Modifier");
                         } else {
                             if ($modx->recordCount($result) == 0) {
                                 // If snippet not found, look in the modifiers folder
                                 $filename = $modx->config['rb_base_dir'] . 'plugins/phx/modifiers/' . $modifier_cmd[$i] . '.phx.php';
                                 if (@file_exists($filename)) {
                                     $file_contents = @file_get_contents($filename);
                                     $file_contents = str_replace('<' . '?php', '', $file_contents);
                                     $file_contents = str_replace('?' . '>', '', $file_contents);
                                     $file_contents = str_replace('<?', '', $file_contents);
                                     $snippet = $modx->snippetCache[$snippetName] = $file_contents;
                                     $modx->snippetCache[$snippetName . 'Props'] = '';
                                     $this->Log("  |--- File ({$filename}) -> Custom Modifier");
                                 } else {
                                     $this->Log("  |--- PHX Error:  {$modifier_cmd[$i]} could not be found");
                                 }
                             }
                         }
                     }
                     $cm = $snippet;
                     // end //
                     ob_start();
                     $options = $modifier_value[$i];
                     $custom = eval($cm);
                     $msg = ob_get_contents();
                     $output = $msg . $custom;
                     ob_end_clean();
                     break;
             }
             if (count($condition)) {
                 $this->Log("  |--- Condition = '" . $condition[count($condition) - 1] . "'");
             }
             $this->Log("  |--- Output = '" . $output . "'");
         }
     }
     return $output;
 }
 /**
  * load extenders
  *
  * @param string $ext name extender separated by ,
  * @return boolean status load extenders
  */
 public function loadExtender($ext = '')
 {
     $out = true;
     if ($ext != '') {
         $ext = explode(",", $ext);
         foreach ($ext as $item) {
             try {
                 if ($item != '' && !$this->_loadExtender($item)) {
                     $out = false;
                     throw new Exception('Error load ' . APIHelpers::e($item) . ' extender');
                     break;
                 }
             } catch (Exception $e) {
                 $this->ErrorLogger($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine(), $e->getTrace());
             }
         }
     }
     return $out;
 }