1 <?php
3 class gosaSupportDaemon
4 {
5 private $o_sock = NULL;
6 private $s_host = "";
7 private $i_port = 0;
8 private $f_timeout = 0.2;
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=0.2)
20 {
21 $this->s_host = $host;
22 $this->i_port = $port;
23 $this->f_timeout = $timeout;
24 $this->s_encryption_key = $key;
25 }
27 public function connect()
28 {
29 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_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 $this->pointer = 0;
50 $xml_msg = "<xml><header>gosa_query_jobdb</header><where><status>*</status></where></xml>";
51 $this->connect();
52 if($this->is_connected){
53 $this->o_sock->write($xml_msg);
54 $str = trim($this->o_sock->read());
55 $entries = $this->xml_to_array($str);
56 if(!isset($entries['XML'])){
57 print_a($entries);
58 echo htmlentities($str);
59 $this->set_error("Couldn't parse xml.");
60 $this->disconnect();
61 return(FALSE);
62 }else{
63 $ret = array_values($entries['XML']);
64 }
65 $this->entries = $ret;
66 $this->disconnect();
67 return(TRUE);
68 }
69 $this->set_error("Could not establish socket connection.");
70 return(FALSE);
71 }
73 function xml_to_array($xml)
74 {
75 $params = array();
76 $level = array();
77 $parser = xml_parser_create_ns();
78 xml_parse_into_struct($parser, $xml, $vals, $index);
80 $err_id = xml_get_error_code($parser);
81 if($err_id){
82 $this->set_error(xml_error_string(xml_get_error_code($parser)));
83 xml_parser_free($parser);
84 }else{
85 xml_parser_free($parser);
87 foreach ($vals as $xml_elem) {
88 if ($xml_elem['type'] == 'open') {
89 if (array_key_exists('attributes',$xml_elem)) {
90 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
91 } else {
92 $level[$xml_elem['level']] = $xml_elem['tag'];
93 }
94 }
95 if ($xml_elem['type'] == 'complete') {
96 $start_level = 1;
97 $php_stmt = '$params';
98 while($start_level < $xml_elem['level']) {
99 $php_stmt .= '[$level['.$start_level.']]';
100 $start_level++;
101 }
102 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
103 eval($php_stmt);
104 }
105 }
106 }
107 return($params);
108 }
110 public function load()
111 {
112 return($this->__reload());
113 }
115 public function fetch()
116 {
117 if(isset($this->entries[$this->pointer])){
118 $p = $this->entries[$this->pointer];
119 $this->pointer ++;
120 return($p);
121 }
122 return(FALSE);
123 }
125 private function set_error($str)
126 {
127 $this->b_error = TRUE;
128 $this->s_error = $str;
129 }
131 public function is_error()
132 {
133 return($this->b_error);
134 }
136 public function get_error()
137 {
138 return($this->s_error);
139 }
140 }
142 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
143 ?>