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