Code

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