]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhoto/actions/newphoto.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / GNUsocialPhoto / actions / newphoto.php
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
28 if(!defined('STATUSNET')){
29     exit(1);
30 }
31
32 class NewphotoAction extends Action
33 {
34     var $user = null;
35
36     function prepare(array $args=array())
37     {
38         parent::prepare($args);
39         $this->user = common_current_user();
40
41         if(empty($this->user)){
42             throw new ClientException(_('Must be logged in to post a photo'),
43                 403);
44         }
45
46         if($this->isPost()){
47             $this->checkSessionToken();
48         }
49
50         return true;
51     }
52
53     function handle(array $args=array())
54     {
55         parent::handle($args);
56
57         if ($this->isPost()) {
58             $this->handlePost($args);
59         } else {
60             $this->showPage();
61         }
62     }
63     
64     function handlePost($args)
65     {
66
67         /*
68         // Workaround for PHP returning empty $_POST and $_FILES when POST
69         // length > post_max_size in php.ini
70         if (empty($_FILES)
71             && empty($_POST)
72             && ($_SERVER['CONTENT_LENGTH'] > 0)
73         ) {
74             $msg = _('The server was unable to handle that much POST ' .
75                 'data (%s bytes) due to its current configuration.');
76             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
77             return;
78         } */
79
80         $profile = $this->user->getProfile();
81
82         $options = array();
83         
84         ToSelector::fillOptions($this, $options);
85
86         try {
87             $this->handleUpload();
88         } catch (Exception $e) {
89             $this->showForm($e->getMessage());
90             return;
91         }
92         
93
94         common_redirect($photo->uri, 303);
95     }
96
97     function getUpload()
98     {
99             $imagefile = ImageFile::fromUpload('photo_upload');
100
101             if($imagefile === null) {
102                 throw new Exception(_('No file uploaded'));
103             }
104
105             $title = $this->trimmed('title');
106             $description = $this->trimmed('description');
107
108             $new_filename = UUID::gen() . image_type_to_extension($imagefile->type);
109             move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $new_filename);
110            
111             // XXX: we should be using https where we can. TODO: detect whether the server
112             // supports this.
113             $photo_uri = 'http://' . common_config('site', 'server') . '/file/'
114                 . $new_filename;
115             $thumb_uri = $photo_uri;
116
117  
118             $photo = Photo::saveNew($profile, $photo_uri, $thumb_uri, $title,
119                 $description, $options);
120         
121     }
122
123     function showContent()
124     {
125         $form = new NewPhotoForm();
126         $form->show();
127     }
128 }
129
130