]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
Bump DB version
[friendica.git] / mod / photos.php
index a4bf19e9400e78a9f3ccd21aefcec02dd35cdb2d..3acd39b2af6311f144a1af9fc86a44b13cdaa556 100644 (file)
@@ -1,21 +1,24 @@
 <?php
-require_once('include/Photo.php');
-require_once('include/photos.php');
-require_once('include/items.php');
-require_once('include/acl_selectors.php');
-require_once('include/bbcode.php');
-require_once('include/security.php');
-require_once('include/redir.php');
-require_once('include/tags.php');
-require_once('include/threads.php');
-require_once('include/Probe.php');
-
-function photos_init(&$a) {
-
-       if ($a->argc > 1)
+require_once 'include/Photo.php';
+require_once 'include/photos.php';
+require_once 'include/items.php';
+require_once 'include/acl_selectors.php';
+require_once 'include/bbcode.php';
+require_once 'include/security.php';
+require_once 'include/redir.php';
+require_once 'include/tags.php';
+require_once 'include/threads.php';
+require_once 'include/Probe.php';
+
+use \Friendica\Core\Config;
+
+function photos_init(App $a) {
+
+       if ($a->argc > 1) {
                auto_redir($a, $a->argv[1]);
+       }
 
-       if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+       if ((get_config('system', 'block_public')) && (! local_user()) && (! remote_user())) {
                return;
        }
 
@@ -27,8 +30,9 @@ function photos_init(&$a) {
                        dbesc($nick)
                );
 
-               if (! count($user))
+               if (! dbm::is_result($user)) {
                        return;
+               }
 
                $a->data['user'] = $user[0];
                $a->profile_uid = $user[0]['uid'];
@@ -57,8 +61,9 @@ function photos_init(&$a) {
 
                if ($albums) {
                        $a->data['albums'] = $albums;
-                       if ($albums_visible)
+                       if ($albums_visible) {
                                $ret['success'] = true;
+                       }
 
                        $ret['albums'] = array();
                        foreach ($albums as $k => $album) {
@@ -78,24 +83,26 @@ function photos_init(&$a) {
 
                $albums = $ret;
 
-               if (local_user() && $a->data['user']['uid'] == local_user())
+               if (local_user() && $a->data['user']['uid'] == local_user()) {
                        $can_post = true;
+               }
 
                if ($albums['success']) {
-                       $photo_albums_widget = replace_macros(get_markup_template('photo_albums.tpl'),array(
+                       $photo_albums_widget = replace_macros(get_markup_template('photo_albums.tpl'), array(
                                '$nick'     => $a->data['user']['nickname'],
                                '$title'    => t('Photo Albums'),
-                               '$recent'    => t('Recent Photos'),
+                               '$recent'   => t('Recent Photos'),
                                '$albums'   => $albums['albums'],
                                '$baseurl'  => z_root(),
-                               '$upload'   => array( t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload'),
+                               '$upload'   => array(t('Upload New Photos'), 'photos/' . $a->data['user']['nickname'] . '/upload'),
                                '$can_post' => $can_post
                        ));
                }
 
 
-               if (! x($a->page,'aside'))
+               if (! x($a->page, 'aside')) {
                        $a->page['aside'] = '';
+               }
                $a->page['aside'] .= $vcard_widget;
                $a->page['aside'] .= $photo_albums_widget;
 
@@ -112,7 +119,7 @@ function photos_init(&$a) {
 
 
 
-function photos_post(&$a) {
+function photos_post(App $a) {
 
        logger('mod-photos: photos_post: begin' , LOGGER_DEBUG);
 
@@ -128,9 +135,9 @@ function photos_post(&$a) {
        $page_owner_uid = $a->data['user']['uid'];
        $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
 
-       if ((local_user()) && (local_user() == $page_owner_uid))
+       if ((local_user()) && (local_user() == $page_owner_uid)) {
                $can_post = true;
-       else {
+       else {
                if ($community_page && remote_user()) {
                        $contact_id = 0;
                        if (is_array($_SESSION['remote'])) {
@@ -182,11 +189,11 @@ function photos_post(&$a) {
                        return; // NOTREACHED
                }
 
-               $r = qu("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
+               $r = qu("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
                        dbesc($album),
                        intval($page_owner_uid)
                );
-               if (! dbm::is_result($r)) {
+               if (!dbm::is_result($r)) {
                        notice( t('Album not found.') . EOL);
                        goaway($_SESSION['photo_return']);
                        return; // NOTREACHED
@@ -208,6 +215,9 @@ function photos_post(&$a) {
                                dbesc($album),
                                intval($page_owner_uid)
                        );
+                       // Update the photo albums cache
+                       photo_albums($page_owner_uid, true);
+
                        $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
                        goaway($newurl);
                        return; // NOTREACHED
@@ -290,11 +300,16 @@ function photos_post(&$a) {
 
                                        // send the notification upstream/downstream as the case may be
 
-                                       if ($rr['visible'])
+                                       if ($rr['visible']) {
                                                proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
+                                       }
                                }
                        }
+
+                       // Update the photo albums cache
+                       photo_albums($page_owner_uid, true);
                }
+
                goaway('photos/' . $a->data['user']['nickname']);
                return; // NOTREACHED
        }
@@ -346,7 +361,7 @@ function photos_post(&$a) {
                                dbesc($r[0]['resource-id']),
                                intval($page_owner_uid)
                        );
-                       if (count($i)) {
+                       if (dbm::is_result($i)) {
                                q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
                                        dbesc(datetime_convert()),
                                        dbesc(datetime_convert()),
@@ -359,8 +374,12 @@ function photos_post(&$a) {
                                $url = App::get_baseurl();
                                $drop_id = intval($i[0]['id']);
 
-                               if ($i[0]['visible'])
+                               // Update the photo albums cache
+                               photo_albums($page_owner_uid, true);
+
+                               if ($i[0]['visible']) {
                                        proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
+                               }
                        }
                }
 
@@ -370,10 +389,11 @@ 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);
-               $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
+               $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);
+               $albname     = ((x($_POST,'albname'))   ? notags(trim($_POST['albname'])) : '');
+               $origaname   = ((x($_POST,'origaname')) ? notags(trim($_POST['origaname'])) : '');
                $str_group_allow   = perms2str($_POST['group_allow']);
                $str_contact_allow = perms2str($_POST['contact_allow']);
                $str_group_deny    = perms2str($_POST['group_deny']);
@@ -381,15 +401,16 @@ function photos_post(&$a) {
 
                $resource_id = $a->argv[2];
 
-               if (! strlen($albname))
+               if (! strlen($albname)) {
                        $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
+               }
 
 
                if ((x($_POST,'rotate') !== false) &&
                   ( (intval($_POST['rotate']) == 1) || (intval($_POST['rotate']) == 2) )) {
                        logger('rotate');
 
-                       $r = q("select * from photo where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
+                       $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0 LIMIT 1",
                                dbesc($resource_id),
                                intval($page_owner_uid)
                        );
@@ -402,7 +423,7 @@ function photos_post(&$a) {
                                        $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",
+                                       $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 0",
                                                dbesc($ph->imageString()),
                                                intval($height),
                                                intval($width),
@@ -415,7 +436,7 @@ function photos_post(&$a) {
                                                $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",
+                                               $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 1",
                                                        dbesc($ph->imageString()),
                                                        intval($height),
                                                        intval($width),
@@ -429,7 +450,7 @@ function photos_post(&$a) {
                                                $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",
+                                               $x = q("UPDATE `photo` SET `data` = '%s', `height` = %d, `width` = %d WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = 2",
                                                        dbesc($ph->imageString()),
                                                        intval($height),
                                                        intval($width),
@@ -445,7 +466,7 @@ function photos_post(&$a) {
                        dbesc($resource_id),
                        intval($page_owner_uid)
                );
-               if (count($p)) {
+               if (dbm::is_result($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),
@@ -457,13 +478,19 @@ function photos_post(&$a) {
                                dbesc($resource_id),
                                intval($page_owner_uid)
                        );
+
+                       // Update the photo albums cache if album name was changed
+                       if ($albname !== $origaname) {
+                               photo_albums($page_owner_uid, true);
+                       }
                }
 
                /* Don't make the item visible if the only change was the album name */
 
                $visibility = 0;
-               if ($p[0]['desc'] !== $desc || strlen($rawtags))
+               if ($p[0]['desc'] !== $desc || strlen($rawtags)) {
                        $visibility = 1;
+               }
 
                if (! $item_id) {
 
@@ -523,40 +550,45 @@ function photos_post(&$a) {
                        // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
 
                        $x = substr($rawtags,0,1);
-                       if ($x !== '@' && $x !== '#')
+                       if ($x !== '@' && $x !== '#') {
                                $rawtags = '#' . $rawtags;
+                       }
 
                        $taginfo = array();
                        $tags = get_tags($rawtags);
 
                        if (count($tags)) {
                                foreach ($tags as $tag) {
-                                       if (isset($profile))
+                                       if (isset($profile)) {
                                                unset($profile);
-                                       if (strpos($tag,'@') === 0) {
+                                       }
+                                       if (strpos($tag, '@') === 0) {
                                                $name = substr($tag,1);
-                                               if ((strpos($name,'@')) || (strpos($name,'http://'))) {
+                                               if ((strpos($name, '@')) || (strpos($name, 'http://'))) {
                                                        $newname = $name;
                                                        $links = @Probe::lrdd($name);
                                                        if (count($links)) {
                                                                foreach ($links as $link) {
-                                                                       if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
+                                                                       if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
                                                                                $profile = $link['@attributes']['href'];
+                                                                       }
                                                                        if ($link['@attributes']['rel'] === 'salmon') {
-                                                                               $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
-                                                                               if (strlen($inform))
+                                                                               $salmon = '$url:' . str_replace(',', '%sc', $link['@attributes']['href']);
+                                                                               if (strlen($inform)) {
                                                                                        $inform .= ',';
-                                                       $inform .= $salmon;
+                                                                               }
+                                                                               $inform .= $salmon;
                                                                        }
                                                                }
                                                        }
-                                                       $taginfo[] = array($newname,$profile,$salmon);
+                                                       $taginfo[] = array($newname, $profile, $salmon);
                                                } else {
                                                        $newname = $name;
                                                        $alias = '';
                                                        $tagcid = 0;
-                                                       if (strrpos($newname,'+'))
-                                                               $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
+                                                       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",
@@ -572,7 +604,7 @@ function photos_post(&$a) {
                                                                                intval($page_owner_uid)
                                                                );
 
-                                                               if (! $r) {
+                                                               if (! dbm::is_result($r)) {
                                                                        //select someone by attag or nick and the name passed in
                                                                        $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
                                                                                        dbesc($name),
@@ -581,53 +613,46 @@ function photos_post(&$a) {
                                                                        );
                                                                }
                                                        }
-/*                                                     elseif (strstr($name,'_') || strstr($name,' ')) {
-                                                               $newname = str_replace('_',' ',$name);
-                                                               $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
-                                                                       dbesc($newname),
-                                                                       intval($page_owner_uid)
-                                                               );
-                                                       } else {
-                                                               $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)
-                                                               );
-                                                       }*/
+
                                                        if (dbm::is_result($r)) {
                                                                $newname = $r[0]['name'];
                                                                $profile = $r[0]['url'];
                                                                $notify = 'cid:' . $r[0]['id'];
-                                                               if (strlen($inform))
+                                                               if (strlen($inform)) {
                                                                        $inform .= ',';
+                                                               }
                                                                $inform .= $notify;
                                                        }
                                                }
                                                if ($profile) {
-                                                       if (substr($notify,0,4) === 'cid:')
-                                                               $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname  . '[/url]');
-                                                       else
-                                                               $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname     . '[/url]');
-                                                       if (strlen($str_tags))
+                                                       if (substr($notify, 0, 4) === 'cid:') {
+                                                               $taginfo[] = array($newname, $profile, $notify, $r[0], '@[url=' . str_replace(',','%2c',$profile) . ']' . $newname . '[/url]');
+                                                       } else {
+                                                               $taginfo[] = array($newname, $profile, $notify, null, $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]');
+                                                       }
+                                                       if (strlen($str_tags)) {
                                                                $str_tags .= ',';
-                                                       $profile = str_replace(',','%2c',$profile);
+                                                       }
+                                                       $profile = str_replace(',', '%2c', $profile);
                                                        $str_tags .= '@[url='.$profile.']'.$newname.'[/url]';
                                                }
-                                       } elseif (strpos($tag,'#') === 0) {
+                                       } elseif (strpos($tag, '#') === 0) {
                                                $tagname = substr($tag, 1);
-                                               $str_tags .= '#[url='.App::get_baseurl()."/search?tag=".$tagname.']'.$tagname.'[/url]';
+                                               $str_tags .= '#[url=' . App::get_baseurl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url]';
                                        }
                                }
                        }
 
                        $newtag = $old_tag;
-                       if (strlen($newtag) && strlen($str_tags))
+                       if (strlen($newtag) && strlen($str_tags)) {
                                $newtag .= ',';
+                       }
                        $newtag .= $str_tags;
 
                        $newinform = $old_inform;
-                       if (strlen($newinform) && strlen($inform))
+                       if (strlen($newinform) && strlen($inform)) {
                                $newinform .= ',';
+                       }
                        $newinform .= $inform;
 
                        $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d",
@@ -690,8 +715,9 @@ function photos_post(&$a) {
 
                                        $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])
+                                       if ($tagged[3]) {
                                                $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_IMAGE . '</type><title>' . $p[0]['desc'] . '</title><id>'
@@ -703,9 +729,7 @@ function photos_post(&$a) {
                                                proc_run(PRIORITY_HIGH, "include/notifier.php", "tag", $item_id);
                                        }
                                }
-
                        }
-
                }
                goaway($_SESSION['photo_return']);
                return; // NOTREACHED
@@ -728,38 +752,39 @@ function photos_post(&$a) {
        logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG);
 
        if (! strlen($album)) {
-               if (strlen($newalbum))
+               if (strlen($newalbum)) {
                        $album = $newalbum;
-               else
+               } else {
                        $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
+               }
        }
 
-       /**
-        *
+       /*
         * 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 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 AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
                dbesc($album),
                intval($page_owner_uid)
        );
-       if ((! dbm::is_result($r)) || ($album == t('Profile Photos')))
+       if ((! dbm::is_result($r)) || ($album == t('Profile Photos'))) {
                $visible = 1;
-       else
+       } else {
                $visible = 0;
+       }
 
-       if (intval($_REQUEST['not_visible']) || $_REQUEST['not_visible'] === 'true')
+       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'])));
+       $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, 'type' => '');
 
@@ -776,7 +801,9 @@ function photos_post(&$a) {
                $filesize   = intval($_FILES['userfile']['size']);
                $type       = $_FILES['userfile']['type'];
        }
-       if ($type=="") $type=guess_image_type($filename);
+       if ($type == "") {
+               $type = guess_image_type($filename);
+       }
 
        logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
 
@@ -803,22 +830,23 @@ function photos_post(&$a) {
        $imagedata = @file_get_contents($src);
 
 
+       $limit = service_class_fetch($a->data['user']['uid'], 'photo_upload_limit');
 
-       $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
-               intval($a->data['user']['uid'])
-       );
-
-       $limit = service_class_fetch($a->data['user']['uid'],'photo_upload_limit');
-
-       if (($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
-               notice( upgrade_message() . EOL );
-               @unlink($src);
-               $foo = 0;
-               call_hooks('photo_post_end',$foo);
-               killme();
+       if ($limit) {
+               $r = q("SELECT SUM(OCTET_LENGTH(`data`)) AS `total` FROM `photo` WHERE `uid` = %d AND `scale` = 0 AND `album` != 'Contact Photos'",
+                       intval($a->data['user']['uid'])
+               );
+               $size = $r[0]['total'];
+
+               if (($size + strlen($imagedata)) > $limit) {
+                       notice( upgrade_message() . EOL );
+                       @unlink($src);
+                       $foo = 0;
+                       call_hooks('photo_post_end',$foo);
+                       killme();
+               }
        }
 
-
        $ph = new Photo($imagedata, $type);
 
        if (! $ph->is_valid()) {
@@ -833,11 +861,13 @@ function photos_post(&$a) {
        $exif = $ph->orient($src);
        @unlink($src);
 
-       $max_length = get_config('system','max_image_length');
-       if (! $max_length)
+       $max_length = get_config('system', 'max_image_length');
+       if (! $max_length) {
                $max_length = MAX_IMAGE_LENGTH;
-       if ($max_length > 0)
+       }
+       if ($max_length > 0) {
                $ph->scaleImage($max_length);
+       }
 
        $width  = $ph->getWidth();
        $height = $ph->getHeight();
@@ -873,6 +903,7 @@ function photos_post(&$a) {
 
        $lat = $lon = null;
 
+       /// @TODO merge these 2 if() into one?
        if ($exif && $exif['GPS']) {
                if (feature_enabled($channel_id,'photo_location')) {
                        $lat = getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
@@ -882,8 +913,9 @@ function photos_post(&$a) {
 
        $arr = array();
 
-       if ($lat && $lon)
+       if ($lat && $lon) {
                $arr['coord'] = $lat . ' ' . $lon;
+       }
 
        $arr['guid']          = get_guid(32);
        $arr['uid']           = $page_owner_uid;
@@ -913,22 +945,25 @@ function photos_post(&$a) {
                                . '[/url]';
 
        $item_id = item_store($arr);
+       // Update the photo albums cache
+       photo_albums($page_owner_uid, true);
 
-       if ($visible)
+       if ($visible) {
                proc_run(PRIORITY_HIGH, "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
-       // if they do not wish to be redirected
+       /*
+        * addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
+        * if they do not wish to be redirected
+        */
 
        goaway($_SESSION['photo_return']);
        // NOTREACHED
 }
 
-
-
-function photos_content(&$a) {
+function photos_content(App $a) {
 
        // URLs:
        // photos/name
@@ -940,12 +975,11 @@ function photos_content(&$a) {
        // photos/name/image/xxxxx/edit
 
 
-       if ((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
+       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');
@@ -966,15 +1000,17 @@ function photos_content(&$a) {
        if ($a->argc > 3) {
                $datatype = $a->argv[2];
                $datum = $a->argv[3];
-       } elseif (($a->argc > 2) && ($a->argv[2] === 'upload'))
+       } elseif (($a->argc > 2) && ($a->argv[2] === 'upload')) {
                $datatype = 'upload';
-       else
+       } else {
                $datatype = 'summary';
+       }
 
-       if ($a->argc > 4)
+       if ($a->argc > 4) {
                $cmd = $a->argv[4];
-       else
+       } else {
                $cmd = 'view';
+       }
 
        //
        // Setup permissions structures
@@ -990,9 +1026,9 @@ function photos_content(&$a) {
 
        $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
 
-       if ((local_user()) && (local_user() == $owner_uid))
+       if ((local_user()) && (local_user() == $owner_uid)) {
                $can_post = true;
-       else {
+       else {
                if ($community_page && remote_user()) {
                        if (is_array($_SESSION['remote'])) {
                                foreach ($_SESSION['remote'] as $v) {
@@ -1043,6 +1079,7 @@ function photos_content(&$a) {
                }
        }
 
+       /// @TODO merge these 2 if() into one?
        if (! $remote_contact) {
                if (local_user()) {
                        $contact_id = $_SESSION['cid'];
@@ -1061,7 +1098,7 @@ function photos_content(&$a) {
 
        // tabs
        $is_owner = (local_user() && (local_user() == $owner_uid));
-       $o .= profile_tabs($a,$is_owner, $a->data['user']['nickname']);
+       $o .= profile_tabs($a, $is_owner, $a->data['user']['nickname']);
 
        /**
         * Display upload form
@@ -1069,7 +1106,7 @@ function photos_content(&$a) {
 
        if ($datatype === 'upload') {
                if (! ($can_post)) {
-                       notice( t('Permission denied.'));
+                       notice(t('Permission denied.'));
                        return;
                }
 
@@ -1083,8 +1120,9 @@ function photos_content(&$a) {
                $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')))
+                               if (($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos'))) {
                                        continue;
+                               }
                                $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
                                $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
                        }
@@ -1096,7 +1134,6 @@ function photos_content(&$a) {
                                'addon_text' => $uploader,
                                'default_upload' => true);
 
-
                call_hooks('photo_upload_form',$ret);
 
                $default_upload_box = replace_macros(get_markup_template('photos_default_uploader_box.tpl'), array());
@@ -1105,34 +1142,37 @@ function photos_content(&$a) {
                ));
 
                $usage_message = '';
-               $limit = service_class_fetch($a->data['user']['uid'],'photo_upload_limit');
+               $limit = service_class_fetch($a->data['user']['uid'], 'photo_upload_limit');
                if ($limit !== false) {
 
-                       $r = q("select sum(datasize) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
+                       $r = q("SELECT SUM(`datasize`) AS `total` FROM `photo` WHERE `uid` = %d AND `scale` = 0 AND `album` != 'Contact Photos'",
                                intval($a->data['user']['uid'])
                        );
-                       $usage_message = sprintf( t("You have used %1$.2f Mbytes of %2$.2f Mbytes photo storage."), $r[0]['total'] / 1024000, $limit / 1024000 );
+                       $usage_message = sprintf(t("You have used %1$.2f Mbytes of %2$.2f Mbytes photo storage."), $r[0]['total'] / 1024000, $limit / 1024000 );
                }
 
 
                // Private/public post links for the non-JS ACL form
                $private_post = 1;
-               if ($_REQUEST['public'])
+               if ($_REQUEST['public']) {
                        $private_post = 0;
+               }
 
                $query_str = $a->query_string;
-               if (strpos($query_str, 'public=1') !== false)
+               if (strpos($query_str, 'public=1') !== false) {
                        $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
+               }
 
-               // I think $a->query_string may never have ? in it, but I could be wrong
-               // It looks like it's from the index.php?q=[etc] rewrite that the web
-               // server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
-               if (strpos($query_str, '?') === false)
+               /*
+                * I think $a->query_string may never have ? in it, but I could be wrong
+                * It looks like it's from the index.php?q=[etc] rewrite that the web
+                * server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
+                */
+               if (strpos($query_str, '?') === false) {
                        $public_post_link = '?public=1';
-               else
+               } else {
                        $public_post_link = '&public=1';
-
-
+               }
 
                $tpl = get_markup_template('photos_upload.tpl');
 
@@ -1193,10 +1233,12 @@ function photos_content(&$a) {
                        $a->set_pager_itemspage(20);
                }
 
-               if ($_GET['order'] === 'posted')
+               /// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it?
+               if ($_GET['order'] === 'posted') {
                        $order = 'ASC';
-               else
+               } else {
                        $order = 'DESC';
+               }
 
                $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` $order LIMIT %d , %d",
@@ -1206,7 +1248,7 @@ function photos_content(&$a) {
                        intval($a->pager['itemspage'])
                );
 
-               //edit album name
+               // edit album name
                if ($cmd === 'edit') {
                        if (($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
                                if ($can_post) {
@@ -1229,27 +1271,27 @@ function photos_content(&$a) {
                                }
                        }
                } else {
-                       if (($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
-                               if ($can_post) {
-                                       $edit = array(t('Edit Album'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit');
-                               }
+                       if (($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos')) && $can_post) {
+                               $edit = array(t('Edit Album'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit');
                        }
                }
 
-               if ($_GET['order'] === 'posted')
+               if ($_GET['order'] === 'posted') {
                        $order =  array(t('Show Newest First'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album));
-               else
+               } else {
                        $order = array(t('Show Oldest First'), 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '?f=&order=posted');
+               }
 
                $photos = array();
 
                if (dbm::is_result($r))
                        $twist = 'rotright';
                        foreach ($r as $rr) {
-                               if ($twist == 'rotright')
+                               if ($twist == 'rotright') {
                                        $twist = 'rotleft';
-                               else
+                               } else {
                                        $twist = 'rotright';
+                               }
 
                                $ext = $phototypes[$rr['type']];
 
@@ -1290,10 +1332,9 @@ function photos_content(&$a) {
 
        }
 
-       /*
+       /*
         * Display one photo
         */
-
        if ($datatype === 'image') {
 
                //$o = '';
@@ -1305,51 +1346,57 @@ function photos_content(&$a) {
                        dbesc($datum)
                );
 
-               if (! count($ph)) {
+               if (! dbm::is_result($ph)) {
                        $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 );
+                       if (dbm::is_result($ph)) {
+                               notice(t('Permission denied. Access to this item may be restricted.'));
+                       } else {
+                               notice(t('Photo not available') . EOL );
+                       }
                        return;
                }
 
                $prevlink = '';
                $nextlink = '';
 
-               if ($_GET['order'] === 'posted')
-                       $order = 'ASC';
-               else
-                       $order = 'DESC';
-
+               /// @todo This query is totally bad, the whole functionality has to be changed
+               // The query leads to a really intense used index.
+               // By now we hide it if someone wants to.
+               if (!Config::get('system', 'no_count', false)) {
+                       if ($_GET['order'] === 'posted')
+                               $order = 'ASC';
+                       else
+                               $order = 'DESC';
 
-               $prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
-                       $sql_extra ORDER BY `created` $order ",
-                       dbesc($ph[0]['album']),
-                       intval($owner_uid)
-               );
+                       $prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
+                               $sql_extra ORDER BY `created` $order ",
+                               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;
+                       if (dbm::is_result($prvnxt)) {
+                               foreach ($prvnxt as $z => $entry) {
+                                       if ($entry['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 = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
-                       $nextlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
-               }
-
+                               $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
+                               $prevlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
+                               $nextlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
+                       }
+               }
 
                if (count($ph) == 1)
                        $hires = $lores = $ph[0];
@@ -1364,8 +1411,8 @@ function photos_content(&$a) {
                }
 
                $album_link = 'photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
-               $tools = Null;
-               $lock = Null;
+               $tools = null;
+               $lock = null;
 
                if ($can_post && ($ph[0]['uid'] == $owner_uid)) {
                        $tools = array(
@@ -1403,8 +1450,9 @@ function photos_content(&$a) {
                        'filename' => $hires['filename'],
                );
 
-               if ($nextlink)
+               if ($nextlink) {
                        $nextlink = array($nextlink, '<div class="icon next"></div>');
+               }
 
 
                // Do we have an item for this photo?
@@ -1421,8 +1469,9 @@ function photos_content(&$a) {
 
                $map = null;
 
-               if (count($linked_items)) {
+               if (dbm::is_result($linked_items)) {
                        $link_item = $linked_items[0];
+
                        $r = qu("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 and `item`.`moderated` = 0
@@ -1435,8 +1484,9 @@ function photos_content(&$a) {
 
                        );
 
-                       if (dbm::is_result($r))
+                       if (dbm::is_result($r)) {
                                $a->set_pager_total($r[0]['total']);
+                       }
 
 
                        $r = qu("SELECT `item`.*, `item`.`id` AS `item_id`,
@@ -1470,15 +1520,16 @@ function photos_content(&$a) {
                        }
                }
 
-               $tags=Null;
+               $tags = null;
 
                if (count($linked_items) && strlen($link_item['tag'])) {
-                       $arr = explode(',',$link_item['tag']);
+                       $arr = explode(',', $link_item['tag']);
                        // parse tags and add links
                        $tag_str = '';
                        foreach ($arr as $t) {
-                               if (strlen($tag_str))
+                               if (strlen($tag_str)) {
                                        $tag_str .= ', ';
+                               }
                                $tag_str .= bbcode($t);
                        }
                        $tags = array(t('Tags: '), $tag_str);
@@ -1495,21 +1546,25 @@ function photos_content(&$a) {
 
                        // Private/public post links for the non-JS ACL form
                        $private_post = 1;
-                       if ($_REQUEST['public'])
+                       if ($_REQUEST['public']) {
                                $private_post = 0;
+                       }
 
                        $query_str = $a->query_string;
-                       if (strpos($query_str, 'public=1') !== false)
+                       if (strpos($query_str, 'public=1') !== false) {
                                $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
+                       }
 
-                       // I think $a->query_string may never have ? in it, but I could be wrong
-                       // It looks like it's from the index.php?q=[etc] rewrite that the web
-                       // server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
-                       if (strpos($query_str, '?') === false)
+                       /*
+                        * I think $a->query_string may never have ? in it, but I could be wrong
+                        * It looks like it's from the index.php?q=[etc] rewrite that the web
+                        * server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
+                        */
+                       if (strpos($query_str, '?') === false) {
                                $public_post_link = '?public=1';
-                       else
+                       } else {
                                $public_post_link = '&public=1';
-
+                       }
 
                        if ($a->theme['template_engine'] === 'internal') {
                                $album_e = template_escape($ph[0]['album']);
@@ -1526,9 +1581,9 @@ function photos_content(&$a) {
                                '$album' => array('albname', t('New album name'), $album_e,''),
                                '$caption' => array('desc', t('Caption'), $caption_e, ''),
                                '$tags' => array('newtag', t('Add a Tag'), "", t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping')),
-                               '$rotate_none' => array('rotate',t('Do not rotate'),0,'', true),
-                               '$rotate_cw' => array('rotate',t('Rotate CW (right)'),1,''),
-                               '$rotate_ccw' => array('rotate',t('Rotate CCW (left)'),2,''),
+                               '$rotate_none' => array('rotate', t('Do not rotate'),0,'', true),
+                               '$rotate_cw' => array('rotate', t('Rotate CW (right)'),1,''),
+                               '$rotate_ccw' => array('rotate', t('Rotate CCW (left)'),2,''),
 
                                '$nickname' => $a->data['user']['nickname'],
                                '$resource_id' => $ph[0]['resource-id'],
@@ -1561,12 +1616,11 @@ function photos_content(&$a) {
 
                        $likebuttons = '';
 
-                       if ($can_post || can_write_wall($a,$owner_uid)) {
-                               $likebuttons = replace_macros($like_tpl,array(
+                       if ($can_post || can_write_wall($a, $owner_uid)) {
+                               $likebuttons = replace_macros($like_tpl, array(
                                        '$id' => $link_item['id'],
                                        '$likethis' => t("I like this \x28toggle\x29"),
                                        '$nolike' => (feature_enabled(local_user(), 'dislike') ? t("I don't like this \x28toggle\x29") : ''),
-                                       '$share' => t('Share'),
                                        '$wait' => t('Please wait'),
                                        '$return_path' => $a->query_string,
                                ));
@@ -1574,26 +1628,24 @@ function photos_content(&$a) {
 
                        $comments = '';
                        if (! dbm::is_result($r)) {
-                               if ($can_post || can_write_wall($a,$owner_uid)) {
-                                       if ($link_item['last-child']) {
-                                               $comments .= replace_macros($cmnt_tpl,array(
-                                                       '$return_path' => '',
-                                                       '$jsreload' => $return_url,
-                                                       '$type' => 'wall-comment',
-                                                       '$id' => $link_item['id'],
-                                                       '$parent' => $link_item['id'],
-                                                       '$profile_uid' =>  $owner_uid,
-                                                       '$mylink' => $contact['url'],
-                                                       '$mytitle' => t('This is you'),
-                                                       '$myphoto' => $contact['thumb'],
-                                                       '$comment' => t('Comment'),
-                                                       '$submit' => t('Submit'),
-                                                       '$preview' => t('Preview'),
-                                                       '$sourceapp' => t($a->sourcename),
-                                                       '$ww' => '',
-                                                       '$rand_num' => random_digits(12)
-                                               ));
-                                       }
+                               if (($can_post || can_write_wall($a, $owner_uid)) && $link_item['last-child']) {
+                                       $comments .= replace_macros($cmnt_tpl, array(
+                                               '$return_path' => '',
+                                               '$jsreload' => $return_url,
+                                               '$type' => 'wall-comment',
+                                               '$id' => $link_item['id'],
+                                               '$parent' => $link_item['id'],
+                                               '$profile_uid' =>  $owner_uid,
+                                               '$mylink' => $contact['url'],
+                                               '$mytitle' => t('This is you'),
+                                               '$myphoto' => $contact['thumb'],
+                                               '$comment' => t('Comment'),
+                                               '$submit' => t('Submit'),
+                                               '$preview' => t('Preview'),
+                                               '$sourceapp' => t($a->sourcename),
+                                               '$ww' => '',
+                                               '$rand_num' => random_digits(12)
+                                       ));
                                }
                        }
 
@@ -1608,8 +1660,6 @@ function photos_content(&$a) {
                                'attendyes' => array('title' => t('Attending','title')), 'attendno' => array('title' => t('Not attending','title')), 'attendmaybe' => array('title' => t('Might attend','title'))
                        );
 
-
-
                        // display comments
                        if (dbm::is_result($r)) {
 
@@ -1617,31 +1667,27 @@ function photos_content(&$a) {
                                        builtin_activity_puller($item, $conv_responses);
                                }
 
-                               $like    = ((x($conv_responses['like'],$link_item['uri'])) ? format_like($conv_responses['like'][$link_item['uri']],$conv_responses['like'][$link_item['uri'] . '-l'],'like',$link_item['id']) : '');
-                               $dislike = ((x($conv_responses['dislike'],$link_item['uri'])) ? format_like($conv_responses['dislike'][$link_item['uri']],$conv_responses['dislike'][$link_item['uri'] . '-l'],'dislike',$link_item['id']) : '');
-
-
-
-                               if ($can_post || can_write_wall($a,$owner_uid)) {
-                                       if ($link_item['last-child']) {
-                                               $comments .= replace_macros($cmnt_tpl,array(
-                                                       '$return_path' => '',
-                                                       '$jsreload' => $return_url,
-                                                       '$type' => 'wall-comment',
-                                                       '$id' => $link_item['id'],
-                                                       '$parent' => $link_item['id'],
-                                                       '$profile_uid' =>  $owner_uid,
-                                                       '$mylink' => $contact['url'],
-                                                       '$mytitle' => t('This is you'),
-                                                       '$myphoto' => $contact['thumb'],
-                                                       '$comment' => t('Comment'),
-                                                       '$submit' => t('Submit'),
-                                                       '$preview' => t('Preview'),
-                                                       '$sourceapp' => t($a->sourcename),
-                                                       '$ww' => '',
-                                                       '$rand_num' => random_digits(12)
-                                               ));
-                                       }
+                               $like    = ((x($conv_responses['like'], $link_item['uri'])) ? format_like($conv_responses['like'][$link_item['uri']], $conv_responses['like'][$link_item['uri'] . '-l'], 'like',$link_item['id']) : '');
+                               $dislike = ((x($conv_responses['dislike'], $link_item['uri'])) ? format_like($conv_responses['dislike'][$link_item['uri']], $conv_responses['dislike'][$link_item['uri'] . '-l'], 'dislike',$link_item['id']) : '');
+
+                               if (($can_post || can_write_wall($a, $owner_uid)) && $link_item['last-child']) {
+                                       $comments .= replace_macros($cmnt_tpl,array(
+                                               '$return_path' => '',
+                                               '$jsreload' => $return_url,
+                                               '$type' => 'wall-comment',
+                                               '$id' => $link_item['id'],
+                                               '$parent' => $link_item['id'],
+                                               '$profile_uid' =>  $owner_uid,
+                                               '$mylink' => $contact['url'],
+                                               '$mytitle' => t('This is you'),
+                                               '$myphoto' => $contact['thumb'],
+                                               '$comment' => t('Comment'),
+                                               '$submit' => t('Submit'),
+                                               '$preview' => t('Preview'),
+                                               '$sourceapp' => t($a->sourcename),
+                                               '$ww' => '',
+                                               '$rand_num' => random_digits(12)
+                                       ));
                                }
 
 
@@ -1653,7 +1699,7 @@ function photos_content(&$a) {
                                        if (((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
                                                continue;
 
-                                       $redirect_url = 'redir/' . $item['cid'] ;
+                                       $redirect_url = 'redir/' . $item['cid'];
 
 
                                        if (local_user() && ($item['contact-uid'] == local_user())
@@ -1672,8 +1718,6 @@ function photos_content(&$a) {
 
                                        $profile_link = $profile_url;
 
-
-
                                        $dropping = (($item['contact-id'] == $contact_id) || ($item['uid'] == local_user()));
                                        $drop = array(
                                                'dropping' => $dropping,
@@ -1682,7 +1726,6 @@ function photos_content(&$a) {
                                                'delete' => t('Delete'),
                                        );
 
-
                                        if ($a->theme['template_engine'] === 'internal') {
                                                $name_e = template_escape($profile_name);
                                                $title_e = template_escape($item['title']);
@@ -1707,27 +1750,24 @@ function photos_content(&$a) {
                                                '$comment' => $comment
                                        ));
 
-                                       if ($can_post || can_write_wall($a,$owner_uid)) {
-
-                                               if ($item['last-child']) {
-                                                       $comments .= replace_macros($cmnt_tpl,array(
-                                                               '$return_path' => '',
-                                                               '$jsreload' => $return_url,
-                                                               '$type' => 'wall-comment',
-                                                               '$id' => $item['item_id'],
-                                                               '$parent' => $item['parent'],
-                                                               '$profile_uid' =>  $owner_uid,
-                                                               '$mylink' => $contact['url'],
-                                                               '$mytitle' => t('This is you'),
-                                                               '$myphoto' => $contact['thumb'],
-                                                               '$comment' => t('Comment'),
-                                                               '$submit' => t('Submit'),
-                                                               '$preview' => t('Preview'),
-                                                               '$sourceapp' => t($a->sourcename),
-                                                               '$ww' => '',
-                                                               '$rand_num' => random_digits(12)
-                                                       ));
-                                               }
+                                       if (($can_post || can_write_wall($a, $owner_uid)) && $item['last-child']) {
+                                               $comments .= replace_macros($cmnt_tpl, array(
+                                                       '$return_path' => '',
+                                                       '$jsreload' => $return_url,
+                                                       '$type' => 'wall-comment',
+                                                       '$id' => $item['item_id'],
+                                                       '$parent' => $item['parent'],
+                                                       '$profile_uid' =>  $owner_uid,
+                                                       '$mylink' => $contact['url'],
+                                                       '$mytitle' => t('This is you'),
+                                                       '$myphoto' => $contact['thumb'],
+                                                       '$comment' => t('Comment'),
+                                                       '$submit' => t('Submit'),
+                                                       '$preview' => t('Preview'),
+                                                       '$sourceapp' => t($a->sourcename),
+                                                       '$ww' => '',
+                                                       '$rand_num' => random_digits(12)
+                                               ));
                                        }
                                }
                        }
@@ -1737,9 +1777,10 @@ function photos_content(&$a) {
 
 
                $response_verbs = array('like');
-               if (feature_enabled($owner_uid,'dislike'))
+               if (feature_enabled($owner_uid, 'dislike')) {
                        $response_verbs[] = 'dislike';
-               $responses = get_responses($conv_responses,$response_verbs,'',$link_item);
+               }
+               $responses = get_responses($conv_responses,$response_verbs, '', $link_item);
 
                $photo_tpl = get_markup_template('photo_view.tpl');
 
@@ -1749,7 +1790,7 @@ function photos_content(&$a) {
                        $like_e = template_escape($like);
                        $dislike_e = template_escape($dislike);
                } else {
-                       $album_e = array($album_link,$ph[0]['album']);
+                       $album_e = array($album_link, $ph[0]['album']);
                        $tags_e = $tags;
                        $like_e = $like;
                        $dislike_e = $dislike;
@@ -1776,11 +1817,11 @@ function photos_content(&$a) {
                        '$paginate' => $paginate,
                ));
 
-               $a->page['htmlhead'] .= "\n".'<meta name="twitter:card" content="photo" />'."\n";
-               $a->page['htmlhead'] .= '<meta name="twitter:title" content="'.$photo["album"].'" />'."\n";
-               $a->page['htmlhead'] .= '<meta name="twitter:image" content="'.$photo["href"].'" />'."\n";
-               $a->page['htmlhead'] .= '<meta name="twitter:image:width" content="'.$photo["width"].'" />'."\n";
-               $a->page['htmlhead'] .= '<meta name="twitter:image:height" content="'.$photo["height"].'" />'."\n";
+               $a->page['htmlhead'] .= "\n" . '<meta name="twitter:card" content="photo" />' . "\n";
+               $a->page['htmlhead'] .= '<meta name="twitter:title" content="' . $photo["album"] . '" />' . "\n";
+               $a->page['htmlhead'] .= '<meta name="twitter:image" content="' . $photo["href"] . '" />' . "\n";
+               $a->page['htmlhead'] .= '<meta name="twitter:image:width" content="' . $photo["width"] . '" />' . "\n";
+               $a->page['htmlhead'] .= '<meta name="twitter:image:height" content="' . $photo["height"] . '" />' . "\n";
 
                return $o;
        }