| 1 |
<?php
|
| 2 |
// $Id: gallery_filter.inc,v 1.3.2.12 2007/09/10 18:04:08 profix898 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* gallery.module : gallery_filter.inc
|
| 6 |
* Gallery Filter functions
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Function gallery_filter_process().
|
| 11 |
* (implementation of the main filter routine)
|
| 12 |
*/
|
| 13 |
function gallery_filter_process($text) {
|
| 14 |
$prefix = trim(variable_get('gallery_filter_prefix', 'G2'));
|
| 15 |
preg_match_all("/\[$prefix:(\d+)\s*(.*?)\]/i", $text, $matches, PREG_SET_ORDER);
|
| 16 |
|
| 17 |
// Initialize G2 and set default arguments
|
| 18 |
if (count($matches) > 0) {
|
| 19 |
if (!_gallery_init(TRUE)) {
|
| 20 |
return $text;
|
| 21 |
}
|
| 22 |
$default['n'] = variable_get('gallery_filter_n_images', 1);
|
| 23 |
$default['type'] = implode('|', array_fill(0, $default['n'], variable_get('gallery_filter_default_block_type', 'recentImage')));
|
| 24 |
$default['maxsize'] = variable_get('gallery_filter_default_maxsize', GALLERY_FILTER_MAXSIZE_DEFAULT);
|
| 25 |
$default['exactsize'] = variable_get('gallery_filter_default_exactsize', GALLERY_FILTER_EXACTSIZE_DEFAULT);
|
| 26 |
$default['class'] = variable_get('gallery_filter_default_div_class', 'nowrap');
|
| 27 |
$default['album_frame'] = variable_get('gallery_filter_default_album_frame', 'none');
|
| 28 |
$default['item_frame'] = variable_get('gallery_filter_default_item_frame', 'none');
|
| 29 |
$default['show'] = variable_get('gallery_filter_default_show', array('none'));
|
| 30 |
$default['target'] = variable_get('gallery_filter_default_link_target', '');
|
| 31 |
$default['link'] = variable_get('gallery_filter_default_link', '');
|
| 32 |
}
|
| 33 |
else {
|
| 34 |
return $text;
|
| 35 |
}
|
| 36 |
|
| 37 |
$head_array = array();
|
| 38 |
// Loop over all matches
|
| 39 |
foreach ($matches as $match) {
|
| 40 |
// First argument is numeric => valid G2 filter tag
|
| 41 |
if (is_numeric($match[1])) {
|
| 42 |
$params = array();
|
| 43 |
$params['itemId'] = intval($match[1]);
|
| 44 |
$args = array_filter(preg_split('/[\s,]+/', $match[2]));
|
| 45 |
// Loop over all (optional) arguments
|
| 46 |
foreach ($args as $arg) {
|
| 47 |
list($key, $value) = array_filter(explode('=', $arg));
|
| 48 |
$key = preg_replace('/\W/', '', $key);
|
| 49 |
$params[$key] = _gallery_filter_sanitize($key, $value);
|
| 50 |
}
|
| 51 |
// Treat 'maxsize' and 'size' as the same
|
| 52 |
if (isset($params['size'])) {
|
| 53 |
$params['maxsize'] = $params['size'];
|
| 54 |
unset($params['size']);
|
| 55 |
}
|
| 56 |
// Carefully treat the default size method (cannot just merge them as the
|
| 57 |
// entered value must take precedence over the default)
|
| 58 |
if (isset($params['maxsize'])) {
|
| 59 |
unset($default['exactsize']);
|
| 60 |
}
|
| 61 |
if (isset($params['exactsize'])) {
|
| 62 |
unset($default['maxsize']);
|
| 63 |
}
|
| 64 |
// Merge params with default values
|
| 65 |
$params = array_merge($default, $params);
|
| 66 |
// Transform 'type' into a valid parameter
|
| 67 |
if (is_array($params['type'])) {
|
| 68 |
// Ensure 'type' contains 'n' elements (auto-append if necessary)
|
| 69 |
$count = count($params['type']);
|
| 70 |
if (($num = $params['n'] - $count) > 0) {
|
| 71 |
$params['type'] += array_fill($count, $num, end($params['type']));
|
| 72 |
}
|
| 73 |
}
|
| 74 |
else {
|
| 75 |
$params['type'] = array_fill(0, $params['n'], $params['type']);
|
| 76 |
}
|
| 77 |
// 'frame' overrides 'album_frame' and 'item_frame'
|
| 78 |
if ($params['frame']) {
|
| 79 |
$params['album_frame'] = $params['item_frame'] = $params['frame'];
|
| 80 |
}
|
| 81 |
// Convert into G2-compatible arguments
|
| 82 |
$params['blocks'] = implode('|', $params['type']);
|
| 83 |
if (isset($params['maxsize']) && !empty($params['maxsize'])) {
|
| 84 |
$params['maxSize'] = $params['maxsize'];
|
| 85 |
unset($params['exactSize']);
|
| 86 |
}
|
| 87 |
if (isset($params['exactsize']) && !empty($params['exactsize'])) {
|
| 88 |
$params['exactSize'] = $params['exactsize'];
|
| 89 |
unset($params['maxSize']);
|
| 90 |
}
|
| 91 |
$params['albumFrame'] = $params['album_frame'];
|
| 92 |
$params['itemFrame'] = $params['item_frame'];
|
| 93 |
$params['linkTarget'] = $params['target'];
|
| 94 |
$params['link'] = $params['link'];
|
| 95 |
|
| 96 |
// Fetch the images and format output
|
| 97 |
list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
|
| 98 |
if ($ret) {
|
| 99 |
gallery_error(t('Unable to get Gallery image block'), $ret);
|
| 100 |
continue;
|
| 101 |
}
|
| 102 |
if ($content) {
|
| 103 |
// Replace G2 filter tag with image block html
|
| 104 |
$params['class'] = 'giImageBlock'. ($params['class'] ? ' '. $params['class'] : '');
|
| 105 |
$content = '<div class ="'. $params['class'] .'">'. $content .'</div>';
|
| 106 |
$text = str_replace($match[0], $content, $text);
|
| 107 |
}
|
| 108 |
if ($head) {
|
| 109 |
$head_array[] = trim($head);
|
| 110 |
}
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
// Add html head items and css
|
| 115 |
if (count($head_array)) {
|
| 116 |
gallery_set_head(implode("\n", array_unique($head_array)));
|
| 117 |
}
|
| 118 |
GalleryEmbed::done();
|
| 119 |
|
| 120 |
return $text .'<br class="giImageBlock-clear-both" />';
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Function _gallery_filter_sanitize().
|
| 125 |
* (sanitize filter parameters)
|
| 126 |
*/
|
| 127 |
function _gallery_filter_sanitize($key, $value) {
|
| 128 |
switch (strtolower($key)) {
|
| 129 |
case 'n':
|
| 130 |
case 'size':
|
| 131 |
case 'maxsize':
|
| 132 |
case 'exactsize':
|
| 133 |
return intval(preg_replace('/\D/', '', $value));
|
| 134 |
case 'class':
|
| 135 |
case 'frame':
|
| 136 |
case 'album_frame':
|
| 137 |
case 'item_frame':
|
| 138 |
case 'target':
|
| 139 |
case 'link':
|
| 140 |
return preg_replace('/\W/', '', $value);
|
| 141 |
case 'type':
|
| 142 |
case 'show':
|
| 143 |
return explode('|', preg_replace('/[^\w\x7c]/', '', $value));
|
| 144 |
default :
|
| 145 |
return check_plain($value);
|
| 146 |
}
|
| 147 |
|
| 148 |
return $value;
|
| 149 |
}
|