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