[44] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | class bbcode{
|
---|
| 4 | var $tags;
|
---|
| 5 | var $settings;
|
---|
| 6 | function bbcode(){
|
---|
| 7 | $this->tags = array();
|
---|
| 8 | $this->settings = array('enced'=>true);
|
---|
| 9 | }
|
---|
| 10 | function get_data($name,$cfa = ''){
|
---|
| 11 | if(!array_key_exists($name,$this->tags)) return '';
|
---|
| 12 | $data = $this->tags[$name];
|
---|
| 13 | if($cfa) $sbc = $cfa; else $sbc = $name;
|
---|
| 14 | if(!is_array($data)){
|
---|
| 15 | $data = preg_replace('/^ALIAS(.+)$/','$1',$data);
|
---|
| 16 | return $this->get_data($data,$sbc);
|
---|
| 17 | }else{
|
---|
| 18 | $data['Name'] = $sbc;
|
---|
| 19 | return $data;
|
---|
| 20 | }
|
---|
| 21 | }
|
---|
| 22 | function change_setting($name,$value){
|
---|
| 23 | $this->settings[$name] = $value;
|
---|
| 24 | }
|
---|
| 25 | function add_alias($name,$aliasof){
|
---|
| 26 | if(!array_key_exists($aliasof,$this->tags) or array_key_exists($name,$this->tags)) return false;
|
---|
| 27 | $this->tags[$name] = 'ALIAS'.$aliasof;
|
---|
| 28 | return true;
|
---|
| 29 | }
|
---|
| 30 | function onparam($param,$regexarray){
|
---|
| 31 | $param = replace_pcre_array($param,$regexarray);
|
---|
| 32 | if(!$this->settings['enced']){
|
---|
| 33 | $param = htmlentities($param);
|
---|
| 34 | }
|
---|
| 35 | return $param;
|
---|
| 36 | }
|
---|
| 37 | function export_definition(){
|
---|
| 38 | return serialize($this->tags);
|
---|
| 39 | }
|
---|
| 40 | function import_definiton($definition,$mode = 'append'){
|
---|
| 41 | switch($mode){
|
---|
| 42 | case 'append':
|
---|
| 43 | $array = unserialize($definition);
|
---|
| 44 | $this->tags = $array + $this->tags;
|
---|
| 45 | break;
|
---|
| 46 | case 'prepend':
|
---|
| 47 | $array = unserialize($definition);
|
---|
| 48 | $this->tags = $this->tags + $array;
|
---|
| 49 | break;
|
---|
| 50 | case 'overwrite':
|
---|
| 51 | $this->tags = unserialize($definition);
|
---|
| 52 | break;
|
---|
| 53 | default:
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 | function add_tag($params){
|
---|
| 59 | if(!is_array($params)) return 'Paramater array not an array.';
|
---|
| 60 | if(!array_key_exists('Name',$params) or empty($params['Name'])) return 'Name parameter is required.';
|
---|
| 61 | if(preg_match('/[^A-Za-z]/',$params['Name'])) return 'Name can only contain letters.';
|
---|
| 62 | if(!array_key_exists('HasParam',$params)) $params['HasParam'] = false;
|
---|
| 63 | if(!array_key_exists('HtmlBegin',$params)) return 'HtmlBegin paremater not specified!';
|
---|
| 64 | if(!array_key_exists('HtmlEnd',$params)){
|
---|
| 65 | if(preg_match('/^(<[A-Za-z]>)+$/',$params['HtmlBegin'])){
|
---|
| 66 | $params['HtmlEnd'] = begtoend($params['HtmlBegin']);
|
---|
| 67 | }else{
|
---|
| 68 | return 'You didn\'t specify the HtmlEnd parameter, and your HtmlBegin parameter is too complex to change to an HtmlEnd parameter. Please specify HtmlEnd.';
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | if(!array_key_exists('ParamRegexReplace',$params)) $params['ParamRegexReplace'] = array();
|
---|
| 72 | if(!array_key_exists('ParamRegex',$params)) $params['ParamRegex'] = '[^\\]]+';
|
---|
| 73 | if(!array_key_exists('HasEnd',$params)) $params['HasEnd'] = true;
|
---|
| 74 | if(array_key_exists($params['Name'],$this->tags)) return 'The name you specified is already in use.';
|
---|
| 75 | $this->tags[$params['Name']] = $params;
|
---|
| 76 | return '';
|
---|
| 77 | }
|
---|
| 78 | function parse_bbcode($text){
|
---|
| 79 | foreach($this->tags as $tagname => $tagdata){
|
---|
| 80 | if(!is_array($tagdata)) $tagdata = $this->get_data($tagname);
|
---|
| 81 | $startfind = "/\\[{$tagdata['Name']}";
|
---|
| 82 | if($tagdata['HasParam']){
|
---|
| 83 | $startfind.= '=('.$tagdata['ParamRegex'].')';
|
---|
| 84 | }
|
---|
| 85 | $startfind.= '\\]/';
|
---|
| 86 | if($tagdata['HasEnd']){
|
---|
| 87 | $endfind = "[/{$tagdata['Name']}]";
|
---|
| 88 | $starttags = preg_match_all($startfind,$text,$ignore);
|
---|
| 89 | $endtags = substr_count($text,$endfind);
|
---|
| 90 | if($endtags < $starttags){
|
---|
| 91 | $text.= str_repeat($endfind,$starttags - $endtags);
|
---|
| 92 | }
|
---|
| 93 | $text = str_replace($endfind,$tagdata['HtmlEnd'],$text);
|
---|
| 94 | }
|
---|
| 95 | $replace = str_replace(array('%%P%%','%%p%%'),'\'.$this->onparam(\'$1\',$tagdata[\'ParamRegexReplace\']).\'','\''.$tagdata['HtmlBegin'].'\'');
|
---|
| 96 | $text = preg_replace($startfind.'e',$replace,$text);
|
---|
| 97 | }
|
---|
| 98 | return $text;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | ?>
|
---|