]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
Enabled Profile photo revisions to be displayed on /photos page
[friendica.git] / mod / photos.php
index b74ca85d71b5b1272b277f92c872614a5bb0be7a..a6552994e5258c9aa1ef788ae98b8251e9fb3026 100644 (file)
@@ -5,6 +5,7 @@ require_once('include/acl_selectors.php');
 require_once('include/bbcode.php');
 require_once('include/security.php');
 
+
 function photos_init(&$a) {
 
 
@@ -15,7 +16,7 @@ function photos_init(&$a) {
 
        if($a->argc > 1) {
                $nick = $a->argv[1];
-               $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
+               $r = q("SELECT `user`.*, `contact`.`avatar-date` AS picdate FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid` WHERE `user`.`nickname` = '%s' AND `user`.`blocked` = 0 LIMIT 1",
                        dbesc($nick)
                );
 
@@ -33,20 +34,32 @@ function photos_init(&$a) {
                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 .= '<ul>';
-                       foreach($albums as $album) {
+                       $o .= '<div class="vcard">';
+                       $o .= '<div class="fn">' . $a->data['user']['username'] . '</div>';
+                       $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?rev=' . urlencode($a->data['user']['picdate']) . '" alt="' . $a->data['user']['username'] . '" /></div>';
+                       $o .= '</div>';
+                       
+                       if(! intval($a->data['user']['hidewall'])) {
+                               $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></h3>';
+                                       
+                               $o .= '<ul>';
+                               foreach($albums as $album) {
 
-                               // don't show contact photos. We once trasnlated 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).
+                                       // 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>'; 
+                                       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>'; 
+                               }
+                               $o .= '</ul>';
+                       }
+                       if(local_user() && $a->data['user']['uid'] == local_user()) {
+                               $o .= '<div id="photo-albums-upload-link"><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .t('Upload New Photos') . '</a></div>';
                        }
-                       $o .= '</ul>';
+
+                       $o .= '</div>';
                }
 
                if(! x($a->page,'aside'))
@@ -87,15 +100,13 @@ EOT;
 
 function photos_post(&$a) {
 
-       logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
+       logger('mod-photos: photos_post: begin' , 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');
-       }
+       logger('mod_photos: REQUEST ' . print_r($_REQUEST,true), LOGGER_DATA);
+       logger('mod_photos: FILES '   . print_r($_FILES,true), LOGGER_DATA);
+
+       $phototypes = Photo::supportedTypes();
 
        $can_post  = false;
        $visitor   = 0;
@@ -279,6 +290,7 @@ function photos_post(&$a) {
 
        if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
 
+
                $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))    : '');
                $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
                $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
@@ -292,13 +304,68 @@ function photos_post(&$a) {
 
                if(! strlen($albname))
                        $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
+
+
+               if((x($_POST,'rotate') !== false) && (intval($_POST['rotate']) == 1)) {
+                       logger('rotate');
+
+                       $r = q("select * from photo where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
+                               dbesc($resource_id),
+                               intval($page_owner_uid)
+                       );
+                       if(count($r)) {
+                               $ph = new Photo($r[0]['data'], $r[0]['type']);
+                               if($ph->is_valid()) {
+                                       $ph->rotate(270);
+
+                                       $width  = $ph->getWidth();
+                                       $height = $ph->getHeight();
+
+                                       $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
+                                               dbesc($ph->imageString()),
+                                               intval($height),
+                                               intval($width),
+                               dbesc($resource_id),
+                               intval($page_owner_uid)
+                                       );
+
+                                       if($width > 640 || $height > 640) {
+                                               $ph->scaleImage(640);
+                                               $width  = $ph->getWidth();
+                                               $height = $ph->getHeight();
                
+                                               $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 1 limit 1",
+                                                       dbesc($ph->imageString()),
+                                                       intval($height),
+                                                       intval($width),
+                               dbesc($resource_id),
+                               intval($page_owner_uid)
+                                               );
+                                       }
+
+                                       if($width > 320 || $height > 320) {
+                                               $ph->scaleImage(320);
+                                               $width  = $ph->getWidth();
+                                               $height = $ph->getHeight();
+
+                                               $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 2 limit 1",
+                                                       dbesc($ph->imageString()),
+                                                       intval($height),
+                                                       intval($width),
+                               dbesc($resource_id),
+                               intval($page_owner_uid)
+                                               );
+                                       }       
+                               }
+                       }
+               }
 
                $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
                        dbesc($resource_id),
                        intval($page_owner_uid)
                );
                if(count($p)) {
+                       $ext = $phototypes[$p[0]['type']];
                        $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),
