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