1<?php
2/**
3 * This template is used to generate cache images. Running it will process the entire gallery,
4 * supplying an album name (ex: loadAlbums.php?album=newalbum) will only process the album named.
5 * Passing clear=on will purge the designated cache before generating cache images
6 * @package plugins
7 * @subpackage cachemanager
8 */
9// force UTF-8 Ø
10define('OFFSET_PATH', 3);
11require_once("../../admin-globals.php");
12require_once(SERVERPATH . '/' . ZENFOLDER . '/functions-image.php');
13require_once(SERVERPATH . '/' . ZENFOLDER . '/template-functions.php');
14
15
16if (isset($_REQUEST['album'])) {
17	$localrights = ALBUM_RIGHTS;
18} else {
19	$localrights = NULL;
20}
21admin_securityChecks($localrights, $return = currentRelativeURL());
22
23if (isset($_GET['action'])) {
24	$action = sanitize($_GET['action']);
25	if ($action == 'cleanup_cache_sizes') {
26		XSRFdefender('CleanupCacheSizes');
27		cacheManager::cleanupCacheSizes();
28		$report = gettext('Image cache sizes cleaned up.');
29		redirectURL(FULLWEBPATH .'/'. ZENFOLDER. '/admin.php?action=external&msg=' . $report);
30	}
31}
32
33if (isset($_GET['album'])) {
34	$alb = sanitize($_GET['album']);
35} else if (isset($_POST['album'])) {
36	$alb = sanitize(urldecode($_POST['album']));
37} else {
38	$alb = '';
39}
40if ($alb) {
41	$folder = sanitize_path($alb);
42	$object = $folder;
43	$tab = 'edit';
44	$album = newAlbum($folder);
45	if (!$album->isMyItem(ALBUM_RIGHTS)) {
46		if (!zp_apply_filter('admin_managed_albums_access', false, $return)) {
47			redirectURL(FULLWEBPATH . '/' . ZENFOLDER . '/admin.php');
48		}
49	}
50} else {
51	$object = '<em>' . gettext('Gallery') . '</em>';
52	$zenphoto_tabs['overview']['subtabs'] = array(
53			gettext('Cache images') => FULLWEBPATH .'/'. ZENFOLDER .'/' . PLUGIN_FOLDER . '/cacheManager/cacheImages.php?page=overview&tab=images',
54			gettext('Cache stored images') => FULLWEBPATH .'/'. ZENFOLDER . '/' . PLUGIN_FOLDER . '/cacheManager/cacheDBImages.php?page=overview&tab=DB&XSRFToken=' . getXSRFToken('cacheDBImages'));
55}
56cacheManager::$sizes = cacheManager::getSizes('active');
57
58if (isset($_GET['select']) && isset($_POST['enable'])) {
59	XSRFdefender('cacheImages');
60	$enabled_sizes = sanitize($_POST['enable']);
61	if(!is_array($enabled_sizes) || empty($enabled_sizes)) {
62		$enabled_sizes = array();
63	}
64	cacheManager::$enabledsizes = $enabled_sizes;
65} else {
66	cacheManager::$enabledsizes = array();
67}
68printAdminHeader('overview', 'images'); ?>
69</head>
70<body>
71<?php printLogoAndLinks(); ?>
72<div id = "main">
73<?php printTabs(); ?>
74<div id = "content">
75<?php printSubtabs(); ?>
76<div class="tabbox">
77	<?php
78	zp_apply_filter('admin_note', 'cache', '');
79	$clear = sprintf(gettext('Refreshing cache for %s'), $object);
80
81	if ($alb) {
82		$returnpage = '/admin-edit.php?page = edit&album = ' . $alb;
83		echo "\n<h2>" . $clear . "</h2>";
84	} else {
85		$returnpage = '/admin.php';
86		echo "\n<h2>" . $clear . "</h2>";
87	}
88
89	$cachesizes = 0;
90	$currenttheme = $_zp_gallery->getCurrentTheme();
91	$themes = array();
92	foreach ($_zp_gallery->getThemes() as $theme => $data) {
93		$themes[$theme] = $data['name'];
94	}
95	$last = '';
96	cacheManager::printJS();
97	cacheManager::printCurlNote();
98	if (empty(cacheManager::$enabledsizes)) {
99		?>
100		<p>
101			<?php echo gettext('This tool searches uncached image sizes from your albums or within a theme or plugin if they are registered to the cacheManager properly. If uncached images sizes exist you can have this tool generate these. If you like to re-generate existing cache image sizes, you have to clear the image cache manually first.'); ?>
102		</p>
103		<p class="notebox"><?php echo gettext('Note that this is a quite time and server power consuming measure depending on the number of images to pre-cache, their dimensions and the power of your server.'); ?>
104			<?php echo gettext('If your server is not able to process all albums and images try one album after another from each album edit page. Also remember that Zenphoto will create any size on the fly right when needed.'); ?>
105		</p>
106		<?php
107	}
108	?>
109	<form class="dirty-check clearfix" name="size_selections" action="?select&album=<?php echo $alb; ?>" method="post" autocomplete="off">
110			<?php XSRFToken('cacheImages') ?>
111		<ol class="no_bullets">
112			<?php
113			$defaultsizes = array(
114					array(
115							'option' => 'cache_full_image',
116							'key' => '*',
117							'text' => gettext('Full Image')),
118					array(
119							'option' => 'cachemanager_defaultthumb',
120							'key' => 'defaultthumb',
121							'text' => gettext('Default thumb size (or manual crop)')),
122					array(
123							'option' => 'cachemanager_defaultsizedimage',
124							'key' => 'defaultsizedimage',
125							'text' => gettext('Default sized image size'))
126			);
127			foreach($defaultsizes as $defaultsize) {
128				if (getOption($defaultsize['option']) && (empty(cacheManager::$enabledsizes) || array_key_exists($defaultsize['key'], cacheManager::$enabledsizes))) {
129					if (!empty(cacheManager::$enabledsizes)) {
130						$checked = ' checked="checked" disabled="disabled"';
131					} else {
132						if(in_array($defaultsize['key'], array('defaultthumb', 'defaultsizedimage'))) {
133							$checked = ' checked="checked"';
134						} else {
135							$checked = '';
136						}
137					}
138					$cachesizes++;
139					cacheManager::printSizesListEntry($defaultsize['key'], $checked, $defaultsize['text']);
140				}
141			}
142			$seen = array();
143			foreach (cacheManager::$sizes as $key => $cacheimage) {
144				if ((empty(cacheManager::$enabledsizes) || array_key_exists($key, cacheManager::$enabledsizes))) {
145					$checked = '';
146					if (array_key_exists($key, cacheManager::$enabledsizes)) {
147						$checked = ' checked="checked" disabled="disabled"';
148					} else {
149						if ($currenttheme == $cacheimage['theme'] || $cacheimage['theme'] == 'admin') {
150							$checked = ' checked="checked"';
151						}
152					}
153					$cachesizes++;
154					$size = isset($cacheimage['image_size']) ? $cacheimage['image_size'] : NULL;
155					$width = isset($cacheimage['image_width']) ? $cacheimage['image_width'] : NULL;
156					$height = isset($cacheimage['image_height']) ? $cacheimage['image_height'] : NULL;
157					$cw = isset($cacheimage['crop_width']) ? $cacheimage['crop_width'] : NULL;
158					$ch = isset($cacheimage['crop_height']) ? $cacheimage['crop_height'] : NULL;
159					$cx = isset($cacheimage['crop_x']) ? $cacheimage['crop_x'] : NULL;
160					$cy = isset($cacheimage['crop_y']) ? $cacheimage['crop_y'] : NULL;
161					$thumbstandin = isset($cacheimage['thumb']) ? $cacheimage['thumb'] : NULL;
162					$effects = isset($cacheimage['gray']) ? $cacheimage['gray'] : NULL;
163					$passedWM = isset($cacheimage['wmk']) ? $cacheimage['wmk'] : NULL;
164					$args = array($size, $width, $height, $cw, $ch, $cx, $cy, NULL, $thumbstandin, NULL, $thumbstandin, $passedWM, NULL, $effects);
165					$postfix = getImageCachePostfix($args);
166					if (isset($cacheimage['maxspace']) && $cacheimage['maxspace']) {
167						if ($width && $height) {
168							$postfix = str_replace('_w', '_wMax', $postfix);
169							$postfix = str_replace('_h', '_hMax', $postfix);
170						} else {
171							$postfix = '_' . gettext('invalid_MaxSpace');
172							$checked .= ' disabled="disabled"';
173						}
174					}
175					$themeid = $theme = $cacheimage['theme'];
176					if (isset($themes[$theme])) {
177						$themeid = $themes[$theme];
178					}
179					if ($theme != $last && empty(cacheManager::$enabledsizes)) {
180						if ($last) {
181							?>
182						</ol>
183						</span>
184						</li>
185						<?php
186					}
187					$last = $theme;
188					?>
189					<li>
190						<span class="icons" id="<?php echo $theme; ?>_arrow">
191							<a href="javascript:showTheme('<?php echo $theme; ?>');" title="<?php echo gettext('Show'); ?>">
192								<img class="icon-position-top4" src="<?php echo WEBPATH . '/' . ZENFOLDER . '/images/arrow_down.png'; ?>" alt="" />
193							</a>
194						</span>
195						<label>
196							<input type="checkbox" name="<?php echo $theme; ?>" id="<?php echo $theme; ?>" value="" onclick="checkTheme('<?php echo $theme; ?>');"<?php echo $checked; ?> /> <?php printf(gettext('all sizes for <i>%1$s</i>'), $themeid); ?>
197						</label>
198						<span id="<?php echo $theme; ?>_list" style="display:none">
199							<ol class="no_bullets">
200								<?php
201							}
202							$show = true;
203							if (!empty(cacheManager::$enabledsizes)) {
204								if (array_key_exists($postfix, $seen)) {
205									$show = false;
206									unset(cacheManager::$sizes[$key]);
207								}
208								$seen[$postfix] = true;
209							}
210							if ($show) {
211								cacheManager::printSizesListEntry($key, $checked, ltrim($postfix, '_'), $theme);
212							}
213						}
214					}
215					if (empty(cacheManager::$enabledsizes)) {
216						?>
217					</ol>
218				</span>
219			</li>
220			<?php
221		}
222		?>
223		</ol>
224		<?php
225		$button = false;
226		if (!empty(cacheManager::$enabledsizes)) {
227			if ($cachesizes) {
228				$allalbums = array();
229				if($alb) {
230					$allalbums[] = $alb;
231				} else {
232					$allalbums = $_zp_gallery->getAlbums();
233				}
234
235				//progress count
236				cacheManager::$albums_cached = 0;
237				cacheManager::$images_cached = 0;
238				cacheManager::$imagesizes_cached = 0;
239				cacheManager::$imagesizes_failed = 0;
240
241				// general counts
242				if ($alb) {
243					$albobj = newAlbum($alb);
244					$images_total = $albobj->getNumAllImages();
245					$imagesizes_total = $images_total * $cachesizes;
246					$albums_total = $albobj->getNumAllAlbums() + 1; // the album itself counts, too ;)
247					unset($albobj);
248				} else {
249					$images_total = $_zp_gallery->getNumImages();
250					$imagesizes_total = $_zp_gallery->getNumImages() * $cachesizes;
251					$albums_total = $_zp_gallery->getNumAlbums(true);
252				}
253				?>
254				<p><?php printf(ngettext('%1$u cache size to apply for %2$u images (%3$u cache size images in total*)', '%1$u cache sizes to apply for %2$u images (%3$u cache size images in total*)', $imagesizes_total), $cachesizes, $images_total, $imagesizes_total); ?><br>
255				<em><?php echo gettext('* Approximate number not counting already existing cache sizes.'); ?></em></p>
256				<hr>
257				<div class="imagecaching_progress">
258					<h2 class="imagecaching_headline"><?php echo gettext('Image caching in progress.'); ?></h2>
259					<div class="notebox">
260						<p><?php echo gettext('Please be patient as this might take quite a while! It depends on the number of images to pre-cache, their dimensions and the power of your server.'); ?></p>
261						<p><?php echo gettext('If you move away from this page before this loader disapeared, the caching will be incomplete but you can re-start any time later.'); ?></p>
262					</div>
263					<img class="imagecaching_loader" src="<?php echo WEBPATH . '/' . ZENFOLDER; ?>/images/ajax-loader.gif" alt="">
264					<ul>
265						<?php if (function_exists('curl_init') && getOption('cachemanager_generationmode') == 'curl') { ?>
266							<li><?php echo gettext('Image cache sizes generated: '); ?><span class="imagecaching_imagesizes"><?php echo cacheManager::$imagesizes_cached; ?></li>
267							<li><?php echo gettext('Image cache sizes failed: '); ?><span class="imagecaching_imagesizes_failed">0</span></li>
268						<?php } ?>
269						<li><?php echo gettext('Images processed: '); ?><span class="imagecaching_imagecount">0</span>/<span><?php echo $images_total; ?></span></li>
270						<li><?php echo gettext('Albums processed: '); ?><span class="imagecaching_albumcount">0</span>/<span><?php echo $albums_total; ?></span></li>
271						<li><?php echo gettext('Processing time: '); ?><span class="imagecaching_time">0</span> <?php echo gettext('minutes'); ?></li>
272					</ul>
273				</div>
274				<?php cacheManager::printButtons($returnpage, $alb, true); ?>
275				<hr>
276				<h2><?php echo gettext('Caching log'); ?></h2>
277				<?php
278				cacheManager::$starttime = time();
279				?>
280				<ol>
281				<?php
282				@set_time_limit(3000);
283				foreach ($allalbums as $album) {
284					$albumobj = newAlbum($album);
285					if (!$albumobj->isDynamic() || count($allalbums) == 1) {
286						cacheManager::loadAlbums($albumobj);
287					}
288				}
289				?>
290				</ol>
291				<?php
292				$endtime = time();
293				$time_total = ($endtime - cacheManager::$starttime) / 60;
294				?>
295				<p><strong><?php echo gettext('Caching done!'); ?></strong></p>
296				<script>
297					$( document ).ready(function() {
298						$('.imagecaching_progress').addClass('messagebox');
299						$('.imagecaching_headline').text('<?php echo gettext('Caching done!'); ?>');
300						$('.imagecaching_progress .notebox, .imagecaching_loader').remove();
301						$('.imagecaching_imagecount').text(<?php echo cacheManager::$images_cached; ?>);
302						$('.imagecaching_imagesizes').text(<?php echo cacheManager::$imagesizes_cached; ?>);
303						$('.imagecaching_imagesizes_failed').text(<?php echo cacheManager::$imagesizes_failed; ?>);
304						$('.imagecaching_albumcount').text(<?php echo cacheManager::$albums_cached; ?>);
305						$('.imagecaching_time').text(<?php echo round($time_total, 2); ?>);
306						$('.buttons_cachefinished').removeClass('hidden');
307					});
308				</script>
309				<?php
310			} else {
311				$button = false;
312				?>
313				<p><?php echo gettext('No cache sizes enabled.'); ?></p>
314				<?php
315			}
316		} else {
317			$button = array('text' => gettext("Cache the images"), 'title' => gettext('Executes the caching of the selected image sizes.'));
318		}
319		cacheManager::printButtons($returnpage, $alb, true);
320		if ($button) {
321			?>
322			<p class="buttons clearfix">
323				<button class="tooltip" type="submit" title="<?php echo $button['title']; ?>" >
324					<img src="<?php echo WEBPATH . '/' . ZENFOLDER; ?>/images/pass.png" alt="" />
325			<?php echo $button['text']; ?>
326				</button>
327			</p>
328			<?php
329		}
330		?>
331	</form>
332
333</div>
334</div>
335</div>
336<?php printAdminFooter(); ?>
337
338</body>
339</html>
340
341
342