1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211:
<?php
if (defined('OFFSET_PATH')) {
$plugin_is_filter = 5 | ADMIN_PLUGIN;
$plugin_description = gettext("Provides a utility SEO file/folder name clenser.");
$plugin_author = "Stephen Billard (sbillard)";
$plugin_category = gettext('SEO');
zp_register_filter('admin_utilities_buttons', 'seo_cleanup_button');
function seo_cleanup_button($buttons) {
$buttons[] = array(
'XSRFTag' => 'seo_cleanup',
'category' => gettext('Seo'),
'enable' => true,
'button_text' => gettext('SEO Cleaner'),
'formname' => 'seo_cleanup.php',
'action' => FULLWEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/seo_cleanup.php',
'icon' => FULLWEBPATH . '/' . ZENFOLDER . '/images/redo.png',
'title' => gettext('Make file and folder names in the Gallery SEO friendly.'),
'alt' => '',
'hidden' => '',
'rights' => ADMIN_RIGHTS
);
return $buttons;
}
} else {
define('OFFSET_PATH', 3);
require_once(dirname(dirname(__FILE__)) . '/admin-globals.php');
admin_securityChecks(ALBUM_RIGHTS, currentRelativeURL());
XSRFdefender('seo_cleanup');
function getE($e) {
switch ($e) {
case 2:
return gettext("Image already exists.");
case 3:
return gettext("Album already exists.");
case 4:
return gettext("Cannot move, copy, or rename to a subalbum of this album.");
case 5:
return gettext("Cannot move, copy, or rename to a dynamic album.");
case 6:
return gettext('Cannot rename an image to a different suffix');
case 7:
return gettext('Album delete failed');
default:
return sprintf(gettext("There was an error #%d with the rename operation."), $e);
}
}
function cleanAlbum($obj) {
global $albumcount, $_zp_current_admin_obj;
$subalbum = $obj->name;
$file = basename($subalbum);
$seoname = seoFriendly($file);
$album_cleaned = false;
if ($seoname != $file) {
$newname = dirname($subalbum);
if (empty($newname) || $newname == '.') {
$newname = $seoname;
} else {
$newname .= '/' . $seoname;
}
if ($e = $obj->rename($newname)) {
$error = getE($e, $subalbum, $newname);
printf(gettext('<em>%1$s</em> rename to <em>%2$s</em> failed: %3$s'), $subalbum, $newname, $error);
echo "<br />\n";
} else {
$obj->setLastChangeUser($_zp_current_admin_obj->getUser());
$obj->save();
clearstatcache();
printf(gettext('<em>%1$s</em> renamed to <em>%2$s</em>'), $subalbum, $newname);
echo "<br />\n";
$albumcount++;
$obj = newAlbum($newname);
removeDir(SERVERCACHE . '/' . $subalbum);
if (extensionEnabled('static_html_cache')) {
Gallery::clearCache(SERVERPATH . '/' . STATIC_CACHE_FOLDER);
}
$album_cleaned = true;
}
}
if (!$obj->isDynamic())
checkFolder($obj, $album_cleaned);
}
function checkFolder($album, $album_cleaned) {
global $count, $albumcount, $_zp_current_admin_obj;
$subalbums = $album->getAlbums(0);
foreach ($subalbums as $subalbum) {
$obj = newAlbum($subalbum);
cleanAlbum($obj);
}
$folder = $album->name . '/';
$files = $album->getImages(0);
foreach ($files as $filename) {
$seoname = seoFriendly($filename);
if (stripSuffix($seoname) != stripSuffix($filename)) {
$image = newImage($album, $filename);
if ($e = $image->rename($seoname)) {
$error = getE($e, $filename, $seoname);
printf(gettext('<em>%1$s</em> rename to <em>%2$s</em> failed: %3$s'), $folder . $filename, $seoname, $error);
echo "<br />\n";
} else {
$image->setLastChangeUser($_zp_current_admin_obj->getUser());
$image->save();
clearstatcache();
echo ' ';
printf(gettext('<em>%1$s</em> renamed to <em>%2$s</em>'), $folder . $filename, $seoname);
echo "<br />\n";
$count++;
if(!$album_cleaned) {
Gallery::clearCache(SERVERCACHE . '/' . $album->name);
if(extensionEnabled('static_html_cache')) {
Gallery::clearCache(SERVERPATH . '/' . STATIC_CACHE_FOLDER);
}
}
}
}
}
}
$_zp_gallery->garbageCollect();
$zenphoto_tabs['overview']['subtabs'] = array(gettext('SEO cleaner') => FULLWEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/seo_cleanup.php?XSRFToken=' . getXSRFToken('seo_cleanup'));
printAdminHeader('overview', 'SEO cleaner');
if (isset($_GET['todo'])) {
$count = sanitize_numeric($_GET['imagecount']);
$albumcount = sanitize_numeric($_GET['albumcount']);
$albums = array();
foreach (explode(',', sanitize($_GET['todo'])) as $album) {
$albums[] = sanitize($album);
}
} else {
$count = 0;
$albumcount = 0;
$albums = $_zp_gallery->getAlbums();
}
?>
<?php echo '</head>'; ?>
<body>
<?php printLogoAndLinks(); ?>
<div id="main">
<?php printTabs(); ?>
<div id="content">
<?php printSubtabs(); ?>
<div class="tabbox">
<?php zp_apply_filter('admin_note', 'seo_cleanup', ''); ?>
<h1><?php echo gettext('Cleanup album and image names to be SEO friendly'); ?></h1>
<?php
foreach ($albums as $album) {
$obj = newAlbum($album);
cleanAlbum($obj);
}
if ($albumcount || $count) {
?>
<div class="notebox">
<p>
<?php
if ($albumcount) {
printf(ngettext('%d album cleaned.', '%d albums cleaned.', $albumcount), $albumcount);
} else {
echo gettext('No albums cleaned.');
}
?>
</p>
<p>
<?php
if ($count) {
printf(ngettext('%d image cleaned.', '%d images cleaned.', $count), $count);
} else {
echo gettext('No images cleaned.');
}
?>
</p>
</div>
<?php
} else {
?>
<p class="notebox"><?php echo gettext('No albums or images cleaned up.'); ?></p>
<?php
}
?>
</div>
</div><!-- content -->
</div><!-- main -->
<?php printAdminFooter(); ?>
</body>
<?php
echo "</html>";
}
?>