function executeSingleRecFuncs() { global $g_sqlSingleRecFuncs; global $g_sqlMathOps; debug_printb("[executeSingleRecFuncs] executing singlerec functions...<br>"); for ($i = 0; $i < count($this->colFuncs); ++$i) { if (!$this->colFuncs[$i] || $this->colFuncsExecuted[$i]) { continue; } if (!in_array($this->colFuncs[$i], $g_sqlSingleRecFuncs)) { continue; } debug_print($this->colFuncs[$i] . "(" . $this->colNames[$i] . "): "); // EVAL if ($this->colFuncs[$i] == "EVAL") { $eval_str = $this->colNames[$i]; $out_str = ""; if (has_quotes($eval_str)) { remove_quotes($eval_str); } debug_print("EVAL function, eval String is {$eval_str}!<br>"); $sp = new StringParser(); $sp->specialElements = $g_sqlMathOps; $sp->setString($eval_str); while (!is_empty_str($elem = $sp->parseNextElement())) { debug_print("ELEM: {$elem}\n"); if (is_numeric($elem) || in_array($elem, $g_sqlMathOps)) { $out_str .= $elem . " "; } else { $origColNr = $this->findColNrByAttrs($elem, "", ""); if ($origColNr == NOT_FOUND) { print_error_msg("EVAL: Column '" . $elem . "' not found!"); return false; } $out_str .= "%{$origColNr}%"; } } debug_print("New Eval String: {$out_str}\n"); $val_str = ""; // apply function (use values from the original column as input) $rowCount = count($this->rows); $colCount = count($this->colNames); for ($j = 0; $j < $rowCount; ++$j) { $val_str = $out_str; for ($k = 0; $k < $colCount; ++$k) { if (!is_false(strpos($val_str, "%{$k}%"))) { $val_str = str_replace("%{$k}%", $this->rows[$j]->fields[$k], $val_str); } } debug_print("VAL_STR={$val_str}\n"); $this->rows[$j]->fields[$i] = execFunc($this->colFuncs[$i], $val_str); } $this->colFuncsExecuted[$i] = true; // function with paramater, but the parameter is not a column } else { if ($this->colNames[$i] && !is_empty_str($this->colNames[$i]) && (is_numeric($this->colNames[$i]) || has_quotes($this->colNames[$i]))) { $param = $this->colNames[$i]; if (has_quotes($param)) { remove_quotes($param); } $result = execFunc($this->colFuncs[$i], $param); $rowCount = count($this->rows); debug_print("a function with a non-column parameter! (result={$result})<br>"); for ($j = 0; $j < $rowCount; ++$j) { $this->rows[$j]->fields[$i] = $result; } $this->colFuncsExecuted[$i] = true; // function with parameter? =>execute function with the values form the original column } else { if ($this->colNames[$i]) { debug_print("a function with a column parameter!<br>"); // find original column (without function) $origColNr = $this->findColNrByAttrs($this->colNames[$i], $this->colTables[$i], ""); if ($origColNr == NOT_FOUND) { print_error_msg("Column '" . $this->colNames[$i] . "' not found!"); return false; } // copy some column header data from the original $this->colTables[$i] = $this->colTables[$origColNr]; $this->colTableAliases[$i] = $this->colTableAliases[$origColNr]; // apply function (use values from the original column as input) $rowCount = count($this->rows); for ($j = 0; $j < $rowCount; ++$j) { $this->rows[$j]->fields[$i] = execFunc($this->colFuncs[$i], $this->rows[$j]->fields[$origColNr]); } $this->colFuncsExecuted[$i] = true; // function without parameter: just execute! } else { debug_print("a function with no parameters!<br>"); $result = execFunc($this->colFuncs[$i], ""); $rowCount = count($this->rows); for ($j = 0; $j < $rowCount; ++$j) { $this->rows[$j]->fields[$i] = $result; } $this->colFuncsExecuted[$i] = true; } } } } }
function getFilteredResultSet(&$resultSet) { global $g_sqlComparisonOperators; for ($i = 0; $i < $this->getChildCount(); ++$i) { $child = $this->childs[$i]; $this->resultSetsFromChilds[] = $child->getFilteredResultSet($resultSet); } $sp = new StringParser(); //$or_expr=explode_resp_quotes(" OR ",$this->expr_str); $sp->setConfig(array("'", "\""), "\\", array(), array(" OR "), false, false); $sp->setString($this->expr_str); $or_expr = $sp->splitWithSpecialElements(); $andResultSets = array(); for ($i = 0; $i < count($or_expr); ++$i) { $fieldValuePair = array(); $fields = array(); $values = array(); $operators = array(); $childsToInclude = 0; //$and_expr=explode_resp_quotes(" AND ",$or_expr[$i]); $sp->setConfig(array("'", "\""), "\\", array(), array(" AND "), false, false); $sp->setString($or_expr[$i]); $and_expr = $sp->splitWithSpecialElements(); array_walk($and_expr, "array_walk_trim"); for ($j = 0; $j < count($and_expr); ++$j) { if ($and_expr[$j] == "%") { $childsToInclude++; } else { //$operators[]=explode_resp_quotes_multisep($g_sqlComparisonOperators,$and_expr[$j],$fieldValuePair); $usedOps = array(); $sp->setConfig(array("'", "\""), "\\", array(), $g_sqlComparisonOperators, false, false); $sp->setString($and_expr[$j]); $fieldValuePair = $sp->splitWithSpecialElementsRetUsed($usedOps); $operators[] = trim($usedOps[0]); // Remove escape chars (because parseNextElementRaw() was used // to parse Where Statements) list($field, $value) = $fieldValuePair; $fields[] = stripslashes(trim($field)); // like bugfix: don't remove escape chars on the LIKE value if (strtoupper(trim($operators[count($operators) - 1])) == "LIKE") { $values[] = trim($value); } else { $values[] = stripslashes(trim($value)); } } } if (TXTDBAPI_DEBUG) { echo "<br>fields and values to use for AND filter:<br>"; print_r($fields); echo "<br>"; print_r($values); echo "<br>"; print_r($operators); echo "<br>"; echo "childsToInclude: " . $childsToInclude . "<br>"; } $andResultSets[] = $resultSet->filterRowsByAndConditions($fields, $values, $operators); debug_print("filterRowsByAndConditions done<br>"); for ($j = 0; $j < $childsToInclude; ++$j) { $andResultSets[$i]->filterResultSetAndWithAnother($this->resultSetsFromChilds[$this->childResultSetPos++]); } if (TXTDBAPI_DEBUG) { echo "Filtered ResultSet:<br>"; $andResultSets[$i]->dump(); } } $finalResultSet = $andResultSets[0]; for ($i = 1; $i < count($andResultSets); ++$i) { $andResultSets[$i]->reset(); while ($andResultSets[$i]->next()) { $finalResultSet->appendRow($andResultSets[$i]->getCurrentValues(), $andResultSets[$i]->getCurrentRowId()); } } if (TXTDBAPI_DEBUG) { echo "<br><br>FINAL RESULT SET:<br>"; $finalResultSet->dump(); } return $finalResultSet; }
function compare_like($value1, $value2) { static $patterns = array(); // Lookup precomputed pattern if (isset($patterns[$value2])) { $pat = $patterns[$value2]; } else { // Calculate pattern $rc = 0; $mod = ""; $prefix = "/^"; $suffix = "\$/"; // quote regular expression characters $str = preg_quote($value2, "/"); // unquote \ $str = str_replace("\\\\", "\\", $str); // Optimize leading/trailing wildcards if (substr($str, 0, 1) == '%') { $str = substr($str, 1); $prefix = "/"; } if (substr($str, -1) == '%' && substr($str, -2, 1) != '\\') { $str = substr($str, 0, -1); $suffix = "/"; } // case sensitive ? if (!LIKE_CASE_SENSITIVE) { $mod = "i"; } // setup a StringParser and replace unescaped '%' with '.*' $sp = new StringParser(); $sp->setConfig(array(), "\\", array()); $sp->setString($str); $str = $sp->replaceCharWithStr("%", ".*"); // replace unescaped '_' with '.' $sp->setString($str); $str = $sp->replaceCharWithStr("_", "."); $pat = $prefix . $str . $suffix . $mod; // Stash precomputed value $patterns[$value2] = $pat; } return preg_match($pat, $value1); }