Vanilla commit.
[tinybbs.git] / search.php
blobad4282ab717ea77b6e4eed20c84084f8f48bdb18
1 <?php
3 require('includes/header.php');
4 update_activity('search');
5 $page_title = 'Search';
6 $onload_javascript = 'focusId(\'phrase\'); init();';
8 if( ! empty($_POST['phrase']))
10 if($_POST['deep_search'])
12 $redirect_to = DOMAIN . 'deep_search/' . urlencode($_POST['phrase']);
14 else
16 $redirect_to = DOMAIN . 'quick_search/' . urlencode($_POST['phrase']);
19 header('Location: ' . $redirect_to);
20 exit;
25 <p>The "quick" option searches only topic headlines, while the "deep" option searches both headlines and bodies.</p>
27 <form action="" method="post">
28 <div class="row">
29 <input id="phrase" name="phrase" type="text" size="80" maxlength="255" value="<?php echo htmlspecialchars($_GET['q']) ?>" class="inline" />
30 <input type="submit" value="Quick" class="inline" />
31 <input type="submit" value="Deep" name="deep_search" class="inline" />
32 </div>
33 </form>
35 <?php
37 if( ! empty($_GET['q']))
39 $search_query = addcslashes( trim($_GET['q']), '%_' );
40 $common_words = array('the', 'and', 'are', 'that', 'for', 'with', 'lol', 'what', 'where', 'when', 'why');
42 if(strlen($search_query) < 3)
44 add_error('Your query must be at least 3 characters.');
46 else if(in_array($search_query, $common_words))
48 add_error('Your search query is too common a word.');
50 if($_SERVER['REQUEST_TIME'] - $_SESSION['last_search'] < 5)
52 add_error('Wait at least 5 seconds between searches.');
55 if( ! $erred)
57 $_SESSION['last_search'] = $_SERVER['REQUEST_TIME'];
59 $search_query = '%' . $search_query . '%';
60 if($_GET['deep_search'])
62 $search_topics = $link->prepare('SELECT id, time, replies, visits, headline FROM topics WHERE headline LIKE ? OR body LIKE ? ORDER BY id DESC LIMIT 50');
63 $search_topics->bind_param('ss', $search_query, $search_query);
65 else
67 $search_topics = $link->prepare('SELECT id, time, replies, visits, headline FROM topics WHERE headline LIKE ? ORDER BY id DESC LIMIT 50');
68 $search_topics->bind_param('s', $search_query);
70 $search_topics->execute();
71 $search_topics->store_result();
73 if($search_topics->num_rows > 0)
75 echo '<h4 class="section">Topics</h3>';
77 $search_topics->bind_result($topic_id, $topic_time, $topic_replies, $topic_visits, $topic_headline);
79 $topics = new table();
80 $columns = array(
81 'Headline',
82 'Replies',
83 'Visits',
84 'Age ▼'
86 $topics->define_columns($columns, 'Headline');
87 $topics->add_td_class('Headline', 'topic_headline');
89 while($search_topics->fetch())
91 $values = array
93 '<a href="/topic/' . $topic_id . '">' . str_ireplace( $_GET['q'], '<em class="marked">' . htmlspecialchars($_GET['q']) . '</em>', htmlspecialchars($topic_headline) ) . '</a>',
94 replies($topic_id, $topic_replies),
95 format_number($topic_visits),
96 '<span class="help" title="' . format_date($topic_time) . '">' . calculate_age($topic_time) . '</span>'
99 $topics->row($values);
101 $num_topics_fetched = $topics->num_rows_fetched;
102 echo $topics->output('', true);
104 if($num_topics_fetched == 50)
106 echo '<p class="unimportant">(Tons of results found; stopping here.)</p>';
109 else
111 echo '<p>(No matching topic headlines';
112 if($_GET['deep_search'])
114 echo ' or bodies';
116 echo '.)</p>';
118 $search_topics->close();
120 if($_GET['deep_search'])
122 $search_replies = $link->prepare('SELECT replies.id, replies.parent_id, replies.time, replies.body, topics.headline, topics.time FROM replies INNER JOIN topics ON replies.parent_id = topics.id WHERE replies.body LIKE ? ORDER BY id DESC LIMIT 50');
123 $search_replies->bind_param('s', $search_query);
124 $search_replies->execute();
125 $search_replies->store_result();
127 if($search_replies->num_rows > 0)
129 $search_replies->bind_result($reply_id, $parent_id, $reply_time, $reply_body, $topic_headline, $topic_time);
131 $replies = new table();
132 $columns = array
134 'Reply snippet',
135 'Topic',
136 'Age ▼'
138 $replies->define_columns($columns, 'Topic');
139 $replies->add_td_class('Topic', 'topic_headline');
140 $replies->add_td_class('Reply snippet', 'reply_body_snippet');
142 while($search_replies->fetch())
144 $values = array
146 '<a href="/topic/' . $parent_id . '#reply_' . $reply_id . '">' . str_ireplace( $_GET['q'], '<em class="marked">' . htmlspecialchars($_GET['q']) . '</em>', snippet($reply_body) ) .'</a>',
147 '<a href="/topic/' . $parent_id . '">' . htmlspecialchars($topic_headline) . '</a> <span class="help unimportant" title="' . format_date($topic_time) . '">(' . calculate_age($topic_time) . ' old)</span>',
148 '<span class="help" title="' . format_date($reply_time) . '">' . calculate_age($reply_time) . '</span>'
151 $replies->row($values);
153 $num_replies_fetched = $replies->num_rows_fetched;
154 echo $replies->output('', true);
157 if($num_replies_fetched == 50)
159 echo '<p class="unimportant">(Tons of results found; stopping here.)</p>';
162 else
164 echo '<p>(No matching replies.)</p>';
166 $search_replies->close();
171 print_errors();
173 require('includes/footer.php');