Skoro prosicie

<?php
function addCode ($name, $callback_type, $callback_func, $callback_params, $content_type, $allowed_within, $not_allowed_within) {
if (isset ($this->_codes
[$name])) { return false; // already exists
}
if (!preg_match ('/^[a-zA-Z0-9*_!+-]+$/', $name)) { return false; // invalid
}
$this->_codes
[$name] = array ( 'name' => $name,
'callback_type' => $callback_type,
'callback_func' => $callback_func,
'callback_params' => $callback_params,
'content_type' => $content_type,
'allowed_within' => $allowed_within,
'not_allowed_within' => $not_allowed_within,
);
return true;
}
?>
I jeszcze jedna funkcja, która może pomoże:
<?php
function validate ($action = 'validate') {
if ($action != 'validate' && $action != 'validate_again') {
return false;
}
if ($this->_codeInfo['callback_type'] != 'simple_replace' && $this->_codeInfo['callback_type'] != 'simple_replace_single') {
if (!is_callable ($this->_codeInfo['callback_func'])) {
return false;
}
if (($this->_codeInfo
['callback_type'] == 'usecontent' || $this->_codeInfo
['callback_type'] == 'usecontent?' || $this->_codeInfo
['callback_type'] == 'callback_replace?') && count ($this->_children
) == 1
&& $this->_children
[0]->_type
== STRINGPARSER_NODE_TEXT
) { // we have to make sure the object gets passed on as a reference
// if we do call_user_func(..., &$this) this will clash with PHP5
$callArray = array ($action, $this->_attributes
, $this->_children
[0
]->content, $this->_codeInfo
['callback_params']); $callArray[] =& $this;
$res = call_user_func_array ($this->_codeInfo['callback_func'], $callArray);
if ($res) {
// ok, now, if we've got a usecontent type, set a flag that
// this may not be broken up by paragraph handling!
// but PLEASE do NOT change if already set to any other setting
// than BBCODE_PARAGRAPH_ALLOW_BREAKUP because we could
// override e.g. BBCODE_PARAGRAPH_BLOCK_ELEMENT!
$val = $this->getFlag ('paragraph_type', 'integer', BBCODE_PARAGRAPH_ALLOW_BREAKUP);
if ($val == BBCODE_PARAGRAPH_ALLOW_BREAKUP) {
$this->_flags['paragraph_type'] = BBCODE_PARAGRAPH_ALLOW_INSIDE;
}
}
return $res;
}
// we have to make sure the object gets passed on as a reference
// if we do call_user_func(..., &$this) this will clash with PHP5
$callArray = array ($action, $this->_attributes
, null, $this->_codeInfo
['callback_params']); $callArray[] =& $this;
return call_user_func_array ($this->_codeInfo['callback_func'], $callArray);
}
return (bool
)(!count ($this->_attributes
)); }
?>
Uporałem się jakoś z tamtymi problemami i doszedłem do czegoś takiego:
<?php
require_once 'bbcode/stringparser_bbcode.class.php';
//kontener
class BBCode{
var $bbcode;
//konstruktor
public function __construct() {
$this->bbcode = new StringParser_BBCode ();
$this->bbcode->setParagraphHandlingParameters ("\n\n", "<p>", "</p>");
$this->bbcode->addCode ('b', 'simple_replace', null, array ('start_tag' => '<b>', 'end_tag' => '</b>'),
//PHP
$this->bbcode->addCode ('php', 'callback_replace', array($this, 'callbackPHP'), array ('start_tag' => '<code>', 'end_tag' => '</code>'), }
//funkcja kolorujaca kod za pomoca Geshi'
function codeHighlight($str, $lang) {
require_once 'highlight/geshi.php';
$geshi =& new GeSHi($str, $lang);
return $geshi->parse_code();
}
//callbacki
//PHP
public function callbackPHP($action, $attributes, $content, $params, &$node_object) {
echo '#DEBUG: wywolano callback'; return $this->codeHighlight($content, 'cpp');
}
}
//----- MAIN -------
if (!empty($_POST['text'])) {
$bbcode = new BBCode();
echo $bbcode->bbcode->parse ($_POST['text']); }
?>
Jedyny problem jest taki, że callbackPHP() wywołuje się teraz, dwa razy. Skutkom ubocznym częściowo zaradziłem zamieniając rquire... na require_once 'highlight/geshi.php'; ale mimo wszystko funkcja dalej wywołuje się raz niepotrzebnie. Myślę, że powodem jest ten zapis:
<?php
array($this, 'callbackPHP') ?>
ale nie mam pojęcia jak to zapisać prawidłowo.