File: validate_albums.php

package info (click to toggle)
gallery 1.5-1sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 21,172 kB
  • ctags: 4,516
  • sloc: php: 26,456; sh: 427; perl: 188; makefile: 58; xml: 48
file content (300 lines) | stat: -rw-r--r-- 8,895 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: validate_albums.php,v 1.1.2.3 2005/04/04 16:08:02 cryptographite Exp $
 */
?>
<?php

require(dirname(dirname(__FILE__)) . '/init.php');

// Security check
if (!$gallery->user->isAdmin()) {
	header("Location: " . makeAlbumHeaderUrl());
        exit;
}

// Ensure that the results we get aren't due to caching
clearstatcache();

$results = array(
		'file_missing' => array(),
		'invalid_album' => array(),
	    );

function findInvalidAlbums() {
    global $gallery;
    global $results;

    $albumsDir = opendir($gallery->app->albumDir);

    while (($file = readdir($albumsDir)) !== false) {
	$albumPath = $gallery->app->albumDir . '/' . $file;
	if (fs_is_dir($albumPath)) {
	    if ($file[0] == '.' ||
		    $file == 'CVS' ||
		    $file == '_vti_cnf') {
		continue;
	    } else {

		// Load the album - if it fails, it's invalid
		$album = new Album();
		if (!$album->load($file)) {
		    $results['invalid_album'][] = $file;
		    continue;
		}

		// Determine if the album is missing any essential files
		findMissingFiles($album, $albumPath);
	    }
	}
    }
    closedir($albumsDir);

    sort($results['file_missing']);
    sort($results['invalid_album']);
}

function findMissingFiles($album, $albumPath) {
    global $gallery;
    global $results;

    // Try to ensure we'll have enough time to process this album
    @set_time_limit($gallery->app->time_limit);

    /* 
     * Try and load each photo and examine its physical file
     * if the file doesn't exist, we flag it.
     */
    for ($i = 1; $i <= sizeof($album->photos); $i++) {
	$photo = $album->getPhoto($i);

	// Albums will be tested on their own
	if ($photo->isAlbum()) {
	    continue;
	}

	// Get the file path and verify
	$photoPath = $photo->getPhotoPath($albumPath, true);
	if (!fs_file_exists($photoPath)) {
	    // album/filename.ext
	    $results['file_missing'][] = substr($photoPath, strlen($gallery->app->albumDir) + 1);
	}
    }
}

function removeInvalidAlbum($path) {
    $removePath = opendir($path);
    while (($file = readdir($removePath)) !== false) {
	if ($file == '.' || $file == '..') {
	    continue;
	}
	
	if (fs_is_dir($path . '/' . $file)) {
	    removeInvalidAlbum($path . '/' . $file);
	} else {
	    unlink($path . '/' . $file);
	}
    }
    closedir($removePath);
    rmdir($path);
}

$action = getRequestVar('action');
if (empty($action)) {
    findInvalidAlbums();
} else {
   if (!$GALLERY_EMBEDDED_INSIDE) {
        doctype();
?>
<html>
<head>
  <title><?php echo ($action == 'unlinkInvalidAlbum') ? _("Delete Album") : _("Delete Photo") ?></title>
  <?php common_header(); ?>
</head>
<body dir="<?php echo $gallery->direction ?>" class="popupbody">
<?php
    }
?>
<div class="popuphead"><?php echo ($action == 'unlinkInvalidAlbum') ? _("Delete Album") : _("Delete Photo") ?></div>
<div class="popup" align="center">
<?php
    switch ($action) {
	case 'unlinkInvalidAlbum':
	    list ($verified, $invalidAlbum) = getRequestVar(array('verified', 'invalidAlbum'));
	    if ($verified) {
		removeInvalidAlbum($gallery->app->albumDir . '/' . $invalidAlbum);
		dismiss();
	    } else {
		echo makeFormIntro('tools/validate_albums.php', array(), array('action' => $action, 'invalidAlbum' =>
$invalidAlbum));
		echo "<p><input type='submit' name='verified' value='Delete $invalidAlbum'></p>";
		echo "</form>";
	    }
	    break;

	case 'deleteMissingPhoto':
	    list ($verified, $album, $id) = getRequestVar(array('verified', 'album', 'id'));
	    if ($verified) {
		$targetAlbum = new Album();
		$targetAlbum->load($album);
		$photoIndex = $targetAlbum->getPhotoIndex($id);
		$targetAlbum->deletePhoto($photoIndex);
		$targetAlbum->save(array(i18n("Photo $id deleted from $album because the target image file is missing")));
		dismiss();
	    } else {
		echo makeFormIntro('tools/validate_albums.php', array(), array('action' => $action, 'album' => $album,
'id' => $id));
		$targetAlbum = new Album();
		$targetAlbum->load($album);
		echo $targetAlbum->getThumbnailTagById($id);
		echo "<p><input type='submit' name='verified' value='Delete $album/$id'></p>";
		echo "<p>" . _("Please Note: Even if the thumbnail image is properly displayed above, the actual full-sized image has been verified to be missing.") . "</p>";
		echo "</form>";
	    }
	    break;

	default:
	    echo '<p>Invalid Action</p>';
	    break;
	}
	?>
</div>
</body>
</html>
<?php
    exit;
}

