Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezcontentupload.php
Go to the documentation of this file.
1 <?php
48 class eZContentUpload
49 {
50
51 const STATUS_PERMISSION_DENIED = 1;
52
59 public function __construct( $params = false )
60 {
61 $http = eZHTTPTool::instance();
62 if ( !$params && $http->hasSessionVariable( 'ContentUploadParameters' ) )
63 {
64 $this->Parameters =& $http->sessionVariable( 'ContentUploadParameters' );
65 }
66 else
67 {
68 if ( $params === false )
69 $params = array();
70 $this->Parameters = $params;
71 }
72 }
73
78 function eZContentUpload( $params = false )
79 {
80 self::__construct( $params );
81 }
82
86 function attributes()
87 {
88 return array_keys( $this->Parameters );
89 }
90
94 function hasAttribute( $attributeName )
95 {
96 return array_key_exists( $attributeName, $this->Parameters );
97 }
98
102 function attribute( $attributeName )
103 {
104 if ( isset( $this->Parameters[$attributeName] ) )
105 {
106 return $this->Parameters[$attributeName];
107 }
108
109 eZDebug::writeError( "Attribute '$attributeName' does not exist", __METHOD__ );
110 return null;
111 }
112
119 static function upload( $parameters = array(), $module )
120 {
121 $ini = eZINI::instance( 'upload.ini' );
122
123 if ( !isset( $parameters['action_name'] ) )
124 $parameters['action_name'] = $ini->variable( 'UploadSettings', 'DefaultActionName' );
125
126 if ( !isset( $parameters['result_action_name'] ) )
127 $parameters['result_action_name'] = $parameters['action_name'];
128
129 if ( !isset( $parameters['navigation_part_identifier'] ) )
130 $parameters['navigation_part_identifier'] = false;
131 if ( !$parameters['navigation_part_identifier'] and
132 $ini->hasVariable( 'UploadSettings', 'NavigationPartIdentifier' ) )
133 $parameters['navigation_part_identifier'] = $ini->variable( 'UploadSettings', 'NavigationPartIdentifier' );
134
135 if ( !isset( $parameters['type'] ) )
136 $parameters['type'] = $parameters['action_name'];
137
138 if ( !isset( $parameters['return_type'] ) )
139 {
140 if ( $ini->hasVariable( $parameters['type'], 'ReturnType' ) )
141 $parameters['return_type'] = $ini->variable( $parameters['type'], 'ReturnType' );
142 else
143 $parameters['return_type'] = $ini->variable( 'UploadSettings', 'DefaultReturnType' );
144 }
145
146 if ( !isset( $parameters['upload_custom_action'] ) )
147 $parameters['upload_custom_action'] = false;
148
149 if ( !isset( $parameters['custom_action_data'] ) )
150 $parameters['custom_action_data'] = false;
151
152 if ( !isset( $parameters['description_template'] ) )
153 $parameters['description_template'] = false;
154
155 if ( !isset( $parameters['parent_nodes'] ) )
156 {
157 $parameters['parent_nodes'] = false;
158 if ( $ini->hasVariable( $parameters['type'], 'ParentNodes' ) )
159 {
160 $parameters['parent_nodes'] = $ini->variable( $parameters['type'], 'ParentNodes' );
161 }
162 }
163
164 if ( !isset( $parameters['persistent_data'] ) )
165 $parameters['persistent_data'] = false;
166
167 if ( isset( $parameters['parent_nodes'] ) and
168 is_array( $parameters['parent_nodes'] ) )
169 {
170 foreach ( $parameters['parent_nodes'] as $key => $parentNode )
171 {
172 if ( !is_numeric( $parentNode ) )
173 {
174 $parameters['parent_nodes'][$key] = eZContentUpload::nodeAliasID( $parentNode );
175 }
176 }
177 }
178
179 if ( !isset( $parameters['result_uri'] ) )
180 $parameters['result_uri'] = false;
181
182 if ( !isset( $parameters['cancel_uri'] ) )
183 $parameters['cancel_uri'] = false;
184
185 if ( !isset( $parameters['result_module'] ) )
186 $parameters['result_module'] = false;
187
188 $parameters['result'] = false;
189
190 $http = eZHTTPTool::instance();
191 $http->setSessionVariable( 'ContentUploadParameters', $parameters );
192
193 if ( $module === null )
194 {
195 return "/content/upload/";
196 }
197 else
198 {
199 $module->redirectTo( "/content/upload/" );
200 return "/content/upload/";
201 }
202 }
203
228 function handleLocalFile( &$result, $filePath, $location, $existingNode, $nameString = '', $localeCode = false )
229 {
230 $result = array( 'errors' => array(),
231 'notices' => array(),
232 'result' => false,
233 'redirect_url' => false,
234 'status' => false );
235
236 if ( !file_exists( $filePath ) )
237 {
238 $result['errors'][] =
239 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
240 'The file %filename does not exist, cannot insert file.', null,
241 array( '%filename' => $filePath ) ) );
242 return false;
243 }
244 $mimeData = eZMimeType::findByFileContents( $filePath );
245 $mime = $mimeData['name'];
246
247 $handler = $this->findHandler( $result, $mimeData );
248 if ( $handler === false )
249 {
250 $result['errors'][] =
251 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
252 'There was an error trying to instantiate content upload handler.' ) );
253 return false;
254 }
255
256 // If this is an object we have a special handler for the file.
257 // The handler will be responsible for the rest of the execution.
258 if ( is_object( $handler ) )
259 {
260 $originalFilename = $nameString != '' ? $nameString : $filePath;
261 return $handler->handleFile( $this, $result,
262 $filePath, $originalFilename, $mimeData,
263 $location, $existingNode );
264 }
265
266 $object = false;
267 $class = false;
268 // Figure out class identifier from an existing node
269 // if not we will have to detect it from the mimetype
270 if ( is_object( $existingNode ) )
271 {
272 $object = $existingNode->object();
273 $class = $object->contentClass();
274 $classIdentifier = $class->attribute( 'identifier' );
275 }
276 else
277 {
278 $classIdentifier = $this->detectClassIdentifier( $mime );
279 }
280
281 if ( !$classIdentifier )
282 {
283 $result['errors'][] =
284 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
285 'No matching class identifier found.' ) );
286 return false;
287 }
288
289 if ( !is_object( $class ) )
290 $class = eZContentClass::fetchByIdentifier( $classIdentifier );
291
292 if ( !$class )
293 {
294 $result['errors'][] =
295 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
296 'The class %class_identifier does not exist.', null,
297 array( '%class_identifier' => $classIdentifier ) ) );
298 return false;
299 }
300
301 $parentNodes = false;
302 $parentMainNode = false;
303 // If do not have an existing node we need to figure
304 // out the locations from $location and $classIdentifier
305 if ( !is_object( $existingNode ) )
306 {
307 $locationOK = $this->detectLocations( $classIdentifier, $class, $location, $parentNodes, $parentMainNode );
308 if ( $locationOK === false )
309 {
310 $result['errors'][] =
311 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
312 'Was not able to figure out placement of object.' ) );
313 return false;
314 }
315 elseif ( $locationOK === null )
316 {
317 $result['status'] = eZContentUpload::STATUS_PERMISSION_DENIED;
318 $result['errors'][] =
319 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
320 'Permission denied' ) );
321 return false;
322 }
323 }
324
325 $uploadINI = eZINI::instance( 'upload.ini' );
326 $iniGroup = $classIdentifier . '_ClassSettings';
327 if ( !$uploadINI->hasGroup( $iniGroup ) )
328 {
329 $result['errors'][] =
330 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
331 'No configuration group in upload.ini for class identifier %class_identifier.', null,
332 array( '%class_identifier' => $classIdentifier ) ) );
333 return false;
334 }
335
336 $fileAttribute = $uploadINI->variable( $iniGroup, 'FileAttribute' );
337 $dataMap = $class->dataMap();
338
339 $fileAttribute = $this->findRegularFileAttribute( $dataMap, $fileAttribute );
340 if ( !$fileAttribute )
341 {
342 $result['errors'][] =
343 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
344 'No matching file attribute found, cannot create content object without this.' ) );
345 return false;
346 }
347
348 $nameAttribute = $uploadINI->variable( $iniGroup, 'NameAttribute' );
349 if ( !$nameAttribute )
350 {
351 $nameAttribute = $this->findStringAttribute( $dataMap, $nameAttribute );
352 }
353 if ( !$nameAttribute )
354 {
355 $result['errors'][] =
356 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
357 'No matching name attribute found, cannot create content object without this.' ) );
358 return false;
359 }
360
361 $variables = array( 'original_filename' => $mimeData['filename'],
362 'mime_type' => $mime );
363 $variables['original_filename_base'] = $mimeData['basename'];
364 $variables['original_filename_suffix'] = $mimeData['suffix'];
365
366 if ( !$nameString )
367 {
368 $namePattern = $uploadINI->variable( $iniGroup, 'NamePattern' );
369 $nameString = $this->processNamePattern( $variables, $namePattern );
370 }
371 // If we have an existing node we need to create
372 // a new version in it.
373 // If we don't we have to make a new object
374 if ( is_object( $existingNode ) )
375 {
376 if ( $existingNode->canEdit( ) != '1' )
377 {
378 $result['status'] = eZContentUpload::STATUS_PERMISSION_DENIED;
379 $result['errors'][] =
380 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
381 'Permission denied' ) );
382 return false;
383 }
384 $version = $object->createNewVersion( false, true, $localeCode );
385 unset( $dataMap );
386 $dataMap = $version->dataMap();
387 $publishVersion = $version->attribute( 'version' );
388 }
389 else
390 {
391 $object = $class->instantiateIn( $localeCode );
392 unset( $dataMap );
393 $dataMap = $object->dataMap();
394 $publishVersion = $object->attribute( 'current_version' );
395 }
396
397 if ( $localeCode === false )
398 {
399 $localeCode = eZContentObject::defaultLanguage();
400 }
401 $status = $dataMap[$fileAttribute]->insertRegularFile( $object, $publishVersion, $localeCode,
402 $filePath,
403 $storeResult );
404 if ( $status === null )
405 {
406 $result['errors'][] =
407 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
408 'The attribute %class_identifier does not support regular file storage.', null,
409 array( '%class_identifier' => $classIdentifier ) ) );
410 return false;
411 }
412 else if ( !$status )
413 {
414 $result['errors'] = array_merge( $result['errors'], $storeResult['errors'] );
415 return false;
416 }
417 if ( $storeResult['require_storage'] )
418 $dataMap[$fileAttribute]->store();
419
420 $status = $dataMap[$nameAttribute]->insertSimpleString( $object, $publishVersion, $localeCode,
421 $nameString,
422 $storeResult );
423 if ( $status === null )
424 {
425 $result['errors'][] =
426 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
427 'The attribute %class_identifier does not support simple string storage.', null,
428 array( '%class_identifier' => $classIdentifier ) ) );
429 return false;
430 }
431 else if ( !$status )
432 {
433 $result['errors'] = array_merge( $result['errors'], $storeResult['errors'] );
434 return false;
435 }
436 if ( $storeResult['require_storage'] )
437 $dataMap[$nameAttribute]->store();
438
439 if ( is_array( $parentNodes ) )
440 {
441 foreach ( $parentNodes as $parentNode )
442 {
443 $object->createNodeAssignment(
444 $parentNode,
445 $parentNode == $parentMainNode
446 );
447 }
448 }
449
450 return $this->publishObject( $result, $result['errors'], $result['notices'],
451 $object, $publishVersion, $class, $parentNodes, $parentMainNode );
452 }
453
480 function handleUpload( &$result, $httpFileIdentifier, $location, $existingNode, $nameString = '', $localeCode = false, $publish = true )
481 {
482 $result = array( 'errors' => array(),
483 'notices' => array(),
484 'result' => false,
485 'redirect_url' => false );
486
487 $this->fetchHTTPFile( $httpFileIdentifier, $result['errors'], $file, $mimeData );
488 if ( !$file )
489 {
490 $result['errors'][] =
491 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
492 'No HTTP file found, cannot fetch uploaded file.' ) );
493 return false;
494 }
495 $mime = $mimeData['name'];
496 if ( $mime == '' )
497 $mime = $file->attribute( "mime_type" );
498
499 $handler = $this->findHandler( $result, $mimeData );
500 if ( $handler === false )
501 {
502 $result['errors'][] =
503 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
504 'There was an error trying to instantiate content upload handler.' ) );
505 return false;
506 }
507
508 // If this is an object we have a special handler for the file
509 // The handler will be responsible for the rest of the execution.
510 if ( is_object( $handler ) )
511 {
512 $filePath = $file->attribute( "filename" );
513 $originalFilename = $file->attribute( "original_filename" );
514 $handlerResult = $handler->handleFile( $this, $result,
515 $filePath, $originalFilename, $mimeData,
516 $location, $existingNode );
517 if ( is_object( $result['contentobject'] ) )
518 return $handlerResult;
519 }
520
521 $object = false;
522 $class = false;
523 // Figure out class identifier from an existing node
524 // if not we will have to detect it from the mimetype
525 if ( is_object( $existingNode ) )
526 {
527 $object = $existingNode->object();
528 $class = $object->contentClass();
529 $classIdentifier = $class->attribute( 'identifier' );
530 }
531 else
532 {
533 $classIdentifier = $this->detectClassIdentifier( $mime );
534 }
535
536 if ( !$classIdentifier )
537 {
538 $result['errors'][] =
539 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
540 'No matching class identifier found.' ) );
541 return false;
542 }
543
544 if ( !is_object( $class ) )
545 $class = eZContentClass::fetchByIdentifier( $classIdentifier );
546
547 if ( !$class )
548 {
549 $result['errors'][] =
550 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
551 'The class %class_identifier does not exist.', null,
552 array( '%class_identifier' => $classIdentifier ) ) );
553 return false;
554 }
555
556
557 $parentNodes = false;
558 $parentMainNode = false;
559 // If do not have an existing node we need to figure
560 // out the locations from $location and $classIdentifier
561 if ( !is_object( $existingNode ) )
562 {
563 $locationOK = $this->detectLocations( $classIdentifier, $class, $location, $parentNodes, $parentMainNode );
564 if ( $locationOK === false )
565 {
566 $result['errors'][] =
567 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
568 'Was not able to figure out placement of object.' ) );
569 return false;
570 }
571 elseif ( $locationOK === null )
572 {
573 $result['status'] = eZContentUpload::STATUS_PERMISSION_DENIED;
574 $result['errors'][] =
575 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
576 'Permission denied' ) );
577 return false;
578 }
579 }
580
581 $uploadINI = eZINI::instance( 'upload.ini' );
582 $iniGroup = $classIdentifier . '_ClassSettings';
583 if ( !$uploadINI->hasGroup( $iniGroup ) )
584 {
585 $result['errors'][] =
586 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
587 'No configuration group in upload.ini for class identifier %class_identifier.', null,
588 array( '%class_identifier' => $classIdentifier ) ) );
589 return false;
590 }
591
592 $fileAttribute = $uploadINI->variable( $iniGroup, 'FileAttribute' );
593 $dataMap = $class->dataMap();
594
595 if ( $classIdentifier == 'image' )
596 {
597 $classAttribute = $dataMap['image'];
598 $maxSize = 1024 * 1024 * $classAttribute->attribute( 'data_int1' );
599 if ( $maxSize != 0 && $file->attribute( 'filesize' ) > $maxSize )
600 {
601 $result['errors'][] =
602 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
603 'The size of the uploaded file exceeds the limit set for this site: %1 bytes.', null, array( $maxSize ) ) );
604 return false;
605 }
606 }
607
608
609 $fileAttribute = $this->findHTTPFileAttribute( $dataMap, $fileAttribute );
610 if ( !$fileAttribute )
611 {
612 $result['errors'][] =
613 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
614 'No matching file attribute found, cannot create content object without this.' ) );
615 return false;
616 }
617
618 $nameAttribute = $uploadINI->variable( $iniGroup, 'NameAttribute' );
619 if ( !$nameAttribute )
620 {
621 $nameAttribute = $this->findStringAttribute( $dataMap, $nameAttribute );
622 }
623 if ( !$nameAttribute )
624 {
625 $result['errors'][] =
626 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
627 'No matching name attribute found, cannot create content object without this.' ) );
628 return false;
629 }
630
631 $variables = array( 'original_filename' => $file->attribute( 'original_filename' ),
632 'mime_type' => $mime );
633 $variables['original_filename_base'] = $mimeData['basename'];
634 $variables['original_filename_suffix'] = $mimeData['suffix'];
635
636 if ( !$nameString )
637 {
638 $namePattern = $uploadINI->variable( $iniGroup, 'NamePattern' );
639 $nameString = $this->processNamePattern( $variables, $namePattern );
640 }
641
642 $db = eZDB::instance();
643 $db->begin();
644
645 // If we have an existing node we need to create
646 // a new version in it.
647 // If we don't we have to make a new object
648 if ( is_object( $existingNode ) )
649 {
650 if ( $existingNode->canEdit( ) != '1' )
651 {
652 $result['status'] = eZContentUpload::STATUS_PERMISSION_DENIED;
653 $result['errors'][] =
654 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
655 'Permission denied' ) );
656
657 $db->commit();
658 return false;
659 }
660 $version = $object->createNewVersion( false, true, $localeCode );
661 unset( $dataMap );
662 $dataMap = $version->dataMap();
663 $publishVersion = $version->attribute( 'version' );
664 }
665 else
666 {
667 $object = $class->instantiateIn( $localeCode );
668 unset( $dataMap );
669 $dataMap = $object->dataMap();
670 $publishVersion = $object->attribute( 'current_version' );
671 }
672
673 unset( $dataMap );
674 $dataMap = $object->dataMap();
675
676 if ( $localeCode === false )
677 {
678 $localeCode = eZContentObject::defaultLanguage();
679 }
680 $status = $dataMap[$fileAttribute]->insertHTTPFile( $object, $publishVersion, $localeCode,
681 $file, $mimeData,
682 $storeResult );
683 if ( $status === null )
684 {
685 $result['errors'][] =
686 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
687 'The attribute %class_identifier does not support HTTP file storage.', null,
688 array( '%class_identifier' => $classIdentifier ) ) );
689 $db->commit();
690 return false;
691 }
692 else if ( !$status )
693 {
694 $result['errors'] = array_merge( $result['errors'], $storeResult['errors'] );
695 $db->commit();
696 return false;
697 }
698 if ( $storeResult['require_storage'] )
699 $dataMap[$fileAttribute]->store();
700
701 $status = $dataMap[$nameAttribute]->insertSimpleString( $object, $publishVersion, $localeCode,
702 $nameString,
703 $storeResult );
704 if ( $status === null )
705 {
706 $result['errors'][] =
707 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
708 'The attribute %class_identifier does not support simple string storage.', null,
709 array( '%class_identifier' => $classIdentifier ) ) );
710 $db->commit();
711 return false;
712 }
713 else if ( !$status )
714 {
715 $result['errors'] = array_merge( $result['errors'], $storeResult['errors'] );
716 $db->commit();
717 return false;
718 }
719 if ( $storeResult['require_storage'] )
720 $dataMap[$nameAttribute]->store();
721
722 if ( is_array( $parentNodes ) )
723 {
724 foreach ( $parentNodes as $key => $parentNode )
725 {
726 $object->createNodeAssignment(
727 $parentNode,
728 $parentNode == $parentMainNode
729 );
730 }
731 }
732
733 $object->setName( $class->contentObjectName( $object ) );
734 $object->store();
735
736 if ( $publish )
737 {
738 $tmpresult = $this->publishObject(
739 $result, $result['errors'], $result['notices'],
740 $object, $publishVersion, $class, $parentNodes, $parentMainNode
741 );
742 }
743 else
744 {
745 $tmpresult = $result;
746 $tmpresult['contentobject'] = $object;
747 $tmpresult['contentobject_id'] = $object->attribute( 'id' );
748 $tmpresult['contentobject_version'] = $publishVersion;
749 $tmpresult['contentobject_main_node'] = false;
750 $tmpresult['contentobject_main_node_id'] = false;
751 $this->setResult(
752 array(
753 'node_id' => 0,
754 'object_id' => $object->attribute( 'id' ),
755 'object_version' => $publishVersion
756 )
757 );
758 }
759
760 $db->commit();
761 return $tmpresult;
762 }
763
770 function publishObject( &$result, &$errors, &$notices,
771 $object, $publishVersion, $class, $parentNodes, $parentMainNode )
772 {
773 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $object->attribute( 'id' ),
774 'version' => $publishVersion ) );
775
776 $objectID = $object->attribute( 'id' );
777 unset( $object );
778 $object = eZContentObject::fetch( $objectID );
779 $result['contentobject'] = $object;
780 $result['contentobject_id'] = $object->attribute( 'id' );
781 $result['contentobject_version'] = $publishVersion;
782 $result['contentobject_main_node'] = false;
783 $result['contentobject_main_node_id'] = false;
784
785 $this->setResult( array( 'node_id' => false,
786 'object_id' => $object->attribute( 'id' ),
787 'object_version' => $publishVersion ) );
788
789 switch ( $operationResult['status'] )
790 {
791 case eZModuleOperationInfo::STATUS_HALTED:
792 {
793 if ( isset( $operationResult['redirect_url'] ) )
794 {
795 $result['redirect_url'] = $operationResult['redirect_url'];
796 $notices[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload',
797 'Publishing of content object was halted.' ) );
798 return true;
799 }
800 else if ( isset( $operationResult['result'] ) )
801 {
802 $result['result'] = $operationResult['result'];
803 return true;
804 }
805 } break;
806
807 case eZModuleOperationInfo::STATUS_CANCELLED:
808 {
809 $result['result'] = ezpI18n::tr( 'kernel/content/upload',
810 'Publish process was cancelled.' );
811 return true;
812 } break;
813
814 case eZModuleOperationInfo::STATUS_CONTINUE:
815 {
816 }
817 }
818
819 $mainNode = $object->mainNode();
820 $result['contentobject_main_node'] = $mainNode;
821
822 // The published object may not have a node if publishing was interrupted
823 $mainNodeId = $mainNode ? $mainNode->attribute( 'node_id' ) : 0;
824
825 $result['contentobject_main_node_id'] = $mainNodeId;
826 $this->setResult( array( 'node_id' => $mainNodeId,
827 'object_id' => $object->attribute( 'id' ),
828 'object_version' => $publishVersion ) );
829
830 return true;
831 }
832
838 function objectFileInfo( $contentObject )
839 {
840 $uploadINI = eZINI::instance( 'upload.ini' );
841
842 $class = $contentObject->contentClass();
843 $classIdentifier = $class->attribute( 'identifier' );
844 $classDataMap = $class->dataMap();
845 $attributeIdentifier = false;
846 if ( $uploadINI->hasGroup( $classIdentifier . '_ClassSettings' ) )
847 {
848 $attributeIdentifier = $uploadINI->variable( $classIdentifier . '_ClassSettings', 'FileAttribute' );
849 }
850
851 $attributeIdentifier = $this->findRegularFileAttribute( $classDataMap, $attributeIdentifier );
852 if ( !$attributeIdentifier )
853 {
854 // No file information for this object
855 return false;
856 }
857
858 $dataMap = $contentObject->dataMap();
859 $fileAttribute = $dataMap[$attributeIdentifier];
860
861 if ( $fileAttribute->hasStoredFileInformation( $contentObject, false, false ) )
862 {
863 return $fileAttribute->storedFileInformation( $contentObject, false, false );
864 }
865 return false;
866 }
867
873 function fetchHTTPFile( $httpFileIdentifier, &$errors, &$file, &$mimeData )
874 {
875 $returnCode = eZHTTPFile::canFetch( $httpFileIdentifier, 0 );
876 if ( $returnCode !== eZHTTPFile::UPLOADEDFILE_OK && $returnCode !== true )
877 {
878 switch ( $returnCode )
879 {
880 case eZHTTPFile::UPLOADEDFILE_DOES_NOT_EXIST:
881 $errors[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload', 'A file is required for upload, no file were found.' ) );
882 break;
883 case eZHTTPFile::UPLOADEDFILE_EXCEEDS_PHP_LIMIT:
884 case eZHTTPFile::UPLOADEDFILE_EXCEEDS_MAX_SIZE:
885 $errors[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload', 'The uploaded file size is above the maximum limit.' ) );
886 break;
887 case eZHTTPFile::UPLOADEDFILE_MISSING_TMP_DIR:
888 case eZHTTPFile::UPLOADEDFILE_CANT_WRITE:
889 case eZHTTPFile::UPLOADEDFILE_UNKNOWN_ERROR:
890 $errors[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload', 'A system error occured while writing the uploaded file.' ) );
891 break;
892 }
893 return false;
894 }
895
896 $file = eZHTTPFile::fetch( $httpFileIdentifier );
897 if ( !( $file instanceof eZHTTPFile ) )
898 {
899 $errors[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload',
900 'Expected a eZHTTPFile object but got nothing.' ) );
901 return false;
902 }
903
904 $mimeData = eZMimeType::findByFileContents( $file->attribute( "original_filename" ) );
905
906 return true;
907 }
908
920 function findHTTPFileAttribute( $dataMap, $fileAttribute )
921 {
922 $fileDatatype = false;
923 if ( isset( $dataMap[$fileAttribute] ) )
924 $fileDatatype = $dataMap[$fileAttribute]->dataType();
925 if ( !$fileDatatype or
926 !$fileDatatype->isHTTPFileInsertionSupported() )
927 {
928 $fileAttribute = false;
929 foreach ( $dataMap as $identifier => $attribute )
930 {
931 $datatype = $attribute->dataType();
932 if ( $datatype->isHTTPFileInsertionSupported() )
933 {
934 $fileAttribute = $identifier;
935 break;
936 }
937 }
938 }
939 return $fileAttribute;
940 }
941
953 function findRegularFileAttribute( $dataMap, $fileAttribute )
954 {
955 $fileDatatype = false;
956 if ( isset( $dataMap[$fileAttribute] ) )
957 $fileDatatype = $dataMap[$fileAttribute]->dataType();
958 if ( !$fileDatatype or
959 !$fileDatatype->isRegularFileInsertionSupported() )
960 {
961 $fileAttribute = false;
962 foreach ( $dataMap as $identifier => $attribute )
963 {
964 $datatype = $attribute->dataType();
965 if ( $datatype->isRegularFileInsertionSupported() )
966 {
967 $fileAttribute = $identifier;
968 break;
969 }
970 }
971 }
972 return $fileAttribute;
973 }
974
986 function findStringAttribute( $dataMap, $nameAttribute )
987 {
988 $nameDatatype = false;
989 if ( isset( $dataMap[$nameAttribute] ) )
990 $nameDatatype = $dataMap[$nameAttribute]->dataType();
991 if ( !$nameDatatype or
992 !$nameDatatype->isSimpleStringInsertionSupported() )
993 {
994 $nameAttribute = false;
995 foreach ( $dataMap as $identifier => $attribute )
996 {
997 $datatype = $attribute->dataType();
998 if ( $datatype->isSimpleStringInsertionSupported() )
999 {
1000 $nameAttribute = $identifier;
1001 break;
1002 }
1003 }
1004 }
1005 return $nameAttribute;
1006 }
1007
1015 function detectClassIdentifier( $mime )
1016 {
1017 $uploadINI = eZINI::instance( 'upload.ini' );
1018
1019 $mimeClassMap = $uploadINI->variable( 'CreateSettings', 'MimeClassMap' );
1020 $defaultClass = $uploadINI->variable( 'CreateSettings', 'DefaultClass' );
1021
1022 list( $group, $type ) = explode( '/', $mime );
1023 if ( isset( $mimeClassMap[$mime] ) )
1024 {
1025 $classIdentifier = $mimeClassMap[$mime];
1026 }
1027 else if ( isset( $mimeClassMap[$group] ) )
1028 {
1029 $classIdentifier = $mimeClassMap[$group];
1030 }
1031 else
1032 {
1033 $classIdentifier = $defaultClass;
1034 }
1035 return $classIdentifier;
1036 }
1037
1052 function detectLocations( $classIdentifier, $class, $location,
1053 &$parentNodes, &$parentMainNode )
1054 {
1055 $isAccessChecked = false;
1056 $parentMainNode = false;
1057 if ( $this->hasAttribute( 'parent_nodes' ) &&
1058 $this->attribute( 'parent_nodes' ) )
1059 {
1060 $parentNodes = $this->attribute( 'parent_nodes' );
1061 foreach( $parentNodes as $key => $parentNode )
1062 {
1063 $parentNodes[$key] = eZContentUpload::nodeAliasID( $parentNode );
1064 if ( !eZContentUpload::checkAccess( $parentNodes[$key], $class ) )
1065 {
1066 $parentNodes = false;
1067 return null;
1068 }
1069 }
1070 }
1071 else
1072 {
1073 if ( $location == 'auto' or !is_numeric( $location ) )
1074 {
1075 $contentINI = eZINI::instance( 'content.ini' );
1076
1077 $classPlacementMap = $contentINI->variable( 'RelationAssignmentSettings', 'ClassSpecificAssignment' );
1078 $defaultPlacement = $contentINI->variable( 'RelationAssignmentSettings', 'DefaultAssignment' );
1079
1080 // Find location that matches the class and where user is allowed to create objects
1081 foreach ( $classPlacementMap as $classData )
1082 {
1083 $classElements = explode( ';', $classData );
1084 $classList = explode( ',', $classElements[0] );
1085
1086 if ( in_array( $classIdentifier, $classList ) )
1087 {
1088 if ( !isset( $classElements[1] ) )
1089 continue;
1090 $parentNodes = explode( ',', $classElements[1] );
1091 if ( count( $parentNodes ) == 0 )
1092 continue;
1093
1094 if ( isset( $classElements[2] ) )
1095 $parentMainNode = eZContentUpload::nodeAliasID( $classElements[2] );
1096
1097 // check access rights and convert to IDs
1098 foreach ( $parentNodes as $key => $parentNode )
1099 {
1100 $parentNodeID = eZContentUpload::nodeAliasID( $parentNode );
1101 if ( !$parentNodeID )
1102 {
1103 $parentNodes = false;
1104 break;
1105 }
1106
1107 $parentNodes[$key] = $parentNodeID;
1108
1109 if ( !eZContentUpload::checkAccess( $parentNodeID, $class ) )
1110 {
1111 eZDebug::writeNotice( "Upload assignment setting '$classData' skipped - no permissions", __METHOD__ );
1112 $parentNodes = false;
1113 break;
1114 }
1115 }
1116
1117 if ( $parentNodes )
1118 {
1119 eZDebug::writeNotice( "Matched assignment for upload :'$classData'", __METHOD__ );
1120 break;
1121 }
1122 }
1123 }
1124
1125 if ( !$parentNodes && isset( $defaultPlacement ) && $defaultPlacement )
1126 {
1127 $defaultNodeID = eZContentUpload::nodeAliasID( $defaultPlacement );
1128 if ( $defaultNodeID )
1129 {
1130 if ( eZContentUpload::checkAccess( $defaultNodeID, $class ) )
1131 {
1132 $parentNodes = array( $defaultNodeID );
1133 }
1134 else
1135 {
1136 eZDebug::writeNotice( "No create permission for default upload location: node #$defaultNodeID", __METHOD__ );
1137 return null;
1138 }
1139
1140 }
1141 }
1142 }
1143 else
1144 {
1145 $locationID = eZContentUpload::nodeAliasID( $location );
1146 if ( $locationID )
1147 {
1148 if ( eZContentUpload::checkAccess( $locationID, $class ) )
1149 {
1150 $parentNodes = array( $locationID );
1151 }
1152 else
1153 {
1154 eZDebug::writeNotice( "No create permission for upload location: node #$locationID", __METHOD__ );
1155 return null;
1156 }
1157 }
1158 }
1159 }
1160
1161 if ( !$parentNodes || count( $parentNodes ) == 0 )
1162 {
1163 return false;
1164 }
1165
1166 if ( !$parentMainNode )
1167 {
1168 $parentMainNode = $parentNodes[0];
1169 }
1170
1171 return true;
1172 }
1173
1179 function checkAccess( $nodeID, $class )
1180 {
1181 $parentNodeObj = eZContentObjectTreeNode::fetch( $nodeID );
1182 $parentObject = $parentNodeObj->attribute( 'object' );
1183
1184 if ( $parentNodeObj->checkAccess( 'create',
1185 $class->attribute( 'id' ),
1186 $parentObject->attribute( 'contentclass_id' ) ) != '1' )
1187 {
1188 return false;
1189 }
1190 return true;
1191 }
1192
1211 function processNamePattern( $variables, $namePattern )
1212 {
1213 $tags = array();
1214 $pos = 0;
1215 $lastPos = false;
1216 $len = strlen( $namePattern );
1217 while ( $pos < $len )
1218 {
1219 $markerPos = strpos( $namePattern, '<', $pos );
1220 if ( $markerPos !== false )
1221 {
1222 $markerEndPos = strpos( $namePattern, '>', $markerPos + 1 );
1223 if ( $markerEndPos === false )
1224 {
1225 $markerEndPos = $len;
1226 $pos = $len;
1227 }
1228 else
1229 {
1230 $pos = $markerEndPos + 1;
1231 }
1232 $tag = substr( $namePattern, $markerPos + 1, $markerEndPos - $markerPos - 1 );
1233 $tags[] = array( 'name' => $tag );
1234 }
1235 if ( $lastPos !== false and
1236 $lastPos < $pos )
1237 {
1238 $tags[] = substr( $namePattern, $lastPos, $pos - $lastPos );
1239 }
1240 $lastPos = $pos;
1241 }
1242 $nameString = '';
1243 foreach ( $tags as $tag )
1244 {
1245 if ( is_string( $tag ) )
1246 {
1247 $nameString .= $tag;
1248 }
1249 else
1250 {
1251 if ( isset( $variables[$tag['name']] ) )
1252 $nameString .= $variables[$tag['name']];
1253 }
1254 }
1255 return $nameString;
1256 }
1257
1262 function nodeAliasID( $nodeName )
1263 {
1264 if ( is_numeric( $nodeName ) )
1265 {
1266 $node = eZContentObjectTreeNode::fetch( $nodeName, false, false );
1267 if ( is_array( $node ) )
1268 {
1269 $result['status'] = eZContentUpload::STATUS_PERMISSION_DENIED;
1270 $errors[] = array( 'description' => ezpI18n::tr( 'kernel/content/upload',
1271 'Permission denied' ) );
1272
1273 return $nodeName;
1274 }
1275 }
1276
1277 $uploadINI = eZINI::instance( 'upload.ini' );
1278 $aliasList = $uploadINI->variable( 'UploadSettings', 'AliasList' );
1279 if ( isset( $aliasList[$nodeName] ) )
1280 return $aliasList[$nodeName];
1281 $contentINI = eZINI::instance( 'content.ini' );
1282 if ( $nodeName == 'content' or $nodeName == 'root' )
1283 return $contentINI->variable( 'NodeSettings', 'RootNode' );
1284 else if ( $nodeName == 'users' )
1285 return $contentINI->variable( 'NodeSettings', 'UserRootNode' );
1286 else if ( $nodeName == 'media' )
1287 return $contentINI->variable( 'NodeSettings', 'MediaRootNode' );
1288 else if ( $nodeName == 'setup' )
1289 return $contentINI->variable( 'NodeSettings', 'SetupRootNode' );
1290
1291 // Check for node path element
1292 $pathPos = strpos( $nodeName, '/' );
1293 if ( $pathPos !== false )
1294 {
1295 $node = eZContentObjectTreeNode::fetchByURLPath( $nodeName, false );
1296 if ( is_array( $node ) )
1297 {
1298 return $node['node_id'];
1299 }
1300 }
1301 return false;
1302 }
1303
1307 function setResult( $result )
1308 {
1309 $this->Parameters['result'] = $result;
1310 $http = eZHTTPTool::instance();
1311 $http->setSessionVariable( 'ContentUploadParameters', $this->Parameters );
1312 }
1313
1320 static function result( $actionName, $cleanup = true )
1321 {
1322 $upload = new eZContentUpload();
1323
1324 $isNodeSelection = $upload->attribute( 'return_type' ) == 'NodeID';
1325 $resultData = $upload->attribute( 'result' );
1326 $result = false;
1327 if ( $isNodeSelection )
1328 {
1329 $result = $resultData['node_id'];
1330 }
1331 else
1332 {
1333 $result = $resultData['object_id'];
1334 }
1335 if ( $cleanup )
1336 eZContentUpload::cleanup( $actionName );
1337 return $result;
1338 }
1339
1344 static function cleanup( $actionName )
1345 {
1346 $http = eZHTTPTool::instance();
1347 $http->removeSessionVariable( 'ContentUploadParameters' );
1348 }
1349
1354 function cleanupAll()
1355 {
1356 $http = eZHTTPTool::instance();
1357 $http->removeSessionVariable( 'ContentUploadParameters' );
1358 }
1359
1367 function findHandler( &$result, $mimeInfo )
1368 {
1369 // Check for specific mime handler plugin
1370 $uploadINI = eZINI::instance( 'upload.ini' );
1371 $uploadSettings = $uploadINI->variable( 'CreateSettings', 'MimeUploadHandlerMap' );
1372
1373 $mime = $mimeInfo['name'];
1374 $elements = explode( '/', $mime );
1375 $mimeGroup = $elements[0];
1376
1377 $handlerName = false;
1378 // Check first for MIME-Type group, this allows a handler to work
1379 // with an entire group, e.g. image
1380 if ( isset( $uploadSettings[$mimeGroup] ) )
1381 {
1382 $handlerName = $uploadSettings[$mimeGroup];
1383 }
1384 else if ( isset( $uploadSettings[$mime] ) )
1385 {
1386 $handlerName = $uploadSettings[$mime];
1387 }
1388
1389 if ( $handlerName !== false )
1390 {
1391 $baseDirectory = eZExtension::baseDirectory();
1392 $extensionDirectories = eZExtension::activeExtensions();
1393
1394 // Check all extension directories for an upload handler for this mimetype
1395 foreach ( $extensionDirectories as $extensionDirectory )
1396 {
1397 $handlerPath = $baseDirectory . '/' . $extensionDirectory . '/uploadhandlers/' . $handlerName . ".php";
1398 if ( !file_exists( $handlerPath ) )
1399 continue;
1400
1401 include_once( $handlerPath );
1402 $handlerClass = $handlerName;
1403 $handler = new $handlerClass();
1404 if ( !$handler instanceof eZContentUploadHandler )
1405 {
1406 eZDebug::writeError( "Content upload handler '$handlerName' is not inherited from eZContentUploadHandler. All upload handlers must do this.", __METHOD__ );
1407 return false;
1408 }
1409 return $handler;
1410 }
1411
1412 $result['errors'][] =
1413 array( 'description' => ezpI18n::tr( 'kernel/content/upload',
1414 "Could not find content upload handler '%handler_name'",
1415 null, array( '%handler_name' => $handlerName ) ) );
1416
1417 return false;
1418 }
1419 return true;
1420 }
1421
1424 public $Parameters = false;
1425 }
1426
1427 ?>
eZContentClass\fetchByIdentifier
static fetchByIdentifier( $identifier, $asObject=true, $version=eZContentClass::VERSION_STATUS_DEFINED, $user_id=false, $parent_id=null)
Definition: ezcontentclass.php:1203
eZContentObjectTreeNode\fetch
static fetch( $nodeID=false, $lang=false, $asObject=true, $conditions=false)
Definition: ezcontentobjecttreenode.php:3058
eZContentObjectTreeNode\fetchByURLPath
static fetchByURLPath( $pathString, $asObject=true)
Definition: ezcontentobjecttreenode.php:2943
eZContentObject\defaultLanguage
static defaultLanguage()
Definition: ezcontentobject.php:5330
eZContentObject\fetch
static fetch( $id, $asObject=true)
Definition: ezcontentobject.php:885
eZContentUploadHandler
Interface for all content upload handlers.
Definition: ezcontentuploadhandler.php:28
eZContentUpload
Handles simple creation of content objects by uploading files.
Definition: ezcontentupload.php:49
eZContentUpload\setResult
setResult( $result)
Definition: ezcontentupload.php:1307
eZContentUpload\nodeAliasID
nodeAliasID( $nodeName)
Definition: ezcontentupload.php:1262
eZContentUpload\eZContentUpload
eZContentUpload( $params=false)
Definition: ezcontentupload.php:78
eZContentUpload\findStringAttribute
findStringAttribute( $dataMap, $nameAttribute)
Definition: ezcontentupload.php:986
eZContentUpload\findHTTPFileAttribute
findHTTPFileAttribute( $dataMap, $fileAttribute)
Definition: ezcontentupload.php:920
eZContentUpload\fetchHTTPFile
fetchHTTPFile( $httpFileIdentifier, &$errors, &$file, &$mimeData)
Definition: ezcontentupload.php:873
eZContentUpload\objectFileInfo
objectFileInfo( $contentObject)
Definition: ezcontentupload.php:838
eZContentUpload\detectClassIdentifier
detectClassIdentifier( $mime)
Definition: ezcontentupload.php:1015
eZContentUpload\attribute
attribute( $attributeName)
Definition: ezcontentupload.php:102
eZContentUpload\publishObject
publishObject(&$result, &$errors, &$notices, $object, $publishVersion, $class, $parentNodes, $parentMainNode)
Definition: ezcontentupload.php:770
eZContentUpload\cleanupAll
cleanupAll()
Definition: ezcontentupload.php:1354
eZContentUpload\findHandler
findHandler(&$result, $mimeInfo)
Definition: ezcontentupload.php:1367
eZContentUpload\processNamePattern
processNamePattern( $variables, $namePattern)
Definition: ezcontentupload.php:1211
eZContentUpload\attributes
attributes()
Definition: ezcontentupload.php:86
eZContentUpload\hasAttribute
hasAttribute( $attributeName)
Definition: ezcontentupload.php:94
eZContentUpload\detectLocations
detectLocations( $classIdentifier, $class, $location, &$parentNodes, &$parentMainNode)
Definition: ezcontentupload.php:1052
eZContentUpload\upload
static upload( $parameters=array(), $module)
Definition: ezcontentupload.php:119
eZContentUpload\__construct
__construct( $params=false)
Definition: ezcontentupload.php:59
eZContentUpload\$Parameters
$Parameters
Definition: ezcontentupload.php:1424
eZContentUpload\STATUS_PERMISSION_DENIED
const STATUS_PERMISSION_DENIED
Definition: ezcontentupload.php:51
eZContentUpload\cleanup
static cleanup( $actionName)
Definition: ezcontentupload.php:1344
eZContentUpload\handleLocalFile
handleLocalFile(&$result, $filePath, $location, $existingNode, $nameString='', $localeCode=false)
Definition: ezcontentupload.php:228
eZContentUpload\handleUpload
handleUpload(&$result, $httpFileIdentifier, $location, $existingNode, $nameString='', $localeCode=false, $publish=true)
Definition: ezcontentupload.php:480
eZContentUpload\findRegularFileAttribute
findRegularFileAttribute( $dataMap, $fileAttribute)
Definition: ezcontentupload.php:953
eZContentUpload\checkAccess
checkAccess( $nodeID, $class)
Definition: ezcontentupload.php:1179
eZContentUpload\result
static result( $actionName, $cleanup=true)
Definition: ezcontentupload.php:1320
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:459
eZExtension\activeExtensions
static activeExtensions( $extensionType=false, eZINI|null $siteINI=null)
Definition: ezextension.php:60
eZExtension\baseDirectory
static baseDirectory(eZINI|null $siteINI=null)
Definition: ezextension.php:39
eZHTTPFile
Provides access to HTTP post files.
Definition: ezhttpfile.php:23
eZHTTPFile\UPLOADEDFILE_MISSING_TMP_DIR
const UPLOADEDFILE_MISSING_TMP_DIR
Definition: ezhttpfile.php:28
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_UNKNOWN_ERROR
const UPLOADEDFILE_UNKNOWN_ERROR
Definition: ezhttpfile.php:30
eZHTTPFile\UPLOADEDFILE_EXCEEDS_PHP_LIMIT
const UPLOADEDFILE_EXCEEDS_PHP_LIMIT
Definition: ezhttpfile.php:26
eZHTTPFile\UPLOADEDFILE_CANT_WRITE
const UPLOADEDFILE_CANT_WRITE
Definition: ezhttpfile.php:29
eZHTTPFile\UPLOADEDFILE_OK
const UPLOADEDFILE_OK
Definition: ezhttpfile.php:24
eZHTTPFile\UPLOADEDFILE_DOES_NOT_EXIST
const UPLOADEDFILE_DOES_NOT_EXIST
Definition: ezhttpfile.php:25
eZHTTPTool\instance
static instance()
Definition: ezhttptool.php:189
eZMimeType\findByFileContents
static findByFileContents( $url, $returnDefault=true)
Definition: ezmimetype.php:308
eZModuleOperationInfo\STATUS_HALTED
const STATUS_HALTED
Definition: ezmoduleoperationinfo.php:27
eZModuleOperationInfo\STATUS_CANCELLED
const STATUS_CANCELLED
Definition: ezmoduleoperationinfo.php:26
eZModuleOperationInfo\STATUS_CONTINUE
const STATUS_CONTINUE
Definition: ezmoduleoperationinfo.php:25
eZOperationHandler\execute
static execute( $moduleName, $operationName, $operationParameters, $lastTriggerName=null, $useTriggers=true)
Definition: ezoperationhandler.php:42
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+.