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