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
|
<?php
/**
* Helper functions for ECML.
*
* @package ECML
*/
/**
* Parses a string for ECML.
*
* @param string $string
* @return string $string with ECML replaced by HTML.
*/
function ecml_parse_string($string, $view = NULL) {
global $CONFIG;
$CONFIG->ecml_current_view = $view;
return preg_replace_callback(ECML_KEYWORD_REGEX, 'ecml_parse_view_match', $string);
}
/**
* Returns ECML-style keywords found in $string.
* Doesn't validate them.
* Returns an array of keyword => arguments
*
* @param string $string
* @return array
*/
function ecml_extract_keywords($string) {
$return = array();
if (preg_match_all(ECML_KEYWORD_REGEX, $string, $matches)) {
foreach ($matches[1] as $i => $keyword) {
$return[] = array('keyword' => $keyword, 'params' => $matches[2][$i]);
}
}
return $return;
}
/**
* Parse ECML keywords
*
* @param array $matches
* @return string html
*/
function ecml_parse_view_match($matches) {
global $CONFIG;
$view = $CONFIG->ecml_current_view;
$keyword = trim($matches[1]);
$params_string = trim($matches[2]);
// reject keyword if blacklisted for view or invalid
if (!ecml_is_valid_keyword($keyword, $view)) {
return $matches[0];
}
switch ($keyword) {
case 'entity_list':
$options = ecml_keywords_parse_entity_params($params_string);
// must use this lower-level function because I missed refactoring
// the list entity functions for relationships.
// (which, since you're here, is the only function that runs through all
// possible options for elgg_get_entities*() functions...)
$entities = elgg_get_entities_from_relationship($options);
$content = elgg_view_entity_list($entities, count($entities), $options['offset'],
$options['limit'], $options['full_view'], $options['view_type_toggle'], $options['pagination']);
break;
case 'view':
// src is a required attribute of view
$vars = ecml_keywords_tokenize_params($params_string);
$vars['ecml_keyword'] = $keyword;
$vars['ecml_params_string'] = $params_string;
$content = elgg_view($vars['src'], $vars);
break;
default:
// match against custom keywords with optional args
$keyword_info = $CONFIG->ecml_keywords[$keyword];
$vars = ecml_keywords_tokenize_params($params_string);
$vars['ecml_keyword'] = $keyword;
$vars['ecml_params_string'] = $params_string;
$content = elgg_view($keyword_info['view'], $vars);
break;
}
// if nothing matched return the original string.
// @todo this might be undesirable. will show ugly code everywhere
// if you delete a file or something.
if (!$content) {
$content = $matches[0];
}
return $content;
}
/**
* Creates an array from a name=value name2='value2' name3="value3" string.
*
* @param $string
* @return array
*/
function ecml_keywords_tokenize_params($string) {
if (empty($string)) {
return array();
}
$params = array();
$pos = 0;
$char = substr($string, $pos, 1);
// working var for assembling name and values
$operand = $name = '';
while ($char !== FALSE) {
switch ($char) {
// handle quoted names/values
case '"':
case "'":
$quote = $char;
$next_char = substr($string, ++$pos, 1);
while ($next_char != $quote) {
if ($next_char === FALSE) {
// no matching quote. bail.
return array();
}
$operand .= $next_char;
$next_char = substr($string, ++$pos, 1);
}
break;
case ECML_ATTR_SEPARATOR:
// normalize true and false
if ($operand == 'true'){
$operand = TRUE;
} elseif ($operand == 'false') {
$operand = FALSE;
}
$params[$name] = $operand;
$operand = $name = '';
break;
case ECML_ATTR_OPERATOR:
// save name, switch to value
$name = $operand;
$operand = '';
break;
default:
$operand .= $char;
}
$char = substr($string, ++$pos, 1);
}
// need to get the last attr
if ($name && $operand) {
if ($operand == 'true'){
$operand = TRUE;
} elseif ($operand == 'false') {
$operand = FALSE;
}
$params[$name] = $operand;
}
return $params;
// this is much faster, but doesn't allow quoting.
// $pairs = array_map('trim', explode(',', $string));
// $params = array();
//
// foreach ($pairs as $pair) {
// list($name, $value) = explode('=', $pair);
//
// $name = trim($name);
// $value = trim($value);
//
// // normalize BOOL values
// if ($value === 'true') {
// $value = TRUE;
// } elseif ($value === 'false') {
// $value = FALSE;
// }
//
// // don't check against value since a falsy/empty value is valid.
// if ($name) {
// $params[$name] = $value;
// }
// }
//
// return $params;
}
/**
* Returns an options array suitable for using in elgg_get_entities()
*
* @param string $string "name=value, name2=value2"
* @return array
*/
function ecml_keywords_parse_entity_params($string) {
$params = ecml_keywords_tokenize_params($string);
// handle some special cases
if (isset($params['owner'])) {
if ($user = get_user_by_username($params['owner'])) {
$params['owner_guid'] = $user->getGUID();
}
}
// @todo probably need to add more for
// group -> container_guid, etc
return $params;
}
/**
* Checks granular permissions if keyword is valid for view
*
* @param unknown_type $keyword
* @param unknown_type $view
* @return bool
*/
function ecml_is_valid_keyword($keyword, $view = NULL) {
global $CONFIG;
// this isn't even a real keyword.
if (!isset($CONFIG->ecml_keywords[$keyword])) {
return FALSE;
}
// don't check against views. Already know it's a real one.
if (!$view) {
return TRUE;
}
// this keyword is restricted to certain views
if (isset($CONFIG->ecml_keywords[$keyword]['restricted'])
&& !in_array($view, $CONFIG->ecml_keywords[$keyword]['restricted'])) {
return FALSE;
}
$views = $CONFIG->ecml_permissions;
// this is a blacklist, so return TRUE by default.
$r = TRUE;
if (isset($views[$view]) && in_array($keyword, $views[$view])) {
$r = FALSE;
}
return $r;
}
/**
* Grab the ECML keywords as saved in $CONFIG or regenerate.
*/
function ecml_get_keywords($recache = FALSE) {
global $CONFIG;
if (isset($CONFIG->ecml_keywords) && !$recache) {
return $CONFIG->ecml_keywords;
}
$keywords = elgg_trigger_plugin_hook('get_keywords', 'ecml', NULL, array());
$CONFIG->ecml_keywords = $keywords;
return $keywords;
}
/**
* Return basic info about the keyword.
*
* @param string $keyword
* @return array
*/
function ecml_get_keyword_info($keyword) {
global $CONFIG;
if (isset($CONFIG->ecml_keywords[$keyword])) {
return $CONFIG->ecml_keywords[$keyword];
}
return FALSE;
}
|