Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezbinaryfiletype.php
Go to the documentation of this file.
1 <?php
18 class eZBinaryFileType extends eZDataType
19 {
20 const MAX_FILESIZE_FIELD = 'data_int1';
21
22 const MAX_FILESIZE_VARIABLE = '_ezbinaryfile_max_filesize_';
23
24 const DATA_TYPE_STRING = "ezbinaryfile";
25
26 public function __construct()
27 {
28 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', "File", 'Datatype name' ),
29 array( 'serialize_supported' => true ) );
30 $this->FileExtensionBlackListValidator = new eZFileExtensionBlackListValidator();
31 }
32
36 function fileHandler()
37 {
38 return eZBinaryFileHandler::instance();
39 }
40
44 function viewTemplate( $contentobjectAttribute )
45 {
46 $handler = $this->fileHandler();
47 $handlerTemplate = $handler->viewTemplate( $contentobjectAttribute );
48 $template = $this->DataTypeString;
49 if ( $handlerTemplate !== false )
50 $template .= '_' . $handlerTemplate;
51 return $template;
52 }
53
62 function editTemplate( $contentobjectAttribute )
63 {
64 $handler = $this->fileHandler();
65 $handlerTemplate = $handler->editTemplate( $contentobjectAttribute );
66 $template = $this->DataTypeString;
67 if ( $handlerTemplate !== false )
68 $template .= '_' . $handlerTemplate;
69 return $template;
70 }
71
80 function informationTemplate( $contentobjectAttribute )
81 {
82 $handler = $this->fileHandler();
83 $handlerTemplate = $handler->informationTemplate( $contentobjectAttribute );
84 $template = $this->DataTypeString;
85 if ( $handlerTemplate !== false )
86 $template .= '_' . $handlerTemplate;
87 return $template;
88 }
89
93 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
94 {
95 if ( $currentVersion != false )
96 {
97 $contentObjectAttributeID = $originalContentObjectAttribute->attribute( "id" );
98 $version = $contentObjectAttribute->attribute( "version" );
99 $oldfile = eZBinaryFile::fetch( $contentObjectAttributeID, $currentVersion );
100 if ( $oldfile != null )
101 {
102 $oldfile->setAttribute( 'contentobject_attribute_id', $contentObjectAttribute->attribute( 'id' ) );
103 $oldfile->setAttribute( "version", $version );
104 $oldfile->store();
105 }
106 }
107 }
108
113 function trashStoredObjectAttribute( $contentObjectAttribute, $version = null )
114 {
115 $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
116 $sys = eZSys::instance();
117 $storage_dir = $sys->storageDirectory();
118
119 if ( $version == null )
120 $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
121 else
122 $binaryFiles = array( eZBinaryFile::fetch( $contentObjectAttributeID, $version ) );
123
124 foreach ( $binaryFiles as $binaryFile )
125 {
126 if ( $binaryFile == null )
127 continue;
128 $mimeType = $binaryFile->attribute( "mime_type" );
129 list( $prefix, $suffix ) = explode( '/', $mimeType );
130 $orig_dir = $storage_dir . '/original/' . $prefix;
131 $fileName = $binaryFile->attribute( "filename" );
132
133 // Check if there are any other records in ezbinaryfile that point to that fileName.
134 $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
135
136 $filePath = $orig_dir . "/" . $fileName;
137 $file = eZClusterFileHandler::instance( $filePath );
138
139 if ( $file->exists() and count( $binaryObjectsWithSameFileName ) <= 1 )
140 {
141 // create dest filename in the same manner as eZHTTPFile::store()
142 // grab file's suffix
143 $fileSuffix = eZFile::suffix( $fileName );
144 // prepend dot
145 if ( $fileSuffix )
146 $fileSuffix = '.' . $fileSuffix;
147 // grab filename without suffix
148 $fileBaseName = basename( $fileName, $fileSuffix );
149 // create dest filename
150 $newFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
151 $newFilePath = $orig_dir . "/" . $newFileName;
152
153 // rename the file, and update the database data
154 $file->move( $newFilePath );
155 $binaryFile->setAttribute( 'filename', $newFileName );
156 $binaryFile->store();
157 }
158 }
159 }
160
164 function deleteStoredObjectAttribute( $contentObjectAttribute, $version = null )
165 {
166 $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
167 $sys = eZSys::instance();
168 $storage_dir = $sys->storageDirectory();
169
170 if ( $version == null )
171 {
172 $binaryFiles = eZBinaryFile::fetch( $contentObjectAttributeID );
173 eZBinaryFile::removeByID( $contentObjectAttributeID, null );
174
175 foreach ( $binaryFiles as $binaryFile )
176 {
177 $mimeType = $binaryFile->attribute( "mime_type" );
178 list( $prefix, $suffix ) = explode('/', $mimeType );
179 $orig_dir = $storage_dir . '/original/' . $prefix;
180 $fileName = $binaryFile->attribute( "filename" );
181
182 // Check if there are any other records in ezbinaryfile that point to that fileName.
183 $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
184
185 $filePath = $orig_dir . "/" . $fileName;
186 $file = eZClusterFileHandler::instance( $filePath );
187
188 if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
189 $file->delete();
190 }
191 }
192 else
193 {
194 $count = 0;
195 $binaryFile = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
196 if ( $binaryFile != null )
197 {
198 $mimeType = $binaryFile->attribute( "mime_type" );
199 list( $prefix, $suffix ) = explode('/', $mimeType );
200 $orig_dir = $storage_dir . "/original/" . $prefix;
201 $fileName = $binaryFile->attribute( "filename" );
202
203 eZBinaryFile::removeByID( $contentObjectAttributeID, $version );
204
205 // Check if there are any other records in ezbinaryfile that point to that fileName.
206 $binaryObjectsWithSameFileName = eZBinaryFile::fetchByFileName( $fileName );
207
208 $filePath = $orig_dir . "/" . $fileName;
209 $file = eZClusterFileHandler::instance( $filePath );
210
211 if ( $file->exists() and count( $binaryObjectsWithSameFileName ) < 1 )
212 $file->delete();
213 }
214 }
215 }
216
220 function checkFileUploads()
221 {
222 $isFileUploadsEnabled = ini_get( 'file_uploads' ) != 0;
223 if ( !$isFileUploadsEnabled )
224 {
225 $isFileWarningAdded = $GLOBALS['eZBinaryFileTypeWarningAdded'];
226 if ( !isset( $isFileWarningAdded ) or
227 !$isFileWarningAdded )
228 {
229 eZAppendWarningItem( array( 'error' => array( 'type' => 'kernel',
230 'number' => eZError::KERNEL_NOT_AVAILABLE ),
231 'text' => ezpI18n::tr( 'kernel/classes/datatypes',
232 'File uploading is not enabled. Please contact the site administrator to enable it.' ) ) );
233 $GLOBALS['eZBinaryFileTypeWarningAdded'] = true;
234 }
235 }
236 }
237
242 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
243 {
244 eZBinaryFileType::checkFileUploads();
245 $classAttribute = $contentObjectAttribute->contentClassAttribute();
246 $mustUpload = false;
247 $httpFileName = $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" );
248 $maxSize = 1024 * 1024 * $classAttribute->attribute( self::MAX_FILESIZE_FIELD );
249
250 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
251 $version = $contentObjectAttribute->attribute( 'version' );
252 $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
253 $extensionsBlackList = implode(', ', $this->FileExtensionBlackListValidator->extensionsBlackList() );
254 if ( $binary === null )
255 {
256 if ( $contentObjectAttribute->validateIsRequired() )
257 {
258 $mustUpload = true;
259 }
260 }
261 else
262 {
263 $state = $this->FileExtensionBlackListValidator->validate( $binary->attribute( 'filename' ) );
264 if ( $state === eZInputValidator::STATE_INVALID || $state === eZInputValidator::STATE_INTERMEDIATE )
265 {
266 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
267 "A valid file is required. The following file extensions are blacklisted: $extensionsBlackList" ) );
268 return eZInputValidator::STATE_INVALID;
269 }
270 }
271
272 if ( isset( $_FILES[$httpFileName] ) && $_FILES[$httpFileName]['tmp_name'] !== '')
273 {
274 $state = $this->FileExtensionBlackListValidator->validate( $_FILES[$httpFileName]['name'] );
275 if ( $state === eZInputValidator::STATE_INVALID || $state === eZInputValidator::STATE_INTERMEDIATE )
276 {
277 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
278 "A valid file is required. The following file extensions are blacklisted: $extensionsBlackList" ) );
279 return eZInputValidator::STATE_INVALID;
280 }
281 }
282
283 $canFetchResult = eZHTTPFile::canFetch( $httpFileName, $maxSize );
284 if ( $mustUpload && $canFetchResult == eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST )
285 {
286 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
287 'A valid file is required.' ) );
288 return eZInputValidator::STATE_INVALID;
289 }
290 if ( $canFetchResult == eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT )
291 {
292 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
293 'The size of the uploaded file exceeds the limit set by the upload_max_filesize directive in php.ini.' ) );
294 return eZInputValidator::STATE_INVALID;
295 }
296 if ( $canFetchResult == eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE )
297 {
298 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
299 'The size of the uploaded file exceeds the maximum upload size: %1 bytes.' ), $maxSize );
300 return eZInputValidator::STATE_INVALID;
301 }
302 return eZInputValidator::STATE_ACCEPTED;
303 }
304
308 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
309 {
310 eZBinaryFileType::checkFileUploads();
311 if ( $this->isDeletingFile( $http, $contentObjectAttribute ) )
312 {
313 return false;
314 }
315
316 if ( $this->validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute ) !== eZInputValidator::STATE_ACCEPTED )
317 {
318 return false;
319 }
320
321 if ( !eZHTTPFile::canFetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) ) )
322 return false;
323
324 $httpBinaryFile = eZHTTPFile::fetch( $base . "_data_binaryfilename_" . $contentObjectAttribute->attribute( "id" ) );
325
326 $contentObjectAttribute->setContent( $httpBinaryFile );
327
328 if ( $httpBinaryFile instanceof eZHTTPFile )
329 {
330 $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
331 $version = $contentObjectAttribute->attribute( "version" );
332
333 /*
334 $mimeObj = new eZMimeType();
335 $mimeData = $mimeObj->findByURL( $httpBinaryFile->attribute( "original_filename" ), true );
336 $mime = $mimeData['name'];
337 */
338
339 $mimeData = eZMimeType::findByFileContents( $httpBinaryFile->attribute( "original_filename" ) );
340 $mime = $mimeData['name'];
341
342 if ( $mime == '' )
343 {
344 $mime = $httpBinaryFile->attribute( "mime_type" );
345 }
346 $extension = eZFile::suffix( $httpBinaryFile->attribute( "original_filename" ) );
347 $httpBinaryFile->setMimeType( $mime );
348 if ( !$httpBinaryFile->store( "original", $extension ) )
349 {
350 eZDebug::writeError( "Failed to store http-file: " . $httpBinaryFile->attribute( "original_filename" ),
351 "eZBinaryFileType" );
352 return false;
353 }
354
355 $binary = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
356 if ( $binary === null )
357 {
358 $binary = eZBinaryFile::create( $contentObjectAttributeID, $version );
359 }
360 else
361 {
362 // if storing a different file for the same version, see if the existing file can be removed.
363 $newFileName = basename( $httpBinaryFile->attribute( "filename" ) );
364 if ( $newFileName != $binary->Filename )
365 {
366 $this->deleteStoredObjectAttribute( $contentObjectAttribute, $version );
367 }
368 }
369
370 $orig_dir = $httpBinaryFile->storageDir( "original" );
371
372 $binary->setAttribute( "contentobject_attribute_id", $contentObjectAttributeID );
373 $binary->setAttribute( "version", $version );
374 $binary->setAttribute( "filename", basename( $httpBinaryFile->attribute( "filename" ) ) );
375 $binary->setAttribute( "original_filename", $httpBinaryFile->attribute( "original_filename" ) );
376 $binary->setAttribute( "mime_type", $mime );
377
378 $binary->store();
379
380 $filePath = $httpBinaryFile->attribute( 'filename' );
381 $fileHandler = eZClusterFileHandler::instance();
382 $fileHandler->fileStore( $filePath, 'binaryfile', true, $mime );
383
384 $contentObjectAttribute->setContent( $binary );
385 }
386 return true;
387 }
388
392 function storeObjectAttribute( $contentObjectAttribute )
393 {
394 }
395
396 function customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters )
397 {
398 eZBinaryFileType::checkFileUploads();
399 if( $action == "delete_binary" )
400 {
401 $contentObjectAttributeID = $contentObjectAttribute->attribute( "id" );
402 $version = $contentObjectAttribute->attribute( "version" );
403 $this->deleteStoredObjectAttribute( $contentObjectAttribute, $version );
404 }
405 }
406
410 function isHTTPFileInsertionSupported()
411 {
412 return true;
413 }
414
418 function isRegularFileInsertionSupported()
419 {
420 return true;
421 }
422
426 function insertHTTPFile( $object, $objectVersion, $objectLanguage,
427 $objectAttribute, $httpFile, $mimeData,
428 &$result )
429 {
430 $result = array( 'errors' => array(),
431 'require_storage' => false );
432 $attributeID = $objectAttribute->attribute( 'id' );
433
434 $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
435 if ( $binary === null )
436 $binary = eZBinaryFile::create( $attributeID, $objectVersion );
437
438 $httpFile->setMimeType( $mimeData['name'] );
439
440 $suffix = false;
441 if ( isset( $mimeData['suffix'] ) )
442 $suffix = $mimeData['suffix'];
443
444 $db = eZDB::instance();
445 $db->begin();
446
447 if ( !$httpFile->store( "original", $suffix, false ) )
448 {
449 $result['errors'][] = array( 'description' => ezpI18n::tr( 'kernel/classes/datatypes/ezbinaryfile',
450 'Failed to store file %filename. Please contact the site administrator.', null,
451 array( '%filename' => $httpFile->attribute( "original_filename" ) ) ) );
452 return false;
453 }
454
455
456 $filePath = $binary->attribute( 'filename' );
457
458 $binary->setAttribute( "contentobject_attribute_id", $attributeID );
459 $binary->setAttribute( "version", $objectVersion );
460 $binary->setAttribute( "filename", basename( $httpFile->attribute( "filename" ) ) );
461 $binary->setAttribute( "original_filename", $httpFile->attribute( "original_filename" ) );
462 $binary->setAttribute( "mime_type", $mimeData['name'] );
463
464 $binary->store();
465
466 $filePath = $httpFile->attribute( 'filename' );
467 $fileHandler = eZClusterFileHandler::instance();
468 $fileHandler->fileStore( $filePath, 'binaryfile', true, $mimeData['name'] );
469 $objectAttribute->setContent( $binary );
470
471 $db->commit();
472
473 $objectAttribute->setContent( $binary );
474
475 return true;
476 }
477
481 function insertRegularFile( $object, $objectVersion, $objectLanguage,
482 $objectAttribute, $filePath,
483 &$result )
484 {
485 $result = array( 'errors' => array(),
486 'require_storage' => false );
487 $attributeID = $objectAttribute->attribute( 'id' );
488
489 $binary = eZBinaryFile::fetch( $attributeID, $objectVersion );
490 if ( $binary === null )
491 $binary = eZBinaryFile::create( $attributeID, $objectVersion );
492
493 $fileName = basename( $filePath );
494 $mimeData = eZMimeType::findByFileContents( $filePath );
495 $storageDir = eZSys::storageDirectory();
496 list( $group, $type ) = explode( '/', $mimeData['name'] );
497 $destination = $storageDir . '/original/' . $group;
498
499 if ( !file_exists( $destination ) )
500 {
501 if ( !eZDir::mkdir( $destination, false, true ) )
502 {
503 return false;
504 }
505 }
506
507 // create dest filename in the same manner as eZHTTPFile::store()
508 // grab file's suffix
509 $fileSuffix = eZFile::suffix( $fileName );
510 // prepend dot
511 if( $fileSuffix )
512 $fileSuffix = '.' . $fileSuffix;
513 // grab filename without suffix
514 $fileBaseName = basename( $fileName, $fileSuffix );
515 // create dest filename
516 $destFileName = md5( $fileBaseName . microtime() . mt_rand() ) . $fileSuffix;
517 $destination = $destination . '/' . $destFileName;
518
519 copy( $filePath, $destination );
520
521 $fileHandler = eZClusterFileHandler::instance();
522 $fileHandler->fileStore( $destination, 'binaryfile', true, $mimeData['name'] );
523
524
525 $binary->setAttribute( "contentobject_attribute_id", $attributeID );
526 $binary->setAttribute( "version", $objectVersion );
527 $binary->setAttribute( "filename", $destFileName );
528 $binary->setAttribute( "original_filename", $fileName );
529 $binary->setAttribute( "mime_type", $mimeData['name'] );
530
531 $binary->store();
532
533 $objectAttribute->setContent( $binary );
534 return true;
535 }
536
540 function hasStoredFileInformation( $object, $objectVersion, $objectLanguage,
541 $objectAttribute )
542 {
543 return true;
544 }
545
549 function storedFileInformation( $object, $objectVersion, $objectLanguage,
550 $objectAttribute )
551 {
552 $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
553 $objectAttribute->attribute( "version" ) );
554 if ( $binaryFile )
555 {
556 return $binaryFile->storedFileInfo();
557 }
558 return false;
559 }
563 function handleDownload( $object, $objectVersion, $objectLanguage,
564 $objectAttribute )
565 {
566 $binaryFile = eZBinaryFile::fetch( $objectAttribute->attribute( "id" ),
567 $objectAttribute->attribute( "version" ) );
568
569 $contentObjectAttributeID = $objectAttribute->attribute( 'id' );
570 $version = $objectAttribute->attribute( "version" );
571
572 if ( $binaryFile )
573 {
574 $db = eZDB::instance();
575 $db->query( "UPDATE ezbinaryfile SET download_count=(download_count+1)
576 WHERE
577 contentobject_attribute_id=$contentObjectAttributeID AND version=$version" );
578 return true;
579 }
580 return false;
581 }
582
583 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
584 {
585 $filesizeName = $base . self::MAX_FILESIZE_VARIABLE . $classAttribute->attribute( 'id' );
586 if ( $http->hasPostVariable( $filesizeName ) )
587 {
588 $filesizeValue = $http->postVariable( $filesizeName );
589 $classAttribute->setAttribute( self::MAX_FILESIZE_FIELD, $filesizeValue );
590 }
591 }
595 function title( $contentObjectAttribute, $name = "original_filename" )
596 {
597 $value = false;
598 $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( 'id' ),
599 $contentObjectAttribute->attribute( 'version' ) );
600 if ( is_object( $binaryFile ) )
601 $value = $binaryFile->attribute( $name );
602
603 return $value;
604 }
605
606 function hasObjectAttributeContent( $contentObjectAttribute )
607 {
608 $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
609 $contentObjectAttribute->attribute( "version" ) );
610 if ( !$binaryFile )
611 return false;
612 return true;
613 }
614
615 function objectAttributeContent( $contentObjectAttribute )
616 {
617 $binaryFile = eZBinaryFile::fetch( $contentObjectAttribute->attribute( "id" ),
618 $contentObjectAttribute->attribute( "version" ) );
619 if ( !$binaryFile )
620 {
621 $attrValue = false;
622 return $attrValue;
623 }
624 return $binaryFile;
625 }
626
627 function isIndexable()
628 {
629 return true;
630 }
631
632 function metaData( $contentObjectAttribute )
633 {
634 $binaryFile = $contentObjectAttribute->content();
635
636 $metaData = "";
637 if ( $binaryFile instanceof eZBinaryFile )
638 {
639 $metaData = $binaryFile->metaData();
640 }
641 return $metaData;
642 }
643
644 function serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
645 {
646 $dom = $attributeParametersNode->ownerDocument;
647 $maxSize = $classAttribute->attribute( self::MAX_FILESIZE_FIELD );
648 $maxSizeNode = $dom->createElement( 'max-size' );
649 $maxSizeNode->appendChild( $dom->createTextNode( $maxSize ) );
650 $maxSizeNode->setAttribute( 'unit-size', 'mega' );
651 $attributeParametersNode->appendChild( $maxSizeNode );
652 }
653
654 function unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode )
655 {
656 $sizeNode = $attributeParametersNode->getElementsByTagName( 'max-size' )->item( 0 );
657 $maxSize = $sizeNode->textContent;
658 $unitSize = $sizeNode->getAttribute( 'unit-size' );
659 $classAttribute->setAttribute( self::MAX_FILESIZE_FIELD, $maxSize );
660 }
661
666 function toString( $objectAttribute )
667 {
668 $binaryFile = $objectAttribute->content();
669
670 if ( is_object( $binaryFile ) )
671 {
672 return implode( '|', array( $binaryFile->attribute( 'filepath' ), $binaryFile->attribute( 'original_filename' ) ) );
673 }
674 else
675 return '';
676 }
677
678
679
680 function fromString( $objectAttribute, $string )
681 {
682 if( !$string )
683 return true;
684
685 $result = array();
686 return $this->insertRegularFile( $objectAttribute->attribute( 'object' ),
687 $objectAttribute->attribute( 'version' ),
688 $objectAttribute->attribute( 'language_code' ),
689 $objectAttribute,
690 $string,
691 $result );
692 }
693
694 function serializeContentObjectAttribute( $package, $objectAttribute )
695 {
696 $node = $this->createContentObjectAttributeDOMNode( $objectAttribute );
697
698 $binaryFile = $objectAttribute->attribute( 'content' );
699 if ( is_object( $binaryFile ) )
700 {
701 $fileKey = md5( mt_rand() );
702 $package->appendSimpleFile( $fileKey, $binaryFile->attribute( 'filepath' ) );
703
704 $dom = $node->ownerDocument;
705 $fileNode = $dom->createElement( 'binary-file' );
706 $fileNode->setAttribute( 'filesize', $binaryFile->attribute( 'filesize' ) );
707 $fileNode->setAttribute( 'filename', $binaryFile->attribute( 'filename' ) );
708 $fileNode->setAttribute( 'original-filename', $binaryFile->attribute( 'original_filename' ) );
709 $fileNode->setAttribute( 'mime-type', $binaryFile->attribute( 'mime_type' ) );
710 $fileNode->setAttribute( 'filekey', $fileKey );
711 $node->appendChild( $fileNode );
712 }
713
714 return $node;
715 }
716
717 function unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode )
718 {
719 $fileNode = $attributeNode->getElementsByTagName( 'binary-file' )->item( 0 );
720 if ( !is_object( $fileNode ) or !$fileNode->hasAttributes() )
721 {
722 return;
723 }
724
725 $binaryFile = eZBinaryFile::create( $objectAttribute->attribute( 'id' ), $objectAttribute->attribute( 'version' ) );
726
727 $sourcePath = $package->simpleFilePath( $fileNode->getAttribute( 'filekey' ) );
728
729 if ( !file_exists( $sourcePath ) )
730 {
731 eZDebug::writeError( "The file '$sourcePath' does not exist, cannot initialize file attribute with it", __METHOD__ );
732 return false;
733 }
734
735 $ini = eZINI::instance();
736 $mimeType = $fileNode->getAttribute( 'mime-type' );
737 list( $mimeTypeCategory, $mimeTypeName ) = explode( '/', $mimeType );
738 $destinationPath = eZSys::storageDirectory() . '/original/' . $mimeTypeCategory . '/';
739 if ( !file_exists( $destinationPath ) )
740 {
741 $oldumask = umask( 0 );
742 if ( !eZDir::mkdir( $destinationPath, false, true ) )
743 {
744 umask( $oldumask );
745 return false;
746 }
747 umask( $oldumask );
748 }
749
750 $basename = basename( $fileNode->getAttribute( 'filename' ) );
751 while ( file_exists( $destinationPath . $basename ) )
752 {
753 $basename = substr( md5( mt_rand() ), 0, 8 ) . '.' . eZFile::suffix( $fileNode->getAttribute( 'filename' ) );
754 }
755
756 eZFileHandler::copy( $sourcePath, $destinationPath . $basename );
757 eZDebug::writeNotice( 'Copied: ' . $sourcePath . ' to: ' . $destinationPath . $basename, __METHOD__ );
758
759 $binaryFile->setAttribute( 'contentobject_attribute_id', $objectAttribute->attribute( 'id' ) );
760 $binaryFile->setAttribute( 'filename', $basename );
761 $binaryFile->setAttribute( 'original_filename', $fileNode->getAttribute( 'original-filename' ) );
762 $binaryFile->setAttribute( 'mime_type', $fileNode->getAttribute( 'mime-type' ) );
763
764 $binaryFile->store();
765
766 $fileHandler = eZClusterFileHandler::instance();
767 $fileHandler->fileStore( $destinationPath . $basename, 'binaryfile', true );
768 }
769
770 function supportsBatchInitializeObjectAttribute()
771 {
772 return true;
773 }
774
781 private function isDeletingFile( eZHTTPTool $http, eZContentObjectAttribute $contentObjectAttribute )
782 {
783 $isDeletingFile = false;
784 if ( $http->hasPostVariable( 'CustomActionButton' ) )
785 {
786 $customActionArray = $http->postVariable( 'CustomActionButton' );
787 $attributeID = $contentObjectAttribute->attribute( 'id' );
788 if ( isset( $customActionArray[$attributeID . '_delete_binary'] ) )
789 {
790 $isDeletingFile = true;
791 }
792 }
793
794 return $isDeletingFile;
795 }
796
799 private $FileExtensionBlackListValidator;
800 }
801
802 eZDataType::register( eZBinaryFileType::DATA_TYPE_STRING, "eZBinaryFileType" );
803
804 ?>
eZBinaryFileHandler\instance
static instance( $identifier=false)
Definition: ezbinaryfilehandler.php:161
eZBinaryFileType
The class eZBinaryFileType handles files and association with content objects.
Definition: ezbinaryfiletype.php:19
eZBinaryFileType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezbinaryfiletype.php:606
eZBinaryFileType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezbinaryfiletype.php:242
eZBinaryFileType\fromString
fromString( $objectAttribute, $string)
Definition: ezbinaryfiletype.php:680
eZBinaryFileType\storeObjectAttribute
storeObjectAttribute( $contentObjectAttribute)
Definition: ezbinaryfiletype.php:392
eZBinaryFileType\informationTemplate
informationTemplate( $contentobjectAttribute)
Definition: ezbinaryfiletype.php:80
eZBinaryFileType\deleteStoredObjectAttribute
deleteStoredObjectAttribute( $contentObjectAttribute, $version=null)
Definition: ezbinaryfiletype.php:164
eZBinaryFileType\isDeletingFile
isDeletingFile(eZHTTPTool $http, eZContentObjectAttribute $contentObjectAttribute)
Definition: ezbinaryfiletype.php:781
eZBinaryFileType\__construct
__construct()
Definition: ezbinaryfiletype.php:26
eZBinaryFileType\serializeContentClassAttribute
serializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezbinaryfiletype.php:644
eZBinaryFileType\storedFileInformation
storedFileInformation( $object, $objectVersion, $objectLanguage, $objectAttribute)
Definition: ezbinaryfiletype.php:549
eZBinaryFileType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezbinaryfiletype.php:583
eZBinaryFileType\title
title( $contentObjectAttribute, $name="original_filename")
Definition: ezbinaryfiletype.php:595
eZBinaryFileType\handleDownload
handleDownload( $object, $objectVersion, $objectLanguage, $objectAttribute)
Definition: ezbinaryfiletype.php:563
eZBinaryFileType\unserializeContentObjectAttribute
unserializeContentObjectAttribute( $package, $objectAttribute, $attributeNode)
Definition: ezbinaryfiletype.php:717
eZBinaryFileType\toString
toString( $objectAttribute)
Definition: ezbinaryfiletype.php:666
eZBinaryFileType\metaData
metaData( $contentObjectAttribute)
Definition: ezbinaryfiletype.php:632
eZBinaryFileType\$FileExtensionBlackListValidator
$FileExtensionBlackListValidator
Definition: ezbinaryfiletype.php:799
eZBinaryFileType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezbinaryfiletype.php:770
eZBinaryFileType\insertHTTPFile
insertHTTPFile( $object, $objectVersion, $objectLanguage, $objectAttribute, $httpFile, $mimeData, &$result)
Definition: ezbinaryfiletype.php:426
eZBinaryFileType\insertRegularFile
insertRegularFile( $object, $objectVersion, $objectLanguage, $objectAttribute, $filePath, &$result)
Definition: ezbinaryfiletype.php:481
eZBinaryFileType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezbinaryfiletype.php:615
eZBinaryFileType\isRegularFileInsertionSupported
isRegularFileInsertionSupported()
Definition: ezbinaryfiletype.php:418
eZBinaryFileType\checkFileUploads
checkFileUploads()
Definition: ezbinaryfiletype.php:220
eZBinaryFileType\fileHandler
fileHandler()
Definition: ezbinaryfiletype.php:36
eZBinaryFileType\customObjectAttributeHTTPAction
customObjectAttributeHTTPAction( $http, $action, $contentObjectAttribute, $parameters)
Definition: ezbinaryfiletype.php:396
eZBinaryFileType\hasStoredFileInformation
hasStoredFileInformation( $object, $objectVersion, $objectLanguage, $objectAttribute)
Definition: ezbinaryfiletype.php:540
eZBinaryFileType\isIndexable
isIndexable()
Definition: ezbinaryfiletype.php:627
eZBinaryFileType\serializeContentObjectAttribute
serializeContentObjectAttribute( $package, $objectAttribute)
Definition: ezbinaryfiletype.php:694
eZBinaryFileType\unserializeContentClassAttribute
unserializeContentClassAttribute( $classAttribute, $attributeNode, $attributeParametersNode)
Definition: ezbinaryfiletype.php:654
eZBinaryFileType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezbinaryfiletype.php:308
eZBinaryFileType\editTemplate
editTemplate( $contentobjectAttribute)
Definition: ezbinaryfiletype.php:62
eZBinaryFileType\viewTemplate
viewTemplate( $contentobjectAttribute)
Definition: ezbinaryfiletype.php:44
eZBinaryFileType\isHTTPFileInsertionSupported
isHTTPFileInsertionSupported()
Definition: ezbinaryfiletype.php:410
eZBinaryFileType\trashStoredObjectAttribute
trashStoredObjectAttribute( $contentObjectAttribute, $version=null)
Definition: ezbinaryfiletype.php:113
eZBinaryFileType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezbinaryfiletype.php:93
eZBinaryFileType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezbinaryfiletype.php:24
eZBinaryFileType\MAX_FILESIZE_VARIABLE
const MAX_FILESIZE_VARIABLE
Definition: ezbinaryfiletype.php:22
eZBinaryFileType\MAX_FILESIZE_FIELD
const MAX_FILESIZE_FIELD
Definition: ezbinaryfiletype.php:20
eZBinaryFile
The class eZBinaryFile handles registered binaryfiles.
Definition: ezbinaryfile.php:19
eZBinaryFile\fetchByFileName
static fetchByFileName( $filename, $version=null, $asObject=true)
Definition: ezbinaryfile.php:126
eZBinaryFile\fetch
static fetch( $id, $version=null, $asObject=true)
Definition: ezbinaryfile.php:105
eZBinaryFile\create
static create( $contentObjectAttributeID, $version)
Definition: ezbinaryfile.php:94
eZBinaryFile\removeByID
static removeByID( $id, $version)
Definition: ezbinaryfile.php:147
eZClusterFileHandler\instance
static instance( $filename=false)
Definition: ezclusterfilehandler.php:21
eZContentObjectAttribute
Encapsulates the data for an object attribute.
Definition: ezcontentobjectattribute.php:20
eZContentObjectAttribute\attribute
attribute( $attr, $noFunction=false)
Definition: ezcontentobjectattribute.php:229
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZDataType\$DataTypeString
$DataTypeString
The datatype string ID, used for uniquely identifying a datatype.
Definition: ezdatatype.php:1515
eZDataType\createContentObjectAttributeDOMNode
createContentObjectAttributeDOMNode( $objectAttribute)
Definition: ezdatatype.php:1371
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:459
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZError\KERNEL_NOT_AVAILABLE
const KERNEL_NOT_AVAILABLE
Definition: ezerror.php:29
eZFileExtensionBlackListValidator
The class eZFileExtensionBlackListValidator validates file extensions based on a blacklist.
Definition: ezfileextensionblacklistvalidator.php:17
eZFileHandler\copy
static copy( $sourceFilename, $destinationFilename)
Definition: ezfilehandler.php:356
eZFile\suffix
static suffix( $filename)
Definition: ezfile.php:102
eZHTTPFile
Provides access to HTTP post files.
Definition: ezhttpfile.php:23
eZHTTPFile\UPLOADEDFILE_EXCEEDS_MAX_SIZE
const UPLOADEDFILE_EXCEEDS_MAX_SIZE
Definition: ezhttpfile.php:27
eZHTTPFile\canFetch
static canFetch( $httpName, $maxSize=false)
Definition: ezhttpfile.php:203
eZHTTPFile\fetch
static fetch( $http_name)
Definition: ezhttpfile.php:269
eZHTTPFile\UPLOADEDFILE_EXCEEDS_PHP_LIMIT
const UPLOADEDFILE_EXCEEDS_PHP_LIMIT
Definition: ezhttpfile.php:26
eZHTTPFile\UPLOADEDFILE_DOES_NOT_EXIST
const UPLOADEDFILE_DOES_NOT_EXIST
Definition: ezhttpfile.php:25
eZHTTPTool
Provides access to HTTP post,get and session variables.
Definition: ezhttptool.php:24
eZHTTPTool\postVariable
postVariable( $var, $fallbackValue=null)
Definition: ezhttptool.php:50
eZHTTPTool\hasPostVariable
hasPostVariable( $var)
Definition: ezhttptool.php:69
eZInputValidator\STATE_INTERMEDIATE
const STATE_INTERMEDIATE
Definition: ezinputvalidator.php:80
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZMimeType\findByFileContents
static findByFileContents( $url, $returnDefault=true)
Definition: ezmimetype.php:308
eZSys\storageDirectory
static storageDirectory()
Definition: ezsys.php:514
eZSys\instance
static instance()
Definition: ezsys.php:1272
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.