4 public $cache_dir = 'cache/rainbow/';
9 public function loadFile($file)
11 $this -> assignCode(file_get_contents($file));
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');
23 public function doCache($id, $readycode)
25 $hash = md5($id.'/code/');
27 if(@file_put_contents
($this -> cache_dir
.$hash.'.cache',$readycode))
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;
50 $this -> mycode
= $code;
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
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('@(="[^\"]*)\\\"( |>)@','\\1\"\\2', $this -> mycode
);
80 $this -> mycode
= str_replace("\\\"",'\\"', $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);
90 $newcode = $compiler -> $mode($this -> mycode
);
92 $readyCode = $compiler -> display($newcode);
95 $readyCode = str_replace('<?php','',$readyCode);
96 $readyCode = str_replace('?>','',$readyCode);
98 if($cache && $id) $this -> doCache($id, $readyCode);