]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
more notes on Windows install
[friendica.git] / mod / photos.php
1 <?php
2
3 require_once('include/Photo.php');
4 require_once('include/items.php');
5 require_once('include/acl_selectors.php');
6 require_once('include/bbcode.php');
7
8 function photos_init(&$a) {
9
10         $o = '';
11
12         if($a->argc > 1) {
13                 $nick = $a->argv[1];
14                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
15                         dbesc($nick)
16                 );
17
18                 if(! count($r))
19                         return;
20
21                 $a->data['user'] = $r[0];
22
23                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
24                         intval($a->data['user']['uid'])
25                 );
26
27                 if(count($albums)) {
28                         $a->data['albums'] = $albums;
29
30                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
31                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
32                 
33                         $o .= '<ul>';
34                         foreach($albums as $album) {
35                                 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
36                                         continue;
37                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
38                         }
39                         $o .= '</ul>';
40                 }
41
42                 if(! x($a->page,'aside'))
43                         $a->page['aside'] = '';
44                 $a->page['aside'] .= $o;
45         }
46
47         return;
48 }
49
50
51
52
53 function photos_post(&$a) {
54
55         $can_post  = false;
56         $visitor   = 0;
57
58         $page_owner_uid = $a->data['user']['uid'];
59         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
60
61         if((local_user()) && (local_user() == $page_owner_uid))
62                 $can_post = true;
63         else {
64                 if($community_page && remote_user()) {
65                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
66                                 intval(remote_user()),
67                                 intval($page_owner_uid)
68                         );
69                         if(count($r)) {
70                                 $can_post = true;
71                                 $visitor = remote_user();
72                         }
73                 }
74         }
75
76         if(! $can_post) {
77                 notice( t('Permission denied.') . EOL );
78                 killme();
79         }
80
81         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
82                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
83                 intval($page_owner_uid)
84         );
85
86         if(! count($r)) {
87                 notice( t('Contact information unavailable') . EOL);
88                 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
89                 killme();
90         }
91
92         $owner_record = $r[0];  
93
94
95         if(($a->argc > 3) && ($a->argv[2] === 'album')) {
96                 $album = hex2bin($a->argv[3]);
97
98                 if($album == t('Profile Photos') || $album == t('Contact Photos')) {
99                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
100                         return; // NOTREACHED
101                 }
102
103                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
104                         dbesc($album),
105                         intval($page_owner_uid)
106                 );
107                 if(! count($r)) {
108                         notice( t('Album not found.') . EOL);
109                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
110                         return; // NOTREACHED
111                 }
112
113                 $newalbum = notags(trim($_POST['albumname']));
114                 if($newalbum != $album) {
115                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
116                                 dbesc($newalbum),
117                                 dbesc($album),
118                                 intval($page_owner_uid)
119                         );
120                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
121                         goaway($a->get_baseurl() . '/' . $newurl);
122                         return; // NOTREACHED
123                 }
124
125
126                 if($_POST['dropalbum'] == t('Delete Album')) {
127
128                         $res = array();
129
130                         // get the list of photos we are about to delete
131
132                         if($visitor) {
133                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
134                                         intval($visitor),
135                                         intval($page_owner_uid),
136                                         dbesc($album)
137                                 );
138                         }
139                         else {
140                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
141                                         intval(local_user()),
142                                         dbesc($album)
143                                 );
144                         }
145                         if(count($r)) {
146                                 foreach($r as $rr) {
147                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
148                                 }
149                         }
150                         else {
151                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
152                                 return; // NOTREACHED
153                         }
154
155                         $str_res = implode(',', $res);
156
157                         // remove the associated photos
158
159                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
160                                 intval($page_owner_uid)
161                         );
162
163                         // find and delete the corresponding item with all the comments and likes/dislikes
164
165                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
166                                 intval($page_owner_uid)
167                         );
168                         if(count($r)) {
169                                 foreach($r as $rr) {
170                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
171                                                 dbesc(datetime_convert()),
172                                                 dbesc($rr['parent-uri']),
173                                                 intval($page_owner_uid)
174                                         );
175
176                                         $drop_id = intval($rr['id']);
177                                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
178
179                                         // send the notification upstream/downstream as the case may be
180
181                                         if($rr['visible'])
182                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
183                                                         array(),$foo));
184
185                                 }
186                         }
187                 }
188                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
189                 return; // NOTREACHED
190         }
191
192         if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
193
194                 // same as above but remove single photo
195
196                 if($visitor) {
197                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
198                                 intval($visitor),
199                                 intval($page_owner_uid),
200                                 dbesc($a->argv[2])
201                         );
202                 }
203                 else {
204                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
205                                 intval(local_user()),
206                                 dbesc($a->argv[2])
207                         );
208                 }
209                 if(count($r)) {
210                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
211                                 intval($page_owner_uid),
212                                 dbesc($r[0]['resource-id'])
213                         );
214                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
215                                 dbesc($r[0]['resource-id']),
216                                 intval($page_owner_uid)
217                         );
218                         if(count($i)) {
219                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
220                                         dbesc(datetime_convert()),
221                                         dbesc(datetime_convert()),
222                                         dbesc($i[0]['uri']),
223                                         intval($page_owner_uid)
224                                 );
225
226                                 $url = $a->get_baseurl();
227                                 $drop_id = intval($i[0]['id']);
228                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
229
230                                 if($i[0]['visible'])
231                                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
232                                                 array(),$foo));
233                         }
234                 }
235
236                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
237                 return; // NOTREACHED
238         }
239
240         if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false))) {
241
242                 $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))   : '');
243                 $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag'])) : '');
244                 $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])      : 0);
245                 $resource_id = $a->argv[2];
246
247                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
248                         dbesc($resource_id),
249                         intval($page_owner_uid)
250                 );
251                 if((count($p)) && ($p[0]['desc'] !== $desc)) {
252                         $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
253                                 dbesc($desc),
254                                 dbesc($resource_id),
255                                 intval($page_owner_uid)
256                         );
257                 }
258                 if(! $item_id) {
259
260                         // Create item container
261
262                         $title = '';
263                         $basename = basename($filename);
264                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
265
266                         $arr = array();
267
268                         $arr['uid']           = $page_owner_uid;
269                         $arr['uri']           = $uri;
270                         $arr['parent-uri']    = $uri; 
271                         $arr['type']          = 'photo';
272                         $arr['wall']          = 1;
273                         $arr['resource-id']   = $p[0]['resource-id'];
274                         $arr['contact-id']    = $owner_record['id'];
275                         $arr['owner-name']    = $owner_record['name'];
276                         $arr['owner-link']    = $owner_record['url'];
277                         $arr['owner-avatar']  = $owner_record['thumb'];
278                         $arr['author-name']   = $owner_record['name'];
279                         $arr['author-link']   = $owner_record['url'];
280                         $arr['author-avatar'] = $owner_record['thumb'];
281                         $arr['title']         = $title;
282                         $arr['allow_cid']     = $p[0]['allow_cid'];
283                         $arr['allow_gid']     = $p[0]['allow_gid'];
284                         $arr['deny_cid']      = $p[0]['deny_cid'];
285                         $arr['deny_gid']      = $p[0]['deny_gid'];
286                         $arr['last-child']    = 1;
287                         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
288                                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
289                                                 . '[/url]';
290                 
291                         $item_id = item_store($arr);
292
293                 }
294
295                 if($item_id) {
296                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
297                                 intval($item_id),
298                                 intval($page_owner_uid)
299                         );
300                 }
301                 if(count($r)) {
302                         $old_tag    = $r[0]['tag'];
303                         $old_inform = $r[0]['inform'];
304                 }
305
306                 if(strlen($rawtags)) {
307
308                         $str_tags = '';
309                         $inform   = '';
310
311                         // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
312
313                         $x = substr($rawtags,0,1);
314                         if($x !== '@' && $x !== '#')
315                                 $rawtags = '#' . $rawtags;
316
317                         $taginfo = array();
318                         $tags = get_tags($rawtags);
319
320                         if(count($tags)) {
321                                 foreach($tags as $tag) {
322                                         if(strpos($tag,'@') === 0) {
323                                                 $name = substr($tag,1);
324                                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
325                                                         $newname = $name;
326                                                         $links = @lrdd($name);
327                                                         if(count($links)) {
328                                                                 foreach($links as $link) {
329                                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
330                                                         $profile = $link['@attributes']['href'];
331                                                                         if($link['@attributes']['rel'] === 'salmon') {
332                                                                                 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
333                                                                                 if(strlen($inform))
334                                                                                         $inform .= ',';
335                                                         $inform .= $salmon;
336                                                                         }
337                                                                 }
338                                                         }
339                                                         $taginfo[] = array($newname,$profile,$salmon);
340                                                 }
341                                                 else {
342                                                         $newname = $name;
343                                                         if(strstr($name,'_')) {
344                                                                 $newname = str_replace('_',' ',$name);
345                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
346                                                                         dbesc($newname),
347                                                                         intval($page_owner_uid)
348                                                                 );
349                                                         }
350                                                         else {
351                                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
352                                                                         dbesc($name),
353                                                                         intval($page_owner_uid)
354                                                                 );
355                                                         }
356                                                         if(count($r)) {
357                                                                 $newname = $r[0]['name'];
358                                                                 $profile = $r[0]['url'];
359                                                                 $notify = 'cid:' . $r[0]['id'];
360                                                                 if(strlen($inform))
361                                                                         $inform .= ',';
362                                                                 $inform .= $notify;
363                                                         }
364                                                 }
365                                                 if($profile) {
366                                                         if(substr($notify,0,4) === 'cid:')
367                                                                 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname  . '[/url]');
368                                                         else
369                                                                 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname     . '[/url]');
370                                                         if(strlen($str_tags))
371                                                                 $str_tags .= ',';
372                                                         $profile = str_replace(',','%2c',$profile);
373                                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
374                                                 }
375                                         }
376                                 }
377                         }
378
379                         $newtag = $old_tag;
380                         if(strlen($newtag) && strlen($str_tags)) 
381                                 $newtag .= ',';
382                         $newtag .= $str_tags;
383
384                         $newinform = $old_inform;
385                         if(strlen($newinform) && strlen($inform))
386                                 $newinform .= ',';
387                         $newinform .= $inform;
388
389                         $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
390                                 dbesc($newtag),
391                                 dbesc($newinform),
392                                 dbesc(datetime_convert()),
393                                 dbesc(datetime_convert()),
394                                 intval($item_id),
395                                 intval($page_owner_uid)
396                         );
397
398                         $best = 0;
399                         foreach($p as $scales) {
400                                 if(intval($scales['scale']) == 2) {
401                                         $best = 2;
402                                         break;
403                                 }
404                                 if(intval($scales['scale']) == 4) {
405                                         $best = 4;
406                                         break;
407                                 }
408                         }
409
410                         if(count($taginfo)) {
411                                 foreach($taginfo as $tagged) {
412                 
413                                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
414
415                                         $arr = array();
416
417                                         $arr['uid']           = $page_owner_uid;
418                                         $arr['uri']           = $uri;
419                                         $arr['parent-uri']    = $uri;
420                                         $arr['type']          = 'activity';
421                                         $arr['wall']          = 1;
422                                         $arr['contact-id']    = $owner_record['id'];
423                                         $arr['owner-name']    = $owner_record['name'];
424                                         $arr['owner-link']    = $owner_record['url'];
425                                         $arr['owner-avatar']  = $owner_record['thumb'];
426                                         $arr['author-name']   = $owner_record['name'];
427                                         $arr['author-link']   = $owner_record['url'];
428                                         $arr['author-avatar'] = $owner_record['thumb'];
429                                         $arr['title']         = '';
430                                         $arr['allow_cid']     = $p[0]['allow_cid'];
431                                         $arr['allow_gid']     = $p[0]['allow_gid'];
432                                         $arr['deny_cid']      = $p[0]['deny_cid'];
433                                         $arr['deny_gid']      = $p[0]['deny_gid'];
434                                         $arr['last-child']    = 1;
435                                         $arr['visible']       = 1;
436                                         $arr['verb']          = ACTIVITY_TAG;
437                                         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
438                                         $arr['target-type']   = ACTIVITY_OBJ_PHOTO;
439                                         $arr['tag']           = $tagged[4];
440                                         $arr['inform']        = $tagged[2];
441
442                                         $arr['body']          = '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]' . ' ' . t('was tagged in a') . ' ' . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('photo') . '[/url]' . ' ' . t('by') . ' ' . '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]' ;
443                                         $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '[/img][/url]' . "\n" ;
444
445                                         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
446                                         $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
447                                         if($tagged[3])
448                                                 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
449                                         $arr['object'] .= '</link></object>' . "\n";
450
451                                         $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
452                                                 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
453                                         $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="image/jpeg" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '" />') . '</link></target>';
454
455                                         $item_id = item_store($arr);
456                                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
457                                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"tag\" \"$item_id\" & ",
458                                                 array(),$foo));
459                                 }
460
461                         }
462
463                 }
464                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
465                 return; // NOTREACHED
466         }
467
468
469         // default post action - upload a photo
470
471         if(! x($_FILES,'userfile'))
472                 killme();
473
474         if($_POST['partitionCount'])
475                 $java_upload = true;
476         else
477                 $java_upload = false;
478
479         $album =  notags(trim($_POST['album']));
480         $newalbum = notags(trim($_POST['newalbum']));
481
482         if(! strlen($album)) {
483                 if(strlen($newalbum))
484                         $album = $newalbum;
485                 else
486                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
487         }
488
489         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
490                 dbesc($album),
491                 intval($page_owner_uid)
492         );
493         if((! count($r)) || ($album == t('Profile Photos')))
494                 $visible = 1;
495         else
496                 $visible = 0;
497
498
499         $str_group_allow   = perms2str($_POST['group_allow']);
500         $str_contact_allow = perms2str($_POST['contact_allow']);
501         $str_group_deny    = perms2str($_POST['group_deny']);
502         $str_contact_deny  = perms2str($_POST['contact_deny']);
503
504         $src               = $_FILES['userfile']['tmp_name'];
505         $filename          = basename($_FILES['userfile']['name']);
506         $filesize          = intval($_FILES['userfile']['size']);
507
508         $maximagesize = get_config('system','maximagesize');
509
510         if(($maximagesize) && ($filesize > $maximagesize)) {
511                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
512                 @unlink($src);
513                 return;
514         }
515
516         $imagedata = @file_get_contents($src);
517         $ph = new Photo($imagedata);
518
519         if(! $ph->is_valid()) {
520                 notice( t('Unable to process image.') . EOL );
521                 @unlink($src);
522                 killme();
523         }
524
525         @unlink($src);
526
527         $width = $ph->getWidth();
528         $height = $ph->getHeight();
529
530         $smallest = 0;
531
532         $photo_hash = photo_new_resource();
533
534         $r = $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
535
536         if(! $r) {
537                 notice( t('Image upload failed.') . EOL );
538                 killme();
539         }
540
541         if($width > 640 || $height > 640) {
542                 $ph->scaleImage(640);
543                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
544                 $smallest = 1;
545         }
546
547         if($width > 320 || $height > 320) {
548                 $ph->scaleImage(320);
549                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
550                 $smallest = 2;
551         }
552         
553         $basename = basename($filename);
554         $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
555
556         // Create item container
557
558
559         $arr = array();
560
561         $arr['uid']           = $page_owner_uid;
562         $arr['uri']           = $uri;
563         $arr['parent-uri']    = $uri;
564         $arr['type']          = 'photo';
565         $arr['wall']          = 1;
566         $arr['resource-id']   = $photo_hash;
567         $arr['contact-id']    = $owner_record['id'];
568         $arr['owner-name']    = $owner_record['name'];
569         $arr['owner-link']    = $owner_record['url'];
570         $arr['owner-avatar']  = $owner_record['thumb'];
571         $arr['author-name']   = $owner_record['name'];
572         $arr['author-link']   = $owner_record['url'];
573         $arr['author-avatar'] = $owner_record['thumb'];
574         $arr['title']         = '';
575         $arr['allow_cid']     = $str_contact_allow;
576         $arr['allow_gid']     = $str_group_allow;
577         $arr['deny_cid']      = $str_contact_deny;
578         $arr['deny_gid']      = $str_group_deny;
579         $arr['last-child']    = 1;
580         $arr['visible']       = $visible;
581         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
582                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
583                                 . '[/url]';
584
585         $item_id = item_store($arr);
586
587         if(! $java_upload) {
588                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
589                 return; // NOTREACHED
590         }
591
592         killme();
593         return; // NOTREACHED
594
595 }
596
597
598
599 function photos_content(&$a) {
600
601         // URLs:
602         // photos/name
603         // photos/name/upload
604         // photos/name/album/xxxxx
605         // photos/name/album/xxxxx/edit
606         // photos/name/image/xxxxx
607         // photos/name/image/xxxxx/edit
608
609
610         if(! x($a->data,'user')) {
611                 notice( t('No photos selected') . EOL );
612                 return;
613         }
614
615         $_SESSION['photo_return'] = $a->cmd;
616
617         //
618         // Parse arguments 
619         //
620
621         if($a->argc > 3) {
622                 $datatype = $a->argv[2];
623                 $datum = $a->argv[3];
624         }
625         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
626                 $datatype = 'upload';
627         else
628                 $datatype = 'summary';
629
630         if($a->argc > 4)
631                 $cmd = $a->argv[4];
632         else
633                 $cmd = 'view';
634
635         //
636         // Setup permissions structures
637         //
638
639         $can_post       = false;
640         $visitor        = 0;
641         $contact        = null;
642         $remote_contact = false;
643
644         $owner_uid = $a->data['user']['uid'];
645
646         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
647
648         if((local_user()) && (local_user() == $owner_uid))
649                 $can_post = true;
650         else {
651                 if($community_page && remote_user()) {
652                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
653                                 intval(remote_user()),
654                                 intval($owner_uid)
655                         );
656                         if(count($r)) {
657                                 $can_post = true;
658                                 $contact  = $r[0];
659                                 $remote_contact = true;
660                                 $visitor = remote_user();
661                         }
662                 }
663         }
664
665         // perhaps they're visiting - but not a community page, so they wouldn't have write access
666
667         if(remote_user() && (! $visitor)) {
668                 $contact_id = $_SESSION['visitor_id'];
669                 $groups = init_groups_visitor($contact_id);
670                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
671                         intval(remote_user()),
672                         intval($owner_uid)
673                 );
674                 if(count($r)) {
675                         $contact = $r[0];
676                         $remote_contact = true;
677                 }
678         }
679
680         if(! $remote_contact) {
681                 if(local_user()) {
682                         $contact_id = $_SESSION['cid'];
683                         $contact = $a->contact;
684                 }
685         }
686
687         // default permissions - anonymous user
688
689         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
690
691         // Profile owner - everything is visible
692
693         if(local_user() && (local_user() == $owner_uid)) {
694                 $sql_extra = '';        
695         }
696         elseif(remote_user()) {
697                 // authenticated visitor - here lie dragons
698                 $gs = '<<>>'; // should be impossible to match
699                 if(count($groups)) {
700                         foreach($groups as $g)
701                                 $gs .= '|<' . intval($g) . '>';
702                 } 
703                 $sql_extra = sprintf(
704                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
705                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
706                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
707                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
708
709                         intval(remote_user()),
710                         intval(remote_user()),
711                         dbesc($gs),
712                         dbesc($gs)
713                 );
714         }
715
716         //
717         // dispatch request
718         //
719
720
721         if($datatype === 'upload') {
722                 if(! ($can_post)) {
723                         notice( t('Permission denied.'));
724                         return;
725                 }
726                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
727
728                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
729                 if(count($a->data['albums'])) {
730                         foreach($a->data['albums'] as $album) {
731                                 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
732                                         continue;
733                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
734                         }
735                 }
736
737                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
738
739                 $albumselect .= '</select>';
740                 $tpl = load_view_file('view/photos_upload.tpl');
741                 $o .= replace_macros($tpl,array(
742                         '$pagename' => t('Upload Photos'),
743                         '$sessid' => session_id(),
744                         '$nickname' => $a->data['user']['nickname'],
745                         '$newalbum' => t('New album name: '),
746                         '$existalbumtext' => t('or existing album name: '),
747                         '$filestext' => t('Select files to upload: '),
748                         '$albumselect' => $albumselect,
749                         '$permissions' => t('Permissions'),
750                         '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
751                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
752                         '$nojava' => t('Use the following controls only if the Java uploader [above] fails to launch.'),
753                         '$uploadurl' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
754                         '$submit' => t('Submit')
755                 ));
756
757                 return $o; 
758         }
759
760         if($datatype === 'album') {
761
762                 $album = hex2bin($datum);
763
764                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
765                         $sql_extra GROUP BY `resource-id`",
766                         intval($owner_uid),
767                         dbesc($album)
768                 );
769                 if(count($r))
770                         $a->set_pager_total(count($r));
771
772
773                 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
774                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
775                         intval($owner_uid),
776                         dbesc($album),
777                         intval($a->pager['start']),
778                         intval($a->pager['itemspage'])
779                 );
780
781                 $o .= '<h3>' . $album . '</h3>';
782                 
783                 if($cmd === 'edit') {           
784                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
785                                 if($can_post) {
786                                         $edit_tpl = load_view_file('view/album_edit.tpl');
787                                         $o .= replace_macros($edit_tpl,array(
788                                                 '$nametext' => t('New album name: '),
789                                                 '$nickname' => $a->data['user']['nickname'],
790                                                 '$album' => $album,
791                                                 '$hexalbum' => bin2hex($album),
792                                                 '$submit' => t('Submit'),
793                                                 '$dropsubmit' => t('Delete Album')
794                                         ));
795                                 }
796                         }
797                 }
798                 else {
799                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
800                                 if($can_post) {
801                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
802                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
803                                                 . t('Edit Album') . '</a></div>';
804                                 }
805                         }
806                 }
807                 $tpl = load_view_file('view/photo_album.tpl');
808                 if(count($r))
809                         foreach($r as $rr) {
810                                 $o .= replace_macros($tpl,array(
811                                         '$id' => $rr['id'],
812                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
813                                         '$phototitle' => t('View Photo'),
814                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
815                                         '$imgalt' => $rr['filename']
816                                 ));
817
818                 }
819                 $o .= '<div id="photo-album-end"></div>';
820                 return $o;
821
822         }       
823
824
825         if($datatype === 'image') {
826
827                 require_once('security.php');
828                 require_once('bbcode.php');
829
830                 $o = '<div id="live-display"></div>' . "\r\n";
831                 // fetch image, item containing image, then comments
832
833                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
834                         $sql_extra ORDER BY `scale` ASC ",
835                         intval($owner_uid),
836                         dbesc($datum)
837                 );
838
839                 if(! count($ph)) {
840                         notice( t('Photo not available') . EOL );
841                         return;
842                 }
843
844                 if(count($ph) == 1)
845                         $hires = $lores = $ph[0];
846                 if(count($ph) > 1) {
847                         if($ph[1]['scale'] == 2) {
848                                 // original is 640 or less, we can display it directly
849                                 $hires = $lores = $ph[0];
850                         }
851                         else {
852                         $hires = $ph[0];
853                         $lores = $ph[1];
854                         }
855                 }
856
857                 
858                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
859  
860                 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
861                         $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>';
862                 }
863
864
865                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
866                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
867                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
868                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
869
870
871                 // Do we have an item for this photo?
872
873                 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
874                         dbesc($datum)
875                 );
876                 if(count($linked_items)) {
877                         $link_item = $linked_items[0];
878                         $r = q("SELECT COUNT(*) AS `total`
879                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
880                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
881                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
882                                 AND `item`.`uid` = %d 
883                                 $sql_extra ",
884                                 dbesc($link_item['uri']),
885                                 dbesc($link_item['uri']),
886                                 intval($link_item['uid'])
887
888                         );
889
890                         if(count($r))
891                                 $a->set_pager_total($r[0]['total']);
892
893
894                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
895                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
896                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
897                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
898                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
899                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
900                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
901                                 AND `item`.`uid` = %d
902                                 $sql_extra
903                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
904                                 dbesc($link_item['uri']),
905                                 dbesc($link_item['uri']),
906                                 intval($link_item['uid']),
907                                 intval($a->pager['start']),
908                                 intval($a->pager['itemspage'])
909
910                         );
911                 
912                         if((local_user()) && (local_user() == $link_item['uid'])) {
913                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
914                                         intval($link_item['parent']),
915                                         intval(local_user())
916                                 );
917                         }
918                 }
919
920                 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
921
922                 if(count($linked_items) && strlen($link_item['tag'])) {
923                         $arr = explode(',',$link_item['tag']);
924                         // parse tags and add links     
925                         $o .= '<div id="in-this-photo-text">' . t('Tags: ') . '</div>';
926                         $o .= '<div id="in-this-photo">';
927                         $tag_str = '';
928                         foreach($arr as $t) {
929                                 if(strlen($tag_str))
930                                         $tag_str .= ', ';
931                                 $tag_str .= bbcode($t);
932                         } 
933                         $o .= $tag_str . '</div>';
934                         if($cmd === 'edit')
935                                 $o .= '<div id="tag-remove"><a href="' . $a->get_baseurl() . '/tagrm/' . $link_item['id'] . '">' . t('[Remove any tag]') . '</a></div>';
936                 }
937
938
939                 if(($cmd === 'edit') && ($can_post)) {
940                         $edit_tpl = load_view_file('view/photo_edit.tpl');
941                         $o .= replace_macros($edit_tpl, array(
942                                 '$id' => $ph[0]['id'],
943                                 '$nickname' => $a->data['user']['nickname'],
944                                 '$resource_id' => $ph[0]['resource-id'],
945                                 '$capt_label' => t('Caption'),
946                                 '$caption' => $ph[0]['desc'],
947                                 '$tag_label' => t('Add a Tag'),
948                                 '$tags' => $link_item['tag'],
949                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
950                                 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
951                                 '$submit' => t('Submit'),
952                                 '$delete' => t('Delete Photo')
953                         ));
954                 }
955
956                 if(count($linked_items)) {
957
958                         $cmnt_tpl = load_view_file('view/comment_item.tpl');
959                         $tpl = load_view_file('view/photo_item.tpl');
960                         $return_url = $a->cmd;
961
962                         $like_tpl = load_view_file('view/like.tpl');
963
964                         $likebuttons = '';
965
966                         if($can_post || can_write_wall($a,$owner_uid))
967                                 $likebuttons = replace_macros($like_tpl,array('$id' => $link_item['id']));
968
969                         if(! count($r)) {
970                                 $o .= '<div id="photo-like-div">';
971                                 $o .= $likebuttons;
972                                 $o .= '</div>';
973
974                                 if($can_post || can_write_wall($a,$owner_uid)) {
975                                         if($link_item['last-child']) {
976                                                 $o .= replace_macros($cmnt_tpl,array(
977                                                         '$return_path' => $return_url,
978                                                         '$type' => 'wall-comment',
979                                                         '$id' => $link_item['id'],
980                                                         '$parent' => $link_item['id'],
981                                                         '$profile_uid' =>  $owner_uid,
982                                                         '$mylink' => $contact['url'],
983                                                         '$mytitle' => t('This is you'),
984                                                         '$myphoto' => $contact['thumb'],
985                                                         '$ww' => ''
986                                                 ));
987                                         }
988                                 }
989                         }
990
991                         $alike = array();
992                         $dlike = array();
993
994                         // display comments
995                         if(count($r)) {
996
997                                 foreach($r as $item) {
998                                         like_puller($a,$item,$alike,'like');
999                                         like_puller($a,$item,$dlike,'dislike');
1000                                 }
1001
1002                     $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1003                                 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1004
1005                                 $o .= '<div id="photo-like-div">';
1006                                 $o .= $likebuttons;
1007                                 $o .= $like;
1008                                 $o .= $dislike;
1009                                 $o .= '</div>';
1010
1011
1012
1013                                 if($can_post || can_write_wall($a,$owner_uid)) {
1014                                         if($link_item['last-child']) {
1015                                                 $o .= replace_macros($cmnt_tpl,array(
1016                                                         '$return_path' => $return_url,
1017                                                         '$type' => 'wall-comment',
1018                                                         '$id' => $link_item['id'],
1019                                                         '$parent' => $link_item['id'],
1020                                                         '$profile_uid' =>  $owner_uid,
1021                                                         '$mylink' => $contact['url'],
1022                                                         '$mytitle' => t('This is you'),
1023                                                         '$myphoto' => $contact['thumb'],
1024                                                         '$ww' => ''
1025                                                 ));
1026                                         }
1027                                 }
1028
1029
1030                                 foreach($r as $item) {
1031                                         $comment = '';
1032                                         $template = $tpl;
1033                                         $sparkle = '';
1034
1035                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1036                                                 continue;
1037
1038                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1039                         
1040                                         if($can_post || can_write_wall($a,$owner_uid)) {
1041
1042                                                 if($item['last-child']) {
1043                                                         $comment = replace_macros($cmnt_tpl,array(
1044                                                                 '$return_path' => $return_url,
1045                                                                 '$type' => 'wall-comment',
1046                                                                 '$id' => $item['item_id'],
1047                                                                 '$parent' => $item['parent'],
1048                                                                 '$profile_uid' =>  $owner_uid,
1049                                                                 '$mylink' => $contact['url'],
1050                                                                 '$mytitle' => t('This is you'),
1051                                                                 '$myphoto' => $contact['thumb'],
1052                                                                 '$ww' => ''
1053                                                         ));
1054                                                 }
1055                                         }
1056
1057
1058                                         if(local_user() && ($item['contact-uid'] == local_user()) 
1059                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1060                                                 $profile_url = $redirect_url;
1061                                                 $sparkle = ' sparkle';
1062                                         }
1063                                         else {
1064                                                 $profile_url = $item['url'];
1065                                                 $sparkle = '';
1066                                         }
1067  
1068                                         $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1069
1070                                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
1071                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1072
1073                                         $profile_link = $profile_url;
1074
1075                                         $drop = '';
1076
1077                                         if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1078                                                 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id']));
1079
1080
1081                                         $o .= replace_macros($template,array(
1082                                                 '$id' => $item['item_id'],
1083                                                 '$profile_url' => $profile_link,
1084                                                 '$name' => $profile_name,
1085                                                 '$thumb' => $profile_avatar,
1086                                                 '$sparkle' => $sparkle,
1087                                                 '$title' => $item['title'],
1088                                                 '$body' => bbcode($item['body']),
1089                                                 '$ago' => relative_date($item['created']),
1090                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1091                                                 '$drop' => $drop,
1092                                                 '$comment' => $comment
1093                                         ));
1094                                 }
1095                         }
1096
1097                         $o .= paginate($a);
1098                 }
1099                 return $o;
1100         }
1101
1102         // Default - show recent photos with upload link (if applicable)
1103         $o = '';
1104
1105         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
1106                 $sql_extra GROUP BY `resource-id`",
1107                 intval($a->data['user']['uid']),
1108                 dbesc( t('Contact Photos'))
1109         );
1110         if(count($r))
1111                 $a->set_pager_total(count($r));
1112
1113
1114         $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1115                 WHERE `uid` = %d AND `album` != '%s' 
1116                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1117                 intval($a->data['user']['uid']),
1118                 dbesc( t('Contact Photos')),
1119                 intval($a->pager['start']),
1120                 intval($a->pager['itemspage'])
1121         );
1122
1123         $o .= '<h3>' . t('Recent Photos') . '</h3>';
1124
1125         if($can_post) {
1126                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
1127                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
1128         }
1129
1130         $tpl = load_view_file('view/photo_top.tpl');
1131         if(count($r)) {
1132                 foreach($r as $rr) {
1133                         $o .= replace_macros($tpl,array(
1134                                 '$id' => $rr['id'],
1135                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
1136                                         . '/image/' . $rr['resource-id'],
1137                                 '$phototitle' => t('View Photo'),
1138                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
1139                                         . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1140                                 '$albumlink' => $a->get_baseurl() . '/photos/' 
1141                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1142                                 '$albumname' => $rr['album'],
1143                                 '$albumalt' => t('View Album'),
1144                                 '$imgalt' => $rr['filename']
1145                         ));
1146
1147                 }
1148                 $o .= '<div id="photo-top-end"></div>';
1149         }
1150         return $o;
1151 }