Code

dbdd47a7591a33f362159a4f9b2953edf38fbee7
[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_kernels($release)
138   {
139     $xml_msg = "<xml><header>gosa_get_available_kernel</header><source>GOSA</source><target>GOSA</target><release>halut</release></xml>";
141     if($this->connect()){
142       $this->o_sock->write($xml_msg);
143       $str = trim($this->o_sock->read());
145       /* Check if something went wrong while reading */
146       if($this->o_sock->is_error()){
147         $this->set_error($this->o_sock->get_error());
148         return($ret);
149       }
151       $entries = $this->xml_to_array($str);
152       if(isset($entries['XML']) && is_array($entries['XML'])){
154         /* Check if returned values represent a valid answer */
155         if(isset($entries['XML'])){
156           if(isset($entries['XML']['ERROR_STRING'])) {
157             $this->set_error($entries['XML']['ERROR_STRING']);
158             new log("debug","GOsa-si",
159                 get_class($this)."::".__FUNCTION__, array(),
160                 "FAILED error was ".$this->get_error());
161             return($ret);
162           }
164           /* Unset header tags */
165           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
166             if(isset($entries['XML'][$type])){
167               unset($entries['XML'][$type]);
168             }
169           }
170           $ret = $entries['XML'];
171         }
172       }
173     }
174     return($ret);
175   }
178   public function FAI_get_package_sections($release)
179   {
180     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
181       "<select>distinct section</select>".
182       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
184     if($this->connect()){
185       $this->o_sock->write($xml_msg);
186       $str = trim($this->o_sock->read());
188       /* Check if something went wrong while reading */
189       if($this->o_sock->is_error()){
190         $this->set_error($this->o_sock->get_error());
191         return($ret);
192       }
194       $entries = $this->xml_to_array($str);
195       if(isset($entries['XML']) && is_array($entries['XML'])){
197         /* Check if returned values represent a valid answer */
198         if(isset($entries['XML'])){
199           if(isset($entries['XML']['ERROR_STRING'])) {
200             $this->set_error($entries['XML']['ERROR_STRING']);
201             new log("debug","GOsa-si",
202                 get_class($this)."::".__FUNCTION__, array(),
203                 "FAILED error was ".$this->get_error());
204             return($ret);
205           }
207           /* Unset header tags */
208           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
209             if(isset($entries['XML'][$type])){
210               unset($entries['XML'][$type]);
211             }
212           }
213           $ret = $entries['XML'];
214         }
215       }
216     }
217     return($ret);
218   }
221   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
222   {
223     $this->reset_error();
224     $ret = array();
226     /* Check Parameter */
227     if(!is_array($attrs) || !count($attrs)){
228       trigger_error("Second parameter must be an array. With at least one attribute name.");
229       return($ret);
230     }
232     /* Check Parameter */
233     if(!is_array($package)){
234       trigger_error("Third parameter must be an array. With at least one attribute name.");
235       return($ret);
236     }
238     /* Create list of attributes to fetch */
239     $attr = ""; 
240     foreach($attrs as $at){
241       $attr.= "<select>".$at."</select>";
242     }
244     /* If no package is given, search for all */
245     if(!count($package)) $package = array("%");
247     /* Create limit tag */
248     if($from == -1){
249       $limit =""; 
250     }else{
251       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
252     }
254     /* Create list of attributes to fetch */
255     $pkgs = ""; 
256     foreach($package as $pkg){
257       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
258     }
260     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
261       $attr.
262       "<where>
263       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
264       <clause><connector>OR</connector>
265       ".$pkgs."
266       </clause>
267       </where>".
268       $limit.
269       "</xml>";
271     if($this->connect()){
272       $this->o_sock->write($xml_msg);
273       $str = trim($this->o_sock->read());
275       /* Check if something went wrong while reading */
276       if($this->o_sock->is_error()){
277         $this->set_error($this->o_sock->get_error());
278         return($ret);
279       }
281       $entries = $this->xml_to_array($str);
282       if(isset($entries['XML']) && is_array($entries['XML'])){
284         /* Check if returned values represent a valid answer */
285         if(isset($entries['XML'])){
286           if(isset($entries['XML']['ERROR_STRING'])) {
287             $this->set_error($entries['XML']['ERROR_STRING']);
288             new log("debug","GOsa-si",
289                 get_class($this)."::".__FUNCTION__, array(),
290                 "FAILED error was ".$this->get_error());
291             return($ret);
292           }
294           /* Unset header tags */
295           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
296             if(isset($entries['XML'][$type])){
297               unset($entries['XML'][$type]);
298             }
299           }
300           $ret = $entries['XML'];
301         }
302       }
303     }
304     return($ret);
306     
307   }
310   public function FAI_get_server($name = "")
311   {
312     $this->reset_error();
314     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
315     $ret = array();
316     if($this->connect()){
317       $this->o_sock->write($xml_msg);
318       $str = trim($this->o_sock->read());
320       /* Check if something went wrong while reading */
321       if($this->o_sock->is_error()){
322         $this->set_error($this->o_sock->get_error());
323         return($ret);
324       }
326       $entries = $this->xml_to_array($str);
327       if(isset($entries['XML']) && is_array($entries['XML'])){
329         /* Check if returned values represent a valid answer */
330         if(isset($entries['XML'])){
331           if(isset($entries['XML']['ERROR_STRING'])) {
332             $this->set_error($entries['XML']['ERROR_STRING']);
333             new log("debug","GOsa-si", 
334               get_class($this)."::".__FUNCTION__, array(),
335               "FAILED error was ".$this->get_error());
336             return($ret);
337           }
339           /* Unset header tags */
340           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
341             if(isset($entries['XML'][$type])){
342               unset($entries['XML'][$type]);
343             }
344           }
345           $ret = $entries['XML']; 
346         }
347       }
348     }
349     return($ret);
350   }
353   public function FAI_get_classes($name)
354   {
355     $this->reset_error();
356     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
357                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
358     $ret = array();
359     if($this->connect()){
360       $this->o_sock->write($xml_msg);
361       $str = trim($this->o_sock->read());
363       /* Check if something went wrong while reading */
364       if($this->o_sock->is_error()){
365         $this->set_error($this->o_sock->get_error());
366         return($ret);
367       }
369       $entries = $this->xml_to_array($str);
370       if(isset($entries['XML']) && is_array($entries['XML'])){
372         /* Check if returned values represent a valid answer */
373         if(isset($entries['XML'])){
374           if(isset($entries['XML']['ERROR_STRING'])) {
375             $this->set_error($entries['XML']['ERROR_STRING']);
376             new log("debug","GOsa-si", 
377               get_class($this)."::".__FUNCTION__, array($name),
378               "FAILED error was ".$this->get_error());
379             return($ret);
380           }
382           /* Unset header tags */
383           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
384             if(isset($entries['XML'][$type])){
385               unset($entries['XML'][$type]);
386             }
387           }
388           $ret = $entries['XML']; 
389         }
390       }
391     }
392     return($ret);
393   }
396   /*! \brief  Returns an array containing all queued entries.
397     @return Array All queued entries as an array.
398    */
399   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
400   {
401     $this->reset_error();
402     $ret = array();
404     $tags = "";
405     foreach($event_types as $type){
406       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
407     }
408     if(count($event_types) > 1){
409       $tags = "<connector>or</connector>".$tags;
410     }
411     if(count($event_types)){
412       $tags = "<where><clause>".$tags."</clause></where>";
413     }
415     $xml_msg = 
416 "<xml>
417       <header>gosa_query_jobdb</header>
418       <target>GOSA</target>
419       <source>GOSA</source>
420       ".$tags."
422       <orderby>".$sort."</orderby>";
423 if($from != -1 && $to != -1){
424 $xml_msg.= "
425       <limit>
426        <from>".$from."</from>
427        <to>".$to."</to>
428       </limit>";
430 $xml_msg.= "
431       </xml>";
433     if($this->connect()){
434       $this->o_sock->write($xml_msg);
435       $str = trim($this->o_sock->read());
437       /* Check if something went wrong while reading */
438       if($this->o_sock->is_error()){
439         $this->set_error($this->o_sock->get_error());
440         return($ret);
441       }
443       $entries = $this->xml_to_array($str);
444       if(isset($entries['XML']) && is_array($entries['XML'])){
446         /* Check if returned values represent a valid answer */
447         if(isset($entries['XML'])){
448           
449           /* Unset header tags */
450           foreach(array("HEADER","SOURCE","TARGET") as $type){
451             unset($entries['XML'][$type]);
452           }
453           $ret = $entries['XML']; 
454         }
455       }
456     }
457     
458     /* Remove session ID. No one is interested in this... */
459     unset($ret['SESSION_ID']);
461     return($ret);
462   }
465   /*! \brief  Checks if the given ids are used queue ids.
466     @param  Array   The ids we want to check..
467     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
468    */
469   public function ids_exist($ids)
470   {
471     if(!is_array($ids)){
472       trigger_error("Requires an array as parameter.");
473       return;
474     }
475     $this->reset_error();
477     $ret = array();
479     $xml_msg = "<xml>
480       <header>gosa_query_jobdb</header>
481       <target>GOSA</target>
482       <source>GOSA</source>
483       <where>
484       <clause>
485       <connector>or</connector>";
486     foreach($ids as $id){
487       $xml_msg .= "<phrase>
488         <operator>eq</operator>
489         <id>".$id."</id>
490         </phrase>";
491     }
492     $xml_msg .= "</clause>
493       </where>
494       </xml>";
496     if($this->connect()){
497       $this->o_sock->write($xml_msg);
498       $str = trim($this->o_sock->read());
500       /* Check if something went wrong while reading */
501       if($this->o_sock->is_error()){
502         $this->set_error($this->o_sock->get_error());
503         return($ret);
504       }
506       $entries = $this->xml_to_array($str);
507       if(isset($entries['XML']) && is_array($entries['XML'])){
508         foreach($entries['XML'] as $entry){
509           if(isset($entry['ID'])){
510             $ret[] = $entry['ID'];
511           }
512         }
513       }
514     }
515     return($ret);
516   }
519   /*! \brief  Returns an entry containing all requested ids.
520     @param  Array   The IDs of the entries we want to return.
521     @return Array   Of the requested entries. 
522    */
523   public function get_entries_by_mac($macs)
524   {
525     if(!is_array($macs)){
526       trigger_error("Requires an array as parameter.");
527       return;
528     }
529     $this->reset_error();
531     $ret = array();
533     $xml_msg = "<xml>
534       <header>gosa_query_jobdb</header>
535       <target>GOSA</target>
536       <source>GOSA</source>
537       <where>
538       <clause>
539       <connector>or</connector>";
540     foreach($macs as $mac){
541       $xml_msg .= "<phrase>
542         <operator>eq</operator>
543         <macaddress>".$mac."</macaddress>
544         </phrase>";
545     }
546     $xml_msg .= "</clause>
547       </where>
548       </xml>";
550     if($this->connect()){
551       $this->o_sock->write($xml_msg);
552       $str = trim($this->o_sock->read());
554       /* Check if something went wrong while reading */
555       if($this->o_sock->is_error()){
556         $this->set_error($this->o_sock->get_error());
557         return($ret);
558       }
560       $entries = $this->xml_to_array($str); 
561       if(isset($entries['XML'])){
562         foreach($entries['XML'] as $name => $entry){
563           if(preg_match("/^ANSWER[0-9]*$/",$name)){
564             $ret[$name] = $entry;
565           }
566         }
567       }
568     }
569     return($ret);
570   }
573   /*! \brief  Returns an entry containing all requested ids.
574     @param  Array   The IDs of the entries we want to return.
575     @return Array   Of the requested entries. 
576    */
577   public function get_entries_by_id($ids)
578   {
579     if(!is_array($ids)){
580       trigger_error("Requires an array as parameter.");
581       return;
582     }
583     $this->reset_error();
585     $ret = array();
587     $xml_msg = "<xml>
588       <header>gosa_query_jobdb</header>
589       <target>GOSA</target>
590       <source>GOSA</source>
591       <where>
592       <clause>
593       <connector>or</connector>";
594     foreach($ids as $id){
595       $xml_msg .= "<phrase>
596         <operator>eq</operator>
597         <id>".$id."</id>
598         </phrase>";
599     }
600     $xml_msg .= "</clause>
601       </where>
602       </xml>";
604     if($this->connect()){
605       $this->o_sock->write($xml_msg);
606       $str = trim($this->o_sock->read());
608       /* Check if something went wrong while reading */
609       if($this->o_sock->is_error()){
610         $this->set_error($this->o_sock->get_error());
611         return($ret);
612       }
614       $entries = $this->xml_to_array($str); 
615       if(isset($entries['XML'])){
616         foreach($entries['XML'] as $name => $entry){
617           if(preg_match("/^ANSWER[0-9]*$/",$name)){
618             $ret[$name] = $entry;
619           }
620         }
621       }
622     }
623     return($ret);
624   }
627   /*! \brief  Checks if the given id is in use.
628     @param  Integer The ID of the entry.
629     @return Boolean TRUE if entry exists. 
630    */
631   public function id_exists($id)
632   {
633     if(!is_numeric($id)){
634       trigger_error("Requires an integer as parameter.");
635       return;
636     }
638     $this->reset_error();
640     $xml_msg = "<xml>
641       <header>gosa_query_jobdb</header>
642       <target>GOSA</target>
643       <source>GOSA</source>
644       <where>
645       <clause>
646       <phrase>
647       <operator>eq</operator>
648       <id>".$id."</id>
649       </phrase>
650       </clause>
651       </where>
652       </xml>";
654     if($this->connect()){
655       $this->o_sock->write($xml_msg);
656       $str = trim($this->o_sock->read());
658       /* Check if something went wrong while reading */
659       if($this->o_sock->is_error()){
660         $this->set_error($this->o_sock->get_error());
661         return(FALSE);
662       }
664       $entries = $this->xml_to_array($str); 
665       if( isset($entries['XML']['HEADER']) && 
666           $entries['XML']['HEADER']=="answer" && 
667           isset($entries['XML']['ANSWER1'])){
668         return(TRUE);
669       }
670     }
671     return(FALSE);
672   }
675   /*! \brief  Returns an entry from the gosaSupportQueue
676     @param  Integer The ID of the entry we want to return.
677     @return Array   Of the requested entry. 
678    */
679   public function get_entry_by_id($id)
680   {
681     if(!is_numeric($id)){
682       trigger_error("Requires an integer as parameter.");
683       return;
684     }
685     $this->reset_error();
686   
687     $ret = array();
688     $xml_msg = "<xml>
689       <header>gosa_query_jobdb</header>
690       <target>GOSA</target>
691       <source>GOSA</source>
692       <where>
693       <clause>
694       <phrase>
695       <operator>eq</operator>
696       <id>".$id."</id>
697       </phrase>
698       </clause>
699       </where>
700       </xml>";
701     if($this->connect()){
702       $this->o_sock->write($xml_msg);
703       $str = trim($this->o_sock->read());
705       /* Check if something went wrong while reading */
706       if($this->o_sock->is_error()){
707         $this->set_error($this->o_sock->get_error());
708         return($ret);
709       }
711       $entries = $this->xml_to_array($str); 
712       if( isset($entries['XML']['HEADER']) &&
713           $entries['XML']['HEADER']=="answer" &&
714           isset($entries['XML']['ANSWER1'])){
715         $ret = $entries['XML']['ANSWER1'];
716       }
717     }
718     return($ret);
719   }
722   /*! \brief  Removes a set of entries from the GOsa support queue. 
723     @param  Array The IDs to remove.
724     @return Boolean True on success.
725    */
726   public function remove_entries($ids)
727   {
728     if(!is_array($ids)){
729       trigger_error("Requires an array as parameter.");
730       return;
731     }
733     $this->reset_error();
735     $ret = array();
737     $xml_msg = "<xml>
738       <header>gosa_delete_jobdb_entry</header>
739       <target>GOSA</target>
740       <source>GOSA</source>
741       <where>
742       <clause>
743       <connector>or</connector>";
744     foreach($ids as $id){
745       $xml_msg .= "<phrase>
746         <operator>eq</operator>
747         <id>".$id."</id>
748         </phrase>";
749     }
750     $xml_msg .= "</clause>
751       </where>
752       </xml>";
754     if($this->connect()){
755       $this->o_sock->write($xml_msg);
756       $str = $this->o_sock->read();
758       /* Check if something went wrong while reading */
759       if($this->o_sock->is_error()){
760         $this->set_error($this->o_sock->get_error());
761         return($ret);
762       }
764       $entries = $this->xml_to_array($str);
765       if(isset($entries['XML']) || isset($entries['COUNT'])){
766         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
767         return(TRUE);
768       }else{
769         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
770       }
771     }
772     return(FALSE);
773   }
777   /*! \brief  Removes an entry from the GOsa support queue. 
778     @param  Integer The ID of the entry we want to remove.
779     @return Boolean True on success.
780    */
781   public function remove_entry($id)
782   {
783     return($this->remove_entries(array($id)));
784   }
787   /*! \brief  Parses the given xml string into an array 
788     @param  String XML string  
789     @return Array Returns an array containing the xml structure. 
790    */
791   private function xml_to_array($xml)
792   {
793     $params = array();
794     $level = array();
795     $parser  = xml_parser_create_ns();
796     xml_parse_into_struct($parser, $xml, $vals, $index);
798     $err_id = xml_get_error_code($parser);
799     if($err_id){
800       xml_parser_free($parser);
801     }else{
802       xml_parser_free($parser);
804       foreach ($vals as $xml_elem) {
805         if ($xml_elem['type'] == 'open') {
806           if (array_key_exists('attributes',$xml_elem)) {
807             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
808           } else {
809             $level[$xml_elem['level']] = $xml_elem['tag'];
810           }
811         }
812         if ($xml_elem['type'] == 'complete') {
813           $start_level = 1;
814           $php_stmt = '$params';
815           while($start_level < $xml_elem['level']) {
816             $php_stmt .= '[$level['.$start_level.']]';
817             $start_level++;
818           }
819           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
820           @eval($php_stmt);
821         }
822       }
823     }
825     if(!isset($params['XML'])){
826       if (!array_key_exists('XML', $params)){
827         $this->set_error(_("Cannot not parse XML!"));
828       }
829       $params = array("COUNT" => 0);
830     }
832     return($params); 
833   }
836   /*! \brief  Updates an entry with a set of new values, 
837     @param  Integer The ID of the entry, we want to update.
838     @param  Array   The variables to update.   
839     @return Boolean Returns TRUE on success. 
840    */
841   public function update_entries($ids,$data)
842   {
843     $this->reset_error();
844     if(!is_array($ids)){
845       trigger_error("Requires an array as first parameter.");
846       return;
847     }
849     if(!is_array($data)){
850       trigger_error("Requires an array as second parameter.");
851       return;
852     }
854     $attr = "";
855     foreach($data as $key => $value){
856       if(is_array($value)){
857         foreach($value as $sub_value){
858           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
859         }
860       }else{
861         $attr.= "<$key>".strtolower($value)."</$key>\n";
862       }
863     }
865     $xml_msg = "<xml>
866       <header>gosa_update_status_jobdb_entry</header>
867       <target>GOSA</target>
868       <source>GOSA</source>
869       <where>
870       <clause>
871       <connector>or</connector>";
872     foreach($ids as $id){
873       $xml_msg .= "<phrase>
874         <operator>eq</operator>
875         <id>".$id."</id>
876         </phrase>";
877     }
878     $xml_msg .= "</clause>
879       </where>
880       <update>
881       ".$attr." 
882       </update>
883       </xml>";
885     if($this->connect()){
887       $this->o_sock->write($xml_msg);
888       $str      = trim($this->o_sock->read());
890       /* Check if something went wrong while reading */
891       if($this->o_sock->is_error()){
892         $this->set_error($this->o_sock->get_error());
893         return(FALSE);
894       }
896       $entries = $this->xml_to_array($str);
897       if(isset($entries['XML'])){
898         if(isset($entries['XML']['ERROR_STRING'])) {
899           $this->set_error($entries['XML']['ERROR_STRING']);
900           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
901           return(FALSE);
902         }
903         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
904         return(TRUE);
905       }
906     }
907     return(FALSE);
908   }
911   /*! \brief  Returns the number of currently queued objects.
912       @return Integer  
913    */
914   public function number_of_queued_entries()
915   {
916     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
917     $this->connect();
918     if($this->connect()){
919       $this->o_sock->write($xml_msg);
920       $str     = trim($this->o_sock->read());
922       /* Check if something went wrong while reading */
923       if($this->o_sock->is_error()){
924         $this->set_error($this->o_sock->get_error());
925         return(0);
926       }
928       $entries = $this->xml_to_array($str);
929       if(isset($entries['XML'])){
930         return($entries['XML']['COUNT']);
931       }
932     }
933     return(-1);
934   } 
937   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
938   {
939     $xml_message= "";
941     /* Prepare data */
942     foreach ($data as $key => $value){
943       if(is_array($value)){
944         foreach($value as $sub_value){
945           $xml_message.= "<$key>$sub_value</$key>";
946         }
947       }else{
948         $xml_message.= "<$key>$value</$key>";
949       }
950     }
952     /* Multiple targets? */
953     if (!is_array($to)){
954       $to_targets= array($to);
955     } else {
956       $to_targets= $to;
957     }
959     /* Build target strings */
960     $target ="";
961     foreach($to_targets as $to){
962       $target.= "<target>$to</target>";
963     }
965     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
966   }
969   /* Allows simply appending a new DaemonEvent 
970    */
971   public function append($event)
972   {
973     if(!($event instanceof DaemonEvent)){
974       return(FALSE);
975     }
976   
977     $this->reset_error();
979     /* Add to queue if new 
980      */
981     if($event->is_new()){
983       $request_answer = FALSE;
984       if($event->get_type() == SCHEDULED_EVENT){
985         $action = $event->get_schedule_action();
986       }elseif($event->get_type() == TRIGGERED_EVENT){
987         $action = $event->get_trigger_action();
988       }else{
989         trigger_error("Unknown type of queue event given.");
990         return(FALSE);
991       }
993       /* Get event informations, like targets..
994        */
995       $targets    = $event->get_targets();
996       $data       = $event->save();
998       /* Append an entry for each target 
999        */
1000       foreach($targets as $target){
1001         $data['macaddress'] = $target;
1002         $this->send_data($action,$target,$data,$request_answer);
1004         if($this->is_error()){
1005           return(FALSE);
1006         }
1007       }
1008       return(TRUE);
1009     }else{
1011       /* Updated edited entry.
1012        */
1013       $id                 = $event->get_id();
1014       $data               = $event->save();
1015       return($this->update_entries(array($id),$data));
1016     }
1018     return(FALSE);
1019   }
1022 /*! \brief  Returns an array containing all queued entries.
1023     @return Array All queued entries as an array.
1024    */
1025   public function _send($data, $answer_expected= FALSE)
1026   {
1027     $this->reset_error();
1028     $ret = array();
1030     if($this->connect()){
1031       $this->o_sock->write($data);
1032       if ($answer_expected){
1033         $str = trim($this->o_sock->read());
1035         /* Check if something went wrong while reading */
1036         if($this->o_sock->is_error()){
1037           $this->set_error($this->o_sock->get_error());
1038           return($ret);
1039         }
1041         $entries = $this->xml_to_array($str);
1042         if(isset($entries['XML']) && is_array($entries['XML'])){
1043           $ret = $entries;
1044           if(isset($entries['XML']['ERROR_STRING'])) {
1045             $this->set_error($entries['XML']['ERROR_STRING']);
1046             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1047           }else{
1048             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1049           }
1050         }
1051       }else{
1052         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1053       }
1054     }
1055     return($ret);
1056   }
1059   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1060   {
1061     $xml_message= "";
1063     /* Get communication object */
1064     $d= new gosaSupportDaemon(TRUE,10);
1066     /* Prepare data */
1067     foreach ($data as $key => $value){
1068       if(is_array($value)){
1069         foreach($value as $sub_val){
1070           $xml_message.= "<$key>$sub_value</$key>";
1071         }
1072       }else{
1073         $xml_message.= "<$key>$value</$key>";
1074       }
1075     }
1077     /* Multiple targets? */
1078     if (!is_array($to)){
1079       $to_targets= array($to);
1080     } else {
1081       $to_targets= $to;
1082     }
1084     /* Build target strings */
1085     $target ="";
1086     foreach($to_targets as $to){
1087       $target.= "<target>$to</target>";
1088     }
1090     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1091   }
1094   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1095       @param  String  $mac  The mac address for which we want to remove all jobs.      
1096    */
1097   function clean_queue_from_mac($mac)
1098   {
1099     global $config;
1101     /* First of all we have to check which jobs are startet 
1102      *  for $mac 
1103      */
1104     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1105     
1106     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1107  
1108     $data = $this->_send($xml_msg,TRUE);
1109     if(is_array($data) && isset($data['XML'])){
1110       $already_aborted = FALSE;
1111       foreach($data['XML']  as $name => $entry){
1112         if(preg_match("/answer[0-9]*/i",$name)){
1113           $entry['STATUS'] = strtoupper($entry['STATUS']);
1114           switch($entry['STATUS']){
1116             case 'PROCESSING' :
1118               /* Send abort event, but only once 
1119                */
1120               if($already_aborted){
1121                 break;
1122               }elseif(class_available("DaemonEvent_faireboot")){
1123                 $already_aborted = TRUE;
1124                 $tmp = new DaemonEvent_faireboot($config);
1125                 $tmp->add_targets(array($mac));
1126                 $tmp->set_type(TRIGGERED_EVENT);
1127                 if(!$this->append($tmp)){
1128                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1129                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1130                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1131                 }else{
1132                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1133                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1134                 }
1135                 ;break;
1136               }else{
1137                 /* Couldn't find abort event, just remove entry */
1138               }
1140             case 'WAITING':
1141             case 'ERROR':
1142             default :
1143             
1144               /* Simply remove entries from queue. 
1145                *  Failed or waiting events, can be removed without any trouble.
1146                */ 
1147               if(!$this->remove_entries(array($entry['ID']))){
1148                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1149               }
1150               ;break;
1151           }
1152     
1153         }
1154       }
1155     }
1156   }
1159 static function ping($target)
1161   if (tests::is_mac($target)){
1162     /* Get communication object */
1163     $d= new gosaSupportDaemon(TRUE,0.5);
1164     $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1165     return (count($answer) ? TRUE:FALSE);
1166   }
1168   return (FALSE);
1173 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1174 ?>