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