f11f31735d24e6e67b9ba64146f9914ca089d518
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_entries - Updates a set of entries.
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>or</connector>
124 <phrase>
125 <operator>eq</operator>
126 <HEADERTAG>ping</HEADERTAG>
127 </phrase>
129 <phrase>
130 <operator>eq</operator>
131 <HEADERTAG>sayHello</HEADERTAG>
132 </phrase>
133 </clause>
134 </where>
135 <limit>
136 <from>".$from."</from>
137 <to>".$to."</to>
138 </limit>
139 </xml>";
141 $this->connect();
142 if($this->is_connected){
143 $this->o_sock->write($xml_msg);
144 $str = trim($this->o_sock->read());
145 $entries = $this->xml_to_array($str);
147 if(!array_key_exists("XML",$entries)){
148 $this->set_error("!!!Couldn't parse xml.");
149 $this->disconnect();
150 return;
151 }else{
152 if(!is_array($entries['XML'])) {
153 $ret = array();
154 }else{
155 $ret = $entries['XML'];
156 }
157 }
158 return($ret);
159 }
160 $this->set_error("Could not establish socket connection.");
161 $this->disconnect();
162 return;
163 }
165 /*! \brief Checks if the given ids are used queue ids.
166 @param Array The ids we want to check..
167 @return Array An array containing all ids as index and TRUE/FALSE as value.
168 */
169 public function ids_exist($ids)
170 {
171 if(!is_array($ids)){
172 trigger_error("Requires an array as parameter.");
173 return;
174 }
175 $this->b_error = FALSE;
176 $this->s_error = "";
178 $ret = array();
180 $xml_msg = "<xml>
181 <header>gosa_query_jobdb</header>
182 <where>
183 <clause>
184 <connector>or</connector>";
185 foreach($ids as $id){
186 $xml_msg .= "<phrase>
187 <operator>eq</operator>
188 <id>".$id."</id>
189 </phrase>";
190 }
191 $xml_msg .= "</clause>
192 </where>
193 </xml>";
195 $this->connect();
196 if(!$this->is_connected){
197 $this->set_error("Could not establish socket connection.");
198 }else{
199 $this->o_sock->write($xml_msg);
200 $str = trim($this->o_sock->read());
201 $entries = $this->xml_to_array($str);
202 if(isset($entries['XML'])){
203 foreach($entries['XML'] as $entry){
204 $ret[] = $entry['ID'];
205 }
206 $this->disconnect();
207 return($ret);
208 }
209 }
210 $this->disconnect();
211 return(FALSE);
212 }
215 /*! \brief Returns an entry containing all requested ids.
216 @param Array The IDs of the entries we want to return.
217 @return Array Of the requested entries.
218 */
219 public function get_entries_by_id($ids)
220 {
221 if(!is_array($ids)){
222 trigger_error("Requires an array as parameter.");
223 return;
224 }
225 $this->b_error = FALSE;
226 $this->s_error = "";
228 $ret = array();
230 $xml_msg = "<xml>
231 <header>gosa_query_jobdb</header>
232 <where>
233 <clause>
234 <connector>or</connector>";
235 foreach($ids as $id){
236 $xml_msg .= "<phrase>
237 <operator>eq</operator>
238 <id>".$id."</id>
239 </phrase>";
240 $ret[$id] = FALSE;
241 }
242 $xml_msg .= "</clause>
243 </where>
244 </xml>";
246 $this->connect();
247 if(!$this->is_connected){
248 $this->set_error("Could not establish socket connection.");
249 }else{
250 $this->o_sock->write($xml_msg);
251 $str = trim($this->o_sock->read());
252 $entries = $this->xml_to_array($str);
253 if(!isset($entries['XML'])){
254 $this->set_error("Entry with id (".$id.") not found.");
255 $this->disconnect();
256 }else{
257 $ret = $entries['XML'];
258 return($ret);
259 }
260 }
261 return;
262 }
265 /*! \brief Checks if the given id is in use.
266 @param Integer The ID of the entry.
267 @return Boolean TRUE if entry exists.
268 */
269 public function id_exists($id)
270 {
271 if(!is_numeric($id)){
272 trigger_error("Requires an integer as parameter.");
273 return;
274 }
276 $this->b_error = FALSE;
277 $this->s_error = "";
278 $xml_msg = "<xml>
279 <header>gosa_query_jobdb</header>
280 <where>
281 <clause>
282 <phrase>
283 <operator>eq</operator>
284 <id>".$id."</id>
285 </phrase>
286 </clause>
287 </where>
288 </xml>";
289 $this->connect();
290 if(!$this->is_connected){
291 $this->set_error("Could not establish socket connection.");
292 }else{
293 $this->o_sock->write($xml_msg);
294 $str = trim($this->o_sock->read());
295 $entries = $this->xml_to_array($str);
296 if(isset($entries['XML']['ANSWER1'])){
297 $this->disconnect();
298 return(TRUE);
299 }
300 }
301 $this->disconnect();
302 return(FALSE);
303 }
306 /*! \brief Returns an entry from the gosaSupportQueue
307 @param Integer The ID of the entry we want to return.
308 @return Array Of the requested entry.
309 */
310 public function get_entry_by_id($id)
311 {
312 if(!is_numeric($id)){
313 trigger_error("Requires an integer as parameter.");
314 return;
315 }
317 $this->b_error = FALSE;
318 $this->s_error = "";
319 $xml_msg = "<xml>
320 <header>gosa_query_jobdb</header>
321 <where>
322 <clause>
323 <phrase>
324 <operator>eq</operator>
325 <id>".$id."</id>
326 </phrase>
327 </clause>
328 </where>
329 </xml>";
330 $this->connect();
331 if(!$this->is_connected){
332 $this->set_error("Could not establish socket connection.");
333 }else{
334 $this->o_sock->write($xml_msg);
335 $str = trim($this->o_sock->read());
336 $entries = $this->xml_to_array($str);
337 if(!isset($entries['XML']['ANSWER1'])){
338 $this->set_error("Entry with id (".$id.") not found.");
339 $this->disconnect();
340 }else{
341 $ret = $entries['XML']['ANSWER1'];
342 return($ret);
343 }
344 }
345 return;
346 }
349 /*! \brief Removes a set of entries from the GOsa support queue.
350 @param Array The IDs to remove.
351 @return Boolean True on success.
352 */
353 public function remove_entries($ids)
354 {
355 if(!is_array($ids)){
356 trigger_error("Requires an array as parameter.");
357 return;
358 }
359 $this->b_error = FALSE;
360 $this->s_error = "";
362 $ret = array();
364 $xml_msg = "<xml>
365 <header>gosa_delete_jobdb_entry</header>
366 <where>
367 <clause>
368 <connector>or</connector>";
369 foreach($ids as $id){
370 $xml_msg .= "<phrase>
371 <operator>eq</operator>
372 <id>".$id."</id>
373 </phrase>";
374 }
375 $xml_msg .= "</clause>
376 </where>
377 </xml>";
378 $this->b_error = FALSE;
379 $this->s_error = "";
381 $this->connect();
382 if($this->is_connected){
383 $this->o_sock->write($xml_msg);
384 return(TRUE);
385 }
386 $this->set_error("Could not establish socket connection.");
387 return(FALSE);
388 }
392 /*! \brief Removes an entry from the GOsa support queue.
393 @param Integer The ID of the entry we want to remove.
394 @return Boolean True on success.
395 */
396 public function remove_entry($id)
397 {
398 $this->b_error = FALSE;
399 $this->s_error = "";
401 $xml_msg = "<xml>
402 <header>gosa_delete_jobdb_entry</header>
403 <where>
404 <clause>
405 <phrase>
406 <operator>eq</operator>
407 <id>".$id."</id>
408 </phrase>
409 </clause>
410 </where>
411 </xml>";
412 $this->connect();
413 if($this->is_connected){
414 $this->o_sock->write($xml_msg);
415 return(TRUE);
416 }
417 $this->set_error("Could not establish socket connection.");
418 return(FALSE);
419 }
422 /*! \brief Parses the given xml string into an array
423 @param String XML string
424 @return Array Returns an array containing the xml structure.
425 */
426 function xml_to_array($xml)
427 {
428 $params = array();
429 $level = array();
430 $parser = xml_parser_create_ns();
431 xml_parse_into_struct($parser, $xml, $vals, $index);
433 $err_id = xml_get_error_code($parser);
434 if($err_id){
435 $this->set_error(xml_error_string(xml_get_error_code($parser)));
436 xml_parser_free($parser);
437 }else{
438 xml_parser_free($parser);
440 foreach ($vals as $xml_elem) {
441 if ($xml_elem['type'] == 'open') {
442 if (array_key_exists('attributes',$xml_elem)) {
443 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
444 } else {
445 $level[$xml_elem['level']] = $xml_elem['tag'];
446 }
447 }
448 if ($xml_elem['type'] == 'complete') {
449 $start_level = 1;
450 $php_stmt = '$params';
451 while($start_level < $xml_elem['level']) {
452 $php_stmt .= '[$level['.$start_level.']]';
453 $start_level++;
454 }
455 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
456 @eval($php_stmt);
457 }
458 }
459 }
460 return($params);
461 }
464 /*! \brief Updates an entry with a set of new values,
465 @param Integer The ID of the entry, we want to update.
466 @param Array The variables to update.
467 @return Boolean Returns TRUE on success.
468 */
469 public function update_entries($ids,$entry)
470 {
471 $this->b_error = FALSE;
472 $this->s_error = "";
473 if(!is_array($ids)){
474 trigger_error("Requires an array as first parameter.");
475 return;
476 }
478 if(!is_array($entry)){
479 trigger_error("Requires an array as second parameter.");
480 return;
481 }
483 $attr = "";
484 foreach($entry as $name => $entry){
485 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
486 }
487 $xml_msg = "<xml>
488 <header>gosa_update_status_jobdb_entry</header>
489 <where>
490 <clause>
491 <connector>or</connector>";
492 foreach($ids as $id){
493 $xml_msg .= "<phrase>
494 <operator>eq</operator>
495 <id>".$id."</id>
496 </phrase>";
497 }
498 $xml_msg .= "</clause>
499 </where>
500 <update>
501 ".$attr."
502 </update>
503 </xml>";
504 $this->connect();
505 if($this->is_connected){
506 $this->o_sock->write($xml_msg);
507 $str = trim($this->o_sock->read());
508 $entries = $this->xml_to_array($str);
509 if(!empty($str)){
510 return(TRUE);
511 }
512 return(FALSE);
513 }
514 $this->set_error("Could not establish socket connection.");
515 return(FALSE);
516 }
519 /*! \brief Updates an entry with a set of new values,
520 @param Integer The ID of the entry, we want to update.
521 @param Array The variables to update.
522 @return Boolean Returns TRUE on success.
523 */
524 public function number_of_queued_entries()
525 {
526 $xml_msg ="<xml> <header>gosa_count_jobdb</header></xml>";
527 $this->connect();
528 if($this->is_connected){
529 $this->o_sock->write($xml_msg);
530 $str = trim($this->o_sock->read());
531 $entries = $this->xml_to_array($str);
532 if(isset($entries['XML']['COUNT'])){
533 return($entries['XML']['COUNT']);
534 }
535 return;
536 }
537 $this->set_error("Could not establish socket connection.");
538 return;
539 }
540 }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>