bbcode
[watermeloncms.git] / wtrmln / helpers / bbcode / rainbow.class.php
blob9321fdc09110ce90b3572e62398aecdb78f7589d
1 <?php
2 class Rainbow
4 public $cache_dir = 'cache/rainbow/';
5 public $mycode='';
6 public $id = '';
7 private $mode = '';
9 public function loadFile($file)
11 $this -> assignCode(file_get_contents($file));
14 // cache functions
15 public function getCache($id)
17 $hash = md5($id.'/code/');
18 if(file_exists($this -> cache_dir.$hash.'.cache'))
19 return file_get_contents($this -> cache_dir.$hash.'.cache');
20 else return false;
23 public function doCache($id, $readycode)
25 $hash = md5($id.'/code/');
27 if(@file_put_contents($this -> cache_dir.$hash.'.cache',$readycode))
28 return true;
29 else echo 'error in doCache(); - file_put_contents('.$this -> cache_dir.$hash.'.cache)';
32 public function deleteCache($id = '')
34 $hash = md5($id.'/code/');
36 if(file_exists($this -> cache_dir.$hash.'.cache'))
37 unlink($this -> cache_dir.$hash.'.cache');
40 public function make($mode, $code ='', $id = false, $cache=1)
42 // sprawdzamy czy tryb cachowania wlączony a cache dostepny
43 // jesli tak pobieramy i zwracamy cache
44 if($cache && $id && $cachecode = $this -> getCache($id)) return $cachecode;
46 // sql znacznik specjalny który nie wymaga przedparsowania za pomocą highlight_string();
47 $deletePhpTags= false;
49 if($mode=='sql')
50 $this -> mycode = $code;
51 else
53 /* jesli kod php nie posiada znakow <?php ?> to musimy je tymczasowo dodac aby kod sie pokolorowal */
54 if($mode == 'php' && strpos($this -> mycode,'<?php')===false)
56 $code = '<?php'.$code.'?>';
57 $deletePhpTags = true;
60 /* przygotowanie kodu do obrobki
61 funkcja highlight_string() świetnie konwertuje zmienną $code wyręczając kilka wyrażen regularnych
62 dodatkowo kolorując kod php
65 ob_start();
66 highlight_string($code);
67 $this -> mycode = ob_get_clean();
69 // usuwanie przejsc do nastepnej lini
70 $this -> mycode = str_replace("\n",'', $this -> mycode);
73 zamiana wszyskich \" na kod ktory nie bedzie parsowany jako cudzyslow htmlowy
74 oraz protekcja cudzyslowow w przypadku gdy na backslashu konczy sie parametr html
75 np. a href="c:\Program Files\"
78 $this -> mycode = preg_replace('@(="[^\"]*)\\\"(&nbsp;|&gt;)@','\\1&#92;"\\2', $this -> mycode);
80 $this -> mycode = str_replace("\\\"",'\\&quot;', $this -> mycode);
84 if($mode == 'code' || ($mode == 'html' && (strpos($this -> mycode,'style')!==false) || strpos($this -> mycode,'script')!==false)) $mode = 'all';
86 include_once('rainbow_compiler.class.php');
87 $compiler = new rainbowCompiler($mode);
89 $mode = 'hl_'.$mode;
90 $newcode = $compiler -> $mode($this -> mycode);
92 $readyCode = $compiler -> display($newcode);
93 if($deletePhpTags)
95 $readyCode = str_replace('&lt;?php','',$readyCode);
96 $readyCode = str_replace('?&gt;','',$readyCode);
98 if($cache && $id) $this -> doCache($id, $readyCode);
100 return $readyCode;