Code

Updated sorting.
[gosa.git] / gosa-core / include / class_gosaSupportDaemon.inc
1 <?php
4 /*
5    __construct        - Create a new deamon handle. 
6    connect            - Connect to deamon socket.
7    disconnect         - Disconnect from socket.
8    set_error          - Sets a new error.
9    is_error           - Returns TRUE if there was an error.
10    get_error          - Returns the last error or "".
11    get_queued_entries - Returns all queued entries, with limitations.
12    ids_exist          - Checks if the given id exists.
13    get_entries_by_id  - Returns a set of entries.
14    id_exists          - Checks if a set entries exists.
15    get_entry_by_id    - Returns a single entry.
16    remove_entries     - Remove a set of entries.
17    remove_entry       - Removes a single entry.
18    update_entries     - Updates a set of entries.
19    xml_to_array       - XML to Array. 
20 */
24 class gosaSupportDaemon
25 {
26   private $o_sock       = NULL;
27   private $s_host       = "";
28   private $i_port       = 0;
29   private $f_timeout    = 0.2;
31   private $is_connected     = FALSE;
32   private $s_encryption_key = "";
34   private $s_error  = "";
35   private $b_error  = FALSE;
38   /*! \brief  Creates a new gosaSupportDaemon object.
39     @param string   Host    The Host where the deamon is running on.  
40     @param integer  Port    The port which the deamon use.
41     @param string   Key     The encryption string.
42     @param boolean  Connect Directly connect to deamon socket.
43     @param float    Timeout The timelimit for all socket actions.
44    */
45   public function __construct($host,$port,$key="secret-gosa-password",$connect=TRUE,$timeout=0.2)
46   {
47     $this->s_host    = $host;
48     $this->i_port    = $port;
49     $this->f_timeout = $timeout;
50     $this->s_encryption_key = $key;
51     if($connect){
52       $this->connect();
53     }
54   }
57   /*! \brief  Establish deamon connection. 
58     @return boolean Returns true if the connection was succesfully established. 
59    */
60   public function connect()
61   {
62     $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
63     if($this->o_sock->connected()){ 
64       $this->o_sock->setEncryptionKey($this->s_encryption_key); 
65       $this->is_connected = TRUE;
66     }else{
67       $this->error = $this->o_sock->get_error();
68       $this->disconnect();
69     }
70     return($this->is_connected);
71   }
74   /*! \brief  Disconnect from gosa deamon.
75    */
76   public function disconnect()
77   {
78     $this->o_sock->close();
79     $this->is_connected = FALSE;
80   }
83   /*! \brief  Sets an error message, which can be returned with get_error().
84     @param  string  The Error message,
85    */
86   private function set_error($str)
87   {
88     $this->b_error = TRUE;
89     $this->s_error = $str;
90   }
93   /*! \brief  Checks if an error occured.
94     @return boolean returns TRUE or FALSE, whether there is an error or not.
95    */
96   public function is_error()
97   {
98     return($this->b_error);
99   }
102   /*! \brief  Returns the last error. 
103     @return Returns the last error.
104    */
105   public function get_error()
106   {
107     return($this->s_error);
108   }
111   /*! \brief  Returns an array containing all queued entries.
112     @return Array All queued entries as an array.
113    */
114   public function get_queued_entries($from=0,$to=10,$sort="timestamp DESC")
115   {
116     $this->b_error = FALSE;
117     $this->s_error = "";
118     $ret = array();
120     $xml_msg = "<xml>
121       <header>gosa_query_jobdb</header>
122       <where>
123       <clause>
124         <phrase>
125         <operator>ne</operator>
126         <HEADERTAG>*</HEADERTAG>
127         </phrase>
128       </clause>
129       </where>
130       <orderby>".$sort."</orderby>
131       <limit>
132       <from>".$from."</from>
133       <to>".$to."</to>
134       </limit>
135       </xml>";
137     if($this->connect()){
138       $this->o_sock->write($xml_msg);
139       $str = trim($this->o_sock->read());
140       $entries = $this->xml_to_array($str);
141       if(isset($entries['XML']) && is_array($entries['XML'])){
142         $ret = $entries; 
143       }
144     }
145     return($ret);
146   }
148   /*! \brief  Checks if the given ids are used queue ids.
149     @param  Array   The ids we want to check..
150     @return Array   An array containing all ids as index and TRUE/FALSE as value. 
151    */
152   public function ids_exist($ids)
153   {
154     if(!is_array($ids)){
155       trigger_error("Requires an array as parameter.");
156       return;
157     }
158     $this->b_error = FALSE;
159     $this->s_error = "";
161     $ret = array();
163     $xml_msg = "<xml>
164       <header>gosa_query_jobdb</header>
165       <where>
166       <clause>
167       <connector>or</connector>";
168     foreach($ids as $id){
169       $xml_msg .= "<phrase>
170         <operator>eq</operator>
171         <id>".$id."</id>
172         </phrase>";
173     }
174     $xml_msg .= "</clause>
175       </where>
176       </xml>";
178     if($this->connect()){
179       $this->o_sock->write($xml_msg);
180       $str = trim($this->o_sock->read());
181       $entries = $this->xml_to_array($str);
182       if(isset($entries['XML']) && is_array($entries['XML'])){
183         foreach($entries['XML'] as $entry){
184           $ret[] = $entry['ID'];
185         }
186       }
187     }
188     return($ret);
189   }
192   /*! \brief  Returns an entry containing all requested ids.
193     @param  Array   The IDs of the entries we want to return.
194     @return Array   Of the requested entries. 
195    */
196   public function get_entries_by_id($ids)
197   {
198     if(!is_array($ids)){
199       trigger_error("Requires an array as parameter.");
200       return;
201     }
202     $this->b_error = FALSE;
203     $this->s_error = "";
205     $ret = array();
207     $xml_msg = "<xml>
208       <header>gosa_query_jobdb</header>
209       <where>
210       <clause>
211       <connector>or</connector>";
212     foreach($ids as $id){
213       $xml_msg .= "<phrase>
214         <operator>eq</operator>
215         <id>".$id."</id>
216         </phrase>";
217       $ret[$id] = FALSE;
218     }
219     $xml_msg .= "</clause>
220       </where>
221       </xml>";
223     if($this->connect()){
224       $this->o_sock->write($xml_msg);
225       $str = trim($this->o_sock->read());
226       $entries = $this->xml_to_array($str); 
227       if(isset($entries['XML'])){
228         $ret = $entries['XML'];
229       }
230     }
231     return($ret);
232   }
235   /*! \brief  Checks if the given id is in use.
236     @param  Integer The ID of the entry.
237     @return Boolean TRUE if entry exists. 
238    */
239   public function id_exists($id)
240   {
241     if(!is_numeric($id)){
242       trigger_error("Requires an integer as parameter.");
243       return;
244     }
246     $this->b_error = FALSE;
247     $this->s_error = "";
248     $xml_msg = "<xml>
249       <header>gosa_query_jobdb</header>
250       <where>
251       <clause>
252       <phrase>
253       <operator>eq</operator>
254       <id>".$id."</id>
255       </phrase>
256       </clause>
257       </where>
258       </xml>";
260     if($this->connect()){
261       $this->o_sock->write($xml_msg);
262       $str = trim($this->o_sock->read());
263       $entries = $this->xml_to_array($str); 
264       if(isset($entries['XML']['ANSWER1'])){
265         return(TRUE);
266       }
267     }
268     return(FALSE);
269   }
272   /*! \brief  Returns an entry from the gosaSupportQueue
273     @param  Integer The ID of the entry we want to return.
274     @return Array   Of the requested entry. 
275    */
276   public function get_entry_by_id($id)
277   {
278     if(!is_numeric($id)){
279       trigger_error("Requires an integer as parameter.");
280       return;
281     }
283     $this->b_error = FALSE;
284     $this->s_error = "";
285     $ret = array();
286     $xml_msg = "<xml>
287       <header>gosa_query_jobdb</header>
288       <where>
289       <clause>
290       <phrase>
291       <operator>eq</operator>
292       <id>".$id."</id>
293       </phrase>
294       </clause>
295       </where>
296       </xml>";
297     if($this->connect()){
298       $this->o_sock->write($xml_msg);
299       $str = trim($this->o_sock->read());
300       $entries = $this->xml_to_array($str); 
301       if(!isset($entries['XML']['ANSWER1'])){
302         $ret = $entries['XML']['ANSWER1'];
303       }
304     }
305     return($ret);
306   }
309   /*! \brief  Removes a set of entries from the GOsa support queue. 
310     @param  Array The IDs to remove.
311     @return Boolean True on success.
312    */
313   public function remove_entries($ids)
314   {
315     if(!is_array($ids)){
316       trigger_error("Requires an array as parameter.");
317       return;
318     }
319     $this->b_error = FALSE;
320     $this->s_error = "";
322     $ret = array();
324     $xml_msg = "<xml>
325       <header>gosa_delete_jobdb_entry</header>
326       <where>
327       <clause>
328       <connector>or</connector>";
329     foreach($ids as $id){
330       $xml_msg .= "<phrase>
331         <operator>eq</operator>
332         <id>".$id."</id>
333         </phrase>";
334     }
335     $xml_msg .= "</clause>
336       </where>
337       </xml>";
338     $this->b_error = FALSE;
339     $this->s_error = "";
341     if($this->connect()){
342       $this->o_sock->write($xml_msg);
343       $str = $this->o_sock->read();
344       $entries = $this->xml_to_array($str);
345       if(isset($entries['XML'])){
346         return(TRUE);
347       }
348     }
349     return(FALSE);
350   }
354   /*! \brief  Removes an entry from the GOsa support queue. 
355     @param  Integer The ID of the entry we want to remove.
356     @return Boolean True on success.
357    */
358   public function remove_entry($id)
359   {
360     $this->b_error = FALSE;
361     $this->s_error = "";
363     $xml_msg = "<xml>
364       <header>gosa_delete_jobdb_entry</header>
365       <where>
366       <clause>
367       <phrase>
368       <operator>eq</operator>
369       <id>".$id."</id>
370       </phrase>
371       </clause>
372       </where>
373       </xml>";
374     if($this->connect()){
375       $this->o_sock->write($xml_msg);
376       $str = $this->o_sock->read();
377       $entries = $this->xml_to_array($str);
378       if(isset($entries['XML'])){
379         return(TRUE);
380       }
381     }
382     return(FALSE);
383   }
386   /*! \brief  Parses the given xml string into an array 
387     @param  String XML string  
388     @return Array Returns an array containing the xml structure. 
389    */
390   function xml_to_array($xml)
391   {
392     $params = array();
393     $level = array();
394     $parser  = xml_parser_create_ns();
395     xml_parse_into_struct($parser, $xml, $vals, $index);
397     $err_id = xml_get_error_code($parser);
398     if($err_id){
399       xml_parser_free($parser);
400     }else{
401       xml_parser_free($parser);
403       foreach ($vals as $xml_elem) {
404         if ($xml_elem['type'] == 'open') {
405           if (array_key_exists('attributes',$xml_elem)) {
406             list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
407           } else {
408             $level[$xml_elem['level']] = $xml_elem['tag'];
409           }
410         }
411         if ($xml_elem['type'] == 'complete') {
412           $start_level = 1;
413           $php_stmt = '$params';
414           while($start_level < $xml_elem['level']) {
415             $php_stmt .= '[$level['.$start_level.']]';
416             $start_level++;
417           }
418           $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
419           @eval($php_stmt);
420         }
421       }
422     }
423     if(!isset($params['XML'])){
424       $this->set_error(_("Could not parse XML."));
425       $params = array();
426     }
427     return($params); 
428   }
431   /*! \brief  Updates an entry with a set of new values, 
432     @param  Integer The ID of the entry, we want to update.
433     @param  Array   The variables to update.   
434     @return Boolean Returns TRUE on success. 
435    */
436   public function update_entries($ids,$entry)
437   {
438     $this->b_error = FALSE;
439     $this->s_error = "";
440     if(!is_array($ids)){
441       trigger_error("Requires an array as first parameter.");
442       return;
443     }
445     if(!is_array($entry)){
446       trigger_error("Requires an array as second parameter.");
447       return;
448     }
450     $attr = "";
451     foreach($entry as $name => $entry){
452       $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
453     }
454     $xml_msg = "<xml>
455       <header>gosa_update_status_jobdb_entry</header>
456       <where>
457       <clause>
458       <connector>or</connector>";
459     foreach($ids as $id){
460       $xml_msg .= "<phrase>
461         <operator>eq</operator>
462         <id>".$id."</id>
463         </phrase>";
464     }
465     $xml_msg .= "</clause>
466       </where>
467       <update>
468       ".$attr." 
469       </update>
470       </xml>";
471     if($this->connect()){
472       $this->o_sock->write($xml_msg);
473       $str      = trim($this->o_sock->read());
474       $entries = $this->xml_to_array($str);
475       if(isset($entries['XML'])){
476         return(TRUE);
477       }
478     }
479     return(FALSE);
480   }
483   /*! \brief  Updates an entry with a set of new values, 
484     @param  Integer The ID of the entry, we want to update.
485     @param  Array   The variables to update.   
486     @return Boolean Returns TRUE on success. 
487    */
488   public function number_of_queued_entries()
489   {
490     $xml_msg ="<xml> <header>gosa_count_jobdb</header></xml>";
491     $this->connect();
492     if($this->connect()){
493       $this->o_sock->write($xml_msg);
494       $str     = trim($this->o_sock->read());
495       $entries = $this->xml_to_array($str);
496       if(isset($entries['XML'])){
497         return($entries['XML']['COUNT']);
498       }
499     }
500     return(-1);
501   } 
504 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
505 ?>