PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/messageboard.php

https://bitbucket.org/ryanhowdy/family-connections
PHP | 1219 lines | 863 code | 178 blank | 178 comment | 82 complexity | 8a78d031db5cef9dc531706bff148601 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Members
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category FCMS
  8. * @package FamilyConnections
  9. * @author Ryan Haudenschilt <r.haudenschilt@gmail.com>
  10. * @copyright 2007 Haudenschilt LLC
  11. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
  12. * @link http://www.familycms.com/wiki/
  13. */
  14. session_start();
  15. define('URL_PREFIX', '');
  16. define('GALLERY_PREFIX', 'gallery/');
  17. require 'fcms.php';
  18. load('datetime', 'messageboard');
  19. init();
  20. // Setup some globals
  21. $currentAccess = checkAccess($fcmsUser->id);
  22. $msgBoardObj = new MessageBoard($fcmsUser->id);
  23. // Setup the Template variables;
  24. $TMPL = array(
  25. 'currentUserId' => $fcmsUser->id,
  26. 'sitename' => getSiteName(),
  27. 'nav-link' => getNavLinks(),
  28. 'pagetitle' => T_('Message Board'),
  29. 'path' => URL_PREFIX,
  30. 'displayname' => $fcmsUser->displayName,
  31. 'version' => getCurrentVersion(),
  32. 'year' => date('Y')
  33. );
  34. control();
  35. exit();
  36. /**
  37. * control
  38. *
  39. * The controlling structure for this script.
  40. *
  41. * @return void
  42. */
  43. function control ()
  44. {
  45. global $currentAccess;
  46. // New Post
  47. if (isset($_GET['reply']))
  48. {
  49. displayNewPostForm();
  50. }
  51. elseif (isset($_POST['reply_submit']))
  52. {
  53. displayNewPostSubmit();
  54. }
  55. elseif (isset($_POST['post_submit']))
  56. {
  57. displayNewThreadSubmit();
  58. }
  59. // Edit Post
  60. elseif (isset($_POST['editpost']))
  61. {
  62. displayEditPostForm();
  63. }
  64. elseif (isset($_POST['edit_submit']))
  65. {
  66. displayEditPostSubmit();
  67. }
  68. // Delete
  69. elseif (isset($_POST['delpost']) && !isset($_POST['confirmed']))
  70. {
  71. displayConfirmDelete();
  72. }
  73. elseif (isset($_POST['delconfirm']) || isset($_POST['confirmed']))
  74. {
  75. displayDeletePostSubmit();
  76. }
  77. // Administrate Thread
  78. elseif (isset($_POST['submit_admin']) && $currentAccess < 2)
  79. {
  80. displayAdministrateThreadSubmit();
  81. }
  82. // Admin Edit Subject
  83. elseif (isset($_POST['edit_admin_submit']))
  84. {
  85. displayAdminEditSubjectSubmit();
  86. }
  87. // Search results
  88. elseif (isset($_POST['search']))
  89. {
  90. displaySearchSubmit();
  91. }
  92. elseif (isset($_GET['search']))
  93. {
  94. displayAdvancedSearchForm();
  95. }
  96. elseif (isset($_GET['thread']))
  97. {
  98. displayThread();
  99. }
  100. else
  101. {
  102. displayThreads();
  103. }
  104. }
  105. /**
  106. * displayHeader
  107. *
  108. * @param string $js Javascript to overwrite the default
  109. *
  110. * @return void
  111. */
  112. function displayHeader ($js = '')
  113. {
  114. global $fcmsUser, $TMPL;
  115. $TMPL['javascript'] = $js;
  116. // Default js
  117. if ($js == '')
  118. {
  119. $TMPL['javascript'] = '
  120. <script type="text/javascript">
  121. //<![CDATA[
  122. Event.observe(window, \'load\', function() {
  123. initChatBar(\''.T_('Chat').'\', \''.$TMPL['path'].'\');
  124. if (!$$(\'.delpost input[type="submit"]\')) { return; }
  125. $$(\'.delpost input[type="submit"]\').each(function(item) {
  126. item.onclick = function() { return confirm(\''.T_('Are you sure you want to DELETE this?').'\'); };
  127. var hid = document.createElement(\'input\');
  128. hid.setAttribute(\'type\', \'hidden\');
  129. hid.setAttribute(\'name\', \'confirmed\');
  130. hid.setAttribute(\'value\', \'true\');
  131. item.insert({\'after\':hid});
  132. });
  133. if ($(\'toolbar\')) {
  134. $(\'toolbar\').removeClassName("hideme");
  135. }
  136. if ($(\'smileys\')) {
  137. $(\'smileys\').removeClassName("hideme");
  138. }
  139. if ($(\'upimages\')) {
  140. $(\'upimages\').removeClassName("hideme");
  141. }
  142. return true;
  143. });
  144. //]]>
  145. </script>';
  146. }
  147. include getTheme($fcmsUser->id).'header.php';
  148. echo '
  149. <div id="messageboard" class="centercontent">';
  150. }
  151. /**
  152. * displayFooter
  153. *
  154. * @return void
  155. */
  156. function displayFooter ()
  157. {
  158. global $fcmsUser, $TMPL;
  159. echo '
  160. </div><!-- #messageboard .centercontent -->';
  161. include getTheme($fcmsUser->id).'footer.php';
  162. }
  163. /**
  164. * displayThreads
  165. *
  166. * @return void
  167. */
  168. function displayThreads ()
  169. {
  170. global $msgBoardObj;
  171. $page = getPage();
  172. displayHeader();
  173. if (isset($_SESSION['success']))
  174. {
  175. displayOkMessage();
  176. unset($_SESSION['success']);
  177. }
  178. $msgBoardObj->showThreads('announcement');
  179. $msgBoardObj->showThreads('thread', $page);
  180. displayFooter();
  181. }
  182. /**
  183. * displayThread
  184. *
  185. * Displays the posts for a specific thread.
  186. *
  187. * @return void
  188. */
  189. function displayThread ()
  190. {
  191. global $msgBoardObj;
  192. displayHeader();
  193. $threadId = (int)$_GET['thread'];
  194. $page = getPage();
  195. if (isset($_SESSION['success']))
  196. {
  197. displayOkMessage();
  198. unset($_SESSION['success']);
  199. }
  200. $msgBoardObj->showPosts($threadId, $page);
  201. displayFooter();
  202. }
  203. /**
  204. * displayNewThreadSubmit
  205. *
  206. * @return void
  207. */
  208. function displayNewThreadSubmit ()
  209. {
  210. global $fcmsUser, $TMPL, $msgBoardObj;
  211. $rawPost = $_POST['post'];
  212. $rawSubject = $_POST['subject'];
  213. $post = escape_string($_POST['post']);
  214. $subject = escape_string($_POST['subject']);
  215. displayHeader();
  216. if (isset($_POST['sticky']))
  217. {
  218. $subject = "#ANOUNCE#".$subject;
  219. }
  220. // Create new thread
  221. $sql = "INSERT INTO `fcms_board_threads`
  222. (`subject`, `started_by`, `updated`, `updated_by`)
  223. VALUES (
  224. '$subject',
  225. '$fcmsUser->id',
  226. NOW(),
  227. '$fcmsUser->id'
  228. )";
  229. if (!mysql_query($sql))
  230. {
  231. displaySqlError($sql, mysql_error());
  232. displayFooter();
  233. return;
  234. }
  235. $newThreadId = mysql_insert_id();
  236. // Create new post
  237. $sql = "INSERT INTO `fcms_board_posts`(`date`, `thread`, `user`, `post`)
  238. VALUES (
  239. NOW(),
  240. '$newThreadId',
  241. '$fcmsUser->id',
  242. '$post'
  243. )";
  244. if (!mysql_query($sql))
  245. {
  246. displaySqlError($sql, mysql_error());
  247. displayFooter();
  248. return;
  249. }
  250. // Email members
  251. $sql = "SELECT u.`email`, s.`user`
  252. FROM `fcms_user_settings` AS s, `fcms_users` AS u
  253. WHERE `email_updates` = '1'
  254. AND u.`id` = s.`user`";
  255. $result = mysql_query($sql);
  256. if (!$result)
  257. {
  258. displaySqlError($sql, mysql_error());
  259. }
  260. if (mysql_num_rows($result) > 0)
  261. {
  262. while ($r = mysql_fetch_array($result))
  263. {
  264. $name = getUserDisplayName($fcmsUser->id);
  265. $to = getUserDisplayName($r['user']);
  266. // Email is sent as plain text
  267. $emailHeaders = getEmailHeaders();
  268. $emailSubject = sprintf(T_('%s started the new thread %s.'), $name, $rawSubject);
  269. $email = $r['email'];
  270. $url = getDomainAndDir();
  271. $msg = T_('Dear').' '.$to.',
  272. '.$emailSubject.'
  273. '.$url.'messageboard.php?thread='.$newThreadId.'
  274. ----
  275. '.T_('To stop receiving these notifications, visit the following url and change your \'Email Update\' setting to No:').'
  276. '.$url.'settings.php
  277. ';
  278. mail($email, $rawSubject, $msg, $emailHeaders);
  279. }
  280. }
  281. // Display the new thread
  282. $msgBoardObj->showPosts($newThreadId, 1);
  283. displayFooter();
  284. }
  285. /**
  286. * displayNewPostSubmit
  287. *
  288. * @return void
  289. */
  290. function displayNewPostSubmit ()
  291. {
  292. global $fcmsUser, $TMPL, $msgBoardObj;
  293. displayHeader();
  294. $rawPost = $_POST['post'];
  295. $post = escape_string($rawPost);
  296. $threadId = (int)$_POST['thread_id'];
  297. // Update Thread info
  298. $sql = "UPDATE `fcms_board_threads`
  299. SET `updated` = NOW(), `updated_by` = '$fcmsUser->id'
  300. WHERE `id` = $threadId";
  301. if (!mysql_query($sql))
  302. {
  303. displaySqlError($sql, mysql_error());
  304. return;
  305. }
  306. // Insert new Post
  307. $sql = "INSERT INTO `fcms_board_posts` (`date`, `thread`, `user`, `post`)
  308. VALUES (
  309. NOW(),
  310. '$threadId',
  311. '$fcmsUser->id',
  312. '$post'
  313. )";
  314. if (!mysql_query($sql))
  315. {
  316. displaySqlError($sql, mysql_error());
  317. return;
  318. }
  319. // Email members
  320. $sql = "SELECT u.`email`, s.`user`
  321. FROM `fcms_user_settings` AS s, `fcms_users` AS u
  322. WHERE `email_updates` = '1'
  323. AND u.`id` = s.`user`";
  324. $result = mysql_query($sql);
  325. if (!$result)
  326. {
  327. displaySqlError($sql, mysql_error());
  328. }
  329. if (mysql_num_rows($result) > 0)
  330. {
  331. while ($r = mysql_fetch_array($result))
  332. {
  333. $name = getUserDisplayName($fcmsUser->id);
  334. $sql = "SELECT `subject`
  335. FROM `fcms_board_threads`
  336. WHERE `id` = $threadId";
  337. $subject_result = mysql_query($sql);
  338. if (!$subject_result)
  339. {
  340. displaySqlError($sql, mysql_error());
  341. return;
  342. }
  343. $row = mysql_fetch_array($subject_result);
  344. $threadSubject = $row['subject'];
  345. $pos = strpos($threadSubject, '#ANOUNCE#');
  346. if ($pos !== false)
  347. {
  348. $threadSubject = substr($threadSubject, 9, strlen($threadSubject)-9);
  349. }
  350. // Emails sent as plain text
  351. $emailHeaders = getEmailHeaders();
  352. $subject = sprintf(T_('%s has replied to the thread: %s'), $name, $threadSubject);
  353. $email = $r['email'];
  354. $to = getUserDisplayName($r['user']);
  355. $url = getDomainAndDir();
  356. $msg = T_('Dear').' '.$to.',
  357. '.$subject.'
  358. '.$url.'messageboard.php?thread='.$threadId.'
  359. ----
  360. '.T_('To stop receiving these notifications, visit the following url and change your \'Email Update\' setting to No:').'
  361. '.$url.'settings.php
  362. ';
  363. mail($email, $subject, $msg, $emailHeaders);
  364. }
  365. }
  366. $msgBoardObj->showPosts($threadId, 1);
  367. displayFooter();
  368. }
  369. /**
  370. * displayNewPostForm
  371. *
  372. * Used to create new posts. Used when creating a new thread also.
  373. *
  374. * @return void
  375. */
  376. function displayNewPostForm ()
  377. {
  378. global $fcmsUser, $TMPL, $currentAccess, $msgBoardObj;
  379. displayHeader();
  380. if ($currentAccess >= 8 && $currentAccess == 5)
  381. {
  382. echo '
  383. <p class="error-alert">'.T_('You do not have access to view this page.').'</p>';
  384. displayFooter();
  385. return;
  386. }
  387. if ($_GET['reply'] == 'new')
  388. {
  389. $msgBoardObj->displayForm('new');
  390. }
  391. else
  392. {
  393. $reply = (int)$_GET['reply'];
  394. if (isset($_POST['quotepost']))
  395. {
  396. $id = (int)$_POST['id'];
  397. $msgBoardObj->displayForm('reply', $reply, $id);
  398. }
  399. else
  400. {
  401. $msgBoardObj->displayForm('reply', $reply);
  402. }
  403. }
  404. displayFooter();
  405. }
  406. /**
  407. * displayEditPostForm
  408. *
  409. * Displays the form for editing a post.
  410. *
  411. * @return void
  412. */
  413. function displayEditPostForm ()
  414. {
  415. global $msgBoardObj;
  416. displayHeader();
  417. $id = (int)$_POST['id'];
  418. $sql = "SELECT `post`, `thread`
  419. FROM `fcms_board_posts`
  420. WHERE `id` = '$id'
  421. LIMIT 1";
  422. $result = mysql_query($sql);
  423. if (!$result)
  424. {
  425. displaySqlError($sql, mysql_error());
  426. displayFooter();
  427. return;
  428. }
  429. $r = mysql_fetch_array($result);
  430. $msgBoardObj->displayForm('edit', $r['thread'], $id, $r['post']);
  431. displayFooter();
  432. }
  433. /**
  434. * displayEditPostSubmit
  435. *
  436. * TODO - see below
  437. *
  438. * @return void
  439. */
  440. function displayEditPostSubmit ()
  441. {
  442. global $msgBoardObj;
  443. $id = (int)$_POST['id'];
  444. $threadId = (int)$_POST['thread_id'];
  445. $post = escape_string($_POST['post']);
  446. displayHeader();
  447. // TODO
  448. // Need to find a better way to add the edited by text
  449. // this method could mess up if the site changes languages at some point
  450. $pos = strpos($post, "\n\n[size=small][i]".T_('Edited'));
  451. if ($pos === false)
  452. {
  453. $post = $post."\n\n[size=small][i]".T_('Edited')." ".fixDate('n/d/Y g:ia', $msgBoardObj->tzOffset)."[/i][/size]";
  454. }
  455. else
  456. {
  457. $post = substr($post, 0, $pos);
  458. $post = $post."[size=small][i]".T_('Edited')." ".fixDate('n/d/Y g:ia', $msgBoardObj->tzOffset)."[/i][/size]";
  459. }
  460. // Update Post
  461. $sql = "UPDATE `fcms_board_posts`
  462. SET `post` = '$post'
  463. WHERE `id` = '$id'";
  464. if (!mysql_query($sql))
  465. {
  466. displaySqlError($sql, mysql_error());
  467. displayFooter();
  468. return;
  469. }
  470. $msgBoardObj->showPosts($threadId, 1);
  471. displayFooter();
  472. }
  473. /**
  474. * displayAdminEditSubjectSubmit
  475. *
  476. * The submit screen for editing the subject of a thread.
  477. *
  478. * @return void
  479. */
  480. function displayAdminEditSubjectSubmit ()
  481. {
  482. global $msgBoardObj;
  483. $threadId = (int)$_POST['thread'];
  484. displayHeader();
  485. if (isset($_POST['sticky']))
  486. {
  487. $subject = "#ANOUNCE#".$_POST->escMySQL('subject');
  488. }
  489. else
  490. {
  491. $subject = $_POST->escMySQL('subject');
  492. }
  493. $sql = "UPDATE `fcms_board_threads`
  494. SET `subject` = '$subject'
  495. WHERE `id` = '$threadId'";
  496. if (!mysql_query($sql))
  497. {
  498. displaySqlError($sql, mysql_error());
  499. displayFooter();
  500. return;
  501. }
  502. displayOkMessage();
  503. $msgBoardObj->showPosts($threadId, 1);
  504. displayFooter();
  505. }
  506. /**
  507. * displayConfirmDelete
  508. *
  509. * The delete post confirmation screen, used when user doesn't have js turned on.
  510. *
  511. * @return void
  512. */
  513. function displayConfirmDelete ()
  514. {
  515. $threadId = (int)$_POST['thread'];
  516. $id = (int)$_POST['id'];
  517. displayHeader();
  518. echo '
  519. <div class="info-alert">
  520. <form action="messageboard.php?thread='.$threadId.'" method="post">
  521. <h2>'.T_('Are you sure you want to DELETE this?').'</h2>
  522. <p><b><i>'.T_('This can NOT be undone.').'</i></b></p>
  523. <div>
  524. <input type="hidden" name="id" value="'.$id.'"/>
  525. <input type="hidden" name="thread" value="'.$threadId.'"/>
  526. <input style="float:left;" type="submit" id="delconfirm" name="delconfirm" value="'.T_('Yes').'"/>
  527. <a style="float:right;" href="messageboard.php?thread='.$threadId.'">'.T_('Cancel').'</a>
  528. </div>
  529. </form>
  530. </div>';
  531. displayFooter();
  532. }
  533. /**
  534. * displayDeletePostSubmit
  535. *
  536. * The submit screen for deleting a post.
  537. *
  538. * @return void
  539. */
  540. function displayDeletePostSubmit ()
  541. {
  542. global $msgBoardObj;
  543. $id = (int)$_POST['id'];
  544. $threadId = (int)$_POST['thread'];
  545. // Get id of last post in the current thread
  546. $sql = "SELECT MAX(`id`) AS max
  547. FROM `fcms_board_posts`
  548. WHERE `thread` = '$threadId'";
  549. $result = mysql_query($sql);
  550. if (!$result)
  551. {
  552. displayHeader();
  553. displaySqlError($sql, mysql_error());
  554. displayFooter();
  555. return;
  556. }
  557. $r = mysql_fetch_array($result);
  558. $max = $r['max'];
  559. // Get total post count for this thread
  560. $sql = "SELECT `id`
  561. FROM `fcms_board_posts`
  562. WHERE `thread` = '$threadId'";
  563. $result = mysql_query($sql);
  564. if (!$result)
  565. {
  566. displayHeader();
  567. displaySqlError($sql, mysql_error());
  568. displayFooter();
  569. return;
  570. }
  571. $total = mysql_num_rows($result);
  572. // If this thread only has one post
  573. if ($total == 1)
  574. {
  575. // Delete the entire thread
  576. $sql = "DELETE FROM `fcms_board_threads`
  577. WHERE `id` = '$threadId'";
  578. if (!mysql_query($sql))
  579. {
  580. displayHeader();
  581. displaySqlError($sql, mysql_error());
  582. displayFooter();
  583. return;
  584. }
  585. $_SESSION['success'] = 1;
  586. header("Location: messageboard.php?thread=$threadId");
  587. return;
  588. }
  589. // If we are deleting the last post in the thread
  590. elseif ($id == $max)
  591. {
  592. // Delete post
  593. $sql = "DELETE FROM `fcms_board_posts`
  594. WHERE `id` = '$id'";
  595. if (!mysql_query($sql))
  596. {
  597. displayHeader();
  598. displaySqlError($sql, mysql_error());
  599. displayFooter();
  600. return;
  601. }
  602. // Get new last post in the thread
  603. $sql = "SELECT MAX(`id`) AS max
  604. FROM `fcms_board_posts`
  605. WHERE `thread` = '$threadId'";
  606. $result = mysql_query($sql);
  607. if (!$result)
  608. {
  609. displayHeader();
  610. displaySqlError($sql, mysql_error());
  611. displayFooter();
  612. return;
  613. }
  614. $row = mysql_fetch_array($result);
  615. $newmax = $row['max'];
  616. // Get info from new last post
  617. $sql = "SELECT `date`, `user`
  618. FROM `fcms_board_posts`
  619. WHERE `id` = '$newmax'";
  620. $result = mysql_query($sql);
  621. if (!$result)
  622. {
  623. displayHeader();
  624. displaySqlError($sql, mysql_error());
  625. displayFooter();
  626. return;
  627. }
  628. $r = mysql_fetch_array($result);
  629. // Update the thread with last post info
  630. $sql = "UPDATE `fcms_board_threads`
  631. SET `updated` = '".$r['date']."', `updated_by` = ".$r['user']."
  632. WHERE `id` = '$threadId'";
  633. if (!mysql_query($sql))
  634. {
  635. displayHeader();
  636. displaySqlError($sql, mysql_error());
  637. displayFooter();
  638. return;
  639. }
  640. }
  641. // We are deleting a post in the middle of the thread
  642. else
  643. {
  644. $sql = "DELETE FROM `fcms_board_posts`
  645. WHERE `id` = '$id'";
  646. if (!mysql_query($sql))
  647. {
  648. displayHeader();
  649. displaySqlError($sql, mysql_error());
  650. displayFooter();
  651. return;
  652. }
  653. }
  654. $_SESSION['success'] = 1;
  655. header("Location: messageboard.php?thread=$threadId");
  656. }
  657. /**
  658. * displayAdministrateThreadSubmit
  659. *
  660. * The submit screen for administering a thread.
  661. *
  662. * @return void
  663. */
  664. function displayAdministrateThreadSubmit ()
  665. {
  666. global $msgBoardObj;
  667. $threadId = (int)$_POST['thread'];
  668. $adminOption = $_POST['admin_option'];
  669. // Did they submit a blank form?
  670. if (empty($adminOption))
  671. {
  672. header("Location: messageboard.php?thread=$threadId");
  673. return;
  674. }
  675. // Changing Thread type
  676. if ($adminOption == 'normal' || $adminOption == 'announcement')
  677. {
  678. $sql = "SELECT `subject`
  679. FROM `fcms_board_threads`
  680. WHERE `id` = '$threadId'
  681. LIMIT 1";
  682. $result = mysql_query($sql);
  683. if (!$result)
  684. {
  685. displayHeader();
  686. displaySqlError($sql, mysql_error());
  687. displayFooter();
  688. return;
  689. }
  690. if (mysql_num_rows($result) < 1)
  691. {
  692. displayHeader();
  693. echo '<p class="error-alert">'.T_('Thread does not exist.').'</p>';
  694. displayFooter();
  695. return;
  696. }
  697. $row = mysql_fetch_array($result);
  698. // Normal Thread
  699. if ($adminOption == 'normal')
  700. {
  701. $subject = $msgBoardObj->fixSubject($row['subject']);
  702. }
  703. // Announcement
  704. else
  705. {
  706. $subject = '#ANOUNCE#'.$row['subject'];
  707. }
  708. $sql = "UPDATE `fcms_board_threads`
  709. SET `subject` = '$mysqlSubject'
  710. WHERE `id` = '$threadId'";
  711. if (!mysql_query($sql))
  712. {
  713. displayHeader();
  714. displaySqlError($sql, mysql_error());
  715. displayFooter();
  716. return;
  717. }
  718. $_SESSION['success'] = 1;
  719. header("Location: messageboard.php?thread=$threadId");
  720. return;
  721. }
  722. // Edit Thread Subject
  723. if ($adminOption == 'subject')
  724. {
  725. displayHeader();
  726. $msgBoardObj->displayAdminEditSubjectForm($threadId);
  727. displayFooter();
  728. return;
  729. }
  730. // Delete thread
  731. if ($adminOption == 'delete')
  732. {
  733. $sql = "DELETE FROM `fcms_board_posts`
  734. WHERE `thread` = '$threadId'";
  735. if (!mysql_query($sql))
  736. {
  737. displayHeader();
  738. displaySqlError($sql, mysql_error());
  739. displayFooter();
  740. return;
  741. }
  742. $sql = "DELETE FROM `fcms_board_threads`
  743. WHERE `id` = '$threadId'";
  744. if (!mysql_query($sql))
  745. {
  746. displayHeader();
  747. displaySqlError($sql, mysql_error());
  748. displayFooter();
  749. return;
  750. }
  751. $_SESSION['success'] = 1;
  752. }
  753. header("Location: messageboard.php");
  754. }
  755. /**
  756. * displaySearchSubmit
  757. *
  758. * Display the results for the search query.
  759. *
  760. * @return void
  761. */
  762. function displaySearchSubmit ()
  763. {
  764. global $msgBoardObj;
  765. $search = $_POST['search'];
  766. $advanced = false;
  767. // validate start date
  768. if (isset($_POST['start']))
  769. {
  770. $start = $_POST['start'];
  771. $found = preg_match('/^\d{4}-(1[012]|0?\d)-(3[01]|[012]?\d)$/', $start);
  772. if ($found === false || $found < 1)
  773. {
  774. $error = sprintf(T_('Invalid Date [%s]'), cleanOutput($start));
  775. displayAdvancedSearchForm($error);
  776. return;
  777. }
  778. $advanced = true;
  779. }
  780. // validate end date
  781. if (isset($_POST['end']))
  782. {
  783. $end = $_POST['end'];
  784. $found = preg_match('/^\d{4}-(1[012]|0?\d)-(3[01]|[012]?\d)$/', $end);
  785. if ($found === false || $found < 1)
  786. {
  787. $error = sprintf(T_('Invalid Date [%s]'), cleanOutput($end));
  788. displayAdvancedSearchForm($error);
  789. return;
  790. }
  791. $advanced = true;
  792. }
  793. displayHeader();
  794. echo '
  795. <div id="sections_menu">
  796. <ul>
  797. <li><a href="messageboard.php">'.T_('Message Board Home').'</a></li>
  798. </ul>
  799. </div>
  800. <form method="post" action="messageboard.php">
  801. <p id="big_search">
  802. <input type="text" id="search" name="search" value="'.cleanOutput($search).'"/>
  803. <input type="submit" value="'.T_('Search').'"/><br/>
  804. <a href="?search=advanced">'.T_('Advanced Search').'</a>
  805. </p>
  806. </form>';
  807. $mysqlSearch = escape_string($_POST['search']);
  808. // Thread subject
  809. $sql = "SELECT t.`id`, t.`subject`, t.`started_by`, p.`date`, p.`post`
  810. FROM `fcms_board_posts` AS p, `fcms_board_threads` AS t
  811. WHERE p.`thread` = t.`id`
  812. AND `subject` LIKE '%$mysqlSearch%'";
  813. if ($advanced)
  814. {
  815. $sql .= "
  816. AND p.`date` >= '$start'
  817. AND p.`date` <= '$end'";
  818. }
  819. // Post body
  820. $sql .= "
  821. GROUP BY p.`thread`
  822. UNION
  823. SELECT t.`id`, t.`subject`, t.`started_by`, p.`date`, p.`post`
  824. FROM `fcms_board_posts` AS p, `fcms_board_threads` AS t
  825. WHERE p.`thread` = t.`id`
  826. AND `post` LIKE '%$mysqlSearch%'";
  827. if ($advanced)
  828. {
  829. $sql .= "
  830. AND p.`date` >= '$start'
  831. AND p.`date` <= '$end'";
  832. }
  833. $result = mysql_query($sql);
  834. if (!$result)
  835. {
  836. displaySqlError($sql, mysql_error());
  837. displayFooter();
  838. return;
  839. }
  840. if (mysql_num_rows($result) <= 0)
  841. {
  842. echo '
  843. <div class="search_result">
  844. <p>'.T_('Could not find anything matching your search.').'</p>
  845. </div>';
  846. }
  847. while ($r = mysql_fetch_assoc($result))
  848. {
  849. // Remove #ANNOUNCE#
  850. $subject = $msgBoardObj->fixSubject($r['subject']);
  851. // Clean html
  852. $subject = cleanOutput($subject, 'html');
  853. // Put in our html (should be the only html rendered)
  854. $subject = str_ireplace($search, '<b>'.$search.'</b>', $subject);
  855. // Remove orig bbcode
  856. $post = removeBBCode($r['post']);
  857. // Clean html
  858. $post = cleanOutput($post, 'html');
  859. // Put in our html (should be the only html rendered)
  860. $post = str_ireplace($search, '<b>'.$search.'</b>', $post);
  861. $date = fixDate('n/d/Y g:ia', $msgBoardObj->tzOffset, $r['date']);
  862. echo '
  863. <div class="search_result">
  864. <a href="?thread='.$r['id'].'">'.$subject.'</a>
  865. <p>'.$post.'</p>
  866. <span>'.$date.'</span>
  867. </div>';
  868. }
  869. displayFooter();
  870. }
  871. /**
  872. * displayAdvancedSearchForm
  873. *
  874. * @param string $error Any previous error for this form.
  875. *
  876. * @return void
  877. */
  878. function displayAdvancedSearchForm ($error = '')
  879. {
  880. global $tzOffset;
  881. $js = '
  882. <link rel="stylesheet" type="text/css" href="ui/datechooser.css"/>
  883. <script type="text/javascript" src="ui/js/datechooser.js"></script>
  884. <script type="text/javascript">
  885. //<![CDATA[
  886. Event.observe(window, \'load\', function() {
  887. var dc1 = new DateChooser();
  888. dc1.setUpdateField({\'start\':\'Y-m-d\'});
  889. dc1.setIcon(\'ui/themes/default/images/datepicker.jpg\', \'start\');
  890. var dc2 = new DateChooser();
  891. dc2.setUpdateField({\'end\':\'Y-m-d\'});
  892. dc2.setIcon(\'ui/themes/default/images/datepicker.jpg\', \'end\');
  893. });
  894. //]]>
  895. </script>';
  896. displayHeader($js);
  897. $end = fixDate('Y-m-d', $tzOffset, gmdate('Y-m-d H:i:s'));
  898. $start = date('Y-m-d', strtotime("$end -30 day"));
  899. if ($error != '')
  900. {
  901. $error = '<div class="error-alert">'.$error.'</div>';
  902. }
  903. echo '
  904. <div id="sections_menu">
  905. <ul>
  906. <li><a href="messageboard.php">'.T_('Message Board Home').'</a></li>
  907. </ul>
  908. </div>
  909. '.$error.'
  910. <form method="post" action="messageboard.php">
  911. <fieldset>
  912. <legend><span>'.T_('Advanced Search').'</span></legend>
  913. <div>
  914. <label for="search">'.T_('Search For').'</label><br/>
  915. <input type="text" id="search" name="search"/>
  916. </div><br/>
  917. <div>
  918. <label for="date">'.T_('Date').'</label><br/>
  919. <input type="text" id="start" name="start" value="'.$start.'" size="6" maxlength="10"/> -
  920. <input type="text" id="end" name="end" value="'.$end.'" size="6" maxlength="10"/>
  921. </div><br/>
  922. <p>
  923. <input type="submit" class="sub1" value="'.T_('Search').'" name="advanced" id="advanced"/>
  924. </p>
  925. </fieldset>
  926. </form>';
  927. displayFooter();
  928. }
  929. /**
  930. * displayAdvancedSearchSubmit
  931. *
  932. * @return void
  933. */
  934. function displayAdvancedSearchSubmit ()
  935. {
  936. global $msgBoardObj;
  937. $start = $_POST['start'];
  938. $end = $_POST['end'];
  939. $search = $_POST['advanced-search'];
  940. // validate dates
  941. $found = preg_match('/^\d{4}-(1[012]|0?\d)-(3[01]|[012]?\d)$/', $start);
  942. if ($found === false || $found < 1)
  943. {
  944. $error = sprintf(T_('Invalid Date [%s]'), cleanOutput($start));
  945. displayAdvancedSearchForm($error);
  946. return;
  947. }
  948. $found = preg_match('/^\d{4}-(1[012]|0?\d)-(3[01]|[012]?\d)$/', $end);
  949. if ($found === false || $found < 1)
  950. {
  951. $error = sprintf(T_('Invalid Date [%s]'), cleanOutput($end));
  952. displayAdvancedSearchForm($error);
  953. return;
  954. }
  955. displayHeader();
  956. echo '
  957. <div id="sections_menu">
  958. <ul>
  959. <li><a href="messageboard.php">'.T_('Message Board Home').'</a></li>
  960. </ul>
  961. </div>
  962. <form method="post" action="messageboard.php">
  963. <p id="big_search">
  964. <input type="text" id="search" name="search" value="'.cleanOutput($search, 'html').'"/>
  965. <input type="submit" value="'.T_('Search').'"/><br/>
  966. <a href="?search=advanced">'.T_('Advanced Search').'</a>
  967. </p>
  968. </form>';
  969. $mysqlSearch = $_POST->escMySQL($search);
  970. $sql = "SELECT t.`id`, t.`subject`, t.`started_by`, t.`updated`, p.`post`,
  971. 'thread' AS type
  972. FROM `fcms_board_threads` AS t, `fcms_board_posts` AS p
  973. WHERE p.`thread` = t.`id`
  974. AND `subject` LIKE '%$mysqlSearch%'
  975. UNION
  976. SELECT t.`id`, t.`subject`, t.`started_by`, p.`date` AS updated, p.`post`,
  977. 'post' AS type
  978. FROM `fcms_board_threads` AS t, `fcms_board_posts` AS p
  979. WHERE p.`thread` = t.`id`
  980. AND `post` LIKE '%$mysqlSearch%'
  981. AND `date` >= '$start'
  982. AND `date` <= '$end'";
  983. $result = mysql_query($sql);
  984. if (!$result)
  985. {
  986. displaySqlError($sql, mysql_error());
  987. displayFooter();
  988. return;
  989. }
  990. if (mysql_num_rows($result) <= 0)
  991. {
  992. echo '
  993. <div class="search_result">
  994. <p>'.T_('Could not find anything matching your search.').'</p>
  995. </div>';
  996. }
  997. $threadsFound = array();
  998. while ($r = mysql_fetch_assoc($result))
  999. {
  1000. // if the search is found both in the subject and post
  1001. // skip the post, so we don't show doubles
  1002. if ($r['type'] == 'post')
  1003. {
  1004. if (isset($threadsFound[$r['id']]))
  1005. {
  1006. continue;
  1007. }
  1008. }
  1009. $threadsFound[$r['id']] = 1;
  1010. // Remove #ANNOUNCE#
  1011. $subject = $msgBoardObj->fixSubject($r['subject']);
  1012. // Clean html
  1013. $subject = cleanOutput($subject, 'html');
  1014. // Put in our html (should be the only html rendered)
  1015. $subject = str_ireplace($search, '<b>'.$search.'</b>', $subject);
  1016. // Remove orig bbcode
  1017. $post = removeBBCode($r['post']);
  1018. // Clean html
  1019. $post = cleanOutput($post, 'html');
  1020. // Put in our html (should be the only html rendered)
  1021. $post = str_ireplace($search, '<b>'.$search.'</b>', $post);
  1022. $date = fixDate('n/d/Y g:ia', $msgBoardObj->tzOffset, $r['updated']);
  1023. echo '
  1024. <div class="search_result">
  1025. <a href="?thread='.$r['id'].'">'.$subject.'</a>
  1026. <p>'.$post.'</p>
  1027. <span>'.$date.'</span>
  1028. </div>';
  1029. }
  1030. displayFooter();
  1031. }