Code

7d8a4f494c94a1a8a041f0909cff547805d6fbcf
[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 = 
140       "<xml>".
141       "<header>gosa_get_available_kernel</header>".
142       "<source>GOSA</source>".
143       "<target>GOSA</target>".
144       "<release>".$release."</release>".
145       "</xml>";
147     $ret = array();
148     if($this->connect()){
149       $this->o_sock->write($xml_msg);
150       $str = trim($this->o_sock->read());
152       /* Check if something went wrong while reading */
153       if($this->o_sock->is_error()){
154         $this->set_error($this->o_sock->get_error());
155         return($ret);
156       }
158       $entries = $this->xml_to_array($str);
159       if(isset($entries['XML']) && is_array($entries['XML'])){
161         /* Check if returned values represent a valid answer */
162         if(isset($entries['XML'])){
163           if(isset($entries['XML']['ERROR_STRING'])) {
164             $this->set_error($entries['XML']['ERROR_STRING']);
165             new log("debug","GOsa-si",
166                 get_class($this)."::".__FUNCTION__, array(),
167                 "FAILED error was ".$this->get_error());
168             return($ret);
169           }
171           /* Unset header tags */
172           $ret = $entries['XML'];
173           foreach($ret as $key => $entry){
174             if(!preg_match("/^answer/i",$key)){
175               unset($ret[$key]);
176             }
177           }
178         }
179       }
180     }
181     return($ret);
182   }
185   public function FAI_get_package_sections($release)
186   {
187     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
188       "<select>distinct section</select>".
189       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
191     if($this->connect()){
192       $this->o_sock->write($xml_msg);
193       $str = trim($this->o_sock->read());
195       /* Check if something went wrong while reading */
196       if($this->o_sock->is_error()){
197         $this->set_error($this->o_sock->get_error());
198         return($ret);
199       }
201       $entries = $this->xml_to_array($str);
202       if(isset($entries['XML']) && is_array($entries['XML'])){
204         /* Check if returned values represent a valid answer */
205         if(isset($entries['XML'])){
206           if(isset($entries['XML']['ERROR_STRING'])) {
207             $this->set_error($entries['XML']['ERROR_STRING']);
208             new log("debug","GOsa-si",
209                 get_class($this)."::".__FUNCTION__, array(),
210                 "FAILED error was ".$this->get_error());
211             return($ret);
212           }
214           /* Unset header tags */
215           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
216             if(isset($entries['XML'][$type])){
217               unset($entries['XML'][$type]);
218             }
219           }
220           $ret = $entries['XML'];
221         }
222       }
223     }
224     return($ret);
225   }
228   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
229   {
230     $this->reset_error();
231     $ret = array();
233     /* Check Parameter */
234     if(!is_array($attrs) || !count($attrs)){
235       trigger_error("Second parameter must be an array. With at least one attribute name.");
236       return($ret);
237     }
239     /* Check Parameter */
240     if(!is_array($package)){
241       trigger_error("Third parameter must be an array. With at least one attribute name.");
242       return($ret);
243     }
245     /* Create list of attributes to fetch */
246     $attr = ""; 
247     foreach($attrs as $at){
248       $attr.= "<select>".$at."</select>";
249     }
251     /* If no package is given, search for all */
252     if(!count($package)) $package = array("%");
254     /* Create limit tag */
255     if($from == -1){
256       $limit =""; 
257     }else{
258       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
259     }
261     /* Create list of attributes to fetch */
262     $pkgs = ""; 
263     foreach($package as $pkg){
264       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
265     }
267     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
268       $attr.
269       "<where>
270       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
271       <clause><connector>OR</connector>
272       ".$pkgs."
273       </clause>
274       </where>".
275       $limit.
276       "</xml>";
278     if($this->connect()){
279       $this->o_sock->write($xml_msg);
280       $str = trim($this->o_sock->read());
282       /* Check if something went wrong while reading */
283       if($this->o_sock->is_error()){
284         $this->set_error($this->o_sock->get_error());
285         return($ret);
286       }
288       $entries = $this->xml_to_array($str);
289       if(isset($entries['XML']) && is_array($entries['XML'])){
291         /* Check if returned values represent a valid answer */
292         if(isset($entries['XML'])){
293           if(isset($entries['XML']['ERROR_STRING'])) {
294             $this->set_error($entries['XML']['ERROR_STRING']);
295             new log("debug","GOsa-si",
296                 get_class($this)."::".__FUNCTION__, array(),
297                 "FAILED error was ".$this->get_error());
298             return($ret);
299           }
301           /* Unset header tags */
302           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
303             if(isset($entries['XML'][$type])){
304               unset($entries['XML'][$type]);
305             }
306           }
307           $ret = $entries['XML'];
308         }
309       }
310     }
311     return($ret);
313     
314   }
317   public function FAI_get_server($name = "")
318   {
319     $this->reset_error();
321     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
322     $ret = array();
323     if($this->connect()){
324       $this->o_sock->write($xml_msg);
325       $str = trim($this->o_sock->read());
327       /* Check if something went wrong while reading */
328       if($this->o_sock->is_error()){
329         $this->set_error($this->o_sock->get_error());
330         return($ret);
331       }
333       $entries = $this->xml_to_array($str);
334       if(isset($entries['XML']) && is_array($entries['XML'])){
336         /* Check if returned values represent a valid answer */
337         if(isset($entries['XML'])){
338           if(isset($entries['XML']['ERROR_STRING'])) {
339             $this->set_error($entries['XML']['ERROR_STRING']);
340             new log("debug","GOsa-si", 
341               get_class($this)."::".__FUNCTION__, array(),
342               "FAILED error was ".$this->get_error());
343             return($ret);
344           }
346           /* Unset header tags */
347           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
348             if(isset($entries['XML'][$type])){
349               unset($entries['XML'][$type]);
350             }
351           }
352           $ret = $entries['XML']; 
353         }
354       }
355     }
356     return($ret);
357   }
360   public function FAI_get_classes($name)
361   {
362     $this->reset_error();
363     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
364                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
365     $ret = array();
366     if($this->connect()){
367       $this->o_sock->write($xml_msg);
368       $str = trim($this->o_sock->read());
370       /* Check if something went wrong while reading */
371       if($this->o_sock->is_error()){
372         $this->set_error($this->o_sock->get_error());
373         return($ret);
374       }
376       $entries = $this->xml_to_array($str);
377       if(isset($entries['XML']) && is_array($entries['XML'])){
379         /* Check if returned values represent a valid answer */
380         if(isset($entries['XML'])){
381           if(isset($entries['XML']['ERROR_STRING'])) {
382             $this->set_error($entries['XML']['ERROR_STRING']);
383             new log("debug","GOsa-si", 
384               get_class($this)."::".__FUNCTION__, array($name),
385               "FAILED error was ".$this->get_error());
386             return($ret);
387           }
389           /* Unset header tags */
390           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
391             if(isset($entries['XML'][$type])){
392               unset($entries['XML'][$type]);
393             }
394           }
395           $ret = $entries['XML']; 
396         }
397       }
398     }
399     return($ret);
400   }
403   /*! \brief  Returns an array containing all queued entries.
404     @return Array All queued entries as an array.
405    */
406   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
407   {
408     $this->reset_error();
409     $ret = array();
411     $tags = "";
412     foreach($event_types as $type){
413       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
414     }
415     if(count($event_types) > 1){
416       $tags = "<connector>or</connector>".$tags;
417     }
418     if(count($event_types)){
419       $tags = "<where><clause>".$tags."</clause></where>";
420     }
422     $xml_msg = 
423       "<xml>
424       <header>gosa_query_jobdb</header>
425       <target>GOSA</target>
426       <source>GOSA</source>
427       ".$tags."
429       <orderby>".$sort."</orderby>";
430     if($from != -1 && $to != -1){
431       $xml_msg.= "
432         <limit>
433         <from>".$from."</from>
434         <to>".$to."</to>
435         </limit>";
436     }
437     $xml_msg.= "
438       </xml>";
440     if($this->connect()){
441       $this->o_sock->write($xml_msg);
442       $str = trim($this->o_sock->read());
444       /* Check if something went wrong while reading */
445       if($this->o_sock->is_error()){
446         $this->set_error($this->o_sock->get_error());
447         return($ret);
448       }
450       $entries = $this->xml_to_array($str);
451       if(isset($entries['XML']) && is_array($entries['XML'])){
453         /* Check if returned values represent a valid answer */
454         if(isset($entries['XML'])){
455           
456           /* Unset header tags */
457           foreach(array("HEADER","SOURCE","TARGET") as $type){
458             unset($entries['XML'][$type]);
459           }
460           $ret = $entries['XML']; 
461         }
462       }
463     }
464     
465     /* Remove session ID. No one is interested in this... */
466     unset($ret['SESSION_ID']);
468     return($ret);
469   }
472   /*! \brief  Checks if the given ids are used queue ids.
473     @param  Array   The ids we want to check..
474     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
475    */
476   public function ids_exist($ids)
477   {
478     if(!is_array($ids)){
479       trigger_error("Requires an array as parameter.");
480       return;
481     }
482     $this->reset_error();
484     $ret = array();
486     $xml_msg = "<xml>
487       <header>gosa_query_jobdb</header>
488       <target>GOSA</target>
489       <source>GOSA</source>
490       <where>
491       <clause>
492       <connector>or</connector>";
493     foreach($ids as $id){
494       $xml_msg .= "<phrase>
495         <operator>eq</operator>
496         <id>".$id."</id>
497         </phrase>";
498     }
499     $xml_msg .= "</clause>
500       </where>
501       </xml>";
503     if($this->connect()){
504       $this->o_sock->write($xml_msg);
505       $str = trim($this->o_sock->read());
507       /* Check if something went wrong while reading */
508       if($this->o_sock->is_error()){
509         $this->set_error($this->o_sock->get_error());
510         return($ret);
511       }
513       $entries = $this->xml_to_array($str);
514       if(isset($entries['XML']) && is_array($entries['XML'])){
515         foreach($entries['XML'] as $entry){
516           if(isset($entry['ID'])){
517             $ret[] = $entry['ID'];
518           }
519         }
520       }
521     }
522     return($ret);
523   }
526   /*! \brief  Returns an entry containing all requested ids.
527     @param  Array   The IDs of the entries we want to return.
528     @return Array   Of the requested entries. 
529    */
530   public function get_entries_by_mac($macs)
531   {
532     if(!is_array($macs)){
533       trigger_error("Requires an array as parameter.");
534       return;
535     }
536     $this->reset_error();
538     $ret = array();
540     $xml_msg = "<xml>
541       <header>gosa_query_jobdb</header>
542       <target>GOSA</target>
543       <source>GOSA</source>
544       <where>
545       <clause>
546       <connector>or</connector>";
547     foreach($macs as $mac){
548       $xml_msg .= "<phrase>
549         <operator>eq</operator>
550         <macaddress>".$mac."</macaddress>
551         </phrase>";
552     }
553     $xml_msg .= "</clause>
554       </where>
555       </xml>";
557     if($this->connect()){
558       $this->o_sock->write($xml_msg);
559       $str = trim($this->o_sock->read());
561       /* Check if something went wrong while reading */
562       if($this->o_sock->is_error()){
563         $this->set_error($this->o_sock->get_error());
564         return($ret);
565       }
567       $entries = $this->xml_to_array($str); 
568       if(isset($entries['XML'])){
569         foreach($entries['XML'] as $name => $entry){
570           if(preg_match("/^ANSWER[0-9]*$/",$name)){
571             $ret[$name] = $entry;
572           }
573         }
574       }
575     }
576     return($ret);
577   }
580   /*! \brief  Returns an entry containing all requested ids.
581     @param  Array   The IDs of the entries we want to return.
582     @return Array   Of the requested entries. 
583    */
584   public function get_entries_by_id($ids)
585   {
586     if(!is_array($ids)){
587       trigger_error("Requires an array as parameter.");
588       return;
589     }
590     $this->reset_error();
592     $ret = array();
594     $xml_msg = "<xml>
595       <header>gosa_query_jobdb</header>
596       <target>GOSA</target>
597       <source>GOSA</source>
598       <where>
599       <clause>
600       <connector>or</connector>";
601     foreach($ids as $id){
602       $xml_msg .= "<phrase>
603         <operator>eq</operator>
604         <id>".$id."</id>
605         </phrase>";
606     }
607     $xml_msg .= "</clause>
608       </where>
609       </xml>";
611     if($this->connect()){
612       $this->o_sock->write($xml_msg);
613       $str = trim($this->o_sock->read());
615       /* Check if something went wrong while reading */
616       if($this->o_sock->is_error()){
617         $this->set_error($this->o_sock->get_error());
618         return($ret);
619       }
621       $entries = $this->xml_to_array($str); 
622       if(isset($entries['XML'])){
623         foreach($entries['XML'] as $name => $entry){
624           if(preg_match("/^ANSWER[0-9]*$/",$name)){
625             $ret[$name] = $entry;
626           }
627         }
628       }
629     }
630     return($ret);
631   }
634   /*! \brief  Checks if the given id is in use.
635     @param  Integer The ID of the entry.
636     @return Boolean TRUE if entry exists. 
637    */
638   public function id_exists($id)
639   {
640     if(!is_numeric($id)){
641       trigger_error("Requires an integer as parameter.");
642       return;
643     }
645     $this->reset_error();
647     $xml_msg = "<xml>
648       <header>gosa_query_jobdb</header>
649       <target>GOSA</target>
650       <source>GOSA</source>
651       <where>
652       <clause>
653       <phrase>
654       <operator>eq</operator>
655       <id>".$id."</id>
656       </phrase>
657       </clause>
658       </where>
659       </xml>";
661     if($this->connect()){
662       $this->o_sock->write($xml_msg);
663       $str = trim($this->o_sock->read());
665       /* Check if something went wrong while reading */
666       if($this->o_sock->is_error()){
667         $this->set_error($this->o_sock->get_error());
668         return(FALSE);
669       }
671       $entries = $this->xml_to_array($str); 
672       if( isset($entries['XML']['HEADER']) && 
673           $entries['XML']['HEADER']=="answer" && 
674           isset($entries['XML']['ANSWER1'])){
675         return(TRUE);
676       }
677     }
678     return(FALSE);
679   }
682   /*! \brief  Returns an entry from the gosaSupportQueue
683     @param  Integer The ID of the entry we want to return.
684     @return Array   Of the requested entry. 
685    */
686   public function get_entry_by_id($id)
687   {
688     if(!is_numeric($id)){
689       trigger_error("Requires an integer as parameter.");
690       return;
691     }
692     $this->reset_error();
693   
694     $ret = array();
695     $xml_msg = "<xml>
696       <header>gosa_query_jobdb</header>
697       <target>GOSA</target>
698       <source>GOSA</source>
699       <where>
700       <clause>
701       <phrase>
702       <operator>eq</operator>
703       <id>".$id."</id>
704       </phrase>
705       </clause>
706       </where>
707       </xml>";
708     if($this->connect()){
709       $this->o_sock->write($xml_msg);
710       $str = trim($this->o_sock->read());
712       /* Check if something went wrong while reading */
713       if($this->o_sock->is_error()){
714         $this->set_error($this->o_sock->get_error());
715         return($ret);
716       }
718       $entries = $this->xml_to_array($str); 
719       if( isset($entries['XML']['HEADER']) &&
720           $entries['XML']['HEADER']=="answer" &&
721           isset($entries['XML']['ANSWER1'])){
722         $ret = $entries['XML']['ANSWER1'];
723       }
724     }
725     return($ret);
726   }
729   /*! \brief  Removes a set of entries from the GOsa support queue. 
730     @param  Array The IDs to remove.
731     @return Boolean True on success.
732    */
733   public function remove_entries($ids)
734   {
735     if(!is_array($ids)){
736       trigger_error("Requires an array as parameter.");
737       return;
738     }
740     $this->reset_error();
742     $ret = array();
744     $xml_msg = "<xml>
745       <header>gosa_delete_jobdb_entry</header>
746       <target>GOSA</target>
747       <source>GOSA</source>
748       <where>
749       <clause>
750       <connector>or</connector>";
751     foreach($ids as $id){
752       $xml_msg .= "<phrase>
753         <operator>eq</operator>
754         <id>".$id."</id>
755         </phrase>";
756     }
757     $xml_msg .= "</clause>
758       </where>
759       </xml>";
761     if($this->connect()){
762       $this->o_sock->write($xml_msg);
763       $str = $this->o_sock->read();
765       /* Check if something went wrong while reading */
766       if($this->o_sock->is_error()){
767         $this->set_error($this->o_sock->get_error());
768         return($ret);
769       }
771       $entries = $this->xml_to_array($str);
772       if(isset($entries['XML']) || isset($entries['COUNT'])){
773         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
774         return(TRUE);
775       }else{
776         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
777       }
778     }
779     return(FALSE);
780   }
784   /*! \brief  Removes an entry from the GOsa support queue. 
785     @param  Integer The ID of the entry we want to remove.
786     @return Boolean True on success.
787    */
788   public function remove_entry($id)
789   {
790     return($this->remove_entries(array($id)));
791   }
794   /*! \brief  Parses the given xml string into an array 
795     @param  String XML string  
796     @return Array Returns an array containing the xml structure. 
797    */
798   private function xml_to_array($xml)
799   {
800     $params = array();
801     $level = array();
802     $parser  = xml_parser_create_ns();
803     xml_parse_into_struct($parser, $xml, $vals, $index);
805     $err_id = xml_get_error_code($parser);
806     if($err_id){
807       xml_parser_free($parser);
808     }else{
809       xml_parser_free($parser);
811       foreach ($vals as $xml_elem) {
812         if ($xml_elem['type'] == 'open') {
813           if (array_key_exists('attributes',$xml_elem)) {
814             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
815           } else {
816             $level[$xml_elem['level']] = $xml_elem['tag'];
817           }
818         }
819         if ($xml_elem['type'] == 'complete') {
820           $start_level = 1;
821           $php_stmt = '$params';
822           while($start_level < $xml_elem['level']) {
823             $php_stmt .= '[$level['.$start_level.']]';
824             $start_level++;
825           }
826           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
827           @eval($php_stmt);
828         }
829       }
830     }
832     if(!isset($params['XML'])){
833       if (!array_key_exists('XML', $params)){
834         $this->set_error(_("Cannot not parse XML!"));
835       }
836       $params = array("COUNT" => 0);
837     }
839     return($params); 
840   }
843   /*! \brief  Updates an entry with a set of new values, 
844     @param  Integer The ID of the entry, we want to update.
845     @param  Array   The variables to update.   
846     @return Boolean Returns TRUE on success. 
847    */
848   public function update_entries($ids,$data)
849   {
850     $this->reset_error();
851     if(!is_array($ids)){
852       trigger_error("Requires an array as first parameter.");
853       return;
854     }
856     if(!is_array($data)){
857       trigger_error("Requires an array as second parameter.");
858       return;
859     }
861     $attr = "";
862     foreach($data as $key => $value){
863       if(is_array($value)){
864         foreach($value as $sub_value){
865           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
866         }
867       }else{
868         $attr.= "<$key>".strtolower($value)."</$key>\n";
869       }
870     }
872     $xml_msg = "<xml>
873       <header>gosa_update_status_jobdb_entry</header>
874       <target>GOSA</target>
875       <source>GOSA</source>
876       <where>
877       <clause>
878       <connector>or</connector>";
879     foreach($ids as $id){
880       $xml_msg .= "<phrase>
881         <operator>eq</operator>
882         <id>".$id."</id>
883         </phrase>";
884     }
885     $xml_msg .= "</clause>
886       </where>
887       <update>
888       ".$attr." 
889       </update>
890       </xml>";
892     if($this->connect()){
894       $this->o_sock->write($xml_msg);
895       $str      = trim($this->o_sock->read());
897       /* Check if something went wrong while reading */
898       if($this->o_sock->is_error()){
899         $this->set_error($this->o_sock->get_error());
900         return(FALSE);
901       }
903       $entries = $this->xml_to_array($str);
904       if(isset($entries['XML'])){
905         if(isset($entries['XML']['ERROR_STRING'])) {
906           $this->set_error($entries['XML']['ERROR_STRING']);
907           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
908           return(FALSE);
909         }
910         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
911         return(TRUE);
912       }
913     }
914     return(FALSE);
915   }
918   /*! \brief  Returns the number of currently queued objects.
919       @return Integer  
920    */
921   public function number_of_queued_entries($event_types)
922   {
923     $tags = "";
924     foreach($event_types as $type){
925       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
926     }
927     if(count($event_types) > 1){
928       $tags = "<connector>or</connector>".$tags;
929     }
930     if(count($event_types)){
931       $tags = "<where><clause>".$tags."</clause></where>";
932     }
935     $xml_msg =
936       "<xml>".
937       "<header>gosa_query_jobdb</header>".
938       "<target>GOSA</target>".
939       "<source>GOSA</source>".
940       "<select> count ID</select>".
941       $tags.
942       "</xml>";
944     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
945     $this->connect();
946     if($this->connect()){
947       $this->o_sock->write($xml_msg);
948       $str     = trim($this->o_sock->read());
950       /* Check if something went wrong while reading */
951       if($this->o_sock->is_error()){
952         $this->set_error($this->o_sock->get_error());
953         return(0);
954       }
956       $entries = $this->xml_to_array($str);
957       if(isset($entries['XML'])){
958         return($entries['XML']['COUNT']);
959       }
960     }
961     return(-1);
962   } 
965   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
966   {
967     $xml_message= "";
969     /* Prepare data */
970     foreach ($data as $key => $value){
971       if(is_array($value)){
972         foreach($value as $sub_value){
973           $xml_message.= "<$key>$sub_value</$key>";
974         }
975       }else{
976         $xml_message.= "<$key>$value</$key>";
977       }
978     }
980     /* Multiple targets? */
981     if (!is_array($to)){
982       $to_targets= array($to);
983     } else {
984       $to_targets= $to;
985     }
987     /* Build target strings */
988     $target ="";
989     foreach($to_targets as $to){
990       $target.= "<target>$to</target>";
991     }
993     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
994   }
997   /* Allows simply appending a new DaemonEvent 
998    */
999   public function append($event)
1000   {
1001     if(!($event instanceof DaemonEvent)){
1002       return(FALSE);
1003     }
1004   
1005     $this->reset_error();
1007     /* Add to queue if new 
1008      */
1009     if($event->is_new()){
1011       $request_answer = FALSE;
1012       if($event->get_type() == SCHEDULED_EVENT){
1013         $action = $event->get_schedule_action();
1014       }elseif($event->get_type() == TRIGGERED_EVENT){
1015         $action = $event->get_trigger_action();
1016       }else{
1017         trigger_error("Unknown type of queue event given.");
1018         return(FALSE);
1019       }
1021       /* Get event informations, like targets..
1022        */
1023       $targets    = $event->get_targets();
1024       $data       = $event->save();
1026       /* Append an entry for each target 
1027        */
1028       foreach($targets as $target){
1029         $data['macaddress'] = $target;
1030         $this->send_data($action,$target,$data,$request_answer);
1032         if($this->is_error()){
1033           return(FALSE);
1034         }
1035       }
1036       return(TRUE);
1037     }else{
1039       /* Updated edited entry.
1040        */
1041       $id                 = $event->get_id();
1042       $data               = $event->save();
1043       return($this->update_entries(array($id),$data));
1044     }
1046     return(FALSE);
1047   }
1050 /*! \brief  Returns an array containing all queued entries.
1051     @return Array All queued entries as an array.
1052    */
1053   public function _send($data, $answer_expected= FALSE)
1054   {
1055     $this->reset_error();
1056     $ret = array();
1058     if($this->connect()){
1059       $this->o_sock->write($data);
1060       if ($answer_expected){
1061         $str = trim($this->o_sock->read());
1063         /* Check if something went wrong while reading */
1064         if($this->o_sock->is_error()){
1065           $this->set_error($this->o_sock->get_error());
1066           return($ret);
1067         }
1069         $entries = $this->xml_to_array($str);
1070         if(isset($entries['XML']) && is_array($entries['XML'])){
1071           $ret = $entries;
1072           if(isset($entries['XML']['ERROR_STRING'])) {
1073             $this->set_error($entries['XML']['ERROR_STRING']);
1074             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1075           }else{
1076             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1077           }
1078         }
1079       }else{
1080         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1081       }
1082     }
1083     return($ret);
1084   }
1087   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1088   {
1089     $xml_message= "";
1091     /* Get communication object */
1092     $d= new gosaSupportDaemon(TRUE,10);
1094     /* Prepare data */
1095     foreach ($data as $key => $value){
1096       if(is_array($value)){
1097         foreach($value as $sub_val){
1098           $xml_message.= "<$key>$sub_value</$key>";
1099         }
1100       }else{
1101         $xml_message.= "<$key>$value</$key>";
1102       }
1103     }
1105     /* Multiple targets? */
1106     if (!is_array($to)){
1107       $to_targets= array($to);
1108     } else {
1109       $to_targets= $to;
1110     }
1112     /* Build target strings */
1113     $target ="";
1114     foreach($to_targets as $to){
1115       $target.= "<target>$to</target>";
1116     }
1118     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1119   }
1122   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1123       @param  String  $mac  The mac address for which we want to remove all jobs.      
1124    */
1125   function clean_queue_from_mac($mac)
1126   {
1127     global $config;
1129     /* First of all we have to check which jobs are startet 
1130      *  for $mac 
1131      */
1132     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1133     
1134     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1135  
1136     $data = $this->_send($xml_msg,TRUE);
1137     if(is_array($data) && isset($data['XML'])){
1138       $already_aborted = FALSE;
1139       foreach($data['XML']  as $name => $entry){
1140         if(preg_match("/answer[0-9]*/i",$name)){
1141           $entry['STATUS'] = strtoupper($entry['STATUS']);
1142           switch($entry['STATUS']){
1144             case 'PROCESSING' :
1146               /* Send abort event, but only once 
1147                */
1148               if($already_aborted){
1149                 break;
1150               }elseif(class_available("DaemonEvent_faireboot")){
1151                 $already_aborted = TRUE;
1152                 $tmp = new DaemonEvent_faireboot($config);
1153                 $tmp->add_targets(array($mac));
1154                 $tmp->set_type(TRIGGERED_EVENT);
1155                 if(!$this->append($tmp)){
1156                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1157                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1158                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1159                 }else{
1160                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1161                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1162                 }
1163                 ;break;
1164               }else{
1165                 /* Couldn't find abort event, just remove entry */
1166               }
1168             case 'WAITING':
1169             case 'ERROR':
1170             default :
1171             
1172               /* Simply remove entries from queue. 
1173                *  Failed or waiting events, can be removed without any trouble.
1174                */ 
1175               if(!$this->remove_entries(array($entry['ID']))){
1176                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1177               }
1178               ;break;
1179           }
1180     
1181         }
1182       }
1183     }
1184   }
1187 static function ping($target)
1189   if (tests::is_mac($target)){
1190     /* Get communication object */
1191     $d= new gosaSupportDaemon(TRUE,0.5);
1192     $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1193     return (count($answer) ? TRUE:FALSE);
1194   }
1196   return (FALSE);
1201 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1202 ?>