/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/* SEARCH ENGINE */
/**
* This is the search engine class. It is useful to make a text search for cmsdam sections body.
* Class instance: $cms->search
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
class cmsdam_search
{
/**
* You should not use this variable from your section.
* Sections class instance.
*/
var $sections;
/**
* You should not use this variable from your section.
* Users class instance.
*/
var $users;
/**
* You should not use this variable from your section.
* Groups class instance.
*/
var $groups;
/**
* You should not use this variable from your section.
* Configuration class instance.
*/
var $conf;
/**
* Sections found array, null if a search wasn't made
* @type array
*/
var $search_found_section = null; // sections found (Are null if a search wasn't made)
/**
* Priority search array, null if a search wasn't made
* @type array
*/
var $search_found_priority = null;
/**
* Time taken by search in seconds.
* @type int
*/
var $search_stime = 0; // seconds for search (0 if a search wasn't made)
/**
* Time taken by search in microseconds.
* @type int
*/
var $search_utime = 0; // useconds for search (0 if a search wasn't made)
/**
* Do the search for the granted sections and returns the number of found sections.
* NOTE: The array search_found_section is setted with the name of found sections.
* "$search" is the text you want to search.
* @param str search The text you want to search
* @return int The number of found sections
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @author Paolo Ciuffetti <paolo@underbridges.com>
*/
function search_find($search)
{ /* This method set some search array and return number of found sections */
// Pickup the first time
$time1 = microtime ();
// Returns a section array with all valid section names for that user
$User = $this->users->logged_user;
if ($User == "") { // Anonymous user
$User = "anonymous";
}
$count = 0;
for ($i=0; $i<$this->sections->n_sections; $i++) { // Section cycle
unset ($users_arr);
$flag=0;
// Users array valid for this section
$users_arr = split (',', $this->conf->Search_users_arr[$i]);
$users_arr_cnt = count ($users_arr);
$groups_arr = split(',', $this->conf->Search_groups_arr[$i]);
// Fill in the users array for each group user
for ($cnt = 0; $cntgroups->group_users_name($temp);
for ($tmp = 0; $tmp0) && ($this->conf->Section_arr[$i] != "") &&
($this->conf->Section_file_arr[$i] != "") &&
($this->sections->section_isvalid($this->conf->Section_arr[$i]))) {
// This section is valid for that user
$sections_valid[$count] = $this->conf->Section_arr[$i];
$count ++;
}
}
// sections_valid now contains an array with all sections that can be searched for the logged user
// echo "DEBUG: sections_valid: " . print_r ($sections_valid);
$search = htmlspecialchars($search);
// Make the string vector using $search
$search = trim($search);
$V = explode (" ", $search);
$n_parole = count($V);
// Priority vector is initialized!
// for ($i = 0; $i < $n_parole; $P[$i++] = 0);
// die ('DEBUG: $P[0]: ' . $P[0]);
$key = 1;
for ($cont = 0; $cont < $n_parole; $cont++)
{ /* SEARCH ENGINE */
for ($i = 0; $i < count($this->conf->Section_file_arr); $i++) {
// File list cycle
$file = $this->sections->templates_lang_relpath . "/" . $this->conf->Section_file_arr[$i];
if (!is_file($file)) { // template file for the given language does not exist.
// Switchin to default.
$file = $this->sections->templates_relpath . "/" . $this->conf->Section_file_arr[$i];
}
// $ext = substr($file, -3);
// if ($this->conf->Search_arr[$i] == "YES" && $this->conf->Section_arr[$i] && $this->conf->Visible_arr[$i] == "YES") { // Must be searched!!
/*
echo "DEBUG: sezione: " . $this->conf->Section_arr[$i];
echo "DEBUG: in_array(): " . in_array($this->conf->Section_arr[$i], $sections_valid);
echo "DEBUG: array_search(): " . array_search($this->conf->Section_arr[$i], $sections_valid)."
";
*/
// Is the section in the list of valid sections to be searched?
if (in_array($this->conf->Section_arr[$i], $sections_valid) == true) { // Must be searched!!
/*
$HFile = @fopen ($file, 'r');
// echo "DEBUG: HFile: " . $HFile;
if ($HFile != null) { // The file exist an can be open
$record = @fread ($HFile, filesize ($file));
@fclose ($HFile);
}
*/
$record = @file_get_contents($file);
$record = strip_tags($record);
$section = "";
// $shortdescription = "";
// $longdescription = "";
$section = $this->conf->Section_arr[$i];
// $shortdescription = $Shortdescription_arr[$i];
// $longdescription = $Longdescription_arr[$i];
// Take only 40 chars from the matched string.
$rec = substr (@stristr ($record, $V[$cont]), 0, 40);
if ((strlen($rec) > 0) && (strlen($section) > 0)) { // Search string found!
if (count ($this->search_found_section) == 0) { // Result array is empty
$this->search_found_section[$key] = "$section";
// $this->search_found_sdesc[$key] = "$shortdescription";
// $this->search_found_ldesc[$key] = "$longdescription";
$this->search_found_priority[$key] = 1;
// echo ("First element
");
} else { // Result array is not empty
$key = array_search($section, $this->search_found_section);
if ($key == FALSE) { // Found page not present in result array
// echo ('DEBUG: $key: ' . $key . "
");
$key = count ($this->search_found_section) + 1;
$this->search_found_section[$key] = "$section";
// $this->search_found_sdesc[$key] = "$shortdescription";
// $this->search_found_ldesc[$key] = "$longdescription";
$this->search_found_priority[$key] = 1;
} else { // Found page already present in result array
$this->search_found_priority[$key] ++;
}
}
} // if
} // if
} // for
/* END OF SEARCH ENGINE */
}
/*
array_shift($this->search_found_section);
*/
if (count ($this->search_found_section) > 0) { // Almost one section was found
// Sort the array to get the most important sections on TOP!!
// array_multisort($this->search_found_priority, SORT_DESC, SORT_NUMERIC, $this->search_found_section, $this->search_found_sdesc, $this->search_found_ldesc);
array_multisort($this->search_found_priority, SORT_DESC, SORT_NUMERIC, $this->search_found_section);
if ($this->search_found_section[0] == "") { // The first element must start from 0
$this->search_found_section[0] = "x";
// array_shift($this->search_found_section);
for ($i=0; $isearch_found_section); $i++) { // Array shifter
// Some PHP version have a bug with array_shift!!
$this->search_found_section[$i++] = $this->search_found_section[$i];
}
array_pop($this->search_found_section);
}
} else { // No sections found
// sections found (Are null if a search wasn't made)
// sections found short description
// $this->search_found_sdesc = null;
// sections found long description
// $this->search_found_ldesc = null;
// sections found descendent priority order
$this->search_found_section = null;
$this->search_found_priority = null;
}
// Pick the finish time
$time2 = microtime ();
// Calculate search time
$ar1 = explode(" ", $time1);
$ar2 = explode(" ", $time2);
$sec_rel = $ar2[1] - $ar1[1];
$usec_rel = (($ar2[0] - $ar1[0]) * 1000);
if ($ar2[0] < $ar1[0]) {
$usec_rel = (1 - ($ar1[0] - $ar2[0])) * 1000;
$sec_rel --;
}
$usec_rel = round ($usec_rel, 3);
if ($sec_rel < 0)
$sec_rel = 0;
// Search time in seconds
$this->search_stime = $sec_rel;
// Search time in milliseconds
$this->search_utime = $usec_rel;
return count ($this->search_found_section);
}
}
?>