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