fisheye
[ class tree: fisheye ] [ index: fisheye ] [ all elements ]

Source for file FisheyeImage.php

Documentation is available at FisheyeImage.php

  1. <?php
  2. /**
  3.  * @package fisheye
  4.  */
  5.  
  6. /**
  7.  * required setup
  8.  */
  9. require_once( FISHEYE_PKG_PATH.'FisheyeBase.php' );
  10. // Needed for getting event_time and possible image title and data
  11. require_once( LIBERTY_PKG_PATH.'plugins/mime.image.php' );
  12.  
  13. define('FISHEYEIMAGE_CONTENT_TYPE_GUID', 'fisheyeimage');
  14.  
  15. /**
  16.  * @package fisheye
  17.  */
  18. class FisheyeImage extends FisheyeBase {
  19.     var $mImageId;
  20.  
  21.     function FisheyeImage($pImageId = NULL, $pContentId = NULL) {
  22.         FisheyeBase::FisheyeBase();
  23.         $this->mImageId = (int)$pImageId;
  24.         $this->mContentId = (int)$pContentId;
  25.  
  26.         $this->registerContentType(
  27.             FISHEYEIMAGE_CONTENT_TYPE_GUID, array( 'content_type_guid' => FISHEYEIMAGE_CONTENT_TYPE_GUID,
  28.                 'content_name' => 'Image',
  29.                 'handler_class' => 'FisheyeImage',
  30.                 'handler_package' => 'fisheye',
  31.                 'handler_file' => 'FisheyeImage.php',
  32.                 'maintainer_url' => 'http://www.bitweaver.org'
  33.         ));
  34.  
  35.         // Permission setup
  36.         $this->mViewContentPerm  = 'p_fisheye_view';
  37.         $this->mUpdateContentPerm  = 'p_fisheye_update';
  38.         $this->mAdminContentPerm = 'p_fisheye_admin';
  39.     }
  40.  
  41.     public static function lookup( $pLookupHash ) {
  42.         global $gBitDb;
  43.         $ret = NULL;
  44.  
  45.         $lookupContentId = NULL;
  46.         if (!empty($pLookupHash['image_id']) && is_numeric($pLookupHash['image_id'])) {
  47.             if( $lookup = $gBitDb->getRow( "SELECT lc.`content_id`, lc.`content_type_guid` FROM `".BIT_DB_PREFIX."fisheye_image` fi INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON(lc.`content_id`=fi.`content_id`) WHERE `image_id`=?", array( $pLookupHash['image_id'] ) ) ) {
  48.                 $lookupContentId = $lookup['content_id'];
  49.                 $lookupContentGuid = $lookup['content_type_guid'];
  50.             }
  51.         } elseif (!empty($pLookupHash['content_id']) && is_numeric($pLookupHash['content_id'])) {
  52.             $lookupContentId = $pLookupHash['content_id'];
  53.             $lookupContentGuid = NULL;
  54.         }
  55.  
  56.         if( BitBase::verifyId( $lookupContentId ) ) {
  57.             $ret = LibertyBase::getLibertyObject( $lookupContentId, $lookupContentGuid );
  58.         }
  59.  
  60.         return $ret;
  61.     }
  62.  
  63.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  64.         if( $this->isValid() ) {
  65.             global $gBitSystem;
  66.             $gateSql = NULL;
  67.             $selectSql = $joinSql = $whereSql = '';
  68.             $bindVars = array();
  69.  
  70.             if ( @$this->verifyId( $this->mImageId ) ) {
  71.                 $whereSql = " WHERE fi.`image_id` = ?";
  72.                 $bindVars[] = $this->mImageId;
  73.             } elseif ( @$this->verifyId( $this->mContentId ) ) {
  74.                 $whereSql = " WHERE fi.`content_id` = ?";
  75.                 $bindVars[] = $this->mContentId;
  76.             }
  77.  
  78.             $this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  79.  
  80.             $sql = "SELECT fi.*, lc.* $gateSql $selectSql
  81.                         , uue.`login` AS `modifier_user`, uue.`real_name` AS `modifier_real_name`
  82.                         , uuc.`login` AS `creator_user`, uuc.`real_name` AS `creator_real_name`, ufm.`favorite_content_id` AS `is_favorite`
  83.                         , lch.`hits`
  84.                     FROM `".BIT_DB_PREFIX."fisheye_image` fi
  85.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id` = fi.`content_id`)
  86.                         LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON (uue.`user_id` = lc.`modifier_user_id`)
  87.                         LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON (uuc.`user_id` = lc.`user_id`)
  88.                         LEFT JOIN `".BIT_DB_PREFIX."users_favorites_map` ufm ON (ufm.`favorite_content_id`=lc.`content_id` AND ufm.`user_id`=uuc.`user_id`)
  89.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON ( lch.`content_id` = lc.`content_id` ) $joinSql
  90.                     $whereSql";
  91.             if( $this->mInfo = $this->mDb->getRow( $sql, $bindVars ) ) {
  92.                 $this->mImageId = $this->mInfo['image_id'];
  93.                 $this->mContentId = $this->mInfo['content_id'];
  94.  
  95.                 $this->mInfo['creator'] = (isset( $this->mInfo['creator_real_name'] ) ? $this->mInfo['creator_real_name'] : $this->mInfo['creator_user'] );
  96.                 $this->mInfo['editor'] = (isset( $this->mInfo['modifier_real_name'] ) ? $this->mInfo['modifier_real_name'] : $this->mInfo['modifier_user'] );
  97.  
  98.                 if( $gBitSystem->isPackageActive( 'gatekeeper' ) && !@$this->verifyId( $this->mInfo['security_id'] ) ) {
  99.                     // check to see if this image is in a protected gallery
  100.                     // this burns an extra select but avoids an big and gnarly LEFT JOIN sequence that may be hard to optimize on all DB's
  101.                     $query = "SELECT ls.* FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim
  102.                                 INNER JOIN `".BIT_DB_PREFIX."gatekeeper_security_map` tsm ON(fgim.`gallery_content_id`=tsm.`content_id` )
  103.                                 INNER JOIN `".BIT_DB_PREFIX."gatekeeper_security` ls ON(tsm.`security_id`=ls.`security_id` )
  104.                               WHERE fgim.`item_content_id`=?";
  105.                     $grs = $this->mDb->query($query, array( $this->mContentId ) );
  106.                     if( $grs && $grs->RecordCount() ) {
  107.                         // order matters here
  108.                         $this->mInfo = array_merge( $grs->fields, $this->mInfo );
  109.                     }
  110.                 }
  111.  
  112.                 // LibertyMime will load the attachment details in $this->mStorage
  113.                 LibertyMime::load( NULL, $pPluginParams );
  114.  
  115.                 // parse the data after parent load so we have our html prefs
  116.                 $this->mInfo['parsed_data'] = $this->parseData();
  117.  
  118.                 // Copy mStorage to mInfo['image_file'] for easy access
  119.                 if( !empty( $this->mStorage ) && count( $this->mStorage ) > 0 ) {
  120.                     // it seems that this is not necessary and causes confusing copies of the same stuff all over the place
  121.                     $this->mInfo = array_merge( current( $this->mStorage ), $this->mInfo );
  122.                     // copy the image data by reference to reduce memory
  123.                     reset( $this->mStorage );
  124.                     $this->mInfo['image_file'] = current( $this->mStorage );
  125.                     // override original display_url that mime knows where we keep the image
  126.                     $this->mInfo['image_file']['display_url'] = $this->getDisplayUrl();
  127.                 } else {
  128.                     $this->mInfo['image_file'] = NULL;
  129.                 }
  130.  
  131.                 if( empty( $this->mInfo['width'] ) ||  empty( $this->mInfo['height'] ) ) {
  132.                     $details = $this->getImageDetails();
  133.                     // bounds checking on the width and height - corrupt photos can be ridiculously huge or negative
  134.                     if( !empty($details) AND $details['width'] > 0 AND $details['width'] < 9999 AND $details['height'] > 0 AND $details['height'] < 9999 ) {
  135.                         $this->mInfo['width'] = $details['width'];
  136.                         $this->mInfo['height'] = $details['height'];
  137.                         $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."fisheye_image` SET `width`=?, `height`=? WHERE `content_id`=?", array( $this->mInfo['width'], $this->mInfo['height'], $this->mContentId ) );
  138.                     }
  139.                 }
  140.             }
  141.         } else {
  142.             // We don't have an image_id or a content_id so there is no way to know what to load
  143.             return NULL;
  144.         }
  145.  
  146.         return count($this->mInfo);
  147.     }
  148.  
  149.     function storeDimensions( $pDetails ) {
  150.         if( $this->isValid() && $this->mInfo['width'] != $pDetails['width'] || $this->mInfo['height'] != $pDetails['height']  ) {
  151.             // if our data got out of sync with the database, force an update
  152.             $query = "UPDATE `".BIT_DB_PREFIX."fisheye_image` SET `width`=?, `height`=? WHERE `content_id`=?";
  153.             $this->mDb->query( $query, array( $pDetails['width'], $pDetails['height'], $this->mContentId ) );
  154.             $this->mInfo['width'] = $pDetails['width'];
  155.             $this->mInfo['height'] = $pDetails['height'];
  156.         }
  157.     }
  158.  
  159.     function exportHash() {
  160.         $ret = NULL;
  161.         // make sure we have a valid image file.
  162.         if( ($ret = parent::exportHash()) && ($details = $this->getImageDetails() ) ) {
  163.             $ret = array_merge( $ret, array(    'type' => $this->getContentType(),
  164.                                                 'landscape' => $this->isLandscape(),
  165.                                                 'has_description' => !empty( $this->mInfo['data'] ),
  166.                                                 'is_favorite' => $this->getField('is_favorite'),
  167.                                             ) );
  168.         }
  169.         return $ret;
  170.     }
  171.  
  172.     function isLandscape() {
  173.         return( !empty( $this->mInfo['width'] ) && !empty( $this->mInfo['height'] ) && ($this->mInfo['width'] > $this->mInfo['height']) );
  174.     }
  175.  
  176.     function verifyImageData(&$pParamHash) {
  177.         $pParamHash['content_type_guid'] = $this->getContentType();
  178.  
  179.         if ( empty($pParamHash['purge_from_galleries']) ) {
  180.             $pParamHash['purge_from_galleries'] = FALSE;
  181.         }
  182.  
  183.         if( !empty( $pParamHash['resize'] ) ) {
  184.             $pParamHash['_files_override'][0]['max_height'] = $pParamHash['_files_override'][0]['max_width'] = $pParamHash['resize'];
  185.         }
  186.  
  187.         // Make sure we know what to update
  188.         if( $this->isValid() ) {
  189.             // these 2 entries will inform LibertyContent and LibertyMime that this is an update
  190.             $pParamHash['content_id'] = $this->mContentId;
  191.             if( !empty(  $this->mInfo['attachment_id'] ) ) {
  192.                 $pParamHash['_files_override'][0]['attachment_id'] = $this->mInfo['attachment_id'];
  193.             }
  194.         }
  195.  
  196.         if( function_exists( 'mime_image_get_exif_data' ) && !empty( $pParamHash['_files_override'][0]['tmp_name'] ) ) {
  197.             $exifFile['source_file'] = $pParamHash['_files_override'][0]['tmp_name'];
  198.             $exifFile['type'] =  $pParamHash['_files_override'][0]['type'];
  199.             $exifHash = mime_image_get_exif_data( $exifFile );
  200.  
  201.             // Set some default values based on the Exif data
  202.             if( !empty( $exifHash['IFD0']['ImageDescription'] ) ) {
  203.                 if( empty( $pParamHash['title'] ) ) {
  204.                     $exifTitle = trim( $exifHash['IFD0']['ImageDescription'] );
  205.                     if( !empty( $exifTitle ) ) {
  206.                         $pParamHash['title'] = $exifTitle;
  207.                     }
  208.                 } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $exifHash['IFD0']['ImageDescription'] ) {
  209.                     $pParamHash['edit'] = $exifHash['IFD0']['ImageDescription'];
  210.                 }
  211.             }
  212.  
  213.             // These come from Photoshop
  214.             if( !empty( $exifHash['headline'] ) ) {
  215.                 if( empty( $pParamHash['title'] ) ) {
  216.                     $pParamHash['title'] = $exifHash['headline'];
  217.                 } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $exifHash['headline'] ) {
  218.                     $pParamHash['edit'] = $exifHash['headline'];
  219.                 }
  220.             }
  221.             if( !empty( $exifFile['caption'] ) ) {
  222.                 if( empty( $pParamHash['title'] ) ) {
  223.                     $pParamHash['title'] = $exifFile['caption'];
  224.                 } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $exifFile['caption'] ) {
  225.                     $pParamHash['edit'] = $exifFile['caption'];
  226.                 }
  227.             }
  228.  
  229.             if( empty( $pParamHash['event_time'] ) && !$this->getField( 'event_time' ) && !empty( $exifHash['EXIF']['DateTimeOriginal'] ) ) {
  230.                 $pParamHash['event_time'] = strtotime( $exifHash['EXIF']['DateTimeOriginal'] );
  231.             }
  232.  
  233.         }
  234.  
  235.         // let's add a default title if we still don't have one or the user has chosen to use filename over exif data
  236.         if( (empty( $pParamHash['title'] ) || !empty($_REQUEST['use_filenames'])) && !empty( $pParamHash['_files_override'][0]['name'] ) ) {
  237.             if( preg_match( '/^[A-Z]:\\\/', $pParamHash['_files_override'][0]['name'] ) ) {
  238.                 // MSIE shit file names if passthrough via gigaupload, etc.
  239.                 // basename will not work - see http://us3.php.net/manual/en/function.basename.php
  240.                 $tmp = preg_split("[\\\]", $pParamHash['_files_override'][0]['name'] );
  241.                 $defaultName = $tmp[count($tmp) - 1];
  242.                 $pParamHash['_files_override'][0]['name'] = $defaultName;
  243.             } else {
  244.                 $defaultName = $pParamHash['_files_override'][0]['name'];
  245.             }
  246.  
  247.             if( strpos( $pParamHash['_files_override'][0]['name'], '.' ) ) {
  248.                 list( $defaultName, $ext ) = explode( '.', $pParamHash['_files_override'][0]['name'] );
  249.             }
  250.             $pParamHash['title'] = str_replace( '_', ' ', $defaultName );
  251.         }
  252.  
  253.         if( count( $this->mErrors ) > 0 ){
  254.             parent::verify( $pParamHash );
  255.         }
  256.  
  257.         return (count($this->mErrors) == 0);
  258.     }
  259.  
  260.     function store(&$pParamHash) {
  261.         global $gBitSystem, $gLibertySystem;
  262.  
  263.         if ($this->verifyImageData($pParamHash)) {
  264.             // Save the current attachment ID for the image attached to this FisheyeImage so we can
  265.             // delete it after saving the new one
  266.             if (!empty($this->mInfo['attachment_id']) && !empty($pParamHash['_files_override'][0])) {
  267.                 $currentImageAttachmentId = $this->mInfo['attachment_id'];
  268.                 $pParamHash['attachment_id'] = $currentImageAttachmentId;
  269.             } else {
  270.                 $currentImageAttachmentId = NULL;
  271.             }
  272.  
  273.             // we have already done all the permission checking needed for this user to upload an image
  274.             $pParamHash['no_perm_check'] = TRUE;
  275.  
  276.             $this->mDb->StartTrans();
  277.             $pParamHash['thumbnail'] = !$gBitSystem->isFeatureActive( 'liberty_offline_thumbnailer' );
  278.             if( LibertyMime::store( $pParamHash ) ) {
  279.                 if( $currentImageAttachmentId && $currentImageAttachmentId != $this->mInfo['attachment_id'] ) {
  280.                     $this->expungeAttachment($currentImageAttachmentId);
  281.                 }
  282.                 // get storage format back from LibertyMime
  283.                 $this->mContentId = $pParamHash['content_id'];
  284.                 $this->mInfo['content_id'] = $this->mContentId;
  285.  
  286.                 if ( !empty( $this->mInfo['source_file'] ) && file_exists( $this->getSourceFile() )) {
  287.                     $imageDetails = $this->getImageDetails( $this->getSourceFile() );
  288.                 } else {
  289.                     $imageDetails = NULL;
  290.                 }
  291.  
  292.                 if (!$imageDetails) {
  293.                     $imageDetails['width'] = (!empty($this->mInfo['width']) ? $this->mInfo['width'] : NULL);
  294.                     $imageDetails['height'] = (!empty($this->mInfo['height']) ? $this->mInfo['height'] : NULL);
  295.                 }
  296.  
  297.                 if ($this->imageExistsInDatabase()) {
  298.                     $sql = "UPDATE `".BIT_DB_PREFIX."fisheye_image`
  299.                             SET `content_id` = ?, `width` = ?, `height` = ?
  300.                             WHERE `image_id` = ?";
  301.                     $bindVars = array($this->mContentId, $imageDetails['width'], $imageDetails['height'], $this->mImageId);
  302.                 } else {
  303.                     $this->mImageId = defined( 'LINKED_ATTACHMENTS' ) ? $this->mContentId : $this->mDb->GenID('fisheye_image_id_seq');
  304.                     $this->mInfo['image_id'] = $this->mImageId;
  305.                     $sql = "INSERT INTO `".BIT_DB_PREFIX."fisheye_image` (`image_id`, `content_id`, `width`, `height`) VALUES (?,?,?,?)";
  306.                     $bindVars = array($this->mImageId, $this->mContentId, $imageDetails['width'], $imageDetails['height']);
  307.                 }
  308.  
  309.                 $rs = $this->mDb->query($sql, $bindVars);
  310.  
  311.                 // check to see if we need offline thumbnailing
  312.                 if( $gBitSystem->isFeatureActive( 'liberty_offline_thumbnailer' ) ) {
  313.                     $resize = !empty( $pParamHash['resize'] ) ? (int)$pParamHash['resize'] : NULL;
  314.                     $this->generateThumbnails( $resize );
  315.                 } else {
  316.                     if( !empty( $pParamHash['resize'] ) && is_numeric( $pParamHash['resize'] ) ) {
  317.                         $this->resizeOriginal( $pParamHash['resize'] );
  318.                     }
  319.                 }
  320.                 $this->mDb->CompleteTrans();
  321.             } else {
  322.                 $this->mDb->RollbackTrans();
  323.             }
  324.         } else {
  325.             $this->mErrors[] = "There were errors while attempting to save this gallery image";
  326.         }
  327.         return (count($this->mErrors) == 0);
  328.     }
  329.  
  330.     function getExifField( $pExifField ) {
  331.         $ret = NULL;
  332.         if( function_exists( 'exif_read_data' ) ) {
  333.             $pExifField = strtolower( $pExifField );
  334.             $file = $this->getSourceFile();
  335.             // only attempt to get exif data from jpg or tiff files - chokes otherwise
  336.             if( empty( $this->mExif ) && preg_match( "!\.(jpe?g|tif{1,2})$!", $file ) ) {
  337.                 if( $exif = @exif_read_data( $file ) ) {
  338.                     $this->mExif = array_change_key_case( $exif, CASE_LOWER );
  339.                 }
  340.             }
  341.             if( !empty( $this->mExif[$pExifField] ) ) {
  342.                 $ret = $this->mExif[$pExifField];
  343.             }
  344.         }
  345.         return $ret;
  346.     }
  347.  
  348.     function rotateImage( $pDegrees, $pImmediateRender = FALSE ) {
  349.         global $gBitSystem;
  350.         if( $this->getField( 'file_name' ) || $this->load() ) {
  351.             $fileHash['source_file'] = $this->getSourceFile();
  352.             $fileHash['dest_base_name'] = preg_replace('/(.+)\..*$/', '$1', basename( $fileHash['source_file'] ) );
  353.             $fileHash['type'] = $gBitSystem->verifyMimeType( $fileHash['source_file'] );
  354.             $fileHash['size'] = filesize( $fileHash['source_file'] );
  355.             $fileHash['dest_branch'] = dirname( $this->getSourceFile() ).'/';
  356.             $fileHash['name'] = $this->getField( 'file_name' );
  357.             if( $pDegrees == 'auto' ) {
  358.                 if( $exifOrientation = $this->getExifField( 'orientation' ) ) {
  359.                     switch( $exifOrientation ) {
  360.                          case 1: //) transform="";;
  361.                             break;
  362.                          case 2: //) transform="-flip horizontal";;
  363.                             break;
  364.                          case 3: //) transform="-rotate 180";;
  365.                             $pDegrees = 180;
  366.                             break;
  367.                          case 4: //) transform="-flip vertical";;
  368.                             break;
  369.                          case 5: //) transform="-transpose";;
  370.                             break;
  371.                          case 6: //) transform="-rotate 90";;
  372.                             // make sure image has not already been rotated
  373.                             if( $this->isLandscape() ) {
  374.                                 $pDegrees = 90;
  375.                             }
  376.                             break;
  377.                          case 7: //) transform="-transverse";;
  378.                             break;
  379.                          case 8: //) transform="-rotate 270";;
  380.                             // make sure image has not already been rotated
  381.                             if( $this->isLandscape() ) {
  382.                                 $pDegrees = 270;
  383.                             }
  384.                             break;
  385.                          // *) transform="";;
  386.                     }
  387.                 }
  388.             }
  389.             if( is_numeric( $pDegrees ) ) {
  390.                 $fileHash['degrees'] = $pDegrees;
  391.                 $rotateFunc = liberty_get_function( 'rotate' );
  392.                 if( $rotateFunc( $fileHash ) ) {
  393.                     liberty_clear_thumbnails( $fileHash );
  394.                     $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."fisheye_image` SET `width`=`height`, `height`=`width` WHERE `content_id`=?", array( $this->mContentId ) );
  395.                     $this->generateThumbnails( FALSE, $pImmediateRender );
  396.                 } else {
  397.                     $this->mErrors['rotate'] = $fileHash['error'];
  398.                 }
  399.             } elseif( $pDegrees == 'auto' ) {
  400.                 $this->mErrors['rotate'] = "Image was not auto-rotated.";
  401.             }
  402.         }
  403.         return (count($this->mErrors) == 0);
  404.     }
  405.  
  406.  
  407.     /**
  408.      * convertColorspace
  409.      *
  410.      * @param string $pColorSpace - target color space, only 'grayscale' is currently supported, and only when using the MagickWand image processor
  411.      * @access public
  412.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  413.      */
  414.     function convertColorspace( $pColorSpace ) {
  415.         global $gBitSystem;
  416.         $ret = FALSE;
  417.         if( $this->getField( 'file_name' ) || $this->load() ) {
  418.             $fileHash['source_file'] = $this->getSourceFile();
  419.             $fileHash['dest_base_name'] = preg_replace('/(.+)\..*$/', '$1', basename( $fileHash['source_file'] ) );
  420.             $fileHash['type'] = $gBitSystem->verifyMimeType( $fileHash['source_file'] );
  421.             $fileHash['size'] = filesize( $fileHash['source_file'] );
  422.             $fileHash['dest_branch'] = dirname( $this->getSourceFile() ).'/';
  423.             $fileHash['name'] = $this->getField( 'file_name' );
  424.             if( $convertFunc = liberty_get_function( 'convert_colorspace' ) ) {
  425.                 if( $ret = $convertFunc( $fileHash, $pColorSpace ) ) {
  426.                     liberty_clear_thumbnails( $fileHash );
  427.                     $sql = "UPDATE `".BIT_DB_PREFIX."liberty_files SET `file_size`=? WHERE `file_id` = ?";
  428.                     $this->mDb->query( $sql, array( filesize( $fileHash['dest_file'] ), $this->mInfo['file_id'] ) );
  429.                     $this->generateThumbnails();
  430.                 }
  431.             }
  432.         }
  433.         return $ret;
  434.     }
  435.  
  436.  
  437.     function resizeOriginal( $pResizeOriginal ) {
  438.         global $gBitSystem;
  439.         if( $this->getField( 'file_name' ) || $this->load() ) {
  440.             $fileHash['source_file'] = $this->getSourceFile();
  441.             $fileHash['dest_base_name'] = preg_replace('/(.+)\..*$/', '$1', basename( $fileHash['source_file'] ) );
  442.             $fileHash['type'] = $gBitSystem->verifyMimeType( $fileHash['source_file'] );
  443.             $fileHash['size'] = filesize( $fileHash['source_file'] );
  444.             $fileHash['dest_branch'] = $this->getStorageBranch();
  445.             $fileHash['name'] = $this->getField( 'file_name' );
  446.             $fileHash['max_height'] = $fileHash['max_width'] = $pResizeOriginal;
  447.             // make a copy of the fileHash that we can compare output after processing
  448.             $preResize = $fileHash;
  449.             $resizeFunc = liberty_get_function( 'resize' );
  450.  
  451.             if( $resizeFile = $resizeFunc( $fileHash ) ) {
  452.                 clearstatcache();
  453.                 // Ack this is evil direct bashing of the liberty tables! XOXO spiderr
  454.                 // should be a cleaner way eventually
  455.  
  456.                 // we need to update the souce_file in case it's changed from a non jpg to a jpg
  457.                 if( $fileHash['name'] != $preResize['name'] ) {
  458.                     $fileHash['source_file'] = dirname( $fileHash['source_file'] ).'/'.$fileHash['name'];
  459.                     // make absolutely certain that we have 2 image files and that they are different, then remove the original
  460.                     if( $fileHash['source_file'] != $preResize['source_file'] && is_file( $fileHash['source_file'] ) && is_file( $preResize['source_file'] ) ) {
  461.                         @unlink( $preResize['source_file'] );
  462.                     }
  463.                 }
  464.                 $details = $this->getImageDetails( $resizeFile );
  465.                 // store all the values that might have changed due to the resize
  466.                 $storeHash = array(
  467.                     'file_size' => filesize( $resizeFile ),
  468.                     'mime_type' => $details['mime'],
  469.                 );
  470.                 $this->mDb->associateUpdate( BIT_DB_PREFIX."liberty_files", $storeHash, array( 'file_id' => $this->mInfo['file_id'] ) );
  471.                 //$query = "UPDATE `".BIT_DB_PREFIX."liberty_files` SET `file_size`=? WHERE `file_id`=?";
  472.                 //$this->mDb->query( $query, array( $details['size'], $this->mInfo['file_id'] ) );
  473.                 $query = "UPDATE `".BIT_DB_PREFIX."fisheye_image` SET `width`=?, `height`=? WHERE `content_id`=?";
  474.                 $this->mDb->query( $query, array( $details['width'], $details['height'], $this->mContentId ) );
  475.                 // if we've come this far, we can try removing the original if it's different to the resized image
  476.                 // make absolutely certain that we have 2 image files and that they are different, then remove the original
  477.                 if( $fileHash['source_file'] != $preResize['source_file'] && is_file( $fileHash['source_file'] ) && is_file( $preResize['source_file'] ) ) {
  478.                     @unlink( $preResize['source_file'] );
  479.                 }
  480.             } else {
  481.                 $this->mErrors['resize'] = $fileHash['error'];
  482.             }
  483.         }
  484.         return (count($this->mErrors) == 0);
  485.     }
  486.  
  487.  
  488.     function generateThumbnails( $pResizeOriginal=NULL, $pImmediateRender=FALSE ) {
  489.         global $gBitSystem;
  490.         $ret = FALSE;
  491.         // LibertyMime will take care of thumbnail generation of the offline thumbnailer is not active
  492.         if( $gBitSystem->isFeatureActive( 'liberty_offline_thumbnailer' ) && !$pImmediateRender ) {
  493.             $query = "DELETE FROM `".BIT_DB_PREFIX."liberty_process_queue`
  494.                       WHERE `content_id`=?";
  495.             $this->mDb->query( $query, array( $this->mContentId ) );
  496.             $query = "INSERT INTO `".BIT_DB_PREFIX."liberty_process_queue`
  497.                       (`content_id`, `queue_date`, `processor_parameters`) VALUES (?,?,?)";
  498.             $this->mDb->query( $query, array( $this->mContentId, $gBitSystem->getUTCTime(), serialize( array( 'resize_original' => $pResizeOriginal ) ) ) );
  499.         } else {
  500.             $ret = $this->renderThumbnails();
  501.         }
  502.         return $ret;
  503.     }
  504.  
  505.  
  506.     function renderThumbnails( $pThumbSizes=NULL ) {
  507.         global $gBitSystem;
  508.         if( $this->getField( 'file_name' ) || $this->load() ) {
  509.             $fileHash['source_file'] = $this->getSourceFile();
  510.             $fileHash['type'] = $gBitSystem->verifyMimeType( $fileHash['source_file'] );
  511.             $fileHash['size'] = filesize( $fileHash['source_file'] );
  512.             $fileHash['dest_branch'] = $this->getStorageBranch( $fileHash );
  513.             $fileHash['name'] = $this->getField( 'file_name' );
  514.             $fileHash['thumbnail_sizes'] = $pThumbSizes;
  515.             // just generate thumbnails
  516.             liberty_generate_thumbnails( $fileHash );
  517.             if( !empty( $fileHash['error'] ) ) {
  518.                 $this->mErrors['thumbnail'] = $fileHash['error'];
  519.             }
  520.         }
  521.         return( count($this->mErrors) == 0 );
  522.     }
  523. /*
  524.     function getStorageUrl( $pParamHash = array() ) {
  525.         $pParamHash['sub_dir'] =  $this->getParameter( $pParamHash, 'sub_dir', liberty_mime_get_storage_sub_dir_name( array( 'type'=>$this->getField( 'mime_type' ), 'name'=>$this->getField('file_name') ) ) );
  526.         $pParamHash['user_id'] = $this->getParameter( $pParamHash, 'user_id', $this->getField('user_id') );
  527.         return parent::getStorageUrl( $pParamHash ).$this->getParameter( $pParamHash, 'attachment_id', $this->getField('attachment_id') ).'/';
  528.     }
  529.  
  530.     function getStorageBranch( $pParamHash = array() ) {
  531.         $pParamHash['sub_dir'] = $this->getParameter( $pParamHash, 'sub_dir', liberty_mime_get_storage_sub_dir_name( array( 'type'=>$this->getField( 'mime_type' ), 'name'=>$this->getField('file_name') ) ) );
  532.         $pParamHash['user_id'] = $this->getParameter( $pParamHash, 'user_id', $this->getField('user_id') );
  533.         return parent::getStorageBranch( $pParamHash ).$this->getParameter( $pParamHash, 'attachment_id', $this->getField('attachment_id') ).'/';
  534.     }
  535.  
  536.     function getStoragePath( $pParamHash, $pRootDir=NULL ) {
  537.         $pParamHash['sub_dir'] = liberty_mime_get_storage_sub_dir_name( array( 'type'=>BitBase::getParameter( $pParamHash, 'mime_type', $this->getField( 'mime_type' ) ), 'name'=>BitBase::getParameter( $pParamHash, 'file_name', $this->getField('file_name') ) ) );
  538.         $pParamHash['user_id'] = $this->getParameter( $pParamHash, 'user_id', $this->getField('user_id') );
  539.         return parent::getStoragePath( $pParamHash ).$this->getParameter( $pParamHash, 'attachment_id', $this->getField('attachment_id') ).'/';
  540.     }
  541. */
  542.     function getPreviewHash() {
  543.         return $this->mInfo;
  544.     }
  545.  
  546.     // Get resolution, etc
  547.     function getImageDetails($pFilePath = NULL) {
  548.         $info = array();
  549.         if( file_exists( $pFilePath ) ) {
  550.             $checkFiles = array( $pFilePath, dirname( $pFilePath ).'/original.jpg' );
  551.         } else {
  552.             $sourceFile  = $this->getSourceFile();
  553.             $checkFiles = array( $sourceFile );
  554.             // was an original file created?
  555.             $originalFile = dirname( $sourceFile ).'/original.jpg';
  556.             if( file_exists( $originalFile ) && !is_link( $originalFile ) ) {
  557.                 $checkFiles[] = $originalFile;
  558.             }
  559.         }
  560.  
  561.         foreach( $checkFiles as $cf ) {
  562.             if ($cf && file_exists( $cf ) && filesize( $cf ) ) {
  563.                 if( $imageSize = getimagesize( rtrim( $cf ) ) ) {
  564.                     $info = $imageSize;
  565.                     $info['width'] = $info[0];
  566.                     $info['height'] = $info[1];
  567.                     $info['size'] = filesize( $cf );
  568.                     break;
  569.                 }
  570.             }
  571.         }
  572.         return $info;
  573.     }
  574.  
  575.     function getWidth() {
  576.         if( !isset( $this->mInfo['width'] ) ) {
  577.             $this->mInfo = array_merge( $this->mInfo, $this->getImageDetails() );
  578.         }
  579.         return $this->getField('width');
  580.     }
  581.  
  582.     function getHeight() {
  583.         if( !isset( $this->mInfo['width'] ) ) {
  584.             $this->mInfo = array_merge( $this->mInfo, $this->getImageDetails() );
  585.         }
  586.         return $this->getField('height');
  587.     }
  588.  
  589.     /**
  590.     * Returns include file that will setup vars for display
  591.     * @return the fully specified path to file to be included
  592.     */
  593.     function getRenderFile() {
  594.         return FISHEYE_PKG_PATH."display_fisheye_image_inc.php";
  595.     }
  596.  
  597.     /**
  598.     * Returns template file used for display
  599.     * @return the fully specified path to file to be included
  600.     */
  601.     function getRenderTemplate() {
  602.         return 'bitpackage:fisheye/view_image.tpl';
  603.     }
  604.  
  605.     /**
  606.     * Function that returns link to display a piece of content
  607.     * @param pImageId id of gallery to link
  608.     * @param pMixed if a string, it is assumed to be the size, if an array, it is assumed to be a mInfo hash
  609.     * @return the url to display the gallery.
  610.     */
  611.     public static function getDisplayUrlFromHash( &$pParamHash ) {
  612.         $ret = '';
  613.         $size = (!empty( $pParamHash['size'] ) && is_string( $pParamHash['size'] ) && isset( $pParamHash['thumbnail_url'][$pParamHash['size']] ) ) ? $pParamHash['size'] : NULL ;
  614.  
  615.         global $gBitSystem;
  616.         if( @BitBase::verifyId( $pParamHash['image_id'] ) ) {
  617.             if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
  618.                 $ret = FISHEYE_PKG_URL.'image/'.$pParamHash['image_id'];
  619.                 if( !empty( $pParamHash['gallery_path'] ) ) {
  620.                     $ret .= $pParamHash['gallery_path'];
  621.                 }
  622.                 if( $size ) {
  623.                     $ret .= '/'.$size;
  624.                 }
  625.             } else {
  626.                 $ret = FISHEYE_PKG_URL.'view_image.php?image_id='.$pParamHash['image_id'];
  627.                 if( !empty( $this ) && !empty( $pParamHash['gallery_path'] ) ) {
  628.                     $ret .= '&gallery_path='.$pParamHash['gallery_path'];
  629.                 }
  630.                 if( $size ) {
  631.                     $ret .= '&size='.$size;
  632.                 }
  633.             }
  634.         } elseif( @BitBase::verifyId( $pParamHash['content_id'] ) ) {
  635.             $ret = FISHEYE_PKG_URL.'view_image.php?content_id='.$pParamHash['content_id'];
  636.         }
  637.         return $ret;
  638.     }
  639.  
  640.     /**
  641.     * Function that returns link to display an image
  642.     * @return the url to display the gallery.
  643.     */
  644.     public function getDisplayUrl() {
  645.         $info = &$this->mInfo;
  646.         $info['image_id'] = $this->mImageId;
  647.         $info['gallery_path'] = $this->mGalleryPath;
  648.         return static::getDisplayUrlFromHash( $info );
  649.     }
  650.  
  651.     /**
  652.     * Function that returns link to display an image
  653.     * Used to display thumbnails for navigation bar
  654.     * @param pImageId id of image to link
  655.     * @return the url to display the image.
  656.     */
  657.     public function getImageUrl( $pImageId ) {
  658.         $info = array( 'image_id' => $pImageId );
  659.         return static::getDisplayUrlFromHash( $info );
  660.     }
  661.  
  662.     /**
  663.      * Generate a valid display link for the Blog
  664.      *
  665.      * @param    object    PostId of the item to use
  666.      * @param    array    Not used
  667.      * @return    object    Fully formatted html link for use by Liberty
  668.      */
  669.     function getDisplayLink( $pTitle=NULL, $pMixed=NULL, $pAnchor=NULL ) {
  670.         global $gBitSystem;
  671.  
  672.         $pTitle = trim( $pTitle );
  673.         if( empty( $pMixed ) && !empty( $this ) ) {
  674.             $pMixed = $this->mInfo;
  675.         }
  676.  
  677.         if( empty( $pTitle ) ) {
  678.             $pTitle = FisheyeImage::getTitleFromHash( $pMixed );
  679.         }
  680.  
  681.         $ret = $pTitle;
  682.         if( $gBitSystem->isPackageActive( 'fisheye' ) ) {
  683.             $ret = '<a title="'.$pTitle.'" href="'.FisheyeImage::getDisplayUrlFromHash( $pMixed ).'">'.$pTitle.'</a>';
  684.         }
  685.         return $ret;
  686.     }
  687.  
  688.     static public function getTitleFromHash( $pHash, $pDefault=TRUE ) {
  689.         $ret = trim( parent::getTitleFromHash( $pHash, $pDefault ) );
  690.         if( empty( $ret ) && $pDefault ) {
  691.             $storage = (!empty( $this ) && !empty( $this->mStorage ) ? current( $this->mStorage ) : NULL);
  692.             if( !empty( $storage['file_name'] ) ) {
  693.                 $ret = $storage['file_name'];
  694.             } else {
  695.                 global $gLibertySystem;
  696.                 $ret = $gLibertySystem->getContentTypeName( $pHash['content_type_guid'] );
  697.                 if( !empty( $pHash['image_id'] ) ) {
  698.                     $ret .= " ".$pHash['image_id'];
  699.                 }
  700.             }
  701.         }
  702.         return $ret;
  703.     }
  704.  
  705.     public function getTitle() {
  706.         $ret = NULL;
  707.         if( $this->isValid() ) {
  708.             $ret = self::getTitleFromHash( $this->mInfo );
  709.         }
  710.         return $ret;
  711.     }
  712.  
  713.  
  714.     function getThumbnailContentId() {
  715.         return( $this->mContentId );
  716.     }
  717.  
  718.     function getThumbnailUrl( $pSize = 'small', $pSecondaryId = NULL, $pDefault=TRUE ) {
  719.         $ret = NULL;
  720.         if( $this->isValid() && isset( $this->mInfo['thumbnail_url'][$pSize] ) ) {
  721.             $ret = $this->mInfo['thumbnail_url'][$pSize];
  722.         }
  723.         return $ret;
  724.     }
  725.  
  726.     public static function getThumbnailUrlFromHash( &$pMixed, $pSize = 'small', $pSecondaryId = NULL, $pDefault=TRUE ) {
  727.         $ret = NULL;
  728.         if( isset( $pMixed['thumbnail_url'][$pSize] ) ) {
  729.             $ret = $pMixed['thumbnail_url'][$pSize];
  730.         }
  731.         return $ret;
  732.     }
  733.  
  734.     function expunge($pExpungeAttachment = TRUE) {
  735.         if( $this->isValid() ) {
  736.             $this->mDb->StartTrans();
  737.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` WHERE `item_content_id` = ?";
  738.             $rs = $this->mDb->query($query, array( $this->mContentId ));
  739.             $query = "UPDATE `".BIT_DB_PREFIX."fisheye_gallery` SET `preview_content_id`=NULL WHERE `preview_content_id` = ?";
  740.             $rs = $this->mDb->query($query, array( $this->mContentId ));
  741.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_image` WHERE `content_id` = ?";
  742.             $rs = $this->mDb->query($query, array( $this->mContentId ));
  743.             if( LibertyMime::expunge($pExpungeAttachment) ) {
  744.                 $this->mDb->CompleteTrans();
  745.                 $this->mImageId = NULL;
  746.                 $this->mContentId = NULL;
  747.             } else {
  748.                 $this->mDb->RollbackTrans();
  749.             }
  750.         }
  751.         return( count( $this->mErrors ) == 0 );
  752.     }
  753.  
  754.     function expungingAttachment($pAttachmentId, $pContentIdArray) {
  755.         foreach ($pContentIdArray as $id) {
  756.             $this->mContentId = $id;
  757.             // Vital that we call LibertyMime::expunge with false since the attachment is already being deleted.
  758.             $this->expunge(FALSE);
  759.         }
  760.     }
  761.  
  762.     function isValid() {
  763.         return( @$this->verifyId( $this->mImageId ) || @$this->verifyId( $this->mContentId ) );
  764.     }
  765.  
  766.     function imageExistsInDatabase() {
  767.         $ret = FALSE;
  768.         if( $this->isValid() && $this->mImageId ) {
  769.             $query = "SELECT COUNT(`image_id`)
  770.                     FROM `".BIT_DB_PREFIX."fisheye_image`
  771.                     WHERE `image_id` = ?";
  772.  
  773.             $bindVars = array($this->mImageId);
  774.  
  775.             if($this->mDb->getOne($query, $bindVars) > 0){
  776.                 $ret = TRUE;
  777.             }
  778.  
  779.         }
  780.         return $ret;
  781.     }
  782.  
  783.  
  784.     function getList( &$pListHash ) {
  785.         global $gBitUser,$gBitSystem;
  786.  
  787.         LibertyContent::prepGetList( $pListHash );
  788.         $ret = $bindVars = array();
  789.         $distinct = '';
  790.         $select = '';
  791.         $whereSql = '';
  792.         $joinSql = '';
  793.  
  794.         if( @$this->verifyId( $pListHash['user_id'] ) ) {
  795.             $whereSql .= " AND lc.`user_id` = ? ";
  796.             $bindVars[] = $pListHash['user_id'];
  797.         } elseif( !empty( $pListHash['recent_users'] )) {
  798.             $distinct = " DISTINCT ON ( lc.created/86400, uu.`user_id` ) ";
  799.             $pListHash['sort_mode'] = 'uu.user_id_desc';
  800.         }
  801.  
  802.         if( @$this->verifyId( $pListHash['gallery_id'] ) ) {
  803.             $whereSql .= " AND fg.`gallery_id` = ? ";
  804.             $bindVars[] = $pListHash['gallery_id'];
  805.         }
  806.  
  807.         if( !empty( $pListHash['search'] ) ) {
  808.             $whereSql .= " AND UPPER(lc.`title`) LIKE ? ";
  809.             $bindVars[] = '%'.strtoupper( $pListHash['search'] ).'%';
  810.         }
  811.  
  812.         if( !empty( $pListHash['max_age'] ) && is_numeric( $pListHash['max_age'] ) ) {
  813.             $whereSql .= " AND lc.`created` > ? ";
  814.             $bindVars[] = $pListHash['max_age'];
  815.         }
  816.  
  817.         $this->getServicesSql( 'content_user_collection_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, $pListHash );
  818.  
  819.         $orderby = '';
  820.         if( !empty( $pListHash['recent_images'] )) {
  821.             // get images from recent user truncated by day. This is necessary because DISTINCT ON expressions must match initial ORDER BY expressions
  822.             $distinct = " DISTINCT ON ( lc.`created`/86400, uu.`user_id` ) ";
  823.             $orderby = " ORDER BY lc.`created`/86400 DESC, uu.`user_id`";
  824.         } elseif ( !empty( $pListHash['sort_mode'] ) ) {
  825.             //converted in prepGetList()
  826.             $orderby = " ORDER BY ".$this->mDb->convertSortmode( $pListHash['sort_mode'] )." ";
  827.         }
  828.  
  829.         $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  830.  
  831.         if( !empty( $whereSql ) ) {
  832.             $whereSql = substr_replace( $whereSql, ' WHERE ', 0, 4 );
  833.         }
  834.  
  835.         $thumbSize = (!empty( $pListHash['size'] ) ? $pListHash['size'] : 'avatar' );
  836.  
  837.         $query = "SELECT $distinct fi.`image_id` AS `hash_key`, fi.*, lf.*, la.attachment_id, lc.*, fg.`gallery_id`, uu.`login`, uu.`real_name` $select $selectSql
  838.                 FROM `".BIT_DB_PREFIX."fisheye_image` fi
  839.                     INNER JOIN `".BIT_DB_PREFIX."liberty_attachments` la ON(la.`content_id`=fi.`content_id`)
  840.                     INNER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON(la.`foreign_id`=lf.`file_id`)
  841.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON(fi.`content_id` = lc.`content_id`)
  842.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON(uu.`user_id` = lc.`user_id`) $joinSql
  843.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim2 ON(tfgim2.`item_content_id`=lc.`content_id`)
  844.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."fisheye_gallery` fg ON(fg.`content_id`=tfgim2.`gallery_content_id`)
  845.                 $whereSql $orderby";
  846.         if( $rs = $this->mDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'], $pListHash['query_cache_time'] ) ) {
  847.             while( $row = $rs->fetchRow() ) {
  848.                 // legacy table data was named storage_path and included a partial path. strip out any path just in case
  849.                 $row['file_name'] = basename( $row['file_name'] );
  850.                 $ret[$row['hash_key']] = $row;
  851.                 $imageId = $row['image_id'];
  852.                 if( empty( $pListHash['no_thumbnails'] ) ) {
  853.                     $ret[$imageId]['display_url']      = static::getDisplayUrlFromHash( $row );
  854.                     $ret[$imageId]['has_machine_name'] = $this->isMachineName( $ret[$imageId]['title'] );
  855.                     $ret[$imageId]['source_url']      = $this->getStorageUrl( $row ).$row['file_name'];
  856.                     $ret[$imageId]['thumbnail_url']    = liberty_fetch_thumbnail_url( array(
  857.                         'source_file'   => $this->getSourceFile( $row ),
  858.                         'default_image' => FISHEYE_PKG_URL.'image/generating_thumbnails.png',
  859.                         'size'          => $thumbSize,
  860.                         'type'            => $row['mime_type'],
  861.                     ));
  862.                 }
  863.             }
  864.         }
  865.         return $ret;
  866.     }
  867.  
  868.     /**
  869.      * isCommentable
  870.      *
  871.      * @access public
  872.      * @return TRUE on success, FALSE on failure
  873.      */
  874.     function isCommentable() {
  875.         global $gGallery;
  876.  
  877.         // if we have a loaded gallery, we just use that to work out if we can add comments to this image
  878.         if( is_object( $gGallery ) ) {
  879.             return $gGallery->isCommentable();
  880.         }
  881.  
  882.         $ret = FALSE;
  883.         if( $parents = $this->getParentGalleries() ) {
  884.             // @TODO: No idea how to work out if you can add a comment to this image
  885.             // for now we'll take the mGalleryPath and use that gallery
  886.             $gal = current( $parents );
  887.             $query = "SELECT `pref_value` FROM `".BIT_DB_PREFIX."liberty_content_prefs` WHERE `content_id` = ? AND `pref_name` = ?";
  888.             $ret = ( $this->mDb->getOne( $query, array( $gal['content_id'], 'allow_comments' )) == 'y' );
  889.         }
  890.         return $ret;
  891.     }
  892.  
  893.     public static function getServiceKey() {
  894.         return 'fisheye';
  895.     }
  896. }
  897.  
  898. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:34 +0000 by phpDocumentor 1.5.0-lsces