Can now edit/change application icon

This commit is contained in:
Zach Copley 2010-01-13 01:22:37 +00:00
parent 7694955cd6
commit adfca01808
3 changed files with 126 additions and 132 deletions

View File

@ -93,47 +93,47 @@ class EditApplicationAction extends OwnerDesignAction
parent::handle($args); parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->handlePost($args); $this->handlePost($args);
} else { } else {
$this->showForm(); $this->showForm();
} }
} }
function handlePost($args) function handlePost($args)
{ {
// Workaround for PHP returning empty $_POST and $_FILES when POST // Workaround for PHP returning empty $_POST and $_FILES when POST
// length > post_max_size in php.ini // length > post_max_size in php.ini
if (empty($_FILES) if (empty($_FILES)
&& empty($_POST) && empty($_POST)
&& ($_SERVER['CONTENT_LENGTH'] > 0) && ($_SERVER['CONTENT_LENGTH'] > 0)
) { ) {
$msg = _('The server was unable to handle that much POST ' . $msg = _('The server was unable to handle that much POST ' .
'data (%s bytes) due to its current configuration.'); 'data (%s bytes) due to its current configuration.');
$this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return; return;
} }
// CSRF protection // CSRF protection
$token = $this->trimmed('token'); $token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { if (!$token || $token != common_session_token()) {
$this->clientError(_('There was a problem with your session token.')); $this->clientError(_('There was a problem with your session token.'));
return; return;
} }
$cur = common_current_user(); $cur = common_current_user();
if ($this->arg('cancel')) { if ($this->arg('cancel')) {
common_redirect(common_local_url('showapplication', common_redirect(common_local_url('showapplication',
array( array(
'nickname' => $cur->nickname, 'nickname' => $cur->nickname,
'id' => $this->app->id) 'id' => $this->app->id)
), 303); ), 303);
} elseif ($this->arg('save')) { } elseif ($this->arg('save')) {
$this->trySave(); $this->trySave();
} else { } else {
$this->clientError(_('Unexpected form submission.')); $this->clientError(_('Unexpected form submission.'));
} }
} }
function showForm($msg=null) function showForm($msg=null)
@ -170,8 +170,8 @@ class EditApplicationAction extends OwnerDesignAction
$access_type = $this->arg('default_access_type'); $access_type = $this->arg('default_access_type');
if (empty($name)) { if (empty($name)) {
$this->showForm(_('Name is required.')); $this->showForm(_('Name is required.'));
return; return;
} elseif (mb_strlen($name) > 255) { } elseif (mb_strlen($name) > 255) {
$this->showForm(_('Name is too long (max 255 chars).')); $this->showForm(_('Name is too long (max 255 chars).'));
return; return;
@ -181,20 +181,17 @@ class EditApplicationAction extends OwnerDesignAction
} elseif (Oauth_application::descriptionTooLong($description)) { } elseif (Oauth_application::descriptionTooLong($description)) {
$this->showForm(sprintf( $this->showForm(sprintf(
_('Description is too long (max %d chars).'), _('Description is too long (max %d chars).'),
Oauth_application::maxDescription())); Oauth_application::maxDescription()));
return; return;
} elseif (empty($source_url)) { } elseif (mb_strlen($source_url) > 255) {
$this->showForm(_('Source URL is required.')); $this->showForm(_('Source URL is too long.'));
return;
} elseif ((strlen($source_url) > 0)
&& !Validate::uri(
$source_url,
array('allowed_schemes' => array('http', 'https'))
)
)
{
$this->showForm(_('Source URL is not valid.'));
return; return;
} elseif ((mb_strlen($source_url) > 0)
&& !Validate::uri($source_url,
array('allowed_schemes' => array('http', 'https'))))
{
$this->showForm(_('Source URL is not valid.'));
return;
} elseif (empty($organization)) { } elseif (empty($organization)) {
$this->showForm(_('Organization is required.')); $this->showForm(_('Organization is required.'));
return; return;
@ -204,35 +201,30 @@ class EditApplicationAction extends OwnerDesignAction
} elseif (empty($homepage)) { } elseif (empty($homepage)) {
$this->showForm(_('Organization homepage is required.')); $this->showForm(_('Organization homepage is required.'));
return; return;
} elseif ((strlen($homepage) > 0) } elseif ((mb_strlen($homepage) > 0)
&& !Validate::uri( && !Validate::uri($homepage,
$homepage, array('allowed_schemes' => array('http', 'https'))))
array('allowed_schemes' => array('http', 'https')) {
) $this->showForm(_('Homepage is not a valid URL.'));
) return;
{ } elseif (mb_strlen($callback_url) > 255) {
$this->showForm(_('Homepage is not a valid URL.')); $this->showForm(_('Callback is too long.'));
return; return;
} elseif (empty($callback_url)) { } elseif (mb_strlen($callback_url) > 0
$this->showForm(_('Callback is required.')); && !Validate::uri($source_url,
return; array('allowed_schemes' => array('http', 'https'))
} elseif (strlen($callback_url) > 0 ))
&& !Validate::uri( {
$source_url, $this->showForm(_('Callback URL is not valid.'));
array('allowed_schemes' => array('http', 'https')) return;
) }
)
{
$this->showForm(_('Callback URL is not valid.'));
return;
}
$cur = common_current_user(); $cur = common_current_user();
// Checked in prepare() above // Checked in prepare() above
assert(!is_null($cur)); assert(!is_null($cur));
assert(!is_null($this->app)); assert(!is_null($this->app));
$orig = clone($this->app); $orig = clone($this->app);
@ -244,9 +236,7 @@ class EditApplicationAction extends OwnerDesignAction
$this->app->callback_url = $callback_url; $this->app->callback_url = $callback_url;
$this->app->type = $type; $this->app->type = $type;
$result = $this->app->update($orig); common_debug("access_type = $access_type");
common_debug("access_type = $access_type");
if ($access_type == 'r') { if ($access_type == 'r') {
$this->app->access_type = 1; $this->app->access_type = 1;
@ -254,11 +244,15 @@ class EditApplicationAction extends OwnerDesignAction
$this->app->access_type = 3; $this->app->access_type = 3;
} }
$result = $this->app->update($orig);
if (!$result) { if (!$result) {
common_log_db_error($this->app, 'UPDATE', __FILE__); common_log_db_error($this->app, 'UPDATE', __FILE__);
$this->serverError(_('Could not update application.')); $this->serverError(_('Could not update application.'));
} }
$this->app->uploadLogo();
common_redirect(common_local_url('apps', common_redirect(common_local_url('apps',
array('nickname' => $cur->nickname)), 303); array('nickname' => $cur->nickname)), 303);
} }

