PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/class.search.php

https://gitlab.com/vectorci/Collabtive
PHP | 352 lines | 306 code | 35 blank | 11 comment | 35 complexity | ef70a50b73d44703c08484dc8c31722a MD5 | raw file
  1. <?php
  2. /**
  3. * This class provides methods for searching content
  4. *
  5. * @author Open Dynamics <info@o-dyn.de>
  6. * @name search
  7. * @version 0.4.6
  8. * @package Collabtive
  9. * @link http://www.o-dyn.de
  10. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
  11. */
  12. class search
  13. {
  14. function __construct()
  15. {
  16. }
  17. function dosearch($query, $project = 0)
  18. {
  19. if (empty($query))
  20. {
  21. return false;
  22. }
  23. if ($project == 0)
  24. {
  25. $projects = $this->searchProjects($query);
  26. $milestones = $this->searchMilestones($query);
  27. if ($_SESSION["adminstate"] > 0)
  28. {
  29. $messages = $this->searchMessage($query);
  30. }
  31. else
  32. {
  33. $messages = array();
  34. }
  35. $tasks = $this->searchTasks($query);
  36. $files = $this->searchFiles($query);
  37. $user = $this->searchUser($query);
  38. $result = array_merge($projects, $milestones, $tasks, $messages , $files, $user);
  39. }
  40. else
  41. {
  42. $milestones = $this->searchMilestones($query, $project);
  43. if ($_SESSION["adminstate"] > 0)
  44. {
  45. $messages = $this->searchMessage($query,$project);
  46. }
  47. else
  48. {
  49. $messages = array();
  50. }
  51. $tasks = $this->searchTasks($query, $project);
  52. $files = $this->searchFiles($query, $project);
  53. $user = $this->searchUser($query, $project);
  54. $result = array_merge($milestones, $tasks, $messages , $files, $user);
  55. }
  56. if (!empty($result))
  57. {
  58. return $result;
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. function searchProjects($query)
  66. {
  67. $query = mysql_real_escape_string($query);
  68. $sel = mysql_query("SELECT `ID`,`name`,`desc`,`status` FROM projekte WHERE `name` LIKE '%$query%' OR `desc` LIKE '%$query%' OR ID = '$query' HAVING status=1");
  69. $projects = array();
  70. while ($result = mysql_fetch_array($sel))
  71. {
  72. if (!empty($result))
  73. {
  74. $result["type"] = "project";
  75. $result["icon"] = "projects.png";
  76. $result["name"] = stripslashes($result["name"]);
  77. $result["desc"] = stripslashes($result["desc"]);
  78. $result["url"] = "manageproject.php?action=showproject&amp;id=$result[ID]";
  79. array_push($projects, $result);
  80. }
  81. }
  82. if (!empty($projects))
  83. {
  84. return $projects;
  85. }
  86. else
  87. {
  88. return array();
  89. }
  90. }
  91. function searchMilestones($query, $project = 0)
  92. {
  93. $query = mysql_real_escape_string($query);
  94. $project = (int) $project;
  95. if ($project > 0)
  96. {
  97. $sel = mysql_query("SELECT `ID`,`name`,`desc`,`status`,`project` FROM milestones WHERE `name` LIKE '%$query%' OR `desc` LIKE '%$query%' HAVING project = $project AND status=1 ");
  98. }
  99. else
  100. {
  101. $sel = mysql_query("SELECT `ID`,`name`,`desc`,`status`,`project` FROM milestones WHERE `name` LIKE '%$query%' OR `desc` LIKE '%$query%' HAVING status=1");
  102. }
  103. $milestones = array();
  104. while ($result = mysql_fetch_array($sel))
  105. {
  106. if (!empty($result))
  107. {
  108. $project = mysql_query("SELECT name FROM projekte WHERE ID = $result[project]");
  109. $project = mysql_fetch_row($project);
  110. $project = $project[0];
  111. $result["pname"] = $project;
  112. $result["type"] = "milestone";
  113. $result["icon"] = "miles.png";
  114. $result["name"] = stripslashes($result["name"]);
  115. $result["desc"] = stripslashes($result["desc"]);
  116. $result["url"] = "managemilestone.php?action=showmilestone&amp;msid=$result[ID]&id=$result[project]";
  117. array_push($milestones, $result);
  118. }
  119. }
  120. if (!empty($milestones))
  121. {
  122. return $milestones;
  123. }
  124. else
  125. {
  126. return array();
  127. }
  128. }
  129. function searchMessage($query, $project = 0)
  130. {
  131. $query = mysql_real_escape_string($query);
  132. $project = (int) $project;
  133. if ($project > 0)
  134. {
  135. $sel = mysql_query("SELECT `ID`,`title`,`text`,`posted`,`user`,`username`,`project` FROM messages WHERE `title` LIKE '%$query%' OR `text` LIKE '%$query%' HAVING project = $project ");
  136. }
  137. else
  138. {
  139. $sel = mysql_query("SELECT `ID`,`title`,`text`,`posted`,`user`,`username`,`project` FROM messages WHERE `title` LIKE '%$query%' OR `text` LIKE '%$query%'");
  140. }
  141. $messages = array();
  142. while ($result = mysql_fetch_array($sel))
  143. {
  144. if (!empty($result))
  145. {
  146. $project = mysql_query("SELECT name FROM projekte WHERE ID = $result[project]");
  147. $project = mysql_fetch_row($project);
  148. $project = $project[0];
  149. $result["pname"] = $project;
  150. $result["type"] = "message";
  151. $result["icon"] = "msgs.png";
  152. $result["title"] = stripslashes($result["title"]);
  153. $result["text"] = stripslashes($result["text"]);
  154. $result["username"] = stripslashes($result["username"]);
  155. $posted = date("d.m.y - H:i", $result["posted"]);
  156. $result["endstring"] = $posted;
  157. $result["url"] = "managemessage.php?action=showmessage&amp;mid=$result[ID]&id=$result[project]";
  158. array_push($messages, $result);
  159. }
  160. }
  161. if (!empty($messages))
  162. {
  163. return $messages;
  164. }
  165. else
  166. {
  167. return array();
  168. }
  169. }
  170. function searchTasks($query, $project = 0)
  171. {
  172. $query = mysql_real_escape_string($query);
  173. $project = (int) $project;
  174. if ($project > 0)
  175. {
  176. $sel = mysql_query("SELECT `ID`,`title`,`text`,`status`,`project` FROM tasks WHERE `title` LIKE '%$query%' OR `text` LIKE '%$query%' HAVING project = $project AND status=1");
  177. }
  178. else
  179. {
  180. $sel = mysql_query("SELECT `ID`,`title`,`text`,`status`,`project` FROM tasks WHERE `title` LIKE '%$query%' OR `text` LIKE '%$query%' HAVING status=1");
  181. }
  182. $tasks = array();
  183. while ($result = mysql_fetch_array($sel))
  184. {
  185. if (!empty($result))
  186. {
  187. $project = mysql_query("SELECT name FROM projekte WHERE ID = $result[project]");
  188. $project = mysql_fetch_row($project);
  189. $project = $project[0];
  190. $result["pname"] = $project;
  191. $result["type"] = "task";
  192. $result["icon"] = "task.png";
  193. $result["title"] = stripslashes($result["title"]);
  194. $result["text"] = stripslashes($result["text"]);
  195. $result["url"] = "managetask.php?action=showtask&amp;tid=$result[ID]&id=$result[project]";
  196. array_push($tasks, $result);
  197. }
  198. }
  199. if (!empty($tasks))
  200. {
  201. return $tasks;
  202. }
  203. else
  204. {
  205. return array();
  206. }
  207. }
  208. function searchFiles($query, $project = 0)
  209. {
  210. $query = mysql_real_escape_string($query);
  211. $project = (int) $project;
  212. if ($project > 0)
  213. {
  214. $sel = mysql_query("SELECT `ID`,`name`,`desc`,`type`,`datei`,`title`,`project` FROM `files` WHERE `name` LIKE '%$query%' OR `desc` LIKE '%$query%' OR `title` LIKE '%$query%' HAVING project = $project");
  215. }
  216. else
  217. {
  218. $sel = mysql_query("SELECT `ID`,`name`,`desc`,`type`,`datei`,`title`,`project` FROM `files` WHERE `name` LIKE '%$query%' OR `desc` LIKE '%$query%' OR `title` LIKE '%$query%'");
  219. }
  220. $files = array();
  221. while ($result = mysql_fetch_array($sel))
  222. {
  223. if (!empty($result))
  224. {
  225. $project = mysql_query("SELECT name FROM projekte WHERE ID = $result[project]");
  226. $project = mysql_fetch_row($project);
  227. $project = $project[0];
  228. $result["pname"] = $project;
  229. $result["ftype"] = str_replace("/", "-", $result["type"]);
  230. $set = new settings();
  231. $settings = $set->getSettings();
  232. $myfile = CL_ROOT . "/templates/" . $settings["template"] . "/images/symbols/files/" . $result["ftype"] . ".png";
  233. if (stristr($result["type"], "image"))
  234. {
  235. $result["imgfile"] = 1;
  236. } elseif (stristr($result['type'], "text"))
  237. {
  238. $result["imgfile"] = 2;
  239. }
  240. else
  241. {
  242. $result["imgfile"] = 0;
  243. }
  244. if (!file_exists($myfile))
  245. {
  246. $result["ftype"] = "none";
  247. }
  248. $result["title"] = stripslashes($result["title"]);
  249. $result["desc"] = stripslashes($result["desc"]);
  250. // $result["tags"] = stripslashes($result["tags"]);
  251. $result["type"] = "file";
  252. $result[3] = "file";
  253. $result["icon"] = "files.png";
  254. array_push($files, $result);
  255. }
  256. }
  257. if (!empty($files))
  258. {
  259. return $files;
  260. }
  261. else
  262. {
  263. return array();
  264. }
  265. }
  266. function searchUser($query)
  267. {
  268. $query = mysql_real_escape_string($query);
  269. $sel = mysql_query("SELECT `ID`,`email`,`name`,`avatar`,`lastlogin` FROM user WHERE name LIKE '%$query%'");
  270. $user = array();
  271. while ($result = mysql_fetch_array($sel))
  272. {
  273. if (!empty($result))
  274. {
  275. $result["type"] = "user";
  276. $result["name"] = stripslashes($result["name"]);
  277. $result["url"] = "manageuser.php?action=profile&amp;id=$result[ID]";
  278. $result["type"] = "user";
  279. $result[3] = "user";
  280. $result["icon"] = "user.png";
  281. array_push($user, $result);
  282. }
  283. }
  284. if (!empty($user))
  285. {
  286. return $user;
  287. }
  288. else
  289. {
  290. return array();
  291. }
  292. }
  293. function limitResult(array $result, $userid)
  294. {
  295. $finresult = array();
  296. $userid = (int) $userid;
  297. foreach($result as $res)
  298. {
  299. if ($res["type"] != "project" and $res["type"] != "user")
  300. {
  301. if (chkproject($userid, $res["project"]))
  302. {
  303. array_push($finresult, $res);
  304. }
  305. }
  306. else
  307. {
  308. if (chkproject($userid, $res["ID"]))
  309. {
  310. array_push($finresult, $res);
  311. }
  312. }
  313. }
  314. return $finresult;
  315. }
  316. }
  317. ?>