]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
Merge pull request #415 from 23n/patch-2
[friendica.git] / mod / photos.php
index 3ce6f9934d27f8d4eb2d5d002ecf87a6b276f953..af40ea30371a43f4e89a1dfdaf56e33487fdcc2d 100644 (file)
@@ -5,6 +5,7 @@ require_once('include/acl_selectors.php');
 require_once('include/bbcode.php');
 require_once('include/security.php');
 
+
 function photos_init(&$a) {
 
 
@@ -35,10 +36,12 @@ function photos_init(&$a) {
 
                        $o .= '<div class="vcard">';
                        $o .= '<div class="fn">' . $a->data['user']['username'] . '</div>';
-                       $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg" alt="' . $a->data['user']['username'] . '" /></div>';
+                       $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_cached_avatar_image($a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg') . '" alt="' . $a->data['user']['username'] . '" /></div>';
                        $o .= '</div>';
-                       
-                       if(! intval($a->data['user']['hidewall'])) {
+
+                       $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);     
+
+                       if($albums_visible) {
                                $o .= '<div id="side-bar-photos-albums" class="widget">';
                                $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h3>';
                                        
@@ -66,30 +69,11 @@ function photos_init(&$a) {
                $a->page['aside'] .= $o;
 
 
-               $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
-
-               $a->page['htmlhead'] .= <<< EOT
-
-               $(document).ready(function() {
-
-                       $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
-                               var selstr;
-                               $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
-                                       selstr = $(this).text();
-                                       $('#jot-perms-icon').removeClass('unlock').addClass('lock');
-                                       $('#jot-public').hide();
-                               });
-                               if(selstr == null) { 
-                                       $('#jot-perms-icon').removeClass('lock').addClass('unlock');
-                                       $('#jot-public').show();
-                               }
-
-                       }).trigger('change');
-
-               });
+               $tpl = get_markup_template("photos_head.tpl");
+               $a->page['htmlhead'] .= replace_macros($tpl,array(
+                       '$ispublic' => t('everybody')
+               ));
 
-               </script>
-EOT;
        }
 
        return;
