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