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

Source for file LibertyAttachable.php

Documentation is available at LibertyAttachable.php

  1. <?php
  2. /**
  3.  * Management of Liberty Content
  4.  *
  5.  * @package  liberty
  6.  * @version  $Header$
  7.  * @author   spider <spider@steelsun.com>
  8.  */
  9. // +----------------------------------------------------------------------+
  10. // | Copyright (c) 2004, bitweaver.org
  11. // +----------------------------------------------------------------------+
  12. // | All Rights Reserved. See below for details and a complete list of authors.
  13. // | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  14. // |
  15. // | For comments, please use phpdocu.sourceforge.net documentation standards!!!
  16. // | -> see http://phpdocu.sourceforge.net/
  17. // +----------------------------------------------------------------------+
  18. // | Authors: spider <spider@steelsun.com>
  19. // +----------------------------------------------------------------------+
  20.  
  21. /**
  22.  * required setup
  23.  */
  24. require_once( LIBERTY_PKG_PATH.'LibertyContent.php' );
  25.  
  26. /**
  27.  * LibertyAttachable class
  28.  *
  29.  * @package liberty
  30.  */
  31. class LibertyAttachable extends LibertyContent {
  32.     var $mContentId;
  33.     var $mStorage;
  34.  
  35.     function LibertyAttachable() {
  36.         parent::__construct();
  37.     }
  38.  
  39.     // {{{ =================== Deprecated Methods ====================
  40.     /**
  41.      * TODO: This code is old and is not used by any package in the bitweaver CVS anymore.
  42.      * We will clean up this code as soon as we are sure that noone is using this code.
  43.      * Please look for the closing tripple '}' brackets to see where this section ends.
  44.      */
  45.  
  46.     /**
  47.      * fully load content and insert any attachments in $this->mStorage
  48.      * allow an optional content_id to be passed in to ease legacy lib style objects (like blogs, articles, etc.)
  49.      *
  50.      * @param array $pContentId 
  51.      * @access public
  52.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  53.      * @deprecated deprecated since version 2.1.0-beta
  54.      */
  55.  
  56.     /**
  57.      * TODO: This code is old and is not used by any package in the bitweaver CVS anymore.
  58.      * We will clean up this code as soon as we migrated all legacy code
  59.      */
  60.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  61.         //deprecated( "This method has been replaced by a method in LibertyMime. Please try to migrate your code." );
  62.         // assume a derived class has joined on the liberty_content table, and loaded it's columns already.
  63.         global $gLibertySystem;
  64.         $conId = ( @$this->verifyId( $pContentId ) ? $pContentId : $this->mContentId );
  65.  
  66.         if( @$this->verifyId( $conId ) ) {
  67.             LibertyContent::load( $conId );
  68.             $query = "
  69.                 SELECT *
  70.                 FROM `".BIT_DB_PREFIX."liberty_attachments` la
  71.                 WHERE la.`content_id`=? ORDER BY la.`pos` ASC, la.`attachment_id` ASC";
  72.             if( $result = $this->mDb->query( $query,array( (int)$conId ))) {
  73.                 $this->mStorage = array();
  74.                 while( $row = $result->fetchRow() ) {
  75.                     if( $func = $gLibertySystem->getPluginFunction( $row['attachment_plugin_guid'], 'load_function', 'mime' )) {
  76.                         // this dummy is needed for forward compatability with LibertyMime plugins
  77.                         $dummy = array();
  78.                         $this->mStorage[$row['attachment_id']] = $func( $row, $dummy );
  79.                         //$this->mStorage[$row['attachment_id']]['is_primary'] = !empty( $row['primary_attachment_id'] );
  80.                     } else {
  81.                         print "No load_function for ".$row['attachment_plugin_guid']." ".$gLibertySystem->mPlugins[$row['attachment_plugin_guid']];
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         return( TRUE );
  87.     }
  88.  
  89.     /* store -- Stores any attachments
  90.      *
  91.      * pass $pParamHash['liberty_attachable']['skip_content_store'] == TRUE
  92.      * to avoid the underlying content store and simply store the attachments.
  93.      *
  94.      * verify() will shove things to store into $pParamHash['STORAGE'] to be
  95.      * gobbled up in this function.
  96.      *
  97.      * pass $pParamHash['liberty_attachable']['auto_primary'] == FALSE to turn off the auto
  98.      * primary on first attachment feature for a content type.
  99.      *
  100.      * @param hash $pParamHash The hash of arguments
  101.      *
  102.      * @deprecated deprecated since version 2.1.0-beta
  103.      */
  104.  
  105.     /**
  106.      * TODO: This code is old and is not used by any package in the bitweaver CVS anymore.
  107.      * We will clean up this code as soon as we migrated all legacy code
  108.      */
  109.     function store( &$pParamHash ) {
  110.         //deprecated( "This method has been replaced by a method in LibertyMime. Please try to migrate your code." );
  111.         global $gLibertySystem, $gBitSystem, $gBitUser;
  112.         $this->mDb->StartTrans();
  113.         if( LibertyAttachable::verify( $pParamHash ) && ( isset($pParamHash['skip_content_store']) ||  LibertyContent::store( $pParamHash ) ) ) {
  114.  
  115.             if(!empty( $pParamHash['STORAGE'] ) && count( $pParamHash['STORAGE'] ) ) {
  116.                 foreach( array_keys( $pParamHash['STORAGE'] ) as $guid ) {
  117.                     $storeRows = &$pParamHash['STORAGE'][$guid]; // short hand variable assignment
  118.                     // If it is empty then nothing more to do. Avoid error in foreach.
  119.                     if (empty($storeRows)) {
  120.                         continue;
  121.                     }
  122.                     foreach( $storeRows as $key => $value ) {
  123.                         $storeRow = &$pParamHash['STORAGE'][$guid][$key];
  124.                         $storeRow['plugin_guid'] = $guid;
  125.  
  126.                         if (!@BitBase::verifyId($pParamHash['content_id'])) {
  127.                             $storeRow['content_id'] = NULL;
  128.                         } else {
  129.                             $storeRow['content_id'] = $pParamHash['content_id']; // copy in content_id
  130.                         }
  131.  
  132.                         if (!empty($pParamHash['user_id'])) {
  133.                             $storeRow['user_id'] = $pParamHash['user_id']; // copy in the user_id
  134.                         } else {
  135.                             $storeRow['user_id'] = $gBitUser->mUserId;
  136.                         }
  137.  
  138.                         // do we have a verify function for this storage type, and do things verify?
  139.                         $verifyFunc = $gLibertySystem->getPluginFunction( $guid, 'verify_function' );
  140.                         if( $verifyFunc && $verifyFunc( $storeRow ) ) {
  141.                             // For backwards compatibility with a single upload.
  142.                             if( @BitBase::verifyId( $pParamHash['attachment_id'] )) {
  143.                                 $storeRow['upload']['attachment_id'] = $storeRow['attachment_id'] = $pParamHash['attachment_id'];
  144.                             } else if ( !isset($storeRow['skip_insert'] ) ) {
  145.                                 if ( defined( 'LINKED_ATTACHMENTS' ) && @BitBase::verifyId( $pParamHash['content_id'] ) ) {
  146.                                     $storeRow['upload']['attachment_id'] = $storeRow['attachment_id'] = $pParamHash['content_id'];
  147.                                 } else {
  148.                                     $storeRow['upload']['attachment_id'] = $storeRow['attachment_id'] =
  149.                                         defined( 'LINKED_ATTACHMENTS' ) ? $this->mDb->GenID( 'liberty_content_id_seq') : $this->mDb->GenID( 'liberty_attachments_id_seq' );
  150.                                 }
  151.                             }
  152.  
  153.                             // if we have uploaded a file, we can take care of that generically
  154.                             if( !empty( $storeRow['upload'] ) && is_array( $storeRow['upload'] ) && !empty( $storeRow['upload']['size'] ) ) {
  155.                                 if( empty( $storeRow['upload']['type'] ) ) {
  156.                                     $ext = substr( $storeRow['upload']['name'], strrpos( $storeRow['upload']['name'], '.' ) + 1 );
  157.                                     $storeRow['upload']['type'] = $gBitSystem->lookupMimeType( $ext );
  158.                                 }
  159.                                 $storeRow['upload']['dest_branch'] = $this->getStorageBranch( $storeRow['attachment_id'], $storeRow['user_id'], $this->getStorageSubDirName() );
  160.                                 if (!empty( $pParamHash['thumbnail_sizes'] ) ) {
  161.                                     $storeRow['upload']['thumbnail_sizes'] = $pParamHash['thumbnail_sizes'];
  162.                                 }
  163.                                 $storagePath = liberty_process_upload( $storeRow['upload'] );
  164.                                 // We're gonna store to local file system & liberty_files table
  165.                                 if( empty( $storagePath ) ) {
  166.                                     $this->mErrors['file'] = tra( "Could not store file" ).": ".$storeRow['upload']['name'].'.';
  167.                                     $storeRow['attachment_id'] = NULL;
  168.                                     $storeRow['upload']['attachment_id'] = NULL;
  169.                                 } else {
  170.                                     $storeRow['upload']['dest_file_path'] = $storagePath;
  171.                                 }
  172.                             }
  173.  
  174.                             if( @BitBase::verifyId( $storeRow['attachment_id'] ) && $storeFunc = $gLibertySystem->getPluginFunction( $storeRow['plugin_guid'], 'store_function' )) {
  175.                                 $this->mStorage = $storeFunc( $storeRow );
  176.                             }
  177.  
  178.                             // don't insert if we already have an entry with this attachment_id
  179.                             if( @BitBase::verifyId( $storeRow['attachment_id'] ) && !isset( $storeRow['skip_insert'] ) && !LibertyMime::loadAttachment( $storeRow['attachment_id'] )) {
  180.                                 $sql = "INSERT INTO `".BIT_DB_PREFIX."liberty_attachments` ( `content_id`, `attachment_id`, `attachment_plugin_guid`, `foreign_id`, `user_id` ) VALUES ( ?, ?, ?, ?, ? )";
  181.                                 $rs = $this->mDb->query( $sql, array( $storeRow['content_id'], $storeRow['attachment_id'], $storeRow['plugin_guid'], (int)$storeRow['foreign_id'], $storeRow['user_id'] ) );
  182.                             }
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.  
  188.             // set the primary attachment id
  189.             $this->setPrimaryAttachment(
  190.                 $pParamHash['liberty_attachments']['primary'],
  191.                 $pParamHash['content_id'],
  192.                 empty( $pParamHash['liberty_attachments']['auto_primary'] ) || $pParamHash['liberty_attachments']['auto_primary'] ? TRUE : FALSE
  193.                 );
  194.         }
  195.         $this->mDb->CompleteTrans();
  196.  
  197.         return( count( $this->mErrors ) == 0 );
  198.     }
  199.  
  200.     /**
  201.      * verifyAttachment
  202.      *
  203.      * @param array $pParamHash 
  204.      * @param array $pFile 
  205.      * @param array $pKey 
  206.      * @access public
  207.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  208.      * @deprecated deprecated since version 2.1.0-beta
  209.      */
  210.     function verifyAttachment( &$pParamHash, $pFile, $pKey ) {
  211.         //deprecated( "This method has been replaced by a method in LibertyMime. Please try to migrate your code." );
  212.         global $gBitSystem, $gBitUser, $gLibertySystem;
  213.  
  214.         if( !empty( $pFile ) && !empty( $pFile['size'] ) ) {
  215.             if( empty( $pParamHash['storage_guid'] )) {
  216.                 // only file format storage available at present
  217.                 $pParamHash['storage_guid'] = $storageGuid = PLUGIN_GUID_BIT_FILES;
  218.             } else {
  219.                 $storageGuid = $pParamHash['storage_guid'];
  220.             }
  221.  
  222.             if( !empty( $pFile['size'] ) ) {
  223.                 $this->extractMetaData( $pParamHash, $pFile );
  224.                 // meta data may be stupid and have stuffed title with all spaces
  225.                 if( !empty( $pParamHash['title'] ) ) {
  226.                     $pParamHash['title'] = trim( $pParamHash['title'] );
  227.                 }
  228.  
  229.                 // let's add a default title
  230.                 if( empty( $pParamHash['title'] ) && !empty( $pFile['name'] ) ) {
  231.                     if( preg_match( '/^[A-Z]:\\\/', $pFile['name'] ) ) {
  232.                         // MSIE shit file names if passthrough via gigaupload, etc.
  233.                         // basename will not work - see http://us3.php.net/manual/en/function.basename.php
  234.                         $tmp = preg_split("#[\\\]#",$pFile['name']);
  235.                         $defaultName = $tmp[count($tmp) - 1];
  236.                     } elseif( strpos( '.', $pFile['name'] ) ) {
  237.                         list( $defaultName, $ext ) = explode( '.', $pFile['name'] );
  238.                     } else {
  239.                         $defaultName = $pFile['name'];
  240.                     }
  241.                     $pParamHash['title'] = str_replace( '_', ' ', substr( $defaultName, 0, strrpos( $defaultName, '.' ) ) );
  242.                 }
  243.  
  244.  
  245.                 if ( !is_windows() ) {
  246.                     list( $pFile['name'], $pFile['type'] ) = $gBitSystem->verifyFileExtension( $pFile['tmp_name'], $pFile['name'] );
  247.                 } else {
  248.                     //$pFile['type'] = $gBitSystem->verifyMimeType( $pFile['tmp_name'] );
  249.                 }
  250.                 // clean out crap that can make life difficult in server maintenance
  251.                 $cleanedBaseName = preg_replace( '/[&\%:\/\\\]/', '', substr( $pFile['name'], 0, strrpos( $pFile['name'], '.' ) ) );
  252.                 $pFile['dest_base_name'] = $cleanedBaseName;
  253.                 $pFile['source_file'] = $pFile['tmp_name'];
  254.                 // lowercase all file extensions
  255.                 $pFile['name'] = $cleanedBaseName.strtolower( substr( $pFile['name'], strrpos( $pFile['name'], '.' ) ) );
  256.                 if (!isset($pParamHash['STORAGE'][$storageGuid])) {
  257.                     $pParamHash['STORAGE'][$storageGuid] = array();
  258.                 }
  259.                 $pParamHash['STORAGE'][$storageGuid][$pKey] = array('upload' => &$pFile);
  260.             }
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * verify - standard API method, with a twist. It will gobble up anything in $_FILES if available, unless an array of arrays is passed in to  $pParamHash['_files_override']
  266.      *
  267.      * @access private
  268.      * @author Christian Fowler<spider@steelsun.com>
  269.      * @param $pParamHash 
  270.      * @return FALSE if errors were present, TRUE meaning object is ready to store
  271.      * @deprecated deprecated since version 2.1.0-beta
  272.      */
  273.     function verify( &$pParamHash ) {
  274.         //deprecated( "This method has been replaced by a method in LibertyMime. Please try to migrate your code." );
  275.         global $gBitSystem, $gBitUser;
  276.         // check to see if we have any files to upload
  277.         if( isset( $pParamHash['_files_override'] ) ) {
  278.             // we have been passed in a manually stuffed files attachment, such as a custom uploader would have done.
  279.             // process this, and skip over $_FILES
  280.             $uploads = $pParamHash['_files_override'];
  281.         } elseif( !empty( $_FILES ) ) {
  282.             // we have some _FILES hanging around we will gobble up. This is inherently dagnerous chewing up a _FILES like this as
  283.             // it can cause premature storing of a _FILE if you are trying to store multiple pieces of content at once.
  284.             foreach( $_FILES as $key => $file ) {
  285.                 if( !empty( $file['name'] )) {
  286.                     $uploads[$key] = $file;
  287.                 }
  288.             }
  289.         }
  290.  
  291.         // don't check for p_liberty_attach_attachments permission on bitpermuser class so registration with avatar upload works
  292.         if( strtolower( get_class( $this )) == 'bitpermuser' ) {
  293.             $pParamHash['no_perm_check'] = TRUE;
  294.         }
  295.  
  296.         // check for the required permissions to upload a file to the liberty attachments area
  297.         if( !empty( $uploads ) && empty( $pParamHash['no_perm_check'] )) {
  298.             if( !$gBitUser->hasPermission( 'p_liberty_attach_attachments' )) {
  299.                 $this->mErrors['permission'] = tra( 'You do not have permission to upload attachments.' );
  300.             }
  301.         }
  302.  
  303.         if( !empty( $pParamHash['attachment_id'] ) && !$this->verifyId( $pParamHash['attachment_id'] ) ) {
  304.             $this->mErrors['file'] = tra('System Error: Non-numeric storage_id.');
  305.         }
  306.  
  307.         if( empty( $pParamHash['user_id'] ) ) {
  308.             // storage is always owned by the user that uploaded it!
  309.             // er... or at least admin if somehow we have a NULL mUserId - anon uploads maybe?
  310.             $pParamHash['user_id'] = @$this->verifyId( $gBitUser->mUserId ) ? $gBitUser->mUserId : ROOT_USER_ID;
  311.         }
  312.         if( empty( $pParamHash['process_storage'] ) ) {
  313.             $pParamHash['process_storage'] = NULL;
  314.         }
  315.  
  316.         if( empty( $pParamHash['subdir'] ) ) {
  317.             $pParamHash['subdir'] = 'files';
  318.         }
  319.  
  320.         if( !empty( $uploads ) ) {
  321.             foreach( array_keys( $uploads ) as $f ) {
  322.                 $this->verifyAttachment( $pParamHash, $uploads[$f], $f );
  323.             }
  324.         }
  325.  
  326.         // primary attachment. Allow 'none' to clear the primary.
  327.         if( !@BitBase::verifyId( $pParamHash['liberty_attachments']['primary'] ) && ( empty( $pParamHash['liberty_attachments']['primary'] ) || $pParamHash['liberty_attachments']['primary'] != 'none' ) ) {
  328.             $pParamHash['liberty_attachments']['primary'] = NULL;
  329.         }
  330.  
  331.         // if we have an error we get them all by checking parent classes for additional errors
  332.         if( count( $this->mErrors ) > 0 ){
  333.             parent::verify( $pParamHash );
  334.         }
  335.  
  336.         return ( count( $this->mErrors ) == 0 );
  337.     }
  338.  
  339.     /**
  340.      * extractMetaData extract meta data from images
  341.      *
  342.      * @param array $pParamHash 
  343.      * @param array $pFile 
  344.      * @access public
  345.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  346.      * @deprecated deprecated since version 2.1.0-beta
  347.      */
  348.     function extractMetaData( &$pParamHash, &$pFile ) {
  349.         //deprecated( "This method has been replaced by a method in LibertyMime. Please try to migrate your code." );
  350.         // Process a JPEG , jpeg_metadata_tk REQUIRES short_tags because that is the way it was written. feel free to fix something. XOXO spiderr
  351.         if( ini_get( 'short_open_tag' ) && function_exists( 'exif_read_data' ) && !empty( $pFile['tmp_name'] ) && strpos( strtolower($pFile['type']), 'jpeg' ) !== FALSE ) {
  352.             $exifHash = @exif_read_data( $pFile['tmp_name'], 0, true);
  353.  
  354.             // Change: Allow this example file to be easily relocatable - as of version 1.11
  355.             require_once UTIL_PKG_PATH.'jpeg_metadata_tk/JPEG.php';
  356.             require_once UTIL_PKG_PATH.'jpeg_metadata_tk/JFIF.php';
  357.             require_once UTIL_PKG_PATH.'jpeg_metadata_tk/PictureInfo.php';
  358.             require_once UTIL_PKG_PATH.'jpeg_metadata_tk/XMP.php';
  359.             require_once UTIL_PKG_PATH.'jpeg_metadata_tk/EXIF.php';
  360.  
  361.             // Retrieve the header information from the JPEG file
  362.             $jpeg_header_data = get_jpeg_header_data( $pFile['tmp_name'] );
  363.  
  364.             // Retrieve EXIF information from the JPEG file
  365.             $Exif_array = get_EXIF_JPEG( $pFile['tmp_name'] );
  366.  
  367.             // Retrieve XMP information from the JPEG file
  368.             $XMP_array = read_XMP_array_from_text( get_XMP_text( $jpeg_header_data ) );
  369.  
  370.             // Retrieve Photoshop IRB information from the JPEG file
  371.             $IRB_array = get_Photoshop_IRB( $jpeg_header_data );
  372.             if( !empty( $exifHash['IFD0']['Software'] ) && preg_match( '/photoshop/i', $exifHash['IFD0']['Software'] ) ) {
  373.                 require_once UTIL_PKG_PATH.'jpeg_metadata_tk/Photoshop_File_Info.php';
  374.                 // Retrieve Photoshop File Info from the three previous arrays
  375.                 $psFileInfo = get_photoshop_file_info( $Exif_array, $XMP_array, $IRB_array );
  376.  
  377.                 if( !empty( $psFileInfo['headline'] ) ) {
  378.                     if( empty( $pParamHash['title'] ) ) {
  379.                         $pParamHash['title'] = $psFileInfo['headline'];
  380.                     } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $psFileInfo['headline'] ) {
  381.                         $pParamHash['edit'] = $psFileInfo['headline'];
  382.                     }
  383.                 }
  384.                 if( !empty( $psFileInfo['caption'] ) ) {
  385.                     if( empty( $pParamHash['title'] ) ) {
  386.                         $pParamHash['title'] = $psFileInfo['caption'];
  387.                     } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $psFileInfo['caption'] ) {
  388.                         $pParamHash['edit'] = $psFileInfo['caption'];
  389.                     }
  390.                 }
  391.             }
  392.  
  393.             if( !empty( $exifHash['EXIF']['DateTimeOriginal'] ) ) {
  394.                 $pParamHash['event_time'] = strtotime( $exifHash['EXIF']['DateTimeOriginal'] );
  395.             }
  396.  
  397.             if( !empty( $exifHash['IFD0']['ImageDescription'] ) ) {
  398.                 if( empty( $pParamHash['title'] ) ) {
  399.                     $pParamHash['title'] = $exifHash['IFD0']['ImageDescription'];
  400.                 } elseif( empty( $pParamHash['edit'] ) && !$this->getField( 'data' ) && $pParamHash['title'] != $exifHash['IFD0']['ImageDescription'] ) {
  401.                     $pParamHash['edit'] = $exifHash['IFD0']['ImageDescription'];
  402.                 }
  403.             }
  404.         }
  405.     }
  406.  
  407.     // }}}
  408.  
  409.  
  410. }
  411.  
  412. // FIXME: this is really dirty and needs to go away from here
  413. // make sure LibertyMime is available during this transition phase
  414. // we need to call this down here since LM extends LA and can't be included before LA is available
  415. require_once( LIBERTY_PKG_PATH.'LibertyMime.php' );
  416.  
  417. /* vim: :set fdm=marker : */
  418. ?>

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