@@ -105,6 +89,8 @@ function photos_post(&$a) {
        logger('mod_photos: REQUEST ' . print_r($_REQUEST,true), LOGGER_DATA);
        logger('mod_photos: FILES '   . print_r($_FILES,true), LOGGER_DATA);
 
+       $phototypes = Photo::supportedTypes();
+
        $can_post  = false;
        $visitor   = 0;
 
@@ -303,7 +289,8 @@ function photos_post(&$a) {
                        $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
 
 
-               if((x($_POST,'rotate') !== false) && (intval($_POST['rotate']) == 1)) {
+               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",
@@ -311,9 +298,10 @@ function photos_post(&$a) {
                                intval($page_owner_uid)
                        );
                        if(count($r)) {
-                               $ph = new Photo($r[0]['data']);
+                               $ph = new Photo($r[0]['data'], $r[0]['type']);
                                if($ph->is_valid()) {
-                                       $ph->rotate(270);
+                                       $rotate_deg = ( (intval($_POST['rotate']) == 1) ? 270 : 90 );
+                                       $ph->rotate($rotate_deg);
 
                                        $width  = $ph->getWidth();
                                        $height = $ph->getHeight();
@@ -322,8 +310,8 @@ function photos_post(&$a) {
                                                dbesc($ph->imageString()),
                                                intval($height),
                                                intval($width),
-                               dbesc($resource_id),
-                               intval($page_owner_uid)
+                                               dbesc($resource_id),
+                                               intval($page_owner_uid)
                                        );
 
                                        if($width > 640 || $height > 640) {
@@ -335,8 +323,8 @@ function photos_post(&$a) {
                                                        dbesc($ph->imageString()),
                                                        intval($height),
                                                        intval($width),
-                               dbesc($resource_id),
-                               intval($page_owner_uid)
+                                                       dbesc($resource_id),
+                                                       intval($page_owner_uid)
                                                );
                                        }
 
@@ -349,8 +337,8 @@ function photos_post(&$a) {
                                                        dbesc($ph->imageString()),
                                                        intval($height),
                                                        intval($width),
-                               dbesc($resource_id),
-                               intval($page_owner_uid)
+                                                       dbesc($resource_id),
+                                                       intval($page_owner_uid)
                                                );
                                        }       
                                }
@@ -362,6 +350,7 @@ function photos_post(&$a) {
                        intval($page_owner_uid)
                );
                if(count($p)) {
+                       $ext = $phototypes[$p[0]['type']];
                        $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
                                dbesc($desc),
                                dbesc($albname),
@@ -386,7 +375,7 @@ function photos_post(&$a) {
 
                        $title = '';
                        $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
-
+                       
                        $arr = array();
 
                        $arr['uid']           = $page_owner_uid;
@@ -412,7 +401,7 @@ function photos_post(&$a) {
                        $arr['origin']        = 1;
                        
                        $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
-                                               . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
+                                               . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]' 
                                                . '[/url]';
                
                        $item_id = item_store($arr);
@@ -581,17 +570,17 @@ function photos_post(&$a) {
                                        $arr['inform']        = $tagged[2];
                                        $arr['origin']        = 1;
                                        $arr['body']          = '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]' . ' ' . t('was tagged in a') . ' ' . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('photo') . '[/url]' . ' ' . t('by') . ' ' . '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]' ;
-                                       $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '[/img][/url]' . "\n" ;
+                                       $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ;
 
                                        $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
                                        $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
                                        if($tagged[3])
-                                               $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
+                                               $arr['object'] .= xmlify('<link rel="photo" type="'.$p[0]['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n");
                                        $arr['object'] .= '</link></object>' . "\n";
 
                                        $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
                                                . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
-                                       $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="image/jpeg" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '" />') . '</link></target>';
+                                       $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
 
                                        $item_id = item_store($arr);
                                        if($item_id) {
@@ -662,7 +651,7 @@ function photos_post(&$a) {
        $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
        $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
 
-       $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
+       $ret = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
 
        call_hooks('photo_post_file',$ret);
 
@@ -670,15 +659,17 @@ function photos_post(&$a) {
                $src      = $ret['src'];
                $filename = $ret['filename'];
                $filesize = $ret['filesize'];
+               $type     = $ret['type'];
        }
        else {
                $src        = $_FILES['userfile']['tmp_name'];
                $filename   = basename($_FILES['userfile']['name']);
                $filesize   = intval($_FILES['userfile']['size']);
+               $type       = $_FILES['userfile']['type'];
        }
+       if ($type=="") $type=guess_image_type($filename);
 
-
-       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
+       logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
 
        $maximagesize = get_config('system','maximagesize');
 
@@ -701,7 +692,25 @@ function photos_post(&$a) {
        logger('mod/photos.php: photos_post(): loading the contents of ' . $src , LOGGER_DEBUG);
 
        $imagedata = @file_get_contents($src);
-       $ph = new Photo($imagedata);
+
+
+
+       $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();
+       }
+               
+
+       $ph = new Photo($imagedata, $type);
 
        if(! $ph->is_valid()) {
                logger('mod/photos.php: photos_post(): unable to process image' , LOGGER_DEBUG);
@@ -712,8 +721,15 @@ function photos_post(&$a) {
                killme();
        }
 
+       $ph->orient($src);
        @unlink($src);
 
+       $max_length = get_config('system','max_image_length');
+       if(! $max_length)
+               $max_length = MAX_IMAGE_LENGTH;
+       if($max_length > 0)
+               $ph->scaleImage($max_length);
+
        $width  = $ph->getWidth();
        $height = $ph->getHeight();
 
@@ -771,7 +787,7 @@ function photos_post(&$a) {
        $arr['origin']        = 1;
 
        $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
-                               . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
+                               . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/img]' 
                                . '[/url]';
 
        $item_id = item_store($arr);
@@ -814,8 +830,8 @@ function photos_content(&$a) {
                notice( t('Public access denied.') . EOL);
                return;
        }
-
-
+       
+       
        require_once('include/bbcode.php');
        require_once('include/security.php');
        require_once('include/conversation.php');
@@ -825,6 +841,8 @@ function photos_content(&$a) {
                return;
        }
 
+       $phototypes = Photo::supportedTypes();
+
        $_SESSION['photo_return'] = $a->cmd;
 
        //
@@ -925,7 +943,7 @@ function photos_content(&$a) {
                $selname = (($datum) ? hex2bin($datum) : '');
 
 
-               $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
+               $albumselect = '';
 
                
                $albumselect .= '<option value="" ' . ((! $selname) ? ' selected="selected" ' : '') . '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
@@ -940,8 +958,6 @@ function photos_content(&$a) {
 
                $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
 
-               $albumselect .= '</select>';
-
                $uploader = '';
 
                $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
@@ -951,16 +967,29 @@ function photos_content(&$a) {
 
                call_hooks('photo_upload_form',$ret);
 
-               $default_upload = '<input type="file" name="userfile" />        <div class="photos-upload-submit-wrapper" >
+               $default_upload = '<input id="photos-upload-choose" type="file" name="userfile" />      <div class="photos-upload-submit-wrapper" >
                <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
 
 
+               $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) {
+                       $usage_message = sprintf( t("You have used %1$.2f Mbytes of %2$.2f Mbytes photo storage."), $r[0]['total'] / 1024000, $limit / 1024000 );
+               }
+               else {
+                       $usage_message = sprintf( t('You have used %1$.2f Mbytes of photo storage.'), $r[0]['total'] / 1024000 );
+               }
+
 
                $tpl = get_markup_template('photos_upload.tpl');
                $o .= replace_macros($tpl,array(
                        '$pagename' => t('Upload Photos'),
                        '$sessid' => session_id(),
+                       '$usage' => $usage_message,
                        '$nickname' => $a->data['user']['nickname'],
                        '$newalbum' => t('New album name: '),
                        '$existalbumtext' => t('or existing album name: '),
@@ -991,7 +1020,7 @@ function photos_content(&$a) {
                        $a->set_pager_itemspage(20);
                }
 
-               $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
+               $r = q("SELECT `resource-id`, `id`, `filename`, type, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
                        AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                        intval($owner_uid),
                        dbesc($album),
@@ -1038,13 +1067,15 @@ function photos_content(&$a) {
                                        $twist = 'rotleft';
                                else
                                        $twist = 'rotright';
+                               
+                               $ext = $phototypes[$rr['type']];
 
                                $o .= replace_macros($tpl,array(
                                        '$id' => $rr['id'],
                                        '$twist' => ' ' . $twist . rand(2,4),
                                        '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
                                        '$phototitle' => t('View Photo'),
-                                       '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
+                                       '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.' .$ext,
                                        '$imgalt' => template_escape($rr['filename']),
                                        '$desc'=> template_escape($rr['desc'])
                                ));
@@ -1143,24 +1174,21 @@ function photos_content(&$a) {
                        
                }
 
-               if(! $cmd !== 'edit') {
-                       $a->page['htmlhead'] .= '<script>
-                               $(document).keydown(function(event) {' . "\n";
-
-                       if($prevlink)
-                               $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 37) { event.preventDefault(); window.location.href = \'' . $prevlink . '\'; }' . "\n";
-                       if($nextlink)
-                               $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 39) { event.preventDefault(); window.location.href = \'' . $nextlink . '\'; }' . "\n";
-                       $a->page['htmlhead'] .= '});</script>';
+               if( $cmd === 'edit') {
+                       $tpl = get_markup_template('photo_edit_head.tpl');
+                       $a->page['htmlhead'] .= replace_macros($tpl,array(
+                               '$prevlink' => $prevlink,
+                               '$nextlink' => $nextlink
+                       ));
                }
 
                if($prevlink)
                        $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
 
                $photo = array(
-                       'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
+                       'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.' . $phototypes[$hires['type']],
                        'title'=> t('View Full Size'),
-                       'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '?f=&_u=' . datetime_convert('','','','ymdhis')
+                       'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.' . $phototypes[$lores['type']] . '?f=&_u=' . datetime_convert('','','','ymdhis')
                );
 
                if($nextlink)
@@ -1240,7 +1268,8 @@ function photos_content(&$a) {
                        $edit_tpl = get_markup_template('photo_edit.tpl');
                        $edit = replace_macros($edit_tpl, array(
                                '$id' => $ph[0]['id'],
-                               '$rotate' => t('Rotate CW'),
+                               '$rotatecw' => t('Rotate CW (right)'),
+                               '$rotateccw' => t('Rotate CCW (left)'),
                                '$album' => template_escape($ph[0]['album']),
                                '$newalbum' => t('New album name'), 
                                '$nickname' => $a->data['user']['nickname'],
@@ -1449,7 +1478,7 @@ function photos_content(&$a) {
                $a->set_pager_itemspage(20);
        }
 
-       $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
+       $r = q("SELECT `resource-id`, `id`, `filename`, type, `album`, max(`scale`) AS `scale` FROM `photo`
                WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'  
                $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
                intval($a->data['user']['uid']),
@@ -1469,13 +1498,14 @@ function photos_content(&$a) {
                                $twist = 'rotleft';
                        else
                                $twist = 'rotright';
-
+                       $ext = $phototypes[$rr['type']];
+                       
                        $photos[] = array(
                                'id'       => $rr['id'],
                                'twist'    => ' ' . $twist . rand(2,4),
                                'link'          => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
                                'title'         => t('View Photo'),
-                               'src'           => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
+                               'src'           => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
                                'alt'           => template_escape($rr['filename']),
                                'album' => array(
                                        'link'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),