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()", "Cannot connect to si-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 = array())
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 /* Check Parameter */
149 if(!is_array($package)){
150 trigger_error("Third parameter must be an array. With at least one attribute name.");
151 return($ret);
152 }
154 /* Create list of attributes to fetch */
155 $attr = "";
156 foreach($attrs as $at){
157 $attr.= "<select>".$at."</select>";
158 }
160 /* Create list of attributes to fetch */
161 $pkgs = "";
162 foreach($package as $pkg){
163 $pkgs .="
164 <phrase>
165 <operator>like</operator>
166 <package>".$pkg."</package>
167 </phrase>
168 ";
170 }
172 /* Create Daemon query */
173 if(!count($package)){
174 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
175 $attr.
176 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where>
177 </xml>";
178 }else{
179 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
180 $attr.
181 "<where>
183 <clause>
184 <phrase>
185 <distribution>".$release."</distribution>
186 </phrase>
187 </clause>
189 <clause>
190 <connector>OR</connector>
191 ".$pkgs."
192 </clause>
193 </where>
194 <limit><from>0</from><to>100</to></limit>
195 </xml>";
196 }
198 if($this->connect()){
199 $this->o_sock->write($xml_msg);
200 $str = trim($this->o_sock->read());
202 /* Check if something went wrong while reading */
203 if($this->o_sock->is_error()){
204 $this->set_error($this->o_sock->get_error());
205 return($ret);
206 }
208 $entries = $this->xml_to_array($str);
209 if(isset($entries['XML']) && is_array($entries['XML'])){
211 /* Check if returned values represent a valid answer */
212 if(isset($entries['XML'])){
213 if(isset($entries['XML']['ERROR_STRING'])) {
214 $this->set_error($entries['XML']['ERROR_STRING']);
215 new log("debug","GOsa-si",
216 get_class($this)."::".__FUNCTION__, array(),
217 "FAILED error was ".$this->get_error());
218 return($ret);
219 }
221 /* Unset header tags */
222 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
223 if(isset($entries['XML'][$type])){
224 unset($entries['XML'][$type]);
225 }
226 }
227 $ret = $entries['XML'];
228 }
229 }
230 }
231 return($ret);
234 }
237 public function FAI_get_server($name = "")
238 {
239 $this->reset_error();
241 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
242 $ret = array();
243 if($this->connect()){
244 $this->o_sock->write($xml_msg);
245 $str = trim($this->o_sock->read());
247 /* Check if something went wrong while reading */
248 if($this->o_sock->is_error()){
249 $this->set_error($this->o_sock->get_error());
250 return($ret);
251 }
253 $entries = $this->xml_to_array($str);
254 if(isset($entries['XML']) && is_array($entries['XML'])){
256 /* Check if returned values represent a valid answer */
257 if(isset($entries['XML'])){
258 if(isset($entries['XML']['ERROR_STRING'])) {
259 $this->set_error($entries['XML']['ERROR_STRING']);
260 new log("debug","GOsa-si",
261 get_class($this)."::".__FUNCTION__, array(),
262 "FAILED error was ".$this->get_error());
263 return($ret);
264 }
266 /* Unset header tags */
267 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
268 if(isset($entries['XML'][$type])){
269 unset($entries['XML'][$type]);
270 }
271 }
272 $ret = $entries['XML'];
273 }
274 }
275 }
276 return($ret);
277 }
280 public function FAI_get_classes($name)
281 {
282 $this->reset_error();
283 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
284 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
285 $ret = array();
286 if($this->connect()){
287 $this->o_sock->write($xml_msg);
288 $str = trim($this->o_sock->read());
290 /* Check if something went wrong while reading */
291 if($this->o_sock->is_error()){
292 $this->set_error($this->o_sock->get_error());
293 return($ret);
294 }
296 $entries = $this->xml_to_array($str);
297 if(isset($entries['XML']) && is_array($entries['XML'])){
299 /* Check if returned values represent a valid answer */
300 if(isset($entries['XML'])){
301 if(isset($entries['XML']['ERROR_STRING'])) {
302 $this->set_error($entries['XML']['ERROR_STRING']);
303 new log("debug","GOsa-si",
304 get_class($this)."::".__FUNCTION__, array($name),
305 "FAILED error was ".$this->get_error());
306 return($ret);
307 }
309 /* Unset header tags */
310 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
311 if(isset($entries['XML'][$type])){
312 unset($entries['XML'][$type]);
313 }
314 }
315 $ret = $entries['XML'];
316 }
317 }
318 }
319 return($ret);
320 }
323 /*! \brief Returns an array containing all queued entries.
324 @return Array All queued entries as an array.
325 */
326 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
327 {
328 $this->reset_error();
329 $ret = array();
331 $tags = "";
332 foreach($event_types as $type){
333 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
334 }
335 if(count($event_types) > 1){
336 $tags = "<connector>or</connector>".$tags;
337 }
338 if(count($event_types)){
339 $tags = "<where><clause>".$tags."</clause></where>";
340 }
342 $xml_msg =
343 "<xml>
344 <header>gosa_query_jobdb</header>
345 <target>GOSA</target>
346 <source>GOSA</source>
347 ".$tags."
349 <orderby>".$sort."</orderby>";
350 if($from != -1 && $to != -1){
351 $xml_msg.= "
352 <limit>
353 <from>".$from."</from>
354 <to>".$to."</to>
355 </limit>";
356 }
357 $xml_msg.= "
358 </xml>";
360 if($this->connect()){
361 $this->o_sock->write($xml_msg);
362 $str = trim($this->o_sock->read());
364 /* Check if something went wrong while reading */
365 if($this->o_sock->is_error()){
366 $this->set_error($this->o_sock->get_error());
367 return($ret);
368 }
370 $entries = $this->xml_to_array($str);
371 if(isset($entries['XML']) && is_array($entries['XML'])){
373 /* Check if returned values represent a valid answer */
374 if(isset($entries['XML'])){
376 /* Unset header tags */
377 foreach(array("HEADER","SOURCE","TARGET") as $type){
378 unset($entries['XML'][$type]);
379 }
380 $ret = $entries['XML'];
381 }
382 }
383 }
385 /* Remove session ID. No one is interested in this... */
386 unset($ret['SESSION_ID']);
388 return($ret);
389 }
392 /*! \brief Checks if the given ids are used queue ids.
393 @param Array The ids we want to check..
394 @return Array An array containing all ids as index and TRUE/FALSE as value.
395 */
396 public function ids_exist($ids)
397 {
398 if(!is_array($ids)){
399 trigger_error("Requires an array as parameter.");
400 return;
401 }
402 $this->reset_error();
404 $ret = array();
406 $xml_msg = "<xml>
407 <header>gosa_query_jobdb</header>
408 <target>GOSA</target>
409 <source>GOSA</source>
410 <where>
411 <clause>
412 <connector>or</connector>";
413 foreach($ids as $id){
414 $xml_msg .= "<phrase>
415 <operator>eq</operator>
416 <id>".$id."</id>
417 </phrase>";
418 }
419 $xml_msg .= "</clause>
420 </where>
421 </xml>";
423 if($this->connect()){
424 $this->o_sock->write($xml_msg);
425 $str = trim($this->o_sock->read());
427 /* Check if something went wrong while reading */
428 if($this->o_sock->is_error()){
429 $this->set_error($this->o_sock->get_error());
430 return($ret);
431 }
433 $entries = $this->xml_to_array($str);
434 if(isset($entries['XML']) && is_array($entries['XML'])){
435 foreach($entries['XML'] as $entry){
436 if(isset($entry['ID'])){
437 $ret[] = $entry['ID'];
438 }
439 }
440 }
441 }
442 return($ret);
443 }
446 /*! \brief Returns an entry containing all requested ids.
447 @param Array The IDs of the entries we want to return.
448 @return Array Of the requested entries.
449 */
450 public function get_entries_by_mac($macs)
451 {
452 if(!is_array($macs)){
453 trigger_error("Requires an array as parameter.");
454 return;
455 }
456 $this->reset_error();
458 $ret = array();
460 $xml_msg = "<xml>
461 <header>gosa_query_jobdb</header>
462 <target>GOSA</target>
463 <source>GOSA</source>
464 <where>
465 <clause>
466 <connector>or</connector>";
467 foreach($macs as $mac){
468 $xml_msg .= "<phrase>
469 <operator>eq</operator>
470 <macaddress>".$mac."</macaddress>
471 </phrase>";
472 }
473 $xml_msg .= "</clause>
474 </where>
475 </xml>";
477 if($this->connect()){
478 $this->o_sock->write($xml_msg);
479 $str = trim($this->o_sock->read());
481 /* Check if something went wrong while reading */
482 if($this->o_sock->is_error()){
483 $this->set_error($this->o_sock->get_error());
484 return($ret);
485 }
487 $entries = $this->xml_to_array($str);
488 if(isset($entries['XML'])){
489 foreach($entries['XML'] as $name => $entry){
490 if(preg_match("/^ANSWER[0-9]*$/",$name)){
491 $ret[$name] = $entry;
492 }
493 }
494 }
495 }
496 return($ret);
497 }
500 /*! \brief Returns an entry containing all requested ids.
501 @param Array The IDs of the entries we want to return.
502 @return Array Of the requested entries.
503 */
504 public function get_entries_by_id($ids)
505 {
506 if(!is_array($ids)){
507 trigger_error("Requires an array as parameter.");
508 return;
509 }
510 $this->reset_error();
512 $ret = array();
514 $xml_msg = "<xml>
515 <header>gosa_query_jobdb</header>
516 <target>GOSA</target>
517 <source>GOSA</source>
518 <where>
519 <clause>
520 <connector>or</connector>";
521 foreach($ids as $id){
522 $xml_msg .= "<phrase>
523 <operator>eq</operator>
524 <id>".$id."</id>
525 </phrase>";
526 }
527 $xml_msg .= "</clause>
528 </where>
529 </xml>";
531 if($this->connect()){
532 $this->o_sock->write($xml_msg);
533 $str = trim($this->o_sock->read());
535 /* Check if something went wrong while reading */
536 if($this->o_sock->is_error()){
537 $this->set_error($this->o_sock->get_error());
538 return($ret);
539 }
541 $entries = $this->xml_to_array($str);
542 if(isset($entries['XML'])){
543 foreach($entries['XML'] as $name => $entry){
544 if(preg_match("/^ANSWER[0-9]*$/",$name)){
545 $ret[$name] = $entry;
546 }
547 }
548 }
549 }
550 return($ret);
551 }
554 /*! \brief Checks if the given id is in use.
555 @param Integer The ID of the entry.
556 @return Boolean TRUE if entry exists.
557 */
558 public function id_exists($id)
559 {
560 if(!is_numeric($id)){
561 trigger_error("Requires an integer as parameter.");
562 return;
563 }
565 $this->reset_error();
567 $xml_msg = "<xml>
568 <header>gosa_query_jobdb</header>
569 <target>GOSA</target>
570 <source>GOSA</source>
571 <where>
572 <clause>
573 <phrase>
574 <operator>eq</operator>
575 <id>".$id."</id>
576 </phrase>
577 </clause>
578 </where>
579 </xml>";
581 if($this->connect()){
582 $this->o_sock->write($xml_msg);
583 $str = trim($this->o_sock->read());
585 /* Check if something went wrong while reading */
586 if($this->o_sock->is_error()){
587 $this->set_error($this->o_sock->get_error());
588 return(FALSE);
589 }
591 $entries = $this->xml_to_array($str);
592 if( isset($entries['XML']['HEADER']) &&
593 $entries['XML']['HEADER']=="answer" &&
594 isset($entries['XML']['ANSWER1'])){
595 return(TRUE);
596 }
597 }
598 return(FALSE);
599 }
602 /*! \brief Returns an entry from the gosaSupportQueue
603 @param Integer The ID of the entry we want to return.
604 @return Array Of the requested entry.
605 */
606 public function get_entry_by_id($id)
607 {
608 if(!is_numeric($id)){
609 trigger_error("Requires an integer as parameter.");
610 return;
611 }
612 $this->reset_error();
614 $ret = array();
615 $xml_msg = "<xml>
616 <header>gosa_query_jobdb</header>
617 <target>GOSA</target>
618 <source>GOSA</source>
619 <where>
620 <clause>
621 <phrase>
622 <operator>eq</operator>
623 <id>".$id."</id>
624 </phrase>
625 </clause>
626 </where>
627 </xml>";
628 if($this->connect()){
629 $this->o_sock->write($xml_msg);
630 $str = trim($this->o_sock->read());
632 /* Check if something went wrong while reading */
633 if($this->o_sock->is_error()){
634 $this->set_error($this->o_sock->get_error());
635 return($ret);
636 }
638 $entries = $this->xml_to_array($str);
639 if( isset($entries['XML']['HEADER']) &&
640 $entries['XML']['HEADER']=="answer" &&
641 isset($entries['XML']['ANSWER1'])){
642 $ret = $entries['XML']['ANSWER1'];
643 }
644 }
645 return($ret);
646 }
649 /*! \brief Removes a set of entries from the GOsa support queue.
650 @param Array The IDs to remove.
651 @return Boolean True on success.
652 */
653 public function remove_entries($ids)
654 {
655 if(!is_array($ids)){
656 trigger_error("Requires an array as parameter.");
657 return;
658 }
660 $this->reset_error();
662 $ret = array();
664 $xml_msg = "<xml>
665 <header>gosa_delete_jobdb_entry</header>
666 <target>GOSA</target>
667 <source>GOSA</source>
668 <where>
669 <clause>
670 <connector>or</connector>";
671 foreach($ids as $id){
672 $xml_msg .= "<phrase>
673 <operator>eq</operator>
674 <id>".$id."</id>
675 </phrase>";
676 }
677 $xml_msg .= "</clause>
678 </where>
679 </xml>";
681 if($this->connect()){
682 $this->o_sock->write($xml_msg);
683 $str = $this->o_sock->read();
685 /* Check if something went wrong while reading */
686 if($this->o_sock->is_error()){
687 $this->set_error($this->o_sock->get_error());
688 return($ret);
689 }
691 $entries = $this->xml_to_array($str);
692 if(isset($entries['XML']) || isset($entries['COUNT'])){
693 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
694 return(TRUE);
695 }else{
696 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
697 }
698 }
699 return(FALSE);
700 }
704 /*! \brief Removes an entry from the GOsa support queue.
705 @param Integer The ID of the entry we want to remove.
706 @return Boolean True on success.
707 */
708 public function remove_entry($id)
709 {
710 return($this->remove_entries(array($id)));
711 }
714 /*! \brief Parses the given xml string into an array
715 @param String XML string
716 @return Array Returns an array containing the xml structure.
717 */
718 private function xml_to_array($xml)
719 {
720 $params = array();
721 $level = array();
722 $parser = xml_parser_create_ns();
723 xml_parse_into_struct($parser, $xml, $vals, $index);
725 $err_id = xml_get_error_code($parser);
726 if($err_id){
727 xml_parser_free($parser);
728 }else{
729 xml_parser_free($parser);
731 foreach ($vals as $xml_elem) {
732 if ($xml_elem['type'] == 'open') {
733 if (array_key_exists('attributes',$xml_elem)) {
734 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
735 } else {
736 $level[$xml_elem['level']] = $xml_elem['tag'];
737 }
738 }
739 if ($xml_elem['type'] == 'complete') {
740 $start_level = 1;
741 $php_stmt = '$params';
742 while($start_level < $xml_elem['level']) {
743 $php_stmt .= '[$level['.$start_level.']]';
744 $start_level++;
745 }
746 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
747 @eval($php_stmt);
748 }
749 }
750 }
752 if(!isset($params['XML'])){
753 if (!array_key_exists('XML', $params)){
754 $this->set_error(_("Cannot not parse XML!"));
755 }
756 $params = array("COUNT" => 0);
757 }
759 return($params);
760 }
763 /*! \brief Updates an entry with a set of new values,
764 @param Integer The ID of the entry, we want to update.
765 @param Array The variables to update.
766 @return Boolean Returns TRUE on success.
767 */
768 public function update_entries($ids,$data)
769 {
770 $this->reset_error();
771 if(!is_array($ids)){
772 trigger_error("Requires an array as first parameter.");
773 return;
774 }
776 if(!is_array($data)){
777 trigger_error("Requires an array as second parameter.");
778 return;
779 }
781 $attr = "";
782 foreach($data as $key => $value){
783 if(is_array($value)){
784 foreach($value as $sub_value){
785 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
786 }
787 }else{
788 $attr.= "<$key>".strtolower($value)."</$key>\n";
789 }
790 }
792 $xml_msg = "<xml>
793 <header>gosa_update_status_jobdb_entry</header>
794 <target>GOSA</target>
795 <source>GOSA</source>
796 <where>
797 <clause>
798 <connector>or</connector>";
799 foreach($ids as $id){
800 $xml_msg .= "<phrase>
801 <operator>eq</operator>
802 <id>".$id."</id>
803 </phrase>";
804 }
805 $xml_msg .= "</clause>
806 </where>
807 <update>
808 ".$attr."
809 </update>
810 </xml>";
812 if($this->connect()){
814 $this->o_sock->write($xml_msg);
815 $str = trim($this->o_sock->read());
817 /* Check if something went wrong while reading */
818 if($this->o_sock->is_error()){
819 $this->set_error($this->o_sock->get_error());
820 return(FALSE);
821 }
823 $entries = $this->xml_to_array($str);
824 if(isset($entries['XML'])){
825 if(isset($entries['XML']['ERROR_STRING'])) {
826 $this->set_error($entries['XML']['ERROR_STRING']);
827 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
828 return(FALSE);
829 }
830 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
831 return(TRUE);
832 }
833 }
834 return(FALSE);
835 }
838 /*! \brief Returns the number of currently queued objects.
839 @return Integer
840 */
841 public function number_of_queued_entries()
842 {
843 $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
844 $this->connect();
845 if($this->connect()){
846 $this->o_sock->write($xml_msg);
847 $str = trim($this->o_sock->read());
849 /* Check if something went wrong while reading */
850 if($this->o_sock->is_error()){
851 $this->set_error($this->o_sock->get_error());
852 return(0);
853 }
855 $entries = $this->xml_to_array($str);
856 if(isset($entries['XML'])){
857 return($entries['XML']['COUNT']);
858 }
859 }
860 return(-1);
861 }
864 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
865 {
866 $xml_message= "";
868 /* Prepare data */
869 foreach ($data as $key => $value){
870 if(is_array($value)){
871 foreach($value as $sub_value){
872 $xml_message.= "<$key>$sub_value</$key>";
873 }
874 }else{
875 $xml_message.= "<$key>$value</$key>";
876 }
877 }
879 /* Multiple targets? */
880 if (!is_array($to)){
881 $to_targets= array($to);
882 } else {
883 $to_targets= $to;
884 }
886 /* Build target strings */
887 $target ="";
888 foreach($to_targets as $to){
889 $target.= "<target>$to</target>";
890 }
892 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
893 }
896 /* Allows simply appending a new DaemonEvent
897 */
898 public function append($event)
899 {
900 if(!($event instanceof DaemonEvent)){
901 return(FALSE);
902 }
904 $this->reset_error();
906 /* Add to queue if new
907 */
908 if($event->is_new()){
910 $request_answer = FALSE;
911 if($event->get_type() == SCHEDULED_EVENT){
912 $action = $event->get_schedule_action();
913 }elseif($event->get_type() == TRIGGERED_EVENT){
914 $action = $event->get_trigger_action();
915 }else{
916 trigger_error("Unknown type of queue event given.");
917 return(FALSE);
918 }
920 /* Get event informations, like targets..
921 */
922 $targets = $event->get_targets();
923 $data = $event->save();
925 /* Append an entry for each target
926 */
927 foreach($targets as $target){
928 $data['macaddress'] = $target;
929 $this->send_data($action,$target,$data,$request_answer);
931 if($this->is_error()){
932 return(FALSE);
933 }
934 }
935 return(TRUE);
936 }else{
938 /* Updated edited entry.
939 */
940 $id = $event->get_id();
941 $data = $event->save();
942 return($this->update_entries(array($id),$data));
943 }
945 return(FALSE);
946 }
949 /*! \brief Returns an array containing all queued entries.
950 @return Array All queued entries as an array.
951 */
952 public function _send($data, $answer_expected= FALSE)
953 {
954 $this->reset_error();
955 $ret = array();
957 if($this->connect()){
958 $this->o_sock->write($data);
959 if ($answer_expected){
960 $str = trim($this->o_sock->read());
962 /* Check if something went wrong while reading */
963 if($this->o_sock->is_error()){
964 $this->set_error($this->o_sock->get_error());
965 return($ret);
966 }
968 $entries = $this->xml_to_array($str);
969 if(isset($entries['XML']) && is_array($entries['XML'])){
970 $ret = $entries;
971 if(isset($entries['XML']['ERROR_STRING'])) {
972 $this->set_error($entries['XML']['ERROR_STRING']);
973 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
974 }else{
975 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
976 }
977 }
978 }else{
979 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
980 }
981 }
982 return($ret);
983 }
986 static function send($header, $to, $data= array(), $answer_expected = FALSE)
987 {
988 $xml_message= "";
990 /* Get communication object */
991 $d= new gosaSupportDaemon(TRUE,10);
993 /* Prepare data */
994 foreach ($data as $key => $value){
995 if(is_array($value)){
996 foreach($value as $sub_val){
997 $xml_message.= "<$key>$sub_value</$key>";
998 }
999 }else{
1000 $xml_message.= "<$key>$value</$key>";
1001 }
1002 }
1004 /* Multiple targets? */
1005 if (!is_array($to)){
1006 $to_targets= array($to);
1007 } else {
1008 $to_targets= $to;
1009 }
1011 /* Build target strings */
1012 $target ="";
1013 foreach($to_targets as $to){
1014 $target.= "<target>$to</target>";
1015 }
1017 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1018 }
1021 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1022 @param String $mac The mac address for which we want to remove all jobs.
1023 */
1024 function clean_queue_from_mac($mac)
1025 {
1026 global $config;
1028 /* First of all we have to check which jobs are startet
1029 * for $mac
1030 */
1031 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1033 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1035 $data = $this->_send($xml_msg,TRUE);
1036 if(is_array($data) && isset($data['XML'])){
1037 $already_aborted = FALSE;
1038 foreach($data['XML'] as $name => $entry){
1039 if(preg_match("/answer[0-9]*/i",$name)){
1040 $entry['STATUS'] = strtoupper($entry['STATUS']);
1041 switch($entry['STATUS']){
1043 case 'PROCESSING' :
1045 /* Send abort event, but only once
1046 */
1047 if($already_aborted){
1048 break;
1049 }elseif(class_available("DaemonEvent_faireboot")){
1050 $already_aborted = TRUE;
1051 $tmp = new DaemonEvent_faireboot($config);
1052 $tmp->add_targets(array($mac));
1053 $tmp->set_type(TRIGGERED_EVENT);
1054 if(!$this->append($tmp)){
1055 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1056 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1057 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1058 }else{
1059 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1060 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1061 }
1062 ;break;
1063 }else{
1064 /* Couldn't find abort event, just remove entry */
1065 }
1067 case 'WAITING':
1068 case 'ERROR':
1069 default :
1071 /* Simply remove entries from queue.
1072 * Failed or waiting events, can be removed without any trouble.
1073 */
1074 if(!$this->remove_entries(array($entry['ID']))){
1075 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1076 }
1077 ;break;
1078 }
1080 }
1081 }
1082 }
1083 }
1086 static function ping($target)
1087 {
1088 if (tests::is_mac($target)){
1089 /* Get communication object */
1090 $d= new gosaSupportDaemon(TRUE,0.5);
1091 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1092 return (count($answer) ? TRUE:FALSE);
1093 }
1095 return (FALSE);
1096 }
1098 }
1100 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1101 ?>