if (!$GALLERY_EMBEDDED_INSIDE) {
	doctype();
?>
<html>
<head>
<title><?php echo $gallery->app->galleryTitle ?>::<?php echo _("Validate Albums") ?></title>
<?php 
	common_header();
?>
</head>
<body dir="<?php echo $gallery->direction ?>" class="popupbody">
<?php 
} 
        includeHtmlWrap("gallery.header");
?>
<div class="popuphead"><?php echo _("Validate Albums") ?></div>
<?php

$adminCommands = '[<a href="'. makeGalleryUrl("admin-page.php") .'">'. _("return to admin page") .'</a>] ';
$adminCommands .= '[<a href="'. makeAlbumUrl() .'">'. _("return to gallery") .'</a>] ';

$adminbox["commands"] = $adminCommands;
$adminbox["bordercolor"] = $gallery->app->default["bordercolor"];
includeLayout('adminbox.inc');
includeLayout('ml_pulldown.inc');

echo '<div class="popup">';
if (empty($action)) {
	if (!empty($results['file_missing'])) { ?>
		<p><?php echo _("Missing Files:") . " " . sizeof($results['file_missing']) ?></p>
		<p><?php echo _("The following files are missing from the albums directory.  Information is still stored about the photo in the album data, but the file itself is no longer present for some reason.  These files will cause failures when attempting to migrate to Gallery 2.0. This can be fixed in one of two ways; the first is to simply delete the photo entry from the album.  The second is to manually re-add the file to the albums directory using the filename you see in the left side of the table.") ?></p>
		<center>
		<table>
		<tr>
			<th><?php echo _("Missing File") ?></th>
			<th>&nbsp;</th>
			<th><?php echo _("Action") ?></th>
		</tr>
<?php
		foreach ($results['file_missing'] as $fileName) {
			$contents = split('/', $fileName);
			$contents[1] = substr($contents[1], 0, strrpos($contents[1], '.'));
			echo "\t<tr>";
			echo "\n\t<td><a href='" . makeAlbumUrl($contents[0], $contents[1]) . "'>" . $fileName . "</a></td>";
			echo "\n\t<td>=&gt;</td>";			
			echo "\n\t<td>" . popup_link(_('delete photo'), 
				"tools/validate_albums.php?action=deleteMissingPhoto" . 
					"&album={$contents[0]}&id={$contents[1]}", 
				false, true, 500, 500) . '</td>';
			echo "\n\t</tr>";
		}
?>
		</table>
		<br>
		</center>
<?php
	} else {
		// No Orphans
?>
		<center>
		<table>
		<tr><th><?php echo _("No Missing Files") ?></th></tr>
		<tr><td><?php echo _("There are no missing files in this Gallery.") ?></td></tr>
		</table>
		</center>
<?php
	}

	if (!empty($results['invalid_album'])) {
?>

		<p><?php echo _("Invalid Albums:") . " " . sizeof($results['invalid_album']) ?></p>
		<p><?php echo _("Invalid Albums are directories which have been created in the albums directory that don't actually contain album data.  The presence of these directories can cause problems for Gallery as well as when trying to migrate to Gallery 2.0") ?></p>
		<center>
		<table>
		<tr>
			<th><?php echo _("Invalid Album") ?></th>
			<th>&nbsp;</th>
			<th><?php echo _("Action") ?></th>
		</tr>
<?php
		foreach ($results['invalid_album'] as $invalidAlbum) {
			echo "\n\t<tr>";
			echo "\n\t<td>$invalidAlbum</td>";
			echo "\n\t<td>=&gt;</td>";
			echo "\n\t<td>" . popup_link(_('delete directory'), 
				"tools/validate_albums.php?action=unlinkInvalidAlbum" . 
					"&invalidAlbum={$invalidAlbum}", 
				false, true, 500, 500) . '</td>';
			echo "\n\t\t</tr>";
		}       
?>
		</table>
		<br>
		</center>
<?php 
	} else {
		// No Orphans
?>
		<center>
		<table>
		<tr><th><?php echo _("No Invalid Albums Found") ?></th></tr>
		<tr><td><?php echo _("There are no invalid albums present in this Gallery.") ?></td></tr>
		</table>
		</center>
<?php
	}
}

includeHtmlWrap("gallery.footer"); 
if (!$GALLERY_EMBEDDED_INSIDE) {
?>
</div>
</body>
</html>
<?php } ?>