]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountupdateprofile.php
Merge branch 'master' of gitorious.org:social/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 /**
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         if (!empty($this->name)) {
100             $profile->fullname = $this->name;
101         }
102
103         if (!empty($this->url)) {
104             $profile->homepage = $this->url;
105         }
106
107         if (!empty($this->description)) {
108             $profile->bio = $this->description;
109         }
110
111         if (!empty($this->location)) {
112             $profile->location = $this->location;
113
114             $loc = Location::fromName($this->location);
115
116             if (!empty($loc)) {
117                 $profile->lat         = $loc->lat;
118                 $profile->lon         = $loc->lon;
119                 $profile->location_id = $loc->location_id;
120                 $profile->location_ns = $loc->location_ns;
121             }
122         }
123
124         $result = $profile->update($original);
125
126         if (!$result) {
127             common_log_db_error($profile, 'UPDATE', __FILE__);
128             // TRANS: Server error displayed if a user profile could not be saved.
129             $this->serverError(_('Could not save profile.'));
130         }
131
132         $twitter_user = $this->twitterUserArray($profile, true);
133
134         if ($this->format == 'xml') {
135             $this->initDocument('xml');
136             $this->showTwitterXmlUser($twitter_user, 'user', true);
137             $this->endDocument('xml');
138         } elseif ($this->format == 'json') {
139             $this->initDocument('json');
140             $this->showJsonObjects($twitter_user);
141             $this->endDocument('json');
142         }
143     }
144 }