]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/photoupload.php
Started working on proper photo upload. not fully functional yet.
[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  * @copyright 2010 Free Software Foundation, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 class PhotouploadAction extends Action
34 {
35     var $user = null;
36
37     function prepare($args)
38     {
39         parent::prepare($args);
40         $this->user = common_current_user();
41         return true;
42     }
43
44     function handle($args)
45     {
46         parent::handle($args);
47         $this->showPage();
48     }
49
50     function title()
51     {
52         return _m('Upload Photos');
53     }
54
55     function showContent()
56     {
57         if(empty($this->user)) {
58             $this->element('p', array(), 'You are not logged in.');
59         } else {
60             $this->elementStart('form', array('enctype' => 'mutlipart/form-data',
61                                               'method' => 'post',
62                                               'action' => common_local_url('photoupload')));
63             $this->element('input', array('name' => 'photofile',
64                                           'type' => 'file',
65                                           'id' => 'photofile'));
66             $this->submit('upload', _('Upload'));
67             $this->elementEnd('form');
68         }
69     }
70
71     function handlePost()
72     {
73
74         common_log(LOG_INFO, 'handlPost()!');
75         // Workaround for PHP returning empty $_POST and $_FILES when POST
76         // length > post_max_size in php.ini
77
78         if (empty($_FILES)
79             && empty($_POST)
80             && ($_SERVER['CONTENT_LENGTH'] > 0)
81         ) {
82             $msg = _('The server was unable to handle that much POST ' .
83                 'data (%s bytes) due to its current configuration.');
84
85             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
86             return;
87         }
88
89         // CSRF protection
90
91         $token = $this->trimmed('token');
92         if (!$token || $token != common_session_token()) {
93             $this->showForm(_('There was a problem with your session token. '.
94                                'Try again, please.'));
95             return;
96         }
97
98         if($this->arg('upload')) {
99             $this->uploadPhoto();
100         }
101     }
102
103     function uploadPhoto()
104     {
105         common_log(LOG_INFO, 'Is this function even getting called?');
106         $cur = common_current_user();
107         if(empty($cur)) {
108             return;
109         }
110         try {
111             $imagefile = ImageFile::fromUpload('photofile');
112         } catch (Exception $e) {
113             $this->showForm($e->getMessage());
114             return;
115         }
116         if ($imagefile === null) {
117             $this->showForm(_('No file uploaded.'));
118             return;
119         }
120
121         common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
122
123         $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . '.' .  image_type_to_extension($imagefile->type);
124         move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
125         photo_make_thumbnail($filename);
126         $photo = new GNUsocialPhoto();
127         $photo->path = '/file/' . $filename;
128         $photo->thumb_path = '/file/thumb.' . $filename;
129         $photo->insert();
130     }
131
132 }