1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2013 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 */
10
11require_once('class2.php');
12e107::coreLan('search');
13e107_require(e_HANDLER.'search_class.php');
14
15if (!check_class($pref['search_restrict']))
16{
17	require_once(HEADERF);
18	$ns->tablerender(LAN_SEARCH_20, "<div style='text-align: center'>".LAN_SEARCH_21."</div>");
19	require_once(FOOTERF);
20	exit;
21}
22
23if (isset($_GET['t']) && is_numeric($_GET['t']))
24{
25  switch ($_GET['t'])
26  {
27	case '0' :
28	  $_GET['t'] = 'news';
29	  break;
30	case 1 :
31	  $_GET['t'] = 'comments';
32	  break;
33	case  2 :
34	  $_GET['t'] = 'users';
35	  break;
36	case 3 :
37	  $_GET['t'] = 'downloads';
38	  break;
39	case 4 :
40	  $_GET['t'] = 'pages';
41	  break;
42  }
43}
44
45
46class search extends e_shortcode
47{
48
49	private $search_prefs = array();
50	private $search_info = array();
51	private $auto_order = 1000;
52	private $enhanced = false;
53	private $query = null;
54	private $doSearch = false;
55	private $result_flag = 0; // same as 'from'
56	private $message = '';
57	public 	$template = array();
58	private $enhancedTypes = array(
59								'in' => LAN_SEARCH_24,
60								'ex' => LAN_SEARCH_25,
61								'ep' => LAN_SEARCH_26,
62								'be' => LAN_SEARCH_27
63							);
64
65	function __construct()
66	{
67		$this->search_prefs = e107::getConfig('search')->getPref();
68		$this->search_info 	= $this->searchConfig();
69
70
71		if(deftrue('BOOTSTRAP'))
72		{
73			$tmp 				= e107::getCoreTemplate('search','form');
74			$SEARCH_TOP_TABLE 	= $tmp['start'];
75			$SEARCH_BOT_TABLE 	= $tmp['end'];
76			$SEARCH_CATS		= $tmp['category'];
77			$SEARCH_TYPE		= $tmp['type'];
78			$SEARCH_ADV			= $tmp['advanced'];
79			$SEARCH_ENHANCED	= $tmp['enhanced'];
80			$SEARCH_ADV_COMBO	= $tmp['advanced-combo'];
81
82			$this->template = $tmp;
83
84
85
86			unset($tmp);
87		}
88		else
89		{
90			$SEARCH_TOP_TABLE = '';
91			$SEARCH_BOT_TABLE = '';
92			$SEARCH_CATS = '';
93			$SEARCH_TYPE = '';
94			$SEARCH_ADV = '';
95			$SEARCH_ENHANCED = '';
96			$SEARCH_ADV_COMBO = '';
97
98			if (file_exists(THEME."templates/search_template.php"))
99			{
100				require(THEME."templates/search_template.php");
101			}
102			elseif (file_exists(THEME."search_template.php"))
103			{
104				require(THEME."search_template.php");
105			}
106			else
107			{
108				require(e_CORE."templates/search_template.php");
109			}
110
111			$SEARCH_TOP_TABLE .= "{SEARCH_ENHANCED}";
112
113			$tmp = array();
114
115			$tmp['start']  			= $SEARCH_TOP_TABLE ;
116			$tmp['end'] 			= $SEARCH_BOT_TABLE ;
117			$tmp['category'] 		= $SEARCH_CATS;
118			$tmp['type'] 			= $SEARCH_TYPE;
119			$tmp['advanced'] 		= $SEARCH_ADV;
120			$tmp['enhanced'] 		= $SEARCH_ENHANCED;
121			$tmp['advanced-combo'] 	= $SEARCH_ADV_COMBO;
122
123			$this->template = $tmp;
124		}
125
126
127
128
129
130		if(e_AJAX_REQUEST)
131		{
132			if(vartrue($_POST['t']))
133			{
134				echo $this->sc_search_advanced_block($_POST['t']);
135			}
136
137			exit;
138		}
139
140	}
141
142	function getPrefs()
143	{
144		return $this->search_prefs;
145	}
146
147
148	function getConfig()
149	{
150		return $this->search_info;
151	}
152
153	// Shortcodes  -----------------------
154
155	function sc_search_main($parm = '')
156	{
157		$tp = e107::getParser();
158		$value = isset($_GET['q']) ? $tp->post_toForm($_GET['q']) : "";
159
160
161
162		$text = "<div class='input-group'>
163		<input class='tbox form-control m_search' type='text' id='q' name='q' size='35' value='".$value."' maxlength='50' />
164		<div class='input-group-btn'>
165		<button class='btn btn-primary' type='submit' name='s' value='1' >".$tp->toGlyph('fa-search',false)."</button>
166		<button class='btn btn-primary dropdown-toggle' tabindex='-1' data-toggle='dropdown' type='button'>
167		";
168
169		if(BOOTSTRAP !== 4)
170		{
171			$text .= "<span class='caret'></span></button>";
172		}
173
174		$text .= '<ul class="dropdown-menu pull-right">
175          <li><a class="e-expandit" href="#" data-target="search-advanced,search-enhanced"><small>'.LAN_SEARCH_202.'</small></a></li>
176        </ul>';
177
178		$text .= "
179		</div>
180
181		</div>
182		<input type='hidden' name='r' value='0' />";
183
184		return $text;
185
186	}
187
188	function sc_search_main_searchfield($parm='')
189	{
190		$tp = e107::getParser();
191		$value = isset($_GET['q']) ? $tp->post_toForm($_GET['q']) : "";
192		return "<input class='tbox form-control m_search' type='text' id='q' name='q' size='35' value='".$value."' maxlength='50' />";
193	}
194
195	function sc_search_main_submit($parm='')
196	{
197		return "<input class='btn btn-primary button' type='submit' name='s' value='".LAN_SEARCH."' />
198		<input type='hidden' name='r' value='0' />";
199	}
200
201	function sc_enhanced_icon($parm='')
202	{
203		return  "<img src='".e_IMAGE_ABS."generic/search_basic.png' style='width: 16px; height: 16px; vertical-align: top' alt='".LAN_SEARCH_23."' title='".LAN_SEARCH_23."' onclick=\"expandit('en_in'); expandit('en_ex'); expandit('en_ep'); expandit('en_be')\"/>";
204	}
205
206	function sc_search_main_checkall($parm='')
207	{
208		 if($this->search_prefs['selector'] == 1)
209		 {
210		 	return "<input class='btn btn-default btn-secondary button' type='button' name='CheckAll' value='".LAN_SEARCH_1."' onclick='checkAll(this);' />";
211		 }
212	}
213
214	function sc_search_main_uncheckall($parm='')
215	{
216		 if($this->search_prefs['selector'] == 1)
217		 {
218		 	return "<input class='btn btn-default btn-secondary button' type='button' name='UnCheckAll' value='".LAN_SEARCH_2."' onclick='uncheckAll(this); uncheckG();' />";
219		 }
220	}
221
222	function sc_search_type_sel($parm='')
223	{
224		return e107::getForm()->radio_switch('adv', vartrue($_GET['adv']), LAN_SEARCH_30, LAN_SEARCH_29, array('class'=>'e-expandit','reverse'=>1, 'data-target'=>'search-advanced'));
225
226
227
228	//	return "<input type='radio' name='adv' value='0' ".(vartrue($_GET['adv']) ? "" : "checked='checked'")." /> ".LAN_SEARCH_29."&nbsp;
229	//	<input type='radio' name='adv' value='1' ".(vartrue($_GET['adv']) ? "checked='checked'" : "" )." /> ".LAN_SEARCH_30;
230	}
231
232	function sc_search_dropdown($parm = '')
233	{
234		return $this->selectElement('dropdown');
235	}
236
237	function sc_search_main_checkboxes($parm = '')
238	{
239		return $this->selectElement('checkboxes');
240	}
241
242	function sc_search_message($parm = '')
243	{
244		return $this->message;
245	}
246
247	function sc_search_form_url($parm='')
248	{
249		return e107::getUrl()->create('search');
250	}
251
252
253
254	// -----------------------
255
256
257
258	private function selectElement($parm)
259	{
260		// standard search config
261		$dropdown = '';
262		$PRE_CHECKBOXES = '';
263		$POST_CHECKBOXES = '';
264
265		$search_count = count($this->search_info);
266		$google_id = $search_count + 1;
267
268
269		if ($this->search_prefs['selector'] == 2)
270		{
271			$dropdown = "<select name='t' id='t' class='tbox form-control e-ajax' data-src='".e_SELF."' data-target='search-advanced' >";
272
273			if ($this->search_prefs['multisearch'])
274			{
275				$dropdown .= "<option value='all'>".LAN_SEARCH_22."</option>";
276			}
277		}
278		else
279		{
280		  $checkboxes = '';
281		}
282
283		foreach($this->search_info as $key => $value)
284		{
285			if ($this->search_prefs['selector'] == 2)
286			{
287				$sel = (isset($this->searchtype[$key]) && $this->searchtype[$key]) ? " selected='selected'" : "";
288			}
289			else
290			{
291				$sel = (isset($this->searchtype[$key]) && $this->searchtype[$key]) ? " checked='checked'" : "";
292			}
293
294			$google_js = check_class($this->search_prefs['google']) ? "onclick=\"uncheckG();\" " : "";
295
296			if ($this->search_prefs['selector'] == 2)
297			{
298				$dropdown .= "<option value='".$key."' ".$sel.">".$value['qtype']."</option>";
299			}
300			else if ($this->search_prefs['selector'] == 1)
301			{
302				$checkboxes .= $PRE_CHECKBOXES."<input ".$google_js." type='checkbox' name='t[".$key."]' ".$sel." />".$value['qtype'].$POST_CHECKBOXES;
303			}
304			else
305			{
306				$checkboxes .= $PRE_CHECKBOXES."<input type='radio' name='t' value='".$key."' ".$sel." />".$value['qtype'].$POST_CHECKBOXES;
307			}
308		}
309
310		if (check_class($this->search_prefs['google']))
311		{
312			if ($this->search_prefs['selector'] == 2)
313			{
314				$dropdown .= "<option value='".$google_id."'>Google</option>";
315			}
316			else if
317			($this->search_prefs['selector'] == 1)  //FIXME PRE_CHECKBOXES and POST_CHECKBOXES
318			{
319				$checkboxes .= $PRE_CHECKBOXES."<input id='google' type='checkbox' name='t[".$google_id."]' onclick='uncheckAll(this)' />Google".$POST_CHECKBOXES;
320			}
321			else
322			{
323				$checkboxes .= $PRE_CHECKBOXES."<input id='google' type='radio' name='t' value='".$google_id."' />Google".$POST_CHECKBOXES;
324			}
325		}
326
327		if ($this->search_prefs['selector'] == 2)
328		{
329			$dropdown .= "</select>";
330		}
331
332		if($parm == 'dropdown')
333		{
334			return $dropdown;
335		}
336		else
337		{
338			return $checkboxes;
339		}
340
341
342	}
343
344	function sc_search_enhanced()
345	{
346
347		$tp = e107::getParser();
348
349		$text = '';
350		$var = array();
351
352		foreach ($this->enhancedTypes as $en_id => $ENHANCED_TEXT)
353		{
354			$var['ENHANCED_TEXT'] 		= $ENHANCED_TEXT;
355			$var['ENHANCED_DISPLAY_ID'] = "en_".$en_id;
356			$var['ENHANCED_FIELD'] 		= "<input class='tbox form-control' type='text' id='".$en_id."' name='".$en_id."' size='35' value='".$tp->post_toForm($_GET[$en_id])."' maxlength='50' />";
357
358			$text .= $tp->simpleParse($this->template['enhanced'], $var);
359		}
360
361		return $text;
362	}
363
364
365	function sc_enhanced_display()
366	{
367		return ($this->enhanced !== true) ?  "style='display: none'" : "" ;
368	}
369
370	function sc_search_advanced($parm='')
371	{
372		$hiddenBlock = (!empty($_GET['t'])) ? "" : "class='e-hideme'";
373		$text = "<div {$hiddenBlock} id='search-advanced' >";
374		$text .= $this->sc_search_advanced_block(vartrue($_GET['t']));
375		$text .= "</div>";
376		return $text;
377
378	}
379
380	private function sc_search_advanced_block($parm='')
381	{
382		$tp = e107::getParser();
383		$sql = e107::getDb();
384		$sql2 = e107::getDb('search');
385
386
387		if(!$parm)
388		{
389			return '';
390		}
391
392		$text = '';
393
394
395		if (isset($this->search_info[$parm]['advanced']))
396		{
397
398			if(is_array($this->search_info[$parm]['advanced']))
399			{
400				$advanced  = ($this->search_info[$parm]['advanced']);
401			}
402			elseif(isset($this->search_info[$parm]['advanced']))
403			{
404				require($this->search_info[$parm]['advanced']);
405
406			}
407
408			$vars = array();
409
410
411
412			foreach ($advanced as $adv_key => $adv_value)
413			{
414				if ($adv_value['type'] == 'single')
415				{
416					$vars['SEARCH_ADV_TEXT'] = $adv_value['text'];
417					$text .= $tp->simpleParse($this->template['advanced-combo'], $vars);
418				}
419				else
420				{
421					if ($adv_value['type'] == 'dropdown')
422					{
423						$vars['SEARCH_ADV_A'] = $adv_value['text'];
424						$vars['SEARCH_ADV_B'] = "<select name='".$adv_key."' class='tbox form-control'>";
425
426						foreach ($adv_value['list'] as $list_item)
427						{
428							$vars['SEARCH_ADV_B'] .= "<option value='".$list_item['id']."' ".($_GET[$adv_key] == $list_item['id'] ? "selected='selected'" : "").">".$list_item['title']."</option>";
429						}
430						$vars['SEARCH_ADV_B'] .= "</select>";
431					}
432					else if ($adv_value['type'] == 'date')
433					{
434						$vars['SEARCH_ADV_A'] = $adv_value['text'];
435						$vars['SEARCH_ADV_B'] = "
436
437						<div class='form-inline'>
438						<select id='on' name='on' class='tbox form-control '>
439						<option value='new' ".($_GET['on'] == 'new' ? "selected='selected'" : "").">".LAN_SEARCH_34."</option>
440						<option value='old' ".($_GET['on'] == 'old' ? "selected='selected'" : "").">".LAN_SEARCH_35."</option>
441						</select>&nbsp;
442
443						<select id='time' name='time' class='tbox form-control '>";
444
445						$time = array(LAN_SEARCH_36 => 'any', LAN_SEARCH_37 => 86400, LAN_SEARCH_38 => 172800, LAN_SEARCH_39 => 259200, LAN_SEARCH_40 => 604800, LAN_SEARCH_41 => 1209600, LAN_SEARCH_42 => 1814400, LAN_SEARCH_43 => 2628000, LAN_SEARCH_44 => 5256000, LAN_SEARCH_45 => 7884000, LAN_SEARCH_46 => 15768000, LAN_SEARCH_47 => 31536000, LAN_SEARCH_48 => 63072000, LAN_SEARCH_49 => 94608000);
446
447						foreach ($time as $time_title => $time_secs)
448						{
449							$vars['SEARCH_ADV_B'] .= "<option value='".$time_secs."' ".($_GET['time'] == $time_secs ? "selected='selected'" : "").">".$time_title."</option>";
450						}
451
452						$vars['SEARCH_ADV_B'] .= "</select>
453						</div>";
454					}
455					else if ($adv_value['type'] == 'author')
456					{
457						$vars['SEARCH_ADV_A'] = $adv_value['text'];
458						$vars['SEARCH_ADV_B'] = e107::getForm()->userpicker($adv_key."_name",$adv_key,$_GET[$adv_key]);
459					}
460					else if ($adv_value['type'] == 'dual')
461					{
462						$vars['SEARCH_ADV_A'] = $adv_value['adv_a'];
463						$vars['SEARCH_ADV_B'] = $adv_value['adv_b'];
464					}
465
466					$text .= $tp->simpleParse($this->template['advanced'], $vars);
467				}
468			}
469
470
471		}
472		else
473		{
474			$_GET['adv'] = 0;
475		}
476
477
478		return $text;
479	}
480
481
482
483
484
485
486	// -------------
487
488
489
490	function searchPrefs()
491	{
492		return $this->search_prefs;
493	}
494
495
496
497	function search_info($id, $type, $plug_require=null, $info='')
498	{
499		$tp = e107::getParser();
500
501		if (check_class($this->search_prefs[$type.'_handlers'][$id]['class']))
502		{
503	//		echo "<br />type = ".$this->search_prefs[$type.'_handlers'][$id]['class'];
504
505	//		print_a($this->search_prefs);
506
507			if ($plug_require)
508			{
509				$search_info = array();
510				require_once($plug_require);
511				$ret = $search_info[0];
512			}
513			else
514			{
515				$ret = $info;
516			}
517
518			if($obj = e107::getAddon($id,'e_search'))
519			{
520				$obj->setParams($_GET);
521
522				if(!$ret = $obj->config())
523				{
524					return false;
525				}
526
527
528				$ret['qtype'] = $ret['name'];
529
530				if(!isset($ret['id']))
531				{
532					$ret['id'] = $ret['name'];
533				}
534
535				$ret['weights'] = array_values($ret['search_fields']);
536				$ret['search_fields'] = array_keys($ret['search_fields']);
537
538			}
539
540
541			$ret['chars'] 			= $this->search_prefs[$type.'_handlers'][$id]['chars'];
542			$ret['results'] 		= $this->search_prefs[$type.'_handlers'][$id]['results'];
543			$ret['pre_title'] 		= $this->search_prefs[$type.'_handlers'][$id]['pre_title'];
544			$ret['pre_title_alt'] 	= $tp -> toHTML($this->search_prefs[$type.'_handlers'][$id]['pre_title_alt']);
545		//	$ret['order'] 			= (isset($this->search_prefs[$type.'_handlers'][$id]['order']) && $this->search_prefs[$type.'_handlers'][$id]['order']) ? $this->search_prefs[$type.'_handlers'][$id]['order'] : $this->auto_order;
546
547			$this->auto_order++;
548
549			return $ret;
550		}
551		else
552		{
553			return false;
554		}
555	}
556
557
558
559
560	// Get Core and Plugin search configurations
561	function searchConfig()
562	{
563
564
565		//core search routines
566
567		$search_info = array();
568
569		/*
570		if ($search_info['news'] = $this->search_info('news', 'core', false, array('sfile' => e_HANDLER.'search/search_news.php', 'qtype' => LAN_SEARCH_98, 'refpage' => 'news.php', 'advanced' => e_HANDLER.'search/advanced_news.php', 'id' => 'news'))) {
571		   //	$search_id++;
572		} else {
573			unset($search_info['news']);
574		}
575		*/
576		if(e107::getConfig('core')->get('comments_disabled')!=1)  // Only when comments are enabled.
577		{
578			if ($search_info['comments'] = $this->search_info('comments', 'core', false, array('sfile' => e_HANDLER.'search/search_comment.php', 'qtype' => LAN_COMMENTS, 'refpage' => 'comment.php', 'advanced' => e_HANDLER.'search/advanced_comment.php', 'id' => 'comment'))) {
579			   //	$search_id++;
580			   $search_info['comments']['listorder'] = $this->search_prefs['core_handlers']['comments']['order'];
581			} else {
582				unset($search_info['comments']);
583			}
584		}
585
586		/* BELOW CODE HAS BEEN MOVED TO e107_plugins/user/e_search.php)
587		if(e107::getConfig('core')->get('user_reg')==1) // Only when user-registration is enabled.
588		{
589			if ($search_info['users'] = $this->search_info('users', 'core', false, array('sfile' => e_HANDLER.'search/search_user.php', 'qtype' => LAN_140, 'refpage' => 'user.php', 'advanced' => e_HANDLER.'search/advanced_user.php', 'id' => 'user'))) {
590				//	$search_id++;
591				$search_info['users']['listorder']  = $this->search_prefs['core_handlers']['users']['order'];
592
593			} else {
594				unset($search_info['users']);
595			}
596		}
597		*/
598
599	/*
600		if ($search_info['pages'] = $this->search_info('pages', 'core', false, array('sfile' => e_HANDLER.'search/search_pages.php', 'qtype' => LAN_418, 'refpage' => 'page.php', 'advanced' => e_HANDLER.'search/advanced_pages.php', 'id' => 'pages'))) {
601		   //	$search_id++;
602		} else {
603			unset($search_info['pages']);
604		}
605	*/
606		 $e_searchList = e107::getConfig('core')->get('e_search_list');
607
608
609		//plugin search routines    // plugin folder is used as the search key. ie. $_GET['t'] = 'chatbox';
610		foreach ($this->search_prefs['plug_handlers'] as $plug_dir => $active)
611		{
612			if(isset($search_info[$plug_dir]))
613			{
614				continue;
615			}
616
617			if (in_array($plug_dir,$e_searchList) && is_readable(e_PLUGIN.$plug_dir."/e_search.php"))
618			{
619				if ($search_info[$plug_dir] = $this->search_info($plug_dir, 'plug', e_PLUGIN.$plug_dir."/e_search.php"))
620				{
621					$search_info[$plug_dir]['listorder'] = $active['order'];
622				  //	$search_id++;
623				}
624				else
625				{
626					unset($search_info[$plug_dir]);
627				}
628
629
630			}
631
632
633		}
634
635		// order search routines
636
637
638		 $search_info = $this->array_sort($search_info, 'listorder', SORT_ASC);
639		 $this->search_info = $search_info;
640
641		if(e_DEBUG)
642		{
643	//		echo e107::getMessage()->addDebug(print_a($this->search_info,true))->render();
644		}
645
646		 return $search_info;
647	}
648
649
650
651
652	// determine areas being searched
653	public function searchType()
654	{
655		$searchtype = array();
656
657		if (!$this->search_prefs['user_select'] && $_GET['r'] < 1)
658		{
659			foreach($this->search_info as $key => $value)
660			{
661				$searchtype[$key] = true;
662			}
663		}
664		 else
665		 {
666			if (isset($_GET['t']))
667			{
668				if (is_array($_GET['t']))
669				{
670					$searchtype = $_GET['t'];
671				}
672				else
673				{
674					$searchtype[$_GET['t']] = true;
675				}
676			}
677			else
678			{
679				if (isset($_GET['ref']))
680				{
681					foreach($this->search_info as $key => $value)
682					{
683						if ($value['id'] == $_GET['ref'])
684						{
685							$searchtype[$key] = true;
686							$_GET['t'] = $key;
687						}
688					}
689				}
690				else if (e_QUERY)
691				{
692					if (isset($_SERVER['HTTP_REFERER']))
693					{
694						if (!$refpage = substr($_SERVER['HTTP_REFERER'], (strrpos($_SERVER['HTTP_REFERER'], "/")+1)))
695						{
696							$refpage = "index.php";
697						}
698					}
699					else
700					{
701						$refpage = "";
702					}
703
704					foreach($this->search_info as $key=>$value)
705					{
706						if ($value['refpage'])
707						{
708							if (strpos($refpage, $value['refpage']) !== FALSE)
709							{
710								$searchtype[$key] = true;
711								$_GET['t'] = $key;
712							}
713						}
714					}
715				}
716
717				if (!isset($this->searchtype) && isset($this->query))
718				{
719					if ($this->search_prefs['multisearch'])
720					{
721						$searchtype['all'] = true;
722					}
723					else
724					{
725						$searchtype[0] = true;
726					}
727				}
728			}
729		}
730
731		$this->searchtype = $searchtype;
732
733		return $searchtype;
734	}
735
736
737
738
739
740	function array_sort($array, $column, $order = SORT_DESC)
741	{
742		$i = 0;
743		foreach($array as $info) {
744			$sortarr[] = $info[$column];
745			$i++;
746		}
747	 	array_multisort($sortarr, $order, $array, $order);
748		return($array);
749	}
750
751
752
753
754	function renderResults()
755	{
756
757		global $query, $search_prefs, $pre_title, $search_chars, $search_res, $result_flag, $advanced_caption;
758
759		$ns = e107::getRender();
760
761		$tp = e107::getParser();
762
763		$con = e107::getDateConvert(); // BC Fix
764
765        $sch = new e_search; // BC Fix
766
767		$query = $this->query;
768
769
770		$_GET['q'] = rawurlencode($_GET['q']);
771		$_GET['t'] = preg_replace('/[^\w\-]/i', '', $_GET['t']);
772
773		$search_prefs	= $this->search_prefs;
774		$result_flag	= $this->result_flag;
775
776		foreach ($this->search_info as $key => $a)
777		{
778			if (isset($this->searchtype[$key]) || isset($this->searchtype['all']))
779			{
780
781				$text = "";
782
783				//if (file_exists($this->search_info[$key]['sfile']))
784				{
785					$pre_title 		= ($this->search_info[$key]['pre_title'] == 2) ? $this->search_info[$key]['pre_title_alt'] : $this->search_info[$key]['pre_title'];
786					$search_chars 	= $this->search_info[$key]['chars'];
787					$search_res 	= $this->search_info[$key]['results'];
788
789					if(!empty($this->search_info[$key]['sfile']) && file_exists($this->search_info[$key]['sfile'])) // Legacy
790					{
791						$text .= '<div class="search-block">';
792						require_once($this->search_info[$key]['sfile']);
793						$text .= '</div>';
794					}
795					else// New v2 standard. @see chatbox_menu/e_search.php
796					{
797
798						$className = $key."_search";
799
800						if(!class_exists($className))
801						{
802							continue;
803						}
804
805						$obj = new $className($this->query);
806
807						$where = (method_exists($obj,'where')) ? $obj->where($_GET) : "";
808
809						$ps = $obj->parsesearch($this->search_info[$key]['table'], $this->search_info[$key]['return_fields'], $this->search_info[$key]['search_fields'], $this->search_info[$key]['weights'], 'self', varset($this->search_info[$key]['no_results'],"<div class='alert alert-danger'>".LAN_198."</div>"), $where , $this->search_info[$key]['order']);
810
811						if(e_DEBUG)
812						{
813						//	echo e107::getMessage()->addDebug(print_a($this->search_info,true))->render();// "DEBUG: Order is missing";
814
815						}
816
817					//	print_a($ps);
818
819						$text .= '<ul id="search-results" class="list-unstyled search-block">';
820						$text .= $ps['text'];
821						$text .= '</ul>';
822						$results = $ps['results'];
823
824					}
825
826
827				//	$parms = $results.",".$search_res.",".$_GET['r'].",".e_REQUEST_SELF."?q=".$_GET['q']."&t=".$key."&r=[FROM]";
828
829					$nextprev = array(
830								'total'			=> $results,
831								'amount'		=> intval($search_res),
832								'current'		=> intval($_GET['r']),
833								'url'			=> urldecode(e_REQUEST_SELF."?q=".$_GET['q']."&t=".$key."&r=[FROM]"),
834							//	'caption'		=> false,
835								'tmpl_prefix'	=>'default'
836					);
837
838					$npParms = http_build_query($nextprev,false,'&');
839
840					$core_parms = array('r' => '', 'q' => '', 't' => '', 's' => '');
841					foreach ($_GET as $pparm_key => $pparm_value)
842					{
843						$temp = preg_replace('/[^\w_]/i','',$pparm_key);
844						$temp1 = preg_replace('/[^\w_ +]/i','',$pparm_value);		// Filter 'non-word' charcters in search term
845						if (($temp == $pparm_key) && !isset($core_parms[$pparm_key]))
846						{
847						//	$parms .= "&".$pparm_key."=".$temp1; //FIXME Unused
848						}
849					}
850					if ($results > $search_res)
851					{
852						$nextprev = ($results > $search_res) ? $tp -> parseTemplate("{NEXTPREV={$npParms}}") : "";
853						$text .= "<div class='nextprev search form-inline'>".$nextprev."</div>";
854					}
855					if ($results > 0)
856					{
857						$res_from = $_GET['r'] + 1;
858						$res_to = ($_GET['r'] + $search_res) > $results ? $results : ($_GET['r'] + $search_res);
859						$res_display = $res_from." - ".$res_to." ".LAN_SEARCH_12." ".$results;
860					}
861					else
862					{
863						$res_display = "";
864					}
865
866					$ns->tablerender(LAN_SEARCH_11." ".$res_display." ".LAN_SEARCH_13." ".(isset($_GET[$advanced_caption['id']]) ? $advanced_caption['title'][$_GET[$advanced_caption['id']]] : $this->search_info[$key]['qtype']), $text, 'search_result');
867				}
868
869			}
870
871		}
872
873	}
874
875
876
877	function magic_search($data)
878	{
879		foreach ($data as $key => $value) {
880			if (is_array($value)) {
881				$data[$key] = $this->magic_search($value);
882			} else {
883				$data[$key] = stripslashes($value);
884			}
885		}
886		return $data;
887	}
888
889
890	function searchQuery()
891	{
892		global $perform_search;
893		$tp = e107::getParser();
894		$sql = e107::getDb();
895
896
897		if (isset($_GET['q']) || isset($_GET['in']) || isset($_GET['ex']) || isset($_GET['ep']) || isset($_GET['beg']))
898		{
899			if (MAGIC_QUOTES_GPC == true)
900			{
901				$_GET = $this->magic_search($_GET);
902			}
903
904			$full_query = $tp->filter($_GET['q']);
905
906			if ($_GET['in'])
907			{
908				$en_in = explode(' ', $_GET['in']);
909				foreach ($en_in as $en_in_key)
910				{
911					$full_query .= " +".$tp->filter($en_in_key);
912				}
913				$this->enhanced = true;
914			}
915			if ($_GET['ex'])
916			{
917				$en_ex = explode(' ', $_GET['ex']);
918				foreach ($en_ex as $en_ex_key)
919				{
920					$full_query .= " -".$tp->filter($en_ex_key);
921				}
922				$this->enhanced = true;
923			}
924			if ($_GET['ep'])
925			{
926				$full_query .= " \"".$tp->filter($_GET['ep'])."\"";
927				$this->enhanced = true;
928			}
929			if ($_GET['be'])
930			{
931				$en_be = explode(' ', $_GET['be']);
932				foreach ($en_be as $en_be_key)
933				{
934					$full_query .= " ".$tp->filter($en_be_key)."*";
935				}
936				$this->enhanced = true;
937			}
938
939			if (isset($_GET['r']) && !is_numeric($_GET['r']))
940			{
941				$perform_search = false;
942				$this->message = LAN_SEARCH_201;
943				$this->result_flag = false;
944			}
945			else if (strlen($full_query) == 0)
946			{
947				$perform_search = false;
948				$this->message = LAN_SEARCH_201;
949			}
950			elseif (strlen($full_query) < ($char_count = ($this->search_prefs['mysql_sort'] ? 4 : 3)))
951			{
952				$perform_search = false;
953				$this->message = str_replace('[x]', $char_count, LAN_417);
954			}
955			elseif ($this->search_prefs['time_restrict'])
956			{
957				$time = time() - $this->search_prefs['time_secs'];
958				$query_check = $tp->toDB($full_query);
959				$ip = e107::getIPHandler()->getIP(FALSE);
960
961				if ($sql->select("tmp", "tmp_ip, tmp_time, tmp_info", "tmp_info LIKE 'type_search%' AND tmp_ip='".$ip."'"))
962				{
963					$row = $sql->fetch();
964					if (($row['tmp_time'] > $time) && ($row['tmp_info'] != 'type_search '.$query_check))
965					{
966						$perform_search = false;
967						$this->message = LAN_SEARCH_17.$this->search_prefs['time_secs'].LAN_SEARCH_18;
968					}
969					else
970					{
971						$sql->update("tmp", "tmp_time='".time()."', tmp_info='type_search ".$query_check."' WHERE tmp_info LIKE 'type_search%' AND tmp_ip='".$ip."'");
972					}
973				}
974				else
975				{
976					$sql->insert("tmp", "'".$ip."', '".time()."', 'type_search ".$query_check."'");
977				}
978			}
979
980
981
982			$this->query = trim($full_query);
983
984			if ($this->query)
985			{
986				$this->result_flag = intval($_GET['r']);
987				$this->doSearch = true;
988			}
989
990
991			return $this->query;
992		}
993
994
995
996	}
997
998	public function doSearch()
999	{
1000		return $this->doSearch;
1001
1002	}
1003
1004}
1005
1006
1007$srchObj 		= new search;
1008$search_info 	= $srchObj->getConfig();
1009$search_prefs	= $srchObj->getPrefs();
1010
1011$SEARCH_VARS = new e_vars();
1012
1013$query =  $srchObj->searchQuery();
1014$perform_search = $srchObj->doSearch();
1015$perform_search = true;
1016// forward user if searching in google
1017$search_count = count($search_info);
1018$google_id = $search_count + 1;
1019
1020
1021
1022
1023if ($perform_search)
1024{
1025  if ($search_prefs['selector'] == 1)
1026  {  // Care needed - with alpha strings on search of single area $_GET['t'][$google_id] returns a character on page > 1
1027	if (isset($_GET['t'][$google_id]) && ($_GET['t']==$google_id) && $_GET['t'][$google_id])
1028	{
1029//	echo "We think google should be used using checkboxes<br />";
1030		header("location:http://www.google.com/search?q=".stripslashes(str_replace(" ", "+", $query)));
1031		exit;
1032	}
1033  }
1034  else
1035  {
1036	if (isset($_GET['t']) && $_GET['t'] == $google_id)
1037	{
1038		header("location:http://www.google.com/search?q=".stripslashes(str_replace(" ", "+", $query)));
1039		exit;
1040	}
1041  }
1042}
1043
1044$searchtype = $srchObj->searchType();
1045
1046$enhanced_types['in'] = LAN_SEARCH_24.':';
1047$enhanced_types['ex'] = LAN_SEARCH_25.':';
1048$enhanced_types['ep'] = LAN_SEARCH_26.':';
1049$enhanced_types['be'] = LAN_SEARCH_27.':';
1050
1051$SEARCH_VARS->ENHANCED_DISPLAY = $enhanced ? "" : "style='display: none'";
1052
1053// advanced search config
1054if (!vartrue($_GET['adv']) || $_GET['t'] == 'all')
1055{
1056  foreach ($_GET as $gk => $gv)
1057  {
1058	if ($gk != 't' && $gk != 'q' && $gk != 'r' && $gk != 'in' && $gk != 'ex' && $gk != 'ep' && $gk != 'be' && $gk != 'adv')
1059	{
1060//	  unset($_GET[$gk]);
1061	}
1062  }
1063}
1064
1065//$SEARCH_VARS->SEARCH_TYPE_SEL = "<input type='radio' name='adv' value='0' ".(varsettrue($_GET['adv']) ? "" : "checked='checked'")." /> ".LAN_SEARCH_29."&nbsp;
1066//<input type='radio' name='adv' value='1' ".(varsettrue($_GET['adv']) ? "checked='checked'" : "" )." /> ".LAN_SEARCH_30;
1067
1068$js_adv = '';
1069foreach ($search_info as $key => $value)
1070{
1071  if (!isset($search_info[$key]['advanced']))
1072  {
1073	$js_adv .= " && abid != '".$key."'";
1074  }
1075}
1076
1077if (isset($_GET['t']) && isset($search_info[$_GET['t']]['advanced']))
1078{
1079  $SEARCH_VARS->SEARCH_TYPE_DISPLAY = "";
1080}
1081else
1082{
1083  $SEARCH_VARS->SEARCH_TYPE_DISPLAY = "style='display: none'";
1084}
1085
1086if (check_class($search_prefs['google'])) {
1087	$js_adv .= " && abid != '".$google_id."'";
1088}
1089
1090
1091if ($perform_search)
1092{
1093  $con = e107::getDate();
1094
1095  $sch = new e_search;
1096
1097  // omitted words message
1098  $stop_count = count($sch -> stop_keys);
1099
1100  if ($stop_count)
1101  {
1102	if ($stop_count > 1)
1103	{
1104	  $SEARCH_VARS->SEARCH_MESSAGE = LAN_SEARCH_32.": ";
1105	}
1106	else
1107	{
1108	  $SEARCH_VARS->SEARCH_MESSAGE = LAN_SEARCH_33.": ";
1109	}
1110
1111	$i = 1;
1112	foreach ($sch -> stop_keys as $stop_key)
1113	{
1114	  $SEARCH_VARS->SEARCH_MESSAGE .= $stop_key;
1115	  if ($i != $stop_count)
1116	  {
1117		$SEARCH_VARS->SEARCH_MESSAGE .= ', ';
1118	  }
1119	  $i++;
1120	}
1121  }
1122}
1123
1124require_once(HEADERF);
1125
1126// render search config
1127
1128if(deftrue('BOOTSTRAP'))
1129{
1130	$tmp 				= e107::getCoreTemplate('search','form');
1131	$SEARCH_TOP_TABLE 	= $tmp['start'];
1132	$SEARCH_BOT_TABLE 	= $tmp['end'];
1133	$SEARCH_CATS		= $tmp['category'];
1134	$SEARCH_TYPE		= $tmp['type'];
1135	$SEARCH_ADV			= $tmp['advanced'];
1136	$SEARCH_ENHANCED	= $tmp['enhanced'];
1137	$SEARCH_ADV_COMBO	= $tmp['advanced-combo'];
1138
1139	$srchObj->template = $tmp;
1140	unset($tmp);
1141}
1142else
1143{
1144	if (file_exists(THEME."templates/search_template.php"))
1145	{
1146		require(THEME."templates/search_template.php");
1147	}
1148	elseif (file_exists(THEME."search_template.php"))
1149	{
1150		require(THEME."search_template.php");
1151	}
1152	else
1153	{
1154		require(e_CORE."templates/search_template.php");
1155	}
1156
1157	$SEARCH_TOP_TABLE .= "{SEARCH_ENHANCED}";
1158}
1159
1160
1161$tp = e107::getParser();
1162$text =  $tp->parseTemplate($SEARCH_TOP_TABLE,true,$srchObj);
1163
1164if ($search_prefs['user_select'])
1165{
1166	$text .= $tp->parseTemplate($SEARCH_CATS,true, $srchObj);
1167}
1168
1169// $text .= $tp->parseTemplate($SEARCH_TYPE,true, $srchObj);
1170/*
1171$hiddenBlock = (!empty($_GET['t'])) ? "" : "class='e-hideme'";
1172$text .= "<div {$hiddenBlock} id='search-advanced' >";
1173$text .= $tp->parseTemplate("{SEARCH_ADVANCED_BLOCK=".vartrue($_GET['t'])."}",true, $srchObj);
1174$text .= "</div>";*/
1175
1176	//print_a($search_prefs);
1177//$
1178
1179// $text .= $SEARCH_MESSAGE ? preg_replace("/\{(.*?)\}/e", '$\1', $SEARCH_TABLE_MSG) : "";
1180$text .= $SEARCH_VARS->SEARCH_MESSAGE ? $tp->simpleParse($SEARCH_TABLE_MSG, $SEARCH_VARS) : "";
1181//$text .= preg_replace("/\{(.*?)\}/e", '$\1', $SEARCH_BOT_TABLE);
1182$text .= $tp->simpleParse($SEARCH_BOT_TABLE, $SEARCH_VARS);
1183
1184if(!isset($_GET['nf'])) // no form flag.
1185{
1186	e107::getRender()->tablerender(PAGE_NAME." ".SITENAME, $text, 'search_head');
1187}
1188
1189// parse search
1190$SEARCH_VARS = new e_vars();
1191
1192if ($perform_search)
1193{
1194	$srchObj->renderResults();
1195}
1196
1197
1198
1199
1200
1201// old 6xx search parser for reverse compatability
1202function parsesearch($text, $match)
1203{
1204	$tp = e107::getParser();
1205	$text = strip_tags($text);
1206	$temp = $tp->ustristr($text, $match);
1207	$pos = $tp->ustrlen($text) - $tp->ustrlen($temp);
1208	$matchedText = $tp->usubstr($text,$pos,$tp->ustrlen($match));
1209	if ($pos < 70) {
1210		$text = "...".$tp->usubstr($text, 0, 100)."...";
1211	} else {
1212		$text = "...".$tp->usubstr($text, ($pos-50), $pos+30)."...";
1213	}
1214	$text = preg_replace("/".$match."/i", "<span class='searchhighlight'>".$matchedText."</span>", $text);
1215	return($text);
1216}
1217
1218
1219function headerjs() {
1220	global $search_count, $google_id, $search_prefs, $js_adv, $search_info;
1221	if ($search_prefs['selector'] == 1) {
1222
1223		$types = array_keys($search_info);
1224		$types = implode("', '", $types);
1225
1226		$script = "<script type='text/javascript'>
1227		<!--
1228		var i;
1229		var stypes = new Array('".$types."');
1230
1231		function checkAll(allbox) {
1232			for (var i in stypes)
1233			document.getElementById('searchform')[\"t[\" + stypes[i] + \"]\"].checked = true ;
1234			uncheckG();
1235		}
1236
1237		function uncheckAll(allbox) {
1238			for (var i in stypes)
1239			document.getElementById('searchform')[\"t[\" + stypes[i] + \"]\"].checked = false ;
1240		}\n";
1241
1242		if (check_class($search_prefs['google'])) {
1243		$script .= "
1244		function uncheckG() {
1245			document.getElementById('searchform')[\"t[".$google_id."]\"].checked = false ;
1246		}\n";
1247		}
1248
1249		$script .= "// -->
1250		</script>";
1251
1252	}
1253
1254
1255
1256	return $script;
1257}
1258
1259require_once(FOOTERF);
1260
1261?>