newapplication.php 8.9 KB

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