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