]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Add icons/icon upload to Oauth apps
authorZach Copley <zach@status.net>
Thu, 7 Jan 2010 09:55:57 +0000 (01:55 -0800)
committerZach Copley <zach@status.net>
Thu, 14 Jan 2010 02:41:05 +0000 (02:41 +0000)
actions/editapplication.php
actions/newapplication.php
actions/showapplication.php
classes/Oauth_application.php
lib/applicationeditform.php
lib/applicationlist.php

index 3af482844f573f6fdaee013d49595135a0be0169..6b8dd501c9621b1617209b1b988fe195c95c243a 100644 (file)
@@ -81,7 +81,7 @@ class EditApplicationAction extends OwnerDesignAction
     /**
      * Handle the request
      *
-     * On GET, show the form. On POST, try to save the group.
+     * On GET, show the form. On POST, try to save the app.
      *
      * @param array $args unused
      *
@@ -91,31 +91,49 @@ class EditApplicationAction extends OwnerDesignAction
     function handle($args)
     {
         parent::handle($args);
+
         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
+           $this->handlePost($args);
+       } else {
+           $this->showForm();
+       }
+    }
 
-            // CSRF protection
-            $token = $this->trimmed('token');
-            if (!$token || $token != common_session_token()) {
-                $this->clientError(_('There was a problem with your session token.'));
-                return;
-            }
-
-            $cur = common_current_user();
-
-            if ($this->arg('cancel')) {
-                common_redirect(common_local_url('showapplication',
-                    array(
-                        'nickname' => $cur->nickname,
-                        'id' => $this->app->id)
-                    ), 303);
-            } elseif ($this->arg('save')) {
-                $this->trySave();
-            } else {
-                $this->clientError(_('Unexpected form submission.'));
-            }
-        } else {
-            $this->showForm();
+    function handlePost($args)
+    {
+       // 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->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
+            return;
         }
+
+       // CSRF protection
+       $token = $this->trimmed('token');
+       if (!$token || $token != common_session_token()) {
+           $this->clientError(_('There was a problem with your session token.'));
+           return;
+       }
+
+       $cur = common_current_user();
+
+       if ($this->arg('cancel')) {
+           common_redirect(common_local_url('showapplication',
+                                            array(
+                                                  'nickname' => $cur->nickname,
+                                                  'id' => $this->app->id)
+                                            ), 303);
+       } elseif ($this->arg('save')) {
+           $this->trySave();
+       } else {
+                $this->clientError(_('Unexpected form submission.'));
+       }
     }
 
     function showForm($msg=null)
@@ -149,7 +167,7 @@ class EditApplicationAction extends OwnerDesignAction
         $homepage     = $this->trimmed('homepage');
         $callback_url = $this->trimmed('callback_url');
         $type         = $this->arg('app_type');
-        $access_type  = $this->arg('access_type');
+        $access_type  = $this->arg('default_access_type');
 
         if (empty($name)) {
              $this->showForm(_('Name is required.'));
@@ -214,6 +232,7 @@ class EditApplicationAction extends OwnerDesignAction
         // Checked in prepare() above
 
         assert(!is_null($cur));
+       assert(!is_null($this->app));
 
         $orig = clone($this->app);
 
@@ -225,16 +244,18 @@ class EditApplicationAction extends OwnerDesignAction
         $this->app->callback_url = $callback_url;
         $this->app->type         = $type;
 
+        $result = $this->app->update($orig);
+
+       common_debug("access_type = $access_type");
+
         if ($access_type == 'r') {
-            $this->app->setAccessFlags(true, false);
+            $this->app->access_type = 1;
         } else {
-            $this->app->setAccessFlags(true, true);
+            $this->app->access_type = 3;
         }
 
-        $result = $this->app->update($orig);
-
         if (!$result) {
-            common_log_db_error($app, 'UPDATE', __FILE__);
+            common_log_db_error($this->app, 'UPDATE', __FILE__);
             $this->serverError(_('Could not update application.'));
         }
 
index ec0f2e7af28aeb17ae52dd3e920ff8cf903d760d..a0e61d288cf9af978b288ac49aa9c5fd6507b4c6 100644 (file)
@@ -71,7 +71,7 @@ class NewApplicationAction extends OwnerDesignAction
     /**
      * Handle the request
      *
-     * On GET, show the form. On POST, try to save the group.
+     * On GET, show the form. On POST, try to save the app.
      *
      * @param array $args unused
      *
@@ -83,29 +83,46 @@ class NewApplicationAction extends OwnerDesignAction
         parent::handle($args);
 
         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
-
-            // CSRF protection
-            $token = $this->trimmed('token');
-            if (!$token || $token != common_session_token()) {
-                $this->clientError(_('There was a problem with your session token.'));
-                return;
-            }
-
-            $cur = common_current_user();
-
-            if ($this->arg('cancel')) {
-                common_redirect(common_local_url('apps',
-                    array('nickname' => $cur->nickname)), 303);
-            } elseif ($this->arg('save')) {
-                $this->trySave();
-            } else {
-                $this->clientError(_('Unexpected form submission.'));
-            }
+           $this->handlePost($args);
         } else {
             $this->showForm();
         }
     }
 
+    function handlePost($args)
+    {
+       // 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->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
+            return;
+        }
+
+       // CSRF protection
+       $token = $this->trimmed('token');
+       if (!$token || $token != common_session_token()) {
+           $this->clientError(_('There was a problem with your session token.'));
+           return;
+       }
+
+       $cur = common_current_user();
+
+       if ($this->arg('cancel')) {
+           common_redirect(common_local_url('apps',
+                                            array('nickname' => $cur->nickname)), 303);
+       } elseif ($this->arg('save')) {
+           $this->trySave();
+       } else {
+           $this->clientError(_('Unexpected form submission.'));
+       }
+    }
+
     function showForm($msg=null)
     {
         $this->msg = $msg;
@@ -130,14 +147,14 @@ class NewApplicationAction extends OwnerDesignAction
 
     function trySave()
     {
-        $name         = $this->trimmed('name');
+       $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('access_type');
+        $access_type  = $this->arg('default_access_type');
 
         if (empty($name)) {
              $this->showForm(_('Name is required.'));
@@ -241,14 +258,16 @@ class NewApplicationAction extends OwnerDesignAction
 
         $app->consumer_key = $consumer->consumer_key;
 
-        $result = $app->insert();
+        $this->app_id = $app->insert();
 
-        if (!$result) {
+        if (!$this->app_id) {
             common_log_db_error($app, 'INSERT', __FILE__);
             $this->serverError(_('Could not create application.'));
             $app->query('ROLLBACK');
         }
 
+       $this->uploadLogo($app);
+
         $app->query('COMMIT');
 
         common_redirect(common_local_url('apps',
@@ -256,5 +275,40 @@ 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);
+       }
+    }
+
 }
 
index 6b8eff4a6082d62b43f7f0b214cf690982f4c1f6..6d19b9561c29ef72d3609f5f113e57e117f50fd8 100644 (file)
@@ -55,7 +55,6 @@ class ShowApplicationAction extends OwnerDesignAction
 
     var $owner = null;
 
-
     var $msg = null;
 
     var $success = null;
@@ -187,6 +186,14 @@ class ShowApplicationAction extends OwnerDesignAction
 
         $this->elementStart('ul', 'entity_application_details');
 
+       $this->elementStart('li', 'entity_application-icon');
+
+       if (!empty($this->application->icon)) {
+           $this->element('img', array('src' => $this->application->icon));
+       }
+
+       $this->elementEnd('li');
+
         $this->elementStart('li', 'entity_application_name');
         $this->element('span', array('class' => 'big'), $this->application->name);
         $this->raw(sprintf(_(' by %1$s'), $this->application->organization));
index ef1bbf6d95a5d0f2c41e5f15ca7e0ea74f61839e..d4de6d82e1639fc04f684c813b44eb9b4119c3c1 100644 (file)
@@ -75,4 +75,17 @@ class Oauth_application extends Memcached_DataObject
         }
     }
 
+    function setOriginal($filename)
+    {
+        $imagefile = new ImageFile($this->id, Avatar::path($filename));
+
+        // XXX: Do we want to have a bunch of different size icons? homepage, stream, mini?
+        // or just one and control size via CSS? --Zach
+
+        $orig = clone($this);
+        $this->icon = Avatar::url($filename);
+        common_debug(common_log_objstring($this));
+        return $this->update($orig);
+    }
+
 }
index ed187ba0b4e402b2aeeecb31b78c559476660b3b..4d3bb06e7551fda2fcd5969ff0d44c9530ee26b8 100644 (file)
@@ -81,6 +81,21 @@ class ApplicationEditForm extends Form
         }
     }
 
+    /**
+     * HTTP method used to submit the form
+     *
+     * For image data we need to send multipart/form-data
+     * so we set that here too
+     *
+     * @return string the method to use for submitting
+     */
+
+    function method()
+    {
+        $this->enctype = 'multipart/form-data';
+        return 'post';
+    }
+
     /**
      * class of the form
      *
@@ -134,6 +149,7 @@ class ApplicationEditForm extends Form
     {
         if ($this->application) {
             $id                = $this->application->id;
+           $icon              = $this->application->icon;
             $name              = $this->application->name;
             $description       = $this->application->description;
             $source_url        = $this->application->source_url;
@@ -144,6 +160,7 @@ class ApplicationEditForm extends Form
             $this->access_type = $this->application->access_type;
         } else {
             $id                = '';
+           $icon              = '';
             $name              = '';
             $description       = '';
             $source_url        = '';
@@ -154,11 +171,31 @@ class ApplicationEditForm extends Form
             $this->access_type = '';
         }
 
+       $this->out->hidden('token', common_session_token());
+
         $this->out->elementStart('ul', 'form_data');
-        $this->out->elementStart('li');
+
+       $this->out->elementStart('li');
+
+       if (!empty($icon)) {
+           $this->out->element('img', array('src' => $icon));
+       }
+
+       $this->out->element('label', array('for' => 'app_icon'),
+                                _('Icon'));
+        $this->out->element('input', array('name' => 'app_icon',
+                                      'type' => 'file',
+                                      'id' => 'app_icon'));
+        $this->out->element('p', 'form_guide', _('Icon for this application'));
+        $this->out->element('input', array('name' => 'MAX_FILE_SIZE',
+                                      'type' => 'hidden',
+                                      'id' => 'MAX_FILE_SIZE',
+                                      'value' => ImageFile::maxFileSizeInt()));
+        $this->out->elementEnd('li');
+
+       $this->out->elementStart('li');
 
         $this->out->hidden('application_id', $id);
-        $this->out->hidden('token', common_session_token());
 
         $this->out->input('name', _('Name'),
                           ($this->out->arg('name')) ? $this->out->arg('name') : $name);
@@ -215,7 +252,7 @@ class ApplicationEditForm extends Form
         // Default to Browser
 
         if ($this->application->type == Oauth_application::$browser
-            || empty($this->applicaiton->type)) {
+            || empty($this->application->type)) {
             $attrs['checked'] = 'checked';
         }
 
index 3141ea97413a44db07a03d1ddf442cf8ee7805f7..5392ddab8cb2191625b7643d605de4f16e56727b 100644 (file)
@@ -93,6 +93,10 @@ class ApplicationList extends Widget
         $this->out->elementStart('li', array('class' => 'application',
                                              'id' => 'oauthclient-' . $this->application->id));
 
+       if (!empty($this->application->icon)) {
+           $this->out->element('img', array('src' => $this->application->icon));
+       }
+
         $this->out->elementStart('a',
             array('href' => common_local_url(
                     'showapplication',