From 5c091dab9be50d6955755c1420c09070ea2da7b6 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sun, 8 Nov 2009 15:29:04 -0800 Subject: Implement /api/account/update_profile_background_image.format --- actions/apiaccountupdateprofilebackgroundimage.php | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 actions/apiaccountupdateprofilebackgroundimage.php (limited to 'actions/apiaccountupdateprofilebackgroundimage.php') diff --git a/actions/apiaccountupdateprofilebackgroundimage.php b/actions/apiaccountupdateprofilebackgroundimage.php new file mode 100644 index 000000000..26d55d448 --- /dev/null +++ b/actions/apiaccountupdateprofilebackgroundimage.php @@ -0,0 +1,170 @@ +. + * + * @category API + * @package StatusNet + * @author Zach Copley + * @copyright 2009 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +require_once INSTALLDIR . '/lib/apiauth.php'; + +/** + * Update the authenticating user's profile background image + * + * @category API + * @package StatusNet + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://status.net/ + */ + +class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction +{ + + var $tile = false; + + /** + * Take arguments for running + * + * @param array $args $_REQUEST args + * + * @return boolean success flag + * + */ + + function prepare($args) + { + parent::prepare($args); + + $this->user = $this->auth_user; + $this->tile = $this->arg('tile'); + + return true; + } + + /** + * Handle the request + * + * Check whether the credentials are valid and output the result + * + * @param array $args $_REQUEST data (unused) + * + * @return void + */ + + function handle($args) + { + parent::handle($args); + + if ($_SERVER['REQUEST_METHOD'] != 'POST') { + $this->clientError( + _('This method requires a POST.'), + 400, $this->format + ); + return; + } + + // Workaround for PHP returning empty $_POST and $_FILES when POST + // length > post_max_size in php.ini + + if (empty($_FILES) + && empty($_POST) + && ($_SERVER['CONTENT_LENGTH'] > 0) + ) { + $msg = _('The server was unable to handle that much POST ' . + 'data (%s bytes) due to its current configuration.'); + + $this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); + return; + } + + if (empty($this->user)) { + $this->clientError(_('No such user!'), 404, $this->format); + return; + } + + try { + $imagefile = ImageFile::fromUpload('image'); + } catch (Exception $e) { + $this->clientError($e->getMessage(), 400, $this->format); + return; + } + + $design = $this->user->getDesign(); + + $filename = Design::filename( + $design->id, + image_type_to_extension($imagefile->type), + common_timestamp() + ); + + $filepath = Design::path($filename); + + move_uploaded_file($imagefile->filepath, $filepath); + + // delete any old backround img laying around + + if (isset($design->backgroundimage)) { + @unlink(Design::path($design->backgroundimage)); + } + + $original = clone($design); + + $design->backgroundimage = $filename; + + $design->setDisposition(true, false, !empty($this->tile)); + + $result = $design->update($original); + + if ($result === false) { + common_log_db_error($design, 'UPDATE', __FILE__); + $this->showForm(_('Couldn\'t update your design.')); + return; + } + + $profile = $this->user->getProfile(); + + if (empty($profile)) { + $this->clientError(_('User has no profile.')); + return; + } + + $twitter_user = $this->twitterUserArray($this->user->getProfile(), true); + + if ($this->format == 'xml') { + $this->initDocument('xml'); + $this->showTwitterXmlUser($twitter_user); + $this->endDocument('xml'); + } elseif ($this->format == 'json') { + $this->initDocument('json'); + $this->showJsonObjects($twitter_user); + $this->endDocument('json'); + } + } + +} -- cgit v1.2.3-54-g00ecf From c8bd6d9f7afde597f3b404acb05622a73f3738f7 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 9 Nov 2009 23:56:02 -0800 Subject: Make /api/account/update_profile_background_image.format work even when there isn't an existing Design for the user. Plus a few other fixups. --- actions/apiaccountratelimitstatus.php | 2 +- actions/apiaccountupdateprofile.php | 11 ++++- actions/apiaccountupdateprofilebackgroundimage.php | 57 +++++++++++++++++++--- actions/apiaccountupdateprofilecolors.php | 17 +++++-- actions/apiaccountupdateprofileimage.php | 4 +- 5 files changed, 75 insertions(+), 16 deletions(-) (limited to 'actions/apiaccountupdateprofilebackgroundimage.php') diff --git a/actions/apiaccountratelimitstatus.php b/actions/apiaccountratelimitstatus.php index 96179f175..1a5afd552 100644 --- a/actions/apiaccountratelimitstatus.php +++ b/actions/apiaccountratelimitstatus.php @@ -67,7 +67,7 @@ class ApiAccountRateLimitStatusAction extends ApiBareAuthAction if (!in_array($this->format, array('xml', 'json'))) { $this->clientError( - _('API method not found!'), + _('API method not found.'), 404, $this->format ); diff --git a/actions/apiaccountupdateprofile.php b/actions/apiaccountupdateprofile.php index 8af0fb866..fd4384a25 100644 --- a/actions/apiaccountupdateprofile.php +++ b/actions/apiaccountupdateprofile.php @@ -92,6 +92,15 @@ class ApiAccountUpdateProfileAction extends ApiAuthAction return; } + if (!in_array($this->format, array('xml', 'json'))) { + $this->clientError( + _('API method not found.'), + 404, + $this->format + ); + return; + } + if (empty($this->user)) { $this->clientError(_('No such user.'), 404, $this->format); return; @@ -135,7 +144,7 @@ class ApiAccountUpdateProfileAction extends ApiAuthAction if (!$result) { common_log_db_error($profile, 'UPDATE', __FILE__); - $this->serverError(_('Couldn\'t save profile.')); + $this->serverError(_('Could not save profile.')); return; } diff --git a/actions/apiaccountupdateprofilebackgroundimage.php b/actions/apiaccountupdateprofilebackgroundimage.php index 26d55d448..3537b9f97 100644 --- a/actions/apiaccountupdateprofilebackgroundimage.php +++ b/actions/apiaccountupdateprofilebackgroundimage.php @@ -89,6 +89,15 @@ class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction return; } + if (!in_array($this->format, array('xml', 'json'))) { + $this->clientError( + _('API method not found.'), + 404, + $this->format + ); + return; + } + // Workaround for PHP returning empty $_POST and $_FILES when POST // length > post_max_size in php.ini @@ -104,10 +113,46 @@ class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction } if (empty($this->user)) { - $this->clientError(_('No such user!'), 404, $this->format); + $this->clientError(_('No such user.'), 404, $this->format); return; } + $design = $this->user->getDesign(); + + // XXX: This is kinda gross, but before we can add a background + // img we have to make sure there's a Design because design ID + // is part of the img filename. + + if (empty($design)) { + + $this->user->query('BEGIN'); + + // save new design + $design = new Design(); + $id = $design->insert(); + + if (empty($id)) { + common_log_db_error($id, 'INSERT', __FILE__); + $this->clientError(_('Unable to save your design settings.')); + return; + } + + $original = clone($this->user); + $this->user->design_id = $id; + $result = $this->user->update($original); + + if (empty($result)) { + common_log_db_error($original, 'UPDATE', __FILE__); + $this->clientError(_('Unable to save your design settings.')); + $this->user->query('ROLLBACK'); + return; + } + + $this->user->query('COMMIT'); + } + + // Okay, now get the image and add it to the design + try { $imagefile = ImageFile::fromUpload('image'); } catch (Exception $e) { @@ -115,8 +160,6 @@ class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction return; } - $design = $this->user->getDesign(); - $filename = Design::filename( $design->id, image_type_to_extension($imagefile->type), @@ -134,16 +177,14 @@ class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction } $original = clone($design); - $design->backgroundimage = $filename; - - $design->setDisposition(true, false, !empty($this->tile)); + $design->setDisposition(true, false, ($this->tile == 'true')); $result = $design->update($original); if ($result === false) { common_log_db_error($design, 'UPDATE', __FILE__); - $this->showForm(_('Couldn\'t update your design.')); + $this->showForm(_('Could not update your design.')); return; } @@ -154,7 +195,7 @@ class ApiAccountUpdateProfileBackgroundImageAction extends ApiAuthAction return; } - $twitter_user = $this->twitterUserArray($this->user->getProfile(), true); + $twitter_user = $this->twitterUserArray($profile, true); if ($this->format == 'xml') { $this->initDocument('xml'); diff --git a/actions/apiaccountupdateprofilecolors.php b/actions/apiaccountupdateprofilecolors.php index d7d2161fe..3cac82974 100644 --- a/actions/apiaccountupdateprofilecolors.php +++ b/actions/apiaccountupdateprofilecolors.php @@ -113,6 +113,15 @@ class ApiAccountUpdateProfileColorsAction extends ApiAuthAction return; } + if (!in_array($this->format, array('xml', 'json'))) { + $this->clientError( + _('API method not found.'), + 404, + $this->format + ); + return; + } + $design = $this->user->getDesign(); if (!empty($design)) { @@ -130,7 +139,7 @@ class ApiAccountUpdateProfileColorsAction extends ApiAuthAction if ($result === false) { common_log_db_error($design, 'UPDATE', __FILE__); - $this->clientError(_('Couldn\'t update your design.')); + $this->clientError(_('Could not update your design.')); return; } @@ -152,7 +161,7 @@ class ApiAccountUpdateProfileColorsAction extends ApiAuthAction if (empty($id)) { common_log_db_error($id, 'INSERT', __FILE__); - $this->clientError(_('Unable to save your design settings!')); + $this->clientError(_('Unable to save your design settings.')); return; } @@ -162,7 +171,7 @@ class ApiAccountUpdateProfileColorsAction extends ApiAuthAction if (empty($result)) { common_log_db_error($original, 'UPDATE', __FILE__); - $this->clientError(_('Unable to save your design settings!')); + $this->clientError(_('Unable to save your design settings.')); $this->user->query('ROLLBACK'); return; } @@ -177,7 +186,7 @@ class ApiAccountUpdateProfileColorsAction extends ApiAuthAction return; } - $twitter_user = $this->twitterUserArray($this->user->getProfile(), true); + $twitter_user = $this->twitterUserArray($profile, true); if ($this->format == 'xml') { $this->initDocument('xml'); diff --git a/actions/apiaccountupdateprofileimage.php b/actions/apiaccountupdateprofileimage.php index 72fb361bf..153ef7818 100644 --- a/actions/apiaccountupdateprofileimage.php +++ b/actions/apiaccountupdateprofileimage.php @@ -102,7 +102,7 @@ class ApiAccountUpdateProfileImageAction extends ApiAuthAction } if (empty($this->user)) { - $this->clientError(_('No such user!'), 404, $this->format); + $this->clientError(_('No such user.'), 404, $this->format); return; } @@ -135,7 +135,7 @@ class ApiAccountUpdateProfileImageAction extends ApiAuthAction common_broadcast_profile($profile); - $twitter_user = $this->twitterUserArray($this->user->getProfile(), true); + $twitter_user = $this->twitterUserArray($profile, true); if ($this->format == 'xml') { $this->initDocument('xml'); -- cgit v1.2.3-54-g00ecf