]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Merge pull request #4404 from annando/item-isolation
[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/acl_selectors.php';
23 require_once 'include/bbcode.php';
24 require_once 'include/security.php';
25
26 function videos_init(App $a) {
27
28         if($a->argc > 1)
29                 DFRN::autoRedir($a, $a->argv[1]);
30
31         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
32                 return;
33         }
34
35         Nav::setSelected('home');
36
37         $o = '';
38
39         if($a->argc > 1) {
40                 $nick = $a->argv[1];
41                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
42                         dbesc($nick)
43                 );
44
45                 if(! count($user))
46                         return;
47
48                 $a->data['user'] = $user[0];
49                 $a->profile_uid = $user[0]['uid'];
50
51                 $profile = Profile::getByNickname($nick, $a->profile_uid);
52
53                 $account_type = Contact::getAccountType($profile);
54
55                 $tpl = get_markup_template("vcard-widget.tpl");
56
57                 $vcard_widget = replace_macros($tpl, [
58                         '$name' => $profile['name'],
59                         '$photo' => $profile['photo'],
60                         '$addr' => defaults($profile, 'addr', ''),
61                         '$account_type' => $account_type,
62                         '$pdesc' => defaults($profile, 'pdesc', ''),
63                 ]);
64
65
66                 /*$sql_extra = permissions_sql($a->data['user']['uid']);
67
68                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra order by created desc",
69                         intval($a->data['user']['uid'])
70                 );
71
72                 if(count($albums)) {
73                         $a->data['albums'] = $albums;
74
75                         $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);
76
77                         if($albums_visible) {
78                                 $o .= '<div id="sidebar-photos-albums" class="widget">';
79                                 $o .= '<h3>' . '<a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '">' . L10n::t('Photo Albums') . '</a></h3>';
80
81                                 $o .= '<ul>';
82                                 foreach($albums as $album) {
83
84                                         // don't show contact photos. We once translated this name, but then you could still access it under
85                                         // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
86
87                                         if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === L10n::t('Contact Photos')))
88                                                 continue;
89                                         $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
90                                 }
91                                 $o .= '</ul>';
92                         }
93                         if(local_user() && $a->data['user']['uid'] == local_user()) {
94                                 $o .= '<div id="photo-albums-upload-link"><a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .L10n::t('Upload New Photos') . '</a></div>';
95                         }
96
97                         $o .= '</div>';
98                 }*/
99
100                 if(! x($a->page,'aside'))
101                         $a->page['aside'] = '';
102                 $a->page['aside'] .= $vcard_widget;
103
104
105                 $tpl = get_markup_template("videos_head.tpl");
106                 $a->page['htmlhead'] .= replace_macros($tpl,[
107                         '$baseurl' => System::baseUrl(),
108                 ]);
109
110                 $tpl = get_markup_template("videos_end.tpl");
111                 $a->page['end'] .= replace_macros($tpl,[
112                         '$baseurl' => System::baseUrl(),
113                 ]);
114
115         }
116
117         return;
118 }
119
120
121
122 function videos_post(App $a) {
123
124         $owner_uid = $a->data['user']['uid'];
125
126         if (local_user() != $owner_uid) {
127                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
128         }
129
130         if (($a->argc == 2) && x($_POST,'delete') && x($_POST, 'id')) {
131
132                 // Check if we should do HTML-based delete confirmation
133                 if (!x($_REQUEST,'confirm')) {
134                         if (x($_REQUEST,'canceled')) {
135                                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
136                         }
137
138                         $drop_url = $a->query_string;
139                         $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), [
140                                 '$method' => 'post',
141                                 '$message' => L10n::t('Do you really want to delete this video?'),
142                                 '$extra_inputs' => [
143                                         ['name'=>'id', 'value'=> $_POST['id']],
144                                         ['name'=>'delete', 'value'=>'x']
145                                 ],
146                                 '$confirm' => L10n::t('Delete Video'),
147                                 '$confirm_url' => $drop_url,
148                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
149                                 '$cancel' => L10n::t('Cancel'),
150
151                         ]);
152                         $a->error = 1; // Set $a->error so the other module functions don't execute
153                         return;
154                 }
155
156                 $video_id = $_POST['id'];
157
158                 $r = q("SELECT `id`  FROM `attach` WHERE `uid` = %d AND `id` = '%s' LIMIT 1",
159                         intval(local_user()),
160                         dbesc($video_id)
161                 );
162
163                 if (DBM::is_result($r)) {
164                         q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
165                                 intval(local_user()),
166                                 dbesc($video_id)
167                         );
168                         $i = q("SELECT `id` FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
169                                 dbesc($video_id),
170                                 intval(local_user())
171                         );
172
173                         if (DBM::is_result($i)) {
174                                 Item::deleteById($i[0]['id']);
175                         }
176                 }
177
178                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
179                 return; // NOTREACHED
180         }
181
182         goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
183
184 }
185
186
187
188 function videos_content(App $a) {
189
190         // URLs (most aren't currently implemented):
191         // videos/name
192         // videos/name/upload
193         // videos/name/upload/xxxxx (xxxxx is album name)
194         // videos/name/album/xxxxx
195         // videos/name/album/xxxxx/edit
196         // videos/name/video/xxxxx
197         // videos/name/video/xxxxx/edit
198
199
200         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
201                 notice(L10n::t('Public access denied.') . EOL);
202                 return;
203         }
204
205
206         require_once('include/bbcode.php');
207         require_once('include/security.php');
208         require_once('include/conversation.php');
209
210         if(! x($a->data,'user')) {
211                 notice(L10n::t('No videos selected') . EOL );
212                 return;
213         }
214
215         //$phototypes = Photo::supportedTypes();
216
217         $_SESSION['video_return'] = $a->cmd;
218
219         //
220         // Parse arguments
221         //
222
223         if($a->argc > 3) {
224                 $datatype = $a->argv[2];
225                 $datum = $a->argv[3];
226         }
227         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
228                 $datatype = 'upload';
229         else
230                 $datatype = 'summary';
231
232         if($a->argc > 4)
233                 $cmd = $a->argv[4];
234         else
235                 $cmd = 'view';
236
237         //
238         // Setup permissions structures
239         //
240
241         $can_post       = false;
242         $visitor        = 0;
243         $contact        = null;
244         $remote_contact = false;
245         $contact_id     = 0;
246
247         $owner_uid = $a->data['user']['uid'];
248
249         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
250
251         if((local_user()) && (local_user() == $owner_uid))
252                 $can_post = true;
253         else {
254                 if($community_page && remote_user()) {
255                         if(is_array($_SESSION['remote'])) {
256                                 foreach($_SESSION['remote'] as $v) {
257                                         if($v['uid'] == $owner_uid) {
258                                                 $contact_id = $v['cid'];
259                                                 break;
260                                         }
261                                 }
262                         }
263                         if($contact_id) {
264
265                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
266                                         intval($contact_id),
267                                         intval($owner_uid)
268                                 );
269                                 if (DBM::is_result($r)) {
270                                         $can_post = true;
271                                         $contact = $r[0];
272                                         $remote_contact = true;
273                                         $visitor = $contact_id;
274                                 }
275                         }
276                 }
277         }
278
279         $groups = [];
280
281         // perhaps they're visiting - but not a community page, so they wouldn't have write access
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 = Group::getIdsByContactId($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(L10n::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::getTabs($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 = [];
376         if (DBM::is_result($r)) {
377                 foreach ($r as $rr) {
378                         $alt_e = $rr['filename'];
379                         $name_e = $rr['album'];
380
381                         $videos[] = [
382                                 'id'       => $rr['id'],
383                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['resource-id'],
384                                 'title'    => L10n::t('View Video'),
385                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
386                                 'alt'      => $alt_e,
387                                 'mime'     => $rr['filetype'],
388                                 'album' => [
389                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
390                                         'name'  => $name_e,
391                                         'alt'   => L10n::t('View Album'),
392                                 ],
393
394                         ];
395                 }
396         }
397
398         $tpl = get_markup_template('videos_recent.tpl');
399         $o .= replace_macros($tpl, [
400                 '$title'      => L10n::t('Recent Videos'),
401                 '$can_post'   => $can_post,
402                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl().'/videos/'.$a->data['user']['nickname'].'/upload'],
403                 '$videos'     => $videos,
404                 '$delete_url' => (($can_post)?System::baseUrl().'/videos/'.$a->data['user']['nickname']:False)
405         ]);
406
407
408         $o .= paginate($a);
409         return $o;
410 }