View File

@ -83,7 +83,7 @@ class NewApplicationAction extends OwnerDesignAction
parent::handle($args); parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->handlePost($args); $this->handlePost($args);
} else { } else {
$this->showForm(); $this->showForm();
} }
@ -91,36 +91,36 @@ class NewApplicationAction extends OwnerDesignAction
function handlePost($args) function handlePost($args)
{ {
// Workaround for PHP returning empty $_POST and $_FILES when POST // Workaround for PHP returning empty $_POST and $_FILES when POST
// length > post_max_size in php.ini // length > post_max_size in php.ini
if (empty($_FILES) if (empty($_FILES)
&& empty($_POST) && empty($_POST)
&& ($_SERVER['CONTENT_LENGTH'] > 0) && ($_SERVER['CONTENT_LENGTH'] > 0)
) { ) {
$msg = _('The server was unable to handle that much POST ' . $msg = _('The server was unable to handle that much POST ' .
'data (%s bytes) due to its current configuration.'); 'data (%s bytes) due to its current configuration.');
$this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return; return;
} }
// CSRF protection // CSRF protection
$token = $this->trimmed('token'); $token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { if (!$token || $token != common_session_token()) {
$this->clientError(_('There was a problem with your session token.')); $this->clientError(_('There was a problem with your session token.'));
return; return;
} }
$cur = common_current_user(); $cur = common_current_user();
if ($this->arg('cancel')) { if ($this->arg('cancel')) {
common_redirect(common_local_url('apps', common_redirect(common_local_url('apps',
array('nickname' => $cur->nickname)), 303); array('nickname' => $cur->nickname)), 303);
} elseif ($this->arg('save')) { } elseif ($this->arg('save')) {
$this->trySave(); $this->trySave();
} else { } else {
$this->clientError(_('Unexpected form submission.')); $this->clientError(_('Unexpected form submission.'));
} }
} }
function showForm($msg=null) function showForm($msg=null)
@ -147,7 +147,7 @@ class NewApplicationAction extends OwnerDesignAction
function trySave() function trySave()
{ {
$name = $this->trimmed('name'); $name = $this->trimmed('name');
$description = $this->trimmed('description'); $description = $this->trimmed('description');
$source_url = $this->trimmed('source_url'); $source_url = $this->trimmed('source_url');
$organization = $this->trimmed('organization'); $organization = $this->trimmed('organization');
@ -200,8 +200,8 @@ class NewApplicationAction extends OwnerDesignAction
{ {
$this->showForm(_('Homepage is not a valid URL.')); $this->showForm(_('Homepage is not a valid URL.'));
return; return;
} elseif (empty($callback_url)) { } elseif (mb_strlen($callback_url) > 255) {
$this->showForm(_('Callback is required.')); $this->showForm(_('Callback is too long.'));
return; return;
} elseif (strlen($callback_url) > 0 } elseif (strlen($callback_url) > 0
&& !Validate::uri( && !Validate::uri(
@ -266,7 +266,7 @@ class NewApplicationAction extends OwnerDesignAction
$app->query('ROLLBACK'); $app->query('ROLLBACK');
} }
$this->uploadLogo($app); $this->app->uploadLogo();
$app->query('COMMIT'); $app->query('COMMIT');
@ -275,40 +275,5 @@ class NewApplicationAction extends OwnerDesignAction
} }
/**
* Handle an image upload
*
* Does all the magic for handling an image upload, and crops the
* image by default.
*
* @return void
*/
function uploadLogo($app)
{
if ($_FILES['app_icon']['error'] ==
UPLOAD_ERR_OK) {
try {
$imagefile = ImageFile::fromUpload('app_icon');
} catch (Exception $e) {
common_debug("damn that sucks");
$this->showForm($e->getMessage());
return;
}
$filename = Avatar::filename($app->id,
image_type_to_extension($imagefile->type),
null,
'oauth-app-icon-'.common_timestamp());
$filepath = Avatar::path($filename);
move_uploaded_file($imagefile->filepath, $filepath);
$app->setOriginal($filename);
}
}
} }

