]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photoupload.php
Creating albums and uploading to them
[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($args)
40     {
41         parent::prepare($args);
42         $this->user = common_current_user();
43         return true;
44     }
45
46     function handle($args)
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             $this->elementStart('form', array('enctype' => 'multipart/form-data',
67                                               'method' => 'post',
68                                               'action' => common_local_url('photoupload')));
69             $this->elementStart('ul', 'form_data');
70             $this->elementStart('li');
71             $this->element('input', array('name' => 'photofile',
72                                           'type' => 'file',
73                                           'id' => 'photofile'));
74             $this->elementEnd('li');
75             //$this->element('br');
76             $this->elementStart('li');
77             $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
78             $this->elementEnd('li');
79             $this->elementStart('li');
80             $this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
81             $this->elementEnd('li');
82             $this->elementStart('li');
83             $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
84             $this->elementEnd('li');
85             $this->elementEnd('ul');
86             $this->submit('upload', _('Upload'));
87             $this->elementEnd('form');
88             $this->element('br');
89
90             //Create a new album
91             $this->element('h3', array(), _("Create a new album"));
92             $this->elementStart('form', array('method' => 'post',
93                                               'action' =>common_local_url('photoupload')));
94             $this->elementStart('ul', 'form_data');
95             $this->elementStart('li');
96             $this->input('album_name', _("Title"), null, _("The title of the album."));
97             $this->elementEnd('li');
98             $this->elementStart('li');
99             $this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
100             $this->elementEnd('li');
101             $this->elementEnd('ul');
102             $this->submit('create', _('Create'));
103             $this->elementEnd('form');
104             
105         }
106     }
107
108     function handlePost()
109     {
110
111         common_log(LOG_INFO, 'handlPost()!');
112         // Workaround for PHP returning empty $_POST and $_FILES when POST
113         // length > post_max_size in php.ini
114
115         if (empty($_FILES)
116             && empty($_POST)
117             && ($_SERVER['CONTENT_LENGTH'] > 0)
118         ) {
119             $msg = _('The server was unable to handle that much POST ' .
120                 'data (%s bytes) due to its current configuration.');
121
122             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
123             return;
124         }
125
126         // CSRF protection
127
128 /*        $token = $this->trimmed('token');
129         if (!$token || $token != common_session_token()) {
130             $this->showForm(_('There was a problem with your session token. '.
131                                'Try again, please.'));
132             return;
133         } */
134
135         if ($this->arg('upload')) {
136             $this->uploadPhoto();
137         }
138         if ($this->arg('create')) {
139             $this->createAlbum();
140         }
141     }
142
143     function showForm($msg, $success=false)
144     {
145         $this->msg = $msg;
146         $this->success = $success;
147
148 //        $this->showPage();
149     }
150
151     function albumList() 
152     {
153         $cur = common_current_user();
154         $album = new GNUsocialPhotoAlbum();
155         $album->user_id = $cur->id;
156
157         $albumlist = array();
158         if (!$album->find()) {
159             GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
160         }
161         while ($album->fetch()) {
162             $albumlist[$album->album_id] = $album->album_name;
163         }
164         return $albumlist;
165     }
166
167     function uploadPhoto()
168     {
169         $cur = common_current_user();
170         if(empty($cur)) {
171             return;
172         }
173         try {
174             $imagefile = ImageFile::fromUpload('photofile');
175         } catch (Exception $e) {
176             $this->showForm($e->getMessage());
177             return;
178         }
179         if ($imagefile === null) {
180             $this->showForm(_('No file uploaded.'));
181             return;
182         }
183
184         $title = $this->trimmed('phototitle');
185         $photo_description = $this->trimmed('photo_description');
186
187         common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
188
189         $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) .  image_type_to_extension($imagefile->type);
190         move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
191         photo_make_thumbnail($filename);
192         $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
193         $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
194         $profile_id = $cur->id;
195        
196         // TODO: proper multiple album support 
197         $album = GNUsocialPhotoAlbum::staticGet('album_id', $this->trimmed('album'));
198         if ($album->profile_id != $profile_id) {
199             $this->showForm(_('Error: This is not your album!'));
200             return;
201         }
202         GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
203     }
204
205     function createAlbum()
206     {
207         $cur = common_current_user();
208         if(empty($cur)) {
209             return;
210         }
211
212         $album_name = $this->trimmed('album_name');
213         $album_description = $this->trimmed('album_description');
214        
215         GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);
216     }
217 }