<?php
/**
* Class for BB Code Parsing
*
* Easy to entend BB Code Parser
*
* @access public
* @author Jörg Stöber <hide@address.com>
* @version 0.5.3 ($Id: CB_bb_code_parser.class.php,v 1.1 2004/03/29 22:45:31 cb_fog Exp $)
*/
class CB_bb_code_parser {
/**
* Location of the bbCodeParser class ... Needed for filter search
*
* @access private
* @var string
*/
var $parserLocation;
/**
* An array of filter objects
*
* @access private
* @var array
*/
var $parserFilters = array();
/**
* Constructor, initialises the options and filters
*
* @param string $location location of the parser, can be left out, if parser is in the same directory like the script
* @return none
* @access public
* @author Jörg Stöber <hide@address.com>
*/
function CB_bb_code_parser($location = "./") {
$this->parserLocation = $location;
$this->_loadCodes();
}
/* _getFileExtension()
* get's extension of a filename
* @access private
* @param string $fileName String to get extension
* @return string $extension
*/
function _getFileExtension($fileName) {
$nameParts = array_reverse(explode(".", $fileName));
$extension = $nameParts[0];
return $extension;
}
/* _isAllowedExtension()
* is this an allowed extension?
* @access private
* @param string $extension extension String to check
* @param array $allowedArr Array with allowed extensions
* @return string $extension
*/
function _isAllowedExtension($extension, $allowedArr) {
$result = false;
if(in_array($extension, $allowedArr)) {
$result = true;
}
return $result;
}
/* Loads filters
* this method loads filters out of the filter directory
* the method parses the directory and search for filter classes.
* The objects are stored in $parserFilters array
* @access private
*/
function _loadCodes() {
$allowedExtensions = array ( "php" );
$dir2scan = $this->parserLocation."bb_filters/";
$filterDir = dir($dir2scan);
while( $filterDirEntry = $filterDir->read() ) {
if(is_file($dir2scan.$filterDirEntry)) {
$extension = $this->_getFileExtension($filterDirEntry);
if($this->_isAllowedExtension($extension, $allowedExtensions)) {
include($dir2scan.$filterDirEntry);
$filterName = explode(".", $filterDirEntry);
$className = "BB_FILTER_".$filterName[0];
if(class_exists($className)) {
$this->parserFilters[] = &new $className;
}
}
}
}
$filterDir->close();
}
/* Parse a given text with loaded filters
* @access public
* @param string $text text to parse
* @return string $text parsed text
*/
function getParsedText($text) {
// every filter is an object
foreach($this->parserFilters as $k => $v) {
// every filter must have at least one method: parse();
$text = $v->parse($text);
}
return $text;
}
/* Reverse parsing of a given text with loaded filters
* @access public
* @param string $text text to parse
* @return string $text parsed text
*/
function getReversedParsedText($text) {
// every filter is an object
foreach($this->parserFilters as $k => $v) {
// every filter must have at least one method: parse();
$text = $v->parseReverse($text);
}
return $text;
}
}
?>