]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SlicedFavorites/actions/favoritedslice.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / SlicedFavorites / actions / favoritedslice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List of popular notices
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Public
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 class FavoritedSliceAction extends FavoritedAction
36 {
37     private $includeUsers = array(), $excludeUsers = array();
38
39     /**
40      * Take arguments for running
41      *
42      * @param array $args $_REQUEST args
43      *
44      * @return boolean success flag
45      *
46      * @todo move queries from showContent() to here
47      */
48     function prepare($args)
49     {
50         parent::prepare($args);
51
52         $this->slice = $this->arg('slice', 'default');
53         $data = array();
54         if (Event::handle('SlicedFavoritesGetSettings', array($this->slice, &$data))) {
55             // TRANS: Client exception.
56             throw new ClientException(_m('Unknown favorites slice.'));
57         }
58         if (isset($data['include'])) {
59             $this->includeUsers = $data['include'];
60         }
61         if (isset($data['exclude'])) {
62             $this->excludeUsers = $data['exclude'];
63         }
64
65         return true;
66     }
67
68     /**
69      * Content area
70      *
71      * Shows the list of popular notices
72      *
73      * @return void
74      */
75     function showContent()
76     {
77         $slice = $this->sliceWhereClause();
78         if (!$slice) {
79             return parent::showContent();
80         }
81
82         $weightexpr = common_sql_weight('fave.modified', common_config('popular', 'dropoff'));
83         $cutoff = sprintf("fave.modified > '%s'",
84                           common_sql_date(time() - common_config('popular', 'cutoff')));
85
86         $qry = 'SELECT notice.*, '.
87           $weightexpr . ' as weight ' .
88           'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
89           "WHERE $cutoff AND $slice " .
90           'GROUP BY id,profile_id,uri,content,rendered,url,created,notice.modified,reply_to,is_local,source,notice.conversation ' .
91           'ORDER BY weight DESC';
92
93         $offset = ($this->page - 1) * NOTICES_PER_PAGE;
94         $limit  = NOTICES_PER_PAGE + 1;
95
96         if (common_config('db', 'type') == 'pgsql') {
97             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
98         } else {
99             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
100         }
101
102         $notice = Memcached_DataObject::cachedQuery('Notice',
103                                                     $qry,
104                                                     600);
105
106         $nl = new NoticeList($notice, $this);
107
108         $cnt = $nl->show();
109
110         if ($cnt == 0) {
111             $this->showEmptyList();
112         }
113
114         $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE,
115                           $this->page, 'favorited');
116     }
117
118     private function sliceWhereClause()
119     {
120         $include = $this->nicknamesToIds($this->includeUsers);
121         $exclude = $this->nicknamesToIds($this->excludeUsers);
122
123         if (count($include) == 1) {
124             return "profile_id = " . intval($include[0]);
125         } else if (count($include) > 1) {
126             return "profile_id IN (" . implode(',', $include) . ")";
127         } else if (count($exclude) == 1) {
128             return "profile_id != " . intval($exclude[0]);
129         } else if (count($exclude) > 1) {
130             return "profile_id NOT IN (" . implode(',', $exclude) . ")";
131         } else {
132             return false;
133         }
134     }
135
136     /**
137      *
138      * @param array $nicks array of user nicknames
139      * @return array of profile/user IDs
140      */
141     private function nicknamesToIds($nicks)
142     {
143         $ids = array();
144         foreach ($nicks as $nick) {
145             // not the most efficient way for a big list!
146             $user = User::getKV('nickname', $nick);
147             if ($user) {
148                 $ids[] = intval($user->id);
149             }
150         }
151         return $ids;
152     }
153 }