newphoto.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2011, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * 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. * @package GNU Social
  23. * @author Ian Denhardt <ian@zenhack.net>
  24. * @copyright 2011 Free Software Foundation, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  26. */
  27. if(!defined('STATUSNET')){
  28. exit(1);
  29. }
  30. class NewphotoAction extends Action
  31. {
  32. var $user = null;
  33. function prepare($args)
  34. {
  35. parent::prepare($args);
  36. $this->user = common_current_user();
  37. if(empty($this->user)){
  38. throw new ClientException(_('Must be logged in to post a photo'),
  39. 403);
  40. }
  41. if($this->isPost()){
  42. $this->checkSessionToken();
  43. }
  44. return true;
  45. }
  46. function handle($args)
  47. {
  48. parent::handle($args);
  49. if ($this->isPost()) {
  50. $this->handlePost($args);
  51. } else {
  52. $this->showPage();
  53. }
  54. }
  55. function handlePost($args)
  56. {
  57. /*
  58. // Workaround for PHP returning empty $_POST and $_FILES when POST
  59. // length > post_max_size in php.ini
  60. if (empty($_FILES)
  61. && empty($_POST)
  62. && ($_SERVER['CONTENT_LENGTH'] > 0)
  63. ) {
  64. $msg = _('The server was unable to handle that much POST ' .
  65. 'data (%s bytes) due to its current configuration.');
  66. $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  67. return;
  68. } */
  69. $profile = $this->user->getProfile();
  70. $options = array();
  71. ToSelector::fillOptions($this, $options);
  72. try {
  73. $this->handleUpload();
  74. } catch (Exception $e) {
  75. $this->showForm($e->getMessage());
  76. return;
  77. }
  78. common_redirect($photo->uri, 303);
  79. }
  80. function getUpload()
  81. {
  82. $imagefile = ImageFile::fromUpload('photo_upload');
  83. if($imagefile === null) {
  84. throw new Exception(_('No file uploaded'));
  85. }
  86. $title = $this->trimmed('title');
  87. $description = $this->trimmed('description');
  88. $new_filename = UUID::gen() . image_type_to_extension($imagefile->type);
  89. move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $new_filename);
  90. // XXX: we should be using https where we can. TODO: detect whether the server
  91. // supports this.
  92. $photo_uri = 'http://' . common_config('site', 'server') . '/file/'
  93. . $new_filename;
  94. $thumb_uri = $photo_uri;
  95. $photo = Photo::saveNew($profile, $photo_uri, $thumb_uri, $title,
  96. $description, $options);
  97. }
  98. function showContent()
  99. {
  100. $form = new NewPhotoForm();
  101. $form->show();
  102. }
  103. }