]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Rename dbesc to DBA::escape
[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\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Group;
14 use Friendica\Model\Item;
15 use Friendica\Model\Profile;
16 use Friendica\Protocol\DFRN;
17
18 require_once 'include/items.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                         DBA::escape($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, [
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'] . '">' . L10n::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'] === L10n::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" >' .L10n::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,[
102                         '$baseurl' => System::baseUrl(),
103                 ]);
104
105                 $tpl = get_markup_template("videos_end.tpl");
106                 $a->page['end'] .= replace_macros($tpl,[
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'), [
135                                 '$method' => 'post',
136                                 '$message' => L10n::t('Do you really want to delete this video?'),
137                                 '$extra_inputs' => [
138                                         ['name'=>'id', 'value'=> $_POST['id']],
139                                         ['name'=>'delete', 'value'=>'x']
140                                 ],
141                                 '$confirm' => L10n::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' => L10n::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                         DBA::escape($video_id)
156                 );
157
158                 if (DBA::isResult($r)) {
159                         q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
160                                 intval(local_user()),
161                                 DBA::escape($video_id)
162                         );
163                         $i = q("SELECT `id` FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
164                                 DBA::escape($video_id),
165                                 intval(local_user())
166                         );
167
168                         if (DBA::isResult($i)) {
169                                 Item::deleteForUser(['id' => $i[0]['id']], local_user());
170                         }
171                 }
172
173                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
174                 return; // NOTREACHED
175         }
176
177         goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
178
179 }
180
181
182
183 function videos_content(App $a) {
184
185         // URLs (most aren't currently implemented):
186         // videos/name
187         // videos/name/upload
188         // videos/name/upload/xxxxx (xxxxx is album name)
189         // videos/name/album/xxxxx
190         // videos/name/album/xxxxx/edit
191         // videos/name/video/xxxxx
192         // videos/name/video/xxxxx/edit
193
194
195         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
196                 notice(L10n::t('Public access denied.') . EOL);
197                 return;
198         }
199
200         require_once('include/security.php');
201         require_once('include/conversation.php');
202
203         if(! x($a->data,'user')) {
204                 notice(L10n::t('No videos selected') . EOL );
205                 return;
206         }
207
208         //$phototypes = Photo::supportedTypes();
209
210         $_SESSION['video_return'] = $a->cmd;
211
212         //
213         // Parse arguments
214         //
215
216         if($a->argc > 3) {
217                 $datatype = $a->argv[2];
218                 $datum = $a->argv[3];
219         }
220         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
221                 $datatype = 'upload';
222         else
223                 $datatype = 'summary';
224
225         if($a->argc > 4)
226                 $cmd = $a->argv[4];
227         else
228                 $cmd = 'view';
229
230         //
231         // Setup permissions structures
232         //
233
234         $can_post       = false;
235         $visitor        = 0;
236         $contact        = null;
237         $remote_contact = false;
238         $contact_id     = 0;
239
240         $owner_uid = $a->data['user']['uid'];
241
242         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
243
244         if((local_user()) && (local_user() == $owner_uid))
245                 $can_post = true;
246         else {
247                 if($community_page && remote_user()) {
248                         if(is_array($_SESSION['remote'])) {
249                                 foreach($_SESSION['remote'] as $v) {
250                                         if($v['uid'] == $owner_uid) {
251                                                 $contact_id = $v['cid'];
252                                                 break;
253                                         }
254                                 }
255                         }
256                         if($contact_id) {
257
258                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
259                                         intval($contact_id),
260                                         intval($owner_uid)
261                                 );
262                                 if (DBA::isResult($r)) {
263                                         $can_post = true;
264                                         $contact = $r[0];
265                                         $remote_contact = true;
266                                         $visitor = $contact_id;
267                                 }
268                         }
269                 }
270         }
271
272         $groups = [];
273
274         // perhaps they're visiting - but not a community page, so they wouldn't have write access
275         if(remote_user() && (! $visitor)) {
276                 $contact_id = 0;
277                 if(is_array($_SESSION['remote'])) {
278                         foreach($_SESSION['remote'] as $v) {
279                                 if($v['uid'] == $owner_uid) {
280                                         $contact_id = $v['cid'];
281                                         break;
282                                 }
283                         }
284                 }
285                 if($contact_id) {
286                         $groups = Group::getIdsByContactId($contact_id);
287                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
288                                 intval($contact_id),
289                                 intval($owner_uid)
290                         );
291                         if (DBA::isResult($r)) {
292                                 $contact = $r[0];
293                                 $remote_contact = true;
294                         }
295                 }
296         }
297
298         if(! $remote_contact) {
299                 if(local_user()) {
300                         $contact_id = $_SESSION['cid'];
301                         $contact = $a->contact;
302                 }
303         }
304
305         if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
306                 notice(L10n::t('Access to this item is restricted.') . EOL);
307                 return;
308         }
309
310         $sql_extra = permissions_sql($owner_uid, $remote_contact, $groups);
311
312         $o = "";
313
314         // tabs
315         $_is_owner = (local_user() && (local_user() == $owner_uid));
316         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
317
318         //
319         // dispatch request
320         //
321
322
323         if($datatype === 'upload') {
324                 return; // no uploading for now
325
326                 // DELETED -- look at mod/photos.php if you want to implement
327         }
328
329         if($datatype === 'album') {
330
331                 return; // no albums for now
332
333                 // DELETED -- look at mod/photos.php if you want to implement
334         }
335
336
337         if($datatype === 'video') {
338
339                 return; // no single video view for now
340
341                 // DELETED -- look at mod/photos.php if you want to implement
342         }
343
344         // Default - show recent videos (no upload link for now)
345         //$o = '';
346
347         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
348                 $sql_extra GROUP BY hash",
349                 intval($a->data['user']['uid'])
350         );
351         if (DBA::isResult($r)) {
352                 $a->set_pager_total(count($r));
353                 $a->set_pager_itemspage(20);
354         }
355
356         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
357                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
358                 FROM `attach`
359                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
360                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
361                 intval($a->data['user']['uid']),
362                 intval($a->pager['start']),
363                 intval($a->pager['itemspage'])
364         );
365
366
367
368         $videos = [];
369         if (DBA::isResult($r)) {
370                 foreach ($r as $rr) {
371                         $alt_e = $rr['filename'];
372                         $name_e = $rr['album'];
373
374                         $videos[] = [
375                                 'id'       => $rr['id'],
376                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['resource-id'],
377                                 'title'    => L10n::t('View Video'),
378                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
379                                 'alt'      => $alt_e,
380                                 'mime'     => $rr['filetype'],
381                                 'album' => [
382                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
383                                         'name'  => $name_e,
384                                         'alt'   => L10n::t('View Album'),
385                                 ],
386
387                         ];
388                 }
389         }
390
391         $tpl = get_markup_template('videos_recent.tpl');
392         $o .= replace_macros($tpl, [
393                 '$title'      => L10n::t('Recent Videos'),
394                 '$can_post'   => $can_post,
395                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl().'/videos/'.$a->data['user']['nickname'].'/upload'],
396                 '$videos'     => $videos,
397                 '$delete_url' => (($can_post)?System::baseUrl().'/videos/'.$a->data['user']['nickname']:False)
398         ]);
399
400
401         $o .= paginate($a);
402         return $o;
403 }