View File

@ -27,7 +27,7 @@ class Oauth_application extends Memcached_DataObject
/* Static get */ /* Static get */
function staticGet($k,$v=NULL) { function staticGet($k,$v=NULL) {
return Memcached_DataObject::staticGet('Oauth_application',$k,$v); return Memcached_DataObject::staticGet('Oauth_application',$k,$v);
} }
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
@ -90,16 +90,51 @@ class Oauth_application extends Memcached_DataObject
static function getByConsumerKey($key) static function getByConsumerKey($key)
{ {
if (empty($key)) { if (empty($key)) {
return null; return null;
} }
$app = new Oauth_application(); $app = new Oauth_application();
$app->consumer_key = $key; $app->consumer_key = $key;
$app->limit(1); $app->limit(1);
$result = $app->find(true); $result = $app->find(true);
return empty($result) ? null : $app; return empty($result) ? null : $app;
}
/**
* Handle an image upload
*
* Does all the magic for handling an image upload, and crops the
* image by default.
*
* @return void
*/
function uploadLogo()
{
if ($_FILES['app_icon']['error'] ==
UPLOAD_ERR_OK) {
try {
$imagefile = ImageFile::fromUpload('app_icon');
} catch (Exception $e) {
common_debug("damn that sucks");
$this->showForm($e->getMessage());
return;
}
$filename = Avatar::filename($this->id,
image_type_to_extension($imagefile->type),
null,
'oauth-app-icon-'.common_timestamp());
$filepath = Avatar::path($filename);
move_uploaded_file($imagefile->filepath, $filepath);
$this->setOriginal($filename);
}
} }
} }