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