| Server IP : 195.130.67.5 / Your IP : 216.73.216.231 Web Server : Microsoft-IIS/10.0 System : Windows NT WEBSERVER1 10.0 build 17763 (Windows Server 2016) i586 User : IUSR ( 0) PHP Version : 7.4.19 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/Civil/includes/ |
Upload File : |
<?
////////////////////////////////////////////////////////////////////////////////////////
// Class: Search
// Purpose: Κλάση διαχείρισης κατηγοριών
///////////////////////////////////////////////////////////////////////////////////////
require_once('Translator.php');
class Search
{
public $query;
private $connector;
private $translator;
public function __construct($db, $ldap=false)
{
$this->connector = $db;
if ($ldap==false)
$this->translator = new Translator($db);
}
// Η μηχανή αναζήτησης επιστρέφει έναν πίνακα με array('id' => array(), 'title' => array(), 'text' => array(), 'cat_id' => array()) με τα αποτελέσματα της αναζήτησης στα Άρθρα
function searchEngine($current_lang_id, $perm_frontend_view_registered_content)
{
$qry_array = preg_split('/[, ]+/',trim($this->query));
$creteria = "";
$hits = "";
if ($perm_frontend_view_registered_content==0)
$access_level = "and access=0";
if ($current_lang_id == $this->translator->default_lang["id"]) // Ψάξε στην default γλώσσα
{
foreach ($qry_array as $qry)
{
if ($creteria!="")
{
$creteria .= " or ";
$hits .= " + ";
}
$creteria .= " title like '%$qry%' or body like '%$qry%' ";
$hits .= " 2 * (title like '%$qry%') + (body like '%$qry%') "; // Μετράει πόσες από τις λέξεις κλειδιά εμφανίζονται στον τίτλο ή στο κυρίως περιεχόμενο ώστε να ταξινομήσουμε τα αποτελέσματα με βάση το ποια περιέχουν τα περισσότερα από τις λέξεις κλειδιά. Κλειδιά στον τίτλο έχουν διπλάσια βαρύτητα.
}
$result = $this->connector->query("SELECT *, id as content_id, ($hits) as hits FROM content WHERE $creteria $access_level and publish_up<=NOW() and (publish_down > NOW() or publish_down='0000-00-00 00:00:00') ORDER BY hits DESC, created DESC, id DESC");
}
else
{
foreach ($qry_array as $qry)
{
if ($creteria!="")
{
$creteria .= " or ";
$hits .= " + ";
}
$creteria .= " translations.value like '%$qry%' ";
$hits .= " (translations.value like '%$qry%') "; // Μετράει πόσες από τις λέξεις κλειδιά εμφανίζονται στον τίτλο ή στο κυρίως περιεχόμενο ώστε να ταξινομήσουμε τα αποτελέσματα με βάση το ποια περιέχουν τα περισσότερα από τις λέξεις κλειδιά. Κλειδιά στον τίτλο έχουν διπλάσια βαρύτητα.
}
if ($creteria!="")
$creteria = "and ($creteria)";
$result = $this->connector->query("SELECT content.id as content_id, content.category_id, translations.reference_field, translations.value, ($hits) as hits FROM content, translations WHERE translations.language_id=".$current_lang_id." and translations.reference_table='content' and translations.reference_id=content.id and (reference_field='title' or reference_field='body') $creteria $access_level and publish_up<=NOW() and (publish_down > NOW() or publish_down='0000-00-00 00:00:00') GROUP BY reference_id ORDER BY hits DESC, content.created DESC, content.id DESC");
}
while ($myrow = $this->connector->fetchArray($result))
{
if ($current_lang_id == $this->translator->default_lang["id"]) // Αποτελέσματα από την default γλώσσα
{
$txt1=$myrow["title"];
$txt2=strip_tags($myrow["body"]);
}
else
{
if ($myrow["reference_field"] == "title")
{
$txt1=$myrow["value"];
$translated_fields = $this->translator->fetchTranslatedContent($current_lang_id, $myrow["content_id"], "content", "body");
if (count($translated_fields)>0)
{
$txt2=strip_tags($translated_fields["body"]);
}
}
else if ($myrow["reference_field"] == "body")
{
$txt2=strip_tags($myrow["value"]);
$translated_fields = $this->translator->fetchTranslatedContent($current_lang_id, $myrow["content_id"], "content", "title");
if (count($translated_fields)>0)
{
$txt1=$translated_fields["title"];
}
}
}
$search_for = array('Ά','ά','Ό','ό','Έ','έ','Ή','ή','Ί','ί','Ύ','ύ','Ώ','ώ','ς','à','é','è','ç','Ä','ä','Ö','ö','Ñ','ñ');
$replace_with = array('Α','α','Ο','ο','Ε','ε','Η','η','Ι','ι','Υ','υ','Ω','ω','σ','a','e','e','c','A','a','O','o','N','n');
$found = false;
$txt1_found_from = array();
$txt1_found_to = array();
$txt2_found_from = array();
$txt2_found_to = array();
foreach($qry_array as $qry)
{
$srchlen = mb_strlen($qry,'utf-8');
$txt1_lower = str_replace($search_for,$replace_with,mb_strtolower($txt1,'utf-8'));
$txt2_lower = str_replace($search_for,$replace_with,mb_strtolower($txt2,'utf-8'));
$query_lower = str_replace($search_for,$replace_with,mb_strtolower($qry,'utf-8'));
if (stristr($txt1_lower, $query_lower))
{
$prevpos=0;
while (($nextpos = mb_strpos($txt1_lower,$query_lower,$prevpos,'utf-8'))!==false)
{
$txt1_found_from[] = $nextpos;
$txt1_found_to[] = $nextpos + $srchlen;
$prevpos = $nextpos+$srchlen;
}
$found = true;
}
if (stristr($txt2_lower, $query_lower))
{
$prevpos=0;
while (($nextpos = mb_strpos($txt2_lower, $query_lower, $prevpos,'utf-8'))!==false)
{
$txt2_found_from[] = $nextpos;
$txt2_found_to[] = $nextpos + $srchlen;
$prevpos = $nextpos+$srchlen;
}
$found = true;
}
}
// Ταξινόμηση των πινάκων $txt1_found_from, $txt1_found_to, $txt2_found_from, $txt2_found_to
sort($txt1_found_from);
sort($txt1_found_to);
sort($txt2_found_from);
sort($txt2_found_to);
$size1 = count($txt1_found_from);
$size2 = count($txt2_found_from);
for ($i=($size1-1); $i>=0; --$i)
{
if ($size1==1)
{
$txt1 = mb_substr($txt1, 0, $txt1_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt1, $txt1_found_from[$i], ($txt1_found_to[$i]-$txt1_found_from[$i]),'utf-8')."</span>".mb_substr($txt1, $txt1_found_to[$i],strlen($txt1),'utf-8');
}
else if ($i==$size1-1 or ($i<$size1-1 and $txt1_found_to[$i] < $txt1_found_from[$i+1]))
{
$txt1 = mb_substr($txt1, 0, $txt1_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt1, $txt1_found_from[$i], ($txt1_found_to[$i]-$txt1_found_from[$i]),'utf-8')."</span>".mb_substr($txt1, $txt1_found_to[$i],strlen($txt1),'utf-8');
}
}
$contex_size = 70;
for ($i=($size2-1); $i>=0; --$i)
{
if ($size2==1)
{
if ($txt2_found_from[$i]>$contex_size)
$txt2 = "... ".mb_substr($txt2, ($txt2_found_from[$i]-$contex_size), $contex_size,'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt2, $txt2_found_from[$i], ($txt2_found_to[$i]-$txt2_found_from[$i]),'utf-8')."</span>".mb_substr($txt2, $txt2_found_to[$i],$contex_size,'utf-8')." ...";
else
$txt2 = mb_substr($txt2, 0, $txt2_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt2, $txt2_found_from[$i], ($txt2_found_to[$i]-$txt2_found_from[$i]),'utf-8')."</span>".mb_substr($txt2, $txt2_found_to[$i],$contex_size,'utf-8')." ...";
}
else if ($i==$size2-1)
{
$txt2 = mb_substr($txt2, 0, $txt2_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt2, $txt2_found_from[$i], ($txt2_found_to[$i]-$txt2_found_from[$i]),'utf-8')."</span>".mb_substr($txt2, $txt2_found_to[$i],$contex_size,'utf-8')." ...";
}
else if ($i<$size2-1)
{
if ($txt2_found_to[$i] < $txt2_found_from[$i+1])
{
if (($txt2_found_from[$i+1] - $txt2_found_to[$i]) > $contex_size)
$txt2 = mb_substr($txt2, 0, $txt2_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt2, $txt2_found_from[$i], ($txt2_found_to[$i]-$txt2_found_from[$i]),'utf-8')."</span>".mb_substr($txt2, $txt2_found_to[$i],round($contex_size/2),'utf-8')." ... ".mb_substr($txt2, ($txt2_found_from[$i+1]-round($contex_size/2)),strlen($txt2),'utf-8');
else
$txt2 = mb_substr($txt2, 0, $txt2_found_from[$i],'utf-8')."<span style='background-color: #FBFCB0'>".mb_substr($txt2, $txt2_found_from[$i], ($txt2_found_to[$i]-$txt2_found_from[$i]),'utf-8')."</span>".mb_substr($txt2, $txt2_found_to[$i],strlen($txt2),'utf-8');
}
if ($i==0 and $txt2_found_from[0]>$contex_size)
$txt2 = "... ".mb_substr($txt2, ($txt2_found_from[0]-$contex_size), strlen($txt2),'utf-8');
}
}
if ($found)
{
$res_id[] = $myrow["content_id"];
$res_title[] = $txt1;
$res_text[] = $txt2;
$res_cat_id[] = $myrow["category_id"];
}
}
return array( 'id' => $res_id, 'title' => $res_title, 'text' => $res_text, 'cat_id' => $res_cat_id);
}
// Η μηχανή αναζήτησης επιστρέφει έναν πίνακα με array('id' => array(), 'title' => array(), 'address' => array(), 'city' => array(), 'country' => array(), 'zipcode' => array(), 'tel' => array(), 'fax' => array(), 'cellphone' => array(), 'email' => array(), 'website' => array(), 'extra_info' => array(), 'cat_id' => array()) με τα αποτελέσματα της αναζήτησης στις Επαφές
function searchContactsEngine($current_lang_id, $perm_frontend_view_registered_content)
{
$creteria = "";
if ($perm_frontend_view_registered_content==0)
$access_level = "and access=0";
if ($current_lang_id == $this->translator->default_lang["id"]) // Ψάξε στην default γλώσσα
{
$result = $this->connector->query("SELECT *, id as contact_id FROM contacts WHERE title like '%".$this->query."%' $access_level ORDER BY title ASC");
}
else
{
$result = $this->connector->query("SELECT contacts.id as contact_id, contacts.category_id, translations.reference_field, translations.value FROM contacts, translations WHERE translations.language_id=".$current_lang_id." and translations.reference_table='contacts' and translations.reference_id=contacts.id and reference_field='title' and translations.value like '%".$this->query."%' $access_level GROUP BY reference_id ORDER BY contacts.title ASC");
}
while ($myrow = $this->connector->fetchArray($result))
{
$contact_found = array();
if ($current_lang_id == $this->translator->default_lang["id"]) // Αποτελέσματα από την default γλώσσα
{
$contact_found = $myrow;
}
else
{
$contact_found["contact_id"] = $myrow["contact_id"];
$contact_found["title"] = $myrow["value"];
$translated_fields = $this->translator->fetchTranslatedContent($current_lang_id, $myrow["contact_id"], "contacts");
if (count($translated_fields)>0)
{
foreach ($translated_fields as $key=>$value)
{
$contact_found[$key] = $value;
}
}
}
$res_id[] = $contact_found["contact_id"];
$res_title[] = $contact_found["title"];
$res_address[] = $contact_found["address"];
$res_city[] = $contact_found["city"];
$res_country[] = $contact_found["country"];
$res_zipcode[] = $contact_found["zipcode"];
$res_tel[] = $contact_found["tel"];
$res_fax[] = $contact_found["fax"];
$res_cellphone[] = $contact_found["cellphone"];
$res_email[] = $contact_found["email"];
$res_website[] = $contact_found["website"];
$res_extra_info[] = $contact_found["extra_info"];
$res_cat_id[] = $myrow["category_id"];
}
return array( 'id' => $res_id, 'title' => $res_title, 'address' => $res_address, 'city' => $res_city, 'country' => $res_country, 'zipcode' => $res_zipcode, 'tel' => $res_tel, 'fax' => $res_fax, 'cellphone' => $res_cellphone, 'email' => $res_email, 'website' => $res_website, 'extra_info' => $res_extra_info, 'cat_id' => $res_cat_id);
}
// Η μηχανή αναζήτησης επιστρέφει έναν πίνακα με τα αποτελέσματα της αναζήτησης στον Ldap
function searchLdapEngine($filter, $attributes, $sizelimit, $current_lang_code)
{
$sr = $this->connector->ldapSearch($filter, $attributes, 0, $sizelimit);
if ($current_lang_code=="el")
$sr = $this->connector->sortResults($sr, "sn;lang-el");
else
$sr = $this->connector->sortResults($sr, "sn");
$entries = $this->connector->getEntries($sr);
return $entries;
}
// Αφαίρεσε μικρές λέξεις κλειδιά μικρότερες ή ίσες των $n χαρακτήρων
public function removeShortKeys($query, $n)
{
if ($n==1)
$query = preg_replace('/(^|\s)\S{1}(\s|$)/u',' ',$query);
else if ($n>1)
$query = preg_replace('/(^|\s)\S{1,'.$n.'}(\s|$)/u',' ',$query);
return $query;
}
// Αφαίρεσε από το query string κοινές λέξεις κλειδιά (common keywords) που περιλαμβάνονται στον πίνακα $search_ignore
public function removeCommonKeys($query, $search_ignore)
{
if (count($search_ignore)>0)
$query = preg_replace('/(^|\s)('.implode('|',$search_ignore).')(\s|$)/ui',' ',$query);
return $query;
}
// Ελέγχει αν το query είναι μία κοινή λέξη (common keyword) που περιλαμβάνονται στον πίνακα $search_ignore
public function isCommonKey($query, $search_ignore)
{
if (count($search_ignore)>0)
{
if (in_array(mb_strtolower($query,'utf-8'),$search_ignore))
return true;
}
return false;
}
// Ελέγχει αν το query μικρή λέξη μικρότερη ή ίση των $n χαρακτήρων
public function isShortKey($query, $n)
{
if (mb_strlen($query,'utf-8')<=$n)
return true;
else
return false;
}
// Strips various characters from $line.
public function cleanQuery($query)
{
$unwanted_chars = array(",","\"","'","+","-","*","/","!","%",">","<","^","(",")","[","]","{","}","\\","=","$","#","?","~",":","_"," ","&","©"," ",""","&",";","\n");
$query = str_replace($unwanted_chars, " ", $query);
return trim($query);
}
}