/
sed_patterns.php
498 lines (411 loc) · 15 KB
/
sed_patterns.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
$plugin['name'] = 'sed_patterns';
$plugin['version'] = '0.1';
$plugin['author'] = 'Netcarver';
$plugin['author_uri'] = 'https://github.com/netcarver/sed_patterns';
$plugin['description'] = 'Pattern package import for your site.';
$plugin['type'] = '3';
$plugin['order'] = 5;
$plugin['flags'] = 0;
@include_once('../zem_tpl.php');
# --- BEGIN PLUGIN CODE ---
if( @txpinterface === 'admin' )
{
/**
* Strings for internationalisation. Adjust as needed for your language...
**/
static $sed_patterns_texts = array(
'h2.summary' => 'Summary of Actions',
'summary' => 'The following {pattern} elements will be installed…',
'existing-changed' => 'Cannot install this copy of {name} [{version}] as the installed copy has edits that you would loose.',
'existing-newer' => 'Cannot install this copy of {name} [{version}] as it is older than the installed version [{installed_version}].',
'existing-same' => 'Plugin already installed.',
);
/**
* DO NOT EDIT ANYTHING ELSE!
**/
/**
* Install the language strings...
**/
global $textarray;
foreach( $sed_patterns_texts as $k => $v )
$textarray['sed_patterns_'.$k] = $v;
/**
* The elements interface enforces a known standard on all element types.
*
* Elements can be plugins, pages, forms, styles, section, categories, images (public), images (admin), strings or external files (JS, static CSS).
**/
interface sed_patterns_element
{
/**
* Expands a blobbed element into an unblobbed element. For some element types, this is idempotent.
* @return the deblobbed element or return false if deblobbing fails;
**/
static public function deblob( $blobbed_element );
/**
* Determine if the element can be safely installed. Eg: check plugin doesn't exist or, if it does, has no edits and is not a newer version.
* @return true if installation is possible, else a string giving the reason installation is not possible.
**/
static public function can_install( $element, $name );
/**
* Installs the element.
* @return true if installation was carried out with no errors, else a string giving the reason why the install failed.
* Some installers (plugins) use the blobbed version for installation.
**/
static public function install( $element , $blobbed_element, $name );
}
/**
* Common base class.
**/
class sed_patterns_common
{
static public final function t( $msg, $args = array() )
{
return gTxt( 'sed_patterns_'.$msg, $args );
}
static public function deblob( $blobbed_element )
{
return $blobbed_element;
}
static public function can_install( $element, $name )
{
return __METHOD__ . ' No way!';
}
/**
* Installs the element.
* @return true if installation was carried out with no errors, else a string giving the reason why the install failed.
**/
static public function install( $element, $blobbed_element, $name )
{
return __METHOD__ . ' No way!';
}
}
/**
* Class sed_patterns_form
**/
class sed_patterns_form
extends sed_patterns_common
implements sed_patterns_element
{
static public function can_install( $element, $name )
{
$p = explode('.',$name);
$existing = safe_row( '*' , 'txp_form' , "`name`='{$p[0]}' AND `type`='{$p[1]}'" );
if( empty( $existing ) )
return true;
$existing['Form'] = trim( $existing['Form'] );
if( empty($existing['Form']))
return true;
if( $existing['Form'] === trim( $element ) )
return 'Form already installed.';
return 'Form already exists and is not empty.';
}
/**
* Installs the element.
* @return true if installation was carried out with no errors, else a string giving the reason why the install failed.
**/
static public function install( $element, $blobbed_element, $name )
{
$p = explode('.',$name);
$type = doSlash( array_pop($p) );
$name = doSlash( implode( '.', $p ) );
$form = doSlash( $element );
safe_upsert( 'txp_form', "`Form`='$form', `type`='$type'", "`name`='$name'" ); # TODO add error reporting
return true;
}
}
/**
* Class sed_patterns_page
**/
class sed_patterns_page
extends sed_patterns_common
implements sed_patterns_element
{
static public function can_install( $element, $name )
{
$name = doSlash($name);
$existing = safe_row( '*' , 'txp_page' , "`name`='{$name}'" );
if( empty( $existing ) )
return true;
$existing['user_html'] = trim( $existing['user_html'] );
if( empty($existing['user_html']))
return true;
if( $existing['user_html'] === trim( $element ) )
return 'Page already installed.';
return 'This page already exists and is not empty.';
}
/**
* Installs the element.
* @return true if installation was carried out with no errors, else a string giving the reason why the install failed.
**/
static public function install( $element, $blobbed_element, $name )
{
$name = doSlash($name);
$page = doSlash($element);
safe_upsert( 'txp_page', "`user_html`='$page'", "`name`='$name'" ); # TODO add error reporting
return true;
}
}
/**
*
**/
class sed_patterns_plugin
extends sed_patterns_common
implements sed_patterns_element
{
/**
* Takes a compiled plugin blob and decodes it to a normal plugin.
*/
static public function deblob( $blobbed_element )
{
$plugin = preg_replace('/^#.*$/m', '', $blobbed_element );
if ($plugin === null)
return false;
$plugin = base64_decode($plugin);
if (strncmp($plugin, "\x1F\x8B", 2) === 0)
$plugin = gzinflate(substr($plugin, 10));
if ($plugin = @unserialize($plugin)) {
if(!is_array($plugin))
return false;
}
else
return false;
return $plugin;
}
/**
* Determines if the given de-blobbed plugin can be installed...
*
* It can be installed if not currently installed OR
* currently installed version is older && unchanged from original
*
* Return values: true => PROCEED, string => DO NOT INSTALL, string is reason.
*/
static public function can_install( $plugin, $name )
{
$pname = doSlash( $plugin['name'] );
$existing = safe_row('*, abs(strcmp(md5(code),code_md5)) as modified','txp_plugin', "`name`='{$pname}'" );
if( $existing ) {
if( (bool)$existing['modified'] ) { # Don't allow mods to plugins to be overwrittern as this may destroy existing site functionality...
return self::t('existing-changed', array(
'{name}' => htmlspecialchars($plugin['name']),
'{version}' => htmlspecialchars($plugin['version']),
));
}
elseif( version_compare( $plugin['version'], $existing['version'] ) ) { # Don't allow downgrading of an installed plugin...
return self::t('existing-newer', array(
'{installed_version}' => htmlspecialchars($existing['version']),
'{name}' => htmlspecialchars($plugin['name']),
'{version}' => htmlspecialchars($plugin['version']),
));
}
elseif( version_compare( $plugin['version'], $existing['version'], '=' ) ) {
return self::t('existing-same', array(
'{name}' => htmlspecialchars($plugin['name']),
'{version}' => htmlspecialchars($plugin['version']),
));
}
}
return true;
}
static public function install( $plugin, $blobbed_plugin, $name )
{
global $txpcfg, $event;
include_once $txpcfg['txpath']. DS . 'include' . DS . 'txp_plugin.php';
$_POST['plugin64'] = $blobbed_plugin;
$_POST['event'] = 'plugin';
$current_event = $event;
#
# Use a new output buffer to swallow the view that is output by calling plugin_install()
#
ob_start();
plugin_install();
ob_clean();
#
# Unfortunately, due to Txp's lack of separation of view from model, we loose the result of the install and have to assume it went ok.
#
return true;
}
}
/**
*
**/
class sed_patterns
extends sed_patterns_common
{
const EVENT = 'sed_patterns';
const PRIVS = '1';
const PATTERN_EXT = 'pattern';
const TAB = 'Patterns';
# =======================================================================================
static private function valid_pattern_name( &$s )
{
$p = explode( '.', $s );
$last = array_pop( $p );
return ($last === self::PATTERN_EXT );
}
/**
* Unpacks a given tmp file and checks if it is a properly structured pattern blob.
*/
static public function deblob( $filename )
{
$blob = file_get_contents( $filename );
if( false === $blob ) {
self::show( 'Uploaded file could not be opened.' );
return false;
}
$blob = preg_replace('/^#.*$/m', '', $blob);
if( null === $blob ) {
self::show( 'Uploaded file could not be opened.' );
return false;
}
$blob = @base64_decode( $blob );
if( false === $blob ) {
self::show( 'Uploaded file could not be opened.' );
return false;
}
$blob = @gzinflate( $blob );
if( false === $blob ) {
self::show( 'Uploaded file could not be opened.' );
return false;
}
$p = unserialize( $blob );
if( !is_array($p) ) {
self::show( 'Uploaded file could not be opened.' );
unset( $p );
return false;
}
if( !$p['info']['name'] ) {
self::show( 'Uploaded file could not be opened.' );
unset( $p );
return false;
}
return $p;
}
static private function action_summary( &$pattern, $blob_location )
{
global $txpcfg;
include_once $txpcfg['txpath']. DS . 'lib' . DS . 'classTextile.php';
$txt = new Textile();
echo $txt->TextileRestricted( $pattern['info']['help.textile'], 0, 0 );
# TODO genarate a log report of all the items added and their initial values...
echo '<h2>'.self::t('h2.summary').'</h2>'.
'<p>' .self::t('summary', array( '{pattern}'=> $pattern['info']['name'])).'</p>';
}
/**
* Public entry point for handling the upload event...
*/
static public function upload( $event, $step )
{
$name = $_FILES['thefile']['name'];
$disp_name = htmlspecialchars( $name );
if( !is_string($name) || empty($name) || !self::valid_pattern_name($name) ) {
self::show( 'Please upload a valid patterns package' );
return;
}
if( $_FILES['thefile']['error'] !== UPLOAD_ERR_OK ) {
self::show( 'Error uploading package ['.$disp_name.']' );
return;
}
global $prefs;
$txp_tmpdir = $prefs['tempdir'];
$tmp_name = $_FILES['thefile']['tmp_name'];
$blob_location = $txp_tmpdir.DS.basename($_FILES['thefile']['tmp_name']);
$file = get_uploaded_file($_FILES['thefile']['tmp_name'], $blob_location);
if( !$file ) {
self::show( 'File upload failed.' );
return;
}
$pattern = self::deblob( $file );
if( false === $pattern ) {
self::show( 'Uploaded file not packaged correctly.' );
return;
}
pagetop( self::TAB );
self::action_summary( $pattern, $blob_location );
echo "<div id=\"list_container\" class=\"txp-container txp-list\">
<table id=\"list\" class=\"list\">
<caption>Installation Actions...</caption>
<thead><tr><th>Action</th><th>Result</th><th>Notes…</th></tr></thead>
<tbody>";
foreach( $pattern['elements'] as $name => $blobbed_element ) {
echo "<tr>";
$p = explode( '.', $name );
$type = strtolower(trim(array_pop( $p )));
$name = implode( '.', $p );
echo '<td>Installing '.htmlspecialchars($type.' "'.$name).'"</td>';
$deblobber = array("sed_patterns_{$type}", 'deblob');
$checker = array("sed_patterns_{$type}", 'can_install');
$installer = array("sed_patterns_{$type}", 'install');
if( is_callable($deblobber) && is_callable($checker) && is_callable($installer) ) {
$element = call_user_func( $deblobber, $blobbed_element );
if( false === $element ) {
echo td('ERROR','','not-ok').td('Could not understand the format of this element','','not-ok')."</tr>\n";
continue;
}
$can_install = call_user_func( $checker, $element, $name );
if( true !== $can_install ) {
echo '<td>SKIPPED</td><td>'.htmlspecialchars($can_install)."</td></tr>\n";
continue;
}
$installed = call_user_func( $installer, $element, $blobbed_element, $name );
if( true !== $installed ) {
echo td('ERROR','','not-ok').td( htmlspecialchars($installed), '', 'not-ok' )."</tr>\n";
continue;
}
echo td('OK').td('Installed');
} else
echo td('ERROR','','not-ok').td('No handler for this type of element','','not-ok');
echo "</tr>\n";
}
echo "</tbody></table></div>\n\n";
unset( $pattern );
}
static private function show($msg = '')
{
pagetop( self::TAB, $msg );
echo '<h1>SED Patterns</h1>';
echo upload_form( 'Install Pattern Pack', '', 'upload', self::EVENT, '');
}
/**
* Public entry point for rendering the default (home) page
*/
static public function home( $event, $step )
{
if( $step && in_array($step, array('upload') ))
self::$step( $event, $step );
else {
self::show();
}
}
}
add_privs( sed_patterns::EVENT, sed_patterns::PRIVS );
register_tab('extensions', sed_patterns::EVENT, gTxt( sed_patterns::TAB ) );
register_callback( 'sed_patterns::home', sed_patterns::EVENT );
# TODO add an installation+enable handler that will take care of keeping the plugin disabled if gzinflate is not installed.
}
# --- END PLUGIN CODE ---
/*
# --- BEGIN PLUGIN CSS ---
<style type="text/css">
div#sed_patterns_help td { vertical-align:top; }
div#sed_patterns_help code { font-weight:bold; font: 105%/130% "Courier New", courier, monospace; background-color: #FFFFCC;}
div#sed_patterns_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_patterns_help a:link, div#sed_patterns_help a:visited { color: blue; text-decoration: none; border-bottom: 1px solid blue; padding-bottom:1px;}
div#sed_patterns_help a:hover, div#sed_patterns_help a:active { color: blue; text-decoration: none; border-bottom: 2px solid blue; padding-bottom:1px;}
div#sed_patterns_help h1 { color: #369; font: 20px Georgia, sans-serif; margin: 0; text-align: center; }
div#sed_patterns_help h2 { border-bottom: 1px solid black; padding:10px 0 0; color: #369; font: 17px Georgia, sans-serif; }
div#sed_patterns_help h3 { color: #693; font: bold 12px Arial, sans-serif; letter-spacing: 1px; margin: 10px 0 0;text-transform: uppercase;}
div#sed_patterns_help ul ul { font-size:85%; }
div#sed_patterns_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_patterns_help">
h1(#top). SED Patterns.
Allows you to easily import patterns into your site. A Pattern is simply a package containing all of the elements needed to add a feature to your site.
The simplest example of a pattern would be that of a contact form. The pattern would include the zem_contact_reborn, zem_contact_lang and rvm_counter plugins, forms for the email address, HTML form layout and subject counting & a section to pull them all together.
<span style="float:right"><a href="#top" title="Jump to the top">top</a></span>
</div>
# --- END PLUGIN HELP ---
*/
?>