PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions_topics.php

https://github.com/igorw-forks/icy_phoenix
PHP | 424 lines | 296 code | 61 blank | 67 comment | 44 complexity | 50b38e95be884a606557bf79ab0e2c3e MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_ICYPHOENIX'))
  11. {
  12. die('Hacking attempt');
  13. }
  14. /* functions_separate.php - BEGIN */
  15. /*
  16. * Select topic to be suggested
  17. */
  18. function get_dividers($topics)
  19. {
  20. global $lang, $config;
  21. $dividers = array();
  22. $total_topics = sizeof($topics);
  23. $total_by_type = array (POST_GLOBAL_ANNOUNCE => 0, POST_ANNOUNCE => 0, POST_STICKY => 0, POST_NORMAL => 0);
  24. for ($i = 0; $i < $total_topics; $i++)
  25. {
  26. $total_by_type[$topics[$i]['topic_type']]++;
  27. }
  28. //$config['split_ga_ann_sticky'] = 2;
  29. $split_options = $config['split_ga_ann_sticky'];
  30. //split_ga_ann_sticky == 0 -> No split
  31. //split_ga_ann_sticky == 1 -> Global Announce Announce and Sticky Toghether (not splitted)
  32. //split_ga_ann_sticky == 2 -> Split global Announce, Announce and Sticky Toghether (splitted)
  33. //split_ga_ann_sticky == 3 -> All Splitted
  34. if (($total_by_type[POST_GLOBAL_ANNOUNCE] + $total_by_type[POST_ANNOUNCE] + $total_by_type[POST_STICKY]) != 0)
  35. {
  36. $count_topics = 0;
  37. switch ($split_options)
  38. {
  39. case '0':
  40. break;
  41. case '1':
  42. $dividers[$count_topics] = $lang['Announcements_and_Sticky'];
  43. $count_topics += $total_by_type[POST_ANNOUNCE] + $total_by_type[POST_STICKY] + $total_by_type[POST_GLOBAL_ANNOUNCE];
  44. break;
  45. case '2':
  46. $dividers[$count_topics] = $lang['Global_Announcements'];
  47. $count_topics += $total_by_type[POST_GLOBAL_ANNOUNCE];
  48. $dividers[$count_topics] = $lang['Announcements_and_Sticky'];
  49. $count_topics += $total_by_type[POST_ANNOUNCE] + $total_by_type[POST_STICKY];
  50. break;
  51. case '3':
  52. $dividers[$count_topics] = $lang['Global_Announcements'];
  53. $count_topics += $total_by_type[POST_GLOBAL_ANNOUNCE];
  54. $dividers[$count_topics] = $lang['Announcements'];
  55. $count_topics += $total_by_type[POST_ANNOUNCE];
  56. $dividers[$count_topics] = $lang['Sticky_Topics'];
  57. $count_topics += $total_by_type[POST_STICKY];
  58. break;
  59. }//end of switch
  60. if ($count_topics < $total_topics)
  61. {
  62. $dividers[$count_topics] = $lang['Topics'];
  63. }
  64. }
  65. return $dividers;
  66. }
  67. /* functions_separate.php - END */
  68. /* functions_bookmark.php - BEGIN */
  69. // Checks whether a bookmark is set or not
  70. function is_bookmark_set($topic_id)
  71. {
  72. global $db, $userdata;
  73. $is_bookmark_set = false;
  74. $user_id = $userdata['user_id'];
  75. $sql = "SELECT topic_id, user_id
  76. FROM " . BOOKMARK_TABLE . "
  77. WHERE topic_id = " . $topic_id . "
  78. AND user_id = " . $user_id . "
  79. LIMIT 1";
  80. $db->sql_return_on_error(true);
  81. $result = $db->sql_query($sql);
  82. $db->sql_return_on_error(false);
  83. if ($result)
  84. {
  85. $is_bookmark_set = ($db->sql_fetchrow($result)) ? true : false;
  86. $db->sql_freeresult($result);
  87. }
  88. return $is_bookmark_set;
  89. }
  90. // Sets a bookmark
  91. function set_bookmark($topic_id)
  92. {
  93. global $db, $userdata;
  94. $user_id = $userdata['user_id'];
  95. if (!is_bookmark_set($topic_id, $user_id))
  96. {
  97. $sql = "INSERT INTO " . BOOKMARK_TABLE . " (topic_id, user_id)
  98. VALUES (" . $topic_id . ", " . $user_id . ")";
  99. $db->sql_query($sql);
  100. }
  101. return;
  102. }
  103. // Removes a bookmark
  104. function remove_bookmark($topic_id)
  105. {
  106. global $db, $userdata;
  107. $user_id = $userdata['user_id'];
  108. $sql = "DELETE FROM " . BOOKMARK_TABLE . "
  109. WHERE topic_id IN (" . $topic_id . ") AND user_id = " . $user_id;
  110. $db->sql_query($sql);
  111. return;
  112. }
  113. /* functions_bookmark.php - END */
  114. /**
  115. * Get similar topics
  116. * If user is guest or bot it will create a cache list in topics table to save some SQL charge
  117. */
  118. function get_similar_topics($similar_forums_auth, $topic_id, $topic_title, $similar_topics_ids = '', $topic_desc = '')
  119. {
  120. global $db, $config, $userdata, $lang;
  121. $similar_topics = array();
  122. if(($similar_topics_ids !== '') && (!$userdata['session_logged_in'] || $userdata['is_bot']))
  123. {
  124. if($similar_topics_ids == 'empty')
  125. {
  126. return $similar_topics;
  127. }
  128. $topics_array = $similar_topics_ids;
  129. $sql = "SELECT t.*, u.user_id, u.username, u.user_active, u.user_color, u2.username as user2, u2.user_id as id2, u2.user_active as user_active2, u2.user_color as user_color2, f.forum_id, f.forum_name, p.post_time, p.post_username
  130. FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . FORUMS_TABLE . " f, " . POSTS_TABLE . " p, " . USERS_TABLE . " u2
  131. WHERE t.topic_id IN (" . $topics_array . ")
  132. AND t.forum_id = f.forum_id
  133. AND p.poster_id = u2.user_id
  134. AND p.post_id = t.topic_last_post_id
  135. AND t.topic_poster = u.user_id
  136. AND t.topic_status <> " . TOPIC_MOVED . "
  137. GROUP BY t.topic_id
  138. ORDER BY p.post_time";
  139. $result = $db->sql_query($sql);
  140. $similar_topics = $db->sql_fetchrowset($result);
  141. $db->sql_freeresult($result);
  142. return $similar_topics;
  143. }
  144. if ($config['similar_ignore_forums_ids'])
  145. {
  146. $ignore_forums_ids = array_map('intval', explode("\n", trim($config['similar_ignore_forums_ids'])));
  147. }
  148. else
  149. {
  150. $ignore_forums_ids = array();
  151. }
  152. // Get forum auth information to insure privacy of hidden topics
  153. $forums_auth_sql = '';
  154. //foreach ($similar_forums_auth as $k=>$v)
  155. //$similar_forums_auth = auth(AUTH_ALL, AUTH_LIST_ALL, $userdata);
  156. foreach ($similar_forums_auth as $k => $v)
  157. {
  158. if (sizeof($ignore_forums_ids) && in_array($k, $ignore_forums_ids))
  159. {
  160. continue;
  161. }
  162. if ($v['auth_view'] && $v['auth_read'])
  163. {
  164. $forums_auth_sql .= (($forums_auth_sql == '') ? '': ', ') . $k;
  165. }
  166. }
  167. if ($forums_auth_sql != '')
  168. {
  169. $forums_auth_sql = ' AND t.forum_id IN (' . $forums_auth_sql . ') ';
  170. }
  171. if ($config['similar_stopwords'])
  172. {
  173. // encoding match for workaround
  174. $multibyte_charset = 'utf-8, big5, shift_jis, euc-kr, gb2312';
  175. // check against stopwords start
  176. @include_once(IP_ROOT_PATH . 'includes/functions_search.' . PHP_EXT);
  177. $stopword_array = @file(IP_ROOT_PATH . 'language/lang_' . $config['default_lang'] . '/search_stopwords.txt');
  178. $synonym_array = array();
  179. // check against stopwords end
  180. $title_search = '';
  181. $title_search_array = (!strstr($multibyte_charset, $lang['ENCODING'])) ? split_words(clean_words('post', $topic_title, $stopword_array, $synonym_array), 'search') : split(' ', $topic_title);
  182. for ($i = 0; $i < sizeof($title_search_array); $i++)
  183. {
  184. $title_search .= (($title_search == '') ? '': ' ') . $title_search_array[$i];
  185. }
  186. }
  187. else
  188. {
  189. $title_search = $topic_title;
  190. }
  191. /*
  192. if (!empty($topic_desc) && $config['similar_topicdesc'])
  193. {
  194. if ($config['similar_stopwords'])
  195. {
  196. $topicdesc = '';
  197. $topic_desc_array = (!strstr($multibyte_charset, $lang['ENCODING'])) ? split_words(clean_words('post', $topic_desc, $stopword_array, $synonym_array), 'search') : split(' ', $topic_desc);
  198. for ($i = 0; $i < sizeof($topic_desc_array); $i++)
  199. {
  200. $topicdesc .= (($topicdesc == '') ? '': ' ') . $topic_desc_array[$i];
  201. }
  202. }
  203. else
  204. {
  205. $topicdesc = $topic_desc;
  206. }
  207. $sql_topic_desc = "+MATCH(t.topic_desc) AGAINST('" . $db->sql_escape($topicdesc) . "')";
  208. }
  209. $sql_match = "MATCH(t.topic_title) AGAINST('" . $db->sql_escape($title_search) . "')" . $sql_topic_desc;
  210. */
  211. $sql_match = "MATCH(t.topic_title) AGAINST('" . $db->sql_escape($title_search) . "')";
  212. if ($config['similar_sort_type'] == 'time')
  213. {
  214. $sql_sort = 'p.post_time';
  215. }
  216. else
  217. {
  218. $sql_sort = 'relevance';
  219. }
  220. //ORDER BY t.topic_type DESC, ' . $sql_sort . ' DESC LIMIT 0,' . intval($config['similar_max_topics']);
  221. $sql = "SELECT t.*, u.user_id, u.username, u.user_active, u.user_color, u2.username as user2, u2.user_id as id2, u2.user_active as user_active2, u2.user_color as user_color2, f.forum_id, f.forum_name, p.post_time, p.post_username, $sql_match as relevance
  222. FROM ". TOPICS_TABLE ." t, ". USERS_TABLE ." u, ". FORUMS_TABLE ." f, ". POSTS_TABLE ." p, " . USERS_TABLE . " u2
  223. WHERE t.topic_id <> $topic_id $forums_auth_sql
  224. AND $sql_match
  225. AND t.forum_id = f.forum_id
  226. AND p.poster_id = u2.user_id
  227. AND p.post_id = t.topic_last_post_id
  228. AND t.topic_poster = u.user_id
  229. AND t.topic_status <> " . TOPIC_MOVED . "
  230. GROUP BY t.topic_id
  231. ORDER BY " . $sql_sort . " DESC LIMIT 0," . intval($config['similar_max_topics']);
  232. $result = $db->sql_query($sql);
  233. $similar_topics = $db->sql_fetchrowset($result);
  234. $db->sql_freeresult($result);
  235. $count_similar = sizeof($similar_topics);
  236. if(!$userdata['session_logged_in'] || $userdata['is_bot'])
  237. {
  238. $similar_ids_array = 'empty';
  239. if (!empty($count_similar))
  240. {
  241. $similar_ids_array = '';
  242. for ($i = 0; $i < $count_similar; $i++)
  243. {
  244. $similar_ids_array .= (empty($similar_ids_array) ? '' : ',') . $similar_topics[$i]['topic_id'];
  245. }
  246. }
  247. $sql = "UPDATE " . TOPICS_TABLE . " SET topic_similar_topics = '" . $similar_ids_array . "' WHERE topic_id = " . $topic_id;
  248. $result = $db->sql_query($sql);
  249. }
  250. return $similar_topics;
  251. }
  252. /**
  253. * Clear similar topics cache
  254. */
  255. function clear_similar_topics($topic_id = 0)
  256. {
  257. global $db;
  258. $sql_where = '';
  259. if (!empty($topic_id))
  260. {
  261. $sql_where = " WHERE topic_id = " . $topic_id;
  262. }
  263. $sql = "UPDATE " . TOPICS_TABLE . " SET topic_similar_topics = ''" . $sql_where;
  264. $result = $db->sql_query($sql);
  265. return true;
  266. }
  267. /**
  268. * Create clean topic title
  269. */
  270. function create_clean_topic_title($topic_id = 0, $forum_id = 0, $topic_title = '', $forum_name = '')
  271. {
  272. global $db, $lang;
  273. if (empty($topic_id))
  274. {
  275. return false;
  276. }
  277. if (empty($forum_id))
  278. {
  279. $sql = "SELECT forum_id FROM " . TOPICS_TABLE . " WHERE topic_id = " . $topic_id;
  280. $result = $db->sql_query($sql);
  281. while ($row = $db->sql_fetchrow($result))
  282. {
  283. $forum_id = $row['forum_id'];
  284. }
  285. $db->sql_freeresult($result);
  286. }
  287. if (empty($forum_id))
  288. {
  289. return false;
  290. }
  291. if (empty($forum_name))
  292. {
  293. $sql = "SELECT * FROM " . FORUMS_TABLE . " WHERE forum_id = " . $forum_id;
  294. $result = $db->sql_query($sql);
  295. while ($row = $db->sql_fetchrow($result))
  296. {
  297. if (empty($row['forum_name_clean']))
  298. {
  299. if (!function_exists('update_clean_forum_name'))
  300. {
  301. @include_once(IP_ROOT_PATH . 'includes/functions_admin_forums.' . PHP_EXT);
  302. }
  303. $forum_name = substr(ip_clean_string($row['forum_name'], $lang['ENCODING']), 0, 254);
  304. update_clean_forum_name($row['forum_id'], $forum_name);
  305. }
  306. else
  307. {
  308. $forum_name = $row['forum_name_clean'];
  309. }
  310. }
  311. $db->sql_freeresult($result);
  312. }
  313. if (empty($topic_title))
  314. {
  315. $sql = "SELECT * FROM " . TOPICS_TABLE . " WHERE topic_id = " . $topic_id;
  316. $result = $db->sql_query($sql);
  317. while ($row = $db->sql_fetchrow($result))
  318. {
  319. $topic_title = empty($row['topic_title_clean']) ? $row['topic_title'] : $row['topic_title_clean'];
  320. }
  321. $db->sql_freeresult($result);
  322. }
  323. $topic_title = substr(ip_clean_string($topic_title, $lang['ENCODING']), 0, 254);
  324. $forum_name = substr(ip_clean_string($forum_name, $lang['ENCODING']), 0, 254);
  325. $sql = "UPDATE " . TOPICS_TABLE . " SET topic_title_clean = " . $db->sql_validate_value($topic_title) . ", topic_ftitle_clean = " . $db->sql_validate_value($forum_name) . " WHERE topic_id = " . $topic_id;
  326. $result = $db->sql_query($sql);
  327. return true;
  328. }
  329. /**
  330. * Update clean topic title
  331. */
  332. function update_clean_topic_title($topic_id = 0, $topic_title = '')
  333. {
  334. global $db;
  335. if (empty($topic_id) || empty($topic_title))
  336. {
  337. return false;
  338. }
  339. $sql = "UPDATE " . TOPICS_TABLE . " SET topic_title_clean = " . $db->sql_validate_value($topic_title) . " WHERE topic_id = " . $topic_id;
  340. $result = $db->sql_query($sql);
  341. return true;
  342. }
  343. /**
  344. * Clear clean topic title
  345. */
  346. function clear_clean_topic_title($topic_id = 0, $forum_id = 0)
  347. {
  348. global $db;
  349. $sql_where = '';
  350. if (!empty($topic_id))
  351. {
  352. $sql_where = " WHERE topic_id = " . $topic_id;
  353. }
  354. elseif (!empty($forum_id))
  355. {
  356. $sql_where = " WHERE forum_id = " . $forum_id;
  357. }
  358. $sql = "UPDATE " . TOPICS_TABLE . " SET topic_title_clean = '', topic_ftitle_clean = ''" . $sql_where;
  359. $result = $db->sql_query($sql);
  360. return true;
  361. }
  362. ?>