Code

Updated gosaServerDeamon
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
1 <?php
3 class gosaSupportDaemon
4 {
5   private $o_sock       = NULL;
6   private $s_host       = "";
7   private $i_port       = 0;
8   private $f_timeout    = 0.2;
10   private $is_connected     = FALSE;
11   private $s_encryption_key = "";
13   private $s_error  = "";
14   private $b_error  = FALSE;
17   /*! \brief  Creates a new gosaSupportDaemon object.
18       @param string   Host    The Host where the deamon is running on.  
19       @param integer  Port    The port which the deamon use.
20       @param string   Key     The encryption string.
21       @param boolean  Connect Directly connect to deamon socket.
22       @param float    Timeout The timelimit for all socket actions.
23    */
24   public function __construct($host,$port,$key="secret-gosa-password",$connect=TRUE,$timeout=0.2)
25   {
26     $this->s_host    = $host;
27     $this->i_port    = $port;
28     $this->f_timeout = $timeout;
29     $this->s_encryption_key = $key;
30     if($connect){
31       $this->connect();
32     }
33   }
36   /*! \brief  Establish deamon connection. 
37       @return boolean Returns true if the connection was succesfully established. 
38    */
39   public function connect()
40   {
41     $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
42     if($this->o_sock->connected()){ 
43       $this->o_sock->setEncryptionKey($this->s_encryption_key); 
44       $this->is_connected = TRUE;
45     }else{
46       $this->error = $this->o_sock->get_error();
47       $this->is_connected = FALSE;
48     }
49   }
52   /*! \brief  Disconnect from gosa deamon.
53    */
54   public function disconnect()
55   {
56     $this->o_sock->close();
57     $this->is_connected = FALSE;
58   }
61   /*! \brief  Sets an error message, which can be returned with get_error().
62       @param  string  The Error message,
63    */
64   private function set_error($str)
65   {
66     $this->b_error = TRUE;
67     $this->s_error = $str;
68   }
71   /*! \brief  Checks if an error occured.
72       @return boolean returns TRUE or FALSE, whether there is an error or not.
73    */
74   public function is_error()
75   {
76     return($this->b_error);
77   }
80   /*! \brief  Returns the last error. 
81       @return Returns the last error.
82    */
83   public function get_error()
84   {
85     return($this->s_error);
86   }
89   /*! \brief  Returns an array containing all queued entries.
90       @return Array All queued entries as an array.
91    */
92   public function get_queued_entries()
93   {
94     $this->b_error = FALSE;
95     $this->s_error = "";
97     $xml_msg = "<xml><header>gosa_query_jobdb</header><where><status>*</status></where></xml>";
98     $this->connect();
99     if($this->is_connected){
100       $this->o_sock->write($xml_msg);
101       $str = trim($this->o_sock->read());
102       $entries = $this->xml_to_array($str);
104       if(!array_key_exists("XML",$entries)){
105         $this->set_error("!!!Couldn't parse xml.");
106         $this->disconnect();
107         return;
108       }else{
109         if(!is_array($entries['XML'])) {
110           $ret = array();
111         }else{
112           $ret = $entries['XML']; 
113         }
114       }
115       return($ret);
116     }
117     $this->set_error("Could not establish socket connection.");
118     $this->disconnect();
119     return;
120   }
123   /*! \brief  Checks if the given id is in use.
124       @param  Integer The ID of the entry.
125       @return Boolean TRUE if entry exists. 
126    */
127   public function id_exists($id)
128   {
129     if(!is_numeric($id)){
130       trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
131       return;
132     }
134     $this->b_error = FALSE;
135     $this->s_error = "";
136     $xml_msg = "<xml>
137                   <header>gosa_query_jobdb</header>
138                   <where>id</where>
139                   <id>".$id."</id>
140                 </xml>";
141     $this->connect();
142     if(!$this->is_connected){
143       $this->set_error("Could not establish socket connection.");
144     }else{
145       $this->o_sock->write($xml_msg);
146       $str = trim($this->o_sock->read());
147       $entries = $this->xml_to_array($str); 
148       if(isset($entries['XML']['ANSWER1'])){
149         $this->disconnect();
150         return(TRUE);
151       }
152     }
153     $this->disconnect();
154     return(FALSE);
155   }
157   
158   /*! \brief  Returns an entry from the gosaSupportQueue
159       @param  Integer The ID of the entry we want to return.
160       @return Array   Of the requested entry. 
161    */
162   public function get_entry($id)
163   {
164     if(!is_numeric($id)){
165       trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
166       return;
167     }
169     $this->b_error = FALSE;
170     $this->s_error = "";
171     $xml_msg = "<xml>
172                   <header>gosa_query_jobdb</header>
173                   <where>id</where>
174                   <id>".$id."</id>
175                 </xml>";
176     $this->connect();
177     if(!$this->is_connected){
178       $this->set_error("Could not establish socket connection.");
179     }else{
180       $this->o_sock->write($xml_msg);
181       $str = trim($this->o_sock->read());
182       $entries = $this->xml_to_array($str); 
183       if(!isset($entries['XML']['ANSWER1'])){
184         $this->set_error("Entry with id (".$id.") not found.");
185         $this->disconnect();
186       }else{
187         $ret = $entries['XML']['ANSWER1'];
188         return($ret);
189       }
190     }
191     return;
192   }
195   /*! \brief  Removes an entry from the GOsa support queue. 
196       @param  Integer The ID of the entry we want to remove.
197       @return Boolean True on success.
198    */
199   public function remove_entry($id)
200   {
201     $this->b_error = FALSE;
202     $this->s_error = "";
204     $xml_msg = "<xml>
205                   <header>gosa_delete_jobdb_entry</header>
206                   <where>id</where>
207                   <id>".$id."</id>
208                 </xml>";
209     $this->connect();
210     if($this->is_connected){
211       $this->o_sock->write($xml_msg);
212       return(TRUE);
213     }
214     $this->set_error("Could not establish socket connection.");
215     return(FALSE);
216   }
217   
219   /*! \brief  Parses the given xml string into an array 
220       @param  String XML string  
221       @return Array Returns an array containing the xml structure. 
222    */
223   function xml_to_array($xml)
224   {
225     $params = array();
226     $level = array();
227     $parser  = xml_parser_create_ns();
228     xml_parse_into_struct($parser, $xml, $vals, $index);
230     $err_id = xml_get_error_code($parser);
231     if($err_id){
232       $this->set_error(xml_error_string(xml_get_error_code($parser)));
233       xml_parser_free($parser);
234     }else{
235       xml_parser_free($parser);
237       foreach ($vals as $xml_elem) {
238         if ($xml_elem['type'] == 'open') {
239           if (array_key_exists('attributes',$xml_elem)) {
240             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
241           } else {
242             $level[$xml_elem['level']] = $xml_elem['tag'];
243           }
244         }
245         if ($xml_elem['type'] == 'complete') {
246           $start_level = 1;
247           $php_stmt = '$params';
248           while($start_level < $xml_elem['level']) {
249             $php_stmt .= '[$level['.$start_level.']]';
250             $start_level++;
251           }
252           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
253           @eval($php_stmt);
254         }
255       }
256     }
257     return($params); 
258   }
261   /*! \brief  Updates an entry with a set of new values, 
262       @param  Integer The ID of the entry, we want to update.
263       @param  Array   The variables to update.   
264       @return Boolean Returns TRUE on success. 
265    */
266   public function update_entry($id,$entry)
267   {
268     $this->b_error = FALSE;
269     $this->s_error = "";
270     if(!is_numeric($id)){
271       trigger_error("Requires an integer value as ID parameter.");
272       return;
273     }
275     if(!is_array($entry)){
276       trigger_error("Requires an array as second parameter.");
277       return;
278     }
280     $attr = "";
281     foreach($entry as $name => $entry){
282       $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
283     }
285     $xml_msg = "<xml> 
286                   <header>gosa_update_status_jobdb_entry</header>
287                   <where>
288                     <id>".$id."</id> 
289                   </where>
290                   <update>
291                     ".$attr." 
292                   </update>
293                 </xml>";
294     $this->connect();
295     if($this->is_connected){
296       $this->o_sock->write($xml_msg);
297       $str      = trim($this->o_sock->read());
298       $entries = $this->xml_to_array($str);
299       if(!empty($str)){
300         return(TRUE);
301       }
302       return(FALSE);
303     }
304     $this->set_error("Could not establish socket connection.");
305     return(FALSE);
306   }
309 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
310 ?>