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