@@ -322,9 +389,8 @@ function photos_post(&$a) {
                        // Create item container
 
                        $title = '';
-                       $basename = basename($filename);
                        $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
-
+                       
                        $arr = array();
 
                        $arr['uid']           = $page_owner_uid;
@@ -347,9 +413,10 @@ function photos_post(&$a) {
                        $arr['deny_gid']      = $p[0]['deny_gid'];
                        $arr['last-child']    = 1;
                        $arr['visible']       = $visibility;
+                       $arr['origin']        = 1;
                        
                        $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]' 
+                                               . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]' 
                                                . '[/url]';
                
                        $item_id = item_store($arr);
@@ -406,7 +473,18 @@ function photos_post(&$a) {
                                                }
                                                else {
                                                        $newname = $name;
-                                                       if(strstr($name,'_') || strstr($name,' ')) {
+                                                       $alias = '';
+                                                       $tagcid = 0;
+                                                       if(strrpos($newname,'+'))
+                                                               $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
+
+                                                       if($tagcid) {
+                                                               $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                                                                       intval($tagcid),
+                                                                       intval($profile_uid)
+                                                               );
+                                                       }
+                                                       elseif(strstr($name,'_') || strstr($name,' ')) {
                                                                $newname = str_replace('_',' ',$name);
                                                                $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
                                                                        dbesc($newname),
@@ -505,19 +583,19 @@ function photos_post(&$a) {
                                        $arr['target-type']   = ACTIVITY_OBJ_PHOTO;
                                        $arr['tag']           = $tagged[4];
                                        $arr['inform']        = $tagged[2];
-
+                                       $arr['origin']        = 1;
                                        $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]' ;
-                                       $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" ;
+                                       $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 . '.' . $ext . '[/img][/url]' . "\n" ;
 
                                        $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
                                        $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
                                        if($tagged[3])
-                                               $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
+                                               $arr['object'] .= xmlify('<link rel="photo" type="'.$p[0]['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n");
                                        $arr['object'] .= '</link></object>' . "\n";
 
                                        $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
                                                . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
-                                       $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>';
+                                       $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="'.$p[0]['type'].'" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
 
                                        $item_id = item_store($arr);
                                        if($item_id) {
@@ -552,7 +630,7 @@ function photos_post(&$a) {
        $album    = notags(trim($_REQUEST['album']));
        $newalbum = notags(trim($_REQUEST['newalbum']));
 
-       logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
+       logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG);
 
        if(! strlen($album)) {
                if(strlen($newalbum))
@@ -579,13 +657,16 @@ function photos_post(&$a) {
                $visible = 1;
        else
                $visible = 0;
+       
+       if(intval($_REQUEST['not_visible']) || $_REQUEST['not_visible'] === 'true')
+               $visible = 0;
 
        $str_group_allow   = perms2str(((is_array($_REQUEST['group_allow']))   ? $_REQUEST['group_allow']   : explode(',',$_REQUEST['group_allow'])));
        $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
        $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
        $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
 
-       $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
+       $ret = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
 
        call_hooks('photo_post_file',$ret);
 
@@ -593,15 +674,17 @@ function photos_post(&$a) {
                $src      = $ret['src'];
                $filename = $ret['filename'];
                $filesize = $ret['filesize'];
+               $type     = $ret['type'];
        }
        else {
                $src        = $_FILES['userfile']['tmp_name'];
                $filename   = basename($_FILES['userfile']['name']);
                $filesize   = intval($_FILES['userfile']['size']);
+               $type       = $_FILES['userfile']['type'];
        }
+       if ($type=="") $type=guess_image_type($filename);
 
-
-       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
+       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
 
        $maximagesize = get_config('system','maximagesize');
 
@@ -621,13 +704,13 @@ function photos_post(&$a) {
                return;
        }
 
-       logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
+       logger('mod/photos.php: photos_post(): loading the contents of ' . $src , LOGGER_DEBUG);
 
        $imagedata = @file_get_contents($src);
-       $ph = new Photo($imagedata);
+       $ph = new Photo($imagedata, $type);
 
        if(! $ph->is_valid()) {
-               logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
+               logger('mod/photos.php: photos_post(): unable to process image' , LOGGER_DEBUG);
                notice( t('Unable to process image.') . EOL );
                @unlink($src);
                $foo = 0;
@@ -647,7 +730,7 @@ function photos_post(&$a) {
        $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);
 
        if(! $r) {
-               logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
+               logger('mod/photos.php: photos_post(): image store failed' , LOGGER_DEBUG);
                notice( t('Image upload failed.') . EOL );
                killme();
        }
@@ -691,8 +774,10 @@ function photos_post(&$a) {
        $arr['deny_gid']      = $str_group_deny;
        $arr['last-child']    = 1;
        $arr['visible']       = $visible;
+       $arr['origin']        = 1;
+
        $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
-                               . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
+                               . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/img]' 
                                . '[/url]';
 
        $item_id = item_store($arr);
@@ -724,6 +809,7 @@ function photos_content(&$a) {
        // URLs:
        // photos/name
        // photos/name/upload
+       // photos/name/upload/xxxxx (xxxxx is album name)
        // photos/name/album/xxxxx
        // photos/name/album/xxxxx/edit
        // photos/name/image/xxxxx
@@ -734,8 +820,8 @@ function photos_content(&$a) {
                notice( t('Public access denied.') . EOL);
                return;
        }
-
-
+       
+       
        require_once('include/bbcode.php');
        require_once('include/security.php');
        require_once('include/conversation.php');
@@ -745,6 +831,8 @@ function photos_content(&$a) {
                return;
        }
 
+       $phototypes = Photo::supportedTypes();
+
        $_SESSION['photo_return'] = $a->cmd;
 
        //
@@ -827,18 +915,8 @@ function photos_content(&$a) {
        $o = "";
 
        // 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",
-       ));     
+       $o .= profile_tabs($a,$_is_owner, $a->data['user']['nickname']);        
 
        //
        // dispatch request
@@ -850,14 +928,21 @@ function photos_content(&$a) {
                        notice( t('Permission denied.'));
                        return;
                }
+
+
+               $selname = (($datum) ? hex2bin($datum) : '');
+
+
                $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
 
-               $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
+               
+               $albumselect .= '<option value="" ' . ((! $selname) ? ' 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'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
                                        continue;
-                               $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
+                               $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
+                               $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
                        }
                }
 
@@ -887,9 +972,10 @@ function photos_content(&$a) {
                        '$nickname' => $a->data['user']['nickname'],
                        '$newalbum' => t('New album name: '),
                        '$existalbumtext' => t('or existing album name: '),
-                       '$albumselect' => $albumselect,
+                       '$nosharetext' => t('Do not show a status post for this upload'),
+                       '$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']
@@ -913,7 +999,7 @@ function photos_content(&$a) {
                        $a->set_pager_itemspage(20);
                }
 
-               $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
+               $r = q("SELECT `resource-id`, `id`, `filename`, type, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
                        AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                        intval($owner_uid),
                        dbesc($album),
@@ -930,7 +1016,7 @@ function photos_content(&$a) {
                                        $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')
@@ -947,16 +1033,30 @@ function photos_content(&$a) {
                                }
                        }
                }
+
+               if($can_post) {
+                       $o .= '<div class="photos-upload-link" ><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album) . '" >' . t('Upload New Photos') . '</a></div>';
+               }
+
                $tpl = get_markup_template('photo_album.tpl');
                if(count($r))
+                       $twist = 'rotright';
                        foreach($r as $rr) {
+                               if($twist == 'rotright')
+                                       $twist = 'rotleft';
+                               else
+                                       $twist = 'rotright';
+                               
+                               $ext = $phototypes[$rr['type']];
+
                                $o .= replace_macros($tpl,array(
                                        '$id' => $rr['id'],
+                                       '$twist' => ' ' . $twist . rand(2,4),
                                        '$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']
+                                       '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.' .$ext,
+                                       '$imgalt' => template_escape($rr['filename']),
+                                       '$desc'=> template_escape($rr['desc'])
                                ));
 
                }
@@ -982,7 +1082,15 @@ 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;
                }
 
@@ -1007,8 +1115,9 @@ function photos_content(&$a) {
                                        break;
                                }
                        }
-                       $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] ;
-                       $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] ;
+                       $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;
                }
 
 
@@ -1031,7 +1140,7 @@ function photos_content(&$a) {
  
                if($can_post && ($ph[0]['uid'] == $owner_uid)) {
                        $tools = array(
-                               'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit', t('Edit photo')),
+                               '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')),
                        );
 
