]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
e680bc5a2fc3c2c5ead0e188c62d0dbd677dde7d
[friendica.git] / mod / photos.php
1 <?php
2
3 require_once('Photo.php');
4 require_once('view/acl_selectors.php');
5
6 function photos_init(&$a) {
7
8         if($a->argc > 1) {
9                 $nick = $a->argv[1];
10                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
11                         dbesc($nick)
12                 );
13
14                 if(! count($r))
15                         return;
16
17                 $a->data['user'] = $r[0];
18
19                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
20                         intval($a->data['user']['uid'])
21                 );
22
23                 if(count($albums)) {
24                         $a->data['albums'] = $albums;
25
26                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
27                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
28                 
29                         $o .= '<ul>';
30                         foreach($albums as $album) {
31                                 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
32                                         continue;
33                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
34                         }
35                         $o .= '</ul>';
36                 }
37                 $a->page['aside'] .= $o;
38         }
39
40
41 }
42
43
44
45
46 function photos_post(&$a) {
47
48
49         if(! local_user()) {
50                 notice( t('Permission denied.') . EOL );
51                 killme();
52         }
53
54         $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
55                 intval($_SESSION['uid'])
56         );
57
58         $contact_record = $r[0];        
59
60         if(! x($_FILES,'userfile'))
61                 killme();
62
63         if($_POST['partitionCount'])
64                 $java_upload = true;
65         else
66                 $java_upload = false;
67
68         $album =  notags(trim($_POST['album']));
69         $newalbum = notags(trim($_POST['newalbum']));
70
71         if(! strlen($album)) {
72                 if(strlen($newalbum))
73                         $album = $newalbum;
74                 else
75                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
76         }
77
78         $str_group_allow = '';
79         $group_allow = $_POST['group_allow'];
80         if(is_array($group_allow)) {
81                 array_walk($group_allow,'sanitise_acl');
82                 $str_group_allow = implode('',$group_allow);
83         }
84
85         $str_contact_allow = '';
86         $contact_allow = $_POST['contact_allow'];
87         if(is_array($contact_allow)) {
88                 array_walk($contact_allow,'sanitise_acl');
89                 $str_contact_allow = implode('',$contact_allow);
90         }
91
92         $str_group_deny = '';
93         $group_deny = $_POST['group_deny'];
94         if(is_array($group_deny)) {
95                 array_walk($group_deny,'sanitise_acl');
96                 $str_group_deny = implode('',$group_deny);
97         }
98
99         $str_contact_deny = '';
100         $contact_deny = $_POST['contact_deny'];
101         if(is_array($contact_deny)) {
102                 array_walk($contact_deny,'sanitise_acl');
103                 $str_contact_deny = implode('',$contact_deny);
104         }
105
106
107         $src      = $_FILES['userfile']['tmp_name'];
108         $filename = basename($_FILES['userfile']['name']);
109         $filesize = intval($_FILES['userfile']['size']);
110
111         $imagedata = @file_get_contents($src);
112         $ph = new Photo($imagedata);
113
114         if(! ($image = $ph->getImage())) {
115                 notice( t('Unable to process image.') . EOL );
116                 @unlink($src);
117                 killme();
118         }
119
120         @unlink($src);
121
122         $width = $ph->getWidth();
123         $height = $ph->getHeight();
124
125         $smallest = 0;
126
127         $photo_hash = hash('md5',uniqid(mt_rand(),true));
128         
129         $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
130
131         if(! $r) {
132                 notice( t('Image upload failed.') . EOL );
133                 killme();
134         }
135
136         if($width > 640 || $height > 640) {
137                 $ph->scaleImage(640);
138                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
139                 $smallest = 1;
140         }
141
142         if($width > 320 || $height > 320) {
143                 $ph->scaleImage(320);
144                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
145                 $smallest = 2;
146         }
147         
148         $basename = basename($filename);
149
150         // Create item container
151
152         $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']' 
153                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
154                 . '[/url]';
155
156                         do {
157                         $dups = false;
158                         $item_hash = random_string();
159
160                         $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $item_hash;
161
162                         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
163                         dbesc($uri));
164                         if(count($r))
165                                 $dups = true;
166                 } while($dups == true);
167
168
169                 $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
170                         `edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
171                         VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
172                         intval($_SESSION['uid']),
173                         dbesc('photo'),
174                         dbesc($photo_hash),                     
175                         intval($contact_record['id']),
176                         dbesc($contact_record['name']),
177                         dbesc($contact_record['url']),
178                         dbesc($contact_record['thumb']),
179                         datetime_convert(),
180                         datetime_convert(),
181                         dbesc($uri),
182                         dbesc($uri),
183                         dbesc($title),
184                         dbesc($body),
185                         dbesc($str_contact_allow),
186                         dbesc($str_group_allow),
187                         dbesc($str_contact_deny),
188                         dbesc($str_group_deny)
189
190                 );
191                 if($r) {
192
193                         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
194                                 dbesc($uri)
195                         );
196                         if(count($r))
197                                 q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
198                                 intval($r[0]['id']),
199                                 intval($r[0]['id'])
200                         );
201         
202                 }
203
204         // if album has no featured photo, promote one.
205
206
207         if(! $java_upload) {
208                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
209                 return; // NOTREACHED
210         }
211
212         killme();
213         return; // NOTREACHED
214
215 }
216
217
218
219 function photos_content(&$a) {
220
221         // URLs:
222         // photos/name
223         // photos/name/upload
224         // photos/name/album/xxxxx
225         // photos/name/album/xxxxx/edit
226         // photos/name/album/xxxxx/drop
227         // photos/name/image/xxxxx
228         // photos/name/image/xxxxx/edit
229         // photos/name/image/xxxxx/drop
230
231         if(! x($a->data,'user')) {
232                 notice( t('No photos selected') . EOL );
233                 return;
234         }
235
236         $_SESSION['photo_return'] = $a->cmd;
237
238         //
239         // Parse arguments 
240         //
241
242         if($a->argc > 3) {
243                 $datatype = $a->argv[2];
244                 $datum = $a->argv[3];
245         }
246         elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
247                 $datatype = 'upload';
248         else
249                 $datatype = 'summary';
250
251         if($a->argc > 4)
252                 $cmd = $a->argv[4];
253         else
254                 $cmd = 'view';
255
256         //
257         // Setup permissions structures
258         //
259
260         $owner_uid = $a->data['user']['uid'];
261
262         if(remote_user()) {
263                 $contact_id = $_SESSION['visitor_id'];
264                 $groups = init_groups_visitor($contact_id);
265         }
266
267         // default permissions - anonymous user
268
269         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
270
271         // Profile owner - everything is visible
272
273         if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
274                 $sql_extra = '';        
275         }
276         elseif(remote_user()) {
277                 // authenticated visitor - here lie dragons
278                 $gs = '<<>>'; // should be impossible to match
279                 if(count($groups)) {
280                         foreach($groups as $g)
281                                 $gs .= '|<' . intval($g) . '>';
282                 } 
283                 $sql_extra = sprintf(
284                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
285                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
286                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
287                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
288
289                         intval($_SESSION['visitor_id']),
290                         intval($_SESSION['visitor_id']),
291                         dbesc($gs),
292                         dbesc($gs)
293                 );
294         }
295
296         //
297         // dispatch request
298         //
299
300
301         if($datatype == 'upload') {
302                 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
303                         notice( t('Permission denied.'));
304                         return;
305                 }
306                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
307
308                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
309                 if(count($a->data['albums'])) {
310                         foreach($a->data['albums'] as $album) {
311                                 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
312                                         continue;
313                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
314                         }
315                 }
316                 $albumselect .= '</select>';
317                 $tpl = file_get_contents('view/photos_upload.tpl');
318                 $o .= replace_macros($tpl,array(
319                         '$pagename' => t('Upload Photos'),
320                         '$sessid' => session_id(),
321                         '$newalbum' => t('New album name: '),
322                         '$existalbumtext' => t('or existing album name: '),
323                         '$filestext' => t('Select files to upload: '),
324                         '$albumselect' => $albumselect,
325                         '$permissions' => t('Permissions'),
326                         '$aclselect' => populate_acl($a->user),
327                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
328                         '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
329                         '$uploadurl' => $a->get_baseurl() . '/photos',
330                         '$submit' => t('Submit')
331                 ));
332
333                 return $o; 
334
335         }
336
337         if($datatype == 'album') {
338
339                 $album = hex2bin($datum);
340
341                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
342                         $sql_extra GROUP BY `resource-id`",
343                         intval($a->data['user']['uid']),
344                         dbesc($album)
345                 );
346                 if(count($r))
347                         $a->set_pager_total(count($r));
348
349
350                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
351                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
352                         intval($a->data['user']['uid']),
353                         dbesc($album),
354                         intval($a->pager['start']),
355                         intval($a->pager['itemspage'])
356                 );
357
358                 $o .= '<h3>' . $album . '</h3>';
359
360                 $tpl = file_get_contents('view/photo_album.tpl');
361                 if(count($r))
362                         foreach($r as $rr) {
363                                 $o .= replace_macros($tpl,array(
364                                         '$id' => $rr['id'],
365                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
366                                         '$phototitle' => t('View Photo'),
367                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
368                                         '$imgalt' => $rr['filename']
369                                 ));
370
371                 }
372                 $o .= '<div id="photo-album-end"></div>';
373                 return $o;
374
375         }       
376
377
378         if($datatype == 'image') {
379
380                 require_once('security.php');
381                 require_once('bbcode.php');
382
383                 // fetch image, item containing image, then comments
384
385                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
386                         $sql_extra ORDER BY `scale` ASC ",
387                         intval($a->data['user']['uid']),
388                         dbesc($datum)
389                 );
390
391                 if(! count($ph)) {
392                         notice( t('Photo not available') . EOL );
393                         return;
394                 }
395
396                 if(count($ph) == 1)
397                         $hires = $lores = $ph[0];
398                 if(count($ph) > 1) {
399                         if($ph[1]['scale'] == 2) {
400                                 // original is 640 or less, we can display it directly
401                                 $hires = $lores = $ph[0];
402                         }
403                         else {
404                         $hires = $ph[0];
405                         $lores = $ph[1];
406                         }
407                 }
408
409                 
410                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
411  
412                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
413                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
414                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
415                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
416
417                 // Do we have an item for this photo?
418
419                 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
420                         dbesc($datum)
421                 );
422                 if(count($i1)) {
423 //dbg(2);
424                         $r = q("SELECT COUNT(*) AS `total`
425                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
426                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
427                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
428                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
429                                 $sql_extra ",
430                                 dbesc($i1[0]['uri']),
431                                 dbesc($i1[0]['uri'])
432
433                         );
434
435                         if(count($r))
436                                 $a->set_pager_total($r[0]['total']);
437
438
439                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
440                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
441                                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
442                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
443                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
444                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
445                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
446                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
447                                 $sql_extra
448                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
449                                 dbesc($i1[0]['uri']),
450                                 dbesc($i1[0]['uri']),
451                                 intval($a->pager['start']),
452                                 intval($a->pager['itemspage'])
453
454                         );
455
456
457                         $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
458
459                         if(strlen($i1[0]['tag'])) {
460                                 // parse tags and add links     
461                                 $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
462                                 $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
463                         }
464
465                         if($cmd == 'edit') {
466                                 $edit_tpl = file_get_contents('view/photo_edit.tpl');
467                                 $o .= replace_macros($edit_tpl, array(
468                                         '$id' => $ph[0]['id']
469                                 ));
470                         }
471
472                         // pull out how many people like the photo
473
474                         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
475                         $tpl = file_get_contents('view/photo_item.tpl');
476                         $return_url = $a->cmd;
477
478                         if(can_write_wall($a,$a->data['user']['uid'])) {
479                                 if($i1[0]['last-child']) {
480                                         $o .= replace_macros($cmnt_tpl,array(
481                                                 '$return_path' => $return_url,
482                                                 '$type' => 'wall-comment',
483                                                 '$id' => $i1[0]['id'],
484                                                 '$parent' => $i1[0]['id'],
485                                                 '$profile_uid' =>  $a->data['user']['uid'],
486                                                 '$ww' => ''
487                                         ));
488                                 }
489                         }
490
491
492                         // display comments
493                         if(count($r)) {
494                                 foreach($r as $item) {
495                                         $comment = '';
496                                         $template = $tpl;
497                         
498                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
499                         
500                                         if(can_write_wall($a,$a->data['user']['uid'])) {
501                                                 if($item['last-child']) {
502                                                         $comment = replace_macros($cmnt_tpl,array(
503                                                                 '$return_path' => $return_url,
504                                                                 '$type' => 'wall-comment',
505                                                                 '$id' => $item['item_id'],
506                                                                 '$parent' => $item['parent'],
507                                                                 '$profile_uid' =>  $a->data['user']['uid'],
508                                                                 '$ww' => ''
509                                                         ));
510                                                 }
511                                         }
512
513                                         $profile_url = $item['url'];
514
515
516                                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) && (strlen($item['dfrn-id'])) && (! $item['self'] ))
517                                                 $profile_url = $redirect_url;
518
519  
520                                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
521                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
522                                         $profile_link = $profile_url;
523
524                                         $drop = '';
525
526                                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
527                                                 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
528
529
530                                         $o .= replace_macros($template,array(
531                                                 '$id' => $item['item_id'],
532                                                 '$profile_url' => $profile_link,
533                                                 '$name' => $profile_name,
534                                                 '$thumb' => $profile_avatar,
535                                                 '$title' => $item['title'],
536                                                 '$body' => bbcode($item['body']),
537                                                 '$ago' => relative_date($item['created']),
538                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
539                                                 '$drop' => $drop,
540                                                 '$comment' => $comment
541                                         ));
542                                 }
543                         }
544
545                         $o .= paginate($a);
546                 }
547                 return $o;
548         }
549
550         // Default - show recent photos with upload link (if applicable)
551
552         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
553                 $sql_extra GROUP BY `resource-id`",
554                 intval($a->data['user']['uid']),
555                 dbesc( t('Contact Photos'))
556         );
557         if(count($r))
558                 $a->set_pager_total(count($r));
559
560
561         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
562                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
563                 intval($a->data['user']['uid']),
564                 dbesc( t('Contact Photos')),
565                 intval($a->pager['start']),
566                 intval($a->pager['itemspage'])
567         );
568
569         $o .= '<h3>' . t('Recent Photos') . '</h3>';
570
571         if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
572                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
573                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
574         }
575
576         $tpl = file_get_contents('view/photo_top.tpl');
577         if(count($r)) {
578                 foreach($r as $rr) {
579                         $o .= replace_macros($tpl,array(
580                                 '$id' => $rr['id'],
581                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
582                                         . '/image/' . $rr['resource-id'],
583                                 '$phototitle' => t('View Photo'),
584                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
585                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
586                                 '$albumlink' => $a->get_baseurl . '/photos/' 
587                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
588                                 '$albumname' => $rr['album'],
589                                 '$albumalt' => t('View Album'),
590                                 '$imgalt' => $rr['filename']
591                         ));
592
593                 }
594                 $o .= '<div id="photo-top-end"></div>';
595         }
596         return $o;
597 }