Code

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