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