/** * (called for each obj attribute) */ public function checkObjectAttribute(array $contentObjectAttribute) { // we adopt the ez api instead of acting on raw data $contentObjectAttribute = new eZContentObjectAttribute($contentObjectAttribute); $handler = $contentObjectAttribute->attribute('content'); $warnings = array(); // do not check attributes which do not even contain images if ($handler->attribute('is_valid')) { // get path to original file $original = $handler->attribute('original'); $filePath = $original['full_path']; // check if it is on fs (remember, images are clusterized) $file = eZClusterFileHandler::instance($filePath); if (!$file->exists()) { $warnings[] = array("Image file not found: {$filePath}" . $this->postfixErrorMsg($contentObjectAttribute)); } else { // if it is, check its size as well if ($this->maxSize > 0) { $maxSize = $this->maxSize * 1024 * 1024; if ($file->size() > $maxSize) { $warnings[] = "Image bigger than {$maxSize} bytes : " . $file->size() . $this->postfixErrorMsg($contentObjectAttribute); } } } // check if it is in custom table in db $image = eZImageFile::fetchByFilepath($contentObjectAttribute->attribute('id'), $filePath, false); if (!$image) { $warnings[] = "Image not found in ezimagefile table: {$filePath}" . $this->postfixErrorMsg($contentObjectAttribute); } } else { if (!$this->nullable) { $warnings[] = "Attribute is null and it should not be" . $this->postfixErrorMsg($contentObjectAttribute); } // q: are these old images possibly tied to older versions of the content ? /*$db = eZDB::instance(); $count = $db->arrayQuery('select count(*) as leftovers from ezimagefile where contentobject_attribute_id='.$contentObjectAttribute->attribute('id')); if($count[0]['leftovers']) { $warnings[] = "Leftovers in ezimageattribute table" . $this->postfixErrorMsg( $contentObjectAttribute ); }*/ } return $warnings; }
function trashStoredObjectAttribute($contentObjectAttribute, $version = null) { $contentObjectAttributeID = $contentObjectAttribute->attribute("id"); $imageHandler = $contentObjectAttribute->attribute('content'); $imageFiles = eZImageFile::fetchForContentObjectAttribute($contentObjectAttributeID); foreach ($imageFiles as $imageFile) { if ($imageFile == null) { continue; } $existingFilepath = $imageFile; // Check if there are any other records in ezimagefile that point to that filename. $imageObjectsWithSameFileName = eZImageFile::fetchByFilepath(false, $existingFilepath); $file = eZClusterFileHandler::instance($existingFilepath); if ($file->exists() and count($imageObjectsWithSameFileName) <= 1) { $orig_dir = dirname($existingFilepath) . '/trashed'; $fileName = basename($existingFilepath); // create dest filename in the same manner as eZHTTPFile::store() // grab file's suffix $fileSuffix = eZFile::suffix($fileName); // prepend dot if ($fileSuffix) { $fileSuffix = '.' . $fileSuffix; } // grab filename without suffix $fileBaseName = basename($fileName, $fileSuffix); // create dest filename $newFileBaseName = md5($fileBaseName . microtime() . mt_rand()); $newFileName = $newFileBaseName . $fileSuffix; $newFilepath = $orig_dir . '/' . $newFileName; // rename the file, and update the database data $imageHandler->updateAliasPath($orig_dir, $newFileBaseName); if ($imageHandler->isStorageRequired()) { $imageHandler->store($contentObjectAttribute); $contentObjectAttribute->store(); } } } }
public function restoreTrashedObjectAttribute($contentObjectAttribute) { $imageHandler = $contentObjectAttribute->attribute("content"); $originalAlias = $imageHandler->imageAlias("original"); $originalPath = str_replace("/trashed", "", $originalAlias["dirpath"]); $originalName = $imageHandler->imageName($contentObjectAttribute, $contentObjectAttribute->objectVersion()); $imageHandler->updateAliasPath($originalPath, $originalName); if ($imageHandler->isStorageRequired()) { $imageHandler->store($contentObjectAttribute); $contentObjectAttribute->store(); } // Now clean all other aliases, not cleanly registered within the attribute content // First get all remaining aliases full path to then safely remove them $aliasNames = array_keys($imageHandler->aliasList()); $aliasesPath = array(); foreach ($aliasNames as $aliasName) { if ($aliasName === "original") { continue; } $aliasesPath[] = "{$originalAlias["dirpath"]}/{$originalAlias["basename"]}_{$aliasName}.{$originalAlias["suffix"]}"; } if (empty($aliasesPath)) { return; } $conds = array("contentobject_attribute_id" => $contentObjectAttribute->attribute("id"), "filepath" => array($aliasesPath)); $remainingAliases = eZPersistentObject::fetchObjectList(eZImageFile::definition(), null, $conds); unset($conds, $remainingAliasesPath); if (!empty($remainingAliases)) { foreach ($remainingAliases as $remainingAlias) { $filename = basename($remainingAlias->attribute("filepath")); $newFilePath = $originalPath . "/" . $originalName . substr($filename, strrpos($filename, '_')); eZClusterFileHandler::instance($remainingAlias->attribute("filepath"))->move($newFilePath); // $newFilePath might have already been processed in eZImageFile // If so, $remainingAlias is a duplicate. We can then remove it safely $imageFile = eZImageFile::fetchByFilepath(false, $newFilePath, false); if (empty($imageFile)) { $remainingAlias->setAttribute("filepath", $newFilePath); $remainingAlias->store(); } else { $remainingAlias->remove(); } } } }
static function removeFilepath( $contentObjectAttributeID, $filepath ) { if ( empty( $filepath ) ) return false; $fileObject = eZImageFile::fetchByFilepath( $contentObjectAttributeID, $filepath ); if ( !$fileObject ) return false; $fileObject->remove(); return true; }
/** * Removes all the image aliases and their information. * The stored images will also be removed if the attribute is the owner * of the images. * * After the images are removed the attribute will containe an internal * structure with empty data * * @param eZContentObjectAttribute $contentObjectAttribute * Content object attribute to remove aliases for * * @return void */ function removeAliases($contentObjectAttribute) { $aliasList = $this->aliasList(); $alternativeText = false; $contentObjectAttributeVersion = $this->ContentObjectAttributeData['version']; $contentObjectAttributeID = $this->ContentObjectAttributeData['id']; $isImageOwner = $this->isImageOwner(); // We loop over each image alias, and look up the file in ezcontentobject_attribute // Only images referenced by one version will be removed foreach ($aliasList as $aliasName => $alias) { $dirpath = $alias['dirpath']; $doNotDelete = false; // Do not delete files from storage if ($aliasName == 'original') { $alternativeText = $alias['alternative_text']; } if ($alias['is_valid']) { $filepath = $alias['url']; /** * If there are no ezimagefile references to this file with this content object attribute id, * we are dealing with an attribute that was copied from another content. * The image doesn't belong to us, and we just don't do anything. * See http://jira.ez.no/browse/EZP-21324 */ if (!eZImageFile::fetchByFilepath($contentObjectAttributeID, $filepath)) { continue; } // Fetch ezimage attributes that use $filepath // Always returns current attribute (array of $contentObjectAttributeID and $contentObjectAttributeVersion) $dbResult = eZImageFile::fetchImageAttributesByFilepath($filepath, $contentObjectAttributeID); $dbResultCount = count($dbResult); // Check if there are the attributes. if ($dbResultCount > 0) { $doNotDelete = true; foreach ($dbResult as $res) { // We only look results where the version matches if ($res['version'] == $contentObjectAttributeVersion) { // If more than one result has been returned, it means // that another version is using the same image, // and we should not delete this file if ($dbResultCount > 1) { continue; } else { $doNotDelete = false; } } eZImageFile::appendFilepath($res['id'], $filepath, true); } } if (!$doNotDelete) { eZImageFile::removeFilepath($contentObjectAttributeID, $filepath); $file = eZClusterFileHandler::instance($filepath); if ($file->exists()) { $file->delete(); eZDir::cleanupEmptyDirectories($dirpath); } else { eZDebug::writeError("Image file {$filepath} for alias {$aliasName} does not exist, could not remove from disk", __METHOD__); } } } } $doc = new DOMDocument('1.0', 'utf-8'); $imageNode = $doc->createElement("ezimage"); $doc->appendChild($imageNode); $imageNode->setAttribute('serial_number', false); $imageNode->setAttribute('is_valid', false); $imageNode->setAttribute('filename', false); $imageNode->setAttribute('suffix', false); $imageNode->setAttribute('basename', false); $imageNode->setAttribute('dirpath', false); $imageNode->setAttribute('url', false); $imageNode->setAttribute('original_filename', false); $imageNode->setAttribute('mime_type', false); $imageNode->setAttribute('width', false); $imageNode->setAttribute('height', false); $imageNode->setAttribute('alternative_text', $alternativeText); $imageNode->setAttribute('alias_key', false); $imageNode->setAttribute('timestamp', false); $this->ContentObjectAttributeData['DataTypeCustom']['dom_tree'] = $doc; unset($this->ContentObjectAttributeData['DataTypeCustom']['alias_list']); $this->storeDOMTree($doc, true, $contentObjectAttribute); }