]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
really commiting files this time, still getting the hang of git.
[friendica.git] / mod / photos.php
index 18fff703ef0e7ade8d663534cc288d40186aafcd..cb7df15ccf96d6428e5f9d32a960ea8ab04e1c1c 100644 (file)
@@ -3,6 +3,7 @@ require_once('include/Photo.php');
 require_once('include/items.php');
 require_once('include/acl_selectors.php');
 require_once('include/bbcode.php');
+require_once('include/security.php');
 
 function photos_init(&$a) {
 
@@ -23,19 +24,30 @@ function photos_init(&$a) {
 
                $a->data['user'] = $r[0];
 
-               $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
+               $sql_extra = permissions_sql($a->data['user']['uid']);
+
+               $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra ",
                        intval($a->data['user']['uid'])
                );
 
                if(count($albums)) {
                        $a->data['albums'] = $albums;
 
-                       $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
-                       $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
-               
+                       $o .= '<div class="vcard">';
+                       $o .= '<div class="fn">' . $a->data['user']['username'] . '</h4>';
+                       $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg" alt="' . $a->data['user']['username'] . '" /></div>';
+                       $o .= '</div>';
+                       
+                       $o .= '<div id="side-bar-photos-albums" class="widget">';
+                       $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
+                                       
                        $o .= '<ul>';
                        foreach($albums as $album) {
-                               if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
+
+                               // don't show contact photos. We once translated this name, but then you could still access it under
+                               // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
+
+                               if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
                                        continue;
                                $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
                        }
@@ -80,15 +92,15 @@ EOT;
 
 function photos_post(&$a) {
 
-logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
+       logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
 
-foreach($_REQUEST AS $key => $val) {
-       logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
-}
+       foreach($_REQUEST AS $key => $val) {
+               logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
+       }
 
-foreach($_FILES AS $key => $val) {
-       logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
-}
+       foreach($_FILES AS $key => $val) {
+               logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
+       }
 
        $can_post  = false;
        $visitor   = 0;
@@ -133,7 +145,7 @@ foreach($_FILES AS $key => $val) {
        if(($a->argc > 3) && ($a->argv[2] === 'album')) {
                $album = hex2bin($a->argv[3]);
 
-               if($album == t('Profile Photos') || $album == t('Contact Photos')) {
+               if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
                        goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
                        return; // NOTREACHED
                }
@@ -376,6 +388,8 @@ foreach($_FILES AS $key => $val) {
 
                        if(count($tags)) {
                                foreach($tags as $tag) {
+                                       if(isset($profile))
+                                               unset($profile);
                                        if(strpos($tag,'@') === 0) {
                                                $name = substr($tag,1);
                                                if((strpos($name,'@')) || (strpos($name,'http://'))) {
@@ -397,7 +411,7 @@ foreach($_FILES AS $key => $val) {
                                                }
                                                else {
                                                        $newname = $name;
-                                                       if(strstr($name,'_')) {
+                                                       if(strstr($name,'_') || strstr($name,' ')) {
                                                                $newname = str_replace('_',' ',$name);
                                                                $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
                                                                        dbesc($newname),
@@ -405,7 +419,8 @@ foreach($_FILES AS $key => $val) {
                                                                );
                                                        }
                                                        else {
-                                                               $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
+                                                               $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
+                                                                       dbesc($name),
                                                                        dbesc($name),
                                                                        intval($page_owner_uid)
                                                                );
@@ -590,6 +605,9 @@ foreach($_FILES AS $key => $val) {
                $filesize   = intval($_FILES['userfile']['size']);
        }
 
+
+       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
+
        $maximagesize = get_config('system','maximagesize');
 
        if(($maximagesize) && ($filesize > $maximagesize)) {
@@ -600,6 +618,14 @@ foreach($_FILES AS $key => $val) {
                return;
        }
 
+       if(! $filesize) {
+               notice( t('Image file is empty.') . EOL);
+               @unlink($src);
+               $foo = 0;
+               call_hooks('photo_post_end',$foo);
+               return;
+       }
+
        logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
 
        $imagedata = @file_get_contents($src);
@@ -755,8 +781,6 @@ function photos_content(&$a) {
 
        $owner_uid = $a->data['user']['uid'];
 
-
-
        $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
 
        if((local_user()) && (local_user() == $owner_uid))
@@ -798,43 +822,28 @@ function photos_content(&$a) {
                }
        }
 
-       $r = q("SELECT `hidewall` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
-               intval($owner_uid)
-       );
-
-       if(count($r) && $r[0]['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
+       if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
                notice( t('Access to this item is restricted.') . EOL);
                return;
        }
 
-       // default permissions - anonymous user
-
-       $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
-
-       // Profile owner - everything is visible
+       $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
 
-       if(local_user() && (local_user() == $owner_uid)) {
-               $sql_extra = '';        
-       }
-       elseif(remote_user()) {
-               // authenticated visitor - here lie dragons
-               $gs = '<<>>'; // should be impossible to match
-               if(count($groups)) {
-                       foreach($groups as $g)
-                               $gs .= '|<' . intval($g) . '>';
-               } 
-               $sql_extra = sprintf(
-                       " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
-                         AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
-                         AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
-                         AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
+       $o = "";
 
-                       intval(remote_user()),
-                       intval(remote_user()),
-                       dbesc($gs),
-                       dbesc($gs)
-               );
-       }
+       // tabs
+       $tpl = get_markup_template('profile_tabs.tpl');
+       $_is_owner = (local_user() && (local_user() == $owner_uid));
+       $o .= replace_macros($tpl,array(
+               '$url' => $a->get_baseurl() . '/profile/' .$a->data['user']['nickname'],
+               '$phototab' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
+               '$status' => t('Status'),
+               '$profile' => t('Profile'),
+               '$photos' => t('Photos'),
+               '$events' => (($_is_owner) ? t('Events') : ''),
+               '$notes' => (($_is_owner) ?     t('Personal Notes') : ''),
+               '$activetab' => "photos",
+       ));     
 
        //
        // dispatch request
@@ -851,7 +860,7 @@ function photos_content(&$a) {
                $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
                if(count($a->data['albums'])) {
                        foreach($a->data['albums'] as $album) {
-                               if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
+                               if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
                                        continue;
                                $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
                        }
@@ -883,9 +892,9 @@ function photos_content(&$a) {
                        '$nickname' => $a->data['user']['nickname'],
                        '$newalbum' => t('New album name: '),
                        '$existalbumtext' => t('or existing album name: '),
-                       '$albumselect' => $albumselect,
+                       '$albumselect' => template_escape($albumselect),
                        '$permissions' => t('Permissions'),
-                       '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
+                       '$aclselect' => (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb))),
                        '$uploader' => $ret['addon_text'],
                        '$default' => (($ret['default_upload']) ? $default_upload : ''),
                        '$uploadurl' => $ret['post_url']
@@ -900,7 +909,7 @@ function photos_content(&$a) {
                $album = hex2bin($datum);
 
                $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
-                       $sql_extra GROUP BY `resource-id`",
+                       AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
                        intval($owner_uid),
                        dbesc($album)
                );
@@ -910,7 +919,7 @@ function photos_content(&$a) {
                }
 
                $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
-                       $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
+                       AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                        intval($owner_uid),
                        dbesc($album),
                        intval($a->pager['start']),
@@ -920,13 +929,13 @@ function photos_content(&$a) {
                $o .= '<h3>' . $album . '</h3>';
                
                if($cmd === 'edit') {           
-                       if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
+                       if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
                                if($can_post) {
                                        $edit_tpl = get_markup_template('album_edit.tpl');
                                        $o .= replace_macros($edit_tpl,array(
                                                '$nametext' => t('New album name: '),
                                                '$nickname' => $a->data['user']['nickname'],
-                                               '$album' => $album,
+                                               '$album' => template_escape($album),
                                                '$hexalbum' => bin2hex($album),
                                                '$submit' => t('Submit'),
                                                '$dropsubmit' => t('Delete Album')
@@ -935,7 +944,7 @@ function photos_content(&$a) {
                        }
                }
                else {
-                       if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
+                       if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
                                if($can_post) {
                                        $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
                                                . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
@@ -951,8 +960,8 @@ function photos_content(&$a) {
                                        '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
                                        '$phototitle' => t('View Photo'),
                                        '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
-                                       '$imgalt' => $rr['filename'],
-                                       '$desc'=> $rr['desc']
+                                       '$imgalt' => template_escape($rr['filename']),
+                                       '$desc'=> template_escape($rr['desc'])
                                ));
 
                }
@@ -968,7 +977,7 @@ function photos_content(&$a) {
 
 
 
-               $o = '';
+               //$o = '';
                // fetch image, item containing image, then comments
 
                $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
@@ -978,7 +987,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;
                }
 
@@ -1003,8 +1020,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;
                }
 
 
@@ -1027,7 +1045,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')),
                        );
 
@@ -1041,7 +1059,7 @@ function photos_content(&$a) {
                }
 
                if($prevlink)
-                       $prevlink = array($prevlink, t('<< Prev')) ;
+                       $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
 
                $photo = array(
                        'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
@@ -1050,7 +1068,7 @@ function photos_content(&$a) {
                );
 
                if($nextlink)
-                       $nextlink = array($nextlink, t('Next >>'));
+                       $nextlink = array($nextlink, '<div class="icon next"></div>');
 
 
                // Do we have an item for this photo?
@@ -1103,6 +1121,7 @@ function photos_content(&$a) {
                }
 
                $tags=Null;
+
                if(count($linked_items) && strlen($link_item['tag'])) {
                        $arr = explode(',',$link_item['tag']);
                        // parse tags and add links
@@ -1125,16 +1144,16 @@ 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'],
+                               '$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'),
@@ -1274,17 +1293,17 @@ function photos_content(&$a) {
                                        $drop = '';
 
                                        if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
-                                               $drop = replace_macros(get_markup_template('wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
+                                               $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
 
 
                                        $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,
@@ -1299,18 +1318,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,
                ));
@@ -1319,11 +1338,12 @@ function photos_content(&$a) {
        }
 
        // Default - show recent photos with upload link (if applicable)
-       $o = '';
+       //$o = '';
 
-       $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
+       $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' 
                $sql_extra GROUP BY `resource-id`",
                intval($a->data['user']['uid']),
+               dbesc('Contact Photos'),
                dbesc( t('Contact Photos'))
        );
        if(count($r)) {
@@ -1332,9 +1352,10 @@ function photos_content(&$a) {
        }
 
        $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
-               WHERE `uid` = %d AND `album` != '%s' 
+               WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'  
                $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                intval($a->data['user']['uid']),
+               dbesc('Contact Photos'),
                dbesc( t('Contact Photos')),
                intval($a->pager['start']),
                intval($a->pager['itemspage'])
@@ -1356,9 +1377,9 @@ function photos_content(&$a) {
                                '$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'],
+                               '$albumname'  => template_escape($rr['album']),
                                '$albumalt'   => t('View Album'),
-                               '$imgalt'     => $rr['filename']
+                               '$imgalt'     => template_escape($rr['filename'])
                        ));
 
                }