123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- class EditApplicationAction extends Action
- {
- var $msg = null;
- var $owner = null;
- var $app = null;
- function title()
- {
-
- return _('Edit application');
- }
-
- function prepare(array $args = array())
- {
- parent::prepare($args);
- if (!common_logged_in()) {
-
- $this->clientError(_('You must be logged in to edit an application.'));
- }
- $id = (int)$this->arg('id');
- $this->app = Oauth_application::getKV($id);
- $this->owner = User::getKV($this->app->owner);
- $cur = common_current_user();
- if ($cur->id != $this->owner->id) {
-
- $this->clientError(_('You are not the owner of this application.'), 401);
- }
- if (!$this->app) {
-
- $this->clientError(_('No such application.'));
- }
- return true;
- }
-
- function handle()
- {
- parent::handle();
- if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $this->handlePost($args);
- } else {
- $this->showForm();
- }
- }
- function handlePost($args)
- {
-
-
- if (empty($_FILES)
- && empty($_POST)
- && ($_SERVER['CONTENT_LENGTH'] > 0)
- ) {
-
-
- $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
- 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
- intval($_SERVER['CONTENT_LENGTH']));
- $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
- return;
- }
-
- $token = $this->trimmed('token');
- if (!$token || $token != common_session_token()) {
-
- $this->clientError(_('There was a problem with your session token.'));
- }
- $cur = common_current_user();
- if ($this->arg('cancel')) {
- common_redirect(common_local_url('showapplication',
- array('id' => $this->app->id)), 303);
- } elseif ($this->arg('save')) {
- $this->trySave();
- } else {
-
- $this->clientError(_('Unexpected form submission.'));
- }
- }
- function showForm($msg=null)
- {
- $this->msg = $msg;
- $this->showPage();
- }
- function showContent()
- {
- $form = new ApplicationEditForm($this, $this->app);
- $form->show();
- }
- function showPageNotice()
- {
- if (!empty($this->msg)) {
- $this->element('p', 'error', $this->msg);
- } else {
- $this->element('p', 'instructions',
-
- _('Use this form to edit your application.'));
- }
- }
- function trySave()
- {
- $name = $this->trimmed('name');
- $description = $this->trimmed('description');
- $source_url = $this->trimmed('source_url');
- $organization = $this->trimmed('organization');
- $homepage = $this->trimmed('homepage');
- $callback_url = $this->trimmed('callback_url');
- $type = $this->arg('app_type');
- $access_type = $this->arg('default_access_type');
- if (empty($name)) {
-
- $this->showForm(_('Name is required.'));
- return;
- } elseif (mb_strlen($name) > 255) {
-
- $this->showForm(_('Name is too long (maximum 255 characters).'));
- return;
- } else if ($this->nameExists($name)) {
-
- $this->showForm(_('Name already in use. Try another one.'));
- return;
- } elseif (empty($description)) {
-
- $this->showForm(_('Description is required.'));
- return;
- } elseif (Oauth_application::descriptionTooLong($description)) {
- $this->showForm(sprintf(
-
-
- _m('Description is too long (maximum %d character).',
- 'Description is too long (maximum %d characters).',
- Oauth_application::maxDesc()),
- Oauth_application::maxDesc()));
- return;
- } elseif (mb_strlen($source_url) > 255) {
-
- $this->showForm(_('Source URL is too long.'));
- return;
- } elseif ((mb_strlen($source_url) > 0)
- && !common_valid_http_url($source_url)) {
-
- $this->showForm(_('Source URL is not valid.'));
- return;
- } elseif (empty($organization)) {
-
- $this->showForm(_('Organization is required.'));
- return;
- } elseif (mb_strlen($organization) > 255) {
-
- $this->showForm(_('Organization is too long (maximum 255 characters).'));
- return;
- } elseif (empty($homepage)) {
-
- $this->showForm(_('Organization homepage is required.'));
- return;
- } elseif ((mb_strlen($homepage) > 0)
- && !common_valid_http_url($homepage)) {
-
- $this->showForm(_('Homepage is not a valid URL.'));
- return;
- } elseif (mb_strlen($callback_url) > 255) {
-
- $this->showForm(_('Callback is too long.'));
- return;
- } elseif (mb_strlen($callback_url) > 0
- && !common_valid_http_url($callback_url)) {
-
- $this->showForm(_('Callback URL is not valid.'));
- return;
- }
- $cur = common_current_user();
-
- assert(!is_null($cur));
- assert(!is_null($this->app));
- $orig = clone($this->app);
- $this->app->name = $name;
- $this->app->description = $description;
- $this->app->source_url = $source_url;
- $this->app->organization = $organization;
- $this->app->homepage = $homepage;
- $this->app->callback_url = $callback_url;
- $this->app->type = $type;
- common_debug("access_type = $access_type");
- if ($access_type == 'r') {
- $this->app->access_type = 1;
- } else {
- $this->app->access_type = 3;
- }
- $result = $this->app->update($orig);
-
-
-
- if ($result === false) {
- common_log_db_error($this->app, 'UPDATE', __FILE__);
-
- $this->serverError(_('Could not update application.'));
- }
- $this->app->uploadLogo();
- common_redirect(common_local_url('oauthappssettings'), 303);
- }
-
- function nameExists($name)
- {
- $newapp = Oauth_application::getKV('name', $name);
- if (empty($newapp)) {
- return false;
- } else {
- return $newapp->id != $this->app->id;
- }
- }
- }
|