@@ -1044,13 +1153,24 @@ function photos_content(&$a) {
                        
                }
 
+               if(! $cmd !== 'edit') {
+                       $a->page['htmlhead'] .= '<script>
+                               $(document).keydown(function(event) {' . "\n";
+
+                       if($prevlink)
+                               $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 37) { event.preventDefault(); window.location.href = \'' . $prevlink . '\'; }' . "\n";
+                       if($nextlink)
+                               $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 39) { event.preventDefault(); window.location.href = \'' . $nextlink . '\'; }' . "\n";
+                       $a->page['htmlhead'] .= '});</script>';
+               }
+
                if($prevlink)
                        $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
 
                $photo = array(
-                       'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
+                       'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.' . $phototypes[$hires['type']],
                        'title'=> t('View Full Size'),
-                       'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
+                       'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.' . $phototypes[$lores['type']] . '?f=&_u=' . datetime_convert('','','','ymdhis')
                );
 
                if($nextlink)
@@ -1066,7 +1186,7 @@ function photos_content(&$a) {
                        $link_item = $linked_items[0];
                        $r = q("SELECT COUNT(*) AS `total`
                                FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                               WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
+                               WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
                                AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                                AND `item`.`uid` = %d 
                                $sql_extra ",
@@ -1085,7 +1205,7 @@ function photos_content(&$a) {
                                `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
                                `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
                                FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
-                               WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
+                               WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
                                AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                                AND `item`.`uid` = %d
                                $sql_extra
@@ -1130,16 +1250,17 @@ function photos_content(&$a) {
                        $edit_tpl = get_markup_template('photo_edit.tpl');
                        $edit = replace_macros($edit_tpl, array(
                                '$id' => $ph[0]['id'],
-                               '$album' => $ph[0]['album'],
+                               '$rotate' => t('Rotate CW'),
+                               '$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' => populate_acl($ph[0]),
+                               '$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'),
@@ -1183,6 +1304,7 @@ function photos_content(&$a) {
                                                        '$myphoto' => $contact['thumb'],
                                                        '$comment' => t('Comment'),
                                                        '$submit' => t('Submit'),
+                                                       '$preview' => t('Preview'),
                                                        '$ww' => ''
                                                ));
                                        }
@@ -1285,11 +1407,11 @@ function photos_content(&$a) {
                                        $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,
@@ -1304,18 +1426,18 @@ function photos_content(&$a) {
                $photo_tpl = get_markup_template('photo_view.tpl');
                $o .= replace_macros($photo_tpl, array(
                        '$id' => $ph[0]['id'],
-                       '$album' => array($album_link,$ph[0]['album']),
+                       '$album' => array($album_link,template_escape($ph[0]['album'])),
                        '$tools' => $tools,
                        '$lock' => $lock,
                        '$photo' => $photo,
                        '$prevlink' => $prevlink,
                        '$nextlink' => $nextlink,
                        '$desc' => $ph[0]['desc'],
-                       '$tags' => $tags,
+                       '$tags' => template_escape($tags),
                        '$edit' => $edit,       
                        '$likebuttons' => $likebuttons,
-                       '$like' => $like,
-                       '$dislike' => $dislike,
+                       '$like' => template_escape($like),
+                       '$dislike' => template_escape($dislike),
                        '$comments' => $comments,
                        '$paginate' => $paginate,
                ));
@@ -1337,7 +1459,7 @@ function photos_content(&$a) {
                $a->set_pager_itemspage(20);
        }
 
-       $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
+       $r = q("SELECT `resource-id`, `id`, `filename`, type, `album`, max(`scale`) AS `scale` FROM `photo`
                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']),
@@ -1347,30 +1469,44 @@ function photos_content(&$a) {
                intval($a->pager['itemspage'])
        );
 
-       $o .= '<h3>' . t('Recent Photos') . '</h3>';
 
-       if($can_post) {
-               $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
-                       . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
-       }
 
-       $tpl = get_markup_template('photo_top.tpl');
+       $photos = array();
        if(count($r)) {
+               $twist = 'rotright';
                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'],
-                               '$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']
-                       ));
-
+                       if($twist == 'rotright')
+                               $twist = 'rotleft';
+                       else
+                               $twist = 'rotright';
+                       $ext = $phototypes[$rr['type']];
+                       
+                       $photos[] = array(
+                               'id'       => $rr['id'],
+                               'twist'    => ' ' . $twist . rand(2,4),
+                               'link'          => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
+                               'title'         => t('View Photo'),
+                               'src'           => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
+                               'alt'           => template_escape($rr['filename']),
+                               'album' => array(
+                                       'link'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
+                                       'name'  => template_escape($rr['album']),
+                                       'alt'   => t('View Album'),
+                               ),
+                               
+                       );
                }
-               $o .= '<div id="photo-top-end"></div>';
        }
+       
+       $tpl = get_markup_template('photos_recent.tpl'); 
+       $o .= replace_macros($tpl,array(
+               '$title' => t('Recent Photos'),
+               '$can_post' => $can_post,
+               '$upload' => array(t('Upload New Photos'), $a->get_baseurl().'/photos/'.$a->data['user']['nickname'].'/upload'),
+               '$photos' => $photos,
+       ));
+
+       
        $o .= paginate($a);
        return $o;
 }