[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezbinaryfile/ -> ezbinaryfiletype.php (source)

   1  <?php
   2  //
   3  // Definition of eZBinaryFileType class
   4  //
   5  // Created on: <30-Apr-2002 13:06:21 bf>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZBinaryFileType ezbinaryfiletype.php
  31    \ingroup eZDatatype
  32    \brief The class eZBinaryFileType handles files and association with content objects
  33  
  34  */
  35  
  36  include_once ( "kernel/classes/ezdatatype.php" );
  37  include_once ( "kernel/classes/datatypes/ezbinaryfile/ezbinaryfile.php" );
  38  include_once ( "kernel/classes/ezbinaryfilehandler.php" );
  39  include_once ( "lib/ezfile/classes/ezfile.php" );
  40  include_once ( "lib/ezutils/classes/ezsys.php" );
  41  include_once ( "lib/ezutils/classes/ezmimetype.php" );
  42  include_once ( "lib/ezutils/classes/ezhttpfile.php" );
  43  
  44  define( 'EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_FIELD', 'data_int1' );
  45  define( 'EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_VARIABLE', '_ezbinaryfile_max_filesize_' );
  46  define( "EZ_DATATYPESTRING_BINARYFILE", "ezbinaryfile" );
  47  
  48  class eZBinaryFileType extends eZDataType
  49  {
  50      function eZBinaryFileType()
  51      {
  52          $this->eZDataType( EZ_DATATYPESTRING_BINARYFILE, ezi18n( 'kernel/classes/datatypes', "File", 'Datatype name' ),
  53                             array( 'serialize_supported' => true ) );
  54      }
  55  
  56      /*!
  57       \return the binary file handler.
  58      */
  59      function &fileHandler()
  60      {
  61          return eZBinaryFileHandler::instance();
  62      }
  63  
  64      /*!
  65       \reimp
  66       \return the template name which the handler decides upon.
  67      */
  68      function &viewTemplate( &$contentobjectAttribute )
  69      {
  70          $handler =& $this->fileHandler();
  71          $handlerTemplate = $handler->viewTemplate( $contentobjectAttribute );
  72          $template = $this->DataTypeString;
  73          if ( $handlerTemplate !== false )
  74              $template .= '_' . $handlerTemplate;
  75          return $template;
  76      }
  77  
  78      /*!
  79       \return the template name to use for editing the attribute.
  80       \note Default is to return the datatype string which is OK
  81             for most datatypes, if you want dynamic templates
  82             reimplement this function and return a template name.
  83       \note The returned template name does not include the .tpl extension.
  84       \sa viewTemplate, informationTemplate
  85      */
  86      function &editTemplate( &$contentobjectAttribute )
  87      {
  88          $handler =& $this->fileHandler();
  89          $handlerTemplate = $handler->editTemplate( $contentobjectAttribute );
  90          $template = $this->DataTypeString;
  91          if ( $handlerTemplate !== false )
  92              $template .= '_' . $handlerTemplate;
  93          return $template;
  94      }
  95  
  96      /*!
  97       \return the template name to use for information collection for the attribute.
  98       \note Default is to return the datatype string which is OK
  99             for most datatypes, if you want dynamic templates
 100             reimplement this function and return a template name.
 101       \note The returned template name does not include the .tpl extension.
 102       \sa viewTemplate, editTemplate
 103      */
 104      function &informationTemplate( &$contentobjectAttribute )
 105      {
 106          $handler =& $this->fileHandler();
 107          $handlerTemplate = $handler->informationTemplate( $contentobjectAttribute );
 108          $template = $this->DataTypeString;
 109          if ( $handlerTemplate !== false )
 110              $template .= '_' . $handlerTemplate;
 111          return $template;
 112      }
 113  
 114      /*!
 115       Sets value according to current version
 116      */
 117      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 118      {
 119          if ( $currentVersion != false )
 120          {
 121              $contentObjectAttributeID = $originalContentObjectAttribute->attribute( "id" );
 122              $version = $contentObjectAttribute->attribute( "version" );
 123              $oldfile = eZBinaryFile::fetch( $contentObjectAttributeID, $currentVersion );
 124              if ( $oldfile != null )
 125              {
 126                  $oldfile->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
 127                  $oldfile->setAttribute( "version",  $version );
 128                  $oldfile->store();
 129              }
 130          }
 131      }
 132  
 133      /*!
 134       Delete stored attribute
 135      */
 136      function deleteStoredObjectAttribute( &$contentObjectAttribute, $version = null )
 137      {
 138          $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
 139          $sys =& eZSys::instance();
 140          $storage_dir = $sys->storageDirectory();
 141  
 142          require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 143          if ( $version == null )
 144          {
 145              $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
 146              eZBinaryFile::remove( $contentObjectAttributeID, null );
 147  
 148              foreach ( array_keys( $binaryFiles ) as $key )
 149              {
 150                  $binaryFile =& $binaryFiles[$key];
 151  
 152                  $mimeType =  $binaryFile->attribute( "mime_type" );
 153                  list( $prefix, $suffix ) = split ('[/]', $mimeType );
 154                  $orig_dir = $storage_dir . '/original/' . $prefix;
 155                  $fileName = $binaryFile->attribute( "filename" );
 156  
 157                  // Check if there are any other records in ezbinaryfile that point to that fileName.
 158                  $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
 159  
 160                  $filePath = $orig_dir . "/" . $fileName;
 161                  $file = eZClusterFileHandler::instance( $filePath );
 162  
 163                  if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
 164                      $file->delete();
 165              }
 166          }
 167          else
 168          {
 169              $count = 0;
 170              $binaryFile = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
 171              if ( $binaryFile != null )
 172              {
 173                  $mimeType =  $binaryFile->attribute( "mime_type" );
 174                  list( $prefix, $suffix ) = split ('[/]', $mimeType );
 175                  $orig_dir = $storage_dir . "/original/" . $prefix;
 176                  $fileName = $binaryFile->attribute( "filename" );
 177  
 178                  eZBinaryFile::remove( $contentObjectAttributeID, $version );
 179  
 180                  // Check if there are any other records in ezbinaryfile that point to that fileName.
 181                  $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
 182  
 183                  $filePath = $orig_dir . "/" . $fileName;
 184                  $file = eZClusterFileHandler::instance( $filePath );
 185  
 186                  if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
 187                      $file->delete();
 188              }
 189          }
 190      }
 191  
 192      /*!
 193       Checks if file uploads are enabled, if not it gives a warning.
 194      */
 195      function checkFileUploads()
 196      {
 197          $isFileUploadsEnabled = ini_get( 'file_uploads' ) != 0;
 198          if ( !$isFileUploadsEnabled )
 199          {
 200              $isFileWarningAdded =& $GLOBALS['eZBinaryFileTypeWarningAdded'];
 201              if ( !isset( $isFileWarningAdded ) or
 202                   !$isFileWarningAdded )
 203              {
 204                  eZAppendWarningItem( array( 'error' => array( 'type' => 'kernel',
 205                                                                'number' => EZ_ERROR_KERNEL_NOT_AVAILABLE ),
 206                                              'text' => ezi18n( 'kernel/classes/datatypes',
 207                                                                'File uploading is not enabled. Please contact the site administrator to enable it.' ) ) );
 208                  $isFileWarningAdded = true;
 209              }
 210          }
 211      }
 212  
 213      /*!
 214       Validates the input and returns true if the input was
 215       valid for this datatype.
 216      */
 217      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 218      {
 219          eZBinaryFileType::checkFileUploads();
 220          $classAttribute =& $contentObjectAttribute->contentClassAttribute();
 221          $mustUpload = false;
 222          $httpFileName = $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" );
 223          $maxSize = 1024 * 1024 * $classAttribute->attribute( EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_FIELD );
 224  
 225          if ( $contentObjectAttribute->validateIsRequired() )
 226          {
 227              $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
 228              $version = $contentObjectAttribute->attribute( "version" );
 229              $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
 230              if ( $binary === null )
 231              {
 232                  $mustUpload = true;
 233              }
 234          }
 235  
 236          $canFetchResult = eZHTTPFile::canFetch( $httpFileName, $maxSize );
 237          if ( $mustUpload && $canFetchResult == EZ_UPLOADEDFILE_DOES_NOT_EXIST )
 238          {
 239              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 240                                                                   'A valid file is required.' ) );
 241              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 242          }
 243          if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_PHP_LIMIT )
 244          {
 245              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 246                  'The size of the uploaded file exceeds the limit set by the upload_max_filesize directive in php.ini.' ) );
 247              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 248          }
 249          if ( $canFetchResult == EZ_UPLOADEDFILE_EXCEEDS_MAX_SIZE )
 250          {
 251              $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 252                                                                   'The size of the uploaded file exceeds the maximum upload size: %1 bytes.' ), $maxSize );
 253              return EZ_INPUT_VALIDATOR_STATE_INVALID;
 254          }
 255          return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 256      }
 257  
 258      /*!
 259       Fetches the http post var integer input and stores it in the data instance.
 260      */
 261      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 262      {
 263          eZBinaryFileType::checkFileUploads();
 264          if ( !eZHTTPFile::canFetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) ) )
 265              return false;
 266  
 267          $binaryFile =& eZHTTPFile::fetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) );
 268  
 269          $contentObjectAttribute->setContent( $binaryFile );
 270  
 271          //$binaryFile =& $contentObjectAttribute->content();
 272  
 273          if ( get_class( $binaryFile ) == "ezhttpfile" )
 274          {
 275              $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
 276              $version = $contentObjectAttribute->attribute( "version" );
 277  
 278              /*
 279              $mimeObj = new  eZMimeType();
 280              $mimeData = $mimeObj->findByURL( $binaryFile->attribute( "original_filename" ), true );
 281              $mime = $mimeData['name'];
 282              */
 283  
 284              $mimeData = eZMimeType::findByFileContents( $binaryFile->attribute( "original_filename" ) );
 285              $mime = $mimeData['name'];
 286  
 287              if ( $mime == '' )
 288              {
 289                  $mime = $binaryFile->attribute( "mime_type" );
 290              }
 291              $extension = preg_replace('/.*\.(.+?)$/', '\\1', $binaryFile->attribute( "original_filename" ) );
 292              $binaryFile->setMimeType( $mime );
 293              if ( !$binaryFile->store( "original", $extension ) )
 294              {
 295                  eZDebug::writeError( "Failed to store http-file: " . $binaryFile->attribute( "original_filename" ),
 296                                       "eZBinaryFileType" );
 297                  return false;
 298              }
 299  
 300              $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
 301              if ( $binary === null )
 302                  $binary = eZBinaryFile::create( $contentObjectAttributeID, $version );
 303  
 304              $orig_dir = $binaryFile->storageDir( "original" );
 305  
 306              $binary->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
 307              $binary->setAttribute( "version", $version );
 308              $binary->setAttribute( "filename", basename( $binaryFile->attribute( "filename" ) ) );
 309              $binary->setAttribute( "original_filename", $binaryFile->attribute( "original_filename" ) );
 310              $binary->setAttribute( "mime_type", $mime );
 311  
 312              $binary->store();
 313  
 314              // VS-DBFILE
 315  
 316              require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 317              $filePath = $binaryFile->attribute( 'filename' );
 318              $fileHandler = eZClusterFileHandler::instance();
 319              $fileHandler->fileStore( $filePath, 'binaryfile', true, $mime );
 320  
 321              $contentObjectAttribute->setContent( $binary );
 322          }
 323          return true;
 324      }
 325  
 326      /*!
 327       Does nothing, since the file has been stored. See fetchObjectAttributeHTTPInput for the actual storing.
 328      */
 329      function storeObjectAttribute( &$contentObjectAttribute )
 330      {
 331      }
 332  
 333      function customObjectAttributeHTTPAction( $http, $action, &$contentObjectAttribute )
 334      {
 335          eZBinaryFileType::checkFileUploads();
 336          if( $action == "delete_binary" )
 337          {
 338              $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
 339              $version = $contentObjectAttribute->attribute( "version" );
 340              $this->deleteStoredObjectAttribute( $contentObjectAttribute, $version );
 341          }
 342      }
 343  
 344      /*!
 345       \reimp
 346       HTTP file insertion is supported.
 347      */
 348      function isHTTPFileInsertionSupported()
 349      {
 350          return true;
 351      }
 352  
 353      /*!
 354       \reimp
 355       HTTP file insertion is supported.
 356      */
 357      function isRegularFileInsertionSupported()
 358      {
 359          return true;
 360      }
 361  
 362      /*!
 363       \reimp
 364       Inserts the file using the eZBinaryFile class.
 365      */
 366      function insertHTTPFile( &$object, $objectVersion, $objectLanguage,
 367                               &$objectAttribute, &$httpFile, $mimeData,
 368                               &$result )
 369      {
 370          $result = array( 'errors' => array(),
 371                           'require_storage' => false );
 372          $errors =& $result['errors'];
 373          $attributeID = $objectAttribute->attribute( 'id' );
 374  
 375          $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
 376          if ( $binary === null )
 377              $binary = eZBinaryFile::create( $attributeID, $objectVersion );
 378  
 379          $httpFile->setMimeType( $mimeData['name'] );
 380  
 381          $db =& eZDB::instance();
 382          $db->begin();
 383  
 384          if ( !$httpFile->store( "original", false, false ) )
 385          {
 386              $errors[] = array( 'description' => ezi18n( 'kernel/classe/datatypes/ezbinaryfile',
 387                                                          'Failed to store file %filename. Please contact the site administrator.', null,
 388                                                          array( '%filename' => $httpFile->attribute( "original_filename" ) ) ) );
 389              return false;
 390          }
 391  
 392  
 393          $filePath = $binary->attribute( 'filename' );
 394  
 395          $binary->setAttribute( "contentobject_attribute_id", $attributeID );
 396          $binary->setAttribute( "version", $objectVersion );
 397          $binary->setAttribute( "filename", basename( $httpFile->attribute( "filename" ) ) );
 398          $binary->setAttribute( "original_filename", $httpFile->attribute( "original_filename" ) );
 399          $binary->setAttribute( "mime_type", $mimeData['name'] );
 400  
 401          $binary->store();
 402  
 403          // SP-DBFILE
 404  
 405          require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 406          $filePath = $httpFile->attribute( 'filename' );
 407          $fileHandler = eZClusterFileHandler::instance();
 408          $fileHandler->fileStore( $filePath, 'binaryfile', true, $mimeData['name'] );
 409          $objectAttribute->setContent( $binary );
 410  
 411          $db->commit();
 412  
 413          $objectAttribute->setContent( $binary );
 414  
 415          return true;
 416      }
 417  
 418      /*!
 419       \reimp
 420       Inserts the file using the eZBinaryFile class.
 421      */
 422      function insertRegularFile( &$object, $objectVersion, $objectLanguage,
 423                                  &$objectAttribute, $filePath,
 424                                  &$result )
 425      {
 426          $result = array( 'errors' => array(),
 427                           'require_storage' => false );
 428          $errors =& $result['errors'];
 429          $attributeID = $objectAttribute->attribute( 'id' );
 430  
 431          $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
 432          if ( $binary === null )
 433              $binary = eZBinaryFile::create( $attributeID, $objectVersion );
 434  
 435          $fileName = basename( $filePath );
 436          $mimeData = eZMimeType::findByFileContents( $filePath );
 437          $storageDir = eZSys::storageDirectory();
 438          list( $group, $type ) = explode( '/', $mimeData['name'] );
 439          $destination = $storageDir . '/original/' . $group;
 440          $oldumask = umask( 0 );
 441          if ( !eZDir::mkdir( $destination, false, true ) )
 442          {
 443              umask( $oldumask );
 444              return false;
 445          }
 446          umask( $oldumask );
 447          $destFileName = md5( basename( $fileName ) . microtime() . mt_rand() );
 448          $destination = $destination . '/' . $destFileName;
 449          copy( $filePath, $destination );
 450  
 451          // SP-DBFILE
 452          require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 453          $fileHandler = eZClusterFileHandler::instance();
 454          $fileHandler->fileStore( $destination, 'binaryfile', true, $mimeData['name'] );
 455  
 456  
 457          $binary->setAttribute( "contentobject_attribute_id", $attributeID );
 458          $binary->setAttribute( "version", $objectVersion );
 459          $binary->setAttribute( "filename", $destFileName );
 460          $binary->setAttribute( "original_filename", $fileName );
 461          $binary->setAttribute( "mime_type", $mimeData['name'] );
 462  
 463          $binary->store();
 464  
 465          $objectAttribute->setContent( $binary );
 466          return true;
 467      }
 468  
 469      /*!
 470        \reimp
 471        We support file information
 472      */
 473      function hasStoredFileInformation( &$object, $objectVersion, $objectLanguage,
 474                                         &$objectAttribute )
 475      {
 476          return true;
 477      }
 478  
 479      /*!
 480        \reimp
 481        Extracts file information for the binaryfile entry.
 482      */
 483      function storedFileInformation( &$object, $objectVersion, $objectLanguage,
 484                                      &$objectAttribute )
 485      {
 486          $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
 487                                              $objectAttribute->attribute( "version" ) );
 488          if ( $binaryFile )
 489          {
 490              return $binaryFile->storedFileInfo();
 491          }
 492          return false;
 493      }
 494      /*!
 495        \reimp
 496        Updates download count for binary file.
 497      */
 498      function handleDownload( &$object, $objectVersion, $objectLanguage,
 499                               &$objectAttribute )
 500      {
 501          $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
 502                                              $objectAttribute->attribute( "version" ) );
 503  
 504          $contentObjectAttributeID = $objectAttribute->attribute( 'id' );
 505          $version =  $objectAttribute->attribute( "version" );
 506  
 507          if ( $binaryFile )
 508          {
 509              $db =& eZDB::instance();
 510              $db->query( "UPDATE ezbinaryfile SET download_count=(download_count+1)
 511                           WHERE
 512                           contentobject_attribute_id=$contentObjectAttributeID AND version=$version" );
 513              return true;
 514          }
 515          return false;
 516      }
 517  
 518      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 519      {
 520          $filesizeName = $base . EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_VARIABLE . $classAttribute->attribute( 'id' );
 521          if ( $http->hasPostVariable( $filesizeName ) )
 522          {
 523              $filesizeValue = $http->postVariable( $filesizeName );
 524              $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_FIELD, $filesizeValue );
 525          }
 526      }
 527      /*!
 528       Returns the object title.
 529      */
 530      function title( &$contentObjectAttribute,  $name = "original_filename" )
 531      {
 532          $value = false;
 533          $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( 'id' ),
 534                                             $contentObjectAttribute->attribute( 'version' ) );
 535          if ( is_object( $binaryFile ) )
 536              $value = $binaryFile->attribute( $name );
 537  
 538          return $value;
 539      }
 540  
 541      function hasObjectAttributeContent( &$contentObjectAttribute )
 542      {
 543          $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
 544                                              $contentObjectAttribute->attribute( "version" ) );
 545          if ( !$binaryFile )
 546              return false;
 547          return true;
 548      }
 549  
 550      function &objectAttributeContent( $contentObjectAttribute )
 551      {
 552          $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
 553                                              $contentObjectAttribute->attribute( "version" ) );
 554          if ( !$binaryFile )
 555          {
 556              $attrValue = false;
 557              return $attrValue;
 558          }
 559          return $binaryFile;
 560      }
 561  
 562      /*!
 563       \reimp
 564      */
 565      function isIndexable()
 566      {
 567          return true;
 568      }
 569  
 570      function &metaData( &$contentObjectAttribute )
 571      {
 572          $binaryFile =& $contentObjectAttribute->content();
 573  
 574          $metaData = "";
 575          if ( get_class( $binaryFile ) == "ezbinaryfile" )
 576          {
 577              $metaData = $binaryFile->metaData();
 578          }
 579          return $metaData;
 580      }
 581  
 582      /*!
 583       \reimp
 584      */
 585      function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 586      {
 587          $maxSize = $classAttribute->attribute( EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_FIELD );
 588          $attributeParametersNode->appendChild( eZDOMDocument::createElementTextNode( 'max-size', $maxSize,
 589                                                                                       array( 'unit-size' => 'mega' ) ) );
 590      }
 591  
 592      /*!
 593       \reimp
 594      */
 595      function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
 596      {
 597          $maxSize = $attributeParametersNode->elementTextContentByName( 'max-size' );
 598          $sizeNode = $attributeParametersNode->elementByName( 'max-size' );
 599          $unitSize = $sizeNode->attributeValue( 'unit-size' );
 600          $classAttribute->setAttribute( EZ_DATATYPESTRING_MAX_BINARY_FILESIZE_FIELD, $maxSize );
 601      }
 602  
 603      /*!
 604       \return string representation of an contentobjectattribute data for simplified export
 605  
 606      */
 607      function toString( $objectAttribute )
 608      {
 609          $binaryFile = $objectAttribute->content();
 610  
 611          if ( is_object( $binaryFile ) )
 612          {
 613              return implode( '|', array( $binaryFile->attribute( 'filepath' ), $binaryFile->attribute( 'original_filename' ) ) );
 614          }
 615          else
 616              return '';
 617      }
 618  
 619  
 620  
 621      function fromString( &$objectAttribute, $string )
 622      {
 623          if( !$string )
 624              return true;
 625  
 626          $result = array();
 627          return $this->insertRegularFile( $objectAttribute->attribute( 'object' ),
 628                                           $objectAttribute->attribute( 'version' ),
 629                                           $objectAttribute->attribute( 'language_code' ),
 630                                           $objectAttribute,
 631                                           $string,
 632                                           $result );
 633      }
 634  
 635      /*!
 636       \param package
 637       \param content attribute
 638  
 639       \return a DOM representation of the content object attribute
 640      */
 641      function serializeContentObjectAttribute( &$package, &$objectAttribute )
 642      {
 643          $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
 644  
 645          $binaryFile = $objectAttribute->attribute( 'content' );
 646          if ( is_object( $binaryFile ) )
 647          {
 648              $fileKey = md5( mt_rand() );
 649              $package->appendSimpleFile( $fileKey, $binaryFile->attribute( 'filepath' ) );
 650  
 651              $fileNode = eZDOMDocument::createElementNode( 'binary-file' );
 652              $fileNode->appendAttribute( eZDOMDocument::createAttributeNode( 'filesize', $binaryFile->attribute( 'filesize' ) ) );
 653              $fileNode->appendAttribute( eZDOMDocument::createAttributeNode( 'filename', $binaryFile->attribute( 'filename' ) ) );
 654              $fileNode->appendAttribute( eZDOMDocument::createAttributeNode( 'original-filename', $binaryFile->attribute( 'original_filename' ) ) );
 655              $fileNode->appendAttribute( eZDOMDocument::createAttributeNode( 'mime-type', $binaryFile->attribute( 'mime_type' ) ) );
 656              $fileNode->appendAttribute( eZDOMDocument::createAttributeNode( 'filekey', $fileKey ) );
 657              $node->appendChild( $fileNode );
 658          }
 659  
 660          return $node;
 661      }
 662  
 663      /*!
 664       \reimp
 665       \param package
 666       \param contentobject attribute object
 667       \param ezdomnode object
 668      */
 669      function unserializeContentObjectAttribute( &$package, &$objectAttribute, $attributeNode )
 670      {
 671          $fileNode = $attributeNode->elementByName( 'binary-file' );
 672          if ( !is_object( $fileNode ) or !$fileNode->hasAttributes() )
 673          {
 674              return;
 675          }
 676  
 677          $binaryFile = eZBinaryFile::create( $objectAttribute->attribute( 'id' ), $objectAttribute->attribute( 'version' ) );
 678  
 679          $sourcePath = $package->simpleFilePath( $fileNode->attributeValue( 'filekey' ) );
 680  
 681          if ( !file_exists( $sourcePath ) )
 682          {
 683              eZDebug::writeError( "The file '$sourcePath' does not exist, cannot initialize file attribute with it",
 684                                   'eZBinaryFileType::unserializeContentObjectAttribute' );
 685              return false;
 686          }
 687  
 688          include_once ( 'lib/ezfile/classes/ezdir.php' );
 689          $ini =& eZINI::instance();
 690          $mimeType = $fileNode->attributeValue( 'mime-type' );
 691          list( $mimeTypeCategory, $mimeTypeName ) = explode( '/', $mimeType );
 692          $destinationPath = eZSys::storageDirectory() . '/original/' . $mimeTypeCategory . '/';
 693          if ( !file_exists( $destinationPath ) )
 694          {
 695              $oldumask = umask( 0 );
 696              if ( !eZDir::mkdir( $destinationPath, eZDir::directoryPermission(), true ) )
 697              {
 698                  umask( $oldumask );
 699                  return false;
 700              }
 701              umask( $oldumask );
 702          }
 703  
 704          $basename = basename( $fileNode->attributeValue( 'filename' ) );
 705          while ( file_exists( $destinationPath . $basename ) )
 706          {
 707              $basename = substr( md5( mt_rand() ), 0, 8 ) . '.' . eZFile::suffix( $fileNode->attributeValue( 'filename' ) );
 708          }
 709  
 710          include_once ( 'lib/ezfile/classes/ezfilehandler.php' );
 711          eZFileHandler::copy( $sourcePath, $destinationPath . $basename );
 712          eZDebug::writeNotice( 'Copied: ' . $sourcePath . ' to: ' . $destinationPath . $basename,
 713                                'eZBinaryFileType::unserializeContentObjectAttribute()' );
 714  
 715          $binaryFile->setAttribute( 'contentobject_attribute_id', $objectAttribute->attribute( 'id' ) );
 716          $binaryFile->setAttribute( 'filename', $basename );
 717          $binaryFile->setAttribute( 'original_filename', $fileNode->attributeValue( 'original-filename' ) );
 718          $binaryFile->setAttribute( 'mime_type', $fileNode->attributeValue( 'mime-type' ) );
 719  
 720          $binaryFile->store();
 721  
 722          // VS-DBFILE + SP DBFile fix
 723  
 724          require_once ( 'kernel/classes/ezclusterfilehandler.php' );
 725          $fileHandler = eZClusterFileHandler::instance();
 726          $fileHandler->fileStore( $destinationPath . $basename, 'binaryfile', true );
 727      }
 728  
 729  }
 730  
 731  eZDataType::register( EZ_DATATYPESTRING_BINARYFILE, "ezbinaryfiletype" );
 732  
 733  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7