1 <?php
5 /* This class is inherited from the original 'Tree'
6 * class written by Heiko Hund.
7 * It is partly rewritten to create a useable html interface
8 * for each single sieve token.
9 * This gives us the ability to edit existing sieve filters.
10 */
11 class My_Tree extends Tree
12 {
13 var $dumpFn_;
14 var $dump_;
16 var $mode_stack = array();
17 var $pap = array();
20 /* Create a html interface for the current sieve filter
21 */
22 function dump()
23 {
24 error_reporting(E_ALL);
26 /* Only parse the tokens once */
27 if(!count($this->pap)){
28 $this->dump_ = "";
29 $this->mode_stack = array();
30 $this->pap = array();
31 $this->doDump_(0, '', true);
32 }
34 /* Create html results */
35 $smarty = get_smarty();
36 $smarty->fetch(get_template_path("templates/element_stop.tpl",TRUE,dirname(__FILE__)));
38 $this -> dump_ = "";
39 foreach($this->pap as $key => $object){
40 if(is_object($object)){
41 $this->dump_ .= preg_replace("/>/",">\n",$object->execute());
42 }
43 }
46 /* Create html results */
47 $smarty = get_smarty();
48 $smarty->assign("Contents",$this->dump_);
49 $ret = $smarty->fetch(get_template_path("templates/edit_frame_base.tpl",TRUE,dirname(__FILE__)));
50 return ($ret);
51 }
54 /* This function walks through the object tree generated by the "Parse" class.
55 * All Commands will be resolved and grouped. So the Commands and their
56 * parameter are combined. Like "IF" and ":comparator"...
57 */
58 function doDump_($node_id, $prefix, $last,$num = 1)
59 {
60 /* Indicates that current comman will only be valid for a single line.
61 * this command type will be removed from mode_stack after displaying it.
62 */
63 $rewoke_last = FALSE;
65 /* Get node */
66 $node = $this->nodes_[$node_id];
68 /* This closes the last mode */
69 if($node['class'] == "block-start"){
70 $tmp = array_pop($this->mode_stack);
71 $this->handle_elements($tmp,$node_id);
72 $this->handle_elements(array("TYPE" => "block_start"),$node_id);
73 }
75 /* This closes the last mode */
76 if($node['class'] == "block-end"){
77 $tmp = array_pop($this->mode_stack);
78 $this->handle_elements($tmp,$node_id);
79 $this->handle_elements(array("TYPE" => "block_end"),$node_id);
80 }
82 /* Semicolon indicates a new command */
83 if($node['class'] == "semicolon"){
84 $tmp =array_pop($this->mode_stack);
85 $this->handle_elements($tmp,$node_id);
86 }
88 /* Handle comments */
89 if($node['class'] == "comment"){
90 $this->mode_stack[] = array("TYPE" => $node['class']);
91 $rewoke_last = TRUE;
92 }
94 /* Handle identifiers */
95 $identifiers = array("else","if","elsif","end","reject","redirect","vacation","keep","discard","comment","fileinto","require","stop");
96 if($node['class'] == "identifier" && in_array($node['text'],$identifiers)){
97 $this->mode_stack[] = array("TYPE" => $node['text']);
98 }
100 /* Add current node to current command stack */
101 end($this->mode_stack);
102 $key = key($this->mode_stack);
103 $this->mode_stack[$key]['ELEMENTS'][] = $node;
105 /* Remove last mode from mode stack, cause it was only valid for a single line */
106 if($rewoke_last){
107 $tmp =array_pop($this->mode_stack);
108 $this->handle_elements($tmp,$node_id);
109 }
111 /* If this is a sub element, just call this for all childs */
112 if(isset($this->childs_[$node_id])){
113 $childs = $this->childs_[$node_id];
114 for ($i=0; $i<count($childs); ++$i)
115 {
116 $c_last = false;
117 if ($i+1 == count($childs))
118 {
119 $c_last = true;
120 }
121 $this->doDump_($childs[$i], "", $num);
122 }
123 }
124 }
127 /* Create a class for each resolved object.
128 * And append this class to a list of objects.
129 */
130 function handle_elements($data,$id)
131 {
132 if(!isset($data['TYPE'])){
133 return;
134 }
135 $type = $data['TYPE'];
137 $class_name= "sieve_".$type ;
138 if(class_exists($class_name)){
139 $this->pap[] = new $class_name($data,$id);
140 }else{
141 echo "<font color='red'>Missing : ".$class_name."</font>"."<br>";
142 }
143 }
145 function save_object()
146 {
147 foreach($this->pap as $key => $obj){
149 if(in_array(get_class($obj),array("sieve_if","sieve_elsif","sieve_vacation","sieve_comment","sieve_reject","sieve_fileinto","sieve_require","sieve_redirect"))){
150 $this->pap[$key]->save_object();
151 }
153 $once = TRUE;
154 foreach($_POST as $name => $value){
156 if(isset($obj->object_id) && preg_match("/^Remove_Object_".$obj->object_id."_/",$name) && $once){
157 $once = FALSE;
158 $this->remove_object($key);
159 }
160 if(isset($obj->object_id) && preg_match("/^Move_Up_Object_".$obj->object_id."_/",$name) && $once){
161 $this->move_object_up($key);
162 $once = FALSE;
163 }
164 if(isset($obj->object_id) && preg_match("/^Move_Down_Object_".$obj->object_id."_/",$name) && $once){
165 $this->move_object_down($key);
166 $once = FALSE;
167 }
168 }
169 }
170 }
173 function remove_object($key_id){
174 unset($this->pap[$key_id]);
175 }
178 function move_object_up($key_id)
179 {
180 $new = array();
181 $add_now = NULL;
182 $key_id --;
184 if(!$key_id < 0) return;
186 foreach($this->pap as $key => $data){
187 if($key == $key_id){
188 $add_now = $data;
189 continue;
190 }else{
191 $new[] = $data;
192 }
194 if($add_now != NULL){
196 $new[] = $add_now;
197 $add_now = NULL;
198 }
199 }
200 if($add_now != NULL){
201 $new[] = $add_now;
202 }
203 $this->pap = $new;
204 }
207 /* This function moves the given element one position down
208 * if the next position is between
209 * '}' 'else[if]' or 'if' '{' use next available
210 */
211 function move_object_down($key_id)
212 {
213 $new = array();
214 $add_now = NULL;
216 /* Walk through all elements, till we found the given id.
217 * If we found it, skip adding the current element,
218 * first add the next element followed by the current.
219 */
220 foreach($this->pap as $key => $data){
222 /* Have we found the given id */
223 if($key == $key_id){
224 $add_now = $data;
225 $last_class = get_class($data);
226 continue;
227 }else{
229 /* Add entry */
230 $new[] = $data;
231 }
233 /* We have skipped adding an element before,
234 * try to add it now, if the position allows this.
235 */
236 if($add_now != NULL){
238 /* Don't allow adding an element directly after
239 * if/else/elsif
240 */
241 if(in_array(get_class($data),array("sieve_if","sieve_elsif","sieve_else"))){
242 continue;
243 }
245 /* If this is an block end, check if there
246 * follows an if/else/elsif and skip adding the element in this case.
247 */
248 $next ="";
249 if(isset($this->pap[$key+1])){
250 $next = get_class($this->pap[$key+1]);
251 }
252 if(in_array(get_class($data),array("sieve_block_end")) && in_array($next,array("sieve_elsif","sieve_else"))){
253 continue;
254 }
256 /* Add element, position seems to be ok */
257 $new[] = $add_now;
258 $add_now = NULL;
259 }
260 }
262 /* Element wasn't added, add it as last element */
263 if($add_now != NULL){
264 $new[] = $add_now;
265 }
266 $this->pap = $new;
267 }
270 /* Need to be reviewed */
271 function get_sieve_script()
272 {
273 $tmp ="";
274 if(count($this->pap)){
275 $buffer = "";
276 foreach($this->pap as $part) {
277 if(get_class($part) == "sieve_block_end"){
278 $buffer = substr($buffer,0,strlen($buffer)-(strlen(SIEVE_INDENT_TAB)));
279 }
280 $tmp2 = $part->get_sieve_script_part();
282 if(get_class($part) == "sieve_reject"){
283 $tmp.=$tmp2;
284 }else{
286 $tmp3 = split("\n",$tmp2);
287 foreach($tmp3 as $str){
288 $str2 = trim($str);
289 if(empty($str2)) continue;
290 $tmp.= $buffer.$str."\n";
291 }
292 }
293 if(get_class($part) == "sieve_block_start"){
294 $buffer .= SIEVE_INDENT_TAB;
295 }
296 }
297 }
298 if(!preg_match("/Generated by GOsa - Gonicus System Administrator/",$tmp)){
299 $tmp = "#Generated by GOsa - Gonicus System Administrator \n ".$tmp;
300 }
301 return($tmp);
302 }
304 function Add_Element()
305 {
306 $tmp = array("ELEMENTS" => array(array("class" => "qouted-string","text"=> "Bla bla, later more")));
307 $this->pap[] = new sieve_comment($tmp,rand(1000,100000));
308 }
309 }
312 /* Create valid sieve string/string-list
313 * out of a given array
314 */
315 function sieve_create_strings($data)
316 {
317 $ret = "";
318 if(is_array($data)){
319 if(count($data) == 1){
320 $ret = "\"";
321 foreach($data as $dat){
322 $ret .=$dat;
323 }
324 $ret.="\"";
325 }else{
326 foreach($data as $dat){
327 $ret.= "\"";
328 $ret.=$dat;
329 $ret.="\", ";
330 }
331 $ret = preg_replace("/,$/","",trim($ret));
332 $ret = "[".$ret."]";
333 }
334 }else{
336 $Multiline = preg_match("/\n/",$data);
337 $data = preg_replace("/\r/","",$data);;
339 if($Multiline){
340 $ret = "text: \r\n".$data."\r\n.\r\n";
341 }else{
342 $ret = "\"".$data."\"";
343 }
344 }
345 $ret = preg_replace("/\"\"/","\"",$ret);
346 $ret = preg_replace("/\n/","\r\n",$ret);
348 return($ret);
349 }
351 /* This checks if there is a string at the current position
352 * in the token array.
353 * If there is a string list at the current position,
354 * this function will return a complete list of all
355 * strings used in this list.
356 * It also returns an offset of the last token position
357 */
358 function sieve_get_strings($data,$id)
359 {
360 $ret = array();
361 if($data[$id]['class'] == "left-bracket"){
362 while($data[$id]['class'] != "right-bracket" && $id < count($data)){
364 if($data[$id]['class'] == "quoted-string"){
365 $ret[] = $data[$id]['text'];
366 }
367 $id ++;
368 }
369 }elseif($data[$id]['class'] == "quoted-string"){
370 $ret[] = $data[$id]['text'];
371 }elseif($data[$id]['class'] == "number"){
372 $ret[] = $data[$id]['text'];
373 }
374 return(array("OFFSET" => $id, "STRINGS" => $ret));
375 }
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>