function smd_if($atts,$thing) { global $thisarticle, $pretext; extract(lAtts(array( 'field' => '', 'operator' => 'eq', 'value' => 'NULL', 'logic' => 'and', 'debug' => '0', ), $atts)); // Special field names that refer to $pretext or elsewhere - everything else is assumed to // exist in $thisarticle so custom fields can be used $allPtxt = array( "id" => '$pretext["id"]', "section" => '$pretext["s"]', "category" => '$pretext["c"]', "query" => '$pretext["q"]', "pg" => '$pretext["pg"]', "status" => '$pretext["status"]', "next_id" => ((isset($pretext['next_id'])) ? '$pretext["next_id"]' : ''), "next_title" => ((isset($pretext['next_title'])) ? '$pretext["next_title"]' : ''), "prev_id" => ((isset($pretext['prev_id'])) ? '$pretext["prev_id"]' : ''), "prev_title" => ((isset($pretext['prev_title'])) ? '$pretext["prev_title"]' : ''), ); // Each entry has the operation to be eval()d later, it's "type" (test VALue or VARiable) and a list of disallowed fields $allOps = array( "eq" => array("FIELD === 'VALUE'", "VAL", ""), "not" => array("FIELD !== 'VALUE'", "VAL", ""), "gt" => array("CAST FIELD > CAST 'VALUE'", "VAL", ""), "ge" => array("CAST FIELD >= CAST 'VALUE'", "VAL", ""), "lt" => array("CAST FIELD < CAST 'VALUE'", "VAL", ""), "le" => array("CAST FIELD <= CAST 'VALUE'", "VAL", ""), "begins" => array("strpos(FIELD, 'VALUE') === 0", "VAL", ""), "contains" => array("strpos(FIELD, 'VALUE') !== false", "VAL", ""), "defined" => array("isset(FIELD)", "VAR", "parent"), "undefined" => array("!isset(FIELD)", "VAR", "parent"), "isnum" => array("is_numeric(FIELD)", "VAL", ""), "isempty" => array("FIELD == ''", "VAL", ""), "isused" => array("FIELD != ''", "VAL", ""), ); $numericOps = "gt, ge, lt, le"; $fields = do_list($field); $ops = do_list($operator); $vals = do_list($value); $out = array(); for ($idx = 0; $idx < count($fields); $idx++) { $fld = $fields[$idx]; $fldParts = explode(":", $fld); $val = $vals[$idx]; $op = $ops[$idx]; $opParts = explode(":", $op); $op = (array_key_exists($opParts[0], $allOps)) ? $opParts[0] : "eq"; $cast = ((count($opParts) == 2) && ($opParts[1] === "NUM") && (in_list($op, $numericOps))) ? '(int)' : ''; // Get the operator replacement code $type = $allOps[$op][1]; $exclude = do_list($allOps[$op][2]); $op = $allOps[$op][0]; // As long as the current operator allows this field... if (!in_array($fldParts[0], $exclude)) { // Make up the test field variable if (array_key_exists($fldParts[0], $allPtxt)) { $fld = $allPtxt[$fldParts[0]]; } else if ($fldParts[0] == "parent") { $level = ''; foreach ($fldParts as $part) { if ($part == "parent") { $theCat = $pretext['c']; } else if (strpos($part, "CAT") === 0) { $theCat = $thisarticle["category".substr($part, 3)]; } else if (strpos($part, "LVL") === 0) { $level = substr($part, 3); } } $tree = getTreePath(doSlash($theCat), 'article'); if ($debug) { dmp($tree); } $items = array(); foreach ($tree as $leaf) { if ($leaf['name'] == "root" || $leaf['name'] == $theCat) { continue; } else if ($level == '' || $level == $leaf['level']) { $items[] = $leaf['name']; } } $fld = doQuote(implode(" ", $items)); } else if ($fldParts[0] == "urlvar") { if (count($fldParts) == 2) { $fld = (gps($fldParts[1]) !== '') ? '$_GET["'.$fldParts[1].'"]' : doQuote(implode("", $fldParts)); } } else if ($fldParts[0] == "svrvar") { if (count($fldParts) == 2) { $fld = (serverSet($fldParts[1]) !== '') ? '$_SERVER["'.$fldParts[1].'"]' : doQuote(implode("", $fldParts)); } } else { $fld = '$thisarticle["'.$fldParts[0].'"]'; } // Find the real value to compare against (may be another field) if (array_key_exists($val, $allPtxt)) { $val = $allPtxt[$fld]; } else if (strpos($val, "urlvar") !== false) { $parts = explode(":", $val); if (count($parts) == 2) { if (isset($_GET[$parts[1]])) { $val = $_GET[$parts[1]]; } } } else if (strpos($val, "svrvar") !== false) { $parts = explode(":", $val); if (count($parts) == 2) { if (isset($_SERVER[$parts[1]])) { $val = $_SERVER[$parts[1]]; } } } else if (isset($thisarticle[$val])) { $val = $thisarticle[$val]; } // Check the type and fix the variable if necessary if (($type == "VAR") && !(strpos($fld, "$") === 0)) { $fld = "$".preg_replace("/[^a-zA-Z0-9\_]+/", "", $fld); } // Replace the string parts... $cmd = str_replace("CAST", $cast, $op); $cmd = str_replace("FIELD", $fld, $cmd); $cmd = "\$out[] = (".str_replace("VALUE", (($val=='' && strpos($op, "strpos") !== false) ? ' ' : $val), $cmd).") ? 'true' : 'false';"; if ($debug) { dmp($cmd); } // ... and evaluate the expression eval($cmd); } } if ($debug) { dmp($out); } // Check logic $result = true; if (strtolower($logic) == "and" && in_array("false", $out)) { $result = false; } if (strtolower($logic) == "or" && !in_array("true", $out)) { $result = false; } return parse(EvalElse($thing, $result)); }