public function setValueForKey($key, $value)
 {
     if (array_key_exists($key, $this->attributeNames)) {
         $this->attributes[$key] = $value;
     } else {
         parent::setValueForKey($key, $value);
     }
 }
Example #2
0
 /**
  * Do the pages search
  * @access public
  * @param queries The search query (array)
  */
 function searchPages($queries)
 {
     global $imSettings;
     $html = "";
     $found_content = array();
     $found_count = array();
     if (is_array($this->scope)) {
         foreach ($this->scope as $filename) {
             $count = 0;
             $weight = 0;
             $file_content = @implode("\n", file($filename));
             // Replace the nonbreaking space with a white space
             // to avoid that is converted to a 196+160 UTF8 char
             $file_content = str_replace(" ", " ", $file_content);
             if (function_exists("html_entity_decode")) {
                 $file_content = html_entity_decode($file_content, ENT_COMPAT, 'UTF-8');
             }
             // Remove the page menu
             while (stristr($file_content, "<div id=\"imPgMn\"") !== false) {
                 $style_start = imstripos($file_content, "<div id=\"imPgMn\"");
                 $style_end = imstripos($file_content, "</div", $style_start);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             // Remove the breadcrumbs
             while (stristr($file_content, "<div id=\"imBreadcrumb\"") !== false) {
                 $style_start = imstripos($file_content, "<div id=\"imBreadcrumb\"");
                 $style_end = imstripos($file_content, "</div", $style_start);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             // Remove CSS
             while (stristr($file_content, "<style") !== false) {
                 $style_start = imstripos($file_content, "<style");
                 $style_end = imstripos($file_content, "</style", $style_start);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             // Remove JS
             while (stristr($file_content, "<script") !== false) {
                 $style_start = imstripos($file_content, "<script");
                 $style_end = imstripos($file_content, "</script", $style_start);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             // Remove PHP
             while (stristr($file_content, "<?php") !== false) {
                 $style_start = imstripos($file_content, "<?php");
                 $style_end = imstripos($file_content, "?>", $style_start) !== false ? imstripos($file_content, "?>", $style_start) + 2 : strlen($file_content);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             $file_title = "";
             // Replace the dynamic objects with their content
             if (is_array($imSettings['search']['dynamicobjects'])) {
                 foreach ($imSettings['search']['dynamicobjects'] as $id => $object) {
                     // Only if the object is in the current scope
                     if ($object['Page'] != $filename) {
                         continue;
                     }
                     // Load the object's content
                     $dynobj = new DynamicObject($object['ObjectId']);
                     $dynobj->setDefaultText($object['DefaultText']);
                     if (isset($object['Folder'])) {
                         // Load from file
                         $dynobj->loadFromFile(pathCombine(array($imSettings['general']['public_folder'], $object['Folder'])));
                     } else {
                         if (isset($object['Database']) && isset($object['Table'])) {
                             // Load from db
                             $db = getDbData($object['Database']);
                             $dynobj->loadFromDb($db['host'], $db['user'], $db['password'], $db['database'], $object['Table']);
                         }
                     }
                     // Replace the content
                     $needle_start = "<!-- search-tag " . $object['ObjectId'] . " start -->";
                     $needle_end = "<!-- search-tag " . $object['ObjectId'] . " end -->";
                     $find_start = strpos($file_content, $needle_start);
                     $find_end = strpos($file_content, $needle_end) + strlen($needle_end);
                     $file_content = substr($file_content, 0, $find_start) . $dynobj->getContent() . substr($file_content, $find_end);
                 }
             }
             // Get the title of the page
             preg_match('/\\<title\\>([^\\<]*)\\<\\/title\\>/', $file_content, $matches);
             if (count($matches) > 1) {
                 $file_title = $matches[1];
             } else {
                 preg_match('/\\<h2\\>([^\\<]*)\\<\\/h2\\>/', $file_content, $matches);
                 if (count($matches) > 1) {
                     $file_title = $matches[1];
                 }
             }
             if ($file_title != "") {
                 foreach ($queries as $query) {
                     $title = imstrtolower($file_title);
                     while (($title = stristr($title, $query)) !== false) {
                         $weight += 5;
                         $count++;
                         $title = substr($title, strlen($query));
                     }
                 }
             }
             // Get the keywords
             preg_match('/\\<meta name\\=\\"keywords\\" content\\=\\"([^\\"]*)\\" \\/>/', $file_content, $matches);
             if (count($matches) > 1) {
                 $keywords = $matches[1];
                 foreach ($queries as $query) {
                     $tkeywords = imstrtolower($keywords);
                     while (($tkeywords = stristr($tkeywords, $query)) !== false) {
                         $weight += 4;
                         $count++;
                         $tkeywords = substr($tkeywords, strlen($query));
                     }
                 }
             }
             // Get the description
             preg_match('/\\<meta name\\=\\"description\\" content\\=\\"([^\\"]*)\\" \\/>/', $file_content, $matches);
             if (count($matches) > 1) {
                 $keywords = $matches[1];
                 foreach ($queries as $query) {
                     $tkeywords = imstrtolower($keywords);
                     while (($tkeywords = stristr($tkeywords, $query)) !== false) {
                         $weight += 3;
                         $count++;
                         $tkeywords = substr($tkeywords, strlen($query));
                     }
                 }
             }
             // Remove the page title from the result
             while (stristr($file_content, "<h2") !== false) {
                 $style_start = imstripos($file_content, "<h2");
                 $style_end = imstripos($file_content, "</h2", $style_start);
                 $style = substr($file_content, $style_start, $style_end - $style_start);
                 $file_content = str_replace($style, "", $file_content);
             }
             $page_pos = strpos($file_content, "<div id=\"imContent\">") + strlen("<div id=\"imContent\">");
             $page_end = strpos($file_content, "<div id=\"imBtMn\">");
             if ($page_end == false) {
                 $page_end = strpos($file_content, "</body>");
             }
             $file_content = strip_tags(substr($file_content, $page_pos, $page_end - $page_pos));
             $t_file_content = imstrtolower($file_content);
             foreach ($queries as $query) {
                 $file = $t_file_content;
                 while (($file = stristr($file, $query)) !== false) {
                     $count++;
                     $weight++;
                     $file = substr($file, strlen($query));
                 }
             }
             if ($count > 0) {
                 $found_count[$filename] = $count;
                 $found_weight[$filename] = $weight;
                 $found_content[$filename] = $file_content;
                 if ($file_title == "") {
                     $found_title[$filename] = $filename;
                 } else {
                     $found_title[$filename] = $file_title;
                 }
             }
         }
     }
     if (count($found_count)) {
         arsort($found_weight);
         $i = 0;
         foreach ($found_weight as $name => $weight) {
             $count = $found_count[$name];
             $i++;
             if ($i > $this->page * $this->results_per_page && $i <= ($this->page + 1) * $this->results_per_page) {
                 $title = strip_tags($found_title[$name]);
                 $file = $found_content[$name];
                 $file = strip_tags($file);
                 $ap = 0;
                 $filelen = strlen($file);
                 $text = "";
                 for ($j = 0; $j < ($count > 6 ? 6 : $count); $j++) {
                     $minpos = $filelen;
                     $word = "";
                     foreach ($queries as $query) {
                         if ($ap < $filelen && ($pos = strpos(strtoupper($file), strtoupper($query), $ap)) !== false) {
                             if ($pos < $minpos) {
                                 $minpos = $pos;
                                 $word = $query;
                             }
                         }
                     }
                     $prev = explode(" ", substr($file, $ap, $minpos - $ap));
                     if (count($prev) > ($ap > 0 ? 9 : 8)) {
                         $prev = ($ap > 0 ? implode(" ", array_slice($prev, 0, 8)) : "") . " ... " . implode(" ", array_slice($prev, -8));
                     } else {
                         $prev = implode(" ", $prev);
                     }
                     if (strlen($word)) {
                         $text .= $prev . "<strong>" . substr($file, $minpos, strlen($word)) . "</strong>";
                         $ap = $minpos + strlen($word);
                     }
                 }
                 $next = explode(" ", substr($file, $ap));
                 if (count($next) > 9) {
                     $text .= implode(" ", array_slice($next, 0, 8)) . "...";
                 } else {
                     $text .= implode(" ", $next);
                 }
                 $text = str_replace("|", "", $text);
                 $text = str_replace("<br />", " ", $text);
                 $text = str_replace("<br>", " ", $text);
                 $text = str_replace("\n", " ", $text);
                 $text = str_replace("\t", " ", $text);
                 $text = trim($text);
                 $html .= "<div class=\"imSearchPageResult\"><h3><a class=\"imCssLink\" href=\"" . $name . "\">" . strip_tags($title, "<b><strong>") . "</a></h3>" . strip_tags($text, "<b><strong>") . "<div class=\"imSearchLink\"><a class=\"imCssLink\" href=\"" . $name . "\">" . $imSettings['general']['url'] . "/" . $name . "</a></div></div>\n";
             }
         }
         $html = preg_replace_callback('/\\s+/', create_function('$matches', 'return implode(\' \', $matches);'), $html);
         $html .= "<div class=\"imSLabel\">&nbsp;</div>\n";
     }
     return array("content" => $html, "count" => count($found_content));
 }
 public function setValueForKey($key, $value)
 {
     if (array_key_exists($key, $this->parentNames)) {
         if (is_a($value, 'DatabaseObject')) {
             $key = $value->primaryKeyName;
             $value = $value->primaryKey;
         }
     }
     if (array_key_exists($key, $this->attributeNames)) {
         $this->attributes[$key] = $value;
     } else {
         if (array_key_exists($key, $this->childNames) && is_array($value)) {
             if (!array_key_exists($key, $this->children)) {
                 $this->children[$key] = array();
             }
             //Add new children
             foreach ($value as $child) {
                 if (is_a($child, $this->childNames[$key]) && is_a($child, 'DatabaseObject')) {
                     if (!array_key_exists($child->primaryKey, $this->children[$key])) {
                         $this->children[$key][$child->primaryKey] = $child;
                     }
                 }
             }
             //Remove old children
             foreach (array_keys($this->children[$key]) as $childPrimaryKey) {
                 if (!array_key_exists($childPrimaryKey, $value)) {
                     unset($this->children[$key][$childPrimaryKey]);
                 }
             }
         } else {
             if (array_key_exists($key, $this->peerNames) && is_array($value)) {
                 if (!array_key_exists($key, $this->peers)) {
                     $this->peers[$key] = array();
                 }
                 //Add new peers
                 foreach ($value as $peer) {
                     if (is_a($peer, $this->peerNames[$key]) && is_a($peer, 'DatabaseObject')) {
                         if (!array_key_exists($peer->primaryKey, $this->peers[$key])) {
                             $this->peers[$key][$peer->primaryKey] = $peer;
                         }
                     }
                 }
                 //Remove old peers
                 foreach (array_keys($this->peers[$key]) as $peerPrimaryKey) {
                     if (!array_key_exists($peerPrimaryKey, $value)) {
                         unset($this->peers[$key][$peerPrimaryKey]);
                     }
                 }
             } else {
                 parent::setValueForKey($key, $value);
             }
         }
     }
 }