/
sed_plugin_library.php
executable file
·371 lines (304 loc) · 11.8 KB
/
sed_plugin_library.php
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
$plugin['name'] = 'sed_plugin_library';
$plugin['version'] = '0.5';
$plugin['author'] = 'Netcarver';
$plugin['author_uri'] = 'http://txp-plugins.netcarving.com';
$plugin['description'] = 'Helper functions for sed plugins.';
$plugin['type'] = 2;
@include_once('../zem_tpl.php');
# --- BEGIN PLUGIN CODE ---
#===============================================================================
#
#===============================================================================
/* parses a string for a name='value' list */
function sed_lib_extract_name_value_pairs( $content , $prefix='', $section_name='' , $attach_name=false, $variable_delim_char=';', $sep_char='_' )
{
$result = array();
$content = trim( $content );
if( empty( $content ) )
return $result;
$chunks = explode( $variable_delim_char , $content );
//
// Build the result array mapping
// [ variable_x => value_x ]
//
if( 0 == count( $chunks ) )
return $result;
foreach( $chunks as $chunk )
{
$chunk = trim( $chunk );
if( empty( $chunk ) )
continue;
list( $storage_key, $value ) = explode( '=', $chunk );
$storage_key = trim($storage_key);
if( empty( $storage_key ) )
continue;
if( $attach_name and !empty($section_name) )
$storage_key = trim($section_name).$sep_char.$storage_key;
if( !empty( $prefix) )
$storage_key = trim($prefix).$sep_char.$storage_key;
$result[ $storage_key ] = trim( $value, " '\"" );
}
return $result;
}
/* Returns an array of key->value mappings parsed from all the sections of a packed string. */
function sed_lib_extract_packed_vars( $packed_string, $prefix='', $attach_name=true, $section_char='|', $variable_delim_char=';' ) {
$result = array();
if( empty( $packed_string ) )
return false;
//
// Break the packed string on the section boundaries...
//
$sections = explode( $section_char , $packed_string );
$count = count( $sections );
if( 0 == $count )
return false;
foreach( $sections as $section )
{
//
// Pull out the section name
//
$section_len = strlen( $section );
$len = strpos( $section , '(' );
$section_name = substr( $section , 0 , $len );
$content = substr( $section , $len + 1 , ($section_len - $len - 2) );
$result = sed_lib_extract_name_value_pairs( $content, $prefix, $section_name, $attach_name, $variable_delim_char );
}
return $result;
}
/* Returns an array of key->value mappings parsed from one section of a packed string. */
function sed_lib_extract_packed_variable_section( $section_name, $packed_string, $prefix='', $attach_name=false, $section_char='|', $variable_delim_char=';' ) {
$result = array();
if( empty( $packed_string ) or empty( $section_name ) )
return false;
//
// Break the packed string on the section boundaries...
//
$sections = explode( $section_char , $packed_string );
$count = count( $sections );
if( 0 == $count )
return false;
//
// Find the section with the matching prefix. If it is not present
// then return false.
//
$found = false;
$section = '';
$len = strlen( $section_name );
for( $i = 0; $i < $count; $i++ ) {
$s = $sections[$i];
if( $s{$len} === '(' )
{
if( substr( $s , 0 , $len ) == $section_name )
{
$section = $s;
$i = $count;
}
}
}
if( '' === $section )
{
return false;
}
//
// Split this section on the variable delimiter...
//
$section_len = strlen( $section );
$content = substr( $section , $len + 1 , $section_len - $len - 2 );
$result = sed_lib_extract_name_value_pairs( $content, $prefix, $section_name, $attach_name, $variable_delim_char );
return $result;
}
/* Array print keys: echo's the keys from a given array in a more compact format than print_r */
function sed_lib_print_keys( $input, $postfix = '', $columnated = false ) {
if( !is_array( $input ) )
return;
echo( 'Array'.(($columnated)? br : '').'( '.(($columnated)? br : '') );
foreach( $input as $k=>$v )
echo( "[$k] ".(($columnated)? br : '') );
echo( ')'.$postfix );
}
/* Array print values: echo's the values from a given array in a more compact format than print_r */
function sed_lib_print_vals( $input, $postfix = '', $columnated = false ) {
if( !is_array( $input ) )
return;
echo( 'Array'.(($columnated)? br : '').'( '.(($columnated)? br : '') );
foreach( $input as $k=>$v )
echo( "[$v] ".(($columnated) ? br : '') );
echo( ')'.$postfix );
}
function sed_lib_radio_set( $vals, $field, $var, $tabindex = '', $id = '' )
{
$id = ($id) ? $id.'-'.$field : $field;
if( !is_array($vals) || count($vals) < 2 )
$vals = array(
'0' => gTxt('no'),
'1' => gTxt('yes')
);
foreach ($vals as $a => $b)
{
$out[] = '<input type="radio" id="'.$id.'-'.$a.'" name="'.$field.'" value="'.$a.'" class="radio"';
$out[] = ($a == $var) ? ' checked="checked"' : '';
$out[] = ($tabindex) ? ' tabindex="'.$tabindex.'"' : '';
$out[] = ' /><label for="'.$id.'-'.$a.'">'.$b.'</label> ';
}
return join('', $out);
}
#===============================================================================
#
#===============================================================================
if( @txpinterface === 'public' )
{
#register_callback('sed_lib_zemcontact_form', 'zemcontact.form' );
#register_callback('sed_lib_zemcontact_submit', 'zemcontact.submit');
function sed_lib_zemcontact_form()
{
$field = '<div class="screenreader" >' .
doLabel('Phone') . finput('text', 'phone', htmlspecialchars(ps('phone')), '', '', '', '', '', 'phone') . '<br />' .
doLabel('Address') . finput('text', 'mail' , htmlspecialchars(ps('mail' )), '', '', '', '', '', 'mail') .
'</div>';
return $field;
}
function sed_lib_zemcontact_submit()
{
$checking_mail_field = trim(ps('mail'));
$checking_phone_field = trim(ps('phone'));
$evaluation =& get_zemcontact_evaluator();
if ($checking_mail_field != '' || $checking_phone_field != '')
$evaluation -> add_zemcontact_status(1);
return;
}
/* Intended as a tag to output the current txp version */
function sed_lib_txp_version()
{
global $prefs;
return $prefs['version'];
}
/* conditional tag tests for homepage or any one of the named set of sections */
function sed_lib_home_or_section( $atts , $thing )
{
extract( lAtts ( array(
'sectlist' => '' ),
$atts, 0 ) );
global $pretext;
$do_thing = (
$pretext['s'] == "default" &&
empty($pretext['c']) &&
empty($pretext['q']) &&
empty($pretext['pg']));
if( !$do_thing && !empty( $sectlist ) ) # Not the homepage so test the sections...
{
$sectlist = do_list($sectlist);
$do_thing = in_array( $pretext['s'] , $sectlist );
}
return parse( EvalElse( $thing , $do_thing ) );
}
}
if (!class_exists('sed_lib_mlp'))
{
#===============================================================================
# MLP Support routines...
#===============================================================================
class sed_lib_mlp
{
var $strings;
var $owner;
var $prefix;
var $lang;
var $event;
function sed_lib_mlp( $plugin_name , $strarray , $prefix='' , $ev='common' , $lng='en-gb' )
{
$this->owner = $plugin_name;
$this->prefix = (empty($prefix)) ? strtolower( strtr($plugin_name, array('-'=>'_') ) ) : $prefix;
$this->strings = $strarray;
$this->lang = $lng;
$this->event = $ev; # valid events are 'public' , 'admin' and 'common'
register_callback( array(&$this, 'callback') , 'l10n.enumerate_strings' );
}
function callback()
{
$r = array(
'owner' => $this->owner,
'prefix' => $this->prefix,
'lang' => $this->lang,
'event' => $this->event,
'strings' => $this->strings,
);
return $r;
}
# Generic lookup
# $what = key to look up, should be a lowercase string.
# $args = any arguments the key is expecting for replacement
function gTxt( $what , $args = array() )
{
global $textarray;
# Prepare the prefixed key for use
#$what = strtolower($what);
$key = $this->prefix . '-' . $what;
# Grab from the global textarray (possibly edited by MLP) if we can
if( isset( $textarray[$key]) )
$str = $textarray[$key];
else
# The string isn't in the localised textarray so fallback to using the (non prefixed) string array in the plugin
$str = ( isset($this->strings[$what]) ) ? $this->strings[$what] : $what;
$str = strtr($str, $args);
return $str;
}
}
}
# --- END PLUGIN CODE ---
/*
# --- BEGIN PLUGIN CSS ---
<style type="text/css">
div#sed_help td { vertical-align:top; }
div#sed_help code { font-weight:bold; font: 105%/130% "Courier New", courier, monospace; background-color: #FFFFCC;}
div#sed_help code.sed_code_tag { font-weight:normal; border:1px dotted #999; background-color: #f0e68c; display:block; margin:10px 10px 20px; padding:10px; }
div#sed_help a:link, div#sed_help a:visited { color: blue; text-decoration: none; border-bottom: 1px solid blue; padding-bottom:1px;}
div#sed_help a:hover, div#sed_help a:active { color: blue; text-decoration: none; border-bottom: 2px solid blue; padding-bottom:1px;}
div#sed_help h1 { color: #369; font: 20px Georgia, sans-serif; margin: 0; text-align: center; }
div#sed_help h2 { border-bottom: 1px solid black; padding:10px 0 0; color: #369; font: 17px Georgia, sans-serif; }
div#sed_help h3 { color: #693; font: bold 12px Arial, sans-serif; letter-spacing: 1px; margin: 10px 0 0;text-transform: uppercase;}
</style>
# --- END PLUGIN CSS ---
# --- BEGIN PLUGIN HELP ---
<div id="sed_help">
h1(#intro). Plugin Library
sed_plugin_library v0.3 (June 18th, 2006)
Provides some useful helper functions for plugins.
h2(#functions). Function Listing
|_. Function |_. Description |
| @sed_lib_extract_name_value_pairs@ | Returns an array of name value pairs from the _variable-list_ it is given. |
| @sed_lib_extract_packed_vars@ | Returns an array of key->value mappings parsed from all the sections of a _packed-string_. |
| @sed_lib_extract_packed_variable_section@ | Returns an array of key->value mappings parsed from one section of a _packed-string_. |
| @sed_lib_print_keys@ | Echo's the keys of a given array without the values being shown. |
| @sed_lib_print_vals@ | Echo's the values of a given array without the keys being shown. |
| @sed_lib_txp_version@ | Txp tag that outputs the current installation's version |
| @sed_lib_home_or_section@ | Conditional tag tests for homepage or any one of the named set of sections |
h2(#formats). Formats
*packed string* :: _section_ ['|' _section_ ]
*section* :: section-name '(' _variable-list_ ')'
*variable-list* :: name='value' [ ';' variable-list ]
h3(#examples). Examples
Here is a valid variable-list...
a='1' ; b='2' ; Hello='Goodbye'
Here is a valid packed-string...
copyright(owner='Steve';start='1970')|location(state='sabah')|personal(dob='1/1/01';email='';phone='')
Notice that a packed string includes one or more variable lists. Each list is wrapped in parenthasis, given a prefixed name and separated from the next section by the '|'
character.
h2(#versions). Version History
v0.4
* Added class @sed_lib_mlp@ to allow easy inclusion of MLP support/string localisation (thanks to Stef Dawson for the idea.)
* Added tags...
** @sed_lib_txp_version()@
** @sed_lib_home_or_section()@
* Added slightly modified routines from the *pap_contact_cleaner* plugin. This includes wrapping the added fields in a classed @div@.
v0.3
* Renamed functions.
* Changed help files.
v0.2
* Pulled out the variable list parsing code into a common function @_extract_name_value_pairs@.
v0.1
* Functions to extract packed variables from strings.
</div>
# --- END PLUGIN HELP ---
*/
?>