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