]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofile.php
Don't accept non-objects before testing with "instanceof".
[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     protected $needPost = true;
47
48     /**
49      * Take arguments for running
50      *
51      * @param array $args $_REQUEST args
52      *
53      * @return boolean success flag
54      */
55     protected function prepare(array $args=array())
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      * @return void
75      */
76     protected function handle()
77     {
78         parent::handle();
79
80         if (!in_array($this->format, array('xml', 'json'))) {
81             // TRANS: Client error displayed when coming across a non-supported API method.
82             $this->clientError(_('API method not found.'), 404);
83         }
84
85         if (empty($this->user)) {
86             // TRANS: Client error displayed if a user could not be found.
87             $this->clientError(_('No such user.'), 404);
88         }
89
90         $profile = $this->user->getProfile();
91
92         if (empty($profile)) {
93             // TRANS: Error message displayed when referring to a user without a profile.
94             $this->clientError(_('User has no profile.'));
95         }
96
97         $original = clone($profile);
98
99         $profile->fullname = $this->name;
100         $profile->homepage = $this->url;
101         $profile->bio = $this->description;
102         $profile->location = $this->location;
103
104         if (!empty($this->location)) {
105             $loc = Location::fromName($this->location);
106
107             if (!empty($loc)) {
108                 $profile->lat         = $loc->lat;
109                 $profile->lon         = $loc->lon;
110                 $profile->location_id = $loc->location_id;
111                 $profile->location_ns = $loc->location_ns;
112             }
113         } else {
114             // location is empty so reset the extrapolated information too
115             $profile->lat = '';
116             $profile->lon = '';
117             $profile->location_id = '';
118             $profile->location_ns = '';
119         }
120
121         $result = $profile->update($original);
122
123         if (!$result) {
124             common_log_db_error($profile, 'UPDATE', __FILE__);
125             // TRANS: Server error displayed if a user profile could not be saved.
126             $this->serverError(_('Could not save profile.'));
127         }
128
129         $twitter_user = $this->twitterUserArray($profile, true);
130
131         if ($this->format == 'xml') {
132             $this->initDocument('xml');
133             $this->showTwitterXmlUser($twitter_user, 'user', true);
134             $this->endDocument('xml');
135         } elseif ($this->format == 'json') {
136             $this->initDocument('json');
137             $this->showJsonObjects($twitter_user);
138             $this->endDocument('json');
139         }
140     }
141 }