]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SearchSub/classes/SearchSub.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / SearchSub / classes / SearchSub.php
1 <?php
2 /**
3  * Data class to store local search subscriptions
4  *
5  * PHP version 5
6  *
7  * @category SearchSubPlugin
8  * @package  StatusNet
9  * @author   Brion Vibber <brion@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the search subscriptions
36  *
37  * @category PollPlugin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45
46 class SearchSub extends Managed_DataObject
47 {
48     public $__table = 'searchsub'; // table name
49     public $search;         // text
50     public $profile_id;  // int -> profile.id
51     public $created;     // datetime
52
53     /**
54      * The One True Thingy that must be defined and declared.
55      */
56     public static function schemaDef()
57     {
58         return array(
59             'description' => 'SearchSubPlugin search subscription records',
60             'fields' => array(
61                 'search' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'hash search associated with this subscription'),
62                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile ID of subscribing user'),
63                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
64             ),
65             'primary key' => array('search', 'profile_id'),
66             'foreign keys' => array(
67                 'searchsub_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
68             ),
69             'indexes' => array(
70                 'searchsub_created_idx' => array('created'),
71                 'searchsub_profile_id_tag_idx' => array('profile_id', 'search'),
72             ),
73         );
74     }
75
76     /**
77      * Start a search subscription!
78      *
79      * @param profile $profile subscriber
80      * @param string $search subscribee
81      * @return SearchSub
82      */
83     static function start(Profile $profile, $search)
84     {
85         $ts = new SearchSub();
86         $ts->search = $search;
87         $ts->profile_id = $profile->id;
88         $ts->created = common_sql_now();
89         $ts->insert();
90         self::blow('searchsub:by_profile:%d', $profile->id);
91         return $ts;
92     }
93
94     /**
95      * End a search subscription!
96      *
97      * @param profile $profile subscriber
98      * @param string $search subscribee
99      */
100     static function cancel(Profile $profile, $search)
101     {
102         $ts = SearchSub::pkeyGet(array('search' => $search,
103                                     'profile_id' => $profile->id));
104         if ($ts) {
105             $ts->delete();
106             self::blow('searchsub:by_profile:%d', $profile->id);
107         }
108     }
109
110     static function forProfile(Profile $profile)
111     {
112         $searches = array();
113
114         $keypart = sprintf('searchsub:by_profile:%d', $profile->id);
115         $searchstring = self::cacheGet($keypart);
116         
117         if ($searchstring !== false) {
118                 if (!empty($searchstring)) {
119                 $searches = explode(',', $searchstring);
120                 }
121         } else {
122             $searchsub = new SearchSub();
123             $searchsub->profile_id = $profile->id;
124             $searchsub->selectAdd();
125             $searchsub->selectAdd('search');
126
127             if ($searchsub->find()) {
128                 $searches = $searchsub->fetchAll('search');
129             }
130
131             self::cacheSet($keypart, implode(',', $searches));
132         }
133
134         return $searches;
135     }
136 }