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