0b2e3e83f2ed30950f2006e15a0677bf3f1a3d63
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 if($connect){
26 $this->connect();
27 }
28 }
30 public function connect()
31 {
32 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
33 $this->o_sock->setEncryptionKey($this->s_encryption_key);
34 if($this->o_sock->connected()){
35 $this->is_connected = TRUE;
36 }else{
37 $this->is_connected = FALSE;
38 }
39 }
41 public function disconnect()
42 {
43 $this->o_sock->close();
44 $this->is_connected = FALSE;
45 }
48 public function __reload()
49 {
50 $this->entries = array();
51 $this->pointer = 0;
52 $this->b_error = FALSE;
53 $this->s_error = "";
55 $xml_msg = "<xml><header>gosa_query_jobdb</header><where><status>*</status></where></xml>";
56 $this->connect();
57 if($this->is_connected){
58 $this->o_sock->write($xml_msg);
59 $str = trim($this->o_sock->read());
60 $entries = $this->xml_to_array($str);
61 if(!isset($entries['XML'])){
62 $this->set_error("Couldn't parse xml.");
63 $this->disconnect();
64 return(FALSE);
65 }else{
66 $ret = array_values($entries['XML']);
67 }
68 $this->entries = $ret;
69 return(TRUE);
70 }
71 $this->set_error("Could not establish socket connection.");
72 return(FALSE);
73 }
75 function xml_to_array($xml)
76 {
77 $params = array();
78 $level = array();
79 $parser = xml_parser_create_ns();
80 xml_parse_into_struct($parser, $xml, $vals, $index);
82 $err_id = xml_get_error_code($parser);
83 if($err_id){
84 $this->set_error(xml_error_string(xml_get_error_code($parser)));
85 xml_parser_free($parser);
86 }else{
87 xml_parser_free($parser);
89 foreach ($vals as $xml_elem) {
90 if ($xml_elem['type'] == 'open') {
91 if (array_key_exists('attributes',$xml_elem)) {
92 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
93 } else {
94 $level[$xml_elem['level']] = $xml_elem['tag'];
95 }
96 }
97 if ($xml_elem['type'] == 'complete') {
98 $start_level = 1;
99 $php_stmt = '$params';
100 while($start_level < $xml_elem['level']) {
101 $php_stmt .= '[$level['.$start_level.']]';
102 $start_level++;
103 }
104 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
105 eval($php_stmt);
106 }
107 }
108 }
109 return($params);
110 }
112 public function load()
113 {
114 return($this->__reload());
115 }
117 public function fetch()
118 {
119 if(isset($this->entries[$this->pointer])){
120 $p = $this->entries[$this->pointer];
121 $this->pointer ++;
122 return($p);
123 }
124 return(FALSE);
125 }
127 private function set_error($str)
128 {
129 $this->b_error = TRUE;
130 $this->s_error = $str;
131 }
133 public function is_error()
134 {
135 return($this->b_error);
136 }
138 public function get_error()
139 {
140 return($this->s_error);
141 }
143 public function decrease_entry_priority($id)
144 {
145 }
147 public function increase_entry_priority($id)
148 {
149 }
151 public function max_entry_priority($id)
152 {
153 }
155 public function min_entry_priority($id)
156 {
157 }
159 public function id_exists($id)
160 {
161 return(TRUE);
162 }
164 public function get_entry($id)
165 {
166 if(!is_numeric($id)){
167 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
168 return;
169 }
171 $this->b_error = FALSE;
172 $this->s_error = "";
174 $xml_msg = "<xml>
175 <header>gosa_query_jobdb</header>
176 <where>id</where>
177 <id>".$id."</id>
178 </xml>";
180 $this->connect();
181 if($this->is_connected){
182 $this->o_sock->write($xml_msg);
183 $str = trim($this->o_sock->read());
184 $entries = $this->xml_to_array($str);
185 if(!isset($entries['XML']['ANSWER1'])){
186 $this->set_error("Entry with id (".$id.") not found.");
187 $this->disconnect();
188 return(FALSE);
189 }else{
190 $ret = array_values($entries['XML']['ANSWER1']);
191 return($ret);
192 }
193 }
194 $this->set_error("Could not establish socket connection.");
195 return(FALSE);
196 }
198 public function update_entry($id,$entry)
199 {
200 $xml_msg = "";
202 if($this->is_connected){
203 #$this->o_sock->write($xml_msg);
204 #$str = trim($this->o_sock->read());
205 #$entries = $this->xml_to_array($str);
206 #$this->disconnect();
207 return(TRUE);
208 }
209 $this->set_error("Could not establish socket connection.");
210 return(FALSE);
211 }
213 public function add_multiple($multiple)
214 {
215 }
217 public function remove_entry($id)
218 {
219 $this->b_error = FALSE;
220 $this->s_error = "";
222 $xml_msg = "<xml>
223 <header>gosa_delete_jobdb_entry</header>
224 <where>id</where>
225 <id>".$id."</id>
226 </xml>";
227 $this->connect();
228 if($this->is_connected){
229 $this->o_sock->write($xml_msg);
230 return(TRUE);
231 }
232 $this->set_error("Could not establish socket connection.");
233 return(FALSE);
234 }
235 }
237 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
238 ?>