Code

5f160733477b1c4b4aae02c7ae594f82a9535981
[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   }
139   public function FAI_get_package_sections($release)
140   {
141     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
142       "<select>distinct section</select>".
143       "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
145     if($this->connect()){
146       $this->o_sock->write($xml_msg);
147       $str = trim($this->o_sock->read());
149       /* Check if something went wrong while reading */
150       if($this->o_sock->is_error()){
151         $this->set_error($this->o_sock->get_error());
152         return($ret);
153       }
155       $entries = $this->xml_to_array($str);
156       if(isset($entries['XML']) && is_array($entries['XML'])){
158         /* Check if returned values represent a valid answer */
159         if(isset($entries['XML'])){
160           if(isset($entries['XML']['ERROR_STRING'])) {
161             $this->set_error($entries['XML']['ERROR_STRING']);
162             new log("debug","GOsa-si",
163                 get_class($this)."::".__FUNCTION__, array(),
164                 "FAILED error was ".$this->get_error());
165             return($ret);
166           }
168           /* Unset header tags */
169           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
170             if(isset($entries['XML'][$type])){
171               unset($entries['XML'][$type]);
172             }
173           }
174           $ret = $entries['XML'];
175         }
176       }
177     }
178     return($ret);
179   }
182   public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
183   {
184     $this->reset_error();
185     $ret = array();
187     /* Check Parameter */
188     if(!is_array($attrs) || !count($attrs)){
189       trigger_error("Second parameter must be an array. With at least one attribute name.");
190       return($ret);
191     }
193     /* Check Parameter */
194     if(!is_array($package)){
195       trigger_error("Third parameter must be an array. With at least one attribute name.");
196       return($ret);
197     }
199     /* Create list of attributes to fetch */
200     $attr = ""; 
201     foreach($attrs as $at){
202       $attr.= "<select>".$at."</select>";
203     }
205     /* If no package is given, search for all */
206     if(!count($package)) $package = array("%");
208     /* Create limit tag */
209     if($from == -1){
210       $limit =""; 
211     }else{
212       $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
213     }
215     /* Create list of attributes to fetch */
216     $pkgs = ""; 
217     foreach($package as $pkg){
218       $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
219     }
221     $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
222       $attr.
223       "<where>
224       <clause><phrase><distribution>".$release."</distribution></phrase></clause>
225       <clause><connector>OR</connector>
226       ".$pkgs."
227       </clause>
228       </where>".
229       $limit.
230       "</xml>";
232     if($this->connect()){
233       $this->o_sock->write($xml_msg);
234       $str = trim($this->o_sock->read());
236       /* Check if something went wrong while reading */
237       if($this->o_sock->is_error()){
238         $this->set_error($this->o_sock->get_error());
239         return($ret);
240       }
242       $entries = $this->xml_to_array($str);
243       if(isset($entries['XML']) && is_array($entries['XML'])){
245         /* Check if returned values represent a valid answer */
246         if(isset($entries['XML'])){
247           if(isset($entries['XML']['ERROR_STRING'])) {
248             $this->set_error($entries['XML']['ERROR_STRING']);
249             new log("debug","GOsa-si",
250                 get_class($this)."::".__FUNCTION__, array(),
251                 "FAILED error was ".$this->get_error());
252             return($ret);
253           }
255           /* Unset header tags */
256           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
257             if(isset($entries['XML'][$type])){
258               unset($entries['XML'][$type]);
259             }
260           }
261           $ret = $entries['XML'];
262         }
263       }
264     }
265     return($ret);
267     
268   }
271   public function FAI_get_server($name = "")
272   {
273     $this->reset_error();
275     $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
276     $ret = array();
277     if($this->connect()){
278       $this->o_sock->write($xml_msg);
279       $str = trim($this->o_sock->read());
281       /* Check if something went wrong while reading */
282       if($this->o_sock->is_error()){
283         $this->set_error($this->o_sock->get_error());
284         return($ret);
285       }
287       $entries = $this->xml_to_array($str);
288       if(isset($entries['XML']) && is_array($entries['XML'])){
290         /* Check if returned values represent a valid answer */
291         if(isset($entries['XML'])){
292           if(isset($entries['XML']['ERROR_STRING'])) {
293             $this->set_error($entries['XML']['ERROR_STRING']);
294             new log("debug","GOsa-si", 
295               get_class($this)."::".__FUNCTION__, array(),
296               "FAILED error was ".$this->get_error());
297             return($ret);
298           }
300           /* Unset header tags */
301           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
302             if(isset($entries['XML'][$type])){
303               unset($entries['XML'][$type]);
304             }
305           }
306           $ret = $entries['XML']; 
307         }
308       }
309     }
310     return($ret);
311   }
314   public function FAI_get_classes($name)
315   {
316     $this->reset_error();
317     $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
318                   "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
319     $ret = array();
320     if($this->connect()){
321       $this->o_sock->write($xml_msg);
322       $str = trim($this->o_sock->read());
324       /* Check if something went wrong while reading */
325       if($this->o_sock->is_error()){
326         $this->set_error($this->o_sock->get_error());
327         return($ret);
328       }
330       $entries = $this->xml_to_array($str);
331       if(isset($entries['XML']) && is_array($entries['XML'])){
333         /* Check if returned values represent a valid answer */
334         if(isset($entries['XML'])){
335           if(isset($entries['XML']['ERROR_STRING'])) {
336             $this->set_error($entries['XML']['ERROR_STRING']);
337             new log("debug","GOsa-si", 
338               get_class($this)."::".__FUNCTION__, array($name),
339               "FAILED error was ".$this->get_error());
340             return($ret);
341           }
343           /* Unset header tags */
344           foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
345             if(isset($entries['XML'][$type])){
346               unset($entries['XML'][$type]);
347             }
348           }
349           $ret = $entries['XML']; 
350         }
351       }
352     }
353     return($ret);
354   }
357   /*! \brief  Returns an array containing all queued entries.
358     @return Array All queued entries as an array.
359    */
360   public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
361   {
362     $this->reset_error();
363     $ret = array();
365     $tags = "";
366     foreach($event_types as $type){
367       $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
368     }
369     if(count($event_types) > 1){
370       $tags = "<connector>or</connector>".$tags;
371     }
372     if(count($event_types)){
373       $tags = "<where><clause>".$tags."</clause></where>";
374     }
376     $xml_msg = 
377 "<xml>
378       <header>gosa_query_jobdb</header>
379       <target>GOSA</target>
380       <source>GOSA</source>
381       ".$tags."
383       <orderby>".$sort."</orderby>";
384 if($from != -1 && $to != -1){
385 $xml_msg.= "
386       <limit>
387        <from>".$from."</from>
388        <to>".$to."</to>
389       </limit>";
391 $xml_msg.= "
392       </xml>";
394     if($this->connect()){
395       $this->o_sock->write($xml_msg);
396       $str = trim($this->o_sock->read());
398       /* Check if something went wrong while reading */
399       if($this->o_sock->is_error()){
400         $this->set_error($this->o_sock->get_error());
401         return($ret);
402       }
404       $entries = $this->xml_to_array($str);
405       if(isset($entries['XML']) && is_array($entries['XML'])){
407         /* Check if returned values represent a valid answer */
408         if(isset($entries['XML'])){
409           
410           /* Unset header tags */
411           foreach(array("HEADER","SOURCE","TARGET") as $type){
412             unset($entries['XML'][$type]);
413           }
414           $ret = $entries['XML']; 
415         }
416       }
417     }
418     
419     /* Remove session ID. No one is interested in this... */
420     unset($ret['SESSION_ID']);
422     return($ret);
423   }
426   /*! \brief  Checks if the given ids are used queue ids.
427     @param  Array   The ids we want to check..
428     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
429    */
430   public function ids_exist($ids)
431   {
432     if(!is_array($ids)){
433       trigger_error("Requires an array as parameter.");
434       return;
435     }
436     $this->reset_error();
438     $ret = array();
440     $xml_msg = "<xml>
441       <header>gosa_query_jobdb</header>
442       <target>GOSA</target>
443       <source>GOSA</source>
444       <where>
445       <clause>
446       <connector>or</connector>";
447     foreach($ids as $id){
448       $xml_msg .= "<phrase>
449         <operator>eq</operator>
450         <id>".$id."</id>
451         </phrase>";
452     }
453     $xml_msg .= "</clause>
454       </where>
455       </xml>";
457     if($this->connect()){
458       $this->o_sock->write($xml_msg);
459       $str = trim($this->o_sock->read());
461       /* Check if something went wrong while reading */
462       if($this->o_sock->is_error()){
463         $this->set_error($this->o_sock->get_error());
464         return($ret);
465       }
467       $entries = $this->xml_to_array($str);
468       if(isset($entries['XML']) && is_array($entries['XML'])){
469         foreach($entries['XML'] as $entry){
470           if(isset($entry['ID'])){
471             $ret[] = $entry['ID'];
472           }
473         }
474       }
475     }
476     return($ret);
477   }
480   /*! \brief  Returns an entry containing all requested ids.
481     @param  Array   The IDs of the entries we want to return.
482     @return Array   Of the requested entries. 
483    */
484   public function get_entries_by_mac($macs)
485   {
486     if(!is_array($macs)){
487       trigger_error("Requires an array as parameter.");
488       return;
489     }
490     $this->reset_error();
492     $ret = array();
494     $xml_msg = "<xml>
495       <header>gosa_query_jobdb</header>
496       <target>GOSA</target>
497       <source>GOSA</source>
498       <where>
499       <clause>
500       <connector>or</connector>";
501     foreach($macs as $mac){
502       $xml_msg .= "<phrase>
503         <operator>eq</operator>
504         <macaddress>".$mac."</macaddress>
505         </phrase>";
506     }
507     $xml_msg .= "</clause>
508       </where>
509       </xml>";
511     if($this->connect()){
512       $this->o_sock->write($xml_msg);
513       $str = trim($this->o_sock->read());
515       /* Check if something went wrong while reading */
516       if($this->o_sock->is_error()){
517         $this->set_error($this->o_sock->get_error());
518         return($ret);
519       }
521       $entries = $this->xml_to_array($str); 
522       if(isset($entries['XML'])){
523         foreach($entries['XML'] as $name => $entry){
524           if(preg_match("/^ANSWER[0-9]*$/",$name)){
525             $ret[$name] = $entry;
526           }
527         }
528       }
529     }
530     return($ret);
531   }
534   /*! \brief  Returns an entry containing all requested ids.
535     @param  Array   The IDs of the entries we want to return.
536     @return Array   Of the requested entries. 
537    */
538   public function get_entries_by_id($ids)
539   {
540     if(!is_array($ids)){
541       trigger_error("Requires an array as parameter.");
542       return;
543     }
544     $this->reset_error();
546     $ret = array();
548     $xml_msg = "<xml>
549       <header>gosa_query_jobdb</header>
550       <target>GOSA</target>
551       <source>GOSA</source>
552       <where>
553       <clause>
554       <connector>or</connector>";
555     foreach($ids as $id){
556       $xml_msg .= "<phrase>
557         <operator>eq</operator>
558         <id>".$id."</id>
559         </phrase>";
560     }
561     $xml_msg .= "</clause>
562       </where>
563       </xml>";
565     if($this->connect()){
566       $this->o_sock->write($xml_msg);
567       $str = trim($this->o_sock->read());
569       /* Check if something went wrong while reading */
570       if($this->o_sock->is_error()){
571         $this->set_error($this->o_sock->get_error());
572         return($ret);
573       }
575       $entries = $this->xml_to_array($str); 
576       if(isset($entries['XML'])){
577         foreach($entries['XML'] as $name => $entry){
578           if(preg_match("/^ANSWER[0-9]*$/",$name)){
579             $ret[$name] = $entry;
580           }
581         }
582       }
583     }
584     return($ret);
585   }
588   /*! \brief  Checks if the given id is in use.
589     @param  Integer The ID of the entry.
590     @return Boolean TRUE if entry exists. 
591    */
592   public function id_exists($id)
593   {
594     if(!is_numeric($id)){
595       trigger_error("Requires an integer as parameter.");
596       return;
597     }
599     $this->reset_error();
601     $xml_msg = "<xml>
602       <header>gosa_query_jobdb</header>
603       <target>GOSA</target>
604       <source>GOSA</source>
605       <where>
606       <clause>
607       <phrase>
608       <operator>eq</operator>
609       <id>".$id."</id>
610       </phrase>
611       </clause>
612       </where>
613       </xml>";
615     if($this->connect()){
616       $this->o_sock->write($xml_msg);
617       $str = trim($this->o_sock->read());
619       /* Check if something went wrong while reading */
620       if($this->o_sock->is_error()){
621         $this->set_error($this->o_sock->get_error());
622         return(FALSE);
623       }
625       $entries = $this->xml_to_array($str); 
626       if( isset($entries['XML']['HEADER']) && 
627           $entries['XML']['HEADER']=="answer" && 
628           isset($entries['XML']['ANSWER1'])){
629         return(TRUE);
630       }
631     }
632     return(FALSE);
633   }
636   /*! \brief  Returns an entry from the gosaSupportQueue
637     @param  Integer The ID of the entry we want to return.
638     @return Array   Of the requested entry. 
639    */
640   public function get_entry_by_id($id)
641   {
642     if(!is_numeric($id)){
643       trigger_error("Requires an integer as parameter.");
644       return;
645     }
646     $this->reset_error();
647   
648     $ret = array();
649     $xml_msg = "<xml>
650       <header>gosa_query_jobdb</header>
651       <target>GOSA</target>
652       <source>GOSA</source>
653       <where>
654       <clause>
655       <phrase>
656       <operator>eq</operator>
657       <id>".$id."</id>
658       </phrase>
659       </clause>
660       </where>
661       </xml>";
662     if($this->connect()){
663       $this->o_sock->write($xml_msg);
664       $str = trim($this->o_sock->read());
666       /* Check if something went wrong while reading */
667       if($this->o_sock->is_error()){
668         $this->set_error($this->o_sock->get_error());
669         return($ret);
670       }
672       $entries = $this->xml_to_array($str); 
673       if( isset($entries['XML']['HEADER']) &&
674           $entries['XML']['HEADER']=="answer" &&
675           isset($entries['XML']['ANSWER1'])){
676         $ret = $entries['XML']['ANSWER1'];
677       }
678     }
679     return($ret);
680   }
683   /*! \brief  Removes a set of entries from the GOsa support queue. 
684     @param  Array The IDs to remove.
685     @return Boolean True on success.
686    */
687   public function remove_entries($ids)
688   {
689     if(!is_array($ids)){
690       trigger_error("Requires an array as parameter.");
691       return;
692     }
694     $this->reset_error();
696     $ret = array();
698     $xml_msg = "<xml>
699       <header>gosa_delete_jobdb_entry</header>
700       <target>GOSA</target>
701       <source>GOSA</source>
702       <where>
703       <clause>
704       <connector>or</connector>";
705     foreach($ids as $id){
706       $xml_msg .= "<phrase>
707         <operator>eq</operator>
708         <id>".$id."</id>
709         </phrase>";
710     }
711     $xml_msg .= "</clause>
712       </where>
713       </xml>";
715     if($this->connect()){
716       $this->o_sock->write($xml_msg);
717       $str = $this->o_sock->read();
719       /* Check if something went wrong while reading */
720       if($this->o_sock->is_error()){
721         $this->set_error($this->o_sock->get_error());
722         return($ret);
723       }
725       $entries = $this->xml_to_array($str);
726       if(isset($entries['XML']) || isset($entries['COUNT'])){
727         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
728         return(TRUE);
729       }else{
730         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
731       }
732     }
733     return(FALSE);
734   }
738   /*! \brief  Removes an entry from the GOsa support queue. 
739     @param  Integer The ID of the entry we want to remove.
740     @return Boolean True on success.
741    */
742   public function remove_entry($id)
743   {
744     return($this->remove_entries(array($id)));
745   }
748   /*! \brief  Parses the given xml string into an array 
749     @param  String XML string  
750     @return Array Returns an array containing the xml structure. 
751    */
752   private function xml_to_array($xml)
753   {
754     $params = array();
755     $level = array();
756     $parser  = xml_parser_create_ns();
757     xml_parse_into_struct($parser, $xml, $vals, $index);
759     $err_id = xml_get_error_code($parser);
760     if($err_id){
761       xml_parser_free($parser);
762     }else{
763       xml_parser_free($parser);
765       foreach ($vals as $xml_elem) {
766         if ($xml_elem['type'] == 'open') {
767           if (array_key_exists('attributes',$xml_elem)) {
768             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
769           } else {
770             $level[$xml_elem['level']] = $xml_elem['tag'];
771           }
772         }
773         if ($xml_elem['type'] == 'complete') {
774           $start_level = 1;
775           $php_stmt = '$params';
776           while($start_level < $xml_elem['level']) {
777             $php_stmt .= '[$level['.$start_level.']]';
778             $start_level++;
779           }
780           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
781           @eval($php_stmt);
782         }
783       }
784     }
786     if(!isset($params['XML'])){
787       if (!array_key_exists('XML', $params)){
788         $this->set_error(_("Cannot not parse XML!"));
789       }
790       $params = array("COUNT" => 0);
791     }
793     return($params); 
794   }
797   /*! \brief  Updates an entry with a set of new values, 
798     @param  Integer The ID of the entry, we want to update.
799     @param  Array   The variables to update.   
800     @return Boolean Returns TRUE on success. 
801    */
802   public function update_entries($ids,$data)
803   {
804     $this->reset_error();
805     if(!is_array($ids)){
806       trigger_error("Requires an array as first parameter.");
807       return;
808     }
810     if(!is_array($data)){
811       trigger_error("Requires an array as second parameter.");
812       return;
813     }
815     $attr = "";
816     foreach($data as $key => $value){
817       if(is_array($value)){
818         foreach($value as $sub_value){
819           $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
820         }
821       }else{
822         $attr.= "<$key>".strtolower($value)."</$key>\n";
823       }
824     }
826     $xml_msg = "<xml>
827       <header>gosa_update_status_jobdb_entry</header>
828       <target>GOSA</target>
829       <source>GOSA</source>
830       <where>
831       <clause>
832       <connector>or</connector>";
833     foreach($ids as $id){
834       $xml_msg .= "<phrase>
835         <operator>eq</operator>
836         <id>".$id."</id>
837         </phrase>";
838     }
839     $xml_msg .= "</clause>
840       </where>
841       <update>
842       ".$attr." 
843       </update>
844       </xml>";
846     if($this->connect()){
848       $this->o_sock->write($xml_msg);
849       $str      = trim($this->o_sock->read());
851       /* Check if something went wrong while reading */
852       if($this->o_sock->is_error()){
853         $this->set_error($this->o_sock->get_error());
854         return(FALSE);
855       }
857       $entries = $this->xml_to_array($str);
858       if(isset($entries['XML'])){
859         if(isset($entries['XML']['ERROR_STRING'])) {
860           $this->set_error($entries['XML']['ERROR_STRING']);
861           new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
862           return(FALSE);
863         }
864         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
865         return(TRUE);
866       }
867     }
868     return(FALSE);
869   }
872   /*! \brief  Returns the number of currently queued objects.
873       @return Integer  
874    */
875   public function number_of_queued_entries()
876   {
877     $xml_msg ="<xml><header>gosa_count_jobdb</header><target>GOSA</target><source>GOSA</source></xml>";
878     $this->connect();
879     if($this->connect()){
880       $this->o_sock->write($xml_msg);
881       $str     = trim($this->o_sock->read());
883       /* Check if something went wrong while reading */
884       if($this->o_sock->is_error()){
885         $this->set_error($this->o_sock->get_error());
886         return(0);
887       }
889       $entries = $this->xml_to_array($str);
890       if(isset($entries['XML'])){
891         return($entries['XML']['COUNT']);
892       }
893     }
894     return(-1);
895   } 
898   public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
899   {
900     $xml_message= "";
902     /* Prepare data */
903     foreach ($data as $key => $value){
904       if(is_array($value)){
905         foreach($value as $sub_value){
906           $xml_message.= "<$key>$sub_value</$key>";
907         }
908       }else{
909         $xml_message.= "<$key>$value</$key>";
910       }
911     }
913     /* Multiple targets? */
914     if (!is_array($to)){
915       $to_targets= array($to);
916     } else {
917       $to_targets= $to;
918     }
920     /* Build target strings */
921     $target ="";
922     foreach($to_targets as $to){
923       $target.= "<target>$to</target>";
924     }
926     return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
927   }
930   /* Allows simply appending a new DaemonEvent 
931    */
932   public function append($event)
933   {
934     if(!($event instanceof DaemonEvent)){
935       return(FALSE);
936     }
937   
938     $this->reset_error();
940     /* Add to queue if new 
941      */
942     if($event->is_new()){
944       $request_answer = FALSE;
945       if($event->get_type() == SCHEDULED_EVENT){
946         $action = $event->get_schedule_action();
947       }elseif($event->get_type() == TRIGGERED_EVENT){
948         $action = $event->get_trigger_action();
949       }else{
950         trigger_error("Unknown type of queue event given.");
951         return(FALSE);
952       }
954       /* Get event informations, like targets..
955        */
956       $targets    = $event->get_targets();
957       $data       = $event->save();
959       /* Append an entry for each target 
960        */
961       foreach($targets as $target){
962         $data['macaddress'] = $target;
963         $this->send_data($action,$target,$data,$request_answer);
965         if($this->is_error()){
966           return(FALSE);
967         }
968       }
969       return(TRUE);
970     }else{
972       /* Updated edited entry.
973        */
974       $id                 = $event->get_id();
975       $data               = $event->save();
976       return($this->update_entries(array($id),$data));
977     }
979     return(FALSE);
980   }
983 /*! \brief  Returns an array containing all queued entries.
984     @return Array All queued entries as an array.
985    */
986   public function _send($data, $answer_expected= FALSE)
987   {
988     $this->reset_error();
989     $ret = array();
991     if($this->connect()){
992       $this->o_sock->write($data);
993       if ($answer_expected){
994         $str = trim($this->o_sock->read());
996         /* Check if something went wrong while reading */
997         if($this->o_sock->is_error()){
998           $this->set_error($this->o_sock->get_error());
999           return($ret);
1000         }
1002         $entries = $this->xml_to_array($str);
1003         if(isset($entries['XML']) && is_array($entries['XML'])){
1004           $ret = $entries;
1005           if(isset($entries['XML']['ERROR_STRING'])) {
1006             $this->set_error($entries['XML']['ERROR_STRING']);
1007             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1008           }else{
1009             new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1010           }
1011         }
1012       }else{
1013         new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1014       }
1015     }
1016     return($ret);
1017   }
1020   static function send($header, $to, $data= array(), $answer_expected = FALSE)
1021   {
1022     $xml_message= "";
1024     /* Get communication object */
1025     $d= new gosaSupportDaemon(TRUE,10);
1027     /* Prepare data */
1028     foreach ($data as $key => $value){
1029       if(is_array($value)){
1030         foreach($value as $sub_val){
1031           $xml_message.= "<$key>$sub_value</$key>";
1032         }
1033       }else{
1034         $xml_message.= "<$key>$value</$key>";
1035       }
1036     }
1038     /* Multiple targets? */
1039     if (!is_array($to)){
1040       $to_targets= array($to);
1041     } else {
1042       $to_targets= $to;
1043     }
1045     /* Build target strings */
1046     $target ="";
1047     foreach($to_targets as $to){
1048       $target.= "<target>$to</target>";
1049     }
1051     return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1052   }
1055   /*! \brief  Removes all jobs from the queue that are tiggered with a specific macAddress.
1056       @param  String  $mac  The mac address for which we want to remove all jobs.      
1057    */
1058   function clean_queue_from_mac($mac)
1059   {
1060     global $config;
1062     /* First of all we have to check which jobs are startet 
1063      *  for $mac 
1064      */
1065     $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";  
1066     
1067     new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1068  
1069     $data = $this->_send($xml_msg,TRUE);
1070     if(is_array($data) && isset($data['XML'])){
1071       $already_aborted = FALSE;
1072       foreach($data['XML']  as $name => $entry){
1073         if(preg_match("/answer[0-9]*/i",$name)){
1074           $entry['STATUS'] = strtoupper($entry['STATUS']);
1075           switch($entry['STATUS']){
1077             case 'PROCESSING' :
1079               /* Send abort event, but only once 
1080                */
1081               if($already_aborted){
1082                 break;
1083               }elseif(class_available("DaemonEvent_faireboot")){
1084                 $already_aborted = TRUE;
1085                 $tmp = new DaemonEvent_faireboot($config);
1086                 $tmp->add_targets(array($mac));
1087                 $tmp->set_type(TRIGGERED_EVENT);
1088                 if(!$this->append($tmp)){
1089                   msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1090                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1091                       "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1092                 }else{
1093                   new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1094                       "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1095                 }
1096                 ;break;
1097               }else{
1098                 /* Couldn't find abort event, just remove entry */
1099               }
1101             case 'WAITING':
1102             case 'ERROR':
1103             default :
1104             
1105               /* Simply remove entries from queue. 
1106                *  Failed or waiting events, can be removed without any trouble.
1107                */ 
1108               if(!$this->remove_entries(array($entry['ID']))){
1109                 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1110               }
1111               ;break;
1112           }
1113     
1114         }
1115       }
1116     }
1117   }
1120 static function ping($target)
1122   if (tests::is_mac($target)){
1123     /* Get communication object */
1124     $d= new gosaSupportDaemon(TRUE,0.5);
1125     $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1126     return (count($answer) ? TRUE:FALSE);
1127   }
1129   return (FALSE);
1134 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1135 ?>