7c15430ecab66becf5fc9c60e0d44fd3744b2d89
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>-1</ROWID>
106 </phrase>
107 </clause>
108 </where>
109 <limit>
110 <from>".$from."</from>
111 <to>".$to."</to>
112 </limit>
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 }
139 /*! \brief Checks if the given ids are used queue ids.
140 @param Array The ids we want to check..
141 @return Array An array containing all ids as index and TRUE/FALSE as value.
142 */
143 public function ids_exist($ids)
144 {
145 if(!is_array($ids)){
146 trigger_error("Requires an array as parameter.");
147 return;
148 }
149 $this->b_error = FALSE;
150 $this->s_error = "";
152 $ret = array();
154 $xml_msg = "<xml>
155 <header>gosa_query_jobdb</header>
156 <where>
157 <clause>
158 <connector>or</connector>";
159 foreach($ids as $id){
160 $xml_msg .= "<phrase>
161 <operator>eq</operator>
162 <id>".$id."</id>
163 </phrase>";
164 }
165 $xml_msg .= "</clause>
166 </where>
167 </xml>";
169 $this->connect();
170 if(!$this->is_connected){
171 $this->set_error("Could not establish socket connection.");
172 }else{
173 $this->o_sock->write($xml_msg);
174 $str = trim($this->o_sock->read());
175 $entries = $this->xml_to_array($str);
176 if(isset($entries['XML'])){
177 foreach($entries['XML'] as $entry){
178 $ret[] = $entry['ID'];
179 }
180 $this->disconnect();
181 return($ret);
182 }
183 }
184 $this->disconnect();
185 return(FALSE);
186 }
189 /*! \brief Returns an entry containing all requested ids.
190 @param Array The IDs of the entries we want to return.
191 @return Array Of the requested entries.
192 */
193 public function get_entries_by_id($ids)
194 {
195 if(!is_array($ids)){
196 trigger_error("Requires an array as parameter.");
197 return;
198 }
199 $this->b_error = FALSE;
200 $this->s_error = "";
202 $ret = array();
204 $xml_msg = "<xml>
205 <header>gosa_query_jobdb</header>
206 <where>
207 <clause>
208 <connector>or</connector>";
209 foreach($ids as $id){
210 $xml_msg .= "<phrase>
211 <operator>eq</operator>
212 <id>".$id."</id>
213 </phrase>";
214 $ret[$id] = FALSE;
215 }
216 $xml_msg .= "</clause>
217 </where>
218 </xml>";
220 $this->connect();
221 if(!$this->is_connected){
222 $this->set_error("Could not establish socket connection.");
223 }else{
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 $this->set_error("Entry with id (".$id.") not found.");
229 $this->disconnect();
230 }else{
231 $ret = $entries['XML'];
232 return($ret);
233 }
234 }
235 return;
236 }
239 /*! \brief Checks if the given id is in use.
240 @param Integer The ID of the entry.
241 @return Boolean TRUE if entry exists.
242 */
243 public function id_exists($id)
244 {
245 if(!is_numeric($id)){
246 trigger_error("Requires an integer as parameter.");
247 return;
248 }
250 $this->b_error = FALSE;
251 $this->s_error = "";
252 $xml_msg = "<xml>
253 <header>gosa_query_jobdb</header>
254 <where>
255 <clause>
256 <phrase>
257 <operator>eq</operator>
258 <id>".$id."</id>
259 </phrase>
260 </clause>
261 </where>
262 </xml>";
263 $this->connect();
264 if(!$this->is_connected){
265 $this->set_error("Could not establish socket connection.");
266 }else{
267 $this->o_sock->write($xml_msg);
268 $str = trim($this->o_sock->read());
269 $entries = $this->xml_to_array($str);
270 if(isset($entries['XML']['ANSWER1'])){
271 $this->disconnect();
272 return(TRUE);
273 }
274 }
275 $this->disconnect();
276 return(FALSE);
277 }
280 /*! \brief Returns an entry from the gosaSupportQueue
281 @param Integer The ID of the entry we want to return.
282 @return Array Of the requested entry.
283 */
284 public function get_entry_by_id($id)
285 {
286 if(!is_numeric($id)){
287 trigger_error("Requires an integer as parameter.");
288 return;
289 }
291 $this->b_error = FALSE;
292 $this->s_error = "";
293 $xml_msg = "<xml>
294 <header>gosa_query_jobdb</header>
295 <where>
296 <clause>
297 <phrase>
298 <operator>eq</operator>
299 <id>".$id."</id>
300 </phrase>
301 </clause>
302 </where>
303 </xml>";
304 $this->connect();
305 if(!$this->is_connected){
306 $this->set_error("Could not establish socket connection.");
307 }else{
308 $this->o_sock->write($xml_msg);
309 $str = trim($this->o_sock->read());
310 $entries = $this->xml_to_array($str);
311 if(!isset($entries['XML']['ANSWER1'])){
312 $this->set_error("Entry with id (".$id.") not found.");
313 $this->disconnect();
314 }else{
315 $ret = $entries['XML']['ANSWER1'];
316 return($ret);
317 }
318 }
319 return;
320 }
323 /*! \brief Removes a set of entries from the GOsa support queue.
324 @param Array The IDs to remove.
325 @return Boolean True on success.
326 */
327 public function remove_entries($ids)
328 {
329 if(!is_array($ids)){
330 trigger_error("Requires an array as parameter.");
331 return;
332 }
333 $this->b_error = FALSE;
334 $this->s_error = "";
336 $ret = array();
338 $xml_msg = "<xml>
339 <header>gosa_delete_jobdb_entry</header>
340 <where>
341 <clause>
342 <connector>or</connector>";
343 foreach($ids as $id){
344 $xml_msg .= "<phrase>
345 <operator>eq</operator>
346 <id>".$id."</id>
347 </phrase>";
348 }
349 $xml_msg .= "</clause>
350 </where>
351 </xml>";
352 $this->b_error = FALSE;
353 $this->s_error = "";
355 $this->connect();
356 if($this->is_connected){
357 $this->o_sock->write($xml_msg);
358 return(TRUE);
359 }
360 $this->set_error("Could not establish socket connection.");
361 return(FALSE);
362 }
366 /*! \brief Removes an entry from the GOsa support queue.
367 @param Integer The ID of the entry we want to remove.
368 @return Boolean True on success.
369 */
370 public function remove_entry($id)
371 {
372 $this->b_error = FALSE;
373 $this->s_error = "";
375 $xml_msg = "<xml>
376 <header>gosa_delete_jobdb_entry</header>
377 <where>
378 <clause>
379 <phrase>
380 <operator>eq</operator>
381 <id>".$id."</id>
382 </phrase>
383 </clause>
384 </where>
385 </xml>";
386 $this->connect();
387 if($this->is_connected){
388 $this->o_sock->write($xml_msg);
389 return(TRUE);
390 }
391 $this->set_error("Could not establish socket connection.");
392 return(FALSE);
393 }
396 /*! \brief Parses the given xml string into an array
397 @param String XML string
398 @return Array Returns an array containing the xml structure.
399 */
400 function xml_to_array($xml)
401 {
402 $params = array();
403 $level = array();
404 $parser = xml_parser_create_ns();
405 xml_parse_into_struct($parser, $xml, $vals, $index);
407 $err_id = xml_get_error_code($parser);
408 if($err_id){
409 $this->set_error(xml_error_string(xml_get_error_code($parser)));
410 xml_parser_free($parser);
411 }else{
412 xml_parser_free($parser);
414 foreach ($vals as $xml_elem) {
415 if ($xml_elem['type'] == 'open') {
416 if (array_key_exists('attributes',$xml_elem)) {
417 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
418 } else {
419 $level[$xml_elem['level']] = $xml_elem['tag'];
420 }
421 }
422 if ($xml_elem['type'] == 'complete') {
423 $start_level = 1;
424 $php_stmt = '$params';
425 while($start_level < $xml_elem['level']) {
426 $php_stmt .= '[$level['.$start_level.']]';
427 $start_level++;
428 }
429 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
430 @eval($php_stmt);
431 }
432 }
433 }
434 return($params);
435 }
438 /*! \brief Updates an entry with a set of new values,
439 @param Integer The ID of the entry, we want to update.
440 @param Array The variables to update.
441 @return Boolean Returns TRUE on success.
442 */
443 public function update_entry($id,$entry)
444 {
445 $this->b_error = FALSE;
446 $this->s_error = "";
447 if(!is_numeric($id)){
448 trigger_error("Requires an integer value as ID parameter.");
449 return;
450 }
452 if(!is_array($entry)){
453 trigger_error("Requires an array as second parameter.");
454 return;
455 }
457 $attr = "";
458 foreach($entry as $name => $entry){
459 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
460 }
462 $xml_msg = "<xml>
463 <header>gosa_update_status_jobdb_entry</header>
464 <where>
465 <id>".$id."</id>
466 </where>
467 <update>
468 ".$attr."
469 </update>
470 </xml>";
471 $this->connect();
472 if($this->is_connected){
473 $this->o_sock->write($xml_msg);
474 $str = trim($this->o_sock->read());
475 $entries = $this->xml_to_array($str);
476 if(!empty($str)){
477 return(TRUE);
478 }
479 return(FALSE);
480 }
481 $this->set_error("Could not establish socket connection.");
482 return(FALSE);
483 }
484 }
486 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
487 ?>