]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Merge pull request #6209 from MrPetovan/task/move-config-to-php-array
[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\Contact;
15 use Friendica\Model\Group;
16 use Friendica\Model\Item;
17 use Friendica\Model\Profile;
18 use Friendica\Protocol\DFRN;
19 use Friendica\Util\Security;
20
21 function videos_init(App $a)
22 {
23         if ($a->argc > 1) {
24                 DFRN::autoRedir($a, $a->argv[1]);
25         }
26
27         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
28                 return;
29         }
30
31         Nav::setSelected('home');
32
33         $o = '';
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                 /// @TODO Old-lost code?
63                 /*$sql_extra = Security::getPermissionsSQLByUserId($a->data['user']['uid']);
64
65                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra order by created desc",
66                         intval($a->data['user']['uid'])
67                 );
68
69                 if(count($albums)) {
70                         $a->data['albums'] = $albums;
71
72                         $albums_visible = ((intval($a->data['user']['hidewall']) && (!local_user()) && (!remote_user())) ? false : true);
73
74                         if($albums_visible) {
75                                 $o .= '<div id="sidebar-photos-albums" class="widget">';
76                                 $o .= '<h3>' . '<a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '">' . L10n::t('Photo Albums') . '</a></h3>';
77
78                                 $o .= '<ul>';
79                                 foreach($albums as $album) {
80
81                                         // don't show contact photos. We once translated this name, but then you could still access it under
82                                         // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
83
84                                         if((!strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === L10n::t('Contact Photos')))
85                                                 continue;
86                                         $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
87                                 }
88                                 $o .= '</ul>';
89                         }
90                         if(local_user() && $a->data['user']['uid'] == local_user()) {
91                                 $o .= '<div id="photo-albums-upload-link"><a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .L10n::t('Upload New Photos') . '</a></div>';
92                         }
93
94                         $o .= '</div>';
95                 }*/
96
97                 // If not there, create 'aside' empty
98                 if (!isset($a->page['aside'])) {
99                         $a->page['aside'] = '';
100                 }
101
102                 $a->page['aside'] .= $vcard_widget;
103
104                 $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
105                 $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
106                         '$baseurl' => System::baseUrl(),
107                 ]);
108         }
109
110         return;
111 }
112
113 function videos_post(App $a)
114 {
115         $owner_uid = $a->data['user']['uid'];
116
117         if (local_user() != $owner_uid) {
118                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
119         }
120
121         if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
122                 // Check if we should do HTML-based delete confirmation
123                 if (empty($_REQUEST['confirm'])) {
124                         if (!empty($_REQUEST['canceled'])) {
125                                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
126                         }
127
128                         $drop_url = $a->query_string;
129
130                         $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
131                                 '$method' => 'post',
132                                 '$message' => L10n::t('Do you really want to delete this video?'),
133                                 '$extra_inputs' => [
134                                         ['name' => 'id'    , 'value' => $_POST['id']],
135                                         ['name' => 'delete', 'value' => 'x']
136                                 ],
137                                 '$confirm' => L10n::t('Delete Video'),
138                                 '$confirm_url' => $drop_url,
139                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
140                                 '$cancel' => L10n::t('Cancel'),
141
142                         ]);
143
144                         $a->error = 1; // Set $a->error so the other module functions don't execute
145
146                         return;
147                 }
148
149                 $video_id = $_POST['id'];
150
151                 $r = q("SELECT `id`  FROM `attach` WHERE `uid` = %d AND `id` = '%s' LIMIT 1",
152                         intval(local_user()),
153                         DBA::escape($video_id)
154                 );
155
156                 if (DBA::isResult($r)) {
157                         q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
158                                 intval(local_user()),
159                                 DBA::escape($video_id)
160                         );
161
162                         $i = q("SELECT `id` FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
163                                 DBA::escape($video_id),
164                                 intval(local_user())
165                         );
166
167                         if (DBA::isResult($i)) {
168                                 Item::deleteForUser(['id' => $i[0]['id']], local_user());
169                         }
170                 }
171
172                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
173                 return; // NOTREACHED
174         }
175
176         $a->internalRedirect('videos/' . $a->data['user']['nickname']);
177 }
178
179 function videos_content(App $a)
180 {
181         // URLs (most aren't currently implemented):
182         // videos/name
183         // videos/name/upload
184         // videos/name/upload/xxxxx (xxxxx is album name)
185         // videos/name/album/xxxxx
186         // videos/name/album/xxxxx/edit
187         // videos/name/video/xxxxx
188         // videos/name/video/xxxxx/edit
189
190
191         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
192                 notice(L10n::t('Public access denied.') . EOL);
193                 return;
194         }
195
196         if (empty($a->data['user'])) {
197                 notice(L10n::t('No videos selected') . EOL );
198                 return;
199         }
200
201         //$phototypes = Photo::supportedTypes();
202
203         $_SESSION['video_return'] = $a->cmd;
204
205         //
206         // Parse arguments
207         //
208         if ($a->argc > 3) {
209                 $datatype = $a->argv[2];
210                 $datum = $a->argv[3];
211         } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
212                 $datatype = 'upload';
213         } else {
214                 $datatype = 'summary';
215         }
216
217         if ($a->argc > 4) {
218                 $cmd = $a->argv[4];
219         } else {
220                 $cmd = 'view';
221         }
222
223         //
224         // Setup permissions structures
225         //
226         $can_post       = false;
227         $visitor        = 0;
228         $contact        = null;
229         $remote_contact = false;
230         $contact_id     = 0;
231
232         $owner_uid = $a->data['user']['uid'];
233
234         $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
235
236         if ((local_user()) && (local_user() == $owner_uid)) {
237                 $can_post = true;
238         } elseif ($community_page && remote_user()) {
239                 if (!empty($_SESSION['remote'])) {
240                         foreach ($_SESSION['remote'] as $v) {
241                                 if ($v['uid'] == $owner_uid) {
242                                         $contact_id = $v['cid'];
243                                         break;
244                                 }
245                         }
246                 }
247
248                 if ($contact_id > 0) {
249                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
250                                 intval($contact_id),
251                                 intval($owner_uid)
252                         );
253
254                         if (DBA::isResult($r)) {
255                                 $can_post = true;
256                                 $contact = $r[0];
257                                 $remote_contact = true;
258                                 $visitor = $contact_id;
259                         }
260                 }
261         }
262
263         $groups = [];
264
265         // perhaps they're visiting - but not a community page, so they wouldn't have write access
266         if (remote_user() && (!$visitor)) {
267                 $contact_id = 0;
268
269                 if (!empty($_SESSION['remote'])) {
270                         foreach($_SESSION['remote'] as $v) {
271                                 if($v['uid'] == $owner_uid) {
272                                         $contact_id = $v['cid'];
273                                         break;
274                                 }
275                         }
276                 }
277
278                 if ($contact_id > 0) {
279                         $groups = Group::getIdsByContactId($contact_id);
280                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
281                                 intval($contact_id),
282                                 intval($owner_uid)
283                         );
284
285                         if (DBA::isResult($r)) {
286                                 $contact = $r[0];
287                                 $remote_contact = true;
288                         }
289                 }
290         }
291
292         if (!$remote_contact && local_user()) {
293                 $contact_id = $_SESSION['cid'];
294                 $contact = $a->contact;
295         }
296
297         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
298                 notice(L10n::t('Access to this item is restricted.') . EOL);
299                 return;
300         }
301
302         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
303
304         $o = "";
305
306         // tabs
307         $_is_owner = (local_user() && (local_user() == $owner_uid));
308         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
309
310         //
311         // dispatch request
312         //
313         if ($datatype === 'upload') {
314                 return; // no uploading for now
315
316                 // DELETED -- look at mod/photos.php if you want to implement
317         }
318
319         if ($datatype === 'album') {
320                 return; // no albums for now
321
322                 // DELETED -- look at mod/photos.php if you want to implement
323         }
324
325
326         if ($datatype === 'video') {
327                 return; // no single video view for now
328
329                 // DELETED -- look at mod/photos.php if you want to implement
330         }
331
332         // Default - show recent videos (no upload link for now)
333         //$o = '';
334
335         $total = 0;
336         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
337                 $sql_extra GROUP BY hash",
338                 intval($a->data['user']['uid'])
339         );
340         if (DBA::isResult($r)) {
341                 $total = count($r);
342         }
343
344         $pager = new Pager($a->query_string, 20);
345
346         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
347                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
348                 FROM `attach`
349                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
350                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
351                 intval($a->data['user']['uid']),
352                 $pager->getStart(),
353                 $pager->getItemsPerPage()
354         );
355
356         $videos = [];
357
358         if (DBA::isResult($r)) {
359                 foreach ($r as $rr) {
360                         $alt_e = $rr['filename'];
361                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
362                         $rr['album'] = '';
363                         $name_e = $rr['album'];
364
365                         $videos[] = [
366                                 'id'       => $rr['id'],
367                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
368                                 'title'    => L10n::t('View Video'),
369                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
370                                 'alt'      => $alt_e,
371                                 'mime'     => $rr['filetype'],
372                                 'album' => [
373                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
374                                         'name'  => $name_e,
375                                         'alt'   => L10n::t('View Album'),
376                                 ],
377                         ];
378                 }
379         }
380
381         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
382         $o .= Renderer::replaceMacros($tpl, [
383                 '$title'      => L10n::t('Recent Videos'),
384                 '$can_post'   => $can_post,
385                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
386                 '$videos'     => $videos,
387                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
388         ]);
389
390         $o .= $pager->renderFull($total);
391
392         return $o;
393 }