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 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
162 "<select> ".trim($attr,", ")." </select>".
163 "<where><clause>
164 <connector>AND</connector>
165 <phrase><distribution>".$release."</distribution></phrase>
166 <phrase><package>".$package."</package></phrase>
167 </clause></where>
168 </xml>";
169 }
171 if($this->connect()){
172 $this->o_sock->write($xml_msg);
173 $str = trim($this->o_sock->read());
175 /* Check if something went wrong while reading */
176 if($this->o_sock->is_error()){
177 $this->set_error($this->o_sock->get_error());
178 return($ret);
179 }
181 $entries = $this->xml_to_array($str);
182 if(isset($entries['XML']) && is_array($entries['XML'])){
184 /* Check if returned values represent a valid answer */
185 if(isset($entries['XML'])){
186 if(isset($entries['XML']['ERROR_STRING'])) {
187 $this->set_error($entries['XML']['ERROR_STRING']);
188 new log("debug","GOsa-si",
189 get_class($this)."::".__FUNCTION__, array(),
190 "FAILED error was ".$this->get_error());
191 return($ret);
192 }
194 /* Unset header tags */
195 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
196 if(isset($entries['XML'][$type])){
197 unset($entries['XML'][$type]);
198 }
199 }
200 $ret = $entries['XML'];
201 }
202 }
203 }
204 return($ret);
207 }
210 public function FAI_get_server($name = "")
211 {
212 $this->reset_error();
214 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
215 $ret = array();
216 if($this->connect()){
217 $this->o_sock->write($xml_msg);
218 $str = trim($this->o_sock->read());
220 /* Check if something went wrong while reading */
221 if($this->o_sock->is_error()){
222 $this->set_error($this->o_sock->get_error());
223 return($ret);
224 }
226 $entries = $this->xml_to_array($str);
227 if(isset($entries['XML']) && is_array($entries['XML'])){
229 /* Check if returned values represent a valid answer */
230 if(isset($entries['XML'])){
231 if(isset($entries['XML']['ERROR_STRING'])) {
232 $this->set_error($entries['XML']['ERROR_STRING']);
233 new log("debug","GOsa-si",
234 get_class($this)."::".__FUNCTION__, array(),
235 "FAILED error was ".$this->get_error());
236 return($ret);
237 }
239 /* Unset header tags */
240 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
241 if(isset($entries['XML'][$type])){
242 unset($entries['XML'][$type]);
243 }
244 }
245 $ret = $entries['XML'];
246 }
247 }
248 }
249 return($ret);
250 }
253 public function FAI_get_classes($name)
254 {
255 $this->reset_error();
256 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
257 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
258 $ret = array();
259 if($this->connect()){
260 $this->o_sock->write($xml_msg);
261 $str = trim($this->o_sock->read());
263 /* Check if something went wrong while reading */
264 if($this->o_sock->is_error()){
265 $this->set_error($this->o_sock->get_error());
266 return($ret);
267 }
269 $entries = $this->xml_to_array($str);
270 if(isset($entries['XML']) && is_array($entries['XML'])){
272 /* Check if returned values represent a valid answer */
273 if(isset($entries['XML'])){
274 if(isset($entries['XML']['ERROR_STRING'])) {
275 $this->set_error($entries['XML']['ERROR_STRING']);
276 new log("debug","GOsa-si",
277 get_class($this)."::".__FUNCTION__, array($name),
278 "FAILED error was ".$this->get_error());
279 return($ret);
280 }
282 /* Unset header tags */
283 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
284 if(isset($entries['XML'][$type])){
285 unset($entries['XML'][$type]);
286 }
287 }
288 $ret = $entries['XML'];
289 }
290 }
291 }
292 return($ret);
293 }
296 /*! \brief Returns an array containing all queued entries.
297 @return Array All queued entries as an array.
298 */
299 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
300 {
301 $this->reset_error();
302 $ret = array();
304 $tags = "";
305 foreach($event_types as $type){
306 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
307 }
308 if(count($event_types) > 1){
309 $tags = "<connector>or</connector>".$tags;
310 }
311 if(count($event_types)){
312 $tags = "<where><clause>".$tags."</clause></where>";
313 }
315 $xml_msg =
316 "<xml>
317 <header>gosa_query_jobdb</header>
318 <target>GOSA</target>
319 <source>GOSA</source>
320 ".$tags."
322 <orderby>".$sort."</orderby>";
323 if($from != -1 && $to != -1){
324 $xml_msg.= "
325 <limit>
326 <from>".$from."</from>
327 <to>".$to."</to>
328 </limit>";
329 }
330 $xml_msg.= "
331 </xml>";
333 if($this->connect()){
334 $this->o_sock->write($xml_msg);
335 $str = trim($this->o_sock->read());
337 /* Check if something went wrong while reading */
338 if($this->o_sock->is_error()){
339 $this->set_error($this->o_sock->get_error());
340 return($ret);
341 }
343 $entries = $this->xml_to_array($str);
344 if(isset($entries['XML']) && is_array($entries['XML'])){
346 /* Check if returned values represent a valid answer */
347 if(isset($entries['XML'])){
349 /* Unset header tags */
350 foreach(array("HEADER","SOURCE","TARGET") as $type){
351 unset($entries['XML'][$type]);
352 }
353 $ret = $entries['XML'];
354 }
355 }
356 }
358 /* Remove session ID. No one is interested in this... */
359 unset($ret['SESSION_ID']);
361 return($ret);
362 }
365 /*! \brief Checks if the given ids are used queue ids.
366 @param Array The ids we want to check..
367 @return Array An array containing all ids as index and TRUE/FALSE as value.
368 */
369 public function ids_exist($ids)
370 {
371 if(!is_array($ids)){
372 trigger_error("Requires an array as parameter.");
373 return;
374 }
375 $this->reset_error();
377 $ret = array();
379 $xml_msg = "<xml>
380 <header>gosa_query_jobdb</header>
381 <target>GOSA</target>
382 <source>GOSA</source>
383 <where>
384 <clause>
385 <connector>or</connector>";
386 foreach($ids as $id){
387 $xml_msg .= "<phrase>
388 <operator>eq</operator>
389 <id>".$id."</id>
390 </phrase>";
391 }
392 $xml_msg .= "</clause>
393 </where>
394 </xml>";
396 if($this->connect()){
397 $this->o_sock->write($xml_msg);
398 $str = trim($this->o_sock->read());
400 /* Check if something went wrong while reading */
401 if($this->o_sock->is_error()){
402 $this->set_error($this->o_sock->get_error());
403 return($ret);
404 }
406 $entries = $this->xml_to_array($str);
407 if(isset($entries['XML']) && is_array($entries['XML'])){
408 foreach($entries['XML'] as $entry){
409 if(isset($entry['ID'])){
410 $ret[] = $entry['ID'];
411 }
412 }
413 }
414 }
415 return($ret);
416 }
419 /*! \brief Returns an entry containing all requested ids.
420 @param Array The IDs of the entries we want to return.
421 @return Array Of the requested entries.
422 */
423 public function get_entries_by_mac($macs)
424 {
425 if(!is_array($macs)){
426 trigger_error("Requires an array as parameter.");
427 return;
428 }
429 $this->reset_error();
431 $ret = array();
433 $xml_msg = "<xml>
434 <header>gosa_query_jobdb</header>
435 <target>GOSA</target>
436 <source>GOSA</source>
437 <where>
438 <clause>
439 <connector>or</connector>";
440 foreach($macs as $mac){
441 $xml_msg .= "<phrase>
442 <operator>eq</operator>
443 <macaddress>".$mac."</macaddress>
444 </phrase>";
445 }
446 $xml_msg .= "</clause>
447 </where>
448 </xml>";
450 if($this->connect()){
451 $this->o_sock->write($xml_msg);
452 $str = trim($this->o_sock->read());
454 /* Check if something went wrong while reading */
455 if($this->o_sock->is_error()){
456 $this->set_error($this->o_sock->get_error());
457 return($ret);
458 }
460 $entries = $this->xml_to_array($str);
461 if(isset($entries['XML'])){
462 foreach($entries['XML'] as $name => $entry){
463 if(preg_match("/^ANSWER[0-9]*$/",$name)){
464 $ret[$name] = $entry;
465 }
466 }
467 }
468 }
469 return($ret);
470 }
473 /*! \brief Returns an entry containing all requested ids.
474 @param Array The IDs of the entries we want to return.
475 @return Array Of the requested entries.
476 */
477 public function get_entries_by_id($ids)
478 {
479 if(!is_array($ids)){
480 trigger_error("Requires an array as parameter.");
481 return;
482 }
483 $this->reset_error();
485 $ret = array();
487 $xml_msg = "<xml>
488 <header>gosa_query_jobdb</header>
489 <target>GOSA</target>
490 <source>GOSA</source>
491 <where>
492 <clause>
493 <connector>or</connector>";
494 foreach($ids as $id){
495 $xml_msg .= "<phrase>
496 <operator>eq</operator>
497 <id>".$id."</id>
498 </phrase>";
499 }
500 $xml_msg .= "</clause>
501 </where>
502 </xml>";
504 if($this->connect()){
505 $this->o_sock->write($xml_msg);
506 $str = trim($this->o_sock->read());
508 /* Check if something went wrong while reading */
509 if($this->o_sock->is_error()){
510 $this->set_error($this->o_sock->get_error());
511 return($ret);
512 }
514 $entries = $this->xml_to_array($str);
515 if(isset($entries['XML'])){
516 foreach($entries['XML'] as $name => $entry){
517 if(preg_match("/^ANSWER[0-9]*$/",$name)){
518 $ret[$name] = $entry;
519 }
520 }
521 }
522 }
523 return($ret);
524 }
527 /*! \brief Checks if the given id is in use.
528 @param Integer The ID of the entry.
529 @return Boolean TRUE if entry exists.
530 */
531 public function id_exists($id)
532 {
533 if(!is_numeric($id)){
534 trigger_error("Requires an integer as parameter.");
535 return;
536 }
538 $this->reset_error();
540 $xml_msg = "<xml>
541 <header>gosa_query_jobdb</header>
542 <target>GOSA</target>
543 <source>GOSA</source>
544 <where>
545 <clause>
546 <phrase>
547 <operator>eq</operator>
548 <id>".$id."</id>
549 </phrase>
550 </clause>
551 </where>
552 </xml>";
554 if($this->connect()){
555 $this->o_sock->write($xml_msg);
556 $str = trim($this->o_sock->read());
558 /* Check if something went wrong while reading */
559 if($this->o_sock->is_error()){
560 $this->set_error($this->o_sock->get_error());
561 return(FALSE);
562 }
564 $entries = $this->xml_to_array($str);
565 if( isset($entries['XML']['HEADER']) &&
566 $entries['XML']['HEADER']=="answer" &&
567 isset($entries['XML']['ANSWER1'])){
568 return(TRUE);
569 }
570 }
571 return(FALSE);
572 }
575 /*! \brief Returns an entry from the gosaSupportQueue
576 @param Integer The ID of the entry we want to return.
577 @return Array Of the requested entry.
578 */
579 public function get_entry_by_id($id)
580 {
581 if(!is_numeric($id)){
582 trigger_error("Requires an integer as parameter.");
583 return;
584 }
585 $this->reset_error();
587 $ret = array();
588 $xml_msg = "<xml>
589 <header>gosa_query_jobdb</header>
590 <target>GOSA</target>
591 <source>GOSA</source>
592 <where>
593 <clause>
594 <phrase>
595 <operator>eq</operator>
596 <id>".$id."</id>
597 </phrase>
598 </clause>
599 </where>
600 </xml>";
601 if($this->connect()){
602 $this->o_sock->write($xml_msg);
603 $str = trim($this->o_sock->read());
605 /* Check if something went wrong while reading */
606 if($this->o_sock->is_error()){
607 $this->set_error($this->o_sock->get_error());
608 return($ret);
609 }
611 $entries = $this->xml_to_array($str);
612 if( isset($entries['XML']['HEADER']) &&
613 $entries['XML']['HEADER']=="answer" &&
614 isset($entries['XML']['ANSWER1'])){
615 $ret = $entries['XML']['ANSWER1'];
616 }
617 }
618 return($ret);
619 }
622 /*! \brief Removes a set of entries from the GOsa support queue.
623 @param Array The IDs to remove.
624 @return Boolean True on success.
625 */
626 public function remove_entries($ids)
627 {
628 if(!is_array($ids)){
629 trigger_error("Requires an array as parameter.");
630 return;
631 }
633 $this->reset_error();
635 $ret = array();
637 $xml_msg = "<xml>
638 <header>gosa_delete_jobdb_entry</header>
639 <target>GOSA</target>
640 <source>GOSA</source>
641 <where>
642 <clause>
643 <connector>or</connector>";
644 foreach($ids as $id){
645 $xml_msg .= "<phrase>
646 <operator>eq</operator>
647 <id>".$id."</id>
648 </phrase>";
649 }
650 $xml_msg .= "</clause>
651 </where>
652 </xml>";
654 if($this->connect()){
655 $this->o_sock->write($xml_msg);
656 $str = $this->o_sock->read();
658 /* Check if something went wrong while reading */
659 if($this->o_sock->is_error()){
660 $this->set_error($this->o_sock->get_error());
661 return($ret);
662 }
664 $entries = $this->xml_to_array($str);
665 if(isset($entries['XML']) || isset($entries['COUNT'])){
666 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
667 return(TRUE);
668 }else{
669 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
670 }
671 }
672 return(FALSE);
673 }
677 /*! \brief Removes an entry from the GOsa support queue.
678 @param Integer The ID of the entry we want to remove.
679 @return Boolean True on success.
680 */
681 public function remove_entry($id)
682 {
683 return($this->remove_entries(array($id)));
684 }
687 /*! \brief Parses the given xml string into an array
688 @param String XML string
689 @return Array Returns an array containing the xml structure.
690 */
691 private function xml_to_array($xml)
692 {
693 $params = array();
694 $level = array();
695 $parser = xml_parser_create_ns();
696 xml_parse_into_struct($parser, $xml, $vals, $index);
698 $err_id = xml_get_error_code($parser);
699 if($err_id){
700 xml_parser_free($parser);
701 }else{
702 xml_parser_free($parser);
704 foreach ($vals as $xml_elem) {
705 if ($xml_elem['type'] == 'open') {
706 if (array_key_exists('attributes',$xml_elem)) {
707 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
708 } else {
709 $level[$xml_elem['level']] = $xml_elem['tag'];
710 }
711 }
712 if ($xml_elem['type'] == 'complete') {
713 $start_level = 1;
714 $php_stmt = '$params';
715 while($start_level < $xml_elem['level']) {
716 $php_stmt .= '[$level['.$start_level.']]';
717 $start_level++;
718 }
719 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
720 @eval($php_stmt);
721 }
722 }
723 }
725 if(!isset($params['XML'])){
726 if (!array_key_exists('XML', $params)){
727 $this->set_error(_("Cannot not parse XML!"));
728 }
729 $params = array("COUNT" => 0);
730 }
732 return($params);
733 }
736 /*! \brief Updates an entry with a set of new values,
737 @param Integer The ID of the entry, we want to update.
738 @param Array The variables to update.
739 @return Boolean Returns TRUE on success.
740 */
741 public function update_entries($ids,$data)
742 {
743 $this->reset_error();
744 if(!is_array($ids)){
745 trigger_error("Requires an array as first parameter.");
746 return;
747 }
749 if(!is_array($data)){
750 trigger_error("Requires an array as second parameter.");
751 return;
752 }
754 $attr = "";
755 foreach($data as $key => $value){
756 if(is_array($value)){
757 foreach($value as $sub_value){
758 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
759 }
760 }else{
761 $attr.= "<$key>".strtolower($value)."</$key>\n";
762 }
763 }
765 $xml_msg = "<xml>
766 <header>gosa_update_status_jobdb_entry</header>
767 <target>GOSA</target>
768 <source>GOSA</source>
769 <where>
770 <clause>
771 <connector>or</connector>";
772 foreach($ids as $id){
773 $xml_msg .= "<phrase>
774 <operator>eq</operator>
775 <id>".$id."</id>
776 </phrase>";
777 }
778 $xml_msg .= "</clause>
779 </where>
780 <update>
781 ".$attr."
782 </update>
783 </xml>";
785 if($this->connect()){
787 $this->o_sock->write($xml_msg);
788 $str = trim($this->o_sock->read());
790 /* Check if something went wrong while reading */
791 if($this->o_sock->is_error()){
792 $this->set_error($this->o_sock->get_error());
793 return(FALSE);
794 }
796 $entries = $this->xml_to_array($str);
797 if(isset($entries['XML'])){
798 if(isset($entries['XML']['ERROR_STRING'])) {
799 $this->set_error($entries['XML']['ERROR_STRING']);
800 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
801 return(FALSE);
802 }
803 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
804 return(TRUE);
805 }
806 }
807 return(FALSE);
808 }
811 /*! \brief Returns the number of currently queued objects.
812 @return Integer
813 */
814 public function number_of_queued_entries()
815 {
816 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
817 $this->connect();
818 if($this->connect()){
819 $this->o_sock->write($xml_msg);
820 $str = trim($this->o_sock->read());
822 /* Check if something went wrong while reading */
823 if($this->o_sock->is_error()){
824 $this->set_error($this->o_sock->get_error());
825 return(0);
826 }
828 $entries = $this->xml_to_array($str);
829 if(isset($entries['XML'])){
830 return($entries['XML']['COUNT']);
831 }
832 }
833 return(-1);
834 }
837 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
838 {
839 $xml_message= "";
841 /* Prepare data */
842 foreach ($data as $key => $value){
843 if(is_array($value)){
844 foreach($value as $sub_value){
845 $xml_message.= "<$key>$sub_value</$key>";
846 }
847 }else{
848 $xml_message.= "<$key>$value</$key>";
849 }
850 }
852 /* Multiple targets? */
853 if (!is_array($to)){
854 $to_targets= array($to);
855 } else {
856 $to_targets= $to;
857 }
859 /* Build target strings */
860 $target ="";
861 foreach($to_targets as $to){
862 $target.= "<target>$to</target>";
863 }
865 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
866 }
869 /* Allows simply appending a new DaemonEvent
870 */
871 public function append($event)
872 {
873 if(!($event instanceof DaemonEvent)){
874 return(FALSE);
875 }
877 $this->reset_error();
879 /* Add to queue if new
880 */
881 if($event->is_new()){
883 $request_answer = FALSE;
884 if($event->get_type() == SCHEDULED_EVENT){
885 $action = $event->get_schedule_action();
886 }elseif($event->get_type() == TRIGGERED_EVENT){
887 $action = $event->get_trigger_action();
888 }else{
889 trigger_error("Unknown type of queue event given.");
890 return(FALSE);
891 }
893 /* Get event informations, like targets..
894 */
895 $targets = $event->get_targets();
896 $data = $event->save();
898 /* Append an entry for each target
899 */
900 foreach($targets as $target){
901 $data['macaddress'] = $target;
902 $this->send_data($action,$target,$data,$request_answer);
904 if($this->is_error()){
905 return(FALSE);
906 }
907 }
908 return(TRUE);
909 }else{
911 /* Updated edited entry.
912 */
913 $id = $event->get_id();
914 $data = $event->save();
915 return($this->update_entries(array($id),$data));
916 }
918 return(FALSE);
919 }
922 /*! \brief Returns an array containing all queued entries.
923 @return Array All queued entries as an array.
924 */
925 public function _send($data, $answer_expected= FALSE)
926 {
927 $this->reset_error();
928 $ret = array();
930 if($this->connect()){
931 $this->o_sock->write($data);
932 if ($answer_expected){
933 $str = trim($this->o_sock->read());
935 /* Check if something went wrong while reading */
936 if($this->o_sock->is_error()){
937 $this->set_error($this->o_sock->get_error());
938 return($ret);
939 }
941 $entries = $this->xml_to_array($str);
942 if(isset($entries['XML']) && is_array($entries['XML'])){
943 $ret = $entries;
944 if(isset($entries['XML']['ERROR_STRING'])) {
945 $this->set_error($entries['XML']['ERROR_STRING']);
946 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
947 }else{
948 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
949 }
950 }
951 }else{
952 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
953 }
954 }
955 return($ret);
956 }
959 static function send($header, $to, $data= array(), $answer_expected = FALSE)
960 {
961 $xml_message= "";
963 /* Get communication object */
964 $d= new gosaSupportDaemon(TRUE,10);
966 /* Prepare data */
967 foreach ($data as $key => $value){
968 if(is_array($value)){
969 foreach($value as $sub_val){
970 $xml_message.= "<$key>$sub_value</$key>";
971 }
972 }else{
973 $xml_message.= "<$key>$value</$key>";
974 }
975 }
977 /* Multiple targets? */
978 if (!is_array($to)){
979 $to_targets= array($to);
980 } else {
981 $to_targets= $to;
982 }
984 /* Build target strings */
985 $target ="";
986 foreach($to_targets as $to){
987 $target.= "<target>$to</target>";
988 }
990 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
991 }
994 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
995 @param String $mac The mac address for which we want to remove all jobs.
996 */
997 function clean_queue_from_mac($mac)
998 {
999 global $config;
1001 /* First of all we have to check which jobs are startet
1002 * for $mac
1003 */
1004 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1006 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1008 $data = $this->_send($xml_msg,TRUE);
1009 if(is_array($data) && isset($data['XML'])){
1010 $already_aborted = FALSE;
1011 foreach($data['XML'] as $name => $entry){
1012 if(preg_match("/answer[0-9]*/i",$name)){
1013 $entry['STATUS'] = strtoupper($entry['STATUS']);
1014 switch($entry['STATUS']){
1016 case 'PROCESSING' :
1018 /* Send abort event, but only once
1019 */
1020 if($already_aborted){
1021 break;
1022 }elseif(class_available("DaemonEvent_faireboot")){
1023 $already_aborted = TRUE;
1024 $tmp = new DaemonEvent_faireboot($config);
1025 $tmp->add_targets(array($mac));
1026 $tmp->set_type(TRIGGERED_EVENT);
1027 if(!$this->append($tmp)){
1028 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1029 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1030 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1031 }else{
1032 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1033 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1034 }
1035 ;break;
1036 }else{
1037 /* Couldn't find abort event, just remove entry */
1038 }
1040 case 'WAITING':
1041 case 'ERROR':
1042 default :
1044 /* Simply remove entries from queue.
1045 * Failed or waiting events, can be removed without any trouble.
1046 */
1047 if(!$this->remove_entries(array($entry['ID']))){
1048 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1049 }
1050 ;break;
1051 }
1053 }
1054 }
1055 }
1056 }
1059 static function ping($target)
1060 {
1061 if (tests::is_mac($target)){
1062 /* Get communication object */
1063 $d= new gosaSupportDaemon(TRUE,0.5);
1064 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1065 return (count($answer) ? TRUE:FALSE);
1066 }
1068 return (FALSE);
1069 }
1071 }
1073 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1074 ?>