]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Reworked "remote" cookie handling
[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\Model\User;
20 use Friendica\Protocol\DFRN;
21 use Friendica\Util\Security;
22
23 function videos_init(App $a)
24 {
25         if ($a->argc > 1) {
26                 DFRN::autoRedir($a, $a->argv[1]);
27         }
28
29         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
30                 return;
31         }
32
33         Nav::setSelected('home');
34
35         if ($a->argc > 1) {
36                 $nick = $a->argv[1];
37                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
38                         DBA::escape($nick)
39                 );
40
41                 if (!DBA::isResult($user)) {
42                         return;
43                 }
44
45                 $a->data['user'] = $user[0];
46                 $a->profile_uid = $user[0]['uid'];
47
48                 $profile = Profile::getByNickname($nick, $a->profile_uid);
49
50                 $account_type = Contact::getAccountType($profile);
51
52                 $tpl = Renderer::getMarkupTemplate("widget/vcard.tpl");
53
54                 $vcard_widget = Renderer::replaceMacros($tpl, [
55                         '$name' => $profile['name'],
56                         '$photo' => $profile['photo'],
57                         '$addr' => defaults($profile, 'addr', ''),
58                         '$account_type' => $account_type,
59                         '$pdesc' => defaults($profile, 'pdesc', ''),
60                 ]);
61
62                 // If not there, create 'aside' empty
63                 if (!isset($a->page['aside'])) {
64                         $a->page['aside'] = '';
65                 }
66
67                 $a->page['aside'] .= $vcard_widget;
68
69                 $tpl = Renderer::getMarkupTemplate("videos_head.tpl");
70                 $a->page['htmlhead'] .= Renderer::replaceMacros($tpl);
71         }
72
73         return;
74 }
75
76 function videos_post(App $a)
77 {
78         $owner_uid = $a->data['user']['uid'];
79
80         if (local_user() != $owner_uid) {
81                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
82         }
83
84         if (($a->argc == 2) && !empty($_POST['delete']) && !empty($_POST['id'])) {
85                 $video_id = $_POST['id'];
86
87                 if (Attach::exists(['id' => $video_id, 'uid' => local_user()])) {
88                         // delete the attachment
89                         Attach::delete(['id' => $video_id, 'uid' => local_user()]);
90
91                         // delete items where the attach is used
92                         Item::deleteForUser(['`attach` LIKE ? AND `uid` = ?',
93                                 '%attach/' . $video_id . '%',
94                                 local_user()
95                         ], local_user());
96                 }
97
98                 $a->internalRedirect('videos/' . $a->data['user']['nickname']);
99                 return; // NOTREACHED
100         }
101
102         $a->internalRedirect('videos/' . $a->data['user']['nickname']);
103 }
104
105 function videos_content(App $a)
106 {
107         // URLs (most aren't currently implemented):
108         // videos/name
109         // videos/name/upload
110         // videos/name/upload/xxxxx (xxxxx is album name)
111         // videos/name/album/xxxxx
112         // videos/name/album/xxxxx/edit
113         // videos/name/video/xxxxx
114         // videos/name/video/xxxxx/edit
115
116
117         if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
118                 notice(L10n::t('Public access denied.') . EOL);
119                 return;
120         }
121
122         if (empty($a->data['user'])) {
123                 notice(L10n::t('No videos selected') . EOL );
124                 return;
125         }
126
127         //$phototypes = Photo::supportedTypes();
128
129         $_SESSION['video_return'] = $a->cmd;
130
131         //
132         // Parse arguments
133         //
134         if ($a->argc > 3) {
135                 $datatype = $a->argv[2];
136         } elseif(($a->argc > 2) && ($a->argv[2] === 'upload')) {
137                 $datatype = 'upload';
138         } else {
139                 $datatype = 'summary';
140         }
141
142         //
143         // Setup permissions structures
144         //
145         $can_post       = false;
146         $visitor        = 0;
147         $contact        = null;
148         $remote_contact = false;
149         $contact_id     = 0;
150
151         $owner_uid = $a->data['user']['uid'];
152
153         $community_page = (($a->data['user']['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
154
155         if ((local_user()) && (local_user() == $owner_uid)) {
156                 $can_post = true;
157         } elseif ($community_page && !empty(remote_user($owner_uid))) {
158                 $contact_id = remote_user($owner_uid);
159
160                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
161                         intval($contact_id),
162                         intval($owner_uid)
163                 );
164
165                 if (DBA::isResult($r)) {
166                         $can_post = true;
167                         $remote_contact = true;
168                         $visitor = $contact_id;
169                 }
170         }
171
172         $groups = [];
173
174         // perhaps they're visiting - but not a community page, so they wouldn't have write access
175         if (!empty(remote_user($owner_uid)) && !$visitor) {
176                 $contact_id = remote_user($owner_uid);
177
178                 if ($contact_id > 0) {
179                         $groups = Group::getIdsByContactId($contact_id);
180                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
181                                 intval($contact_id),
182                                 intval($owner_uid)
183                         );
184
185                         if (DBA::isResult($r)) {
186                                 $remote_contact = true;
187                         }
188                 }
189         }
190
191         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
192                 notice(L10n::t('Access to this item is restricted.') . EOL);
193                 return;
194         }
195
196         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
197
198         $o = "";
199
200         // tabs
201         $_is_owner = (local_user() && (local_user() == $owner_uid));
202         $o .= Profile::getTabs($a, 'videos', $_is_owner, $a->data['user']['nickname']);
203
204         //
205         // dispatch request
206         //
207         if ($datatype === 'upload') {
208                 return; // no uploading for now
209
210                 // DELETED -- look at mod/photos.php if you want to implement
211         }
212
213         if ($datatype === 'album') {
214                 return; // no albums for now
215
216                 // DELETED -- look at mod/photos.php if you want to implement
217         }
218
219
220         if ($datatype === 'video') {
221                 return; // no single video view for now
222
223                 // DELETED -- look at mod/photos.php if you want to implement
224         }
225
226         // Default - show recent videos (no upload link for now)
227         //$o = '';
228
229         $total = 0;
230         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
231                 $sql_extra GROUP BY hash",
232                 intval($a->data['user']['uid'])
233         );
234         if (DBA::isResult($r)) {
235                 $total = count($r);
236         }
237
238         $pager = new Pager($a->query_string, 20);
239
240         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
241                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
242                 FROM `attach`
243                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
244                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
245                 intval($a->data['user']['uid']),
246                 $pager->getStart(),
247                 $pager->getItemsPerPage()
248         );
249
250         $videos = [];
251
252         if (DBA::isResult($r)) {
253                 foreach ($r as $rr) {
254                         $alt_e = $rr['filename'];
255                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
256                         $rr['album'] = '';
257                         $name_e = $rr['album'];
258
259                         $videos[] = [
260                                 'id'       => $rr['id'],
261                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
262                                 'title'    => L10n::t('View Video'),
263                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
264                                 'alt'      => $alt_e,
265                                 'mime'     => $rr['filetype'],
266                                 'album' => [
267                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
268                                         'name'  => $name_e,
269                                         'alt'   => L10n::t('View Album'),
270                                 ],
271                         ];
272                 }
273         }
274
275         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
276         $o .= Renderer::replaceMacros($tpl, [
277                 '$title'      => L10n::t('Recent Videos'),
278                 '$can_post'   => $can_post,
279                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
280                 '$videos'     => $videos,
281                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
282         ]);
283
284         $o .= $pager->renderFull($total);
285
286         return $o;
287 }