Code

Added deamon class.
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
1 <?php
3 class gosaSupportDaemon
4 {
5   private $o_sock       = NULL;
6   private $s_host       = "";
7   private $i_port       = 0;
8   private $i_timeout    = 1;
10   private $is_connected     = FALSE;
11   private $s_encryption_key = "";
13   private $entries  = array();
14   private $pointer  = 0;
16   private $s_error  = "";
17   private $b_error  = FALSE;
19   public function __construct($host,$port,$key="secret-gosa-password",$connect=TRUE,$timeout=1)
20         {
21     $this->s_host    = $host;
22     $this->i_port    = $port;
23     $this->i_timeout = $timeout;
24     $this->s_encryption_key = $key;
25   }
26   
27   public function connect()
28   {
29     $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->i_timeout);
30     $this->o_sock->setEncryptionKey($this->s_encryption_key); 
31     if($this->o_sock->connected()){ 
32       $this->is_connected = TRUE;
33     }else{
34       $this->is_connected = FALSE;
35     }
36   }
38   public function disconnect()
39   {
40     $this->o_sock->close();
41     $this->is_connected = FALSE;
42   }
45   public function __reload()
46   {
47     echo "<b>Reload</b>";
48     $this->entries = array();
49     $xml_msg = "<xml><header>gosa_query_jobdb</header><where><status>*</status></where></xml>";
50     $this->connect();
51     if($this->is_connected){
52       $this->o_sock->write($xml_msg);
53       $str = trim($this->o_sock->read());
54       $entries = $this->xml_to_array($str);
55       if(!isset($entries['XML'])){
56         print_a($entries);
57         echo htmlentities($str);
58         $this->set_error("Couldn't parse xml.");
59         $this->disconnect();
60         return(FALSE);
61       }else{
62         $ret = array_values($entries['XML']); 
63       }
64       $this->entries = $ret;
65       $this->disconnect();
66       return(TRUE);
67     }
68     $this->set_error("Could not establish socket connection.");
69     return(FALSE);
70   }
72   function xml_to_array($xml)
73   {
74     $params = array();
75     $level = array();
76     $parser  = xml_parser_create_ns();
77     xml_parse_into_struct($parser, $xml, $vals, $index);
79     $err_id = xml_get_error_code($parser);
80     if($err_id){
81       $this->set_error(xml_error_string(xml_get_error_code($parser)));
82       xml_parser_free($parser);
83     }else{
84       xml_parser_free($parser);
86       foreach ($vals as $xml_elem) {
87         if ($xml_elem['type'] == 'open') {
88           if (array_key_exists('attributes',$xml_elem)) {
89             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
90           } else {
91             $level[$xml_elem['level']] = $xml_elem['tag'];
92           }
93         }
94         if ($xml_elem['type'] == 'complete') {
95           $start_level = 1;
96           $php_stmt = '$params';
97           while($start_level < $xml_elem['level']) {
98             $php_stmt .= '[$level['.$start_level.']]';
99             $start_level++;
100           }
101           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
102           eval($php_stmt);
103         }
104       }
105     }
106     return($params); 
107   }
109   public function load()
110   {
111     return($this->__reload());
112   }
114   public function fetch()
115   {
116     if(isset($this->entries[$this->pointer])){
117       $p = $this->entries[$this->pointer];
118       $this->pointer ++;
119       return($p);
120     }
121     return(FALSE);
122   }
124   private function set_error($str)
125   {
126     $this->b_error = TRUE;
127     $this->s_error = $str;
128   }
130   public function is_error()
131   {
132     return($this->b_error);
133   }
135   public function get_error()
136   {
137     return($this->s_error);
138   }
141 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
142 ?>