Burn notices, burn
[friendica.git/.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\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Group;
14 use Friendica\Model\Item;
15 use Friendica\Model\Profile;
16 use Friendica\Protocol\DFRN;
17
18 require_once 'include/items.php';
19 require_once 'include/security.php';
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 = get_markup_template("vcard-widget.tpl");
53
54                 $vcard_widget = replace_macros($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 = permissions_sql($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 = get_markup_template("videos_head.tpl");
105                 $a->page['htmlhead'] .= replace_macros($tpl,[
106                         '$baseurl' => System::baseUrl(),
107                 ]);
108
109                 $tpl = get_markup_template("videos_end.tpl");
110                 $a->page['end'] .= replace_macros($tpl,[
111                         '$baseurl' => System::baseUrl(),
112                 ]);
113
114         }
115
116         return;
117 }
118
119 function videos_post(App $a)
120 {
121         $owner_uid = $a->data['user']['uid'];
122
123         if (local_user() != $owner_uid) {
124                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
125         }
126
127         if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
128                 // Check if we should do HTML-based delete confirmation
129                 if (empty($_REQUEST['confirm'])) {
130                         if (!empty($_REQUEST['canceled'])) {
131                                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
132                         }
133
134                         $drop_url = $a->query_string;
135
136                         $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), [
137                                 '$method' => 'post',
138                                 '$message' => L10n::t('Do you really want to delete this video?'),
139                                 '$extra_inputs' => [
140                                         ['name' => 'id'    , 'value' => $_POST['id']],
141                                         ['name' => 'delete', 'value' => 'x']
142                                 ],
143                                 '$confirm' => L10n::t('Delete Video'),
144                                 '$confirm_url' => $drop_url,
145                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
146                                 '$cancel' => L10n::t('Cancel'),
147
148                         ]);
149
150                         $a->error = 1; // Set $a->error so the other module functions don't execute
151
152                         return;
153                 }
154
155                 $video_id = $_POST['id'];
156
157                 $r = q("SELECT `id`  FROM `attach` WHERE `uid` = %d AND `id` = '%s' LIMIT 1",
158                         intval(local_user()),
159                         DBA::escape($video_id)
160                 );
161
162                 if (DBA::isResult($r)) {
163                         q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
164                                 intval(local_user()),
165                                 DBA::escape($video_id)
166                         );
167
168                         $i = q("SELECT `id` FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
169                                 DBA::escape($video_id),
170                                 intval(local_user())
171                         );
172
173                         if (DBA::isResult($i)) {
174                                 Item::deleteForUser(['id' => $i[0]['id']], local_user());
175                         }
176                 }
177
178                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
179                 return; // NOTREACHED
180         }
181
182         goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
183 }
184
185 function videos_content(App $a)
186 {
187         // URLs (most aren't currently implemented):
188         // videos/name
189         // videos/name/upload
190         // videos/name/upload/xxxxx (xxxxx is album name)
191         // videos/name/album/xxxxx
192         // videos/name/album/xxxxx/edit
193         // videos/name/video/xxxxx
194         // videos/name/video/xxxxx/edit
195
196
197         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
198                 notice(L10n::t('Public access denied.') . EOL);
199                 return;
200         }
201
202         require_once 'include/security.php';
203         require_once 'include/conversation.php';
204
205         if (empty($a->data['user'])) {
206                 notice(L10n::t('No videos selected') . EOL );
207                 return;
208         }
209
210         //$phototypes = Photo::supportedTypes();
211
212         $_SESSION['video_return'] = $a->cmd;
213
214         //
215         // Parse arguments
216         //
217         if ($a->argc > 3) {
218                 $datatype = $a->argv[2];
219                 $datum = $a->argv[3];
220         } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
221                 $datatype = 'upload';
222         } else {
223                 $datatype = 'summary';
224         }
225
226         if ($a->argc > 4) {
227                 $cmd = $a->argv[4];
228         } else {
229                 $cmd = 'view';
230         }
231
232         //
233         // Setup permissions structures
234         //
235         $can_post       = false;
236         $visitor        = 0;
237         $contact        = null;
238         $remote_contact = false;
239         $contact_id     = 0;
240
241         $owner_uid = $a->data['user']['uid'];
242
243         $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
244
245         if ((local_user()) && (local_user() == $owner_uid)) {
246                 $can_post = true;
247         } elseif ($community_page && remote_user()) {
248                 if (!empty($_SESSION['remote'])) {
249                         foreach ($_SESSION['remote'] as $v) {
250                                 if ($v['uid'] == $owner_uid) {
251                                         $contact_id = $v['cid'];
252                                         break;
253                                 }
254                         }
255                 }
256
257                 if ($contact_id > 0) {
258                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
259                                 intval($contact_id),
260                                 intval($owner_uid)
261                         );
262
263                         if (DBA::isResult($r)) {
264                                 $can_post = true;
265                                 $contact = $r[0];
266                                 $remote_contact = true;
267                                 $visitor = $contact_id;
268                         }
269                 }
270         }
271
272         $groups = [];
273
274         // perhaps they're visiting - but not a community page, so they wouldn't have write access
275         if (remote_user() && (!$visitor)) {
276                 $contact_id = 0;
277
278                 if (!empty($_SESSION['remote'])) {
279                         foreach($_SESSION['remote'] as $v) {
280                                 if($v['uid'] == $owner_uid) {
281                                         $contact_id = $v['cid'];
282                                         break;
283                                 }
284                         }
285                 }
286
287                 if ($contact_id > 0) {
288                         $groups = Group::getIdsByContactId($contact_id);
289                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
290                                 intval($contact_id),
291                                 intval($owner_uid)
292                         );
293
294                         if (DBA::isResult($r)) {
295                                 $contact = $r[0];
296                                 $remote_contact = true;
297                         }
298                 }
299         }
300
301         if (!$remote_contact && local_user()) {
302                 $contact_id = $_SESSION['cid'];
303                 $contact = $a->contact;
304         }
305
306         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
307                 notice(L10n::t('Access to this item is restricted.') . EOL);
308                 return;
309         }
310
311         $sql_extra = permissions_sql($owner_uid, $remote_contact, $groups);
312
313         $o = "";
314
315         // tabs
316         $_is_owner = (local_user() && (local_user() == $owner_uid));
317         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
318
319         //
320         // dispatch request
321         //
322         if ($datatype === 'upload') {
323                 return; // no uploading for now
324
325                 // DELETED -- look at mod/photos.php if you want to implement
326         }
327
328         if ($datatype === 'album') {
329                 return; // no albums for now
330
331                 // DELETED -- look at mod/photos.php if you want to implement
332         }
333
334
335         if ($datatype === 'video') {
336                 return; // no single video view for now
337
338                 // DELETED -- look at mod/photos.php if you want to implement
339         }
340
341         // Default - show recent videos (no upload link for now)
342         //$o = '';
343
344         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
345                 $sql_extra GROUP BY hash",
346                 intval($a->data['user']['uid'])
347         );
348
349         if (DBA::isResult($r)) {
350                 $a->set_pager_total(count($r));
351                 $a->set_pager_itemspage(20);
352         }
353
354         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
355                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
356                 FROM `attach`
357                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
358                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
359                 intval($a->data['user']['uid']),
360                 intval($a->pager['start']),
361                 intval($a->pager['itemspage'])
362         );
363
364         $videos = [];
365
366         if (DBA::isResult($r)) {
367                 foreach ($r as $rr) {
368                         $alt_e = $rr['filename'];
369                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
370                         $rr['album'] = '';
371                         $name_e = $rr['album'];
372
373                         $videos[] = [
374                                 'id'       => $rr['id'],
375                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
376                                 'title'    => L10n::t('View Video'),
377                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
378                                 'alt'      => $alt_e,
379                                 'mime'     => $rr['filetype'],
380                                 'album' => [
381                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
382                                         'name'  => $name_e,
383                                         'alt'   => L10n::t('View Album'),
384                                 ],
385                         ];
386                 }
387         }
388
389         $tpl = get_markup_template('videos_recent.tpl');
390         $o .= replace_macros($tpl, [
391                 '$title'      => L10n::t('Recent Videos'),
392                 '$can_post'   => $can_post,
393                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
394                 '$videos'     => $videos,
395                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
396         ]);
397
398         $o .= paginate($a);
399
400         return $o;
401 }