1: <?php
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: $plugin_is_filter = 5 | ADMIN_PLUGIN;
85: $plugin_description = gettext("Provides cache management utilities for Image, HTML, and RSS caches.");
86: $plugin_author = "Stephen Billard (sbillard)";
87:
88: $option_interface = 'cacheManager';
89:
90: require_once(SERVERPATH . '/' . ZENFOLDER . '/class-feed.php');
91:
92: zp_register_filter('admin_utilities_buttons', 'cacheManager::overviewbutton');
93: zp_register_filter('edit_album_utilities', 'cacheManager::albumbutton', -9999);
94: zp_register_filter('show_change', 'cacheManager::published');
95:
96: $_zp_cached_feeds = array('RSS');
97:
98: class cacheManagerFeed extends feed {
99:
100:
101:
102: protected $feed = NULL;
103:
104: function __construct($feed) {
105: $this->feed = $feed;
106: }
107:
108: }
109:
110: 111: 112: 113: 114: 115:
116: class cacheManager {
117:
118: function __construct() {
119: self::deleteThemeCacheSizes('admin');
120: self::addThemeCacheSize('admin', 40, NULL, NULL, 40, 40, NULL, NULL, -1);
121: self::addThemeCacheSize('admin', 80, NULL, NULL, 80, 80, NULL, NULL, -1);
122: }
123:
124: 125: 126: 127:
128: function getOptionsSupported() {
129: global $_zp_zenpage;
130: $options = array(gettext('Image caching sizes') => array('key' => 'cropImage_list', 'type' => OPTION_TYPE_CUSTOM,
131: 'order' => 1,
132: 'desc' => '<p>' .
133: gettext('Cropped images will be made in these parameters if the <em>Create image</em> box is checked. Un-check to box to remove the settings.' .
134: 'You can determine the values for these fields by examining your cached images. The file names will look something like these:') .
135: '<ul>' .
136: '<li>' . gettext('<code>photo_595.jpg</code>: sized to 595 pixels') . '</li>' .
137: '<li>' . gettext('<code>photo_w180_cw180_ch80_thumb.jpg</code>: a size of 180px wide and 80px high and it is a thumbnail (<code>thumb</code>)') . '</li>' .
138: '<li>' . gettext('<code>photo_85_cw72_ch72_thumb_copyright_gray.jpg</code>: sized 85px cropped at about 7.6% (one half of 72/85) from the horizontal and vertical sides with a watermark (<code>copyright</code>) and rendered in grayscale (<code>gray</code>).') . '</li>' .
139: '<li>' . gettext('<code>photo_w85_h85_cw350_ch350_cx43_cy169_thumb_copyright.jpg</code>: a custom cropped 85px thumbnail with watermark.') . '</li>' .
140: '</ul>' .
141: '</p>' .
142: '<p>' .
143: gettext('If a field is not represented in the cached name, leave the field blank. Custom crops (those with cx and cy) really cannot be cached easily since each image has unique values. ' .
144: 'See the <em>template-functions</em>::<code>getCustomImageURL()</code> comment block for details on these fields.') .
145: '</p>' .
146: '<p>' .
147: gettext('Some themes use <em>MaxSpace</em> image functions. To cache images referenced by these functions set the <em>width</em> and <em>height</em> parameters to the <em>MaxSpace</em> container size and check the <code>MaxSpace</code> checkbox.') .
148: '</p>'
149: )
150: );
151: $list = array('<em>' . gettext('Albums') . '</em>' => 'cacheManager_albums', '<em>' . gettext('Images') . '</em>' => 'cacheManager_images');
152: if (extensionEnabled('zenpage')) {
153: $list['<em>' . gettext('News') . '</em>'] = 'cacheManager_news';
154: $list['<em>' . gettext('Pages') . '</em>'] = 'cacheManager_pages';
155: } else {
156: setOption('cacheManager_news', 0);
157: setOption('cacheManager_pages', 0);
158: }
159: $options[gettext('Purge cache files')] = array('key' => 'cacheManager_items', 'type' => OPTION_TYPE_CHECKBOX_ARRAY,
160: 'order' => 0,
161: 'checkboxes' => $list,
162: 'desc' => gettext('If a <em>type</em> is checked, the HTML and RSS caches for the item will be purged when the published state of an item of <em>type</em> changes.') .
163: '<div class="notebox">' . gettext('<strong>NOTE:</strong> The entire cache is cleared since there is no way to ascertain if a gallery page contains dependencies on the item.') . '</div>'
164: );
165: return $options;
166: }
167:
168: 169: 170: 171: 172: 173:
174: function handleOption($option, $currentValue) {
175: global $_zp_gallery;
176: $currenttheme = $_zp_gallery->getCurrentTheme();
177: $custom = array();
178: $result = query('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `type`="cacheManager" ORDER BY `aux`');
179: $key = 0;
180: while ($row = db_fetch_assoc($result)) {
181: $theme = $row['aux'];
182: $data = getSerializedArray($row['data']);
183: $custom[$theme][] = $data;
184: }
185: ksort($custom, SORT_LOCALE_STRING);
186: $custom[''] = array(array());
187: $c = 0;
188: ?>
189: <script type="text/javascript">
190:
191: function showTheme(theme) {
192: html = $('#' + theme + '_arrow').html();
193: if (html.match(/down/)) {
194: html = html.replace(/_down/, '_up');
195: html = html.replace(/title="<?php echo gettext('Show'); ?>/, 'title="<?php echo gettext('Hide'); ?>"');
196: $('#' + theme + '_list').show();
197: } else {
198: html = html.replace(/_up/, '_down');
199: html = html.replace(/title="<?php echo gettext('Hide'); ?>/, 'title="<?php echo gettext('Show'); ?>"');
200: $('#' + theme + '_list').hide();
201: }
202: $('#' + theme + '_arrow').html(html);
203: }
204:
205: </script>
206: <?php
207: foreach ($custom as $theme => $themedata) {
208: $themedata = sortMultiArray($themedata, array('thumb', 'image_size', 'image_width', 'image_height'));
209: ?>
210: <span class="icons" id="<?php echo $theme; ?>_arrow">
211: <?php
212: if ($theme) {
213: $inputclass = 'hidden';
214: echo '<em>' . $theme . '</em> (' . count($themedata), ')';
215: } else {
216: $inputclass = 'textbox';
217: echo '<br />' . gettext('add');
218: }
219: ?>
220: <a href="javascript:showTheme('<?php echo $theme; ?>');" title="<?php echo gettext('Show'); ?>">
221: <img class="icon-position-top4" src="<?php echo WEBPATH . '/' . ZENFOLDER . '/images/arrow_down.png'; ?>" alt="" />
222: </a>
223: </span>
224: <br />
225: <div id="<?php echo $theme; ?>_list" style="display:none">
226: <br />
227: <?php
228: foreach ($themedata as $cache) {
229: $key++;
230: if ($c % 2) {
231: $class = 'boxb';
232: } else {
233: $class = 'box';
234: }
235: ?>
236: <div>
237: <?php
238: $c++;
239: if (isset($cache['enable']) && $cache['enable']) {
240: $allow = ' checked="checked"';
241: } else {
242: $allow = '';
243: }
244: ?>
245: <div class="<?php echo $class; ?>">
246: <input type="<?php echo $inputclass; ?>" size="25" name="cacheManager_theme_<?php echo $key; ?>" value="<?php echo $theme; ?>" />
247: <?php
248: if ($theme) {
249: ?>
250: <span class="displayinlineright"><?php echo gettext('Delete'); ?> <input type="checkbox" name="cacheManager_delete_<?php echo $key; ?>" value="1" /></span>
251: <input type="hidden" name="cacheManager_valid_<?php echo $key; ?>" value="1" />
252: <?php
253: }
254: ?>
255: <br />
256: <?php
257: foreach (array('image_size' => gettext('Size'), 'image_width' => gettext('Width'), 'image_height' => gettext('Height'),
258: 'crop_width' => gettext('Crop width'), 'crop_height' => gettext('Crop height'), 'crop_x' => gettext('Crop X axis'),
259: 'crop_y' => gettext('Crop Y axis')) as $what => $display) {
260: if (isset($cache[$what])) {
261: $v = $cache[$what];
262: } else {
263: $v = '';
264: }
265: ?>
266: <span class="nowrap"><?php echo $display; ?> <input type="textbox" size="2" name="cacheManager_<?php echo $what; ?>_<?php echo $key; ?>" value="<?php echo $v; ?>" /></span>
267: <?php
268: }
269: if (isset($cache['wmk'])) {
270: $wmk = $cache['wmk'];
271: } else {
272: $wmk = '';
273: }
274: ?>
275: <span class="nowrap"><?php echo gettext('Watermark'); ?> <input type="textbox" size="20" name="cacheManager_wmk_<?php echo $key; ?>" value="<?php echo $wmk; ?>" /></span>
276: <br />
277: <span class="nowrap"><?php echo gettext('MaxSpace'); ?><input type="checkbox" name="cacheManager_maxspace_<?php echo $key; ?>" value="1"<?php if (isset($cache['maxspace']) && $cache['maxspace']) echo ' checked="checked"'; ?> /></span>
278: <span class="nowrap"><?php echo gettext('Thumbnail'); ?><input type="checkbox" name="cacheManager_thumb_<?php echo $key; ?>" value="1"<?php if (isset($cache['thumb']) && $cache['thumb']) echo ' checked="checked"'; ?> /></span>
279: <span class="nowrap"><?php echo gettext('Grayscale'); ?><input type="checkbox" name="cacheManager_gray_<?php echo $key; ?>" value="gray"<?php if (isset($cache['gray']) && $cache['gray']) echo ' checked="checked"'; ?> /></span>
280: </div>
281: <br />
282: </div>
283: <?php
284: }
285: ?>
286: </div><!-- <?php echo $theme; ?>_list -->
287: <?php
288: }
289: }
290:
291: 292: 293: 294: 295: 296: 297:
298: static function handleOptionSave($themename, $themealbum) {
299: $cache = array();
300: foreach ($_POST as $key => $value) {
301: preg_match('/^cacheManager_(.*)_(.*)/', $key, $matches);
302: if ($value && !empty($matches)) {
303: $cache[$matches[2]][$matches[1]] = sanitize(trim($value));
304: }
305: }
306: query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `type`="cacheManager"');
307: foreach ($cache as $cacheimage) {
308: if (!isset($cacheimage['delete']) && count($cacheimage) > 1) {
309: $cacheimage['theme'] = preg_replace("/[\s\"\']+/", "-", $cacheimage['theme']);
310: $sql = 'INSERT INTO ' . prefix('plugin_storage') . ' (`type`, `aux`,`data`) VALUES ("cacheManager",' . db_quote($cacheimage['theme']) . ',' . db_quote(serialize($cacheimage)) . ')';
311: query($sql);
312: }
313: }
314: return false;
315: }
316:
317: static function addThemeCacheSize($theme, $size, $width, $height, $cw, $ch, $cx, $cy, $thumb, $watermark = NULL, $effects = NULL, $maxspace = NULL) {
318: $cacheSize = serialize(array('theme' => $theme, 'apply' => false, 'image_size' => $size, 'image_width' => $width, 'image_height' => $height,
319: 'crop_width' => $cw, 'crop_height' => $ch, 'crop_x' => $cx, 'crop_y' => $cy, 'thumb' => $thumb, 'wmk' => $watermark, 'gray' => $effects, 'maxspace' => $maxspace, 'valid' => 1));
320: $sql = 'INSERT INTO ' . prefix('plugin_storage') . ' (`type`, `aux`,`data`) VALUES ("cacheManager",' . db_quote($theme) . ',' . db_quote($cacheSize) . ')';
321: query($sql);
322: }
323:
324: 325: 326: 327: 328:
329: static function published($obj) {
330: global $_zp_HTML_cache, $_zp_cached_feeds;
331:
332: if (getOption('cacheManager_' . $obj->table)) {
333: $_zp_HTML_cache->clearHTMLCache();
334: foreach ($_zp_cached_feeds as $feed) {
335: $feeder = new cacheManagerFeed($feed);
336: $feeder->clearCache();
337: }
338: }
339: return $obj;
340: }
341:
342: static function overviewbutton($buttons) {
343: if (query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `type`="cacheManager" LIMIT 1')) {
344: $enable = true;
345: $title = gettext('Finds images that have not been cached and creates the cached versions.');
346: } else {
347: $enable = false;
348: $title = gettext('You must first set the plugin options for cached image parameters.');
349: }
350:
351: $buttons[] = array(
352: 'category' => gettext('Cache'),
353: 'enable' => $enable,
354: 'button_text' => gettext('Cache manager'),
355: 'formname' => 'cacheManager_button',
356: 'action' => WEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/cacheManager/cacheImages.php?page=overview&tab=images',
357: 'icon' => 'images/cache.png',
358: 'alt' => '',
359: 'hidden' => '',
360: 'rights' => ADMIN_RIGHTS,
361: 'title' => $title
362: );
363: if (class_exists('RSS')) {
364: $buttons[] = array(
365: 'XSRFTag' => 'clear_cache',
366: 'category' => gettext('Cache'),
367: 'enable' => true,
368: 'button_text' => gettext('Purge RSS cache'),
369: 'formname' => 'purge_rss_cache.php',
370: 'action' => WEBPATH . '/' . ZENFOLDER . '/admin.php?action=clear_rss_cache',
371: 'icon' => 'images/edit-delete.png',
372: 'alt' => '',
373: 'title' => gettext('Delete all files from the RSS cache'),
374: 'hidden' => '<input type="hidden" name="action" value="clear_rss_cache" />',
375: 'rights' => ADMIN_RIGHTS
376: );
377: }
378: $buttons[] = array(
379: 'XSRFTag' => 'clear_cache',
380: 'category' => gettext('Cache'),
381: 'enable' => true,
382: 'button_text' => gettext('Purge Image cache'),
383: 'formname' => 'purge_image_cache.php',
384: 'action' => WEBPATH . '/' . ZENFOLDER . '/admin.php?action=action=clear_cache',
385: 'icon' => 'images/edit-delete.png',
386: 'alt' => '',
387: 'title' => gettext('Delete all files from the Image cache'),
388: 'hidden' => '<input type="hidden" name="action" value="clear_cache" />',
389: 'rights' => ADMIN_RIGHTS
390: );
391: $buttons[] = array(
392: 'category' => gettext('Cache'),
393: 'enable' => true,
394: 'button_text' => gettext('Purge HTML cache'),
395: 'formname' => 'clearcache_button',
396: 'action' => WEBPATH . '/' . ZENFOLDER . '/admin.php?action=clear_html_cache',
397: 'icon' => 'images/edit-delete.png',
398: 'title' => gettext('Clear the static HTML cache. HTML pages will be re-cached as they are viewed.'),
399: 'alt' => '',
400: 'hidden' => '<input type="hidden" name="action" value="clear_html_cache">',
401: 'rights' => ADMIN_RIGHTS,
402: 'XSRFTag' => 'ClearHTMLCache'
403: );
404:
405: $buttons[] = array(
406: 'category' => gettext('Cache'),
407: 'enable' => true,
408: 'button_text' => gettext('Purge search cache'),
409: 'formname' => 'clearcache_button',
410: 'action' => WEBPATH . '/' . ZENFOLDER . '/admin.php?action=clear_search_cache',
411: 'icon' => 'images/edit-delete.png',
412: 'title' => gettext('Clear the static search cache.'),
413: 'alt' => '',
414: 'hidden' => '<input type="hidden" name="action" value="clear_search_cache">',
415: 'rights' => ADMIN_RIGHTS,
416: 'XSRFTag' => 'ClearSearchCache'
417: );
418: return $buttons;
419: }
420:
421: static function albumbutton($html, $object, $prefix) {
422: $html .= '<hr />';
423: if (query_single_row('SELECT * FROM ' . prefix('plugin_storage') . ' WHERE `type`="cacheManager" LIMIT 1')) {
424: $disable = '';
425: $title = gettext('Finds images that have not been cached and creates the cached versions.');
426: } else {
427: $disable = ' disabled="disabled"';
428: $title = gettext("You must first set the plugin options for cached image parameters.");
429: }
430: $html .= '<div class="button buttons tooltip" title="' . $title . '"><a href="' . WEBPATH . '/' . ZENFOLDER . '/' . PLUGIN_FOLDER . '/cacheManager/cacheImages.php?album=' . html_encode($object->name) . '&XSRFToken=' . getXSRFToken('cacheImages') . '"' . $disable . '><img src="images/cache.png" />' . gettext('Cache album images') . '</a><br class="clearall" /></div>';
431: return $html;
432: }
433:
434: static function deleteThemeCacheSizes($theme) {
435: query('DELETE FROM ' . prefix('plugin_storage') . ' WHERE `type`="cacheManager" AND `aux`=' . db_quote($theme));
436: }
437:
438: }
439: ?>
440: