Code

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