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