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