]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/classes/Poll.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / Poll / classes / Poll.php
1 <?php
2 /**
3  * Data class to mark notices as bookmarks
4  *
5  * PHP version 5
6  *
7  * @category PollPlugin
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 poll options and such
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 Poll extends Managed_DataObject
47 {
48     public $__table = 'poll'; // table name
49     public $id;          // char(36) primary key not null -> UUID
50     public $uri;         // varchar(191)   not 255 because utf8mb4 takes more space
51     public $profile_id;  // int -> profile.id
52     public $question;    // text
53     public $options;     // text; newline(?)-delimited
54     public $created;     // datetime
55
56     /**
57      * The One True Thingy that must be defined and declared.
58      */
59     public static function schemaDef()
60     {
61         return array(
62             'description' => 'Per-notice poll data for Poll plugin',
63             'fields' => array(
64                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
65                 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
66                 'profile_id' => array('type' => 'int'),
67                 'question' => array('type' => 'text'),
68                 'options' => array('type' => 'text'),
69                 'created' => array('type' => 'datetime', 'not null' => true),
70             ),
71             'primary key' => array('id'),
72             'unique keys' => array(
73                 'poll_uri_key' => array('uri'),
74             ),
75         );
76     }
77
78     /**
79      * Get a bookmark based on a notice
80      *
81      * @param Notice $notice Notice to check for
82      *
83      * @return Poll found poll or null
84      */
85     static function getByNotice($notice)
86     {
87         return self::getKV('uri', $notice->uri);
88     }
89
90     function getOptions()
91     {
92         return explode("\n", $this->options);
93     }
94
95     /**
96      * Is this a valid selection index?
97      *
98      * @param numeric $selection (1-based)
99      * @return boolean
100      */
101     function isValidSelection($selection)
102     {
103         if ($selection != intval($selection)) {
104             return false;
105         }
106         if ($selection < 1 || $selection > count($this->getOptions())) {
107             return false;
108         }
109         return true;
110     }
111
112     function getNotice()
113     {
114         return Notice::getKV('uri', $this->uri);
115     }
116
117     function getUrl()
118     {
119         return $this->getNotice()->getUrl();
120     }
121
122     /**
123      * Get the response of a particular user to this poll, if any.
124      *
125      * @param Profile $profile
126      * @return Poll_response object or null
127      */
128     function getResponse(Profile $profile)
129     {
130         $pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
131                                                                            'profile_id' => $profile->id));
132         return $pr;
133     }
134
135     function countResponses()
136     {
137         $pr = new Poll_response();
138         $pr->poll_id = $this->id;
139         $pr->groupBy('selection');
140         $pr->selectAdd('count(profile_id) as votes');
141         $pr->find();
142
143         $raw = array();
144         while ($pr->fetch()) {
145             // Votes list 1-based
146             // Array stores 0-based
147             $raw[$pr->selection - 1] = $pr->votes;
148         }
149
150         $counts = array();
151         foreach (array_keys($this->getOptions()) as $key) {
152             if (isset($raw[$key])) {
153                 $counts[$key] = $raw[$key];
154             } else {
155                 $counts[$key] = 0;
156             }
157         }
158         return $counts;
159     }
160
161     /**
162      * Save a new poll notice
163      *
164      * @param Profile $profile
165      * @param string  $question
166      * @param array   $opts (poll responses)
167      *
168      * @return Notice saved notice
169      */
170     static function saveNew($profile, $question, $opts, $options=null)
171     {
172         if (empty($options)) {
173             $options = array();
174         }
175
176         $p = new Poll();
177
178         $p->id          = UUID::gen();
179         $p->profile_id  = $profile->id;
180         $p->question    = $question;
181         $p->options     = implode("\n", $opts);
182
183         if (array_key_exists('created', $options)) {
184             $p->created = $options['created'];
185         } else {
186             $p->created = common_sql_now();
187         }
188
189         if (array_key_exists('uri', $options)) {
190             $p->uri = $options['uri'];
191         } else {
192             $p->uri = common_local_url('showpoll',
193                                         array('id' => $p->id));
194         }
195
196         common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
197         $p->insert();
198
199         // TRANS: Notice content creating a poll.
200         // TRANS: %1$s is the poll question, %2$s is a link to the poll.
201         $content  = sprintf(_m('Poll: %1$s %2$s'),
202                             $question,
203                             $p->uri);
204         $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
205         // TRANS: Rendered version of the notice content creating a poll.
206         // TRANS: %s is a link to the poll with the question as link description.
207         $rendered = sprintf(_m('Poll: %s'), $link);
208
209         $tags    = array('poll');
210         $replies = array();
211
212         $options = array_merge(array('urls' => array(),
213                                      'rendered' => $rendered,
214                                      'tags' => $tags,
215                                      'replies' => $replies,
216                                      'object_type' => PollPlugin::POLL_OBJECT),
217                                $options);
218
219         if (!array_key_exists('uri', $options)) {
220             $options['uri'] = $p->uri;
221         }
222
223         $saved = Notice::saveNew($profile->id,
224                                  $content,
225                                  array_key_exists('source', $options) ?
226                                  $options['source'] : 'web',
227                                  $options);
228
229         return $saved;
230     }
231 }