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