Skip to content

Commit

Permalink
Merge pull request #9 from blankse/check_image_aliases
Browse files Browse the repository at this point in the history
Check Image Aliases
  • Loading branch information
gggeek authored Dec 4, 2020
2 parents bb11401 + 3aa3178 commit 3e890b3
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions classes/ezdbistoragecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ezdbiStorageChecker extends ezdbiBaseChecker
protected $checks = array(
'Images' => 'checks for any image file in the storage dir which are not in the ezimage table',
'Files' => 'checks for any binary file in the storage dir which are not in the ezmedia or ezbinaryfile tables',
'ImagesAliases' => 'checks for any image alias file in the _aliases dir whithout original image file',
);

public function __construct( $dsn='' )
Expand Down Expand Up @@ -59,6 +60,8 @@ public function check( $type, $doDelete=false, $returnData=false )
return $this->checkImages( $doDelete, $returnData );
case 'Files':
return $this->checkFiles( $doDelete, $returnData );
case 'ImagesAliases':
return $this->checkImageAliases( $doDelete, $returnData );
default:
throw new \Exception( "Unsupported type: '$type'" );
}
Expand Down Expand Up @@ -204,6 +207,62 @@ public function checkFiles( $doDelete=false, $returnData=false )
return $violations;
}

public function checkImageAliases( $doDelete = false, $returnData = false )
{
$violations = array();

$ini = eZINI::instance( 'image.ini' );
$pDir = $this->clusterizeDir( eZSys::storageDirectory() . '/' . $ini->variable( 'FileSettings', 'PublishedImages' ) );
$aliasDir = realpath( $pDir . '/_aliases' );

if ( is_dir( $aliasDir ) ) {
foreach ( glob( $aliasDir . '/*' ) as $aliasNameDir )
{
if ( is_file( $aliasNameDir ) )
{
continue;
}

$aliasName = basename( $aliasNameDir );
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $aliasNameDir ) );

foreach ( $files as $aliasFileName => $aliasFileInfo )
{
if ( $aliasFileInfo->isDir() )
{
continue;
}

$originalFileName = str_replace( '/_aliases/' . $aliasName, '', $aliasFileName );

if ( !file_exists( $originalFileName ) )
{
if ( isset( $violations['violatingFileCount'] ) )
{
$violations['violatingFileCount']++;
}
else
{
$violations['violatingFileCount'] = 1;
}

if ( $returnData )
{
$violations['violatingFiles'][] = $aliasFileName;
}

if ($doDelete)
{
unlink($aliasFileName);
}
}
}
}
}

return $violations;
}

protected function clusterizeDir( $dir )
{
$ini = eZINI::instance( 'file.ini');
Expand Down

0 comments on commit 3e890b3

Please sign in to comment.