EN US translation update THX AndyH3
[friendica.git/.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 && remote_user()) {
158                 if (!empty($_SESSION['remote'])) {
159                         foreach ($_SESSION['remote'] as $v) {
160                                 if ($v['uid'] == $owner_uid) {
161                                         $contact_id = $v['cid'];
162                                         break;
163                                 }
164                         }
165                 }
166
167                 if ($contact_id > 0) {
168                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
169                                 intval($contact_id),
170                                 intval($owner_uid)
171                         );
172
173                         if (DBA::isResult($r)) {
174                                 $can_post = true;
175                                 $remote_contact = true;
176                                 $visitor = $contact_id;
177                         }
178                 }
179         }
180
181         $groups = [];
182
183         // perhaps they're visiting - but not a community page, so they wouldn't have write access
184         if (remote_user() && (!$visitor)) {
185                 $contact_id = 0;
186
187                 if (!empty($_SESSION['remote'])) {
188                         foreach($_SESSION['remote'] as $v) {
189                                 if($v['uid'] == $owner_uid) {
190                                         $contact_id = $v['cid'];
191                                         break;
192                                 }
193                         }
194                 }
195
196                 if ($contact_id > 0) {
197                         $groups = Group::getIdsByContactId($contact_id);
198                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
199                                 intval($contact_id),
200                                 intval($owner_uid)
201                         );
202
203                         if (DBA::isResult($r)) {
204                                 $remote_contact = true;
205                         }
206                 }
207         }
208
209         if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
210                 notice(L10n::t('Access to this item is restricted.') . EOL);
211                 return;
212         }
213
214         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
215
216         $o = "";
217
218         // tabs
219         $_is_owner = (local_user() && (local_user() == $owner_uid));
220         $o .= Profile::getTabs($a, 'videos', $_is_owner, $a->data['user']['nickname']);
221
222         //
223         // dispatch request
224         //
225         if ($datatype === 'upload') {
226                 return; // no uploading for now
227
228                 // DELETED -- look at mod/photos.php if you want to implement
229         }
230
231         if ($datatype === 'album') {
232                 return; // no albums for now
233
234                 // DELETED -- look at mod/photos.php if you want to implement
235         }
236
237
238         if ($datatype === 'video') {
239                 return; // no single video view for now
240
241                 // DELETED -- look at mod/photos.php if you want to implement
242         }
243
244         // Default - show recent videos (no upload link for now)
245         //$o = '';
246
247         $total = 0;
248         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
249                 $sql_extra GROUP BY hash",
250                 intval($a->data['user']['uid'])
251         );
252         if (DBA::isResult($r)) {
253                 $total = count($r);
254         }
255
256         $pager = new Pager($a->query_string, 20);
257
258         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
259                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
260                 FROM `attach`
261                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
262                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
263                 intval($a->data['user']['uid']),
264                 $pager->getStart(),
265                 $pager->getItemsPerPage()
266         );
267
268         $videos = [];
269
270         if (DBA::isResult($r)) {
271                 foreach ($r as $rr) {
272                         $alt_e = $rr['filename'];
273                         /// @todo The album isn't part of the above query. This seems to be some unfinished code that needs to be reworked completely.
274                         $rr['album'] = '';
275                         $name_e = $rr['album'];
276
277                         $videos[] = [
278                                 'id'       => $rr['id'],
279                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['hash'],
280                                 'title'    => L10n::t('View Video'),
281                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
282                                 'alt'      => $alt_e,
283                                 'mime'     => $rr['filetype'],
284                                 'album' => [
285                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
286                                         'name'  => $name_e,
287                                         'alt'   => L10n::t('View Album'),
288                                 ],
289                         ];
290                 }
291         }
292
293         $tpl = Renderer::getMarkupTemplate('videos_recent.tpl');
294         $o .= Renderer::replaceMacros($tpl, [
295                 '$title'      => L10n::t('Recent Videos'),
296                 '$can_post'   => $can_post,
297                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/upload'],
298                 '$videos'     => $videos,
299                 '$delete_url' => (($can_post) ? System::baseUrl() . '/videos/' . $a->data['user']['nickname'] : false)
300         ]);
301
302         $o .= $pager->renderFull($total);
303
304         return $o;
305 }