]> git.mxchange.org Git - friendica.git/blob - mod/videos.php
Merge pull request #4312 from zeroadam/feature/L10n
[friendica.git] / mod / videos.php
1 <?php
2 /**
3  * @file mod/videos.php
4  */
5 use Friendica\App;
6 use Friendica\Content\Nav;
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Group;
14 use Friendica\Model\Profile;
15 use Friendica\Protocol\DFRN;
16
17 require_once 'include/items.php';
18 require_once 'include/acl_selectors.php';
19 require_once 'include/bbcode.php';
20 require_once 'include/security.php';
21
22 function videos_init(App $a) {
23
24         if($a->argc > 1)
25                 DFRN::autoRedir($a, $a->argv[1]);
26
27         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
28                 return;
29         }
30
31         Nav::setSelected('home');
32
33         $o = '';
34
35         if($a->argc > 1) {
36                 $nick = $a->argv[1];
37                 $user = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
38                         dbesc($nick)
39                 );
40
41                 if(! count($user))
42                         return;
43
44                 $a->data['user'] = $user[0];
45                 $a->profile_uid = $user[0]['uid'];
46
47                 $profile = Profile::getByNickname($nick, $a->profile_uid);
48
49                 $account_type = Contact::getAccountType($profile);
50
51                 $tpl = get_markup_template("vcard-widget.tpl");
52
53                 $vcard_widget = replace_macros($tpl, [
54                         '$name' => $profile['name'],
55                         '$photo' => $profile['photo'],
56                         '$addr' => defaults($profile, 'addr', ''),
57                         '$account_type' => $account_type,
58                         '$pdesc' => defaults($profile, 'pdesc', ''),
59                 ]);
60
61
62                 /*$sql_extra = permissions_sql($a->data['user']['uid']);
63
64                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra order by created desc",
65                         intval($a->data['user']['uid'])
66                 );
67
68                 if(count($albums)) {
69                         $a->data['albums'] = $albums;
70
71                         $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);
72
73                         if($albums_visible) {
74                                 $o .= '<div id="sidebar-photos-albums" class="widget">';
75                                 $o .= '<h3>' . '<a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '">' . L10n::t('Photo Albums') . '</a></h3>';
76
77                                 $o .= '<ul>';
78                                 foreach($albums as $album) {
79
80                                         // don't show contact photos. We once translated this name, but then you could still access it under
81                                         // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
82
83                                         if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === L10n::t('Contact Photos')))
84                                                 continue;
85                                         $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
86                                 }
87                                 $o .= '</ul>';
88                         }
89                         if(local_user() && $a->data['user']['uid'] == local_user()) {
90                                 $o .= '<div id="photo-albums-upload-link"><a href="' . System::baseUrl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .L10n::t('Upload New Photos') . '</a></div>';
91                         }
92
93                         $o .= '</div>';
94                 }*/
95
96                 if(! x($a->page,'aside'))
97                         $a->page['aside'] = '';
98                 $a->page['aside'] .= $vcard_widget;
99
100
101                 $tpl = get_markup_template("videos_head.tpl");
102                 $a->page['htmlhead'] .= replace_macros($tpl,[
103                         '$baseurl' => System::baseUrl(),
104                 ]);
105
106                 $tpl = get_markup_template("videos_end.tpl");
107                 $a->page['end'] .= replace_macros($tpl,[
108                         '$baseurl' => System::baseUrl(),
109                 ]);
110
111         }
112
113         return;
114 }
115
116
117
118 function videos_post(App $a) {
119
120         $owner_uid = $a->data['user']['uid'];
121
122         if (local_user() != $owner_uid) {
123                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
124         }
125
126         if (($a->argc == 2) && x($_POST,'delete') && x($_POST, 'id')) {
127
128                 // Check if we should do HTML-based delete confirmation
129                 if (!x($_REQUEST,'confirm')) {
130                         if (x($_REQUEST,'canceled')) {
131                                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
132                         }
133
134                         $drop_url = $a->query_string;
135                         $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), [
136                                 '$method' => 'post',
137                                 '$message' => L10n::t('Do you really want to delete this video?'),
138                                 '$extra_inputs' => [
139                                         ['name'=>'id', 'value'=> $_POST['id']],
140                                         ['name'=>'delete', 'value'=>'x']
141                                 ],
142                                 '$confirm' => L10n::t('Delete Video'),
143                                 '$confirm_url' => $drop_url,
144                                 '$confirm_name' => 'confirm', // Needed so that confirmation will bring us back into this if statement
145                                 '$cancel' => L10n::t('Cancel'),
146
147                         ]);
148                         $a->error = 1; // Set $a->error so the other module functions don't execute
149                         return;
150                 }
151
152                 $video_id = $_POST['id'];
153
154                 $r = q("SELECT `id`  FROM `attach` WHERE `uid` = %d AND `id` = '%s' LIMIT 1",
155                         intval(local_user()),
156                         dbesc($video_id)
157                 );
158
159                 if (DBM::is_result($r)) {
160                         q("DELETE FROM `attach` WHERE `uid` = %d AND `id` = '%s'",
161                                 intval(local_user()),
162                                 dbesc($video_id)
163                         );
164                         $i = q("SELECT * FROM `item` WHERE `attach` like '%%attach/%s%%' AND `uid` = %d LIMIT 1",
165                                 dbesc($video_id),
166                                 intval(local_user())
167                         );
168                         //echo "<pre>"; var_dump($i); killme();
169                         if (DBM::is_result($i)) {
170                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
171                                         dbesc(datetime_convert()),
172                                         dbesc(datetime_convert()),
173                                         dbesc($i[0]['uri']),
174                                         intval(local_user())
175                                 );
176                                 create_tags_from_itemuri($i[0]['uri'], local_user());
177                                 delete_thread_uri($i[0]['uri'], local_user());
178
179                                 $url = System::baseUrl();
180                                 $drop_id = intval($i[0]['id']);
181
182                                 if ($i[0]['visible']) {
183                                         Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
184                                 }
185                         }
186                 }
187
188                 goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
189                 return; // NOTREACHED
190         }
191
192         goaway(System::baseUrl() . '/videos/' . $a->data['user']['nickname']);
193
194 }
195
196
197
198 function videos_content(App $a) {
199
200         // URLs (most aren't currently implemented):
201         // videos/name
202         // videos/name/upload
203         // videos/name/upload/xxxxx (xxxxx is album name)
204         // videos/name/album/xxxxx
205         // videos/name/album/xxxxx/edit
206         // videos/name/video/xxxxx
207         // videos/name/video/xxxxx/edit
208
209
210         if((Config::get('system','block_public')) && (! local_user()) && (! remote_user())) {
211                 notice(L10n::t('Public access denied.') . EOL);
212                 return;
213         }
214
215
216         require_once('include/bbcode.php');
217         require_once('include/security.php');
218         require_once('include/conversation.php');
219
220         if(! x($a->data,'user')) {
221                 notice(L10n::t('No videos selected') . EOL );
222                 return;
223         }
224
225         //$phototypes = Photo::supportedTypes();
226
227         $_SESSION['video_return'] = $a->cmd;
228
229         //
230         // Parse arguments
231         //
232
233         if($a->argc > 3) {
234                 $datatype = $a->argv[2];
235                 $datum = $a->argv[3];
236         }
237         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
238                 $datatype = 'upload';
239         else
240                 $datatype = 'summary';
241
242         if($a->argc > 4)
243                 $cmd = $a->argv[4];
244         else
245                 $cmd = 'view';
246
247         //
248         // Setup permissions structures
249         //
250
251         $can_post       = false;
252         $visitor        = 0;
253         $contact        = null;
254         $remote_contact = false;
255         $contact_id     = 0;
256
257         $owner_uid = $a->data['user']['uid'];
258
259         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
260
261         if((local_user()) && (local_user() == $owner_uid))
262                 $can_post = true;
263         else {
264                 if($community_page && remote_user()) {
265                         if(is_array($_SESSION['remote'])) {
266                                 foreach($_SESSION['remote'] as $v) {
267                                         if($v['uid'] == $owner_uid) {
268                                                 $contact_id = $v['cid'];
269                                                 break;
270                                         }
271                                 }
272                         }
273                         if($contact_id) {
274
275                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
276                                         intval($contact_id),
277                                         intval($owner_uid)
278                                 );
279                                 if (DBM::is_result($r)) {
280                                         $can_post = true;
281                                         $contact = $r[0];
282                                         $remote_contact = true;
283                                         $visitor = $contact_id;
284                                 }
285                         }
286                 }
287         }
288
289         $groups = [];
290
291         // perhaps they're visiting - but not a community page, so they wouldn't have write access
292         if(remote_user() && (! $visitor)) {
293                 $contact_id = 0;
294                 if(is_array($_SESSION['remote'])) {
295                         foreach($_SESSION['remote'] as $v) {
296                                 if($v['uid'] == $owner_uid) {
297                                         $contact_id = $v['cid'];
298                                         break;
299                                 }
300                         }
301                 }
302                 if($contact_id) {
303                         $groups = Group::getIdsByContactId($contact_id);
304                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
305                                 intval($contact_id),
306                                 intval($owner_uid)
307                         );
308                         if (DBM::is_result($r)) {
309                                 $contact = $r[0];
310                                 $remote_contact = true;
311                         }
312                 }
313         }
314
315         if(! $remote_contact) {
316                 if(local_user()) {
317                         $contact_id = $_SESSION['cid'];
318                         $contact = $a->contact;
319                 }
320         }
321
322         if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
323                 notice(L10n::t('Access to this item is restricted.') . EOL);
324                 return;
325         }
326
327         $sql_extra = permissions_sql($owner_uid, $remote_contact, $groups);
328
329         $o = "";
330
331         // tabs
332         $_is_owner = (local_user() && (local_user() == $owner_uid));
333         $o .= Profile::getTabs($a, $_is_owner, $a->data['user']['nickname']);
334
335         //
336         // dispatch request
337         //
338
339
340         if($datatype === 'upload') {
341                 return; // no uploading for now
342
343                 // DELETED -- look at mod/photos.php if you want to implement
344         }
345
346         if($datatype === 'album') {
347
348                 return; // no albums for now
349
350                 // DELETED -- look at mod/photos.php if you want to implement
351         }
352
353
354         if($datatype === 'video') {
355
356                 return; // no single video view for now
357
358                 // DELETED -- look at mod/photos.php if you want to implement
359         }
360
361         // Default - show recent videos (no upload link for now)
362         //$o = '';
363
364         $r = q("SELECT hash FROM `attach` WHERE `uid` = %d AND filetype LIKE '%%video%%'
365                 $sql_extra GROUP BY hash",
366                 intval($a->data['user']['uid'])
367         );
368         if (DBM::is_result($r)) {
369                 $a->set_pager_total(count($r));
370                 $a->set_pager_itemspage(20);
371         }
372
373         $r = q("SELECT hash, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`created`) AS `created`,
374                 ANY_VALUE(`filename`) AS `filename`, ANY_VALUE(`filetype`) as `filetype`
375                 FROM `attach`
376                 WHERE `uid` = %d AND filetype LIKE '%%video%%'
377                 $sql_extra GROUP BY hash ORDER BY `created` DESC LIMIT %d , %d",
378                 intval($a->data['user']['uid']),
379                 intval($a->pager['start']),
380                 intval($a->pager['itemspage'])
381         );
382
383
384
385         $videos = [];
386         if (DBM::is_result($r)) {
387                 foreach ($r as $rr) {
388                         $alt_e = $rr['filename'];
389                         $name_e = $rr['album'];
390
391                         $videos[] = [
392                                 'id'       => $rr['id'],
393                                 'link'     => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/video/' . $rr['resource-id'],
394                                 'title'    => L10n::t('View Video'),
395                                 'src'      => System::baseUrl() . '/attach/' . $rr['id'] . '?attachment=0',
396                                 'alt'      => $alt_e,
397                                 'mime'     => $rr['filetype'],
398                                 'album' => [
399                                         'link'  => System::baseUrl() . '/videos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
400                                         'name'  => $name_e,
401                                         'alt'   => L10n::t('View Album'),
402                                 ],
403
404                         ];
405                 }
406         }
407
408         $tpl = get_markup_template('videos_recent.tpl');
409         $o .= replace_macros($tpl, [
410                 '$title'      => L10n::t('Recent Videos'),
411                 '$can_post'   => $can_post,
412                 '$upload'     => [L10n::t('Upload New Videos'), System::baseUrl().'/videos/'.$a->data['user']['nickname'].'/upload'],
413                 '$videos'     => $videos,
414                 '$delete_url' => (($can_post)?System::baseUrl().'/videos/'.$a->data['user']['nickname']:False)
415         ]);
416
417
418         $o .= paginate($a);
419         return $o;
420 }