newapplication.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Register a new OAuth Application
  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 Applications
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @copyright 2008-2011 StatusNet, Inc.
  26. * @copyright 2013 Free Software Foundation, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Add a new application
  33. *
  34. * This is the form for adding a new application
  35. *
  36. * @category Application
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class NewApplicationAction extends FormAction
  43. {
  44. function title()
  45. {
  46. // TRANS: This is the title of the form for adding a new application.
  47. return _('New application');
  48. }
  49. protected function doPost()
  50. {
  51. if ($this->arg('cancel')) {
  52. common_redirect(common_local_url('oauthappssettings'), 303);
  53. } elseif ($this->arg('save')) {
  54. $this->trySave();
  55. }
  56. // TRANS: Client error displayed when encountering an unexpected action on form submission.
  57. $this->clientError(_('Unexpected form submission.'));
  58. }
  59. protected function getForm()
  60. {
  61. return new ApplicationEditForm($this);
  62. }
  63. protected function getInstructions()
  64. {
  65. // TRANS: Form instructions for registering a new application.
  66. return _('Use this form to register a new application.');
  67. }
  68. private function trySave()
  69. {
  70. $name = $this->trimmed('name');
  71. $description = $this->trimmed('description');
  72. $source_url = $this->trimmed('source_url');
  73. $organization = $this->trimmed('organization');
  74. $homepage = $this->trimmed('homepage');
  75. $callback_url = $this->trimmed('callback_url');
  76. $type = $this->arg('app_type');
  77. $access_type = $this->arg('default_access_type');
  78. if (empty($name)) {
  79. // TRANS: Validation error shown when not providing a name in the "New application" form.
  80. $this->clientError(_('Name is required.'));
  81. } else if ($this->nameExists($name)) {
  82. // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
  83. $this->clientError(_('Name already in use. Try another one.'));
  84. } elseif (mb_strlen($name) > 255) {
  85. // TRANS: Validation error shown when providing too long a name in the "New application" form.
  86. $this->clientError(_('Name is too long (maximum 255 characters).'));
  87. } elseif (empty($description)) {
  88. // TRANS: Validation error shown when not providing a description in the "New application" form.
  89. $this->clientError(_('Description is required.'));
  90. } elseif (Oauth_application::descriptionTooLong($description)) {
  91. $this->clientError(sprintf(
  92. // TRANS: Form validation error in New application form.
  93. // TRANS: %d is the maximum number of characters for the description.
  94. _m('Description is too long (maximum %d character).',
  95. 'Description is too long (maximum %d characters).',
  96. Oauth_application::maxDesc()),
  97. Oauth_application::maxDesc()));
  98. } elseif (empty($source_url)) {
  99. // TRANS: Validation error shown when not providing a source URL in the "New application" form.
  100. $this->clientError(_('Source URL is required.'));
  101. } elseif ((strlen($source_url) > 0) && !common_valid_http_url($source_url)) {
  102. // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
  103. $this->clientError(_('Source URL is not valid.'));
  104. } elseif (empty($organization)) {
  105. // TRANS: Validation error shown when not providing an organisation in the "New application" form.
  106. $this->clientError(_('Organization is required.'));
  107. } elseif (mb_strlen($organization) > 255) {
  108. // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
  109. $this->clientError(_('Organization is too long (maximum 255 characters).'));
  110. } elseif (empty($homepage)) {
  111. // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
  112. $this->clientError(_('Organization homepage is required.'));
  113. } elseif ((strlen($homepage) > 0) && !common_valid_http_url($homepage)) {
  114. // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
  115. $this->clientError(_('Homepage is not a valid URL.'));
  116. } elseif (mb_strlen($callback_url) > 255) {
  117. // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
  118. $this->clientError(_('Callback is too long.'));
  119. } elseif (strlen($callback_url) > 0 && !common_valid_http_url($callback_url)) {
  120. // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
  121. $this->clientError(_('Callback URL is not valid.'));
  122. }
  123. // Login is checked in parent::prepare()
  124. assert(!is_null($this->scoped));
  125. $app = new Oauth_application();
  126. $app->query('BEGIN');
  127. $app->name = $name;
  128. $app->owner = $this->scoped->id;
  129. $app->description = $description;
  130. $app->source_url = $source_url;
  131. $app->organization = $organization;
  132. $app->homepage = $homepage;
  133. $app->callback_url = $callback_url;
  134. $app->type = $type;
  135. // Yeah, I dunno why I chose bit flags. I guess so I could
  136. // copy this value directly to Oauth_application_user
  137. // access_type which I think does need bit flags -- Z
  138. if ($access_type == 'r') {
  139. $app->setAccessFlags(true, false);
  140. } else {
  141. $app->setAccessFlags(true, true);
  142. }
  143. $app->created = common_sql_now();
  144. // generate consumer key and secret
  145. $consumer = Consumer::generateNew();
  146. $result = $consumer->insert();
  147. if (!$result) {
  148. common_log_db_error($consumer, 'INSERT', __FILE__);
  149. $app->query('ROLLBACK');
  150. // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
  151. $this->serverError(_('Could not create application.'));
  152. }
  153. $app->consumer_key = $consumer->consumer_key;
  154. $this->app_id = $app->insert();
  155. if (!$this->app_id) {
  156. common_log_db_error($app, 'INSERT', __FILE__);
  157. $app->query('ROLLBACK');
  158. // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
  159. $this->serverError(_('Could not create application.'));
  160. }
  161. try {
  162. $app->uploadLogo();
  163. } catch (Exception $e) {
  164. $app->query('ROLLBACK');
  165. // TRANS: Form validation error messages displayed when uploading an invalid application logo.
  166. $this->clientError(_('Invalid image.'));
  167. }
  168. $app->query('COMMIT');
  169. common_redirect(common_local_url('oauthappssettings'), 303);
  170. }
  171. /**
  172. * Does the app name already exist?
  173. *
  174. * Checks the DB to see someone has already registered an app
  175. * with the same name.
  176. *
  177. * @param string $name app name to check
  178. *
  179. * @return boolean true if the name already exists
  180. */
  181. function nameExists($name)
  182. {
  183. $app = Oauth_application::getKV('name', $name);
  184. return !empty($app);
  185. }
  186. }