PageRenderTime 306ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ASTRA_Demo_Server/udrive/www/astra/interact/search.php

https://github.com/shafiqissani/ASTRA-College-Website
PHP | 425 lines | 178 code | 92 blank | 155 comment | 23 complexity | 82b89474588ab62f981664f80ffc33dd MD5 | raw file
  1. <?php
  2. /**
  3. * Search
  4. *
  5. * Provides a search function across all spaces
  6. */
  7. /**
  8. * Include main config file
  9. */
  10. require_once('local/config.inc.php');
  11. //get language strings
  12. require_once($CONFIG['LANGUAGE_CPATH'].'/space_strings.inc.php');
  13. $search_terms = $_GET['search_terms'];
  14. $search_terms_raw = strip_tags($_GET['search_terms']);
  15. $search_terms_highlight = '<strong><em>'.interact_stripslashes($search_terms_raw).'</em></strong>';
  16. $rule = $_GET['rule'];
  17. $current_user_key = $_SESSION['current_user_key'];
  18. //check to see if user is logged in. If not refer to Login page.
  19. authenticate_home();
  20. if (!is_object($objDates)) {
  21. if (!class_exists('InteractDate')) {
  22. require_once('includes/lib/date.inc.php');
  23. }
  24. $objDates = new InteractDate();
  25. }
  26. require_once($CONFIG['TEMPLATE_CLASS_PATH'].'/template.inc');
  27. $t = new Template($CONFIG['TEMPLATES_PATH']);
  28. $t->set_file(array(
  29. 'header' => 'header.ihtml',
  30. 'navigation' => 'navigation.ihtml',
  31. 'body' => 'search.ihtml',
  32. 'footer' => 'footer.ihtml'
  33. ));
  34. $space_key=get_space_key();
  35. $page_details = get_page_details($space_key,'');
  36. set_common_template_vars($space_key,$module_key,$page_details, $message, $accesslevel_key, $group_accesslevel);
  37. $t->set_var('PAGE_TITLE','Search');
  38. $t->set_var('BREADCRUMBS','');
  39. $t->set_block('navigation', 'ModuleHeadingBlock', 'ModuleHeadBlock');
  40. $t->set_var('SEARCH_HEADING', sprintf($space_strings['search_heading2'], $general_strings['space_text']));
  41. $t->set_var('YOUR_LINKS_STRING', sprintf($general_strings['your_links'], $general_strings['space_plural']));
  42. $t->set_var('SPACE_STRING', $general_strings['space_plural']);
  43. $t->set_var('SEARCH_STRING', $general_strings['search']);
  44. $t->set_var('ALL_WORDS_STRING', $general_strings['all_words']);
  45. $t->set_var('ANY_WORDS_STRING', $general_strings['any_words']);
  46. $t->set_var('EXACT_PHRASE_STRING', $general_strings['exact_phrase']);
  47. $t->set_var('SITE_CONTENT_STRING', $general_strings['site_content']);
  48. $t->set_var('FORUM_POSTINGS_STRING', $general_strings['forum_postings']);
  49. $t->parse('CONTENTS', 'header', true);
  50. get_navigation();
  51. if (!$search_terms_raw) {
  52. $t->set_block('body', 'ResultsBlock', 'RBlock');
  53. $t->set_var('RBlock', '');
  54. } else {
  55. if ($search_terms_raw!='') {
  56. //find out what groups user is a member of
  57. $groups_sql='(';
  58. $n=1;
  59. $sql = "select group_key from {$CONFIG['DB_PREFIX']}group_user_links where user_key='$current_user_key'";
  60. $rs = $CONN->Execute($sql);
  61. if ($rs->EOF) {
  62. $groups_sql.='-1)';
  63. } else {
  64. $record_count=$rs->RecordCount();
  65. while (!$rs->EOF) {
  66. $current_row=$rs->CurrentRow();
  67. $group_key = $rs->fields[0];
  68. $group_access[$n]=$group_key;
  69. if(++$current_row==$record_count) {
  70. $groups_sql.="$group_key ";
  71. } else {
  72. $groups_sql.="$group_key, ";
  73. }
  74. $n++;
  75. $rs->MoveNext();
  76. }
  77. $rs->Close();
  78. $groups_sql.=')';
  79. }
  80. //$space_search_string = create_space_search_string($search_terms_raw, $rule);
  81. $sql = "SELECT DISTINCT {$CONFIG['DB_PREFIX']}spaces.space_key,name FROM {$CONFIG['DB_PREFIX']}spaces, {$CONFIG['DB_PREFIX']}module_space_links WHERE {$CONFIG['DB_PREFIX']}spaces.module_key={$CONFIG['DB_PREFIX']}module_space_links.module_key AND {$CONFIG['DB_PREFIX']}module_space_links.status_key='1' AND {$CONFIG['DB_PREFIX']}spaces.type_key!='1' AND MATCH(name,short_name,code,description) AGAINST('$search_terms_raw') ORDER BY {$CONFIG['DB_PREFIX']}spaces.name";
  82. $rs = $CONN->Execute($sql);
  83. echo $CONN->ErrorMsg();
  84. if ($rs->EOF) {
  85. $t->set_block('body', 'SpaceBlock', 'SBlock');
  86. $t->set_var('SBlock', sprintf($space_strings['space_search_fail'],$general_strings['space_text'],$search_terms_highlight));
  87. } else {
  88. $t->set_block('body', 'SpaceBlock', 'SBlock');
  89. $t->set_var('MESSAGE',sprintf($space_strings['search_results'],$search_terms_highlight));
  90. $number=1;
  91. while (!$rs->EOF) {
  92. $space_key = $rs->fields[0];
  93. $space_name=$rs->fields[1];
  94. $t->set_var('SPACE_KEY',$space_key);
  95. $t->set_var('SPACE_NAME',$space_name);
  96. $t->set_var('NUMBER',$number);
  97. $t->Parse('SBlock', 'SpaceBlock', true);
  98. $number++;
  99. $rs->MoveNext();
  100. }
  101. $rs->Close();
  102. }
  103. //if not superadmin limit search to own or open spaces
  104. $content_limit = ($_SESSION['userlevel_key']!=1)?" AND ({$CONFIG['DB_PREFIX']}space_user_links.user_key='$current_user_key' OR {$CONFIG['DB_PREFIX']}spaces.access_level_key='1' OR {$CONFIG['DB_PREFIX']}spaces.access_level_key='3') AND ({$CONFIG['DB_PREFIX']}modules.status_key='1' OR {$CONFIG['DB_PREFIX']}modules.status_key='3') AND (group_key='0' OR group_key in $groups_sql) ":'';
  105. //$content_search_string = create_content_search_string($search_terms, $rule);
  106. $sql = "SELECT DISTINCT {$CONFIG['DB_PREFIX']}modules.module_key,{$CONFIG['DB_PREFIX']}module_space_links.group_key,{$CONFIG['DB_PREFIX']}modules.name, {$CONFIG['DB_PREFIX']}modules.type_code, {$CONFIG['DB_PREFIX']}modules.description,{$CONFIG['DB_PREFIX']}module_space_links.space_key,{$CONFIG['DB_PREFIX']}module_space_links.link_key FROM {$CONFIG['DB_PREFIX']}modules, {$CONFIG['DB_PREFIX']}module_space_links,{$CONFIG['DB_PREFIX']}spaces,{$CONFIG['DB_PREFIX']}space_user_links WHERE {$CONFIG['DB_PREFIX']}module_space_links.space_key={$CONFIG['DB_PREFIX']}space_user_links.space_key AND {$CONFIG['DB_PREFIX']}space_user_links.space_key={$CONFIG['DB_PREFIX']}spaces.space_key AND {$CONFIG['DB_PREFIX']}modules.module_key={$CONFIG['DB_PREFIX']}module_space_links.module_key AND {$CONFIG['DB_PREFIX']}modules.type_code!='space' AND {$CONFIG['DB_PREFIX']}modules.type_code!='heading' AND {$CONFIG['DB_PREFIX']}spaces.type_key!='1' $content_limit AND MATCH({$CONFIG['DB_PREFIX']}modules.name,{$CONFIG['DB_PREFIX']}modules.description) AGAINST('$search_terms_raw') AND ({$CONFIG['DB_PREFIX']}module_space_links.status_key!='2') ";
  107. $rs = $CONN->Execute($sql);
  108. if ($rs->EOF) {
  109. $t->set_block('body', 'ContentBlock', 'CBlock');
  110. $t->set_var('CBlock', sprintf($space_strings['content_search_fail'],$search_terms_highlight));
  111. } else {
  112. $t->set_block('body', 'ContentBlock', 'CBlock');
  113. $t->set_var(MESSAGE,sprintf($space_strings['search_results'],$search_terms_highlight));
  114. $number=1;
  115. while (!$rs->EOF) {
  116. $module_key = $rs->fields[0];
  117. $group_key = $rs->fields[1];
  118. $name = $rs->fields[2];
  119. $url = $rs->fields[3];
  120. if ($rs->fields[4]=='') {
  121. $description = $rs->fields[4];
  122. } else {
  123. $description = ' - '.$rs->fields[4];
  124. }
  125. $space_key = $rs->fields[5];
  126. $link_key = $rs->fields[6];
  127. $t->set_var('GROUP_KEY',$group_key);
  128. $t->set_var('MODULE_KEY',$module_key);
  129. $t->set_var('LINK_KEY',$link_key);
  130. $t->set_var('MODULE_NAME',$name);
  131. $t->set_var('CODE',$url);
  132. $t->set_var('PATH',$CONFIG['PATH']);
  133. $t->set_var('SPACE_KEY',$space_key);
  134. $t->set_var('DESCRIPTION',$description);
  135. $t->set_var('NUMBER',$number);
  136. $t->Parse('CBlock', 'ContentBlock', true);
  137. $number++;
  138. $rs->MoveNext();
  139. }
  140. $rs->Close();
  141. }
  142. //now search posts
  143. $postmodules="{$CONFIG['DB_PREFIX']}modules.type_code='forum'";
  144. if(!empty($CONFIG['SEARCH_ALL_JOURNALS'])){
  145. $postmodules="($postmodules OR {$CONFIG['DB_PREFIX']}modules.type_code='journal')";
  146. }
  147. //$forum_search_string = create_forum_search_string($search_terms, $rule);
  148. $sql = "SELECT DISTINCT {$CONFIG['DB_PREFIX']}posts.module_key,{$CONFIG['DB_PREFIX']}module_space_links.group_key,{$CONFIG['DB_PREFIX']}posts.post_key,{$CONFIG['DB_PREFIX']}posts.thread_key,{$CONFIG['DB_PREFIX']}posts.subject,first_name,last_name,{$CONFIG['DB_PREFIX']}posts.date_added,{$CONFIG['DB_PREFIX']}module_space_links.space_key FROM {$CONFIG['DB_PREFIX']}module_space_links, {$CONFIG['DB_PREFIX']}posts,{$CONFIG['DB_PREFIX']}users,{$CONFIG['DB_PREFIX']}spaces,{$CONFIG['DB_PREFIX']}space_user_links, {$CONFIG['DB_PREFIX']}modules WHERE {$CONFIG['DB_PREFIX']}module_space_links.module_key={$CONFIG['DB_PREFIX']}posts.module_key AND {$CONFIG['DB_PREFIX']}posts.added_by_key={$CONFIG['DB_PREFIX']}users.user_key AND {$CONFIG['DB_PREFIX']}module_space_links.space_key={$CONFIG['DB_PREFIX']}space_user_links.space_key AND {$CONFIG['DB_PREFIX']}space_user_links.space_key={$CONFIG['DB_PREFIX']}spaces.space_key AND {$CONFIG['DB_PREFIX']}module_space_links.module_key={$CONFIG['DB_PREFIX']}modules.module_key AND $postmodules AND ({$CONFIG['DB_PREFIX']}space_user_links.user_key='$current_user_key' OR {$CONFIG['DB_PREFIX']}spaces.access_level_key='1' OR {$CONFIG['DB_PREFIX']}spaces.access_level_key='3') AND ({$CONFIG['DB_PREFIX']}module_space_links.group_key='0' OR {$CONFIG['DB_PREFIX']}module_space_links.group_key in $groups_sql) AND MATCH(subject,body) AGAINST('$search_terms_raw') AND ({$CONFIG['DB_PREFIX']}module_space_links.status_key!='2')";
  149. $rs = $CONN->Execute($sql);
  150. echo $CONN->ErrorMsg();
  151. if ($rs->EOF) {
  152. $t->set_block('body', 'ForumBlock', 'FBlock');
  153. $t->set_var('FBlock', sprintf($space_strings['forum_search_fail'],$search_terms_highlight));
  154. } else {
  155. $t->set_block('body', 'ForumBlock', 'FBlock');
  156. $t->set_var('MESSAGE',sprintf($space_strings['search_results'],$search_terms_highlight));
  157. $number=1;
  158. while (!$rs->EOF) {
  159. $module_key = $rs->fields[0];
  160. $group_key = $rs->fields[1];
  161. $post_key = $rs->fields[2];
  162. $thread_key = $rs->fields[3];
  163. $subject = $rs->fields[4];
  164. $added_by = $rs->fields[5].' '.$rs->fields[6];
  165. $date_added = $objDates->formatDate($CONN->UnixTimeStamp($rs->fields[7]),'short');
  166. $space_key = $rs->fields[8];
  167. $t->set_var('GROUP_KEY',$group_key);
  168. $t->set_var('MODULE_KEY',$module_key);
  169. $t->set_var('POST_KEY',$post_key);
  170. $t->set_var('THREAD_KEY',$thread_key);
  171. $t->set_var('SPACE_KEY',$space_key);
  172. $t->set_var('PATH',$CONFIG['PATH']);
  173. $t->set_var('SUBJECT',$subject);
  174. $t->set_var('NUMBER',$number);
  175. $t->set_var('ADDED_BY',$added_by);
  176. $t->set_var('DATE_ADDED',$date_added);
  177. $t->Parse('FBlock', 'ForumBlock', true);
  178. $number++;
  179. $rs->MoveNext();
  180. }
  181. $rs->Close();
  182. }
  183. } else {
  184. $t->set_block('body', 'ResultsBlock', 'RBlock');
  185. $t->set_var('RBlock', '');
  186. $t->set_var('MESSAGE', $space_strings['no_search_terms']);
  187. }
  188. }
  189. $t->parse('CONTENTS', 'body', true);
  190. $t->parse('CONTENTS', 'footer', true);
  191. $t->p('CONTENTS');
  192. $CONN->Close();
  193. exit;
  194. /*
  195. function create_content_search_string($search_terms, $rule) {
  196. global $CONFIG;
  197. // Split up $keywords by the delimiter (" ")
  198. $arg = split(' ', $search_terms);
  199. if ($rule == 'all') {
  200. $joiner = 'AND';
  201. } elseif ($rule == 'any') {
  202. $joiner = 'OR';
  203. }
  204. if ($rule != 'exact') {
  205. for($i=0; $i<count($arg); $i++) {
  206. if ($i==0) {
  207. $cond = "(({$CONFIG['DB_PREFIX']}modules.name LIKE '%$arg[$i]%') OR ".
  208. "({$CONFIG['DB_PREFIX']}modules.description LIKE '%$arg[$i]%'))";
  209. } else {
  210. $cond = "$cond $joiner (({$CONFIG['DB_PREFIX']}modules.name LIKE '%$arg[$i]%') OR ".
  211. "({$CONFIG['DB_PREFIX']}modules.description LIKE '%$arg[$i]%'))";
  212. }
  213. }
  214. } else {
  215. $cond = "(({$CONFIG['DB_PREFIX']}modules.name LIKE '%$search_terms%') OR ".
  216. "({$CONFIG['DB_PREFIX']}modules.description LIKE '$search_terms%'))";
  217. }
  218. return $cond;
  219. } // end function create_content_search_string
  220. function create_space_search_string($search_terms, $rule) {
  221. global $CONFIG;
  222. // Split up $keywords by the delimiter (" ")
  223. $arg = split(' ', $search_terms);
  224. if ($rule == 'all') {
  225. $joiner = 'AND';
  226. } elseif ($rule == 'any') {
  227. $joiner = 'OR';
  228. }
  229. if ($rule != 'exact') {
  230. for($i=0; $i<count($arg); $i++) {
  231. if ($i==0) {
  232. $cond = "(({$CONFIG['DB_PREFIX']}spaces.name LIKE '%$arg[$i]%' OR {$CONFIG['DB_PREFIX']}spaces.code LIKE '%$arg[$i]%') OR ".
  233. "({$CONFIG['DB_PREFIX']}spaces.description LIKE '%$arg[$i]%'))";
  234. } else {
  235. $cond = "$cond $joiner (({$CONFIG['DB_PREFIX']}spaces.name LIKE '%$arg[$i]%' OR {$CONFIG['DB_PREFIX']}spaces.code LIKE '%$arg[$i]%') OR ".
  236. "({$CONFIG['DB_PREFIX']}spaces.description LIKE '%$arg[$i]%'))";
  237. }
  238. }
  239. } else {
  240. $cond = "(({$CONFIG['DB_PREFIX']}spaces.name LIKE '%$search_terms%' OR {$CONFIG['DB_PREFIX']}spaces.code LIKE '%$arg[$i]%') OR ".
  241. "({$CONFIG['DB_PREFIX']}spaces.description LIKE '$search_terms%'))";
  242. }
  243. return $cond;
  244. } // end function create_space_search_string
  245. function create_forum_search_string($search_terms, $rule) {
  246. global $CONFIG;
  247. // Split up $keywords by the delimiter (" ")
  248. $arg = split(' ', $search_terms);
  249. if ($rule == 'all') {
  250. $joiner = 'AND';
  251. } elseif ($rule == 'any') {
  252. $joiner = 'OR';
  253. }
  254. if ($rule != 'exact') {
  255. for($i=0; $i<count($arg); $i++) {
  256. if ($i==0) {
  257. $cond = "(({$CONFIG['DB_PREFIX']}posts.subject LIKE '%$arg[$i]%') OR ".
  258. "({$CONFIG['DB_PREFIX']}posts.body LIKE '%$arg[$i]%'))";
  259. } else {
  260. $cond = "$cond $joiner (({$CONFIG['DB_PREFIX']}posts.subject LIKE '%$arg[$i]%') OR ".
  261. "({$CONFIG['DB_PREFIX']}posts.body LIKE '%$arg[$i]%'))";
  262. }
  263. }
  264. } else {
  265. $cond = "(({$CONFIG['DB_PREFIX']}posts.subject LIKE '%$search_terms%') OR ".
  266. "({$CONFIG['DB_PREFIX']}posts.body LIKE '$search_terms%'))";
  267. }
  268. return $cond;
  269. } // end function create_forum_search_string
  270. */
  271. ?>