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