de02dd55b3460e2a3537795290be0e39785e8305
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,$attrs, $package="")
138 {
139 $this->reset_error();
140 $ret = array();
142 /* Check Parameter */
143 if(!is_array($attrs) || !count($attrs)){
144 trigger_error("Second parameter must be an array. With at least one attribute name.");
145 return($ret);
146 }
148 /* Create list of attributes to fetch */
149 $attr = "";
150 foreach($attrs as $at){
151 $attr.= $at.", ";
152 }
154 /* Create Daemon query */
155 if(empty($package)){
156 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
157 "<select> ".trim($attr,", ")." </select>".
158 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where>
159 </xml>";
160 }else{
161 }
163 echo htmlentities($xml_msg);
165 if($this->connect()){
166 $this->o_sock->write($xml_msg);
167 $str = trim($this->o_sock->read());
169 /* Check if something went wrong while reading */
170 if($this->o_sock->is_error()){
171 $this->set_error($this->o_sock->get_error());
172 return($ret);
173 }
175 $entries = $this->xml_to_array($str);
176 if(isset($entries['XML']) && is_array($entries['XML'])){
178 /* Check if returned values represent a valid answer */
179 if(isset($entries['XML'])){
180 if(isset($entries['XML']['ERROR_STRING'])) {
181 $this->set_error($entries['XML']['ERROR_STRING']);
182 new log("debug","GOsa-si",
183 get_class($this)."::".__FUNCTION__, array(),
184 "FAILED error was ".$this->get_error());
185 return($ret);
186 }
188 /* Unset header tags */
189 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
190 if(isset($entries['XML'][$type])){
191 unset($entries['XML'][$type]);
192 }
193 }
194 $ret = $entries['XML'];
195 }
196 }
197 }
198 return($ret);
201 }
204 public function FAI_get_server($name = "")
205 {
206 $this->reset_error();
208 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
209 $ret = array();
210 if($this->connect()){
211 $this->o_sock->write($xml_msg);
212 $str = trim($this->o_sock->read());
214 /* Check if something went wrong while reading */
215 if($this->o_sock->is_error()){
216 $this->set_error($this->o_sock->get_error());
217 return($ret);
218 }
220 $entries = $this->xml_to_array($str);
221 if(isset($entries['XML']) && is_array($entries['XML'])){
223 /* Check if returned values represent a valid answer */
224 if(isset($entries['XML'])){
225 if(isset($entries['XML']['ERROR_STRING'])) {
226 $this->set_error($entries['XML']['ERROR_STRING']);
227 new log("debug","GOsa-si",
228 get_class($this)."::".__FUNCTION__, array(),
229 "FAILED error was ".$this->get_error());
230 return($ret);
231 }
233 /* Unset header tags */
234 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
235 if(isset($entries['XML'][$type])){
236 unset($entries['XML'][$type]);
237 }
238 }
239 $ret = $entries['XML'];
240 }
241 }
242 }
243 return($ret);
244 }
247 public function FAI_get_classes($name)
248 {
249 $this->reset_error();
250 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
251 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
252 $ret = array();
253 if($this->connect()){
254 $this->o_sock->write($xml_msg);
255 $str = trim($this->o_sock->read());
257 /* Check if something went wrong while reading */
258 if($this->o_sock->is_error()){
259 $this->set_error($this->o_sock->get_error());
260 return($ret);
261 }
263 $entries = $this->xml_to_array($str);
264 if(isset($entries['XML']) && is_array($entries['XML'])){
266 /* Check if returned values represent a valid answer */
267 if(isset($entries['XML'])){
268 if(isset($entries['XML']['ERROR_STRING'])) {
269 $this->set_error($entries['XML']['ERROR_STRING']);
270 new log("debug","GOsa-si",
271 get_class($this)."::".__FUNCTION__, array($name),
272 "FAILED error was ".$this->get_error());
273 return($ret);
274 }
276 /* Unset header tags */
277 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
278 if(isset($entries['XML'][$type])){
279 unset($entries['XML'][$type]);
280 }
281 }
282 $ret = $entries['XML'];
283 }
284 }
285 }
286 return($ret);
287 }
290 /*! \brief Returns an array containing all queued entries.
291 @return Array All queued entries as an array.
292 */
293 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
294 {
295 $this->reset_error();
296 $ret = array();
298 $tags = "";
299 foreach($event_types as $type){
300 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
301 }
302 if(count($event_types) > 1){
303 $tags = "<connector>or</connector>".$tags;
304 }
305 if(count($event_types)){
306 $tags = "<where><clause>".$tags."</clause></where>";
307 }
309 $xml_msg =
310 "<xml>
311 <header>gosa_query_jobdb</header>
312 <target>GOSA</target>
313 <source>GOSA</source>
314 ".$tags."
316 <orderby>".$sort."</orderby>";
317 if($from != -1 && $to != -1){
318 $xml_msg.= "
319 <limit>
320 <from>".$from."</from>
321 <to>".$to."</to>
322 </limit>";
323 }
324 $xml_msg.= "
325 </xml>";
327 if($this->connect()){
328 $this->o_sock->write($xml_msg);
329 $str = trim($this->o_sock->read());
331 /* Check if something went wrong while reading */
332 if($this->o_sock->is_error()){
333 $this->set_error($this->o_sock->get_error());
334 return($ret);
335 }
337 $entries = $this->xml_to_array($str);
338 if(isset($entries['XML']) && is_array($entries['XML'])){
340 /* Check if returned values represent a valid answer */
341 if(isset($entries['XML'])){
343 /* Unset header tags */
344 foreach(array("HEADER","SOURCE","TARGET") as $type){
345 unset($entries['XML'][$type]);
346 }
347 $ret = $entries['XML'];
348 }
349 }
350 }
352 /* Remove session ID. No one is interested in this... */
353 unset($ret['SESSION_ID']);
355 return($ret);
356 }
359 /*! \brief Checks if the given ids are used queue ids.
360 @param Array The ids we want to check..
361 @return Array An array containing all ids as index and TRUE/FALSE as value.
362 */
363 public function ids_exist($ids)
364 {
365 if(!is_array($ids)){
366 trigger_error("Requires an array as parameter.");
367 return;
368 }
369 $this->reset_error();
371 $ret = array();
373 $xml_msg = "<xml>
374 <header>gosa_query_jobdb</header>
375 <target>GOSA</target>
376 <source>GOSA</source>
377 <where>
378 <clause>
379 <connector>or</connector>";
380 foreach($ids as $id){
381 $xml_msg .= "<phrase>
382 <operator>eq</operator>
383 <id>".$id."</id>
384 </phrase>";
385 }
386 $xml_msg .= "</clause>
387 </where>
388 </xml>";
390 if($this->connect()){
391 $this->o_sock->write($xml_msg);
392 $str = trim($this->o_sock->read());
394 /* Check if something went wrong while reading */
395 if($this->o_sock->is_error()){
396 $this->set_error($this->o_sock->get_error());
397 return($ret);
398 }
400 $entries = $this->xml_to_array($str);
401 if(isset($entries['XML']) && is_array($entries['XML'])){
402 foreach($entries['XML'] as $entry){
403 if(isset($entry['ID'])){
404 $ret[] = $entry['ID'];
405 }
406 }
407 }
408 }
409 return($ret);
410 }
413 /*! \brief Returns an entry containing all requested ids.
414 @param Array The IDs of the entries we want to return.
415 @return Array Of the requested entries.
416 */
417 public function get_entries_by_mac($macs)
418 {
419 if(!is_array($macs)){
420 trigger_error("Requires an array as parameter.");
421 return;
422 }
423 $this->reset_error();
425 $ret = array();
427 $xml_msg = "<xml>
428 <header>gosa_query_jobdb</header>
429 <target>GOSA</target>
430 <source>GOSA</source>
431 <where>
432 <clause>
433 <connector>or</connector>";
434 foreach($macs as $mac){
435 $xml_msg .= "<phrase>
436 <operator>eq</operator>
437 <macaddress>".$mac."</macaddress>
438 </phrase>";
439 }
440 $xml_msg .= "</clause>
441 </where>
442 </xml>";
444 if($this->connect()){
445 $this->o_sock->write($xml_msg);
446 $str = trim($this->o_sock->read());
448 /* Check if something went wrong while reading */
449 if($this->o_sock->is_error()){
450 $this->set_error($this->o_sock->get_error());
451 return($ret);
452 }
454 $entries = $this->xml_to_array($str);
455 if(isset($entries['XML'])){
456 foreach($entries['XML'] as $name => $entry){
457 if(preg_match("/^ANSWER[0-9]*$/",$name)){
458 $ret[$name] = $entry;
459 }
460 }
461 }
462 }
463 return($ret);
464 }
467 /*! \brief Returns an entry containing all requested ids.
468 @param Array The IDs of the entries we want to return.
469 @return Array Of the requested entries.
470 */
471 public function get_entries_by_id($ids)
472 {
473 if(!is_array($ids)){
474 trigger_error("Requires an array as parameter.");
475 return;
476 }
477 $this->reset_error();
479 $ret = array();
481 $xml_msg = "<xml>
482 <header>gosa_query_jobdb</header>
483 <target>GOSA</target>
484 <source>GOSA</source>
485 <where>
486 <clause>
487 <connector>or</connector>";
488 foreach($ids as $id){
489 $xml_msg .= "<phrase>
490 <operator>eq</operator>
491 <id>".$id."</id>
492 </phrase>";
493 }
494 $xml_msg .= "</clause>
495 </where>
496 </xml>";
498 if($this->connect()){
499 $this->o_sock->write($xml_msg);
500 $str = trim($this->o_sock->read());
502 /* Check if something went wrong while reading */
503 if($this->o_sock->is_error()){
504 $this->set_error($this->o_sock->get_error());
505 return($ret);
506 }
508 $entries = $this->xml_to_array($str);
509 if(isset($entries['XML'])){
510 foreach($entries['XML'] as $name => $entry){
511 if(preg_match("/^ANSWER[0-9]*$/",$name)){
512 $ret[$name] = $entry;
513 }
514 }
515 }
516 }
517 return($ret);
518 }
521 /*! \brief Checks if the given id is in use.
522 @param Integer The ID of the entry.
523 @return Boolean TRUE if entry exists.
524 */
525 public function id_exists($id)
526 {
527 if(!is_numeric($id)){
528 trigger_error("Requires an integer as parameter.");
529 return;
530 }
532 $this->reset_error();
534 $xml_msg = "<xml>
535 <header>gosa_query_jobdb</header>
536 <target>GOSA</target>
537 <source>GOSA</source>
538 <where>
539 <clause>
540 <phrase>
541 <operator>eq</operator>
542 <id>".$id."</id>
543 </phrase>
544 </clause>
545 </where>
546 </xml>";
548 if($this->connect()){
549 $this->o_sock->write($xml_msg);
550 $str = trim($this->o_sock->read());
552 /* Check if something went wrong while reading */
553 if($this->o_sock->is_error()){
554 $this->set_error($this->o_sock->get_error());
555 return(FALSE);
556 }
558 $entries = $this->xml_to_array($str);
559 if( isset($entries['XML']['HEADER']) &&
560 $entries['XML']['HEADER']=="answer" &&
561 isset($entries['XML']['ANSWER1'])){
562 return(TRUE);
563 }
564 }
565 return(FALSE);
566 }
569 /*! \brief Returns an entry from the gosaSupportQueue
570 @param Integer The ID of the entry we want to return.
571 @return Array Of the requested entry.
572 */
573 public function get_entry_by_id($id)
574 {
575 if(!is_numeric($id)){
576 trigger_error("Requires an integer as parameter.");
577 return;
578 }
579 $this->reset_error();
581 $ret = array();
582 $xml_msg = "<xml>
583 <header>gosa_query_jobdb</header>
584 <target>GOSA</target>
585 <source>GOSA</source>
586 <where>
587 <clause>
588 <phrase>
589 <operator>eq</operator>
590 <id>".$id."</id>
591 </phrase>
592 </clause>
593 </where>
594 </xml>";
595 if($this->connect()){
596 $this->o_sock->write($xml_msg);
597 $str = trim($this->o_sock->read());
599 /* Check if something went wrong while reading */
600 if($this->o_sock->is_error()){
601 $this->set_error($this->o_sock->get_error());
602 return($ret);
603 }
605 $entries = $this->xml_to_array($str);
606 if( isset($entries['XML']['HEADER']) &&
607 $entries['XML']['HEADER']=="answer" &&
608 isset($entries['XML']['ANSWER1'])){
609 $ret = $entries['XML']['ANSWER1'];
610 }
611 }
612 return($ret);
613 }
616 /*! \brief Removes a set of entries from the GOsa support queue.
617 @param Array The IDs to remove.
618 @return Boolean True on success.
619 */
620 public function remove_entries($ids)
621 {
622 if(!is_array($ids)){
623 trigger_error("Requires an array as parameter.");
624 return;
625 }
627 $this->reset_error();
629 $ret = array();
631 $xml_msg = "<xml>
632 <header>gosa_delete_jobdb_entry</header>
633 <target>GOSA</target>
634 <source>GOSA</source>
635 <where>
636 <clause>
637 <connector>or</connector>";
638 foreach($ids as $id){
639 $xml_msg .= "<phrase>
640 <operator>eq</operator>
641 <id>".$id."</id>
642 </phrase>";
643 }
644 $xml_msg .= "</clause>
645 </where>
646 </xml>";
648 if($this->connect()){
649 $this->o_sock->write($xml_msg);
650 $str = $this->o_sock->read();
652 /* Check if something went wrong while reading */
653 if($this->o_sock->is_error()){
654 $this->set_error($this->o_sock->get_error());
655 return($ret);
656 }
658 $entries = $this->xml_to_array($str);
659 if(isset($entries['XML']) || isset($entries['COUNT'])){
660 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
661 return(TRUE);
662 }else{
663 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
664 }
665 }
666 return(FALSE);
667 }
671 /*! \brief Removes an entry from the GOsa support queue.
672 @param Integer The ID of the entry we want to remove.
673 @return Boolean True on success.
674 */
675 public function remove_entry($id)
676 {
677 return($this->remove_entries(array($id)));
678 }
681 /*! \brief Parses the given xml string into an array
682 @param String XML string
683 @return Array Returns an array containing the xml structure.
684 */
685 private function xml_to_array($xml)
686 {
687 $params = array();
688 $level = array();
689 $parser = xml_parser_create_ns();
690 xml_parse_into_struct($parser, $xml, $vals, $index);
692 $err_id = xml_get_error_code($parser);
693 if($err_id){
694 xml_parser_free($parser);
695 }else{
696 xml_parser_free($parser);
698 foreach ($vals as $xml_elem) {
699 if ($xml_elem['type'] == 'open') {
700 if (array_key_exists('attributes',$xml_elem)) {
701 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
702 } else {
703 $level[$xml_elem['level']] = $xml_elem['tag'];
704 }
705 }
706 if ($xml_elem['type'] == 'complete') {
707 $start_level = 1;
708 $php_stmt = '$params';
709 while($start_level < $xml_elem['level']) {
710 $php_stmt .= '[$level['.$start_level.']]';
711 $start_level++;
712 }
713 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
714 @eval($php_stmt);
715 }
716 }
717 }
719 if(!isset($params['XML'])){
720 if (!array_key_exists('XML', $params)){
721 $this->set_error(_("Cannot not parse XML!"));
722 }
723 $params = array("COUNT" => 0);
724 }
726 return($params);
727 }
730 /*! \brief Updates an entry with a set of new values,
731 @param Integer The ID of the entry, we want to update.
732 @param Array The variables to update.
733 @return Boolean Returns TRUE on success.
734 */
735 public function update_entries($ids,$data)
736 {
737 $this->reset_error();
738 if(!is_array($ids)){
739 trigger_error("Requires an array as first parameter.");
740 return;
741 }
743 if(!is_array($data)){
744 trigger_error("Requires an array as second parameter.");
745 return;
746 }
748 $attr = "";
749 foreach($data as $key => $value){
750 if(is_array($value)){
751 foreach($value as $sub_value){
752 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
753 }
754 }else{
755 $attr.= "<$key>".strtolower($value)."</$key>\n";
756 }
757 }
759 $xml_msg = "<xml>
760 <header>gosa_update_status_jobdb_entry</header>
761 <target>GOSA</target>
762 <source>GOSA</source>
763 <where>
764 <clause>
765 <connector>or</connector>";
766 foreach($ids as $id){
767 $xml_msg .= "<phrase>
768 <operator>eq</operator>
769 <id>".$id."</id>
770 </phrase>";
771 }
772 $xml_msg .= "</clause>
773 </where>
774 <update>
775 ".$attr."
776 </update>
777 </xml>";
779 if($this->connect()){
781 $this->o_sock->write($xml_msg);
782 $str = trim($this->o_sock->read());
784 /* Check if something went wrong while reading */
785 if($this->o_sock->is_error()){
786 $this->set_error($this->o_sock->get_error());
787 return(FALSE);
788 }
790 $entries = $this->xml_to_array($str);
791 if(isset($entries['XML'])){
792 if(isset($entries['XML']['ERROR_STRING'])) {
793 $this->set_error($entries['XML']['ERROR_STRING']);
794 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
795 return(FALSE);
796 }
797 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
798 return(TRUE);
799 }
800 }
801 return(FALSE);
802 }
805 /*! \brief Returns the number of currently queued objects.
806 @return Integer
807 */
808 public function number_of_queued_entries()
809 {
810 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
811 $this->connect();
812 if($this->connect()){
813 $this->o_sock->write($xml_msg);
814 $str = trim($this->o_sock->read());
816 /* Check if something went wrong while reading */
817 if($this->o_sock->is_error()){
818 $this->set_error($this->o_sock->get_error());
819 return(0);
820 }
822 $entries = $this->xml_to_array($str);
823 if(isset($entries['XML'])){
824 return($entries['XML']['COUNT']);
825 }
826 }
827 return(-1);
828 }
831 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
832 {
833 $xml_message= "";
835 /* Prepare data */
836 foreach ($data as $key => $value){
837 if(is_array($value)){
838 foreach($value as $sub_value){
839 $xml_message.= "<$key>$sub_value</$key>";
840 }
841 }else{
842 $xml_message.= "<$key>$value</$key>";
843 }
844 }
846 /* Multiple targets? */
847 if (!is_array($to)){
848 $to_targets= array($to);
849 } else {
850 $to_targets= $to;
851 }
853 /* Build target strings */
854 $target ="";
855 foreach($to_targets as $to){
856 $target.= "<target>$to</target>";
857 }
859 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
860 }
863 /* Allows simply appending a new DaemonEvent
864 */
865 public function append($event)
866 {
867 if(!($event instanceof DaemonEvent)){
868 return(FALSE);
869 }
871 $this->reset_error();
873 /* Add to queue if new
874 */
875 if($event->is_new()){
877 $request_answer = FALSE;
878 if($event->get_type() == SCHEDULED_EVENT){
879 $action = $event->get_schedule_action();
880 }elseif($event->get_type() == TRIGGERED_EVENT){
881 $action = $event->get_trigger_action();
882 }else{
883 trigger_error("Unknown type of queue event given.");
884 return(FALSE);
885 }
887 /* Get event informations, like targets..
888 */
889 $targets = $event->get_targets();
890 $data = $event->save();
892 /* Append an entry for each target
893 */
894 foreach($targets as $target){
895 $data['macaddress'] = $target;
896 $this->send_data($action,$target,$data,$request_answer);
898 if($this->is_error()){
899 return(FALSE);
900 }
901 }
902 return(TRUE);
903 }else{
905 /* Updated edited entry.
906 */
907 $id = $event->get_id();
908 $data = $event->save();
909 return($this->update_entries(array($id),$data));
910 }
912 return(FALSE);
913 }
916 /*! \brief Returns an array containing all queued entries.
917 @return Array All queued entries as an array.
918 */
919 public function _send($data, $answer_expected= FALSE)
920 {
921 $this->reset_error();
922 $ret = array();
924 if($this->connect()){
925 $this->o_sock->write($data);
926 if ($answer_expected){
927 $str = trim($this->o_sock->read());
929 /* Check if something went wrong while reading */
930 if($this->o_sock->is_error()){
931 $this->set_error($this->o_sock->get_error());
932 return($ret);
933 }
935 $entries = $this->xml_to_array($str);
936 if(isset($entries['XML']) && is_array($entries['XML'])){
937 $ret = $entries;
938 if(isset($entries['XML']['ERROR_STRING'])) {
939 $this->set_error($entries['XML']['ERROR_STRING']);
940 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
941 }else{
942 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
943 }
944 }
945 }else{
946 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
947 }
948 }
949 return($ret);
950 }
953 static function send($header, $to, $data= array(), $answer_expected = FALSE)
954 {
955 $xml_message= "";
957 /* Get communication object */
958 $d= new gosaSupportDaemon(TRUE,10);
960 /* Prepare data */
961 foreach ($data as $key => $value){
962 if(is_array($value)){
963 foreach($value as $sub_val){
964 $xml_message.= "<$key>$sub_value</$key>";
965 }
966 }else{
967 $xml_message.= "<$key>$value</$key>";
968 }
969 }
971 /* Multiple targets? */
972 if (!is_array($to)){
973 $to_targets= array($to);
974 } else {
975 $to_targets= $to;
976 }
978 /* Build target strings */
979 $target ="";
980 foreach($to_targets as $to){
981 $target.= "<target>$to</target>";
982 }
984 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
985 }
988 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
989 @param String $mac The mac address for which we want to remove all jobs.
990 */
991 function clean_queue_from_mac($mac)
992 {
993 global $config;
995 /* First of all we have to check which jobs are startet
996 * for $mac
997 */
998 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1000 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1002 $data = $this->_send($xml_msg,TRUE);
1003 if(is_array($data) && isset($data['XML'])){
1004 $already_aborted = FALSE;
1005 foreach($data['XML'] as $name => $entry){
1006 if(preg_match("/answer[0-9]*/i",$name)){
1007 $entry['STATUS'] = strtoupper($entry['STATUS']);
1008 switch($entry['STATUS']){
1010 case 'PROCESSING' :
1012 /* Send abort event, but only once
1013 */
1014 if($already_aborted){
1015 break;
1016 }elseif(class_available("DaemonEvent_faireboot")){
1017 $already_aborted = TRUE;
1018 $tmp = new DaemonEvent_faireboot($config);
1019 $tmp->add_targets(array($mac));
1020 $tmp->set_type(TRIGGERED_EVENT);
1021 if(!$this->append($tmp)){
1022 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1023 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1024 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1025 }else{
1026 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1027 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1028 }
1029 ;break;
1030 }else{
1031 /* Couldn't find abort event, just remove entry */
1032 }
1034 case 'WAITING':
1035 case 'ERROR':
1036 default :
1038 /* Simply remove entries from queue.
1039 * Failed or waiting events, can be removed without any trouble.
1040 */
1041 if(!$this->remove_entries(array($entry['ID']))){
1042 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1043 }
1044 ;break;
1045 }
1047 }
1048 }
1049 }
1050 }
1053 static function ping($target)
1054 {
1055 if (tests::is_mac($target)){
1056 /* Get communication object */
1057 $d= new gosaSupportDaemon(TRUE,0.5);
1058 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1059 return (count($answer) ? TRUE:FALSE);
1060 }
1062 return (FALSE);
1063 }
1065 }
1067 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1068 ?>