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