]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photoupload.php
Improved nav bar
[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         if(empty($this->user)) {
63             $this->element('p', array(), 'You are not logged in.');
64         } else {
65             $this->elementStart('form', array('enctype' => 'multipart/form-data',
66                                               'method' => 'post',
67                                               'action' => common_local_url('photoupload')));
68             $this->elementStart('ul', 'form_data');
69             $this->elementStart('li');
70             $this->element('input', array('name' => 'photofile',
71                                           'type' => 'file',
72                                           'id' => 'photofile'));
73             $this->elementEnd('li');
74             //$this->element('br');
75             $this->elementStart('li');
76             $this->input('phototitle', "Title", $this->trimmed('phototitle'), "The title of the photo. (Optional)");
77             $this->elementEnd('li');
78             $this->elementStart('li');
79             $this->textarea('photo_description', "Description", $this->trimmed('photo_description'), "A description of the photo. (Optional)");
80             $this->elementEnd('li');
81             $this->elementEnd('ul');
82             $this->submit('upload', _('Upload'));
83             $this->elementEnd('form');
84         }
85     }
86
87     function handlePost()
88     {
89
90         common_log(LOG_INFO, 'handlPost()!');
91         // Workaround for PHP returning empty $_POST and $_FILES when POST
92         // length > post_max_size in php.ini
93
94         if (empty($_FILES)
95             && empty($_POST)
96             && ($_SERVER['CONTENT_LENGTH'] > 0)
97         ) {
98             $msg = _('The server was unable to handle that much POST ' .
99                 'data (%s bytes) due to its current configuration.');
100
101             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
102             return;
103         }
104
105         // CSRF protection
106
107 /*        $token = $this->trimmed('token');
108         if (!$token || $token != common_session_token()) {
109             $this->showForm(_('There was a problem with your session token. '.
110                                'Try again, please.'));
111             return;
112         } */
113
114         if($this->arg('upload')) {
115             $this->uploadPhoto();
116         }
117     }
118
119     function showForm($msg, $success=false)
120     { 
121         $this->msg = $msg;
122         $this->success = $success;
123
124 //        $this->showPage();
125     }
126
127     function uploadPhoto()
128     {
129         $cur = common_current_user();
130         if(empty($cur)) {
131             return;
132         }
133         try {
134             $imagefile = ImageFile::fromUpload('photofile');
135         } catch (Exception $e) {
136             $this->showForm($e->getMessage());
137             return;
138         }
139         if ($imagefile === null) {
140             $this->showForm(_('No file uploaded.'));
141             return;
142         }
143
144         $title = $this->trimmed('phototitle');
145         $photo_description = $this->trimmed('photo_description');
146
147         common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
148
149         $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) .  image_type_to_extension($imagefile->type);
150         move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
151         photo_make_thumbnail($filename);
152         $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
153         $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
154         $profile_id = $cur->id;
155        
156         // TODO: proper multiple album support 
157         $album = GNUsocialPhotoAlbum::staticGet('profile_id', $profile_id);
158         if(!$album)
159             $album = GNUsocialPhotoAlbum::newAlbum($profile_id, 'Default');
160         GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
161     }
162
163 }