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