1 <?php
4 /*
5 __construct - Create a new deamon handle.
6 connect - Connect to deamon socket.
7 disconnect - Disconnect from socket.
8 set_error - Sets a new error.
9 is_error - Returns TRUE if there was an error.
10 get_error - Returns the last error or "".
11 get_queued_entries - Returns all queued entries, with limitations.
12 ids_exist - Checks if the given id exists.
13 get_entries_by_id - Returns a set of entries.
14 id_exists - Checks if a set entries exists.
15 get_entry_by_id - Returns a single entry.
16 remove_entries - Remove a set of entries.
17 remove_entry - Removes a single entry.
18 update_entry - Updates a single entry.
19 xml_to_array - XML to Array.
20 */
24 class gosaSupportDaemon
25 {
26 private $o_sock = NULL;
27 private $s_host = "";
28 private $i_port = 0;
29 private $f_timeout = 0.2;
31 private $is_connected = FALSE;
32 private $s_encryption_key = "";
34 private $s_error = "";
35 private $b_error = FALSE;
38 /*! \brief Creates a new gosaSupportDaemon object.
39 @param string Host The Host where the deamon is running on.
40 @param integer Port The port which the deamon use.
41 @param string Key The encryption string.
42 @param boolean Connect Directly connect to deamon socket.
43 @param float Timeout The timelimit for all socket actions.
44 */
45 public function __construct($host,$port,$key="secret-gosa-password",$connect=TRUE,$timeout=0.2)
46 {
47 $this->s_host = $host;
48 $this->i_port = $port;
49 $this->f_timeout = $timeout;
50 $this->s_encryption_key = $key;
51 if($connect){
52 $this->connect();
53 }
54 }
57 /*! \brief Establish deamon connection.
58 @return boolean Returns true if the connection was succesfully established.
59 */
60 public function connect()
61 {
62 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
63 if($this->o_sock->connected()){
64 $this->o_sock->setEncryptionKey($this->s_encryption_key);
65 $this->is_connected = TRUE;
66 }else{
67 $this->error = $this->o_sock->get_error();
68 $this->is_connected = FALSE;
69 }
70 }
73 /*! \brief Disconnect from gosa deamon.
74 */
75 public function disconnect()
76 {
77 $this->o_sock->close();
78 $this->is_connected = FALSE;
79 }
82 /*! \brief Sets an error message, which can be returned with get_error().
83 @param string The Error message,
84 */
85 private function set_error($str)
86 {
87 $this->b_error = TRUE;
88 $this->s_error = $str;
89 }
92 /*! \brief Checks if an error occured.
93 @return boolean returns TRUE or FALSE, whether there is an error or not.
94 */
95 public function is_error()
96 {
97 return($this->b_error);
98 }
101 /*! \brief Returns the last error.
102 @return Returns the last error.
103 */
104 public function get_error()
105 {
106 return($this->s_error);
107 }
110 /*! \brief Returns an array containing all queued entries.
111 @return Array All queued entries as an array.
112 */
113 public function get_queued_entries($from=0,$to=10)
114 {
115 $this->b_error = FALSE;
116 $this->s_error = "";
118 $xml_msg = "<xml>
119 <header>gosa_query_jobdb</header>
120 <where>
121 <clause>
122 <connector>and</connector>
123 <phrase>
124 <operator>gt</operator>
125 <ROWID>-1</ROWID>
126 </phrase>
127 </clause>
128 </where>
129 <limit>
130 <from>".$from."</from>
131 <to>".$to."</to>
132 </limit>
133 </xml>";
135 $this->connect();
136 if($this->is_connected){
137 $this->o_sock->write($xml_msg);
138 $str = trim($this->o_sock->read());
139 $entries = $this->xml_to_array($str);
141 if(!array_key_exists("XML",$entries)){
142 $this->set_error("!!!Couldn't parse xml.");
143 $this->disconnect();
144 return;
145 }else{
146 if(!is_array($entries['XML'])) {
147 $ret = array();
148 }else{
149 $ret = $entries['XML'];
150 }
151 }
152 return($ret);
153 }
154 $this->set_error("Could not establish socket connection.");
155 $this->disconnect();
156 return;
157 }
159 /*! \brief Checks if the given ids are used queue ids.
160 @param Array The ids we want to check..
161 @return Array An array containing all ids as index and TRUE/FALSE as value.
162 */
163 public function ids_exist($ids)
164 {
165 if(!is_array($ids)){
166 trigger_error("Requires an array as parameter.");
167 return;
168 }
169 $this->b_error = FALSE;
170 $this->s_error = "";
172 $ret = array();
174 $xml_msg = "<xml>
175 <header>gosa_query_jobdb</header>
176 <where>
177 <clause>
178 <connector>or</connector>";
179 foreach($ids as $id){
180 $xml_msg .= "<phrase>
181 <operator>eq</operator>
182 <id>".$id."</id>
183 </phrase>";
184 }
185 $xml_msg .= "</clause>
186 </where>
187 </xml>";
189 $this->connect();
190 if(!$this->is_connected){
191 $this->set_error("Could not establish socket connection.");
192 }else{
193 $this->o_sock->write($xml_msg);
194 $str = trim($this->o_sock->read());
195 $entries = $this->xml_to_array($str);
196 if(isset($entries['XML'])){
197 foreach($entries['XML'] as $entry){
198 $ret[] = $entry['ID'];
199 }
200 $this->disconnect();
201 return($ret);
202 }
203 }
204 $this->disconnect();
205 return(FALSE);
206 }
209 /*! \brief Returns an entry containing all requested ids.
210 @param Array The IDs of the entries we want to return.
211 @return Array Of the requested entries.
212 */
213 public function get_entries_by_id($ids)
214 {
215 if(!is_array($ids)){
216 trigger_error("Requires an array as parameter.");
217 return;
218 }
219 $this->b_error = FALSE;
220 $this->s_error = "";
222 $ret = array();
224 $xml_msg = "<xml>
225 <header>gosa_query_jobdb</header>
226 <where>
227 <clause>
228 <connector>or</connector>";
229 foreach($ids as $id){
230 $xml_msg .= "<phrase>
231 <operator>eq</operator>
232 <id>".$id."</id>
233 </phrase>";
234 $ret[$id] = FALSE;
235 }
236 $xml_msg .= "</clause>
237 </where>
238 </xml>";
240 $this->connect();
241 if(!$this->is_connected){
242 $this->set_error("Could not establish socket connection.");
243 }else{
244 $this->o_sock->write($xml_msg);
245 $str = trim($this->o_sock->read());
246 $entries = $this->xml_to_array($str);
247 if(!isset($entries['XML'])){
248 $this->set_error("Entry with id (".$id.") not found.");
249 $this->disconnect();
250 }else{
251 $ret = $entries['XML'];
252 return($ret);
253 }
254 }
255 return;
256 }
259 /*! \brief Checks if the given id is in use.
260 @param Integer The ID of the entry.
261 @return Boolean TRUE if entry exists.
262 */
263 public function id_exists($id)
264 {
265 if(!is_numeric($id)){
266 trigger_error("Requires an integer as parameter.");
267 return;
268 }
270 $this->b_error = FALSE;
271 $this->s_error = "";
272 $xml_msg = "<xml>
273 <header>gosa_query_jobdb</header>
274 <where>
275 <clause>
276 <phrase>
277 <operator>eq</operator>
278 <id>".$id."</id>
279 </phrase>
280 </clause>
281 </where>
282 </xml>";
283 $this->connect();
284 if(!$this->is_connected){
285 $this->set_error("Could not establish socket connection.");
286 }else{
287 $this->o_sock->write($xml_msg);
288 $str = trim($this->o_sock->read());
289 $entries = $this->xml_to_array($str);
290 if(isset($entries['XML']['ANSWER1'])){
291 $this->disconnect();
292 return(TRUE);
293 }
294 }
295 $this->disconnect();
296 return(FALSE);
297 }
300 /*! \brief Returns an entry from the gosaSupportQueue
301 @param Integer The ID of the entry we want to return.
302 @return Array Of the requested entry.
303 */
304 public function get_entry_by_id($id)
305 {
306 if(!is_numeric($id)){
307 trigger_error("Requires an integer as parameter.");
308 return;
309 }
311 $this->b_error = FALSE;
312 $this->s_error = "";
313 $xml_msg = "<xml>
314 <header>gosa_query_jobdb</header>
315 <where>
316 <clause>
317 <phrase>
318 <operator>eq</operator>
319 <id>".$id."</id>
320 </phrase>
321 </clause>
322 </where>
323 </xml>";
324 $this->connect();
325 if(!$this->is_connected){
326 $this->set_error("Could not establish socket connection.");
327 }else{
328 $this->o_sock->write($xml_msg);
329 $str = trim($this->o_sock->read());
330 $entries = $this->xml_to_array($str);
331 if(!isset($entries['XML']['ANSWER1'])){
332 $this->set_error("Entry with id (".$id.") not found.");
333 $this->disconnect();
334 }else{
335 $ret = $entries['XML']['ANSWER1'];
336 return($ret);
337 }
338 }
339 return;
340 }
343 /*! \brief Removes a set of entries from the GOsa support queue.
344 @param Array The IDs to remove.
345 @return Boolean True on success.
346 */
347 public function remove_entries($ids)
348 {
349 if(!is_array($ids)){
350 trigger_error("Requires an array as parameter.");
351 return;
352 }
353 $this->b_error = FALSE;
354 $this->s_error = "";
356 $ret = array();
358 $xml_msg = "<xml>
359 <header>gosa_delete_jobdb_entry</header>
360 <where>
361 <clause>
362 <connector>or</connector>";
363 foreach($ids as $id){
364 $xml_msg .= "<phrase>
365 <operator>eq</operator>
366 <id>".$id."</id>
367 </phrase>";
368 }
369 $xml_msg .= "</clause>
370 </where>
371 </xml>";
372 $this->b_error = FALSE;
373 $this->s_error = "";
375 $this->connect();
376 if($this->is_connected){
377 $this->o_sock->write($xml_msg);
378 return(TRUE);
379 }
380 $this->set_error("Could not establish socket connection.");
381 return(FALSE);
382 }
386 /*! \brief Removes an entry from the GOsa support queue.
387 @param Integer The ID of the entry we want to remove.
388 @return Boolean True on success.
389 */
390 public function remove_entry($id)
391 {
392 $this->b_error = FALSE;
393 $this->s_error = "";
395 $xml_msg = "<xml>
396 <header>gosa_delete_jobdb_entry</header>
397 <where>
398 <clause>
399 <phrase>
400 <operator>eq</operator>
401 <id>".$id."</id>
402 </phrase>
403 </clause>
404 </where>
405 </xml>";
406 $this->connect();
407 if($this->is_connected){
408 $this->o_sock->write($xml_msg);
409 return(TRUE);
410 }
411 $this->set_error("Could not establish socket connection.");
412 return(FALSE);
413 }
416 /*! \brief Parses the given xml string into an array
417 @param String XML string
418 @return Array Returns an array containing the xml structure.
419 */
420 function xml_to_array($xml)
421 {
422 $params = array();
423 $level = array();
424 $parser = xml_parser_create_ns();
425 xml_parse_into_struct($parser, $xml, $vals, $index);
427 $err_id = xml_get_error_code($parser);
428 if($err_id){
429 $this->set_error(xml_error_string(xml_get_error_code($parser)));
430 xml_parser_free($parser);
431 }else{
432 xml_parser_free($parser);
434 foreach ($vals as $xml_elem) {
435 if ($xml_elem['type'] == 'open') {
436 if (array_key_exists('attributes',$xml_elem)) {
437 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
438 } else {
439 $level[$xml_elem['level']] = $xml_elem['tag'];
440 }
441 }
442 if ($xml_elem['type'] == 'complete') {
443 $start_level = 1;
444 $php_stmt = '$params';
445 while($start_level < $xml_elem['level']) {
446 $php_stmt .= '[$level['.$start_level.']]';
447 $start_level++;
448 }
449 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
450 @eval($php_stmt);
451 }
452 }
453 }
454 return($params);
455 }
458 /*! \brief Updates an entry with a set of new values,
459 @param Integer The ID of the entry, we want to update.
460 @param Array The variables to update.
461 @return Boolean Returns TRUE on success.
462 */
463 public function update_entries($ids,$entry)
464 {
465 $this->b_error = FALSE;
466 $this->s_error = "";
467 if(!is_array($ids)){
468 trigger_error("Requires an array as first parameter.");
469 return;
470 }
472 if(!is_array($entry)){
473 trigger_error("Requires an array as second parameter.");
474 return;
475 }
477 $attr = "";
478 foreach($entry as $name => $entry){
479 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
480 }
481 $xml_msg = "<xml>
482 <header>gosa_update_status_jobdb_entry</header>
483 <where>
484 <clause>
485 <connector>or</connector>";
486 foreach($ids as $id){
487 $xml_msg .= "<phrase>
488 <operator>eq</operator>
489 <id>".$id."</id>
490 </phrase>";
491 }
492 $xml_msg .= "</clause>
493 </where>
494 <update>
495 ".$attr."
496 </update>
497 </xml>";
498 $this->connect();
499 if($this->is_connected){
500 $this->o_sock->write($xml_msg);
501 $str = trim($this->o_sock->read());
502 $entries = $this->xml_to_array($str);
503 if(!empty($str)){
504 return(TRUE);
505 }
506 return(FALSE);
507 }
508 $this->set_error("Could not establish socket connection.");
509 return(FALSE);
510 }
511 }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>