]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photoupload.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / actions / photoupload.php
1 <?php
2 /**
3  * GNU Social
4  * Copyright (C) 2010, 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  * @category  Widget
23  * @package   GNU Social
24  * @author    Ian Denhardt <ian@zenhack.net>
25  * @author    Sean Corbett <sean@gnu.org>
26  * @author    Max Shinn    <trombonechamp@gmail.com>
27  * @copyright 2010 Free Software Foundation, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 class PhotouploadAction extends Action
36 {
37     var $user = null;
38
39     function prepare(array $args=array())
40     {
41         parent::prepare($args);
42         $this->user = common_current_user();
43         return true;
44     }
45
46     function handle(array $args=array())
47     {
48         parent::handle($args);
49         if($_SERVER['REQUEST_METHOD'] == 'POST') {
50             $this->handlePost();
51         }
52         $this->showPage();
53     }
54
55     function title()
56     {
57         return _m('Upload Photos');
58     }
59
60     function showContent()
61     {
62         //Upload a photo
63         if(empty($this->user)) {
64             $this->element('p', array(), 'You are not logged in.');
65         } else {
66             //showForm() data
67             if(!empty($this->msg)) {
68                 $class = ($this->success) ? 'success' : 'error';
69                 $this->element('p', array('class' => $class), $this->msg);
70             }
71
72             $this->elementStart('form', array('enctype' => 'multipart/form-data',
73                                               'method' => 'post',
74                                               'action' => common_local_url('photoupload')));
75             $this->elementStart('ul', 'form_data');
76             $this->elementStart('li');
77             $this->element('input', array('name' => 'photofile',
78                                           'type' => 'file',
79                                           'id' => 'photofile'));
80             $this->elementEnd('li');
81             //$this->element('br');
82             $this->elementStart('li');
83             $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
84             $this->elementEnd('li');
85             $this->elementStart('li');
86             $this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
87             $this->elementEnd('li');
88             $this->elementStart('li');
89             $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
90             $this->elementEnd('li');
91             $this->elementEnd('ul');
92             $this->submit('upload', _('Upload'));
93             $this->elementEnd('form');
94             $this->element('br');
95
96             //Create a new album
97             $this->element('h3', array(), _("Create a new album"));
98             $this->elementStart('form', array('method' => 'post',
99                                               'action' =>common_local_url('photoupload')));
100             $this->elementStart('ul', 'form_data');
101             $this->elementStart('li');
102             $this->input('album_name', _("Title"), null, _("The title of the album."));
103             $this->elementEnd('li');
104             $this->elementStart('li');
105             $this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
106             $this->elementEnd('li');
107             $this->elementEnd('ul');
108             $this->submit('create', _('Create'));
109             $this->elementEnd('form');
110
111             //Delete an album
112             $this->element('h3', array(), _("Delete an album"));
113             $this->elementStart('form', array('method' => 'post',
114                                               'action' =>common_local_url('photoupload')));
115             $this->elementStart('ul', 'form_data');
116             $this->elementStart('li');
117             $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
118             $this->elementEnd('li');
119             $this->elementEnd('ul');
120             $this->submit('deletealbum', _('Delete'));
121             $this->elementEnd('form');
122             
123         }
124     }
125
126     function handlePost()
127     {
128
129         common_log(LOG_INFO, 'handlPost()!');
130         // Workaround for PHP returning empty $_POST and $_FILES when POST
131         // length > post_max_size in php.ini
132
133         if (empty($_FILES)
134             && empty($_POST)
135             && ($_SERVER['CONTENT_LENGTH'] > 0)
136         ) {
137             $msg = _('The server was unable to handle that much POST ' .
138                 'data (%s bytes) due to its current configuration.');
139
140             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
141             return;
142         }
143
144         // CSRF protection
145
146 /*        $token = $this->trimmed('token');
147         if (!$token || $token != common_session_token()) {
148             $this->showForm(_('There was a problem with your session token. '.
149                                'Try again, please.'));
150             return;
151         } */
152
153         if ($this->arg('upload')) {
154             $this->uploadPhoto();
155         }
156         if ($this->arg('create')) {
157             $this->createAlbum();
158         }
159         if ($this->arg('deletealbum')) {
160             $this->deleteAlbum();
161         }
162     }
163
164     function showForm($msg, $success=false)
165     {
166         $this->msg = $msg;
167         $this->success = $success;
168
169 //        $this->showPage();
170     }
171
172     function albumList() 
173     {
174         $cur = common_current_user();
175         $album = new GNUsocialPhotoAlbum();
176         $album->user_id = $cur->id;
177
178         $albumlist = array();
179         if (!$album->find()) {
180             GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
181         }
182         while ($album->fetch()) {
183             $albumlist[$album->album_id] = $album->album_name;
184         }
185         return $albumlist;
186     }
187
188     function uploadPhoto()
189     {
190         $cur = common_current_user();
191         if(empty($cur)) {
192             return;
193         }
194         try {
195             $imagefile = ImageFile::fromUpload('photofile');
196         } catch (Exception $e) {
197             $this->showForm($e->getMessage());
198             return;
199         }
200         if ($imagefile === null) {
201             $this->showForm(_('No file uploaded.'));
202             return;
203         }
204
205         $title = $this->trimmed('phototitle');
206         $photo_description = $this->trimmed('photo_description');
207
208         common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
209
210         $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) .  image_type_to_extension($imagefile->type);
211         move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
212         photo_make_thumbnail($filename);
213         $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
214         $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
215         $profile_id = $cur->id;
216        
217         $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
218         if ($album->profile_id != $profile_id) {
219             $this->showForm(_('Error: This is not your album!'));
220             return;
221         }
222         GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
223     }
224
225     function createAlbum()
226     {
227         $cur = common_current_user();
228         if(empty($cur)) {
229             return;
230         }
231
232         $album_name = $this->trimmed('album_name');
233         $album_description = $this->trimmed('album_description');
234        
235         GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);
236     }
237
238     function deleteAlbum()
239     {
240         $cur = common_current_user();
241         if(empty($cur)) return;
242         
243         $album_id = $this->trimmed('album');
244         $album = GNUsocialPhotoAlbum::getKV('album_id', $album_id);
245         if (empty($album)) {
246             $this->showForm(_('This album does not exist or has been deleted.'));
247             return;
248         }
249         //Check if the album has any photos in it before deleting
250         $photos = GNUsocialPhoto::getKV('album_id', $album_id);
251         if(empty($photos)) {
252             $album->delete();
253             $this->showForm(_('Album deleted'), true);
254         }
255         else {
256             $this->showForm(_('Cannot delete album when there are photos in it!'), false);
257         }
258     }
259 }