]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GNUsocialPhotos/actions/editphoto.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / GNUsocialPhotos / actions / editphoto.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 EditphotoAction extends Action
36 {
37     var $user = null;
38
39     function prepare($args)
40     {
41         parent::prepare($args);
42         $args = $this->returnToArgs();
43         $this->user = common_current_user();
44         $this->photoid = $args[1]['photoid'];
45         $this->photo = GNUsocialPhoto::getKV('id', $this->photoid);
46         return true;
47     }
48
49     function handle($args)
50     {
51         parent::handle($args);
52         if($_SERVER['REQUEST_METHOD'] == 'POST') {
53             $this->handlePost();
54         }
55         $this->showPage();
56     }
57
58     function title()
59     {
60         if ($this->photo->title)
61             return _m('Edit photo - ' . $this->photo->title);
62         else
63             return _m('Edit photo');
64     }
65
66     function showContent()
67     {
68
69         if ($this->photo->album_id == 0) {
70             $this->element('p', array(), _('This photo does not exist or was deleted.'));
71             return;
72         }
73
74         if ($this->user->profile_id != $this->photo->profile_id) {
75             $this->element('p', array(), _('You are not authorized to edit this photo.'));
76             return;
77         } 
78
79         //showForm() data
80         if(!empty($this->msg)) {
81             $class = ($this->success) ? 'success' : 'error';
82             $this->element('p', array('class' => $class), $this->msg);
83         }
84
85         $this->element('img', array('src' => $this->photo->uri));
86         $this->elementStart('form', array('method' => 'post',
87                                           'action' => '/editphoto/' . $this->photo->id));
88         $this->elementStart('ul', 'form_data');
89         $this->elementStart('li');
90         $this->input('phototitle', _("Title"), $this->photo->title, _("The title of the photo. (Optional)"));
91         $this->elementEnd('li');
92         $this->elementStart('li');
93         $this->textarea('photo_description', _("Description"), $this->photo->photo_description, _("A description of the photo. (Optional)"));
94         $this->elementEnd('li');
95         $this->elementStart('li');
96         $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false, $this->photo->album_id);
97         $this->elementEnd('li');
98         $this->elementEnd('ul');
99         $this->submit('update', _('Update'));
100         $this->elementEnd('form');
101         $this->element('br');            
102         $this->elementStart('form', array('method' => 'post',
103                                           'action' => '/editphoto/' . $this->photo->id));
104         $this->element('input', array('type' => 'submit',
105                                       'name' => 'delete',
106                                       'value' => _('Delete Photo'),
107                                       'id' => 'delete',
108                                       'class' => 'submit',
109                                       'onclick' => 'return confirm(\'Are you sure you would like to delete this photo?\')'));
110         $this->elementEnd('form');
111
112     }
113
114     function handlePost()
115     {
116
117         common_log(LOG_INFO, 'handlPost()!');
118
119         if ($this->photo->album_id == 0) {
120             $this->element('p', array(), _('This photo does not exist or was deleted.'));
121             return;
122         }
123
124         if ($this->user->profile_id != $this->photo->profile_id) {
125             $this->element('p', array(), _('You are not authorized to edit this photo.'));
126             return;
127         } 
128
129         if ($this->arg('update')) {
130             $this->updatePhoto();
131         }
132         if ($this->arg('delete')) {
133             $this->deletePhoto();
134         }
135     }
136
137     function showForm($msg, $success=false)
138     {
139         $this->msg = $msg;
140         $this->success = $success;
141
142 //        $this->showPage();
143     }
144
145     function albumList() 
146     {
147         $cur = common_current_user();
148         $album = new GNUsocialPhotoAlbum();
149         $album->user_id = $cur->id;
150
151         $albumlist = array();
152         if (!$album->find()) {
153             GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
154         }
155         while ($album->fetch()) {
156             $albumlist[$album->album_id] = $album->album_name;
157         }
158         return $albumlist;
159     }
160
161     function updatePhoto()
162     {
163         $cur = common_current_user();
164         $this->photo->title = $this->trimmed('phototitle');
165         $this->photo->photo_description = $this->trimmed('photo_description');
166
167         $profile_id = $cur->id;
168        
169         $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
170         if ($album->profile_id != $profile_id) {
171             $this->showForm(_('Error: This is not your album!'));
172             return;
173         }
174         $this->photo->album_id = $album->album_id;
175         if ($this->photo->validate())
176             $this->photo->update();
177         else {
178             $this->showForm(_('Error: The photo data is not valid.'));
179             return;
180         }
181         common_redirect('/photo/' . $this->photo->id, '303');
182         // common_redirect exits
183     }
184
185     function deletePhoto()
186     {
187         //For redirection
188         $oldalbum = $this->album_id;
189
190         $notice = Notice::getKV('id', $this->photo->notice_id);
191
192         $this->photo->delete();
193         
194         if (Event::handle('StartDeleteOwnNotice', array($this->user, $notice))) {
195             $notice->deleteAs($this->scoped);
196             Event::handle('EndDeleteOwnNotice', array($this->user, $notice));
197         }
198        $this->showForm(_('Success!'));
199         common_redirect('/' . $this->user->nickname . '/photos/' . $oldalbum, '303');
200     }
201
202 }