]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
0558f746a98e40d86136a8680277cfba016020bc
[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
55
56         $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
57                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
58                 intval($_SESSION['uid'])
59         );
60
61         $contact_record = $r[0];        
62
63
64         if(($a->argc > 2) && ($a->argv[1] == 'album')) {
65                 $album = hex2bin($a->argv[2]);
66
67                 if($album == t('Profile Photos') || $album == t('Contact Photos')) {
68                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
69                         return; // NOTREACHED
70                 }
71
72                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
73                         dbesc($album),
74                         intval($_SESSION['uid'])
75                 );
76                 if(! count($r)) {
77                         notice( t('Album not found.') . EOL);
78                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
79                         return; // NOTREACHED
80                 }
81
82                 $newalbum = notags(trim($_POST['albumname']));
83                 if($newalbum != $album) {
84                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
85                                 dbesc($newalbum),
86                                 dbesc($album),
87                                 intval($_SESSION['uid'])
88                         );
89                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
90                         goaway($a->get_baseurl() . '/' . $newurl);
91                         return; // NOTREACHED
92                 }
93
94                 if($_POST['dropalbum'] == t('Delete Album')) {
95
96                         $res = array();
97                         $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
98                                 intval($_SESSION['uid']),
99                                 dbesc($album)
100                         );
101                         if(count($r)) {
102                                 foreach($r as $rr) {
103                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
104                                 }
105                         }
106                         else {
107                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
108                                 return; // NOTREACHED
109                         }
110                         $str_res = implode(',', $res);
111
112                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
113                                 intval($_SESSION['uid'])
114                         );
115                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
116                                 intval($_SESSION['uid'])
117                         );
118                         if(count($r)) {
119                                 foreach($r as $rr) {
120                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
121                                                 dbesc(datetime_convert()),
122                                                 dbesc($rr['parent-uri']),
123                                                 intval($_SESSION['uid'])
124                                         );
125
126                                         $drop_id = intval($rr['id']);
127                                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
128
129                                         // send the notification upstream/downstream as the case may be
130
131                                         if($rr['visible'])
132                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
133                                                         array(),$foo));
134
135                                 }
136                         }
137                 }
138                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
139                 return; // NOTREACHED
140         }
141
142         if(($a->argc > 1) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
143                 $r = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
144                         intval($_SESSION['uid']),
145                         dbesc($a->argv[1])
146                 );
147                 if(count($r)) {
148                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
149                                 intval($_SESSION['uid']),
150                                 dbesc($r[0]['resource-id'])
151                         );
152                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
153                                 dbesc($r[0]['resource-id']),
154                                 intval($_SESSION['uid'])
155                         );
156                         if(count($i)) {
157                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
158                                         dbesc(datetime_convert()),
159                                         dbesc(datetime_convert()),
160                                         dbesc($i[0]['uri']),
161                                         intval($_SESSION['uid'])
162                                 );
163
164                                 $url = $a->get_baseurl();
165                                 $drop_id = intval($i[0]['id']);
166                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
167                                 
168                                 // send the notification upstream/downstream as the case may be
169
170                                 if($i[0]['visible'])
171                                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
172                                                 array(),$foo));
173                         }
174                 }
175
176                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
177                 return; // NOTREACHED
178         }
179
180
181
182         if(($a->argc > 1) && (x($_POST,'desc') !== false)) {
183                 $desc = notags(trim($_POST['desc']));
184                 $tags = notags(trim($_POST['tags']));
185                 $item_id = intval($_POST['item_id']);
186                 $resource_id = $a->argv[1];
187
188                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
189                         dbesc($resource_id),
190                         intval($_SESSION['uid'])
191                 );
192                 if(count($r)) {
193                         $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
194                                 dbesc($desc),
195                                 dbesc($resource_id),
196                                 intval($_SESSION['uid'])
197                         );
198                 }
199                 if(! $item_id) {
200
201                         $title = '';
202                         $basename = basename($filename);
203
204                         // Create item container
205
206                         $body = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
207                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
208                                 . '[/url]';
209
210                         do {
211                                 $dups = false;
212                                 $item_hash = random_string();
213
214                                 $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $_SESSION['uid'] .  ':' . $item_hash;
215
216                                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
217                                 dbesc($uri));
218                                 if(count($r))
219                                         $dups = true;
220                         } while($dups == true);
221
222
223                         $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,
224                                 `owner-name`,`owner-link`,`owner-avatar`, `created`,
225                                 `edited`, `changed`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
226                                 VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
227                                 intval($_SESSION['uid']),
228                                 dbesc('photo'),
229                                 dbesc($p[0]['resource-id']),                    
230                                 intval($contact_record['id']),
231                                 dbesc($contact_record['name']),
232                                 dbesc($contact_record['url']),
233                                 dbesc($contact_record['thumb']),
234                                 dbesc(datetime_convert()),
235                                 dbesc(datetime_convert()),
236                                 dbesc(datetime_convert()),
237                                 dbesc($uri),
238                                 dbesc($uri),
239                                 dbesc($title),
240                                 dbesc($body),
241                                 dbesc($p[0]['allow_cid']),
242                                 dbesc($p[0]['allow_gid']),
243                                 dbesc($p[0]['deny_cid']),
244                                 dbesc($p[0]['deny_gid'])
245
246                         );
247                         if($r) {
248         
249                                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
250                                         dbesc($uri)
251                                 );
252                                 if(count($r))
253                                         $item_id = $r[0]['id'];
254                                         q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
255                                         intval($r[0]['id']),
256                                         intval($r[0]['id'])
257                                 );
258                         }
259                 }
260
261                 $r = q("UPDATE `item` SET `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
262                         dbesc($tags),
263                         dbesc(datetime_convert()),
264                         dbesc(datetime_convert()),
265                         intval($item_id),
266                         intval($_SESSION['uid'])
267                 );
268
269                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
270                 return; // NOTREACHED
271         }
272
273
274
275
276         if(! x($_FILES,'userfile'))
277                 killme();
278
279         if($_POST['partitionCount'])
280                 $java_upload = true;
281         else
282                 $java_upload = false;
283
284         $album =  notags(trim($_POST['album']));
285         $newalbum = notags(trim($_POST['newalbum']));
286
287         if(! strlen($album)) {
288                 if(strlen($newalbum))
289                         $album = $newalbum;
290                 else
291                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
292         }
293
294         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
295                 dbesc($album),
296                 intval($_SESSION['uid'])
297         );
298         if((! count($r)) || ($album == t('Profile Photos')))
299                 $visible = 1;
300         else
301                 $visibile = 0;
302
303         $str_group_allow = '';
304         $group_allow = $_POST['group_allow'];
305         if(is_array($group_allow)) {
306                 array_walk($group_allow,'sanitise_acl');
307                 $str_group_allow = implode('',$group_allow);
308         }
309
310         $str_contact_allow = '';
311         $contact_allow = $_POST['contact_allow'];
312         if(is_array($contact_allow)) {
313                 array_walk($contact_allow,'sanitise_acl');
314                 $str_contact_allow = implode('',$contact_allow);
315         }
316
317         $str_group_deny = '';
318         $group_deny = $_POST['group_deny'];
319         if(is_array($group_deny)) {
320                 array_walk($group_deny,'sanitise_acl');
321                 $str_group_deny = implode('',$group_deny);
322         }
323
324         $str_contact_deny = '';
325         $contact_deny = $_POST['contact_deny'];
326         if(is_array($contact_deny)) {
327                 array_walk($contact_deny,'sanitise_acl');
328                 $str_contact_deny = implode('',$contact_deny);
329         }
330
331
332         $src      = $_FILES['userfile']['tmp_name'];
333         $filename = basename($_FILES['userfile']['name']);
334         $filesize = intval($_FILES['userfile']['size']);
335
336         $imagedata = @file_get_contents($src);
337         $ph = new Photo($imagedata);
338
339         if(! ($image = $ph->getImage())) {
340                 notice( t('Unable to process image.') . EOL );
341                 @unlink($src);
342                 killme();
343         }
344
345         @unlink($src);
346
347         $width = $ph->getWidth();
348         $height = $ph->getHeight();
349
350         $smallest = 0;
351
352         $photo_hash = hash('md5',uniqid(mt_rand(),true));
353         
354         $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
355
356         if(! $r) {
357                 notice( t('Image upload failed.') . EOL );
358                 killme();
359         }
360
361         if($width > 640 || $height > 640) {
362                 $ph->scaleImage(640);
363                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
364                 $smallest = 1;
365         }
366
367         if($width > 320 || $height > 320) {
368                 $ph->scaleImage(320);
369                 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
370                 $smallest = 2;
371         }
372         
373         $basename = basename($filename);
374
375         // Create item container
376
377         $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']' 
378                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
379                 . '[/url]';
380
381         do {
382                 $dups = false;
383                 $item_hash = random_string();
384
385                 $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $_SESSION['uid'] . ':' . $item_hash;
386
387                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
388                 dbesc($uri));
389                 if(count($r))
390                         $dups = true;
391         } while($dups == true);
392
393
394         $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
395                 `edited`, `changed`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `visible`)
396                 VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d )",
397                 intval($_SESSION['uid']),
398                 dbesc('photo'),
399                 dbesc($photo_hash),                     
400                 intval($contact_record['id']),
401                 dbesc($contact_record['name']),
402                 dbesc($contact_record['url']),
403                 dbesc($contact_record['thumb']),
404                 dbesc(datetime_convert()),
405                 dbesc(datetime_convert()),
406                 dbesc(datetime_convert()),
407                 dbesc($uri),
408                 dbesc($uri),
409                 dbesc($title),
410                 dbesc($body),
411                 dbesc($str_contact_allow),
412                 dbesc($str_group_allow),
413                 dbesc($str_contact_deny),
414                 dbesc($str_group_deny),
415                 intval($visible)
416         );
417         if($r) {
418
419                 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
420                         dbesc($uri)
421                 );
422                 if(count($r))
423                         q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
424                         intval($r[0]['id']),
425                         intval($r[0]['id'])
426                 );
427         
428         }
429
430         if(! $java_upload) {
431                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
432                 return; // NOTREACHED
433         }
434
435         killme();
436         return; // NOTREACHED
437
438 }
439
440
441
442 function photos_content(&$a) {
443
444         // URLs:
445         // photos/name
446         // photos/name/upload
447         // photos/name/album/xxxxx
448         // photos/name/album/xxxxx/edit
449         // photos/name/image/xxxxx
450         // photos/name/image/xxxxx/edit
451
452
453         if(! x($a->data,'user')) {
454                 notice( t('No photos selected') . EOL );
455                 return;
456         }
457
458         $_SESSION['photo_return'] = $a->cmd;
459
460         //
461         // Parse arguments 
462         //
463
464         if($a->argc > 3) {
465                 $datatype = $a->argv[2];
466                 $datum = $a->argv[3];
467         }
468         elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
469                 $datatype = 'upload';
470         else
471                 $datatype = 'summary';
472
473         if($a->argc > 4)
474                 $cmd = $a->argv[4];
475         else
476                 $cmd = 'view';
477
478         //
479         // Setup permissions structures
480         //
481
482         $owner_uid = $a->data['user']['uid'];
483
484         if(remote_user()) {
485                 $contact_id = $_SESSION['visitor_id'];
486                 $groups = init_groups_visitor($contact_id);
487         }
488
489         // default permissions - anonymous user
490
491         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
492
493         // Profile owner - everything is visible
494
495         if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
496                 $sql_extra = '';        
497         }
498         elseif(remote_user()) {
499                 // authenticated visitor - here lie dragons
500                 $gs = '<<>>'; // should be impossible to match
501                 if(count($groups)) {
502                         foreach($groups as $g)
503                                 $gs .= '|<' . intval($g) . '>';
504                 } 
505                 $sql_extra = sprintf(
506                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
507                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
508                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
509                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
510
511                         intval($_SESSION['visitor_id']),
512                         intval($_SESSION['visitor_id']),
513                         dbesc($gs),
514                         dbesc($gs)
515                 );
516         }
517
518         //
519         // dispatch request
520         //
521
522
523         if($datatype == 'upload') {
524                 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
525                         notice( t('Permission denied.'));
526                         return;
527                 }
528                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
529
530                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
531                 if(count($a->data['albums'])) {
532                         foreach($a->data['albums'] as $album) {
533                                 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
534                                         continue;
535                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
536                         }
537                 }
538                 $albumselect .= '</select>';
539                 $tpl = file_get_contents('view/photos_upload.tpl');
540                 $o .= replace_macros($tpl,array(
541                         '$pagename' => t('Upload Photos'),
542                         '$sessid' => session_id(),
543                         '$newalbum' => t('New album name: '),
544                         '$existalbumtext' => t('or existing album name: '),
545                         '$filestext' => t('Select files to upload: '),
546                         '$albumselect' => $albumselect,
547                         '$permissions' => t('Permissions'),
548                         '$aclselect' => populate_acl($a->user),
549                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
550                         '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
551                         '$uploadurl' => $a->get_baseurl() . '/photos',
552                         '$submit' => t('Submit')
553                 ));
554
555                 return $o; 
556
557         }
558
559         if($datatype == 'album') {
560
561                 $album = hex2bin($datum);
562
563                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
564                         $sql_extra GROUP BY `resource-id`",
565                         intval($a->data['user']['uid']),
566                         dbesc($album)
567                 );
568                 if(count($r))
569                         $a->set_pager_total(count($r));
570
571
572                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
573                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
574                         intval($a->data['user']['uid']),
575                         dbesc($album),
576                         intval($a->pager['start']),
577                         intval($a->pager['itemspage'])
578                 );
579
580                 $o .= '<h3>' . $album . '</h3>';
581                 
582                 if($cmd == 'edit') {            
583                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
584                                 if(local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
585                                         $edit_tpl = file_get_contents('view/album_edit.tpl');
586                                         $o .= replace_macros($edit_tpl,array(
587                                                 '$nametext' => t('New album name: '),
588                                                 '$album' => $album,
589                                                 '$hexalbum' => bin2hex($album),
590                                                 '$submit' => t('Submit'),
591                                                 '$dropsubmit' => t('Delete Album')
592                                         ));
593                                 }
594                         }
595                 }
596                 else {
597                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
598                                 if(local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
599                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
600                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
601                                                 . t('Edit Album') . '</a></div>';
602                                 }
603                         }
604                 }
605                 $tpl = file_get_contents('view/photo_album.tpl');
606                 if(count($r))
607                         foreach($r as $rr) {
608                                 $o .= replace_macros($tpl,array(
609                                         '$id' => $rr['id'],
610                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
611                                         '$phototitle' => t('View Photo'),
612                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
613                                         '$imgalt' => $rr['filename']
614                                 ));
615
616                 }
617                 $o .= '<div id="photo-album-end"></div>';
618                 return $o;
619
620         }       
621
622
623         if($datatype == 'image') {
624
625                 require_once('security.php');
626                 require_once('bbcode.php');
627
628                 // fetch image, item containing image, then comments
629
630                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
631                         $sql_extra ORDER BY `scale` ASC ",
632                         intval($a->data['user']['uid']),
633                         dbesc($datum)
634                 );
635
636                 if(! count($ph)) {
637                         notice( t('Photo not available') . EOL );
638                         return;
639                 }
640
641                 if(count($ph) == 1)
642                         $hires = $lores = $ph[0];
643                 if(count($ph) > 1) {
644                         if($ph[1]['scale'] == 2) {
645                                 // original is 640 or less, we can display it directly
646                                 $hires = $lores = $ph[0];
647                         }
648                         else {
649                         $hires = $ph[0];
650                         $lores = $ph[1];
651                         }
652                 }
653
654                 
655                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
656  
657                 if(local_user() && ($ph[0]['uid'] == $_SESSION['uid'])) {
658                         $o .= '<div id="photo-edit-link-wrap" ><a id="photo-edit-link" href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit' . '">' . t('Edit photo') . '</a></div>';
659                 }
660
661
662                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
663                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
664                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
665                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
666
667
668                 // Do we have an item for this photo?
669
670                 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
671                         dbesc($datum)
672                 );
673                 if(count($i1)) {
674
675                         $r = q("SELECT COUNT(*) AS `total`
676                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
677                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
678                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
679                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
680                                 $sql_extra ",
681                                 dbesc($i1[0]['uri']),
682                                 dbesc($i1[0]['uri'])
683
684                         );
685
686                         if(count($r))
687                                 $a->set_pager_total($r[0]['total']);
688
689
690                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
691                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
692                                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
693                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
694                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
695                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
696                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
697                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
698                                 $sql_extra
699                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
700                                 dbesc($i1[0]['uri']),
701                                 dbesc($i1[0]['uri']),
702                                 intval($a->pager['start']),
703                                 intval($a->pager['itemspage'])
704
705                         );
706                 }
707
708                 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
709
710                 if(count($i1) && strlen($i1[0]['tag'])) {
711                         // parse tags and add links     
712                         $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
713                         $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
714                 }
715
716                 if($cmd == 'edit') {
717                         $edit_tpl = file_get_contents('view/photo_edit.tpl');
718                         $o .= replace_macros($edit_tpl, array(
719                                 '$id' => $ph[0]['id'],
720                                 '$resource_id' => $ph[0]['resource-id'],
721                                 '$capt_label' => t('Caption'),
722                                 '$caption' => $ph[0]['desc'],
723                                 '$tag_label' => t('Tags'),
724                                 '$tags' => $i1[0]['tag'],
725                                 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
726                                 '$submit' => t('Submit'),
727                                 '$delete' => t('Delete Photo')
728
729                         ));
730                 }
731
732                 if(count($i1)) {
733                         // pull out how many people like the photo
734
735                         $cmnt_tpl = file_get_contents('view/comment_item.tpl');
736                         $tpl = file_get_contents('view/photo_item.tpl');
737                         $return_url = $a->cmd;
738
739                         if(can_write_wall($a,$a->data['user']['uid'])) {
740                                 if($i1[0]['last-child']) {
741                                         $o .= replace_macros($cmnt_tpl,array(
742                                                 '$return_path' => $return_url,
743                                                 '$type' => 'wall-comment',
744                                                 '$id' => $i1[0]['id'],
745                                                 '$parent' => $i1[0]['id'],
746                                                 '$profile_uid' =>  $a->data['user']['uid'],
747                                                 '$ww' => ''
748                                         ));
749                                 }
750                         }
751
752
753                         // display comments
754                         if(count($r)) {
755                                 foreach($r as $item) {
756                                         $comment = '';
757                                         $template = $tpl;
758                         
759                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
760                         
761                                         if(can_write_wall($a,$a->data['user']['uid'])) {
762                                                 if($item['last-child']) {
763                                                         $comment = replace_macros($cmnt_tpl,array(
764                                                                 '$return_path' => $return_url,
765                                                                 '$type' => 'wall-comment',
766                                                                 '$id' => $item['item_id'],
767                                                                 '$parent' => $item['parent'],
768                                                                 '$profile_uid' =>  $a->data['user']['uid'],
769                                                                 '$ww' => ''
770                                                         ));
771                                                 }
772                                         }
773
774                                         $profile_url = $item['url'];
775
776                                         if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) 
777                                                 && ($item['rel'] == DIRECTION_IN || $item['rel'] == DIRECTION_BOTH) && (! $item['self'] ))
778                                                 $profile_url = $redirect_url;
779  
780                                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
781                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
782                                         $profile_link = $profile_url;
783
784                                         $drop = '';
785
786                                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
787                                                 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
788
789
790                                         $o .= replace_macros($template,array(
791                                                 '$id' => $item['item_id'],
792                                                 '$profile_url' => $profile_link,
793                                                 '$name' => $profile_name,
794                                                 '$thumb' => $profile_avatar,
795                                                 '$title' => $item['title'],
796                                                 '$body' => bbcode($item['body']),
797                                                 '$ago' => relative_date($item['created']),
798                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
799                                                 '$drop' => $drop,
800                                                 '$comment' => $comment
801                                         ));
802                                 }
803                         }
804
805                         $o .= paginate($a);
806                 }
807                 return $o;
808         }
809
810         // Default - show recent photos with upload link (if applicable)
811
812         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
813                 $sql_extra GROUP BY `resource-id`",
814                 intval($a->data['user']['uid']),
815                 dbesc( t('Contact Photos'))
816         );
817         if(count($r))
818                 $a->set_pager_total(count($r));
819
820
821         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
822                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
823                 intval($a->data['user']['uid']),
824                 dbesc( t('Contact Photos')),
825                 intval($a->pager['start']),
826                 intval($a->pager['itemspage'])
827         );
828
829         $o .= '<h3>' . t('Recent Photos') . '</h3>';
830
831         if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
832                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
833                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
834         }
835
836         $tpl = file_get_contents('view/photo_top.tpl');
837         if(count($r)) {
838                 foreach($r as $rr) {
839                         $o .= replace_macros($tpl,array(
840                                 '$id' => $rr['id'],
841                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
842                                         . '/image/' . $rr['resource-id'],
843                                 '$phototitle' => t('View Photo'),
844                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
845                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
846                                 '$albumlink' => $a->get_baseurl . '/photos/' 
847                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
848                                 '$albumname' => $rr['album'],
849                                 '$albumalt' => t('View Album'),
850                                 '$imgalt' => $rr['filename']
851                         ));
852
853                 }
854                 $o .= '<div id="photo-top-end"></div>';
855         }
856         return $o;
857 }