]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / videos.php
1 <?php
2 /**
3  * @file mod/videos.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Pager;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Attach;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile;
19 use Friendica\Model\User;
20 use Friendica\Protocol\DFRN;
21 use Friendica\Util\Security;
22
23 function videos_init(App $a)
24 {
25         if ($a->argc > 1) {
26                 DFRN::autoRedir($a, $a->argv[1]);
27         }
28
29         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
30                 return;
31         }
32
33         Nav::setSelected('home');
34
35         if ($a->argc > 1) {
36                 $nick = $a->argv[1];
37                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
38                         DBA::escape($nick)
39                 );
40
41                 if (!DBA::isResult($user)) {
42                         return;
43                 }
44
45                 $a->data['user'] = $user[0];
46                 $a->profile_uid = $user[0]['uid'];
47
48                 $profile = Profile::getByNickname($nick, $a->profile_uid);
49
50                 $account_type = Contact::getAccountType($profile);
51
52                 $tpl = Renderer::getMarkupTemplate("vcard-widget.tpl");
53
54                 $vcard_widget = Renderer::replaceMacros($tpl, [
55                         '$name' => $profile['name'],
56                         '$photo' => $profile['photo'],
57                         '$addr' => defaults($profile, 'addr', ''),
58                         '$account_type' => $account_type,
59                         '$pdesc' => defaults($profile, 'pdesc', ''),
60                 ]);
61
62                 // If not there, create 'aside' empty
63                 if (!isset($a->page['aside'])) {
64                         $a->page['aside'] = '';
65                 }
66
67                 $a->page['aside'] .= $vcard_widget;
68
69                 $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
70                 $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
71                         '$baseurl' => System::baseUrl(),
72                 ]);
73         }
74
75         return;
76 }
77
78 function videos_post(App $a)
79 {
80         $owner_uid = $a->data['user']['uid'];
81
82         if (local_user() != $owner_uid) {
83                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
84         }
85
86         if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
87                 // Check if we should do HTML-based delete confirmation
88                 if (empty($_REQUEST['confirm'])) {
89                         if (!empty($_REQUEST['canceled'])) {
90                                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
91                         }
92
93                         $drop_url = $a->query_string;
94
95                         $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
96                                 '$method' => 'post',
97                                 '$message' => L10n::t('Do you really want to delete this video?'),
98                                 '$extra_inputs' => [
99                                         ['name' => 'id'    , 'value' => $_POST['id']],
100                                         ['name' => 'delete', 'value' => 'x']
101                                 ],
102                                 '$confirm' => L10n::t('Delete Video'),
103                                 '$confirm_url' => $drop_url,
104                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
105                                 '$cancel' => L10n::t('Cancel'),
106
107                         ]);
108
109                         $a->error = 1; // Set $a->error so the other module functions don't execute
110
111                         return;
112                 }
113
114                 $video_id = $_POST['id'];
115
116                 if (Attach::exists(['id' => $video_id, 'uid' => local_user()])) {
117                         // delete the attachment
118                         Attach::delete(['id' => $video_id, 'uid' => local_user()]);
119
120                         // delete items where the attach is used
121                         Item::deleteForUser(['`attach` LIKE ? AND `uid` = ?',
122                                 '%attach/' . $video_id . '%',
123                                 local_user()
124                         ], local_user());
125                 }
126
127                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
128                 return; // NOTREACHED
129         }
130
131         $a->internalRedirect('videos/' . $a->data['user']['nickname']);
132 }
133
134 function videos_content(App $a)
135 {
136         // URLs (most aren't currently implemented):
137         // videos/name
138         // videos/name/upload
139         // videos/name/upload/xxxxx (xxxxx is album name)
140         // videos/name/album/xxxxx
141         // videos/name/album/xxxxx/edit
142         // videos/name/video/xxxxx
143         // videos/name/video/xxxxx/edit
144
145
146         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
147                 notice(L10n::t('Public access denied.') . EOL);
148                 return;
149         }
150
151         if (empty($a->data['user'])) {
152                 notice(L10n::t('No videos selected') . EOL );
153                 return;
154         }
155
156         //$phototypes = Photo::supportedTypes();
157
158         $_SESSION['video_return'] = $a->cmd;
159
160         //
161         // Parse arguments
162         //
163         if ($a->argc > 3) {
164                 $datatype = $a->argv[2];
165         } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
166                 $datatype = 'upload';
167         } else {
168                 $datatype = 'summary';
169         }
170
171         //
172         // Setup permissions structures
173         //
174         $can_post       = false;
175         $visitor        = 0;
176         $contact        = null;
177         $remote_contact = false;
178         $contact_id     = 0;
179
180         $owner_uid = $a->data['user']['uid'];
181
182         $community_page = (($a->data['user']['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
183
184         if ((local_user()) && (local_user() == $owner_uid)) {
185                 $can_post = true;
186         } elseif ($community_page && remote_user()) {
187                 if (!empty($_SESSION['remote'])) {
188                         foreach ($_SESSION['remote'] as $v) {
189                                 if ($v['uid'] == $owner_uid) {
190                                         $contact_id = $v['cid'];
191                                         break;
192                                 }
193                         }
194                 }
195
196                 if ($contact_id > 0) {
197                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
198                                 intval($contact_id),
199                                 intval($owner_uid)
200                         );
201
202                         if (DBA::isResult($r)) {
203                                 $can_post = true;
204                                 $remote_contact = true;
205                                 $visitor = $contact_id;
206                         }
207                 }
208         }
209
210         $groups = [];
211
212         // perhaps they're visiting - but not a community page, so they wouldn't have write access
213         if (remote_user() && (!$visitor)) {
214                 $contact_id = 0;
215
216                 if (!empty($_SESSION['remote'])) {
217                         foreach($_SESSION['remote'] as $v) {
218                                 if($v['uid'] == $owner_uid) {
219                                         $contact_id = $v['cid'];
220                                         break;
221                                 }
222                         }
223                 }
224
225                 if ($contact_id > 0) {
226                         $groups = Group::getIdsByContactId($contact_id);
227                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
228                                 intval($contact_id),
229                                 intval($owner_uid)
230                         );
231
232                         if (DBA::isResult($r)) {
233                                 $remote_contact = true;
234                         }
235                 }
236         }
237
238         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
239                 notice(L10n::t('Access to this item is restricted.') . EOL);
240                 return;
241         }
242
243         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
244
245         $o = "";
246
247         // tabs
248         $_is_owner = (local_user() && (local_user() == $owner_uid));
249         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
250
251         //
252         // dispatch request
253         //
254         if ($datatype === 'upload') {
255                 return; // no uploading for now
256
257                 // DELETED -- look at mod/photos.php if you want to implement
258         }
259
260         if ($datatype === 'album') {
261                 return; // no albums for now
262
263                 // DELETED -- look at mod/photos.php if you want to implement
264         }
265
266
267         if ($datatype === 'video') {
268                 return; // no single video view for now
269
270                 // DELETED -- look at mod/photos.php if you want to implement
271         }
272
273         // Default - show recent videos (no upload link for now)
274         //$o = '';
275
276         $total = 0;
277         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
278                 $sql_extra GROUP BY hash",
279                 intval($a->data['user']['uid'])
280         );
281         if (DBA::isResult($r)) {
282                 $total = count($r);
283         }
284
285         $pager = new Pager($a->query_string, 20);
286
287         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
288                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
289                 FROM `attach`
290                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
291                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
292                 intval($a->data['user']['uid']),
293                 $pager->getStart(),
294                 $pager->getItemsPerPage()
295         );
296
297         $videos = [];
298
299         if (DBA::isResult($r)) {
300                 foreach ($r as $rr) {
301                         $alt_e = $rr['filename'];
302                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
303                         $rr['album'] = '';
304                         $name_e = $rr['album'];
305
306                         $videos[] = [
307                                 'id'       => $rr['id'],
308                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
309                                 'title'    => L10n::t('View Video'),
310                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
311                                 'alt'      => $alt_e,
312                                 'mime'     => $rr['filetype'],
313                                 'album' => [
314                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
315                                         'name'  => $name_e,
316                                         'alt'   => L10n::t('View Album'),
317                                 ],
318                         ];
319                 }
320         }
321
322         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
323         $o .= Renderer::replaceMacros($tpl, [
324                 '$title'      => L10n::t('Recent Videos'),
325                 '$can_post'   => $can_post,
326                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
327                 '$videos'     => $videos,
328                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
329         ]);
330
331         $o .= $pager->renderFull($total);
332
333         return $o;
334 }