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