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