]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
really commiting files this time, still getting the hang of git.
[friendica.git] / mod / photos.php
index 7d3e57616a1f43ddd6fabe713d93a900a93bd08e..cb7df15ccf96d6428e5f9d32a960ea8ab04e1c1c 100644 (file)
@@ -3,9 +3,14 @@ require_once('include/Photo.php');
 require_once('include/items.php');
 require_once('include/acl_selectors.php');
 require_once('include/bbcode.php');
+require_once('include/security.php');
 
 function photos_init(&$a) {
 
+
+       if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+               return;
+       }
        $o = '';
 
        if($a->argc > 1) {
@@ -19,19 +24,30 @@ function photos_init(&$a) {
 
                $a->data['user'] = $r[0];
 
-               $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
+               $sql_extra = permissions_sql($a->data['user']['uid']);
+
+               $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra ",
                        intval($a->data['user']['uid'])
                );
 
                if(count($albums)) {
                        $a->data['albums'] = $albums;
 
-                       $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
-                       $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
-               
+                       $o .= '<div class="vcard">';
+                       $o .= '<div class="fn">' . $a->data['user']['username'] . '</h4>';
+                       $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg" alt="' . $a->data['user']['username'] . '" /></div>';
+                       $o .= '</div>';
+                       
+                       $o .= '<div id="side-bar-photos-albums" class="widget">';
+                       $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
+                                       
                        $o .= '<ul>';
                        foreach($albums as $album) {
-                               if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
+
+                               // don't show contact photos. We once translated this name, but then you could still access it under
+                               // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
+
+                               if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
                                        continue;
                                $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
                        }
@@ -41,6 +57,32 @@ function photos_init(&$a) {
                if(! x($a->page,'aside'))
                        $a->page['aside'] = '';
                $a->page['aside'] .= $o;
+
+
+               $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
+
+               $a->page['htmlhead'] .= <<< EOT
+
+               $(document).ready(function() {
+
+                       $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
+                               var selstr;
+                               $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
+                                       selstr = $(this).text();
+                                       $('#jot-perms-icon').removeClass('unlock').addClass('lock');
+                                       $('#jot-public').hide();
+                               });
+                               if(selstr == null) { 
+                                       $('#jot-perms-icon').removeClass('lock').addClass('unlock');
+                                       $('#jot-public').show();
+                               }
+
+                       }).trigger('change');
+
+               });
+
+               </script>
+EOT;
        }
 
        return;
