]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/noticesearch.php
add csrf protection to profile settings
[quix0rs-gnu-social.git] / actions / noticesearch.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/searchaction.php');
23 define('NOTICES_PER_PAGE', 20);
24
25 # XXX common parent for people and content search?
26
27 class NoticesearchAction extends SearchAction {
28
29         function get_instructions() {
30                 return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
31         }
32
33         function get_title() {
34                 return _('Text search');
35         }
36
37         function show_results($q, $page) {
38
39                 $notice = new Notice();
40
41                 # lcase it for comparison
42                 $q = strtolower($q);
43                 $notice->whereAdd('MATCH(content) against (\''.addslashes($q).'\')');
44
45                 # Ask for an extra to see if there's more.
46
47                 $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
48
49                 $cnt = $notice->find();
50
51                 if ($cnt > 0) {
52                         $terms = preg_split('/[\s,]+/', $q);
53                         common_element_start('ul', array('id' => 'notices'));
54                         for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
55                                 if ($notice->fetch()) {
56                                         $this->show_notice($notice, $terms);
57                                 } else {
58                                         // shouldn't happen!
59                                         break;
60                                 }
61                         }
62                         common_element_end('ul');
63                 } else {
64                         common_element('p', 'error', _('No results'));
65                 }
66
67                 common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
68                                                   $page, 'noticesearch', array('q' => $q));
69         }
70
71         function show_header($arr) {
72                 if ($arr) {
73                         $q = $arr[0];
74                 }
75                 if ($q) {
76                         common_element('link', array('rel' => 'alternate',
77                                                                                  'href' => common_local_url('noticesearchrss',
78                                                                                                                                         array('q' => $q)),
79                                                                                  'type' => 'application/rss+xml',
80                                                                                  'title' => _('Search Stream Feed')));
81                 }
82         }
83
84         # XXX: refactor and combine with StreamAction::show_notice()
85
86         function show_notice($notice, $terms) {
87                 $profile = $notice->getProfile();
88                 if (!$profile) {
89                         common_log_db_error($notice, 'SELECT', __FILE__);
90                         return;
91                 }
92                 # XXX: RDFa
93                 common_element_start('li', array('class' => 'notice_single',
94                                                                                   'id' => 'notice-' . $notice->id));
95                 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
96                 common_element_start('a', array('href' => $profile->profileurl));
97                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_STREAM_SIZE),
98                                                                         'class' => 'avatar stream',
99                                                                         'width' => AVATAR_STREAM_SIZE,
100                                                                         'height' => AVATAR_STREAM_SIZE,
101                                                                         'alt' =>
102                                                                         ($profile->fullname) ? $profile->fullname :
103                                                                         $profile->nickname));
104                 common_element_end('a');
105                 common_element('a', array('href' => $profile->profileurl,
106                                                                   'class' => 'nickname'),
107                                            $profile->nickname);
108                 # FIXME: URL, image, video, audio
109                 common_element_start('p', array('class' => 'content'));
110                 if ($notice->rendered) {
111                         common_raw($this->highlight($notice->rendered, $terms));
112                 } else {
113                         # XXX: may be some uncooked notices in the DB,
114                         # we cook them right now. This should probably disappear in future
115                         # versions (>> 0.4.x)
116                         common_raw($this->highlight(common_render_content($notice->content, $notice), $terms));
117                 }
118                 common_element_end('p');
119                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
120                 common_element_start('p', 'time');
121                 common_element('a', array('class' => 'permalink',
122                                                                   'href' => $noticeurl,
123                                                                   'title' => common_exact_date($notice->created)),
124                                            common_date_string($notice->created));
125                 if ($notice->reply_to) {
126                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
127                         common_text(' (');
128                         common_element('a', array('class' => 'inreplyto',
129                                                                           'href' => $replyurl),
130                                                    _('in reply to...'));
131                         common_text(')');
132                 }
133                 common_element_start('a',
134                                                          array('href' => common_local_url('newnotice',
135                                                                                                                           array('replyto' => $profile->nickname)),
136                                                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
137                                                                    'title' => _('reply'),
138                                                                    'class' => 'replybutton'));
139                 common_raw('&rarr;');
140                 common_element_end('a');
141                 common_element_end('p');
142                 common_element_end('li');
143         }
144
145         function highlight($text, $terms) {
146                 /* Highligh serach terms */
147                 $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
148                 $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
149
150                 /* Remove highlighting from inside links, loop incase multiple highlights in links */
151                 $pattern = '/(href="[^"]*)<strong>('.implode('|',array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
152                 do {
153                         $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
154                 } while ($count);
155                 return $result;
156         }
157 }