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