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