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