]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofile.php
OAuth related syntax fixes, nothing big
[quix0rs-gnu-social.git] / actions / apiaccountupdateprofile.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Update the authenticating user's profile
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 /**
35  * API analog to the profile settings page
36  * Only the parameters specified will be updated.
37  *
38  * @category API
39  * @package  StatusNet
40  * @author   Zach Copley <zach@status.net>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://status.net/
43  */
44 class ApiAccountUpdateProfileAction extends ApiAuthAction
45 {
46     /**
47      * Take arguments for running
48      *
49      * @param array $args $_REQUEST args
50      *
51      * @return boolean success flag
52      */
53     function prepare($args)
54     {
55         parent::prepare($args);
56
57         $this->user = $this->auth_user;
58
59         $this->name        = $this->trimmed('name');
60         $this->url         = $this->trimmed('url');
61         $this->location    = $this->trimmed('location');
62         $this->description = $this->trimmed('description');
63
64         return true;
65     }
66
67     /**
68      * Handle the request
69      *
70      * See which request params have been set, and update the profile
71      *
72      * @param array $args $_REQUEST data (unused)
73      *
74      * @return void
75      */
76     function handle($args)
77     {
78         parent::handle($args);
79
80         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
81             $this->clientError(
82                 // TRANS: Client error. POST is a HTTP command. It should not be translated.
83                 _('This method requires a POST.'),
84                 400, $this->format
85             );
86             return;
87         }
88
89         if (!in_array($this->format, array('xml', 'json'))) {
90             $this->clientError(
91                 // TRANS: Client error displayed when coming across a non-supported API method.
92                 _('API method not found.'),
93                 404,
94                 $this->format
95             );
96             return;
97         }
98
99         if (empty($this->user)) {
100             // TRANS: Client error displayed if a user could not be found.
101             $this->clientError(_('No such user.'), 404, $this->format);
102             return;
103         }
104
105         $profile = $this->user->getProfile();
106
107         if (empty($profile)) {
108             // TRANS: Error message displayed when referring to a user without a profile.
109             $this->clientError(_('User has no profile.'));
110             return;
111         }
112
113         $original = clone($profile);
114
115         if (!empty($this->name)) {
116             $profile->fullname = $this->name;
117         }
118
119         if (!empty($this->url)) {
120             $profile->homepage = $this->url;
121         }
122
123         if (!empty($this->description)) {
124             $profile->bio = $this->description;
125         }
126
127         if (!empty($this->location)) {
128             $profile->location = $this->location;
129
130             $loc = Location::fromName($location);
131
132             if (!empty($loc)) {
133                 $profile->lat         = $loc->lat;
134                 $profile->lon         = $loc->lon;
135                 $profile->location_id = $loc->location_id;
136                 $profile->location_ns = $loc->location_ns;
137             }
138         }
139
140         $result = $profile->update($original);
141
142         if (!$result) {
143             common_log_db_error($profile, 'UPDATE', __FILE__);
144             // TRANS: Server error displayed if a user profile could not be saved.
145             $this->serverError(_('Could not save profile.'));
146             return;
147         }
148
149         common_broadcast_profile($profile);
150
151         $twitter_user = $this->twitterUserArray($profile, true);
152
153         if ($this->format == 'xml') {
154             $this->initDocument('xml');
155             $this->showTwitterXmlUser($twitter_user, 'user', true);
156             $this->endDocument('xml');
157         } elseif ($this->format == 'json') {
158             $this->initDocument('json');
159             $this->showJsonObjects($twitter_user);
160             $this->endDocument('json');
161         }
162     }
163 }