dc2-photoblog/thumbnail_cleanup.php

105 lines
3.8 KiB
PHP

<?php
function file_cleanup()
{
$return = '<h4>' . __('Parsing directory') . ' ' . dcPhotoblog::getImagePath() . '...</h4>';
$return .= treatDirectory(dcPhotoblog::getImagePath());
$return .= '<h3>' . __('All done!') . '</h3>';
return $return;
}
function thumbnail_cleanup($DC12_style_naming = false)
{
$sql = 'select post_id, post_title, post_dt from '.dcCore::app()->prefix.dcBlog::POST_TABLE_NAME.' where blog_id = \''.dcCore::app()->blog->id.'\' order by post_dt ASC';
try {
$result = dcCore::app()->con->select($sql);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
return "error: " . $e->getMessage();
}
$return = '';
while ($result->fetch()) {
$img = dcPhotoblog::getPostImage($result->f('post_id'));
$return .= "<h4>";
$return .= sprintf(__('Post with ID %d and title "%s"'), $result->f('post_id'), $result->f('post_title'));
$return .= "</h4>\n";
if (!preg_match('!'.dcCore::app()->blog->settings->system->public_url.'(.+)!i', $img)) {
$return .= '<p>' . __('No local image found') . '</p>';
continue;
}
if ($DC12_style_naming === true) {
$imgBWThumb = getLocalFile(dcPhotoBlog::getDC12ImageThumbFileName($img, true, true));
$imgColorThumb = getLocalFile(dcPhotoBlog::getDC12ImageThumbFileName($img, false, true));
$imgNavBWThumb = getLocalFile(dcPhotoBlog::getDC12ImageThumbFileName($img, true, false));
$imgNavColorThumb = getLocalFile(dcPhotoBlog::getDC12ImageThumbFileName($img, false, false));
} else {
$imgBWThumb = getLocalFile(dcPhotoBlog::getImageThumbFileName($img, true, true));
$imgColorThumb = getLocalFile(dcPhotoBlog::getImageThumbFileName($img, false, true));
$imgNavBWThumb = getLocalFile(dcPhotoBlog::getImageThumbFileName($img, true, false));
$imgNavColorThumb = getLocalFile(dcPhotoBlog::getImageThumbFileName($img, false, false));
}
$return .= '<ul><li>' . __('Color thumbnail...') . ' ';
$return .= treatFile($imgColorThumb);
$return .= '</li>';
$return .= '<li>' . __('B&W thumbnail...') . ' ';
$return .= treatFile($imgBWThumb);
$return .= '</li>';
$return .= '<li>' . __('Color thumbnail for navigation...') . ' ';
$return .= treatFile($imgNavColorThumb);
$return .= '</li>';
$return .= '<li>' . __('B&W thumbnail for navigation...') . ' ';
$return .= treatFile($imgNavBWThumb);
$return .= '</li>';
$return .= '</ul>';
}
$return .= '<h3>' . __('All done!') . '</h3>';
return $return;
}
function treatDirectory($path)
{
$dir = @dir($path);
$return = '';
if ($dir) {
while (($entry = $dir->read()) !== false) {
$entry_path = $dir->path.'/'.$entry;
if (is_file($entry_path) && preg_match('/TN__\\.\\w+$/', $entry)) {
$return .= '<p>' . __('Removing file') . " $entry... ";
$return .= treatFile($entry_path);
$return .= '</p>';
} elseif (is_dir($entry_path) && !preg_match('!/[.]{1,2}$!', $entry_path)) {
$return .= treatDirectory($entry_path);
}
}
}
return $return;
}
function treatFile($imgFile)
{
$return = '';
if (file_exists($imgFile)) {
$return = __('found') . ' ';
if (unlink($imgFile)) {
$return .= __('and deleted');
} else {
$return .= __('deletion failed!');
}
} else {
$return = __('not found!');
}
return $return;
}
function getLocalFile($imgFile)
{
$localFile = preg_replace('!'.dcCore::app()->blog->settings->system->public_url.'!i', '', $imgFile);
$img_p = dcPhotoBlog::getImagePath();
return $img_p . '/' . $localFile;
}