<?php
/*****************************************************************
************ Kunena Search Plugin For Joomla 1.5 ************
* @version-- plug_kunenasearch.php--- version 1.0--- March 7th, 2009
* @copyright (c) 2008 Miniola
* @license GNU GPL (http://www.gnu.org/licenses/gpl.txt)
* @Author R.T. Nawaz
*@Author Email:
[email protected]
*@Website http://Opensource.Miniola.com
*@Website http://kunena.aide-joomla.com/
******************************************************************/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
//Define registerEvents and language
$mainframe->registerEvent( 'onSearch', 'plgSearchplug_kunenasearch' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchplug_kunenasearchAreas' );
//Still Need to Make Language File for Backend
//JPlugin::loadLanguage( 'plg_search_plug_kunenasearch' );
//Array of search area
function &plgSearchplug_kunenasearchAreas()
{
static $areas = array(
'plug_kunenasearch' => 'Форум'
);
return $areas;
}
//Data base conection has to be made
function plgSearchplug_kunenasearch( $text, $phrase='', $ordering='', $areas=null )
{
$db =& JFactory::getDBO();
$user =& JFactory::getUser();
//Kunena's database tables
$kunena_categories_dbtable = "#__fb_categories";
$kunena_messages_dbtable = "#__fb_messages";
$kunena_messages_text_dbtable = "#__fb_messages_text";
//If the array is not correct, return it:
if (is_array( $areas )) {
if (!array_intersect( $areas, array_keys( plgSearchplug_kunenasearchAreas() ) )) {
return array();
}
}
//Get the plugin
$plugin =& JPluginHelper::getPlugin('search', 'plug_kunenasearch');
//load parameters
$pluginParams = new JParameter( $plugin->params );
//Define parameters
$forum_name = $pluginParams->def( 'forum_name', 'Forum' );
$search_limit = $pluginParams->def( 'search_limit', 50 );
$cat_search = $pluginParams->def( 'cat_search', 1 );
$allow_bbc = $pluginParams->def( 'allow_bbc', 1 );
$credit = $pluginParams->def( 'credit', 2 );
$c_result = $pluginParams->def( 'c_result', 0 );
$c_title = $pluginParams->def( 'c_title', 'Title' );
$c_section = $pluginParams->def( 'c_section', 'Section' );
$c_text = $pluginParams->def( 'c_text', 'This is where you will put the information to show at search results' );
$c_href = $pluginParams->def( 'c_href', 'http://www.anysite.com' );
$c_keywords = $pluginParams->def( 'c_keywords', 'Keyword1,Keword2,Keword3' );
$c_reset = $pluginParams->def( 'c_reset', 0 );
$c_browsernav = 1;
$c_created = "";
$a_title = "Miniola!";
$a_text = "We are proud to support Miniola! which is a small project to develop and revitalize Open Source and Free Software. Visit the Miniola! website to join and participate in the new opensource and free software community";
$a_section = "Free Open Source Software";
$a_href = "http://www.miniola.com/";
$a_created = "";
$a_keywords = "open source, free software, extension, plugin, joomla, software, mambots, template, module, ";
$a_browsernav = 1;
//Setup databases
if($c_reset == 1){
$db->setQuery('TRUNCATE TABLE #__kunena_searchplgc');
$db->query();
}
//Have to do these for functions-- for now just use it here
if( $c_result > 0 ) {
$db->setQuery('CREATE TABLE IF NOT EXISTS `#__kunena_searchplgc` ('
. ' `title` text NOT NULL,'
. ' `text` text NOT NULL,'
. ' `section` text NOT NULL,'
. ' `href` text NOT NULL,'
. ' `created` text NOT NULL,'
. ' `keywords` text NOT NULL,'
. ' `browsernav` tinyint(4) NOT NULL default \'0\','
. ' UNIQUE KEY `key` (`title`(10))'
. ' ) TYPE=MyISAM'
. ' ');
$db->query();
$db->setQuery( "INSERT IGNORE INTO `#__kunena_searchplgc` ("
. "\n `title`,"
. "\n `text`,"
. "\n `section`,"
. "\n `href`,"
. "\n `created`,"
. "\n `keywords`,"
. "\n `browsernav`"
. "\n ) VALUES ("
. "\n '$c_title',"
. "\n '$c_text',"
. "\n '$c_section',"
. "\n '$c_href',"
. "\n '$c_created',"
. "\n '$c_keywords',"
. "\n '$c_browsernav'"
. "\n ) ");
$db->query();
}
if($credit > 0 ) {
$db->setQuery('CREATE TABLE IF NOT EXISTS `#__kunena_searchplga` ('
. ' `title` text NOT NULL,'
. ' `text` text NOT NULL,'
. ' `section` text NOT NULL,'
. ' `href` text NOT NULL,'
. ' `created` text NOT NULL,'
. ' `keywords` text NOT NULL,'
. ' `browsernav` tinyint(4) NOT NULL default \'0\','
. ' UNIQUE KEY `key` (`title`(10))'
. ' ) TYPE=MyISAM'
. ' ');
$db->query();
$db->setQuery( "INSERT IGNORE INTO `#__kunena_searchplga` ("
. "\n `title`,"
. "\n `text`,"
. "\n `section`,"
. "\n `href`,"
. "\n `created`,"
. "\n `keywords`,"
. "\n `browsernav`"
. "\n ) VALUES ("
. "\n '$a_title',"
. "\n '$a_text',"
. "\n '$a_section',"
. "\n '$a_href',"
. "\n '$a_created',"
. "\n '$a_keywords',"
. "\n '$a_browsernav'"
. "\n ) ");
$db->query();
}
//Use the function trim to delete spaces in front of or at the back of the searching terms
$text = trim( $text );
//Return Array when nothing was filled in
if ($text == '') {
return array();
}
//Datebase results
$whereAs = array();
$whereBs = array();
switch ($phrase) {
case 'exact':
$whereAs2 = array();
$whereAs2[] = "LOWER(c.name) LIKE '%$text%'";
$whereAs2[] = "LOWER(c.description) LIKE '%$text%'";
$whereA = '(' . implode( ") \n OR (", $whereAs2 ) . ')';
$whereBs2 = array();
$whereBs2[] = "LOWER(b.subject) LIKE '%$text%'";
$whereBs2[] = "LOWER(a.message) LIKE '%$text%'";
$whereBs2[] = "LOWER(b.name) LIKE '%$text%'";
$whereB = '(' . implode( ") \n OR (", $whereBs2 ) . ')';
break;
case 'all':
case 'any':
default:
$words = explode( ' ', $text );
$whereAs = array();
$whereBs = array();
foreach ($words as $word) {
$whereAs2 = array();
$whereAs2[] = "LOWER(c.name) LIKE '%$word%'";
$whereAs2[] = "LOWER(c.description) LIKE '%$word%'";
$whereAs[] = implode( ' OR ', $whereAs2 );
$whereBs2 = array();
$whereBs2[] = "LOWER(b.subject) LIKE '%$word%'";
$whereBs2[] = "LOWER(a.message) LIKE '%$word%'";
$whereBs2[] = "LOWER(b.name) LIKE '%$word%'";
$whereBs[] = implode( ' OR ', $whereBs2 );
}
$whereA = '(' . implode( ($phrase == 'all' ? ") \n AND (" : ") \n OR ("), $whereAs ) . ')';
$whereB = '(' . implode( ($phrase == 'all' ? ") \n AND (" : ") \n OR ("), $whereBs ) . ')';
break;
}
switch ( $ordering ) {
case 'alpha':
$orderA = 'c.name ASC';
$orderB = 'b.subject ASC';
break;
case 'category':
$orderA = 'c.name ASC';
$orderB = 'c.name ASC, b.subject ASC';
break;
case 'popular':
$orderA = 'c.hits DESC';
$orderB = 'b.hits DESC';
break;
case 'newest':
$orderA = 'created DESC';
$orderB = 'b.time DESC';
break;
case 'oldest':
$orderA = 'created ASC';
$orderB = 'b.time ASC';
break;
default:
$orderA = 'c.name DESC';
$orderB = 'b.subject DESC';
break;
}
$searchplug_kunenasearch = JText::_( 'Plug_kunenasearch' );
// Get Kunena Item ID taken from previous searchbot
$query = "SELECT id AS ItemId"
. "\n FROM #__menu"
. "\n WHERE link = 'index.php?option=com_kunena'"
;
$db->setQuery( $query );
$com_id = $db->loadObjectList();
$Itemid = $com_id[0]->ItemId;
//Get lists for for results
if($c_result > 0){
if($c_result == 1){
$query = "SELECT a.title AS title,"
. "\n a.text AS text,"
. "\n a.created AS created,"
. "\n a.section AS section,"
. "\n a.browsernav AS browsernav,"
. "\n a.href AS href"
. "\n FROM #__kunena_searchplgc AS a"
;
}
else{
$query = "SELECT a.title AS title,"
. "\n a.text AS text,"
. "\n a.created AS created,"
. "\n a.section AS section,"
. "\n a.browsernav AS browsernav,"
. "\n a.href AS href"
. "\n FROM #__kunena_searchplgc AS a"
. "\n WHERE a.title LIKE '%$text%'"
. "\n OR a.text LIKE '%$text%'"
. "\n OR a.section LIKE '%$text%'"
. "\n OR a.keywords LIKE '%$text%'"
;
}
$db->setQuery( $query, 0, $search_limit );
$list1 = $db->loadObjectList();
}
if($credit > 0){
if($credit == 1){
$query = "SELECT a.title AS title,"
. "\n a.text AS text,"
. "\n a.created AS created,"
. "\n a.section AS section,"
. "\n a.browsernav AS browsernav,"
. "\n a.href AS href"
. "\n FROM #__kunena_searchplga AS a"
;
}
else{
$query = "SELECT a.title AS title,"
. "\n a.text AS text,"
. "\n a.created AS created,"
. "\n a.section AS section,"
. "\n a.browsernav AS browsernav,"
. "\n a.href AS href"
. "\n FROM #__kunena_searchplga AS a"
. "\n WHERE a.title LIKE '%$text%'"
. "\n OR a.text LIKE '%$text%'"
. "\n OR a.section LIKE '%$text%'"
. "\n OR a.keywords LIKE '%$text%'"
;
}
$db->setQuery( $query, 0, $search_limit );
$list2 = $db->loadObjectList();
}
//Forum Categories
$query = "SELECT c.name AS title,"
. "\n c.description AS text,"
. "\n c.hits,"
. "\n c.pub_access,"
. "\n c.published,"
. "\n '' AS created,"
. "\n '$forum_name' AS section,"
. "\n '2' AS browsernav,"
. "\n CONCAT('index.php?option=com_kunena&Itemid=', '$Itemid', '&func=showcat&catid=',c.id) AS href"
. "\n FROM $kunena_categories_dbtable AS c"
. "\n WHERE ( $whereA )"
. "\n AND c.published = 1"
. "\n AND c.pub_access <= ". (int) $user->get( 'aid' )
. "\n ORDER BY $orderA"
;
$db->setQuery( $query, 0, $search_limit );
$list3 = $db->loadObjectList();
//Forum Posts Query
$query2 = "SELECT b.subject AS title,"
. "\n a.message AS text,"
. "\n b.hits,"
. "\n c.pub_access,"
. "\n c.published,"
. "\n c.description,"
. "\n FROM_UNIXTIME(b.time) AS created,"
. "\n CONCAT('$forum_name','/', c.name) AS section,"
. "\n '2' AS browsernav,"
. "\n CONCAT('index.php?option=com_kunena&Itemid=', '$Itemid', '&func=view&catid=',b.catid,'&id=',b.thread) AS href"
. "\n FROM $kunena_messages_text_dbtable AS a"
. "\n INNER JOIN $kunena_messages_dbtable AS b ON b.id = a.mesid"
. "\n INNER JOIN $kunena_categories_dbtable AS c ON c.id = b.catid"
. "\n WHERE ( $whereB )"
. "\n AND c.published = 1"
. "\n AND c.pub_access <= ". (int) $user->get( 'aid' )
. "\n ORDER BY $orderB"
;
$db->setQuery( $query2, 0, $search_limit );
$list4 = $db->loadObjectList();
//Setting the Result Orders
if($credit > 0 and $c_result > 0){
switch ($cat_search) {
case 1:
$rows = array_merge($list1, $list2, $list3, $list4);
break;
case 2:
$rows = array_merge($list1, $list2, $list4, $list3);
break;
default:
$rows = array_merge($list1, $list2, $list4);
break;
}
}
elseif($credit == 0 and $c_result > 0){
switch ($cat_search) {
case 1:
$rows = array_merge($list1, $list3, $list4);
break;
case 2:
$rows = array_merge($list1, $list4, $list3);
break;
default:
$rows = array_merge($list1, $list4);
break;
}
}
elseif($c_result == 0 and $credit > 0){
switch ($cat_search) {
case 1:
$rows = array_merge($list2, $list3, $list4);
break;
case 2:
$rows = array_merge($list2, $list4, $list3);
break;
default:
$rows = array_merge($list2, $list4);
break;
}
}
elseif($c_result == 0 and $credit == 0){
switch ($cat_search) {
case 1:
$rows = array_merge($list3, $list4);
break;
case 2:
$rows = array_merge($list4, $list3);
break;
default:
$rows = $list4 ;
break;
}
}
// Remove BBC from the search results from old searchbot
if($allow_bbc == 0){
$BBC = '[:space:]';
$replace = ' ';
}
else{
$BBC = array( // - Convert to PLAIN TEXT
'@(\[url)([^\]]*?)(\])(.*?)(\[/url\])@si',
'@(\[img)([^\]]*?)(\])(.*?)(\[/img\])@si',
'@(\[code[^\]]*?\])(.*?)(\[/code\])@si',
'@(\[code:[0-9][^\]]*?\])(.*?)(\[/code:[0-9]\])@si',
'@(\[quote[^\]]*?\])(.*?)(\[/quote\])@si',
'@(\[b[^\]]*?\])(.*?)(\[/b\])@si',
'@(\[u[^\]]*?\])(.*?)(\[/u\])@si',
'@(\[i[^\]]*?\])(.*?)(\[/i\])@si',
'@(\[size[^\]]*?\])(.*?)(\[/size\])@si',
'@(\[color[^\]]*?\])(.*?)(\[/color\])@si',
'@(\[ol[^\]]*?\])(.*?)(\[/ol\])@si',
'@(\[ul[^\]]*?\])(.*?)(\[/ul\])@si',
'@(\[li[^\]]*?\])(.*?)(\[/li\])@si',
'@(\[video)([^\]]*?)(\])(.*?)(\[/video\])@si'
);
$replace = array(
'${4}', // URL
'[USER POSTED IMAGE]', //IMG
'${2}', // CODE
'${2}', // CODE
'${2}', // QUOTE
'${2}', // BOLD
'${2}', // UNDERLINE
'${2}', // ITALIC
'${2}', // SIZE
'${2}', // COLOR
'', // ORDERED LIST
'', // UNORDERED LIST
'${2}', // LIST ITEM
'[USER POSTED VIDEO]'//VIDEO
);
}
$rows_count = count($rows);
for($xi = 0; $xi <= $rows_count-1; $xi++){
$rows[$xi]->text = preg_replace($BBC, $replace, $rows[$xi]->text);
$rows[$xi]->text = stripslashes($rows[$xi]->text);
$rows[$xi]->title = stripslashes($rows[$xi]->title);
}
// Send the results in an array
return $rows;
}