newapplication.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 SettingsAction
  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. //trySave will never return, just throw exception or redirect
  55. $this->trySave();
  56. }
  57. // TRANS: Client error displayed when encountering an unexpected action on form submission.
  58. $this->clientError(_('Unexpected form submission.'));
  59. }
  60. protected function getForm()
  61. {
  62. return new ApplicationEditForm($this);
  63. }
  64. protected function getInstructions()
  65. {
  66. // TRANS: Form instructions for registering a new application.
  67. return _('Use this form to register a new application.');
  68. }
  69. protected function trySave()
  70. {
  71. $name = $this->trimmed('name');
  72. $description = $this->trimmed('description');
  73. $source_url = $this->trimmed('source_url');
  74. $organization = $this->trimmed('organization');
  75. $homepage = $this->trimmed('homepage');
  76. $callback_url = $this->trimmed('callback_url');
  77. $type = $this->arg('app_type');
  78. $access_type = $this->arg('default_access_type');
  79. if (empty($name)) {
  80. // TRANS: Validation error shown when not providing a name in the "New application" form.
  81. $this->clientError(_('Name is required.'));
  82. } else if ($this->nameExists($name)) {
  83. // TRANS: Validation error shown when providing a name for an application that already exists in the "New application" form.
  84. $this->clientError(_('Name already in use. Try another one.'));
  85. } elseif (mb_strlen($name) > 255) {
  86. // TRANS: Validation error shown when providing too long a name in the "New application" form.
  87. $this->clientError(_('Name is too long (maximum 255 characters).'));
  88. } elseif (empty($description)) {
  89. // TRANS: Validation error shown when not providing a description in the "New application" form.
  90. $this->clientError(_('Description is required.'));
  91. } elseif (Oauth_application::descriptionTooLong($description)) {
  92. $this->clientError(sprintf(
  93. // TRANS: Form validation error in New application form.
  94. // TRANS: %d is the maximum number of characters for the description.
  95. _m('Description is too long (maximum %d character).',
  96. 'Description is too long (maximum %d characters).',
  97. Oauth_application::maxDesc()),
  98. Oauth_application::maxDesc()));
  99. } elseif (empty($source_url)) {
  100. // TRANS: Validation error shown when not providing a source URL in the "New application" form.
  101. $this->clientError(_('Source URL is required.'));
  102. } elseif ((strlen($source_url) > 0) && !common_valid_http_url($source_url)) {
  103. // TRANS: Validation error shown when providing an invalid source URL in the "New application" form.
  104. $this->clientError(_('Source URL is not valid.'));
  105. } elseif (empty($organization)) {
  106. // TRANS: Validation error shown when not providing an organisation in the "New application" form.
  107. $this->clientError(_('Organization is required.'));
  108. } elseif (mb_strlen($organization) > 255) {
  109. // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
  110. $this->clientError(_('Organization is too long (maximum 255 characters).'));
  111. } elseif (empty($homepage)) {
  112. // TRANS: Form validation error show when an organisation name has not been provided in the new application form.
  113. $this->clientError(_('Organization homepage is required.'));
  114. } elseif ((strlen($homepage) > 0) && !common_valid_http_url($homepage)) {
  115. // TRANS: Validation error shown when providing an invalid homepage URL in the "New application" form.
  116. $this->clientError(_('Homepage is not a valid URL.'));
  117. } elseif (mb_strlen($callback_url) > 255) {
  118. // TRANS: Validation error shown when providing too long a callback URL in the "New application" form.
  119. $this->clientError(_('Callback is too long.'));
  120. } elseif (strlen($callback_url) > 0 && !common_valid_http_url($callback_url)) {
  121. // TRANS: Validation error shown when providing an invalid callback URL in the "New application" form.
  122. $this->clientError(_('Callback URL is not valid.'));
  123. }
  124. // Login is checked in parent::prepare()
  125. assert(!is_null($this->scoped));
  126. $app = new Oauth_application();
  127. $app->query('BEGIN');
  128. $app->name = $name;
  129. $app->owner = $this->scoped->getID();
  130. $app->description = $description;
  131. $app->source_url = $source_url;
  132. $app->organization = $organization;
  133. $app->homepage = $homepage;
  134. $app->callback_url = $callback_url;
  135. $app->type = $type;
  136. // Yeah, I dunno why I chose bit flags. I guess so I could
  137. // copy this value directly to Oauth_application_user
  138. // access_type which I think does need bit flags -- Z
  139. if ($access_type == 'r') {
  140. $app->setAccessFlags(true, false);
  141. } else {
  142. $app->setAccessFlags(true, true);
  143. }
  144. $app->created = common_sql_now();
  145. // generate consumer key and secret
  146. $consumer = Consumer::generateNew();
  147. $result = $consumer->insert();
  148. if (!$result) {
  149. common_log_db_error($consumer, 'INSERT', __FILE__);
  150. $app->query('ROLLBACK');
  151. // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
  152. $this->serverError(_('Could not create application.'));
  153. }
  154. $app->consumer_key = $consumer->consumer_key;
  155. $this->app_id = $app->insert();
  156. if (!$this->app_id) {
  157. common_log_db_error($app, 'INSERT', __FILE__);
  158. $app->query('ROLLBACK');
  159. // TRANS: Server error displayed when an application could not be registered in the database through the "New application" form.
  160. $this->serverError(_('Could not create application.'));
  161. }
  162. try {
  163. $app->uploadLogo();
  164. } catch (Exception $e) {
  165. $app->query('ROLLBACK');
  166. // TRANS: Form validation error messages displayed when uploading an invalid application logo.
  167. $this->clientError(_('Invalid image.'));
  168. }
  169. $app->query('COMMIT');
  170. common_redirect(common_local_url('oauthappssettings'), 303);
  171. }
  172. /**
  173. * Does the app name already exist?
  174. *
  175. * Checks the DB to see someone has already registered an app
  176. * with the same name.
  177. *
  178. * @param string $name app name to check
  179. *
  180. * @return boolean true if the name already exists
  181. */
  182. function nameExists($name)
  183. {
  184. $app = Oauth_application::getKV('name', $name);
  185. return !empty($app);
  186. }
  187. }