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