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
|
<?php
/**
* System class for handling the liberty package
*
* @package liberty
* @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertySystem.php,v 1.25 2006/04/13 08:45:11 starrrider Exp $
* @author spider <spider@steelsun.com>
*/
// +----------------------------------------------------------------------+
// | Copyright (c) 2004, bitweaver.org
// +----------------------------------------------------------------------+
// | All Rights Reserved. See copyright.txt for details and a complete list of authors.
// | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details
// |
// | For comments, please use phpdocu.sourceforge.net documentation standards!!!
// | -> see http://phpdocu.sourceforge.net/
// +----------------------------------------------------------------------+
// | Authors: spider <spider@steelsun.com>
// +----------------------------------------------------------------------+
/**
* Local base defines
*/
define( 'STORAGE_PLUGIN', 'storage' );
define( 'FORMAT_PLUGIN', 'format' );
define( 'DATA_PLUGIN', 'data' );
define( 'LIBERTY_SERVICE_ACCESS_CONTROL', 'access_control' );
define( 'LIBERTY_SERVICE_CATEGORIZATION', 'categorization' );
define( 'LIBERTY_SERVICE_COMMERCE', 'commerce' );
define( 'LIBERTY_SERVICE_METADATA', 'metadata' );
define( 'LIBERTY_SERVICE_MENU', 'menu' );
define( 'LIBERTY_SERVICE_DOCUMENT_GENERATION', 'document_generation' );
define( 'LIBERTY_SERVICE_SEARCH', 'search');
define( 'LIBERTY_SERVICE_RATING', 'rating');
define( 'LIBERTY_SERVICE_TRANSLATION', 'translation');
define( 'LIBERTY_SERVICE_CONTENT_TEMPLATES', 'content_templates');
define( 'LIBERTY_TEXT_AREA', 'editliberty');
define( 'DEFAULT_ACCEPTABLE_TAGS', '<a><br><b><blockquote><cite><code><div><dd><dl><dt><em><h1><h2><h3><h4><hr>'
.' <i><it><img><li><ol><p><pre><span><strong><table><tbody><div><tr><td><th><u><ul>'
.' <button><fieldset><form><label><input><option><select><textarea>' );
/**
* Link to base class
*/
require_once( LIBERTY_PKG_PATH.'LibertyBase.php' );
/**
* System class for handling the liberty package
*
* @package liberty
*/
class LibertySystem extends LibertyBase {
var $mPlugins;
var $mDataTags;
var $mContentTypes;
function LibertySystem() {
LibertyBase::LibertyBase();
$this->mDataTags = array();
$this->loadPlugins();
$this->loadContentTypes();
}
function loadPlugins() {
if( $rs = $this->mDb->query( "SELECT * FROM `".BIT_DB_PREFIX."liberty_plugins`", NULL, BIT_QUERY_DEFAULT, BIT_QUERY_DEFAULT ) ) {
while( $row = $rs->fetchRow() ) {
$this->mPlugins[$row['plugin_guid']] = $row;
}
}
}
function scanPlugins() {
$pluginsPath = LIBERTY_PKG_PATH.'plugins/';
if( $pluginDir = opendir( $pluginsPath ) ) {
// Make two passes through the root - 1. to define the DEFINES, and 2. to include the $pScanFile's
while (false !== ($plugin = readdir($pluginDir))) {
if( preg_match( '/\.php$/', $plugin ) ) {
include_once( $pluginsPath.$plugin );
}
}
}
// match up storage_type_id to plugin_guids. this _id varies from install to install, but guids are the same
foreach( array_keys( $this->mPlugins ) as $guid ) {
$handler = &$this->mPlugins[$guid]; //shorthand var alias
if( !isset( $handler['verified'] ) && $handler['is_active'] =='y' ) {
// We are missing a plugin!
$sql = "UPDATE `".BIT_DB_PREFIX."liberty_plugins` SET `is_active`='x' WHERE `plugin_guid`=?";
$this->mDb->query( $sql, array( $guid ) );
$handler['is_active'] = 'n';
} elseif( !empty( $handler['verified'] ) && $handler['is_active'] =='x' ) {
//We found a formally missing plugin - re-enable it
$sql = "UPDATE `".BIT_DB_PREFIX."liberty_plugins` SET `is_active`='y' WHERE `plugin_guid`=?";
$this->mDb->query( $sql, array( $guid ) );
$handler['is_active'] = 'y';
} elseif( empty( $handler['verified'] ) && !isset( $handler['is_active'] ) ) {
//We found a missing plugin - insert it
$handler['is_active'] = ( ( isset( $handler['auto_activate'] ) && $handler['auto_activate'] == FALSE ) ? 'n' : 'y' );
$sql = "INSERT INTO `".BIT_DB_PREFIX."liberty_plugins` ( `plugin_guid`, `plugin_type`, `plugin_description`, `is_active` ) VALUES ( ?, ?, ?, ? )";
$this->mDb->query( $sql, array( $guid, $handler['plugin_type'], $handler['description'], $handler['is_active'] ) );
}
}
asort( $this->mPlugins );
}
function loadContentTypes( $pCacheTime=BIT_QUERY_CACHE_TIME ) {
if( $rs = $this->mDb->query( "SELECT * FROM `".BIT_DB_PREFIX."liberty_content_types`", NULL, BIT_QUERY_DEFAULT, BIT_QUERY_DEFAULT ) ) {
while( $row = $rs->fetchRow() ) {
$this->mContentTypes[$row['content_type_guid']] = $row;
}
}
}
function registerContentType( $pGuid, $pTypeParams ) {
if( !isset( $this->mContentTypes ) ) {
$this->loadContentTypes();
}
$pTypeParams['content_type_guid'] = $pGuid;
if( empty( $this->mContentTypes[$pGuid] ) && !empty( $pTypeParams ) ) {
$result = $this->mDb->associateInsert( BIT_DB_PREFIX."liberty_content_types", $pTypeParams );
// we just ran some SQL - let's flush the loadContentTypes query cache
$this->loadContentTypes( 0 );
} else {
if( $pTypeParams['handler_package'] != $this->mContentTypes[$pGuid]['handler_package'] || $pTypeParams['handler_file'] != $this->mContentTypes[$pGuid]['handler_file'] || $pTypeParams['handler_class'] != $this->mContentTypes[$pGuid]['handler_class'] ) {
$result = $this->mDb->associateUpdate( BIT_DB_PREFIX."liberty_content_types", $pTypeParams, array( 'content_type_guid'=>$pGuid ) );
}
}
}
function getContentTypeDescription( $pContentType ) {
$ret = NULL;
if( !empty( $this->mContentTypes[$pContentType]['content_description'] ) ) {
$ret = $this->mContentTypes[$pContentType]['content_description'];
}
return $ret;
}
function getService( $pPackageName ) {
global $gBitSystem;
return( !empty( $gBitSystem->mPackages[$pPackageName]['service'] ) ? $gBitSystem->mPackages[$pPackageName]['service'] : NULl );
}
function registerService( $pServiceName, $pPackageName, $pServiceHash ) {
$this->mServices[$pServiceName][$pPackageName] = $pServiceHash;
}
function hasService( $pServiceName ) {
return( !empty( $this->mServices[$pServiceName] ) );
}
function getServiceValues( $pServiceValue ) {
global $gBitSystem;
$ret = NULL;
if( !empty( $this->mServices ) ) {
foreach( array_keys( $this->mServices ) as $service ) {
if( $this->hasService( $service ) ) {
if( !($package = $gBitSystem->getConfig( 'liberty_service_'.$service )) ) {
$package = key( $this->mServices[$service] );
}
if( !empty( $this->mServices[$service][$package][$pServiceValue] ) ) {
$ret[$service] = $this->mServices[$service][$package][$pServiceValue];
}
}
}
}
return $ret;
}
function isPluginActive( $pPluginGuid ) {
return( isset( $this->mPlugins[$pGuid] ) && ($this->mPlugins[$pGuid] == 'y') );
}
function registerDataTag( $pTag, $pPluginGuid ) {
$this->mDataTags[strtolower($pTag)] = $pPluginGuid;
}
function registerPlugin( $pGuid, $pPluginParams ) {
if( isset($this->mPlugins[$pGuid] ) ) {
$this->mPlugins[$pGuid]['verified'] = TRUE;
} else {
$this->mPlugins[$pGuid]['verified'] = FALSE;
}
$this->mPlugins[$pGuid] = array_merge( $this->mPlugins[$pGuid], $pPluginParams );
}
// @parameter pPluginGuids an array of all the plugin guids that are active. Any left out are *inactive*!
function setActivePlugins( $pPluginGuids ) {
if( is_array( $pPluginGuids ) ) {
$sql = "UPDATE `".BIT_DB_PREFIX."liberty_plugins` SET `is_active`='n' WHERE `is_active`!='x'";
$this->mDb->query( $sql );
foreach( array_keys( $this->mPlugins ) as $guid ) {
$this->mPlugins[$guid]['is_active'] = 'n';
}
foreach( array_keys( $pPluginGuids ) as $guid ) {
$sql = "UPDATE `".BIT_DB_PREFIX."liberty_plugins` SET `is_active`='y' WHERE `plugin_guid`=?";
$this->mDb->query( $sql, array( $guid ) );
$this->mPlugins[$guid]['is_active'] = 'y';
}
// we just ran some SQL - let's flush the loadPlugins query cache
$this->loadPlugins( 0 );
}
}
function getPluginInfo( $pGuid ) {
$ret = NULL;
if( !empty( $pGuid )
&& !empty( $this->mPlugins[$pGuid] )
) {
$ret = $this->mPlugins[$pGuid];
}
return $ret;
}
function getPluginFunction( $pGuid, $pFunctionName ) {
$ret = NULL;
if( !empty( $pGuid )
&& !empty( $this->mPlugins[$pGuid] )
&& !empty( $this->mPlugins[$pGuid][$pFunctionName] )
&& function_exists( $this->mPlugins[$pGuid][$pFunctionName] )
) {
$ret = $this->mPlugins[$pGuid][$pFunctionName];
}
return $ret;
}
// Get the URL to the icon for the mime type passed in. This should probably check for files of multiple image types instead of just jpg
function getMimeThumbnailURL($pMimeType, $pExt=NULL) {
$ret = NULL;
$parts = split('/',$pMimeType);
if (count($parts) > 1) {
global $gBitSmarty;
require_once $gBitSmarty->_get_plugin_filepath('function','biticon');
$ext = $parts[1];
$biticon = array(
'ipackage' => 'liberty',
'ipath' => 'mime/',
'iname' => $ext,
'iexplain' => $ext,
'url' => 'only',
);
if( !$ret = smarty_function_biticon( $biticon,$gBitSmarty ) ) {
$biticon['iname'] = $pExt;
if( !$ret = smarty_function_biticon( $biticon,$gBitSmarty ) ) {
$biticon['iname'] = 'generic';
$ret = smarty_function_biticon( $biticon,$gBitSmarty );
}
}
}
return $ret;
}
}
function parse_data_plugins( &$data, &$preparsed, &$noparsed, &$pParser ) {
global $gLibertySystem;
// Find the plugins
// note: $curlyTags[0] is the complete match, $curlyTags[1] is plugin name, $curlyTags[2] is plugin arguments
preg_match_all("/\{\/?([A-Za-z]+)([^\}]*)\}/", $data, $curlyTags);
if( count( $curlyTags[0] ) ) {
// if true, replace only CODE plugin, if false, replace all other plugins
$code_first = true;
// Process plugins in reverse order, so that nested plugins are handled
// from the inside out.
$i = count( $curlyTags[0] ) - 1;
$paired_tag_seen = array();
while( $i >= 0 ) {
$plugin_start = $curlyTags[0][$i];
$plugin = $curlyTags[1][$i];
$pos = strpos( $data, $plugin_start ); // where plugin starts
$dataTag = strtolower( $plugin );
// hush up the return of this in case someone uses curly braces to enclose text
$pluginInfo = $gLibertySystem->getPluginInfo( @$gLibertySystem->mDataTags[$dataTag] ) ;
// only process a standalone unpaired tag or the start tag for a paired tag
if( empty( $paired_close_tag_seen[$dataTag] ) || $paired_close_tag_seen[$dataTag] == 0 ) {
$paired_close_tag_seen[$dataTag] = 1;
} else {
$paired_close_tag_seen[$dataTag] = 0;
}
$is_opening_tag = 0;
if( ( empty( $pluginInfo['requires_pair'] ) && (strtolower($plugin_start) != '{/'. $dataTag . '}' ) )
|| (strpos( $plugin_start, ' ' ) > 0)
|| (strtolower($plugin_start) == '{'.$dataTag.'}' && !$paired_close_tag_seen[$dataTag] )
) {
$is_opening_tag = 1;
}
if(
// when in CODE parsing mode, replace only CODE plugins
( ( $code_first && ( $dataTag == 'code' ) )
// when NOT in CODE parsing mode, replace all other plugins
|| ( !$code_first && ( $dataTag <> 'code' ) )
)
&& isset( $gLibertySystem->mDataTags[$dataTag] )
&& ( $pluginInfo )
&& ( $gLibertySystem->getPluginFunction( $gLibertySystem->mDataTags[$dataTag], 'load_function' ) )
&& ( $loadFunc = $gLibertySystem->getPluginFunction( $gLibertySystem->mDataTags[$dataTag], 'load_function' ) )
&& ( $is_opening_tag )
) {
if( $pluginInfo['requires_pair'] ) {
$plugin_end = '{/'.$plugin.'}';
$pos_end = strpos( strtolower( $data ), strtolower( $plugin_end ), $pos ); // where plugin data ends
$plugin_end2 = '{'.$plugin.'}';
$pos_end2 = strpos( strtolower( $data ), strtolower( $plugin_end2 ), $pos+1 ); // where plugin data ends
if( ( $pos_end2 > 0 && $pos_end2 > 0 && $pos_end2 < $pos_end ) || $pos_end === false ) {
$pos_end = $pos_end2;
$plugin_end = $plugin_end2;
}
} else {
$pos_end = $pos + strlen( $curlyTags[0][$i] );
$plugin_end = '';
}
//print " if ( ((($code_first) && ($plugin == 'CODE')) || ((!$code_first) && ($plugin <> 'CODE'))) && ($pos_end > $pos)) { <br/>";
// Extract the plugin data
$plugin_data_len = $pos_end - $pos - strlen( $curlyTags[0][$i] );
$plugin_data = substr( $data, $pos + strlen( $plugin_start ), $plugin_data_len );
//print " $plugin_data_len = $pos_end - $pos - strlen(".$curlyTags[0][$i].") substr( $pos + strlen($plugin_start), $plugin_data_len);";
$arguments = array();
// Construct argument list array
$paramString = str_replace( '>', '>', trim( $curlyTags[2][$i] ) );
if( preg_match( '/^\(.*=>.*\)$/', $paramString ) ) {
$paramString = preg_replace( '/[\(\)]/', '', $paramString );
//we have the old style parms like {CODE (in=>1)}
$params = split( ',', trim( $paramString ) );
foreach( $params as $param ) {
// the following str_replace line is to decode the > char when html is turned off
// perhaps the plugin syntax should be changed in 1.8 not to use any html special chars
$parts = split( '=>?', $param );
if( isset( $parts[0] ) && isset( $parts[1] ) ) {
$name = trim( $parts[0] );
$arguments[$name] = trim( $parts[1] );
}
}
} else {
$paramString = trim( $curlyTags[2][$i], " \t()" );
$paramString = str_replace(""", '"', $paramString);
$arguments = parse_xml_attributes( $paramString );
}
if( $ret = $loadFunc( $plugin_data, $arguments ) ) {
// temporarily replace end of lines so tables and other things render properly
// $ret = preg_replace( "/\n/", '#EOL', $ret );
// Handle pre- & no-parse sections and plugins inserted by this plugin
if( is_object( $pParser ) ) {
// we were passed in a parser object, assume tikiwiki that has parse_first method
$pParser->parse_pp_np( $ret, $preparsed, $noparsed );
} else {
// just nuke all np/pp for now in non tikiwiki formats
$ret = preg_replace( "/\~(\/?)[np]p\~/", '', $ret );
}
// Replace plugin section with its output in data
$data = substr_replace($data, $ret, $pos, $pos_end - $pos + strlen($plugin_end));
}
}
$i--;
// if we are in CODE parsing mode and list is done, switch to 'parse other plugins' mode and start all over
if( ( $code_first ) && ( $i < 0 ) ) {
$i = count( $curlyTags[0] ) - 1;
$code_first = false;
}
} // while
}
}
global $gLibertySystem;
$gLibertySystem = new LibertySystem();
?>
|