@@ -50,15 +92,15 @@ function photos_init(&$a) {
 
 function photos_post(&$a) {
 
-logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
+       logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
 
-foreach($_REQUEST AS $key => $val) {
-       logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
-}
+       foreach($_REQUEST AS $key => $val) {
+               logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
+       }
 
-foreach($_FILES AS $key => $val) {
-       logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
-}
+       foreach($_FILES AS $key => $val) {
+               logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
+       }
 
        $can_post  = false;
        $visitor   = 0;
@@ -103,7 +145,7 @@ foreach($_FILES AS $key => $val) {
        if(($a->argc > 3) && ($a->argv[2] === 'album')) {
                $album = hex2bin($a->argv[3]);
 
-               if($album == t('Profile Photos') || $album == t('Contact Photos')) {
+               if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
                        goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
                        return; // NOTREACHED
                }
@@ -246,6 +288,11 @@ foreach($_FILES AS $key => $val) {
                $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
                $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
                $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
+               $str_group_allow   = perms2str($_POST['group_allow']);
+               $str_contact_allow = perms2str($_POST['contact_allow']);
+               $str_group_deny    = perms2str($_POST['group_deny']);
+               $str_contact_deny  = perms2str($_POST['contact_deny']);
+
                $resource_id = $a->argv[2];
 
                if(! strlen($albname))
@@ -256,10 +303,14 @@ foreach($_FILES AS $key => $val) {
                        dbesc($resource_id),
                        intval($page_owner_uid)
                );
-               if((count($p)) && (($p[0]['desc'] !== $desc) || ($p[0]['album'] !== $albname))) {
-                       $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
+               if(count($p)) {
+                       $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
                                dbesc($desc),
                                dbesc($albname),
+                               dbesc($str_contact_allow),
+                               dbesc($str_group_allow),
+                               dbesc($str_contact_deny),
+                               dbesc($str_group_deny),
                                dbesc($resource_id),
                                intval($page_owner_uid)
                        );
@@ -301,6 +352,7 @@ foreach($_FILES AS $key => $val) {
                        $arr['deny_gid']      = $p[0]['deny_gid'];
                        $arr['last-child']    = 1;
                        $arr['visible']       = $visibility;
+                       
                        $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
                                                . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
                                                . '[/url]';
@@ -336,6 +388,8 @@ foreach($_FILES AS $key => $val) {
 
                        if(count($tags)) {
                                foreach($tags as $tag) {
+                                       if(isset($profile))
+                                               unset($profile);
                                        if(strpos($tag,'@') === 0) {
                                                $name = substr($tag,1);
                                                if((strpos($name,'@')) || (strpos($name,'http://'))) {
@@ -357,7 +411,7 @@ foreach($_FILES AS $key => $val) {
                                                }
                                                else {
                                                        $newname = $name;
-                                                       if(strstr($name,'_')) {
+                                                       if(strstr($name,'_') || strstr($name,' ')) {
                                                                $newname = str_replace('_',' ',$name);
                                                                $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
                                                                        dbesc($newname),
@@ -365,7 +419,8 @@ foreach($_FILES AS $key => $val) {
                                                                );
                                                        }
                                                        else {
-                                                               $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
+                                                               $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
+                                                                       dbesc($name),
                                                                        dbesc($name),
                                                                        intval($page_owner_uid)
                                                                );
@@ -470,8 +525,15 @@ foreach($_FILES AS $key => $val) {
                                        $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>';
 
                                        $item_id = item_store($arr);
-                                       if($item_id)
+                                       if($item_id) {
+                                               q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                                                       dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
+                                                       intval($page_owner_uid),
+                                                       intval($item_id)
+                                               );
+
                                                proc_run('php',"include/notifier.php","tag","$item_id");
+                                       }
                                }
 
                        }
@@ -508,13 +570,13 @@ foreach($_FILES AS $key => $val) {
         *
         * We create a wall item for every photo, but we don't want to
         * overwhelm the data stream with a hundred newly uploaded photos.
-        * So we will make one photo (the first one uploaded to this album)
+        * So we will make the first photo uploaded to this album in the last several hours
         * visible by default, the rest will become visible over time when and if
         * they acquire comments, likes, dislikes, and/or tags 
         *
         */
 
-       $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
+       $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
                dbesc($album),
                intval($page_owner_uid)
        );
@@ -543,11 +605,24 @@ foreach($_FILES AS $key => $val) {
                $filesize   = intval($_FILES['userfile']['size']);
        }
 
+
+       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
+
        $maximagesize = get_config('system','maximagesize');
 
        if(($maximagesize) && ($filesize > $maximagesize)) {
                notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
                @unlink($src);
+               $foo = 0;
+               call_hooks('photo_post_end',$foo);
+               return;
+       }
+
+       if(! $filesize) {
+               notice( t('Image file is empty.') . EOL);
+               @unlink($src);
+               $foo = 0;
+               call_hooks('photo_post_end',$foo);
                return;
        }
 
@@ -560,6 +635,8 @@ foreach($_FILES AS $key => $val) {
                logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
                notice( t('Unable to process image.') . EOL );
                @unlink($src);
+               $foo = 0;
+               call_hooks('photo_post_end',$foo);
                killme();
        }
 
@@ -625,6 +702,17 @@ foreach($_FILES AS $key => $val) {
 
        $item_id = item_store($arr);
 
+       if($item_id) {
+               q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
+                       dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
+                       intval($page_owner_uid),
+                       intval($item_id)
+               );
+       }
+       
+       if($visible) 
+               proc_run('php', "include/notifier.php", 'wall-new', $item_id);
+
        call_hooks('photo_post_end',intval($item_id));
 
        // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
@@ -647,6 +735,16 @@ function photos_content(&$a) {
        // photos/name/image/xxxxx/edit
 
 
+       if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+               notice( t('Public access denied.') . EOL);
+               return;
+       }
+
+
+       require_once('include/bbcode.php');
+       require_once('include/security.php');
+       require_once('include/conversation.php');
+
        if(! x($a->data,'user')) {
                notice( t('No photos selected') . EOL );
                return;
@@ -724,34 +822,28 @@ function photos_content(&$a) {
                }
        }
 
-       // default permissions - anonymous user
+       if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
+               notice( t('Access to this item is restricted.') . EOL);
+               return;
+       }
 
-       $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
+       $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
 
-       // Profile owner - everything is visible
+       $o = "";
 
-       if(local_user() && (local_user() == $owner_uid)) {
-               $sql_extra = '';        
-       }
-       elseif(remote_user()) {
-               // authenticated visitor - here lie dragons
-               $gs = '<<>>'; // should be impossible to match
-               if(count($groups)) {
-                       foreach($groups as $g)
-                               $gs .= '|<' . intval($g) . '>';
-               } 
-               $sql_extra = sprintf(
-                       " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
-                         AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
-                         AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
-                         AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
-
-                       intval(remote_user()),
-                       intval(remote_user()),
-                       dbesc($gs),
-                       dbesc($gs)
-               );
-       }
+       // tabs
+       $tpl = get_markup_template('profile_tabs.tpl');
+       $_is_owner = (local_user() && (local_user() == $owner_uid));
+       $o .= replace_macros($tpl,array(
+               '$url' => $a->get_baseurl() . '/profile/' .$a->data['user']['nickname'],
+               '$phototab' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
+               '$status' => t('Status'),
+               '$profile' => t('Profile'),
+               '$photos' => t('Photos'),
+               '$events' => (($_is_owner) ? t('Events') : ''),
+               '$notes' => (($_is_owner) ?     t('Personal Notes') : ''),
+               '$activetab' => "photos",
+       ));     
 
        //
        // dispatch request
@@ -768,7 +860,7 @@ function photos_content(&$a) {
                $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
                if(count($a->data['albums'])) {
                        foreach($a->data['albums'] as $album) {
-                               if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
+                               if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
                                        continue;
                                $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
                        }
@@ -793,16 +885,16 @@ function photos_content(&$a) {
 
  
 
-               $tpl = load_view_file('view/photos_upload.tpl');
+               $tpl = get_markup_template('photos_upload.tpl');
                $o .= replace_macros($tpl,array(
                        '$pagename' => t('Upload Photos'),
                        '$sessid' => session_id(),
                        '$nickname' => $a->data['user']['nickname'],
                        '$newalbum' => t('New album name: '),
                        '$existalbumtext' => t('or existing album name: '),
-                       '$albumselect' => $albumselect,
+                       '$albumselect' => template_escape($albumselect),
                        '$permissions' => t('Permissions'),
-                       '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
+                       '$aclselect' => (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb))),
                        '$uploader' => $ret['addon_text'],
                        '$default' => (($ret['default_upload']) ? $default_upload : ''),
                        '$uploadurl' => $ret['post_url']
@@ -817,7 +909,7 @@ function photos_content(&$a) {
                $album = hex2bin($datum);
 
                $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
-                       $sql_extra GROUP BY `resource-id`",
+                       AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
                        intval($owner_uid),
                        dbesc($album)
                );
@@ -827,7 +919,7 @@ function photos_content(&$a) {
                }
 
                $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
-                       $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
+                       AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                        intval($owner_uid),
                        dbesc($album),
                        intval($a->pager['start']),
@@ -837,13 +929,13 @@ function photos_content(&$a) {
                $o .= '<h3>' . $album . '</h3>';
                
                if($cmd === 'edit') {           
-                       if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
+                       if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
                                if($can_post) {
-                                       $edit_tpl = load_view_file('view/album_edit.tpl');
+                                       $edit_tpl = get_markup_template('album_edit.tpl');
                                        $o .= replace_macros($edit_tpl,array(
                                                '$nametext' => t('New album name: '),
                                                '$nickname' => $a->data['user']['nickname'],
-                                               '$album' => $album,
+                                               '$album' => template_escape($album),
                                                '$hexalbum' => bin2hex($album),
                                                '$submit' => t('Submit'),
                                                '$dropsubmit' => t('Delete Album')
@@ -852,7 +944,7 @@ function photos_content(&$a) {
                        }
                }
                else {
-                       if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
+                       if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
                                if($can_post) {
                                        $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
                                                . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
@@ -860,7 +952,7 @@ function photos_content(&$a) {
                                }
                        }
                }
-               $tpl = load_view_file('view/photo_album.tpl');
+               $tpl = get_markup_template('photo_album.tpl');
                if(count($r))
                        foreach($r as $rr) {
                                $o .= replace_macros($tpl,array(
@@ -868,8 +960,8 @@ function photos_content(&$a) {
                                        '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
                                        '$phototitle' => t('View Photo'),
                                        '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
-                                       '$imgalt' => $rr['filename'],
-                                       '$desc'=> $rr['desc']
+                                       '$imgalt' => template_escape($rr['filename']),
+                                       '$desc'=> template_escape($rr['desc'])
                                ));
 
                }
@@ -883,10 +975,9 @@ function photos_content(&$a) {
 
        if($datatype === 'image') {
 
-               require_once('security.php');
-               require_once('bbcode.php');
 
-               $o = '<div id="live-display"></div>' . "\r\n";
+
+               //$o = '';
                // fetch image, item containing image, then comments
 
                $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
@@ -896,10 +987,45 @@ function photos_content(&$a) {
                );
 
                if(! count($ph)) {
-                       notice( t('Photo not available') . EOL );
+                       $ph = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
+                               LIMIT 1",
+                               intval($owner_uid),
+                               dbesc($datum)
+                       );
+                       if(count($ph)) 
+                               notice( t('Permission denied. Access to this item may be restricted.'));
+                       else
+                               notice( t('Photo not available') . EOL );
                        return;
                }
 
+               $prevlink = '';
+               $nextlink = '';
+
+               $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 
+                       $sql_extra ORDER BY `created` DESC ",
+                       dbesc($ph[0]['album']),
+                       intval($owner_uid)
+               ); 
+
+               if(count($prvnxt)) {
+                       for($z = 0; $z < count($prvnxt); $z++) {
+                               if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
+                                       $prv = $z - 1;
+                                       $nxt = $z + 1;
+                                       if($prv < 0)
+                                               $prv = count($prvnxt) - 1;
+                                       if($nxt >= count($prvnxt))
+                                               $nxt = 0;
+                                       break;
+                               }
+                       }
+                       $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
+                       $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix;
+                       $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix;
+               }
+
+
                if(count($ph) == 1)
                        $hires = $lores = $ph[0];
                if(count($ph) > 1) {
@@ -913,27 +1039,36 @@ function photos_content(&$a) {
                        }
                }
 
-               
-               $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
+               $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
+               $tools = Null;
+               $lock = Null;
  
                if($can_post && ($ph[0]['uid'] == $owner_uid)) {
-                       $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>';
-                       
-                       $o .= ' - <a id="photo-toprofile-link" href="' . $a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'].'">'.t('Use as profile photo').'</a>';
+                       $tools = array(
+                               'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . (($cmd === 'edit') ? '' : '/edit'), (($cmd === 'edit') ? t('View photo') : t('Edit photo'))),
+                               'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
+                       );
+
                        // lock
-                       $o .= ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
+                       $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
                                        || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) ) 
-                                       ? ' - <img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,\'photo/' . $ph[0]['id'] . '\');" />'
-                                       : '');
+                                       ? t('Private Message')
+                                       : Null);
                        
-                       $o .= '</div>';
+                       
                }
 
+               if($prevlink)
+                       $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
+
+               $photo = array(
+                       'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
+                       'title'=> t('View Full Size'),
+                       'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
+               );
 
-               $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
-                       . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
-                       . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
-                       . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
+               if($nextlink)
+                       $nextlink = array($nextlink, '<div class="icon next"></div>');
 
 
                // Do we have an item for this photo?
@@ -985,37 +1120,40 @@ function photos_content(&$a) {
                        }
                }
 
-               $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
+               $tags=Null;
 
                if(count($linked_items) && strlen($link_item['tag'])) {
                        $arr = explode(',',$link_item['tag']);
-                       // parse tags and add links     
-                       $o .= '<div id="in-this-photo-text">' . t('Tags: ') . '</div>';
-                       $o .= '<div id="in-this-photo">';
+                       // parse tags and add links
                        $tag_str = '';
                        foreach($arr as $t) {
                                if(strlen($tag_str))
                                        $tag_str .= ', ';
                                $tag_str .= bbcode($t);
                        } 
-                       $o .= $tag_str . '</div>';
-                       if($cmd === 'edit')
-                               $o .= '<div id="tag-remove"><a href="' . $a->get_baseurl() . '/tagrm/' . $link_item['id'] . '">' . t('[Remove any tag]') . '</a></div>';
+                       $tags = array(t('Tags: '), $tag_str);
+                       if($cmd === 'edit') {
+                               $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
+                               $tags[] = t('[Remove any tag]');
+                       }
                }
 
 
+               $edit = Null;
                if(($cmd === 'edit') && ($can_post)) {
-                       $edit_tpl = load_view_file('view/photo_edit.tpl');
-                       $o .= replace_macros($edit_tpl, array(
+                       $edit_tpl = get_markup_template('photo_edit.tpl');
+                       $edit = replace_macros($edit_tpl, array(
                                '$id' => $ph[0]['id'],
-                               '$album' => $ph[0]['album'],
+                               '$album' => template_escape($ph[0]['album']),
                                '$newalbum' => t('New album name'), 
                                '$nickname' => $a->data['user']['nickname'],
                                '$resource_id' => $ph[0]['resource-id'],
                                '$capt_label' => t('Caption'),
-                               '$caption' => $ph[0]['desc'],
+                               '$caption' => template_escape($ph[0]['desc']),
                                '$tag_label' => t('Add a Tag'),
                                '$tags' => $link_item['tag'],
+                               '$permissions' => t('Permissions'),
+                               '$aclselect' => template_escape(populate_acl($ph[0])),
                                '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
                                '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
                                '$submit' => t('Submit'),
@@ -1025,31 +1163,29 @@ function photos_content(&$a) {
 
                if(count($linked_items)) {
 
-                       $cmnt_tpl = load_view_file('view/comment_item.tpl');
-                       $tpl = load_view_file('view/photo_item.tpl');
+                       $cmnt_tpl = get_markup_template('comment_item.tpl');
+                       $tpl = get_markup_template('photo_item.tpl');
                        $return_url = $a->cmd;
 
-                       $like_tpl = load_view_file('view/like.tpl');
+                       $like_tpl = get_markup_template('like_noshare.tpl');
 
                        $likebuttons = '';
 
                        if($can_post || can_write_wall($a,$owner_uid)) {
                                $likebuttons = replace_macros($like_tpl,array(
-                                       '$id' => $item['id'],
+                                       '$id' => $link_item['id'],
                                        '$likethis' => t("I like this \x28toggle\x29"),
                                        '$nolike' => t("I don't like this \x28toggle\x29"),
-                                       '$wait' => t('Please wait') 
+                                       '$share' => t('Share'),
+                                       '$wait' => t('Please wait')
                                ));
                        }
 
+                       $comments = '';
                        if(! count($r)) {
-                               $o .= '<div id="photo-like-div">';
-                               $o .= $likebuttons;
-                               $o .= '</div>';
-
                                if($can_post || can_write_wall($a,$owner_uid)) {
                                        if($link_item['last-child']) {
-                                               $o .= replace_macros($cmnt_tpl,array(
+                                               $comments .= replace_macros($cmnt_tpl,array(
                                                        '$return_path' => '', 
                                                        '$jsreload' => $return_url,
                                                        '$type' => 'wall-comment',
@@ -1059,6 +1195,8 @@ function photos_content(&$a) {
                                                        '$mylink' => $contact['url'],
                                                        '$mytitle' => t('This is you'),
                                                        '$myphoto' => $contact['thumb'],
+                                                       '$comment' => t('Comment'),
+                                                       '$submit' => t('Submit'),
                                                        '$ww' => ''
                                                ));
                                        }
@@ -1067,6 +1205,9 @@ function photos_content(&$a) {
 
                        $alike = array();
                        $dlike = array();
+                       
+                       $like = '';
+                       $dislike = '';
 
                        // display comments
                        if(count($r)) {
@@ -1079,17 +1220,11 @@ function photos_content(&$a) {
                                $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
                                $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
 
-                               $o .= '<div id="photo-like-div">';
-                               $o .= $likebuttons;
-                               $o .= $like;
-                               $o .= $dislike;
-                               $o .= '</div>';
-
 
 
                                if($can_post || can_write_wall($a,$owner_uid)) {
                                        if($link_item['last-child']) {
-                                               $o .= replace_macros($cmnt_tpl,array(
+                                               $comments .= replace_macros($cmnt_tpl,array(
                                                        '$return_path' => '',
                                                        '$jsreload' => $return_url,
                                                        '$type' => 'wall-comment',
@@ -1099,6 +1234,8 @@ function photos_content(&$a) {
                                                        '$mylink' => $contact['url'],
                                                        '$mytitle' => t('This is you'),
                                                        '$myphoto' => $contact['thumb'],
+                                                       '$comment' => t('Comment'),
+                                                       '$submit' => t('Submit'),
                                                        '$ww' => ''
                                                ));
                                        }
@@ -1118,7 +1255,7 @@ function photos_content(&$a) {
                                        if($can_post || can_write_wall($a,$owner_uid)) {
 
                                                if($item['last-child']) {
-                                                       $comment = replace_macros($cmnt_tpl,array(
+                                                       $comments .= replace_macros($cmnt_tpl,array(
                                                                '$return_path' => '',
                                                                '$jsreload' => $return_url,
                                                                '$type' => 'wall-comment',
@@ -1128,6 +1265,8 @@ function photos_content(&$a) {
                                                                '$mylink' => $contact['url'],
                                                                '$mytitle' => t('This is you'),
                                                                '$myphoto' => $contact['thumb'],
+                                                               '$comment' => t('Comment'),
+                                                               '$submit' => t('Submit'),
                                                                '$ww' => ''
                                                        ));
                                                }
@@ -1154,17 +1293,17 @@ function photos_content(&$a) {
                                        $drop = '';
 
                                        if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
-                                               $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
+                                               $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
 
 
-                                       $o .= replace_macros($template,array(
+                                       $comments .= replace_macros($template,array(
                                                '$id' => $item['item_id'],
                                                '$profile_url' => $profile_link,
-                                               '$name' => $profile_name,
+                                               '$name' => template_escape($profile_name),
                                                '$thumb' => $profile_avatar,
                                                '$sparkle' => $sparkle,
-                                               '$title' => $item['title'],
-                                               '$body' => bbcode($item['body']),
+                                               '$title' => template_escape($item['title']),
+                                               '$body' => template_escape(bbcode($item['body'])),
                                                '$ago' => relative_date($item['created']),
                                                '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
                                                '$drop' => $drop,
@@ -1173,17 +1312,38 @@ function photos_content(&$a) {
                                }
                        }
 
-                       $o .= paginate($a);
+                       $paginate = paginate($a);
                }
+               
+               $photo_tpl = get_markup_template('photo_view.tpl');
+               $o .= replace_macros($photo_tpl, array(
+                       '$id' => $ph[0]['id'],
+                       '$album' => array($album_link,template_escape($ph[0]['album'])),
+                       '$tools' => $tools,
+                       '$lock' => $lock,
+                       '$photo' => $photo,
+                       '$prevlink' => $prevlink,
+                       '$nextlink' => $nextlink,
+                       '$desc' => $ph[0]['desc'],
+                       '$tags' => template_escape($tags),
+                       '$edit' => $edit,       
+                       '$likebuttons' => $likebuttons,
+                       '$like' => template_escape($like),
+                       '$dislike' => template_escape($dislike),
+                       '$comments' => $comments,
+                       '$paginate' => $paginate,
+               ));
+               
                return $o;
        }
 
        // Default - show recent photos with upload link (if applicable)
-       $o = '';
+       //$o = '';
 
-       $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
+       $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' 
                $sql_extra GROUP BY `resource-id`",
                intval($a->data['user']['uid']),
+               dbesc('Contact Photos'),
                dbesc( t('Contact Photos'))
        );
        if(count($r)) {
@@ -1192,9 +1352,10 @@ function photos_content(&$a) {
        }
 
        $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
-               WHERE `uid` = %d AND `album` != '%s' 
+               WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'  
                $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                intval($a->data['user']['uid']),
+               dbesc('Contact Photos'),
                dbesc( t('Contact Photos')),
                intval($a->pager['start']),
                intval($a->pager['itemspage'])
@@ -1207,21 +1368,18 @@ function photos_content(&$a) {
                        . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
        }
 
-       $tpl = load_view_file('view/photo_top.tpl');
+       $tpl = get_markup_template('photo_top.tpl');
        if(count($r)) {
                foreach($r as $rr) {
                        $o .= replace_macros($tpl,array(
-                               '$id' => $rr['id'],
-                               '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
-                                       . '/image/' . $rr['resource-id'],
+                               '$id'         => $rr['id'],
+                               '$photolink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
                                '$phototitle' => t('View Photo'),
-                               '$imgsrc' => $a->get_baseurl() . '/photo/' 
-                                       . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
-                               '$albumlink' => $a->get_baseurl() . '/photos/' 
-                                       . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
-                               '$albumname' => $rr['album'],
-                               '$albumalt' => t('View Album'),
-                               '$imgalt' => $rr['filename']
+                               '$imgsrc'     => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
+                               '$albumlink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
+                               '$albumname'  => template_escape($rr['album']),
+                               '$albumalt'   => t('View Album'),
+                               '$imgalt'     => template_escape($rr['filename'])
                        ));
 
                }