]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Move unsecured GET endpoint handlers before security token check in Module\Contact
[friendica.git] / mod / videos.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Nav;
24 use Friendica\Content\Pager;
25 use Friendica\Content\Widget;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Attach;
31 use Friendica\Model\Item;
32 use Friendica\Model\Profile;
33 use Friendica\Model\User;
34 use Friendica\Module\BaseProfile;
35 use Friendica\Security\Security;
36 use Friendica\Network\HTTPException;
37
38 function videos_init(App $a)
39 {
40         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
41                 return;
42         }
43
44         Nav::setSelected('home');
45
46         if (DI::args()->getArgc() > 1) {
47                 $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
48                 if (empty($owner)) {
49                         throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
50                 }
51
52                 // If not there, create 'aside' empty
53                 if (!isset(DI::page()['aside'])) {
54                         DI::page()['aside'] = '';
55                 }
56
57                 DI::page()['aside'] .= Widget\VCard::getHTML($owner);
58
59                 $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
60                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl);
61         }
62
63         return;
64 }
65
66 function videos_post(App $a)
67 {
68         $user = User::getByNickname(DI::args()->getArgv()[1]);
69         if (!DBA::isResult($user)) {
70                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
71         }
72
73         if (local_user() != $user['uid']) {
74                 DI::baseUrl()->redirect('videos/' . $user['nickname']);
75         }
76
77         if ((DI::args()->getArgc() == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
78                 $video_id = $_POST['id'];
79
80                 if (Attach::exists(['id' => $video_id, 'uid' => local_user()])) {
81                         // delete the attachment
82                         Attach::delete(['id' => $video_id, 'uid' => local_user()]);
83
84                         // delete items where the attach is used
85                         Item::deleteForUser(['`attach` LIKE ? AND `uid` = ?',
86                                 '%attach/' . $video_id . '%',
87                                 local_user()
88                         ], local_user());
89                 }
90
91                 DI::baseUrl()->redirect('videos/' . $user['nickname']);
92                 return; // NOTREACHED
93         }
94
95         DI::baseUrl()->redirect('videos/' . $user['nickname']);
96 }
97
98 function videos_content(App $a)
99 {
100         // URLs (most aren't currently implemented):
101         // videos/name
102         // videos/name/upload
103         // videos/name/upload/xxxxx (xxxxx is album name)
104         // videos/name/album/xxxxx
105         // videos/name/album/xxxxx/edit
106         // videos/name/video/xxxxx
107         // videos/name/video/xxxxx/edit
108
109         $user = User::getByNickname(DI::args()->getArgv()[1]);
110         if (!DBA::isResult($user)) {
111                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
112         }
113
114         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
115                 notice(DI::l10n()->t('Public access denied.'));
116                 return;
117         }
118
119         if (empty($user)) {
120                 notice(DI::l10n()->t('No videos selected') . EOL );
121                 return;
122         }
123
124         $profile = Profile::getByUID($user['uid']);
125         //$phototypes = Photo::supportedTypes();
126
127         $_SESSION['video_return'] = DI::args()->getCommand();
128
129         //
130         // Parse arguments
131         //
132         if (DI::args()->getArgc() > 3) {
133                 $datatype = DI::args()->getArgv()[2];
134         } elseif((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[2] === 'upload')) {
135                 $datatype = 'upload';
136         } else {
137                 $datatype = 'summary';
138         }
139
140         //
141         // Setup permissions structures
142         //
143         $can_post       = false;
144         $visitor        = 0;
145         $remote_contact = false;
146         $contact_id     = 0;
147
148         $community_page = (($user['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
149
150         if ((local_user()) && (local_user() == $user['uid'])) {
151                 $can_post = true;
152         } elseif ($community_page && !empty(Session::getRemoteContactID($user['uid']))) {
153                 $contact_id = Session::getRemoteContactID($user['uid']);
154                 $can_post = true;
155                 $remote_contact = true;
156                 $visitor = $contact_id;
157         }
158
159         // perhaps they're visiting - but not a community page, so they wouldn't have write access
160         if (!empty(Session::getRemoteContactID($user['uid'])) && !$visitor) {
161                 $contact_id = Session::getRemoteContactID($user['uid']);
162                 $remote_contact = true;
163         }
164
165         if ($user['hidewall'] && (local_user() != $user['uid']) && !$remote_contact) {
166                 notice(DI::l10n()->t('Access to this item is restricted.'));
167                 return;
168         }
169
170         $sql_extra = Security::getPermissionsSQLByUserId($user['uid']);
171
172         $o = "";
173
174         // tabs
175         $_is_owner = (local_user() && (local_user() == $user['uid']));
176         $o .= BaseProfile::getTabsHTML($a, 'videos', $_is_owner, $user['nickname'], $profile['hide-friends']);
177
178         //
179         // dispatch request
180         //
181         if ($datatype === 'upload') {
182                 return; // no uploading for now
183
184                 // DELETED -- look at mod/photos.php if you want to implement
185         }
186
187         if ($datatype === 'album') {
188                 return; // no albums for now
189
190                 // DELETED -- look at mod/photos.php if you want to implement
191         }
192
193
194         if ($datatype === 'video') {
195                 return; // no single video view for now
196
197                 // DELETED -- look at mod/photos.php if you want to implement
198         }
199
200         // Default - show recent videos (no upload link for now)
201         //$o = '';
202
203         $total = 0;
204         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
205                 $sql_extra GROUP BY hash",
206                 intval($user['uid'])
207         );
208         if (DBA::isResult($r)) {
209                 $total = count($r);
210         }
211
212         $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 20);
213
214         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
215                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
216                 FROM `attach`
217                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
218                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
219                 intval($user['uid']),
220                 $pager->getStart(),
221                 $pager->getItemsPerPage()
222         );
223
224         $videos = [];
225
226         if (DBA::isResult($r)) {
227                 foreach ($r as $rr) {
228                         $alt_e = $rr['filename'];
229                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
230                         $rr['album'] = '';
231                         $name_e = $rr['album'];
232
233                         $videos[] = [
234                                 'id'       => $rr['id'],
235                                 'link'     => DI::baseUrl() . '/videos/' . $user['nickname'] . '/video/' . $rr['hash'],
236                                 'title'    => DI::l10n()->t('View Video'),
237                                 'src'      => DI::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
238                                 'alt'      => $alt_e,
239                                 'mime'     => $rr['filetype'],
240                                 'album' => [
241                                         'link'  => DI::baseUrl() . '/videos/' . $user['nickname'] . '/album/' . bin2hex($rr['album']),
242                                         'name'  => $name_e,
243                                         'alt'   => DI::l10n()->t('View Album'),
244                                 ],
245                         ];
246                 }
247         }
248
249         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
250         $o .= Renderer::replaceMacros($tpl, [
251                 '$title'      => DI::l10n()->t('Recent Videos'),
252                 '$can_post'   => $can_post,
253                 '$upload'     => [DI::l10n()->t('Upload New Videos'), DI::baseUrl() . '/videos/' . $user['nickname'] . '/upload'],
254                 '$videos'     => $videos,
255                 '$delete_url' => (($can_post) ? DI::baseUrl() . '/videos/' . $user['nickname'] : false)
256         ]);
257
258         $o .= $pager->renderFull($total);
259
260         return $o;
261 }