]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdatedeliverydevice.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / actions / apiaccountupdatedeliverydevice.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update the authenticating user notification channels
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  API
23  * @package   StatusNet
24  * @author    Siebrand Mazeland <s.mazeland@xs4all.nl>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 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')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/apiauth.php';
36
37 /**
38  * Sets which channel (device) StatusNet delivers updates to for
39  * the authenticating user. Sending none as the device parameter
40  * will disable IM and/or SMS updates.
41  *
42  * @category API
43  * @package  StatusNet
44  * @author   Zach Copley <zach@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class ApiAccountUpdateDeliveryDeviceAction extends ApiAuthAction
50 {
51     /**
52      * Take arguments for running
53      *
54      * @param array $args $_REQUEST args
55      *
56      * @return boolean success flag
57      *
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         $this->user   = $this->auth_user;
65         $this->device = $this->trimmed('device');
66
67         return true;
68     }
69
70     /**
71      * Handle the request
72      *
73      * See which request params have been set, and update the user settings
74      *
75      * @param array $args $_REQUEST data (unused)
76      *
77      * @return void
78      */
79
80     function handle($args)
81     {
82         parent::handle($args);
83
84         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
85             $this->clientError(
86                 // TRANS: Client error message. POST is a HTTP command. It should not be translated.
87                 _('This method requires a POST.'),
88                 400, $this->format
89             );
90             return;
91         }
92
93         if (!in_array($this->format, array('xml', 'json'))) {
94             $this->clientError(
95                 _('API method not found.'),
96                 404,
97                 $this->format
98             );
99             return;
100         }
101
102         // Note: Twitter no longer supports IM
103
104         if (!in_array(strtolower($this->device), array('sms', 'im', 'none'))) {
105             $this->clientError(
106                 _(
107                     'You must specify a parameter named ' .
108                     '\'device\' with a value of one of: sms, im, none.'
109                 )
110             );
111             return;
112         }
113
114         if (empty($this->user)) {
115             $this->clientError(_('No such user.'), 404, $this->format);
116             return;
117         }
118
119         $original = clone($this->user);
120
121         if (strtolower($this->device) == 'sms') {
122             $this->user->smsnotify = true;
123         } elseif (strtolower($this->device) == 'im') {
124             $this->user->jabbernotify = true;
125         } elseif (strtolower($this->device == 'none')) {
126             $this->user->smsnotify    = false;
127             $this->user->jabbernotify = false;
128         }
129
130         $result = $this->user->update($original);
131
132         if ($result === false) {
133             common_log_db_error($this->user, 'UPDATE', __FILE__);
134             $this->serverError(_('Could not update user.'));
135             return;
136         }
137
138         $profile = $this->user->getProfile();
139
140         $twitter_user = $this->twitterUserArray($profile, true);
141
142         // Note: this Twitter API method is retarded because it doesn't give
143         // any success/failure information. Twitter's docs claim that the
144         // notification field will change to reflect notification choice,
145         // but that's not true; notification> is used to indicate
146         // whether the auth user is following the user in question.
147
148         if ($this->format == 'xml') {
149             $this->initDocument('xml');
150             $this->showTwitterXmlUser($twitter_user);
151             $this->endDocument('xml');
152         } elseif ($this->format == 'json') {
153             $this->initDocument('json');
154             $this->showJsonObjects($twitter_user);
155             $this->endDocument('json');
156         }
157     }
158
159 }