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