protected function handle_non_seo_url()
{
$loop_count = 0;
foreach ($this->path_search_regex as $field => $searchRegex) {
foreach ($searchRegex as $key => $search) {
$search_pattern = $this->pattern_search[$field][$key];
$replace_pattern = $this->pattern_replace[$field][$key];
// Resets
$tag_values = array();
$tag_matches = array();
$output_matches = array();
$replace_matches = array();
$statements = array();
if (preg_match("~{$search}\$~i", $this->requesturi) or !empty($_POST) && preg_match("~" . FUSION_ROOT . $search . "~i", $this->requesturi)) {
// enable for debug - print_p($search);
preg_match_all("~{$search}~i", $this->requesturi, $output_matches, PREG_PATTERN_ORDER);
if (empty($output_matches[0]) && !empty($_POST)) {
preg_match_all("~" . FUSION_ROOT . $search . "~i", $this->requesturi, $output_matches, PREG_PATTERN_ORDER);
}
preg_match_all("~%(.*?)%~i", $search_pattern, $tag_matches);
preg_match_all("~%(.*?)%~i", $replace_pattern, $replace_matches);
if (!empty($tag_matches[0])) {
$tagData = array_combine(range(1, count($tag_matches[0])), array_values($tag_matches[0]));
foreach ($tagData as $tagKey => $tagVal) {
$tag_values[$tagVal] = $output_matches[$tagKey];
if (isset($this->pattern_tables[$field][$tagVal])) {
$table_info = $this->pattern_tables[$field][$tagVal];
$table_columns = array_flip($table_info['columns']);
$request_column = array_intersect($replace_matches[0], $table_columns);
$search_value = array_unique($output_matches[$tagKey]);
if (!empty($request_column)) {
$columns = array();
foreach ($request_column as $position => $column_tag) {
$columns[$column_tag] = $table_info['columns'][$column_tag];
$loop_count++;
}
$column_info = array_flip($columns);
/**
* Each SEF Rule Declared, You are going to spend 1 SQL query
*/
$sql = "SELECT " . $table_info['primary_key'] . ", " . implode(", ", $columns) . " ";
$sql .= "FROM " . $table_info['table'];
$sql .= " WHERE " . (!empty($table_info['query']) ? $table_info['query'] . " AND " : "");
$sql .= $table_info['primary_key'] . " IN (" . implode(",", $search_value) . ")";
$result = dbquery($sql);
if (dbrows($result) > 0) {
$other_values = array();
$data_cache = array();
while ($data = dbarray($result)) {
$dataKey = $data[$table_info['primary_key']];
unset($data[$table_info['primary_key']]);
foreach ($data as $key => $value) {
$data_cache[$column_info[$key]][$dataKey] = $value;
}
$loop_count++;
}
foreach ($data_cache as $key => $value) {
for ($i = 0; $i < count($output_matches[0]); $i++) {
$corresponding_value = $tag_values[$tagVal][$i];
$other_values[$key][$i] = $value[$corresponding_value];
$loop_count++;
}
}
$tag_values += $other_values;
}
}
}
$loop_count++;
}
}
/**
* Generate Statements for each buffer matches
*
* First, we merge the basic ones that has without tags
* It is irrelevant whether these are from SQL or from Preg
*/
for ($i = 0; $i < count($output_matches[0]); $i++) {
$statements[$i]["search"] = $search_pattern;
$statements[$i]["replace"] = $replace_pattern;
$loop_count++;
}
reset($tag_values);
while (list($regexTag, $tag_replacement) = each($tag_values)) {
for ($i = 0; $i < count($output_matches[0]); $i++) {
$statements[$i]["search"] = str_replace($regexTag, $tag_replacement[$i], $statements[$i]["search"]);
$statements[$i]["replace"] = str_replace($regexTag, $tag_replacement[$i], $statements[$i]["replace"]);
$loop_count++;
}
$loop_count++;
}
// Change Redirect via Scan
if (isset($statements[0]['replace'])) {
$this->redirect_301($this->cleanURL($statements[0]['replace']));
}
$output_capture_buffer = array("search" => $search, "output_matches" => $output_matches[0], "replace_matches" => $replace_matches[0], "seach_pattern" => $search_pattern, "replace_patern" => $replace_pattern, "tag_values" => $tag_values, "statements" => $statements, "loop_counter" => $loop_count);
} else {
preg_match_all("~{$search}~i", $this->output, $match);
$this->regex_statements['failed'][$field][] = array("search" => $search, "status" => "No matching content or failed regex matches", "results" => $match);
}
}
}
}