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