editapplication.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Edit an 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. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Edit the details of an OAuth application
  34. *
  35. * This is the form for editing an application
  36. *
  37. * @category Application
  38. * @package StatusNet
  39. * @author Zach Copley <zach@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class EditApplicationAction extends Action
  44. {
  45. var $msg = null;
  46. var $owner = null;
  47. var $app = null;
  48. function title()
  49. {
  50. // TRANS: Title for "Edit application" form.
  51. return _('Edit application');
  52. }
  53. /**
  54. * Prepare to run
  55. */
  56. function prepare($args)
  57. {
  58. parent::prepare($args);
  59. if (!common_logged_in()) {
  60. // TRANS: Client error displayed trying to edit an application while not logged in.
  61. $this->clientError(_('You must be logged in to edit an application.'));
  62. }
  63. $id = (int)$this->arg('id');
  64. $this->app = Oauth_application::getKV($id);
  65. $this->owner = User::getKV($this->app->owner);
  66. $cur = common_current_user();
  67. if ($cur->id != $this->owner->id) {
  68. // TRANS: Client error displayed trying to edit an application while not being its owner.
  69. $this->clientError(_('You are not the owner of this application.'), 401);
  70. }
  71. if (!$this->app) {
  72. // TRANS: Client error displayed trying to edit an application that does not exist.
  73. $this->clientError(_('No such application.'));
  74. }
  75. return true;
  76. }
  77. /**
  78. * Handle the request
  79. *
  80. * On GET, show the form. On POST, try to save the app.
  81. *
  82. * @param array $args unused
  83. *
  84. * @return void
  85. */
  86. function handle($args)
  87. {
  88. parent::handle($args);
  89. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  90. $this->handlePost($args);
  91. } else {
  92. $this->showForm();
  93. }
  94. }
  95. function handlePost($args)
  96. {
  97. // Workaround for PHP returning empty $_POST and $_FILES when POST
  98. // length > post_max_size in php.ini
  99. if (empty($_FILES)
  100. && empty($_POST)
  101. && ($_SERVER['CONTENT_LENGTH'] > 0)
  102. ) {
  103. // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
  104. // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
  105. $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
  106. 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
  107. intval($_SERVER['CONTENT_LENGTH']));
  108. $this->clientException(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  109. return;
  110. }
  111. // CSRF protection
  112. $token = $this->trimmed('token');
  113. if (!$token || $token != common_session_token()) {
  114. // TRANS: Client error displayed when the session token does not match or is not given.
  115. $this->clientError(_('There was a problem with your session token.'));
  116. }
  117. $cur = common_current_user();
  118. if ($this->arg('cancel')) {
  119. common_redirect(common_local_url('showapplication',
  120. array('id' => $this->app->id)), 303);
  121. } elseif ($this->arg('save')) {
  122. $this->trySave();
  123. } else {
  124. // TRANS: Client error displayed submitting invalid form data for edit application.
  125. $this->clientError(_('Unexpected form submission.'));
  126. }
  127. }
  128. function showForm($msg=null)
  129. {
  130. $this->msg = $msg;
  131. $this->showPage();
  132. }
  133. function showContent()
  134. {
  135. $form = new ApplicationEditForm($this, $this->app);
  136. $form->show();
  137. }
  138. function showPageNotice()
  139. {
  140. if (!empty($this->msg)) {
  141. $this->element('p', 'error', $this->msg);
  142. } else {
  143. $this->element('p', 'instructions',
  144. // TRANS: Instructions for "Edit application" form.
  145. _('Use this form to edit your application.'));
  146. }
  147. }
  148. function trySave()
  149. {
  150. $name = $this->trimmed('name');
  151. $description = $this->trimmed('description');
  152. $source_url = $this->trimmed('source_url');
  153. $organization = $this->trimmed('organization');
  154. $homepage = $this->trimmed('homepage');
  155. $callback_url = $this->trimmed('callback_url');
  156. $type = $this->arg('app_type');
  157. $access_type = $this->arg('default_access_type');
  158. if (empty($name)) {
  159. // TRANS: Validation error shown when not providing a name in the "Edit application" form.
  160. $this->showForm(_('Name is required.'));
  161. return;
  162. } elseif (mb_strlen($name) > 255) {
  163. // TRANS: Validation error shown when providing too long a name in the "Edit application" form.
  164. $this->showForm(_('Name is too long (maximum 255 characters).'));
  165. return;
  166. } else if ($this->nameExists($name)) {
  167. // TRANS: Validation error shown when providing a name for an application that already exists in the "Edit application" form.
  168. $this->showForm(_('Name already in use. Try another one.'));
  169. return;
  170. } elseif (empty($description)) {
  171. // TRANS: Validation error shown when not providing a description in the "Edit application" form.
  172. $this->showForm(_('Description is required.'));
  173. return;
  174. } elseif (Oauth_application::descriptionTooLong($description)) {
  175. $this->showForm(sprintf(
  176. // TRANS: Validation error shown when providing too long a description in the "Edit application" form.
  177. // TRANS: %d is the maximum number of allowed characters.
  178. _m('Description is too long (maximum %d character).',
  179. 'Description is too long (maximum %d characters).',
  180. Oauth_application::maxDesc()),
  181. Oauth_application::maxDesc()));
  182. return;
  183. } elseif (mb_strlen($source_url) > 255) {
  184. // TRANS: Validation error shown when providing too long a source URL in the "Edit application" form.
  185. $this->showForm(_('Source URL is too long.'));
  186. return;
  187. } elseif ((mb_strlen($source_url) > 0)
  188. && !common_valid_http_url($source_url)) {
  189. // TRANS: Validation error shown when providing an invalid source URL in the "Edit application" form.
  190. $this->showForm(_('Source URL is not valid.'));
  191. return;
  192. } elseif (empty($organization)) {
  193. // TRANS: Validation error shown when not providing an organisation in the "Edit application" form.
  194. $this->showForm(_('Organization is required.'));
  195. return;
  196. } elseif (mb_strlen($organization) > 255) {
  197. // TRANS: Validation error shown when providing too long an arganisation name in the "Edit application" form.
  198. $this->showForm(_('Organization is too long (maximum 255 characters).'));
  199. return;
  200. } elseif (empty($homepage)) {
  201. // TRANS: Form validation error show when an organisation name has not been provided in the edit application form.
  202. $this->showForm(_('Organization homepage is required.'));
  203. return;
  204. } elseif ((mb_strlen($homepage) > 0)
  205. && !common_valid_http_url($homepage)) {
  206. // TRANS: Validation error shown when providing an invalid homepage URL in the "Edit application" form.
  207. $this->showForm(_('Homepage is not a valid URL.'));
  208. return;
  209. } elseif (mb_strlen($callback_url) > 255) {
  210. // TRANS: Validation error shown when providing too long a callback URL in the "Edit application" form.
  211. $this->showForm(_('Callback is too long.'));
  212. return;
  213. } elseif (mb_strlen($callback_url) > 0
  214. && !common_valid_http_url($callback_url)) {
  215. // TRANS: Validation error shown when providing an invalid callback URL in the "Edit application" form.
  216. $this->showForm(_('Callback URL is not valid.'));
  217. return;
  218. }
  219. $cur = common_current_user();
  220. // Checked in prepare() above
  221. assert(!is_null($cur));
  222. assert(!is_null($this->app));
  223. $orig = clone($this->app);
  224. $this->app->name = $name;
  225. $this->app->description = $description;
  226. $this->app->source_url = $source_url;
  227. $this->app->organization = $organization;
  228. $this->app->homepage = $homepage;
  229. $this->app->callback_url = $callback_url;
  230. $this->app->type = $type;
  231. common_debug("access_type = $access_type");
  232. if ($access_type == 'r') {
  233. $this->app->access_type = 1;
  234. } else {
  235. $this->app->access_type = 3;
  236. }
  237. $result = $this->app->update($orig);
  238. // Note: 0 means no rows changed, which can happen if the only
  239. // thing we changed was the icon, since it's not altered until
  240. // the next step.
  241. if ($result === false) {
  242. common_log_db_error($this->app, 'UPDATE', __FILE__);
  243. // TRANS: Server error occuring when an application could not be updated from the "Edit application" form.
  244. $this->serverError(_('Could not update application.'));
  245. }
  246. $this->app->uploadLogo();
  247. common_redirect(common_local_url('oauthappssettings'), 303);
  248. }
  249. /**
  250. * Does the app name already exist?
  251. *
  252. * Checks the DB to see someone has already registered an app
  253. * with the same name.
  254. *
  255. * @param string $name app name to check
  256. *
  257. * @return boolean true if the name already exists
  258. */
  259. function nameExists($name)
  260. {
  261. $newapp = Oauth_application::getKV('name', $name);
  262. if (empty($newapp)) {
  263. return false;
  264. } else {
  265. return $newapp->id != $this->app->id;
  266. }
  267. }
  268. }