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