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