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