function smd_if($atts,$thing) { global $thisarticle, $pretext, $thisfile, $thislink, $thisimage, $variable; extract(lAtts(array( 'field' => '', 'operator' => '', 'value' => '', 'logic' => 'and', 'case_sensitive' => 0, 'param_delim' => ',', 'mod_delim' => ':', 'list_delim' => '/', '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"]', "month" => '$pretext["month"]', "author" => '$pretext["author"]', "status" => '$pretext["status"]', "page" => '$pretext["page"]', "next_id" => '$pretext["next_id"]', "next_title" => '$pretext["next_title"]', "next_utitle" => '$pretext["next_utitle"]', "prev_id" => '$pretext["prev_id"]', "prev_title" => '$pretext["prev_title"]', "prev_utitle" => '$pretext["prev_utitle"]', "permlink_mode" => '$pretext["permlink_mode"]', ); // Each entry has the operation to be eval()d later and a list of disallowed fields $allOps = array( 'eq' => array('isset(VARNAME) && CAST FIELD === CAST VALUE', ''), 'not' => array('isset(VARNAME) && CAST FIELD !== CAST VALUE', ''), 'gt' => array('isset(VARNAME) && CAST FIELD > CAST VALUE', ''), 'ge' => array('isset(VARNAME) && CAST FIELD >= CAST VALUE', ''), 'lt' => array('isset(VARNAME) && CAST FIELD < CAST VALUE', ''), 'le' => array('isset(VARNAME) && CAST FIELD <= CAST VALUE', ''), 'divisible' => array('isset(VARNAME) && CAST FIELD % CAST VALUE === 0', ''), 'in' => array('isset(VARNAME) && in_array(FIELD, explode("'.$list_delim.'", VALUE)) !== false', ''), 'notin' => array('isset(VARNAME) && in_array(FIELD, explode("'.$list_delim.'", VALUE)) === false', ''), 'begins' => array('isset(VARNAME) && strpos(FIELD, VALUE) === 0', ''), 'contains' => array('isset(VARNAME) && strpos(FIELD, VALUE) !== false', ''), 'ends' => array('isset(VARNAME) && substr(FIELD, strlen(FIELD) - strlen(VALUE)) === VALUE', ''), 'defined' => array('isset(VARNAME)', 'parent'), 'undefined' => array('!isset(VARNAME)', 'parent'), 'isempty' => array('isset(VARNAME) && FIELD == ""', ''), 'isused' => array('isset(VARNAME) && FIELD != ""', ''), 'isnum' => array('isset(VARNAME) && ctype_digit((string)FIELD)', ''), 'isalpha' => array('isset(VARNAME) && ctype_alpha((string)FIELD)', ''), 'isalnum' => array('isset(VARNAME) && ctype_alnum((string)FIELD)', ''), 'islower' => array('isset(VARNAME) && ctype_lower((string)FIELD)', ''), 'isupper' => array('isset(VARNAME) && ctype_upper((string)FIELD)', ''), 'ispunct' => array('isset(VARNAME) && ctype_punct((string)FIELD)', ''), 'isspace' => array('isset(VARNAME) && ctype_space((string)FIELD)', ''), ); $numericOps = "gt, ge, lt, le, eq, not, divisible"; $caseOps = "islower, isupper"; $spaceOps = "isnum, isalpha, isalnum, islower, isupper, ispunct, begins, contains, ends"; $fields = do_list($field, $param_delim); $numFlds = count($fields); $ops = do_list($operator, $param_delim); $numOps = count($ops); $vals = do_list($value, $param_delim); $numVals = count($vals); $parentCats = ''; // Placeholder for the concatenated list of category leaf nodes $replacements = array(); $type = ($thisfile) ? "file" : (($thislink) ? "link" : (($thisimage) ? "image" : "article")); $out = array(); $iterations = ($numFlds > $numOps) ? $numFlds : $numOps; for ($idx = 0; $idx < $iterations; $idx++) { $fld = ($idx < $numFlds) ? $fields[$idx] : $fields[0]; // Allow short-circuit $fldParts = explode($mod_delim, $fld); $val = ($idx < $numVals) ? $vals[$idx] : ''; $valParts = explode($mod_delim, $val); $op = ($idx < $numOps && $ops[$idx] != '') ? $ops[$idx] : (($fldParts[0]=="parent") ? "contains" : "eq"); $opParts = explode($mod_delim, $op); $op = (array_key_exists($opParts[0], $allOps)) ? $opParts[0] : "eq"; $cast = ((count($opParts) == 2) && ($opParts[1] === "NUM") && (in_list($op, $numericOps))) ? '(int)' : ''; $length = ((count($opParts) == 2) && ($opParts[1] === "LEN") && (in_list($op, $numericOps))) ? 'strlen(FIELD)' : ''; $killSpaces = ((count($opParts) == 2) && ($opParts[1] === "NOSPACE") && (in_list($op, $spaceOps))) ? true : false; $stripFld = ((count($fldParts) > 1) && (in_array("NOTAGS", $fldParts))) ? true : false; $stripVal = ((count($valParts) > 1) && (in_array("NOTAGS", $valParts))) ? true : false; $case_sensitive = (in_list($op, $caseOps)) ? 1 : $case_sensitive; if ($debug) { echo 'TEST '.($idx+1).n; dmp($fldParts, $opParts, $valParts); } // Get the operator replacement code $exclude = do_list($allOps[$op][1]); $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 (isset($thisfile[$fldParts[0]])) { $rfld = $fldParts[0]; $fld = '$thisfile["'.$rfld.'"]'; } else if (isset($thislink[$fldParts[0]])) { $rfld = $fldParts[0]; $fld = '$thislink["'.$rfld.'"]'; } else if (isset($thisimage[$fldParts[0]])) { $rfld = $fldParts[0]; $fld = '$thisimage["'.$rfld.'"]'; } else if (array_key_exists($fldParts[0], $allPtxt)) { $rfld = $fldParts[0]; $fld = $allPtxt[$rfld]; } else if ($fldParts[0] == "parent") { $level = ''; foreach ($fldParts as $part) { if ($part == "parent") { $theCat = ($thisfile) ? $thisfile['category'] : (($thislink) ? $thislink['category'] : (($thisimage) ? $thisimage['category'] : $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), $type); if ($debug) { echo "CATEGORY TREE:"; 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']; } } $parentCats = $rfld = implode(" ", $items); $fld = '$parentCats'; } else if ($fldParts[0] == "txpvar") { if (count($fldParts) > 1) { $rfld = $fldParts[1]; $fld = '$variable["'.$rfld.'"]'; } } else if ($fldParts[0] == "urlvar") { if (count($fldParts) > 1) { $rfld = $fldParts[1]; $fld = '$_GET["'.$rfld.'"]'; } } else if ($fldParts[0] == "postvar") { if (count($fldParts) > 1) { $rfld = $fldParts[1]; $fld = '$_POST["'.$rfld.'"]'; } } else if ($fldParts[0] == "svrvar") { if (count($fldParts) > 1) { $rfld = $fldParts[1]; $fld = '$_SERVER["'.$rfld.'"]'; } } else if ($fldParts[0] == "phpvar") { if (count($fldParts) > 1) { $rfld = $fldParts[1]; $fld = '$GLOBALS["'.$rfld.'"]'; } } else if (isset($thisarticle[$fldParts[0]])) { $rfld = strtolower($fldParts[0]); $fld = '$thisarticle["'.$rfld.'"]'; } else if ($fldParts[0] == "NULL") { $smd_if_var = ''; $fld = '$smd_if_var'; $rfld = "NULL"; } else { $smd_if_var = $fldParts[0]; $fld = '$smd_if_var'; $rfld = "field".($idx*1+1); } $rlfld = "smd_if_len_".$rfld; $rfld = "smd_if_".$rfld; // Take a copy of $fld to use in any isset() requests $fldClean = $fld; if ($killSpaces) { $fld = 'preg_replace("/\s+/","",'.$fld.')'; } if ($stripFld) { $fld = 'trim(strip_tags('.$fld.'))'; } // Find the real value to compare against (may be another field) if ($valParts[0] == "urlvar") { if (count($valParts) > 1) { $vfld = $valParts[1]; $val = (isset($_GET[$vfld]) && $_GET[$vfld] != "") ? '$_GET["'.$vfld.'"]' : doQuote(str_replace('"', '\"', $vfld)); } } else if ($valParts[0] == "postvar") { if (count($valParts) > 1) { $vfld = $valParts[1]; $val = (isset($_POST[$vfld]) && $_POST[$vfld] != "") ? '$_POST["'.$vfld.'"]' : doQuote(str_replace('"', '\"', $vfld)); } } else if ($valParts[0] == "svrvar") { if (count($valParts) > 1) { $vfld = $valParts[1]; $val = (isset($_SERVER[$vfld]) && $_SERVER[$vfld] != "") ? '$_SERVER["'.$vfld.'"]' : doQuote(str_replace('"', '\"', $vfld)); } } else if ($valParts[0] == "txpvar") { if (count($valParts) > 1) { $vfld = $valParts[1]; $val = (isset($variable[$vfld]) && $variable[$vfld] != "") ? '$variable["'.$vfld.'"]' : doQuote(str_replace('"', '\"', $vfld)); } } else if ($valParts[0] == "phpvar") { if (count($valParts) > 1) { $vfld = $valParts[1]; $val = (isset($GLOBALS[$vfld]) && $GLOBALS[$vfld] != "") ? '$GLOBALS["'.$vfld.'"]' : doQuote(str_replace('"', '\"', $vfld)); } } else if (strpos($valParts[0], "?") === 0) { $valParts[0] = substr(strtolower($valParts[0]), 1); $vfld = $valParts[0]; if (isset($thisfile[$vfld]) && $thisfile[$vfld] != "") { $val = '$thisfile["'.$vfld.'"]'; } else if (isset($thislink[$vfld]) && $thislink[$vfld] != "") { $val = '$thislink["'.$vfld.'"]'; } else if (isset($thisimage[$vfld]) && $thisimage[$vfld] != "") { $val = '$thisimage["'.$vfld.'"]'; } else if (array_key_exists($vfld, $allPtxt) && $allPtxt[$vfld] != "") { $val = $allPtxt[$vfld]; } else if (isset($thisarticle[$vfld]) && $thisarticle[$vfld] != "") { $val = '$thisarticle["'.$vfld.'"]'; } else { $val = doQuote(str_replace('"', '\"', $vfld)); } } else { $vfld = "val".($idx*1+1); $val = doQuote(str_replace('"', '\"', $valParts[0])); } $vlfld = "smd_if_len_".$vfld; $vfld = "smd_if_".$vfld; if ($stripVal) { $val = 'trim(strip_tags('.$val.'))'; } // Replace the string parts... $cmd = str_replace("CAST", $cast, $op); $cmd = ($length) ? str_replace("FIELD", $length, $cmd) : $cmd; $cmd = str_replace("FIELD", (($case_sensitive) ? $fld : 'strtolower('.$fld.')'), $cmd); $cmd = str_replace("VARNAME", $fldClean, $cmd); $cmd = str_replace("VALUE", (($case_sensitive) ? 'VALUE' : 'strtolower(VALUE)'), $cmd); $cmd = "@\$replacements['{".$rfld."}'] = ".$fld."; \n@\$replacements['{".$rlfld."}'] = strlen(".$fld."); \n@\$replacements['{".$vfld."}'] = ".$val."; \n@\$replacements['{".$vlfld."}'] = strlen(".$val."); \n\$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) { echo "RESULT:"; dmp($out); echo "REPLACEMENTS:"; dmp($replacements); } if ($debug > 1) { echo "PRETEXT:"; dmp($pretext); echo "THIS ARTICLE:"; dmp($thisarticle); echo "THIS FILE:"; dmp($thisfile); echo "THIS LINK:"; dmp($thislink); echo "THIS IMAGE:"; dmp($thisimage); } // Check logic $result = ($out) ? true : false; if (strtolower($logic) == "and" && in_array("false", $out)) { $result = false; } if (strtolower($logic) == "or" && !in_array("true", $out)) { $result = false; } return parse(EvalElse(strtr($thing, $replacements), $result)); }