]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Small fixes
[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\Content\Pager;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Attach;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Group;
17 use Friendica\Model\Item;
18 use Friendica\Model\Profile;
19 use Friendica\Protocol\DFRN;
20 use Friendica\Util\Security;
21
22 function videos_init(App $a)
23 {
24         if ($a->argc > 1) {
25                 DFRN::autoRedir($a, $a->argv[1]);
26         }
27
28         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
29                 return;
30         }
31
32         Nav::setSelected('home');
33
34         $o = '';
35
36         if ($a->argc > 1) {
37                 $nick = $a->argv[1];
38                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
39                         DBA::escape($nick)
40                 );
41
42                 if (!DBA::isResult($user)) {
43                         return;
44                 }
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 = Renderer::getMarkupTemplate("vcard-widget.tpl");
54
55                 $vcard_widget = Renderer::replaceMacros($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                 // If not there, create 'aside' empty
64                 if (!isset($a->page['aside'])) {
65                         $a->page['aside'] = '';
66                 }
67
68                 $a->page['aside'] .= $vcard_widget;
69
70                 $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
71                 $a->page['htmlhead'] .= Renderer::replaceMacros($tpl,[
72                         '$baseurl' => System::baseUrl(),
73                 ]);
74         }
75
76         return;
77 }
78
79 function videos_post(App $a)
80 {
81         $owner_uid = $a->data['user']['uid'];
82
83         if (local_user() != $owner_uid) {
84                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
85         }
86
87         if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
88                 // Check if we should do HTML-based delete confirmation
89                 if (empty($_REQUEST['confirm'])) {
90                         if (!empty($_REQUEST['canceled'])) {
91                                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
92                         }
93
94                         $drop_url = $a->query_string;
95
96                         $a->page['content'] = Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
97                                 '$method' => 'post',
98                                 '$message' => L10n::t('Do you really want to delete this video?'),
99                                 '$extra_inputs' => [
100                                         ['name' => 'id'    , 'value' => $_POST['id']],
101                                         ['name' => 'delete', 'value' => 'x']
102                                 ],
103                                 '$confirm' => L10n::t('Delete Video'),
104                                 '$confirm_url' => $drop_url,
105                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
106                                 '$cancel' => L10n::t('Cancel'),
107
108                         ]);
109
110                         $a->error = 1; // Set $a->error so the other module functions don't execute
111
112                         return;
113                 }
114
115                 $video_id = $_POST['id'];
116
117                 if (Attach::exists(['id' => $video_id, 'uid' => local_user()])) {
118                         // delete the attachment
119                         Attach::delete(['id' => $video_id, 'uid' => local_user()]);
120
121                         // delete items where the attach is used
122                         Item::deleteForUser(['`attach` LIKE ? AND `uid` = ?',
123                                 '%attach/' . $video_id . '%',
124                                 local_user()
125                         ], local_user());
126                 }
127
128                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
129                 return; // NOTREACHED
130         }
131
132         $a->internalRedirect('videos/' . $a->data['user']['nickname']);
133 }
134
135 function videos_content(App $a)
136 {
137         // URLs (most aren't currently implemented):
138         // videos/name
139         // videos/name/upload
140         // videos/name/upload/xxxxx (xxxxx is album name)
141         // videos/name/album/xxxxx
142         // videos/name/album/xxxxx/edit
143         // videos/name/video/xxxxx
144         // videos/name/video/xxxxx/edit
145
146
147         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
148                 notice(L10n::t('Public access denied.') . EOL);
149                 return;
150         }
151
152         if (empty($a->data['user'])) {
153                 notice(L10n::t('No videos selected') . EOL );
154                 return;
155         }
156
157         //$phototypes = Photo::supportedTypes();
158
159         $_SESSION['video_return'] = $a->cmd;
160
161         //
162         // Parse arguments
163         //
164         if ($a->argc > 3) {
165                 $datatype = $a->argv[2];
166                 $datum = $a->argv[3];
167         } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
168                 $datatype = 'upload';
169         } else {
170                 $datatype = 'summary';
171         }
172
173         if ($a->argc > 4) {
174                 $cmd = $a->argv[4];
175         } else {
176                 $cmd = 'view';
177         }
178
179         //
180         // Setup permissions structures
181         //
182         $can_post       = false;
183         $visitor        = 0;
184         $contact        = null;
185         $remote_contact = false;
186         $contact_id     = 0;
187
188         $owner_uid = $a->data['user']['uid'];
189
190         $community_page = (($a->data['user']['page-flags'] == Contact::PAGE_COMMUNITY) ? true : false);
191
192         if ((local_user()) && (local_user() == $owner_uid)) {
193                 $can_post = true;
194         } elseif ($community_page && remote_user()) {
195                 if (!empty($_SESSION['remote'])) {
196                         foreach ($_SESSION['remote'] as $v) {
197                                 if ($v['uid'] == $owner_uid) {
198                                         $contact_id = $v['cid'];
199                                         break;
200                                 }
201                         }
202                 }
203
204                 if ($contact_id > 0) {
205                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
206                                 intval($contact_id),
207                                 intval($owner_uid)
208                         );
209
210                         if (DBA::isResult($r)) {
211                                 $can_post = true;
212                                 $contact = $r[0];
213                                 $remote_contact = true;
214                                 $visitor = $contact_id;
215                         }
216                 }
217         }
218
219         $groups = [];
220
221         // perhaps they're visiting - but not a community page, so they wouldn't have write access
222         if (remote_user() && (!$visitor)) {
223                 $contact_id = 0;
224
225                 if (!empty($_SESSION['remote'])) {
226                         foreach($_SESSION['remote'] as $v) {
227                                 if($v['uid'] == $owner_uid) {
228                                         $contact_id = $v['cid'];
229                                         break;
230                                 }
231                         }
232                 }
233
234                 if ($contact_id > 0) {
235                         $groups = Group::getIdsByContactId($contact_id);
236                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
237                                 intval($contact_id),
238                                 intval($owner_uid)
239                         );
240
241                         if (DBA::isResult($r)) {
242                                 $contact = $r[0];
243                                 $remote_contact = true;
244                         }
245                 }
246         }
247
248         if (!$remote_contact && local_user()) {
249                 $contact_id = $_SESSION['cid'];
250                 $contact = $a->contact;
251         }
252
253         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
254                 notice(L10n::t('Access to this item is restricted.') . EOL);
255                 return;
256         }
257
258         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
259
260         $o = "";
261
262         // tabs
263         $_is_owner = (local_user() && (local_user() == $owner_uid));
264         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
265
266         //
267         // dispatch request
268         //
269         if ($datatype === 'upload') {
270                 return; // no uploading for now
271
272                 // DELETED -- look at mod/photos.php if you want to implement
273         }
274
275         if ($datatype === 'album') {
276                 return; // no albums for now
277
278                 // DELETED -- look at mod/photos.php if you want to implement
279         }
280
281
282         if ($datatype === 'video') {
283                 return; // no single video view for now
284
285                 // DELETED -- look at mod/photos.php if you want to implement
286         }
287
288         // Default - show recent videos (no upload link for now)
289         //$o = '';
290
291         $total = 0;
292         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
293                 $sql_extra GROUP BY hash",
294                 intval($a->data['user']['uid'])
295         );
296         if (DBA::isResult($r)) {
297                 $total = count($r);
298         }
299
300         $pager = new Pager($a->query_string, 20);
301
302         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
303                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
304                 FROM `attach`
305                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
306                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
307                 intval($a->data['user']['uid']),
308                 $pager->getStart(),
309                 $pager->getItemsPerPage()
310         );
311
312         $videos = [];
313
314         if (DBA::isResult($r)) {
315                 foreach ($r as $rr) {
316                         $alt_e = $rr['filename'];
317                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
318                         $rr['album'] = '';
319                         $name_e = $rr['album'];
320
321                         $videos[] = [
322                                 'id'       => $rr['id'],
323                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
324                                 'title'    => L10n::t('View Video'),
325                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
326                                 'alt'      => $alt_e,
327                                 'mime'     => $rr['filetype'],
328                                 'album' => [
329                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
330                                         'name'  => $name_e,
331                                         'alt'   => L10n::t('View Album'),
332                                 ],
333                         ];
334                 }
335         }
336
337         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
338         $o .= Renderer::replaceMacros($tpl, [
339                 '$title'      => L10n::t('Recent Videos'),
340                 '$can_post'   => $can_post,
341                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
342                 '$videos'     => $videos,
343                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
344         ]);
345
346         $o .= $pager->renderFull($total);
347
348         return $o;
349 }