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