newapplication.php 8.6 KB

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