Code

8270f9ffc2fb7abf841ae56f0b5d6a8e04139834
[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()", "Could not connect to server.", array(),$this->get_error());
80       }
81     }else{
82       $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
83     }
84     return($this->is_connected);
85   }
88   /*! \brief  Disconnect from gosa daemon.
89    */
90   public function disconnect()
91   {
92     $this->o_sock->close();
93     $this->is_connected = FALSE;
94   }
97   /*! \brief  Sets an error message, which can be returned with get_error().
98     @param  string  The Error message,
99    */
100   private function set_error($str)
101   {
102     $this->b_error = TRUE;
103     $this->s_error = $str;
104   }
107   /*! \brief  Sets an error message, which can be returned with get_error().
108     @param  string  The Error message,
109    */
110   private function reset_error()
111   {
112     $this->b_error = FALSE;
113     $this->s_error = "";
114   }
117   /*! \brief  Checks if an error occured.
118     @return boolean returns TRUE or FALSE, whether there is an error or not.
119    */
120   public function is_error()
121   {
122     return($this->b_error);
123   }
126   /*! \brief  Returns the last error. 
127     @return Returns the last error.
128    */
129   public function get_error()
130   {
131     $str = $this->s_error;
132     $str = preg_replace("/ /","&nbsp;",$str);
133     return($str);
134   }
137   public function FAI_get_packages($release,$attrs, $package="")
138   {
139     $this->reset_error();
140     $ret = array();
142     if(!is_array($attrs) || !count($attrs)){
143       trigger_error("Second parameter must be an array. With at least one attribute name.");
144       return($ret);
145     }
146  
147     $attr = "<select>package</select>";
148     foreach($attrs as $at){
149 #      $attr.= "<select>".trim($at)."</select>";
150     }
151  
152     if(empty($package)){
153       $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
154         $attr.
155         "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where><limit>10</limit></xml>";
156     }else{
157       $xml_msg = 
158         "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
159         $attr.
160           "<where><clause>".
161             "<connector>AND</connector>".
162             "<phrase><distribution>".$release."</distribution></phrase>".
163             "<phrase><package>".$package."</package></phrase>".
164           "</clause></where>".
165         "<limit>10</limit></xml>";
166     }
168     if($this->connect()){
169       $this->o_sock->write($xml_msg);
170       $str = trim($this->o_sock->read());
172       /* Check if something went wrong while reading */
173       if($this->o_sock->is_error()){
174         $this->set_error($this->o_sock->get_error());
175         return($ret);
176       }
178       $entries = $this->xml_to_array($str);
179       if(isset($entries['XML']) && is_array($entries['XML'])){
181         /* Check if returned values represent a valid answer */
182         if(isset($entries['XML'])){
183           if(isset($entries['XML']['ERROR_STRING'])) {
184             $this->set_error($entries['XML']['ERROR_STRING']);
185             new log("debug","GOsa-si",
186                 get_class($this)."::".__FUNCTION__, array(),
187                 "FAILED error was ".$this->get_error());
188             return($ret);
189           }
191           /* Unset header tags */
192           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
193             if(isset($entries['XML'][$type])){
194               unset($entries['XML'][$type]);
195             }
196           }
197           $ret = $entries['XML'];
198         }
199       }
200     }
201     return($ret);
203     
204   }
207   public function FAI_get_server($name = "")
208   {
209     $this->reset_error();
211     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
212     $ret = array();
213     if($this->connect()){
214       $this->o_sock->write($xml_msg);
215       $str = trim($this->o_sock->read());
217       /* Check if something went wrong while reading */
218       if($this->o_sock->is_error()){
219         $this->set_error($this->o_sock->get_error());
220         return($ret);
221       }
223       $entries = $this->xml_to_array($str);
224       if(isset($entries['XML']) && is_array($entries['XML'])){
226         /* Check if returned values represent a valid answer */
227         if(isset($entries['XML'])){
228           if(isset($entries['XML']['ERROR_STRING'])) {
229             $this->set_error($entries['XML']['ERROR_STRING']);
230             new log("debug","GOsa-si", 
231               get_class($this)."::".__FUNCTION__, array(),
232               "FAILED error was ".$this->get_error());
233             return($ret);
234           }
236           /* Unset header tags */
237           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
238             if(isset($entries['XML'][$type])){
239               unset($entries['XML'][$type]);
240             }
241           }
242           $ret = $entries['XML']; 
243         }
244       }
245     }
246     return($ret);
247   }
250   public function FAI_get_classes($name)
251   {
252     $this->reset_error();
253     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
254                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
255     $ret = array();
256     if($this->connect()){
257       $this->o_sock->write($xml_msg);
258       $str = trim($this->o_sock->read());
260       /* Check if something went wrong while reading */
261       if($this->o_sock->is_error()){
262         $this->set_error($this->o_sock->get_error());
263         return($ret);
264       }
266       $entries = $this->xml_to_array($str);
267       if(isset($entries['XML']) && is_array($entries['XML'])){
269         /* Check if returned values represent a valid answer */
270         if(isset($entries['XML'])){
271           if(isset($entries['XML']['ERROR_STRING'])) {
272             $this->set_error($entries['XML']['ERROR_STRING']);
273             new log("debug","GOsa-si", 
274               get_class($this)."::".__FUNCTION__, array($name),
275               "FAILED error was ".$this->get_error());
276             return($ret);
277           }
279           /* Unset header tags */
280           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
281             if(isset($entries['XML'][$type])){
282               unset($entries['XML'][$type]);
283             }
284           }
285           $ret = $entries['XML']; 
286         }
287       }
288     }
289     return($ret);
290   }
293   /*! \brief  Returns an array containing all queued entries.
294     @return Array All queued entries as an array.
295    */
296   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
297   {
298     $this->reset_error();
299     $ret = array();
301     $tags = "";
302     foreach($event_types as $type){
303       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
304     }
305     if(count($event_types) > 1){
306       $tags = "<connector>or</connector>".$tags;
307     }
308     if(count($event_types)){
309       $tags = "<where><clause>".$tags."</clause></where>";
310     }
312     $xml_msg = 
313 "<xml>
314       <header>gosa_query_jobdb</header>
315       <target>GOSA</target>
316       <source>GOSA</source>
317       ".$tags."
319       <orderby>".$sort."</orderby>";
320 if($from != -1 && $to != -1){
321 $xml_msg.= "
322       <limit>
323        <from>".$from."</from>
324        <to>".$to."</to>
325       </limit>";
327 $xml_msg.= "
328       </xml>";
330     if($this->connect()){
331       $this->o_sock->write($xml_msg);
332       $str = trim($this->o_sock->read());
334       /* Check if something went wrong while reading */
335       if($this->o_sock->is_error()){
336         $this->set_error($this->o_sock->get_error());
337         return($ret);
338       }
340       $entries = $this->xml_to_array($str);
341       if(isset($entries['XML']) && is_array($entries['XML'])){
343         /* Check if returned values represent a valid answer */
344         if(isset($entries['XML'])){
345           
346           /* Unset header tags */
347           foreach(array("HEADER","SOURCE","TARGET") as $type){
348             unset($entries['XML'][$type]);
349           }
350           $ret = $entries['XML']; 
351         }
352       }
353     }
354     
355     /* Remove session ID. No one is interested in this... */
356     unset($ret['SESSION_ID']);
358     return($ret);
359   }
362   /*! \brief  Checks if the given ids are used queue ids.
363     @param  Array   The ids we want to check..
364     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
365    */
366   public function ids_exist($ids)
367   {
368     if(!is_array($ids)){
369       trigger_error("Requires an array as parameter.");
370       return;
371     }
372     $this->reset_error();
374     $ret = array();
376     $xml_msg = "<xml>
377       <header>gosa_query_jobdb</header>
378       <target>GOSA</target>
379       <source>GOSA</source>
380       <where>
381       <clause>
382       <connector>or</connector>";
383     foreach($ids as $id){
384       $xml_msg .= "<phrase>
385         <operator>eq</operator>
386         <id>".$id."</id>
387         </phrase>";
388     }
389     $xml_msg .= "</clause>
390       </where>
391       </xml>";
393     if($this->connect()){
394       $this->o_sock->write($xml_msg);
395       $str = trim($this->o_sock->read());
397       /* Check if something went wrong while reading */
398       if($this->o_sock->is_error()){
399         $this->set_error($this->o_sock->get_error());
400         return($ret);
401       }
403       $entries = $this->xml_to_array($str);
404       if(isset($entries['XML']) && is_array($entries['XML'])){
405         foreach($entries['XML'] as $entry){
406           if(isset($entry['ID'])){
407             $ret[] = $entry['ID'];
408           }
409         }
410       }
411     }
412     return($ret);
413   }
416   /*! \brief  Returns an entry containing all requested ids.
417     @param  Array   The IDs of the entries we want to return.
418     @return Array   Of the requested entries. 
419    */
420   public function get_entries_by_mac($macs)
421   {
422     if(!is_array($macs)){
423       trigger_error("Requires an array as parameter.");
424       return;
425     }
426     $this->reset_error();
428     $ret = array();
430     $xml_msg = "<xml>
431       <header>gosa_query_jobdb</header>
432       <target>GOSA</target>
433       <source>GOSA</source>
434       <where>
435       <clause>
436       <connector>or</connector>";
437     foreach($macs as $mac){
438       $xml_msg .= "<phrase>
439         <operator>eq</operator>
440         <macaddress>".$mac."</macaddress>
441         </phrase>";
442     }
443     $xml_msg .= "</clause>
444       </where>
445       </xml>";
447     if($this->connect()){
448       $this->o_sock->write($xml_msg);
449       $str = trim($this->o_sock->read());
451       /* Check if something went wrong while reading */
452       if($this->o_sock->is_error()){
453         $this->set_error($this->o_sock->get_error());
454         return($ret);
455       }
457       $entries = $this->xml_to_array($str); 
458       if(isset($entries['XML'])){
459         foreach($entries['XML'] as $name => $entry){
460           if(preg_match("/^ANSWER[0-9]*$/",$name)){
461             $ret[$name] = $entry;
462           }
463         }
464       }
465     }
466     return($ret);
467   }
470   /*! \brief  Returns an entry containing all requested ids.
471     @param  Array   The IDs of the entries we want to return.
472     @return Array   Of the requested entries. 
473    */
474   public function get_entries_by_id($ids)
475   {
476     if(!is_array($ids)){
477       trigger_error("Requires an array as parameter.");
478       return;
479     }
480     $this->reset_error();
482     $ret = array();
484     $xml_msg = "<xml>
485       <header>gosa_query_jobdb</header>
486       <target>GOSA</target>
487       <source>GOSA</source>
488       <where>
489       <clause>
490       <connector>or</connector>";
491     foreach($ids as $id){
492       $xml_msg .= "<phrase>
493         <operator>eq</operator>
494         <id>".$id."</id>
495         </phrase>";
496     }
497     $xml_msg .= "</clause>
498       </where>
499       </xml>";
501     if($this->connect()){
502       $this->o_sock->write($xml_msg);
503       $str = trim($this->o_sock->read());
505       /* Check if something went wrong while reading */
506       if($this->o_sock->is_error()){
507         $this->set_error($this->o_sock->get_error());
508         return($ret);
509       }
511       $entries = $this->xml_to_array($str); 
512       if(isset($entries['XML'])){
513         foreach($entries['XML'] as $name => $entry){
514           if(preg_match("/^ANSWER[0-9]*$/",$name)){
515             $ret[$name] = $entry;
516           }
517         }
518       }
519     }
520     return($ret);
521   }
524   /*! \brief  Checks if the given id is in use.
525     @param  Integer The ID of the entry.
526     @return Boolean TRUE if entry exists. 
527    */
528   public function id_exists($id)
529   {
530     if(!is_numeric($id)){
531       trigger_error("Requires an integer as parameter.");
532       return;
533     }
535     $this->reset_error();
537     $xml_msg = "<xml>
538       <header>gosa_query_jobdb</header>
539       <target>GOSA</target>
540       <source>GOSA</source>
541       <where>
542       <clause>
543       <phrase>
544       <operator>eq</operator>
545       <id>".$id."</id>
546       </phrase>
547       </clause>
548       </where>
549       </xml>";
551     if($this->connect()){
552       $this->o_sock->write($xml_msg);
553       $str = trim($this->o_sock->read());
555       /* Check if something went wrong while reading */
556       if($this->o_sock->is_error()){
557         $this->set_error($this->o_sock->get_error());
558         return(FALSE);
559       }
561       $entries = $this->xml_to_array($str); 
562       if( isset($entries['XML']['HEADER']) && 
563           $entries['XML']['HEADER']=="answer" && 
564           isset($entries['XML']['ANSWER1'])){
565         return(TRUE);
566       }
567     }
568     return(FALSE);
569   }
572   /*! \brief  Returns an entry from the gosaSupportQueue
573     @param  Integer The ID of the entry we want to return.
574     @return Array   Of the requested entry. 
575    */
576   public function get_entry_by_id($id)
577   {
578     if(!is_numeric($id)){
579       trigger_error("Requires an integer as parameter.");
580       return;
581     }
582     $this->reset_error();
583   
584     $ret = array();
585     $xml_msg = "<xml>
586       <header>gosa_query_jobdb</header>
587       <target>GOSA</target>
588       <source>GOSA</source>
589       <where>
590       <clause>
591       <phrase>
592       <operator>eq</operator>
593       <id>".$id."</id>
594       </phrase>
595       </clause>
596       </where>
597       </xml>";
598     if($this->connect()){
599       $this->o_sock->write($xml_msg);
600       $str = trim($this->o_sock->read());
602       /* Check if something went wrong while reading */
603       if($this->o_sock->is_error()){
604         $this->set_error($this->o_sock->get_error());
605         return($ret);
606       }
608       $entries = $this->xml_to_array($str); 
609       if( isset($entries['XML']['HEADER']) &&
610           $entries['XML']['HEADER']=="answer" &&
611           isset($entries['XML']['ANSWER1'])){
612         $ret = $entries['XML']['ANSWER1'];
613       }
614     }
615     return($ret);
616   }
619   /*! \brief  Removes a set of entries from the GOsa support queue. 
620     @param  Array The IDs to remove.
621     @return Boolean True on success.
622    */
623   public function remove_entries($ids)
624   {
625     if(!is_array($ids)){
626       trigger_error("Requires an array as parameter.");
627       return;
628     }
630     $this->reset_error();
632     $ret = array();
634     $xml_msg = "<xml>
635       <header>gosa_delete_jobdb_entry</header>
636       <target>GOSA</target>
637       <source>GOSA</source>
638       <where>
639       <clause>
640       <connector>or</connector>";
641     foreach($ids as $id){
642       $xml_msg .= "<phrase>
643         <operator>eq</operator>
644         <id>".$id."</id>
645         </phrase>";
646     }
647     $xml_msg .= "</clause>
648       </where>
649       </xml>";
651     if($this->connect()){
652       $this->o_sock->write($xml_msg);
653       $str = $this->o_sock->read();
655       /* Check if something went wrong while reading */
656       if($this->o_sock->is_error()){
657         $this->set_error($this->o_sock->get_error());
658         return($ret);
659       }
661       $entries = $this->xml_to_array($str);
662       if(isset($entries['XML']) || isset($entries['COUNT'])){
663         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
664         return(TRUE);
665       }else{
666         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
667       }
668     }
669     return(FALSE);
670   }
674   /*! \brief  Removes an entry from the GOsa support queue. 
675     @param  Integer The ID of the entry we want to remove.
676     @return Boolean True on success.
677    */
678   public function remove_entry($id)
679   {
680     return($this->remove_entries(array($id)));
681   }
684   /*! \brief  Parses the given xml string into an array 
685     @param  String XML string  
686     @return Array Returns an array containing the xml structure. 
687    */
688   private function xml_to_array($xml)
689   {
690     $params = array();
691     $level = array();
692     $parser  = xml_parser_create_ns();
693     xml_parse_into_struct($parser, $xml, $vals, $index);
695     $err_id = xml_get_error_code($parser);
696     if($err_id){
697       xml_parser_free($parser);
698     }else{
699       xml_parser_free($parser);
701       foreach ($vals as $xml_elem) {
702         if ($xml_elem['type'] == 'open') {
703           if (array_key_exists('attributes',$xml_elem)) {
704             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
705           } else {
706             $level[$xml_elem['level']] = $xml_elem['tag'];
707           }
708         }
709         if ($xml_elem['type'] == 'complete') {
710           $start_level = 1;
711           $php_stmt = '$params';
712           while($start_level < $xml_elem['level']) {
713             $php_stmt .= '[$level['.$start_level.']]';
714             $start_level++;
715           }
716           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
717           @eval($php_stmt);
718         }
719       }
720     }
722     if(!isset($params['XML'])){
723       if (!array_key_exists('XML', $params)){
724         $this->set_error(_("Cannot not parse XML!"));
725       }
726       $params = array("COUNT" => 0);
727     }
729     return($params); 
730   }
733   /*! \brief  Updates an entry with a set of new values, 
734     @param  Integer The ID of the entry, we want to update.
735     @param  Array   The variables to update.   
736     @return Boolean Returns TRUE on success. 
737    */
738   public function update_entries($ids,$data)
739   {
740     $this->reset_error();
741     if(!is_array($ids)){
742       trigger_error("Requires an array as first parameter.");
743       return;
744     }
746     if(!is_array($data)){
747       trigger_error("Requires an array as second parameter.");
748       return;
749     }
751     $attr = "";
752     foreach($data as $key => $value){
753       if(is_array($value)){
754         foreach($value as $sub_value){
755           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
756         }
757       }else{
758         $attr.= "<$key>".strtolower($value)."</$key>\n";
759       }
760     }
762     $xml_msg = "<xml>
763       <header>gosa_update_status_jobdb_entry</header>
764       <target>GOSA</target>
765       <source>GOSA</source>
766       <where>
767       <clause>
768       <connector>or</connector>";
769     foreach($ids as $id){
770       $xml_msg .= "<phrase>
771         <operator>eq</operator>
772         <id>".$id."</id>
773         </phrase>";
774     }
775     $xml_msg .= "</clause>
776       </where>
777       <update>
778       ".$attr." 
779       </update>
780       </xml>";
782     if($this->connect()){
784       $this->o_sock->write($xml_msg);
785       $str      = trim($this->o_sock->read());
787       /* Check if something went wrong while reading */
788       if($this->o_sock->is_error()){
789         $this->set_error($this->o_sock->get_error());
790         return(FALSE);
791       }
793       $entries = $this->xml_to_array($str);
794       if(isset($entries['XML'])){
795         if(isset($entries['XML']['ERROR_STRING'])) {
796           $this->set_error($entries['XML']['ERROR_STRING']);
797           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
798           return(FALSE);
799         }
800         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
801         return(TRUE);
802       }
803     }
804     return(FALSE);
805   }
808   /*! \brief  Returns the number of currently queued objects.
809       @return Integer  
810    */
811   public function number_of_queued_entries()
812   {
813     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
814     $this->connect();
815     if($this->connect()){
816       $this->o_sock->write($xml_msg);
817       $str     = trim($this->o_sock->read());
819       /* Check if something went wrong while reading */
820       if($this->o_sock->is_error()){
821         $this->set_error($this->o_sock->get_error());
822         return(0);
823       }
825       $entries = $this->xml_to_array($str);
826       if(isset($entries['XML'])){
827         return($entries['XML']['COUNT']);
828       }
829     }
830     return(-1);
831   } 
834   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
835   {
836     $xml_message= "";
838     /* Prepare data */
839     foreach ($data as $key => $value){
840       if(is_array($value)){
841         foreach($value as $sub_value){
842           $xml_message.= "<$key>$sub_value</$key>";
843         }
844       }else{
845         $xml_message.= "<$key>$value</$key>";
846       }
847     }
849     /* Multiple targets? */
850     if (!is_array($to)){
851       $to_targets= array($to);
852     } else {
853       $to_targets= $to;
854     }
856     /* Build target strings */
857     $target ="";
858     foreach($to_targets as $to){
859       $target.= "<target>$to</target>";
860     }
862     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
863   }
866   /* Allows simply appending a new DaemonEvent 
867    */
868   public function append($event)
869   {
870     if(!($event instanceof DaemonEvent)){
871       return(FALSE);
872     }
873   
874     $this->reset_error();
876     /* Add to queue if new 
877      */
878     if($event->is_new()){
880       $request_answer = FALSE;
881       if($event->get_type() == SCHEDULED_EVENT){
882         $action = $event->get_schedule_action();
883       }elseif($event->get_type() == TRIGGERED_EVENT){
884         $action = $event->get_trigger_action();
885       }else{
886         trigger_error("Unknown type of queue event given.");
887         return(FALSE);
888       }
890       /* Get event informations, like targets..
891        */
892       $targets    = $event->get_targets();
893       $data       = $event->save();
895       /* Append an entry for each target 
896        */
897       foreach($targets as $target){
898         $data['macaddress'] = $target;
899         $this->send_data($action,$target,$data,$request_answer);
901         if($this->is_error()){
902           return(FALSE);
903         }
904       }
905       return(TRUE);
906     }else{
908       /* Updated edited entry.
909        */
910       $id                 = $event->get_id();
911       $data               = $event->save();
912       return($this->update_entries(array($id),$data));
913     }
915     return(FALSE);
916   }
919 /*! \brief  Returns an array containing all queued entries.
920     @return Array All queued entries as an array.
921    */
922   public function _send($data, $answer_expected= FALSE)
923   {
924     $this->reset_error();
925     $ret = array();
927     if($this->connect()){
928       $this->o_sock->write($data);
929       if ($answer_expected){
930         $str = trim($this->o_sock->read());
932         /* Check if something went wrong while reading */
933         if($this->o_sock->is_error()){
934           $this->set_error($this->o_sock->get_error());
935           return($ret);
936         }
938         $entries = $this->xml_to_array($str);
939         if(isset($entries['XML']) && is_array($entries['XML'])){
940           $ret = $entries;
941           if(isset($entries['XML']['ERROR_STRING'])) {
942             $this->set_error($entries['XML']['ERROR_STRING']);
943             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
944           }else{
945             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
946           }
947         }
948       }else{
949         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
950       }
951     }
952     return($ret);
953   }
956   static function send($header, $to, $data= array(), $answer_expected = FALSE)
957   {
958     $xml_message= "";
960     /* Get communication object */
961     $d= new gosaSupportDaemon(TRUE,10);
963     /* Prepare data */
964     foreach ($data as $key => $value){
965       if(is_array($value)){
966         foreach($value as $sub_val){
967           $xml_message.= "<$key>$sub_value</$key>";
968         }
969       }else{
970         $xml_message.= "<$key>$value</$key>";
971       }
972     }
974     /* Multiple targets? */
975     if (!is_array($to)){
976       $to_targets= array($to);
977     } else {
978       $to_targets= $to;
979     }
981     /* Build target strings */
982     $target ="";
983     foreach($to_targets as $to){
984       $target.= "<target>$to</target>";
985     }
987     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
988   }
991   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
992       @param  String  $mac  The mac address for which we want to remove all jobs.      
993    */
994   function clean_queue_from_mac($mac)
995   {
996     global $config;
998     /* First of all we have to check which jobs are startet 
999      *  for $mac 
1000      */
1001     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1002     
1003     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1004  
1005     $data = $this->_send($xml_msg,TRUE);
1006     if(is_array($data) && isset($data['XML'])){
1007       $already_aborted = FALSE;
1008       foreach($data['XML']  as $name => $entry){
1009         if(preg_match("/answer[0-9]*/i",$name)){
1010           $entry['STATUS'] = strtoupper($entry['STATUS']);
1011           switch($entry['STATUS']){
1013             case 'PROCESSING' :
1015               /* Send abort event, but only once 
1016                */
1017               if($already_aborted){
1018                 break;
1019               }elseif(class_available("DaemonEvent_faireboot")){
1020                 $already_aborted = TRUE;
1021                 $tmp = new DaemonEvent_faireboot($config);
1022                 $tmp->add_targets(array($mac));
1023                 $tmp->set_type(TRIGGERED_EVENT);
1024                 if(!$this->append($tmp)){
1025                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1026                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1027                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1028                 }else{
1029                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1030                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1031                 }
1032                 ;break;
1033               }else{
1034                 /* Couldn't find abort event, just remove entry */
1035               }
1037             case 'WAITING':
1038             case 'ERROR':
1039             default :
1040             
1041               /* Simply remove entries from queue. 
1042                *  Failed or waiting events, can be removed without any trouble.
1043                */ 
1044               if(!$this->remove_entries(array($entry['ID']))){
1045                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1046               }
1047               ;break;
1048           }
1049     
1050         }
1051       }
1052     }
1053   }
1056 static function ping($target)
1058   if (tests::is_mac($target)){
1059     /* Get communication object */
1060     $d= new gosaSupportDaemon(TRUE,0.5);
1061     $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1062     return (count($answer) ? TRUE:FALSE);
1063   }
1065   return (FALSE);
1070 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1071 ?>