1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class gosaSupportDaemon
24 {
25 private $s_host = "";
26 private $i_port = 0;
27 private $s_encryption_key = "";
29 private $o_sock = NULL;
30 private $f_timeout = 2;
31 private $s_error = "";
32 private $b_error = FALSE;
34 private $is_connected = FALSE;
37 /*! \brief Creates a new gosaSupportDaemon object.
38 @param string Host The Host where the daemon is running on.
39 @param integer Port The port which the daemon use.
40 @param string Key The encryption string.
41 @param boolean Connect Directly connect to daemon socket.
42 @param float Timeout The timelimit for all socket actions.
43 */
44 public function __construct($connect=TRUE,$timeout=10)
45 {
46 #FIXME: bad idea about referencing global variables from within classes
47 global $config;
49 # load from config, store statically
50 if (isset($config->current['GOSA_SI'])){
52 if ($this->s_host == ""){
53 $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
54 $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
55 $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
56 }
58 $this->f_timeout = $timeout;
59 if($connect){
60 $this->connect();
61 }
62 }
63 }
66 /*! \brief Establish daemon connection.
67 @return boolean Returns true if the connection was succesfully established.
68 */
69 public function connect()
70 {
71 if(!empty($this->s_host) && !empty($this->i_port)){
72 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
73 if($this->o_sock->connected()){
74 $this->o_sock->setEncryptionKey($this->s_encryption_key);
75 $this->is_connected = TRUE;
76 }else{
77 $this->set_error($this->o_sock->get_error());
78 $this->disconnect();
79 new log("debug","gosaSupportDaemon::connect()", "Could not connect to server.", array(),$this->get_error());
80 }
81 }else{
82 $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
83 }
84 return($this->is_connected);
85 }
88 /*! \brief Disconnect from gosa daemon.
89 */
90 public function disconnect()
91 {
92 $this->o_sock->close();
93 $this->is_connected = FALSE;
94 }
97 /*! \brief Sets an error message, which can be returned with get_error().
98 @param string The Error message,
99 */
100 private function set_error($str)
101 {
102 $this->b_error = TRUE;
103 $this->s_error = $str;
104 }
107 /*! \brief Sets an error message, which can be returned with get_error().
108 @param string The Error message,
109 */
110 private function reset_error()
111 {
112 $this->b_error = FALSE;
113 $this->s_error = "";
114 }
117 /*! \brief Checks if an error occured.
118 @return boolean returns TRUE or FALSE, whether there is an error or not.
119 */
120 public function is_error()
121 {
122 return($this->b_error);
123 }
126 /*! \brief Returns the last error.
127 @return Returns the last error.
128 */
129 public function get_error()
130 {
131 $str = $this->s_error;
132 $str = preg_replace("/ /"," ",$str);
133 return($str);
134 }
137 public function FAI_get_packages($release,$package = "")
138 {
139 $this->reset_error();
141 if(empty($package)){
142 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
143 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
144 }else{
145 $xml_msg =
146 "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
147 "<where><clause>".
148 "<connector>AND</connector>".
149 "<phrase><distribution>".$release."</distribution></phrase>".
150 "<phrase><package>".$package."</package></phrase>".
151 "</clause></where>".
152 "</xml>";
153 }
155 $ret = array();
156 if($this->connect()){
157 $this->o_sock->write($xml_msg);
158 $str = trim($this->o_sock->read());
159 $entries = $this->xml_to_array($str);
160 if(isset($entries['XML']) && is_array($entries['XML'])){
162 /* Check if returned values represent a valid answer */
163 if(isset($entries['XML'])){
164 if(isset($entries['XML']['ERROR_STRING'])) {
165 $this->set_error($entries['XML']['ERROR_STRING']);
166 new log("debug","GOsa-si",
167 get_class($this)."::".__FUNCTION__, array(),
168 "FAILED error was ".$this->get_error());
169 return($ret);
170 }
172 /* Unset header tags */
173 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
174 if(isset($entries['XML'][$type])){
175 unset($entries['XML'][$type]);
176 }
177 }
178 $ret = $entries['XML'];
179 }
180 }
181 }
182 return($ret);
185 }
188 public function FAI_get_server($name = "")
189 {
190 $this->reset_error();
192 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
193 $ret = array();
194 if($this->connect()){
195 $this->o_sock->write($xml_msg);
196 $str = trim($this->o_sock->read());
197 $entries = $this->xml_to_array($str);
198 if(isset($entries['XML']) && is_array($entries['XML'])){
200 /* Check if returned values represent a valid answer */
201 if(isset($entries['XML'])){
202 if(isset($entries['XML']['ERROR_STRING'])) {
203 $this->set_error($entries['XML']['ERROR_STRING']);
204 new log("debug","GOsa-si",
205 get_class($this)."::".__FUNCTION__, array(),
206 "FAILED error was ".$this->get_error());
207 return($ret);
208 }
210 /* Unset header tags */
211 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
212 if(isset($entries['XML'][$type])){
213 unset($entries['XML'][$type]);
214 }
215 }
216 $ret = $entries['XML'];
217 }
218 }
219 }
220 return($ret);
221 }
224 public function FAI_get_classes($name)
225 {
226 $this->reset_error();
227 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
228 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
229 $ret = array();
230 if($this->connect()){
231 $this->o_sock->write($xml_msg);
232 $str = trim($this->o_sock->read());
233 $entries = $this->xml_to_array($str);
234 if(isset($entries['XML']) && is_array($entries['XML'])){
236 /* Check if returned values represent a valid answer */
237 if(isset($entries['XML'])){
238 if(isset($entries['XML']['ERROR_STRING'])) {
239 $this->set_error($entries['XML']['ERROR_STRING']);
240 new log("debug","GOsa-si",
241 get_class($this)."::".__FUNCTION__, array($name),
242 "FAILED error was ".$this->get_error());
243 return($ret);
244 }
246 /* Unset header tags */
247 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
248 if(isset($entries['XML'][$type])){
249 unset($entries['XML'][$type]);
250 }
251 }
252 $ret = $entries['XML'];
253 }
254 }
255 }
256 return($ret);
257 }
260 /*! \brief Returns an array containing all queued entries.
261 @return Array All queued entries as an array.
262 */
263 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
264 {
265 $this->reset_error();
266 $ret = array();
268 $tags = "";
269 foreach($event_types as $type){
270 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
271 }
272 if(count($event_types) > 1){
273 $tags = "<connector>or</connector>".$tags;
274 }
275 if(count($event_types)){
276 $tags = "<where><clause>".$tags."</clause></where>";
277 }
279 $xml_msg =
280 "<xml>
281 <header>gosa_query_jobdb</header>
282 <target>GOSA</target>
283 <source>GOSA</source>
284 ".$tags."
286 <orderby>".$sort."</orderby>";
287 if($from != -1 && $to != -1){
288 $xml_msg.= "
289 <limit>
290 <from>".$from."</from>
291 <to>".$to."</to>
292 </limit>";
293 }
294 $xml_msg.= "
295 </xml>";
297 if($this->connect()){
298 $this->o_sock->write($xml_msg);
299 $str = trim($this->o_sock->read());
300 $entries = $this->xml_to_array($str);
301 if(isset($entries['XML']) && is_array($entries['XML'])){
303 /* Check if returned values represent a valid answer */
304 if(isset($entries['XML'])){
306 /* Unset header tags */
307 foreach(array("HEADER","SOURCE","TARGET") as $type){
308 unset($entries['XML'][$type]);
309 }
310 $ret = $entries['XML'];
311 }
312 }
313 }
315 /* Remove session ID. No one is interested in this... */
316 unset($ret['SESSION_ID']);
318 return($ret);
319 }
322 /*! \brief Checks if the given ids are used queue ids.
323 @param Array The ids we want to check..
324 @return Array An array containing all ids as index and TRUE/FALSE as value.
325 */
326 public function ids_exist($ids)
327 {
328 if(!is_array($ids)){
329 trigger_error("Requires an array as parameter.");
330 return;
331 }
332 $this->reset_error();
334 $ret = array();
336 $xml_msg = "<xml>
337 <header>gosa_query_jobdb</header>
338 <target>GOSA</target>
339 <source>GOSA</source>
340 <where>
341 <clause>
342 <connector>or</connector>";
343 foreach($ids as $id){
344 $xml_msg .= "<phrase>
345 <operator>eq</operator>
346 <id>".$id."</id>
347 </phrase>";
348 }
349 $xml_msg .= "</clause>
350 </where>
351 </xml>";
353 if($this->connect()){
354 $this->o_sock->write($xml_msg);
355 $str = trim($this->o_sock->read());
356 $entries = $this->xml_to_array($str);
357 if(isset($entries['XML']) && is_array($entries['XML'])){
358 foreach($entries['XML'] as $entry){
359 if(isset($entry['ID'])){
360 $ret[] = $entry['ID'];
361 }
362 }
363 }
364 }
365 return($ret);
366 }
369 /*! \brief Returns an entry containing all requested ids.
370 @param Array The IDs of the entries we want to return.
371 @return Array Of the requested entries.
372 */
373 public function get_entries_by_mac($macs)
374 {
375 if(!is_array($macs)){
376 trigger_error("Requires an array as parameter.");
377 return;
378 }
379 $this->reset_error();
381 $ret = array();
383 $xml_msg = "<xml>
384 <header>gosa_query_jobdb</header>
385 <target>GOSA</target>
386 <source>GOSA</source>
387 <where>
388 <clause>
389 <connector>or</connector>";
390 foreach($macs as $mac){
391 $xml_msg .= "<phrase>
392 <operator>eq</operator>
393 <macaddress>".$mac."</macaddress>
394 </phrase>";
395 }
396 $xml_msg .= "</clause>
397 </where>
398 </xml>";
400 if($this->connect()){
401 $this->o_sock->write($xml_msg);
402 $str = trim($this->o_sock->read());
403 $entries = $this->xml_to_array($str);
404 if(isset($entries['XML'])){
405 foreach($entries['XML'] as $name => $entry){
406 if(preg_match("/^ANSWER[0-9]*$/",$name)){
407 $ret[$name] = $entry;
408 }
409 }
410 }
411 }
412 return($ret);
413 }
416 /*! \brief Returns an entry containing all requested ids.
417 @param Array The IDs of the entries we want to return.
418 @return Array Of the requested entries.
419 */
420 public function get_entries_by_id($ids)
421 {
422 if(!is_array($ids)){
423 trigger_error("Requires an array as parameter.");
424 return;
425 }
426 $this->reset_error();
428 $ret = array();
430 $xml_msg = "<xml>
431 <header>gosa_query_jobdb</header>
432 <target>GOSA</target>
433 <source>GOSA</source>
434 <where>
435 <clause>
436 <connector>or</connector>";
437 foreach($ids as $id){
438 $xml_msg .= "<phrase>
439 <operator>eq</operator>
440 <id>".$id."</id>
441 </phrase>";
442 }
443 $xml_msg .= "</clause>
444 </where>
445 </xml>";
447 if($this->connect()){
448 $this->o_sock->write($xml_msg);
449 $str = trim($this->o_sock->read());
450 $entries = $this->xml_to_array($str);
451 if(isset($entries['XML'])){
452 foreach($entries['XML'] as $name => $entry){
453 if(preg_match("/^ANSWER[0-9]*$/",$name)){
454 $ret[$name] = $entry;
455 }
456 }
457 }
458 }
459 return($ret);
460 }
463 /*! \brief Checks if the given id is in use.
464 @param Integer The ID of the entry.
465 @return Boolean TRUE if entry exists.
466 */
467 public function id_exists($id)
468 {
469 if(!is_numeric($id)){
470 trigger_error("Requires an integer as parameter.");
471 return;
472 }
474 $this->reset_error();
476 $xml_msg = "<xml>
477 <header>gosa_query_jobdb</header>
478 <target>GOSA</target>
479 <source>GOSA</source>
480 <where>
481 <clause>
482 <phrase>
483 <operator>eq</operator>
484 <id>".$id."</id>
485 </phrase>
486 </clause>
487 </where>
488 </xml>";
490 if($this->connect()){
491 $this->o_sock->write($xml_msg);
492 $str = trim($this->o_sock->read());
493 $entries = $this->xml_to_array($str);
494 if( isset($entries['XML']['HEADER']) &&
495 $entries['XML']['HEADER']=="answer" &&
496 isset($entries['XML']['ANSWER1'])){
497 return(TRUE);
498 }
499 }
500 return(FALSE);
501 }
504 /*! \brief Returns an entry from the gosaSupportQueue
505 @param Integer The ID of the entry we want to return.
506 @return Array Of the requested entry.
507 */
508 public function get_entry_by_id($id)
509 {
510 if(!is_numeric($id)){
511 trigger_error("Requires an integer as parameter.");
512 return;
513 }
514 $this->reset_error();
516 $ret = array();
517 $xml_msg = "<xml>
518 <header>gosa_query_jobdb</header>
519 <target>GOSA</target>
520 <source>GOSA</source>
521 <where>
522 <clause>
523 <phrase>
524 <operator>eq</operator>
525 <id>".$id."</id>
526 </phrase>
527 </clause>
528 </where>
529 </xml>";
530 if($this->connect()){
531 $this->o_sock->write($xml_msg);
532 $str = trim($this->o_sock->read());
533 $entries = $this->xml_to_array($str);
534 if( isset($entries['XML']['HEADER']) &&
535 $entries['XML']['HEADER']=="answer" &&
536 isset($entries['XML']['ANSWER1'])){
537 $ret = $entries['XML']['ANSWER1'];
538 }
539 }
540 return($ret);
541 }
544 /*! \brief Removes a set of entries from the GOsa support queue.
545 @param Array The IDs to remove.
546 @return Boolean True on success.
547 */
548 public function remove_entries($ids)
549 {
550 if(!is_array($ids)){
551 trigger_error("Requires an array as parameter.");
552 return;
553 }
555 $this->reset_error();
557 $ret = array();
559 $xml_msg = "<xml>
560 <header>gosa_delete_jobdb_entry</header>
561 <target>GOSA</target>
562 <source>GOSA</source>
563 <where>
564 <clause>
565 <connector>or</connector>";
566 foreach($ids as $id){
567 $xml_msg .= "<phrase>
568 <operator>eq</operator>
569 <id>".$id."</id>
570 </phrase>";
571 }
572 $xml_msg .= "</clause>
573 </where>
574 </xml>";
576 if($this->connect()){
577 $this->o_sock->write($xml_msg);
578 $str = $this->o_sock->read();
579 $entries = $this->xml_to_array($str);
580 if(isset($entries['XML']) || isset($entries['COUNT'])){
581 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
582 return(TRUE);
583 }else{
584 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
585 }
586 }
587 return(FALSE);
588 }
592 /*! \brief Removes an entry from the GOsa support queue.
593 @param Integer The ID of the entry we want to remove.
594 @return Boolean True on success.
595 */
596 public function remove_entry($id)
597 {
598 return($this->remove_entries(array($id)));
599 }
602 /*! \brief Parses the given xml string into an array
603 @param String XML string
604 @return Array Returns an array containing the xml structure.
605 */
606 private function xml_to_array($xml)
607 {
608 $params = array();
609 $level = array();
610 $parser = xml_parser_create_ns();
611 xml_parse_into_struct($parser, $xml, $vals, $index);
613 $err_id = xml_get_error_code($parser);
614 if($err_id){
615 xml_parser_free($parser);
616 }else{
617 xml_parser_free($parser);
619 foreach ($vals as $xml_elem) {
620 if ($xml_elem['type'] == 'open') {
621 if (array_key_exists('attributes',$xml_elem)) {
622 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
623 } else {
624 $level[$xml_elem['level']] = $xml_elem['tag'];
625 }
626 }
627 if ($xml_elem['type'] == 'complete') {
628 $start_level = 1;
629 $php_stmt = '$params';
630 while($start_level < $xml_elem['level']) {
631 $php_stmt .= '[$level['.$start_level.']]';
632 $start_level++;
633 }
634 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
635 @eval($php_stmt);
636 }
637 }
638 }
640 if(!isset($params['XML'])){
641 if (!array_key_exists('XML', $params)){
642 $this->set_error(_("Cannot not parse XML!"));
643 }
644 $params = array("COUNT" => 0);
645 }
647 return($params);
648 }
651 /*! \brief Updates an entry with a set of new values,
652 @param Integer The ID of the entry, we want to update.
653 @param Array The variables to update.
654 @return Boolean Returns TRUE on success.
655 */
656 public function update_entries($ids,$data)
657 {
658 $this->reset_error();
659 if(!is_array($ids)){
660 trigger_error("Requires an array as first parameter.");
661 return;
662 }
664 if(!is_array($data)){
665 trigger_error("Requires an array as second parameter.");
666 return;
667 }
669 $attr = "";
670 foreach($data as $key => $value){
671 if(is_array($value)){
672 foreach($value as $sub_value){
673 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
674 }
675 }else{
676 $attr.= "<$key>".strtolower($value)."</$key>\n";
677 }
678 }
680 $xml_msg = "<xml>
681 <header>gosa_update_status_jobdb_entry</header>
682 <target>GOSA</target>
683 <source>GOSA</source>
684 <where>
685 <clause>
686 <connector>or</connector>";
687 foreach($ids as $id){
688 $xml_msg .= "<phrase>
689 <operator>eq</operator>
690 <id>".$id."</id>
691 </phrase>";
692 }
693 $xml_msg .= "</clause>
694 </where>
695 <update>
696 ".$attr."
697 </update>
698 </xml>";
700 if($this->connect()){
702 $this->o_sock->write($xml_msg);
703 $str = trim($this->o_sock->read());
704 $entries = $this->xml_to_array($str);
705 if(isset($entries['XML'])){
706 if(isset($entries['XML']['ERROR_STRING'])) {
707 $this->set_error($entries['XML']['ERROR_STRING']);
708 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
709 return(FALSE);
710 }
711 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
712 return(TRUE);
713 }
714 }
715 return(FALSE);
716 }
719 /*! \brief Returns the number of currently queued objects.
720 @return Integer
721 */
722 public function number_of_queued_entries()
723 {
724 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
725 $this->connect();
726 if($this->connect()){
727 $this->o_sock->write($xml_msg);
728 $str = trim($this->o_sock->read());
729 $entries = $this->xml_to_array($str);
730 if(isset($entries['XML'])){
731 return($entries['XML']['COUNT']);
732 }
733 }
734 return(-1);
735 }
738 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
739 {
740 $xml_message= "";
742 /* Prepare data */
743 foreach ($data as $key => $value){
744 if(is_array($value)){
745 foreach($value as $sub_value){
746 $xml_message.= "<$key>$sub_value</$key>";
747 }
748 }else{
749 $xml_message.= "<$key>$value</$key>";
750 }
751 }
753 /* Multiple targets? */
754 if (!is_array($to)){
755 $to_targets= array($to);
756 } else {
757 $to_targets= $to;
758 }
760 /* Build target strings */
761 $target ="";
762 foreach($to_targets as $to){
763 $target.= "<target>$to</target>";
764 }
766 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
767 }
770 /* Allows simply appending a new DaemonEvent
771 */
772 public function append($event)
773 {
774 if(!($event instanceof DaemonEvent)){
775 return(FALSE);
776 }
778 $this->reset_error();
780 /* Add to queue if new
781 */
782 if($event->is_new()){
784 $request_answer = FALSE;
785 if($event->get_type() == SCHEDULED_EVENT){
786 $action = $event->get_schedule_action();
787 }elseif($event->get_type() == TRIGGERED_EVENT){
788 $action = $event->get_trigger_action();
789 }else{
790 trigger_error("Unknown type of queue event given.");
791 return(FALSE);
792 }
794 /* Get event informations, like targets..
795 */
796 $targets = $event->get_targets();
797 $data = $event->save();
799 /* Append an entry for each target
800 */
801 foreach($targets as $target){
802 $data['macaddress'] = $target;
803 $this->send_data($action,$target,$data,$request_answer);
805 if($this->is_error()){
806 return(FALSE);
807 }
808 }
809 return(TRUE);
810 }else{
812 /* Updated edited entry.
813 */
814 $id = $event->get_id();
815 $data = $event->save();
816 return($this->update_entries(array($id),$data));
817 }
819 return(FALSE);
820 }
823 /*! \brief Returns an array containing all queued entries.
824 @return Array All queued entries as an array.
825 */
826 public function _send($data, $answer_expected= FALSE)
827 {
828 $this->reset_error();
829 $ret = array();
831 if($this->connect()){
832 $this->o_sock->write($data);
833 if ($answer_expected){
834 $str = trim($this->o_sock->read());
835 $entries = $this->xml_to_array($str);
836 if(isset($entries['XML']) && is_array($entries['XML'])){
837 $ret = $entries;
838 if(isset($entries['XML']['ERROR_STRING'])) {
839 $this->set_error($entries['XML']['ERROR_STRING']);
840 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
841 }else{
842 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
843 }
844 }
845 }else{
846 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
847 }
848 }
849 return($ret);
850 }
853 static function send($header, $to, $data= array(), $answer_expected = FALSE)
854 {
855 $xml_message= "";
857 /* Get communication object */
858 $d= new gosaSupportDaemon(TRUE,10);
860 /* Prepare data */
861 foreach ($data as $key => $value){
862 if(is_array($value)){
863 foreach($value as $sub_val){
864 $xml_message.= "<$key>$sub_value</$key>";
865 }
866 }else{
867 $xml_message.= "<$key>$value</$key>";
868 }
869 }
871 /* Multiple targets? */
872 if (!is_array($to)){
873 $to_targets= array($to);
874 } else {
875 $to_targets= $to;
876 }
878 /* Build target strings */
879 $target ="";
880 foreach($to_targets as $to){
881 $target.= "<target>$to</target>";
882 }
884 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
885 }
888 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
889 @param String $mac The mac address for which we want to remove all jobs.
890 */
891 function clean_queue_from_mac($mac)
892 {
893 global $config;
895 /* First of all we have to check which jobs are startet
896 * for $mac
897 */
898 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
900 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
902 $data = $this->_send($xml_msg,TRUE);
903 if(is_array($data) && isset($data['XML'])){
904 $already_aborted = FALSE;
905 foreach($data['XML'] as $name => $entry){
906 if(preg_match("/answer[0-9]*/i",$name)){
907 $entry['STATUS'] = strtoupper($entry['STATUS']);
908 switch($entry['STATUS']){
910 case 'PROCESSING' :
912 /* Send abort event, but only once
913 */
914 if($already_aborted){
915 break;
916 }elseif(class_available("DaemonEvent_faireboot")){
917 $already_aborted = TRUE;
918 $tmp = new DaemonEvent_faireboot($config);
919 $tmp->add_targets(array($mac));
920 $tmp->set_type(TRIGGERED_EVENT);
921 if(!$this->append($tmp)){
922 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
923 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
924 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
925 }else{
926 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
927 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
928 }
929 ;break;
930 }else{
931 /* Couldn't find abort event, just remove entry */
932 }
934 case 'WAITING':
935 case 'ERROR':
936 default :
938 /* Simply remove entries from queue.
939 * Failed or waiting events, can be removed without any trouble.
940 */
941 if(!$this->remove_entries(array($entry['ID']))){
942 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
943 }
944 ;break;
945 }
947 }
948 }
949 }
950 }
953 static function ping($target)
954 {
955 if (tests::is_mac($target)){
956 /* Get communication object */
957 $d= new gosaSupportDaemon(TRUE,0.5);
958 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
959 return (count($answer) ? TRUE:FALSE);
960 }
962 return (FALSE);
963 }
965 }
967 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
968 ?>