]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/SearchSubPlugin.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / SearchSub / SearchSubPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A plugin to enable local tab subscription
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  SearchSubPlugin
24  * @package   StatusNet
25  * @author    Brion Vibber <brion@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * SearchSub plugin main class
37  *
38  * @category  SearchSubPlugin
39  * @package   StatusNet
40  * @author    Brion Vibber <brionv@status.net>
41  * @copyright 2011 StatusNet, Inc.
42  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
43  * @link      http://status.net/
44  */
45 class SearchSubPlugin extends Plugin
46 {
47     const VERSION         = '0.1';
48
49     /**
50      * Database schema setup
51      *
52      * @see Schema
53      *
54      * @return boolean hook value; true means continue processing, false means stop.
55      */
56     function onCheckSchema()
57     {
58         $schema = Schema::get();
59         $schema->ensureTable('searchsub', SearchSub::schemaDef());
60         return true;
61     }
62
63     /**
64      * Map URLs to actions
65      *
66      * @param URLMapper $m path-to-action mapper
67      *
68      * @return boolean hook value; true means continue processing, false means stop.
69      */
70     public function onRouterInitialized(URLMapper $m)
71     {
72         $m->connect('search/:search/subscribe',
73                     array('action' => 'searchsub'),
74                     array('search' => Router::REGEX_TAG));
75         $m->connect('search/:search/unsubscribe',
76                     array('action' => 'searchunsub'),
77                     array('search' => Router::REGEX_TAG));
78         $m->connect(':nickname/search-subscriptions',
79                     array('action' => 'searchsubs'),
80                     array('nickname' => Nickname::DISPLAY_FMT));
81         return true;
82     }
83
84     /**
85      * Plugin version data
86      *
87      * @param array &$versions array of version data
88      *
89      * @return value
90      */
91     function onPluginVersion(array &$versions)
92     {
93         $versions[] = array('name' => 'SearchSub',
94                             'version' => self::VERSION,
95                             'author' => 'Brion Vibber',
96                             'homepage' => 'http://status.net/wiki/Plugin:SearchSub',
97                             'rawdescription' =>
98                             // TRANS: Plugin description.
99                             _m('Plugin to allow following all messages with a given search.'));
100         return true;
101     }
102
103     /**
104      * Hook inbox delivery setup so search subscribers receive all
105      * notices with that search in their inbox.
106      *
107      * Currently makes no distinction between local messages and
108      * remote ones which happen to come in to the system. Remote
109      * notices that don't come in at all won't ever reach this.
110      *
111      * @param Notice $notice
112      * @param array $ni in/out map of profile IDs to inbox constants
113      * @return boolean hook result
114      */
115     function onStartNoticeWhoGets(Notice $notice, array &$ni)
116     {
117         // Warning: this is potentially very slow
118         // with a lot of searches!
119         $sub = new SearchSub();
120         $sub->groupBy('search');
121         $sub->find();
122         while ($sub->fetch()) {
123             $search = $sub->search;
124
125             if ($this->matchSearch($notice, $search)) {
126                 // Match? Find all those who subscribed to this
127                 // search term and get our delivery on...
128                 $searchsub = new SearchSub();
129                 $searchsub->search = $search;
130                 $searchsub->find();
131
132                 while ($searchsub->fetch()) {
133                     // These constants are currently not actually used, iirc
134                     $ni[$searchsub->profile_id] = NOTICE_INBOX_SOURCE_SUB;
135                 }
136             }
137         }
138         return true;
139     }
140
141     /**
142      * Does the given notice match the given fulltext search query?
143      *
144      * Warning: not guaranteed to match other search engine behavior, etc.
145      * Currently using a basic case-insensitive substring match, which
146      * probably fits with the 'LIKE' search but not the default MySQL
147      * or Sphinx search backends.
148      *
149      * @param Notice $notice
150      * @param string $search
151      * @return boolean
152      */
153     function matchSearch(Notice $notice, $search)
154     {
155         return (mb_stripos($notice->content, $search) !== false);
156     }
157
158     /**
159      *
160      * @param NoticeSearchAction $action
161      * @param string $q
162      * @param Notice $notice
163      * @return boolean hook result
164      */
165     function onStartNoticeSearchShowResults($action, $q, $notice)
166     {
167         $user = common_current_user();
168         if ($user) {
169             $search = $q;
170             $searchsub = SearchSub::pkeyGet(array('search' => $search,
171                                                   'profile_id' => $user->id));
172             if ($searchsub) {
173                 $form = new SearchUnsubForm($action, $search);
174             } else {
175                 $form = new SearchSubForm($action, $search);
176             }
177             $action->elementStart('div', 'entity_actions');
178             $action->elementStart('ul');
179             $action->elementStart('li', 'entity_subscribe');
180             $form->show();
181             $action->elementEnd('li');
182             $action->elementEnd('ul');
183             $action->elementEnd('div');
184         }
185         return true;
186     }
187
188     /**
189      * Menu item for personal subscriptions/groups area
190      *
191      * @param Widget $widget Widget being executed
192      *
193      * @return boolean hook return
194      */
195     function onEndSubGroupNav($widget)
196     {
197         $action = $widget->out;
198         $action_name = $action->trimmed('action');
199
200         $action->menuItem(common_local_url('searchsubs', array('nickname' => $action->user->nickname)),
201                           // TRANS: SearchSub plugin menu item on user settings page.
202                           _m('MENU', 'Searches'),
203                           // TRANS: SearchSub plugin tooltip for user settings menu item.
204                           _m('Configure search subscriptions'),
205                           $action_name == 'searchsubs' && $action->arg('nickname') == $action->user->nickname);
206
207         return true;
208     }
209
210     /**
211      * Replace the built-in stub track commands with ones that control
212      * search subscriptions.
213      *
214      * @param CommandInterpreter $cmd
215      * @param string $arg
216      * @param User $user
217      * @param Command $result
218      * @return boolean hook result
219      */
220     function onEndInterpretCommand($cmd, $arg, $user, &$result)
221     {
222         if ($result instanceof TrackCommand) {
223             $result = new SearchSubTrackCommand($user, $arg);
224             return false;
225         } else if ($result instanceof TrackOffCommand) {
226             $result = new SearchSubTrackOffCommand($user);
227             return false;
228         } else if ($result instanceof TrackingCommand) {
229             $result = new SearchSubTrackingCommand($user);
230             return false;
231         } else if ($result instanceof UntrackCommand) {
232             $result = new SearchSubUntrackCommand($user, $arg);
233             return false;
234         } else {
235             return true;
236         }
237     }
238
239     function onHelpCommandMessages($cmd, &$commands)
240     {
241         // TRANS: Help message for IM/SMS command "track <word>"
242         $commands["track <word>"] = _m('COMMANDHELP', "Start following notices matching the given search query.");
243         // TRANS: Help message for IM/SMS command "untrack <word>"
244         $commands["untrack <word>"] = _m('COMMANDHELP', "Stop following notices matching the given search query.");
245         // TRANS: Help message for IM/SMS command "track off"
246         $commands["track off"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
247         // TRANS: Help message for IM/SMS command "untrack all"
248         $commands["untrack all"] = _m('COMMANDHELP', "Disable all tracked search subscriptions.");
249         // TRANS: Help message for IM/SMS command "tracks"
250         $commands["tracks"] = _m('COMMANDHELP', "List all your search subscriptions.");
251         // TRANS: Help message for IM/SMS command "tracking"
252         $commands["tracking"] = _m('COMMANDHELP', "List all your search subscriptions.");
253     }
254
255     function onEndDefaultLocalNav($menu, $user)
256     {
257         $user = common_current_user();
258
259         if (!empty($user)) {
260             $searches = SearchSub::forProfile($user->getProfile());
261
262             if (!empty($searches) && count($searches) > 0) {
263                 $searchSubMenu = new SearchSubMenu($menu->out, $user, $searches);
264                 // TRANS: Sub menu for searches.
265                 $menu->submenu(_m('MENU','Searches'), $searchSubMenu);
266             }
267         }
268
269         return true;
270     }
271 }