Code

b3b66fd7864a182b84eecbf154fabb7dd9dd2b85
[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($from=0,$to=10)
93   {
94     $this->b_error = FALSE;
95     $this->s_error = "";
98     $xml_msg = "<xml>
99                   <header>gosa_query_jobdb</header>
100                   <where>
101                     <clause>
102                       <connector>and</connector>
103                       <phrase>
104                         <operator>gt</operator>
105                         <ROWID>".$from."</ROWID>
106                       </phrase>
107                       <phrase>
108                         <operator>lt</operator>
109                         <ROWID>".$to."</ROWID>
110                       </phrase>
111                     </clause>
112                   </where>
113                 </xml>";
115     $this->connect();
116     if($this->is_connected){
117       $this->o_sock->write($xml_msg);
118       $str = trim($this->o_sock->read());
119       $entries = $this->xml_to_array($str);
121       if(!array_key_exists("XML",$entries)){
122         $this->set_error("!!!Couldn't parse xml.");
123         $this->disconnect();
124         return;
125       }else{
126         if(!is_array($entries['XML'])) {
127           $ret = array();
128         }else{
129           $ret = $entries['XML']; 
130         }
131       }
132       return($ret);
133     }
134     $this->set_error("Could not establish socket connection.");
135     $this->disconnect();
136     return;
137   }
140   /*! \brief  Checks if the given id is in use.
141       @param  Integer The ID of the entry.
142       @return Boolean TRUE if entry exists. 
143    */
144   public function id_exists($id)
145   {
146     if(!is_numeric($id)){
147       trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
148       return;
149     }
151     $this->b_error = FALSE;
152     $this->s_error = "";
153     $xml_msg = "<xml>
154                   <header>gosa_query_jobdb</header>
155                   <where>
156                     <clause>
157                       <phrase>
158                         <operator>eq</operator>
159                         <id>".$id."</id>
160                       </phrase>
161                     </clause>
162                   </where>
163                 </xml>";
164     $this->connect();
165     if(!$this->is_connected){
166       $this->set_error("Could not establish socket connection.");
167     }else{
168       $this->o_sock->write($xml_msg);
169       $str = trim($this->o_sock->read());
170       $entries = $this->xml_to_array($str); 
171       if(isset($entries['XML']['ANSWER1'])){
172         $this->disconnect();
173         return(TRUE);
174       }
175     }
176     $this->disconnect();
177     return(FALSE);
178   }
180   
181   /*! \brief  Returns an entry from the gosaSupportQueue
182       @param  Integer The ID of the entry we want to return.
183       @return Array   Of the requested entry. 
184    */
185   public function get_entry($id)
186   {
187     if(!is_numeric($id)){
188       trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
189       return;
190     }
192     $this->b_error = FALSE;
193     $this->s_error = "";
194     $xml_msg = "<xml>
195                   <header>gosa_query_jobdb</header>
196                   <where>
197                     <clause>
198                       <phrase>
199                         <operator>eq</operator>
200                         <id>".$id."</id>
201                       </phrase>
202                     </clause>
203                   </where>
204                 </xml>";
205     $this->connect();
206     if(!$this->is_connected){
207       $this->set_error("Could not establish socket connection.");
208     }else{
209       $this->o_sock->write($xml_msg);
210       $str = trim($this->o_sock->read());
211       $entries = $this->xml_to_array($str); 
212       if(!isset($entries['XML']['ANSWER1'])){
213         $this->set_error("Entry with id (".$id.") not found.");
214         $this->disconnect();
215       }else{
216         $ret = $entries['XML']['ANSWER1'];
217         return($ret);
218       }
219     }
220     return;
221   }
224   /*! \brief  Removes an entry from the GOsa support queue. 
225       @param  Integer The ID of the entry we want to remove.
226       @return Boolean True on success.
227    */
228   public function remove_entry($id)
229   {
230     $this->b_error = FALSE;
231     $this->s_error = "";
233     $xml_msg = "<xml>
234                   <header>gosa_delete_jobdb_entry</header>
235                   <where>
236                     <clause>
237                       <phrase>
238                         <operator>eq</operator>
239                         <id>".$id."</id>
240                       </phrase>
241                     </clause>
242                   </where>
243                 </xml>";
244     $this->connect();
245     if($this->is_connected){
246       $this->o_sock->write($xml_msg);
247       return(TRUE);
248     }
249     $this->set_error("Could not establish socket connection.");
250     return(FALSE);
251   }
252   
254   /*! \brief  Parses the given xml string into an array 
255       @param  String XML string  
256       @return Array Returns an array containing the xml structure. 
257    */
258   function xml_to_array($xml)
259   {
260     $params = array();
261     $level = array();
262     $parser  = xml_parser_create_ns();
263     xml_parse_into_struct($parser, $xml, $vals, $index);
265     $err_id = xml_get_error_code($parser);
266     if($err_id){
267       $this->set_error(xml_error_string(xml_get_error_code($parser)));
268       xml_parser_free($parser);
269     }else{
270       xml_parser_free($parser);
272       foreach ($vals as $xml_elem) {
273         if ($xml_elem['type'] == 'open') {
274           if (array_key_exists('attributes',$xml_elem)) {
275             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
276           } else {
277             $level[$xml_elem['level']] = $xml_elem['tag'];
278           }
279         }
280         if ($xml_elem['type'] == 'complete') {
281           $start_level = 1;
282           $php_stmt = '$params';
283           while($start_level < $xml_elem['level']) {
284             $php_stmt .= '[$level['.$start_level.']]';
285             $start_level++;
286           }
287           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
288           @eval($php_stmt);
289         }
290       }
291     }
292     return($params); 
293   }
296   /*! \brief  Updates an entry with a set of new values, 
297       @param  Integer The ID of the entry, we want to update.
298       @param  Array   The variables to update.   
299       @return Boolean Returns TRUE on success. 
300    */
301   public function update_entry($id,$entry)
302   {
303     $this->b_error = FALSE;
304     $this->s_error = "";
305     if(!is_numeric($id)){
306       trigger_error("Requires an integer value as ID parameter.");
307       return;
308     }
310     if(!is_array($entry)){
311       trigger_error("Requires an array as second parameter.");
312       return;
313     }
315     $attr = "";
316     foreach($entry as $name => $entry){
317       $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
318     }
320     $xml_msg = "<xml> 
321                   <header>gosa_update_status_jobdb_entry</header>
322                   <where>
323                     <id>".$id."</id> 
324                   </where>
325                   <update>
326                     ".$attr." 
327                   </update>
328                 </xml>";
329     $this->connect();
330     if($this->is_connected){
331       $this->o_sock->write($xml_msg);
332       $str      = trim($this->o_sock->read());
333       $entries = $this->xml_to_array($str);
334       if(!empty($str)){
335         return(TRUE);
336       }
337       return(FALSE);
338     }
339     $this->set_error("Could not establish socket connection.");
340     return(FALSE);
341   }
344 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
345 ?>