944e1fa2d8778888dc088f5b934a094f706b56f1
1 <?php
2 /*
4 This code is part of GOsa (https://gosa.gonicus.de)
5 Copyright (C) 2008 Fabian Hickert
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 Function overview:
24 __construct - Create a new deamon handle.
25 connect - Connect to deamon socket.
26 disconnect - Disconnect from socket.
27 set_error - Sets a new error.
28 is_error - Returns TRUE if there was an error.
29 get_error - Returns the last error or "".
30 get_queued_entries - Returns all queued entries, with limitations.
31 ids_exist - Checks if the given id exists.
32 get_entries_by_id - Returns a set of entries.
33 id_exists - Checks if a set entries exists.
34 get_entry_by_id - Returns a single entry.
35 remove_entries - Remove a set of entries.
36 remove_entry - Removes a single entry.
37 update_entries - Updates a set of entries.
38 xml_to_array - XML to Array.
39 number_of_queued_entries - Returns the number of currently queued entries.
40 */
42 class gosaSupportDaemon
43 {
44 static private $s_host = "";
45 static private $i_port = 0;
46 static private $s_encryption_key = "";
48 private $o_sock = NULL;
49 private $f_timeout = 2;
50 private $s_error = "";
51 private $b_error = FALSE;
53 private $is_connected = FALSE;
56 /*! \brief Creates a new gosaSupportDaemon object.
57 @param string Host The Host where the deamon is running on.
58 @param integer Port The port which the deamon use.
59 @param string Key The encryption string.
60 @param boolean Connect Directly connect to deamon socket.
61 @param float Timeout The timelimit for all socket actions.
62 */
63 public function __construct($connect=TRUE,$timeout=0.2)
64 {
65 #FIXME: bad idea about referencing global variables from within classes
66 global $config;
68 # load from config, store statically
69 if (isset($config->current['GOSA_SI'])){
71 if (gosaSupportDaemon::$s_host == ""){
72 $host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
73 $port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
74 $key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
76 gosaSupportDaemon::$s_host = $host;
77 gosaSupportDaemon::$i_port = $port;
78 gosaSupportDaemon::$s_encryption_key = $key;
79 }
81 $this->f_timeout = $timeout;
82 if($connect){
83 $this->connect();
84 }
85 }
86 }
89 /*! \brief Establish deamon connection.
90 @return boolean Returns true if the connection was succesfully established.
91 */
92 public function connect()
93 {
94 $this->o_sock = new Socket_Client(gosaSupportDaemon::$s_host,gosaSupportDaemon::$i_port,TRUE,$this->f_timeout);
95 if($this->o_sock->connected()){
96 $this->o_sock->setEncryptionKey(gosaSupportDaemon::$s_encryption_key);
97 $this->is_connected = TRUE;
98 }else{
99 $this->error = $this->o_sock->get_error();
100 $this->disconnect();
101 }
102 return($this->is_connected);
103 }
106 /*! \brief Disconnect from gosa deamon.
107 */
108 public function disconnect()
109 {
110 $this->o_sock->close();
111 $this->is_connected = FALSE;
112 }
115 /*! \brief Sets an error message, which can be returned with get_error().
116 @param string The Error message,
117 */
118 private function set_error($str)
119 {
120 $this->b_error = TRUE;
121 $this->s_error = $str;
122 }
125 /*! \brief Checks if an error occured.
126 @return boolean returns TRUE or FALSE, whether there is an error or not.
127 */
128 public function is_error()
129 {
130 return($this->b_error);
131 }
134 /*! \brief Returns the last error.
135 @return Returns the last error.
136 */
137 public function get_error()
138 {
139 return($this->s_error);
140 }
143 /*! \brief Returns an array containing all queued entries.
144 @return Array All queued entries as an array.
145 */
146 public function get_queued_entries($from=0,$to=10,$sort="timestamp DESC")
147 {
148 $this->b_error = FALSE;
149 $this->s_error = "";
150 $ret = array();
152 $xml_msg = "<xml>
153 <header>gosa_query_jobdb</header>
154 <where>
155 <clause>
156 <phrase>
157 <operator>ne</operator>
158 <HEADERTAG>*</HEADERTAG>
159 </phrase>
160 </clause>
161 </where>
162 <orderby>".$sort."</orderby>
163 <limit>
164 <from>".$from."</from>
165 <to>".$to."</to>
166 </limit>
167 </xml>";
169 if($this->connect()){
170 $this->o_sock->write($xml_msg);
171 $str = trim($this->o_sock->read());
172 $entries = $this->xml_to_array($str);
173 if(isset($entries['XML']) && is_array($entries['XML'])){
174 $ret = $entries;
175 }
176 }
177 return($ret);
178 }
180 /*! \brief Checks if the given ids are used queue ids.
181 @param Array The ids we want to check..
182 @return Array An array containing all ids as index and TRUE/FALSE as value.
183 */
184 public function ids_exist($ids)
185 {
186 if(!is_array($ids)){
187 trigger_error("Requires an array as parameter.");
188 return;
189 }
190 $this->b_error = FALSE;
191 $this->s_error = "";
193 $ret = array();
195 $xml_msg = "<xml>
196 <header>gosa_query_jobdb</header>
197 <where>
198 <clause>
199 <connector>or</connector>";
200 foreach($ids as $id){
201 $xml_msg .= "<phrase>
202 <operator>eq</operator>
203 <id>".$id."</id>
204 </phrase>";
205 }
206 $xml_msg .= "</clause>
207 </where>
208 </xml>";
210 if($this->connect()){
211 $this->o_sock->write($xml_msg);
212 $str = trim($this->o_sock->read());
213 $entries = $this->xml_to_array($str);
214 if(isset($entries['XML']) && is_array($entries['XML'])){
215 foreach($entries['XML'] as $entry){
216 $ret[] = $entry['ID'];
217 }
218 }
219 }
220 return($ret);
221 }
224 /*! \brief Returns an entry containing all requested ids.
225 @param Array The IDs of the entries we want to return.
226 @return Array Of the requested entries.
227 */
228 public function get_entries_by_id($ids)
229 {
230 if(!is_array($ids)){
231 trigger_error("Requires an array as parameter.");
232 return;
233 }
234 $this->b_error = FALSE;
235 $this->s_error = "";
237 $ret = array();
239 $xml_msg = "<xml>
240 <header>gosa_query_jobdb</header>
241 <where>
242 <clause>
243 <connector>or</connector>";
244 foreach($ids as $id){
245 $xml_msg .= "<phrase>
246 <operator>eq</operator>
247 <id>".$id."</id>
248 </phrase>";
249 $ret[$id] = FALSE;
250 }
251 $xml_msg .= "</clause>
252 </where>
253 </xml>";
255 if($this->connect()){
256 $this->o_sock->write($xml_msg);
257 $str = trim($this->o_sock->read());
258 $entries = $this->xml_to_array($str);
259 if(isset($entries['XML'])){
260 $ret = $entries['XML'];
261 }
262 }
263 return($ret);
264 }
267 /*! \brief Checks if the given id is in use.
268 @param Integer The ID of the entry.
269 @return Boolean TRUE if entry exists.
270 */
271 public function id_exists($id)
272 {
273 if(!is_numeric($id)){
274 trigger_error("Requires an integer as parameter.");
275 return;
276 }
278 $this->b_error = FALSE;
279 $this->s_error = "";
280 $xml_msg = "<xml>
281 <header>gosa_query_jobdb</header>
282 <where>
283 <clause>
284 <phrase>
285 <operator>eq</operator>
286 <id>".$id."</id>
287 </phrase>
288 </clause>
289 </where>
290 </xml>";
292 if($this->connect()){
293 $this->o_sock->write($xml_msg);
294 $str = trim($this->o_sock->read());
295 $entries = $this->xml_to_array($str);
296 if(isset($entries['XML']['ANSWER1'])){
297 return(TRUE);
298 }
299 }
300 return(FALSE);
301 }
304 /*! \brief Returns an entry from the gosaSupportQueue
305 @param Integer The ID of the entry we want to return.
306 @return Array Of the requested entry.
307 */
308 public function get_entry_by_id($id)
309 {
310 if(!is_numeric($id)){
311 trigger_error("Requires an integer as parameter.");
312 return;
313 }
315 $this->b_error = FALSE;
316 $this->s_error = "";
317 $ret = array();
318 $xml_msg = "<xml>
319 <header>gosa_query_jobdb</header>
320 <where>
321 <clause>
322 <phrase>
323 <operator>eq</operator>
324 <id>".$id."</id>
325 </phrase>
326 </clause>
327 </where>
328 </xml>";
329 if($this->connect()){
330 $this->o_sock->write($xml_msg);
331 $str = trim($this->o_sock->read());
332 $entries = $this->xml_to_array($str);
333 if(isset($entries['XML']['ANSWER1'])){
334 $ret = $entries['XML']['ANSWER1'];
335 }
336 }
337 return($ret);
338 }
341 /*! \brief Removes a set of entries from the GOsa support queue.
342 @param Array The IDs to remove.
343 @return Boolean True on success.
344 */
345 public function remove_entries($ids)
346 {
347 if(!is_array($ids)){
348 trigger_error("Requires an array as parameter.");
349 return;
350 }
351 $this->b_error = FALSE;
352 $this->s_error = "";
354 $ret = array();
356 $xml_msg = "<xml>
357 <header>gosa_delete_jobdb_entry</header>
358 <where>
359 <clause>
360 <connector>or</connector>";
361 foreach($ids as $id){
362 $xml_msg .= "<phrase>
363 <operator>eq</operator>
364 <id>".$id."</id>
365 </phrase>";
366 }
367 $xml_msg .= "</clause>
368 </where>
369 </xml>";
370 $this->b_error = FALSE;
371 $this->s_error = "";
373 if($this->connect()){
374 $this->o_sock->write($xml_msg);
375 $str = $this->o_sock->read();
376 $entries = $this->xml_to_array($str);
377 if(isset($entries['XML'])){
378 return(TRUE);
379 }
380 }
381 return(FALSE);
382 }
386 /*! \brief Removes an entry from the GOsa support queue.
387 @param Integer The ID of the entry we want to remove.
388 @return Boolean True on success.
389 */
390 public function remove_entry($id)
391 {
392 $this->b_error = FALSE;
393 $this->s_error = "";
395 $xml_msg = "<xml>
396 <header>gosa_delete_jobdb_entry</header>
397 <where>
398 <clause>
399 <phrase>
400 <operator>eq</operator>
401 <id>".$id."</id>
402 </phrase>
403 </clause>
404 </where>
405 </xml>";
406 if($this->connect()){
407 $this->o_sock->write($xml_msg);
408 $str = $this->o_sock->read();
409 $entries = $this->xml_to_array($str);
410 if(isset($entries['XML'])){
411 return(TRUE);
412 }
413 }
414 return(FALSE);
415 }
418 /*! \brief Parses the given xml string into an array
419 @param String XML string
420 @return Array Returns an array containing the xml structure.
421 */
422 private function xml_to_array($xml)
423 {
424 $params = array();
425 $level = array();
426 $parser = xml_parser_create_ns();
427 xml_parse_into_struct($parser, $xml, $vals, $index);
429 $err_id = xml_get_error_code($parser);
430 if($err_id){
431 xml_parser_free($parser);
432 }else{
433 xml_parser_free($parser);
435 foreach ($vals as $xml_elem) {
436 if ($xml_elem['type'] == 'open') {
437 if (array_key_exists('attributes',$xml_elem)) {
438 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
439 } else {
440 $level[$xml_elem['level']] = $xml_elem['tag'];
441 }
442 }
443 if ($xml_elem['type'] == 'complete') {
444 $start_level = 1;
445 $php_stmt = '$params';
446 while($start_level < $xml_elem['level']) {
447 $php_stmt .= '[$level['.$start_level.']]';
448 $start_level++;
449 }
450 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
451 @eval($php_stmt);
452 }
453 }
454 }
455 if(!isset($params['XML'])){
456 $this->set_error(_("Could not parse XML."));
457 $params = array();
458 }
459 return($params);
460 }
463 /*! \brief Updates an entry with a set of new values,
464 @param Integer The ID of the entry, we want to update.
465 @param Array The variables to update.
466 @return Boolean Returns TRUE on success.
467 */
468 public function update_entries($ids,$entry)
469 {
470 $this->b_error = FALSE;
471 $this->s_error = "";
472 if(!is_array($ids)){
473 trigger_error("Requires an array as first parameter.");
474 return;
475 }
477 if(!is_array($entry)){
478 trigger_error("Requires an array as second parameter.");
479 return;
480 }
482 $attr = "";
483 foreach($entry as $name => $entry){
484 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
485 }
486 $xml_msg = "<xml>
487 <header>gosa_update_status_jobdb_entry</header>
488 <where>
489 <clause>
490 <connector>or</connector>";
491 foreach($ids as $id){
492 $xml_msg .= "<phrase>
493 <operator>eq</operator>
494 <id>".$id."</id>
495 </phrase>";
496 }
497 $xml_msg .= "</clause>
498 </where>
499 <update>
500 ".$attr."
501 </update>
502 </xml>";
503 if($this->connect()){
504 $this->o_sock->write($xml_msg);
505 $str = trim($this->o_sock->read());
506 $entries = $this->xml_to_array($str);
507 if(isset($entries['XML'])){
508 return(TRUE);
509 }
510 }
511 return(FALSE);
512 }
515 /*! \brief Returns the number of currently queued objects.
516 @return Integer
517 */
518 public function number_of_queued_entries()
519 {
520 $xml_msg ="<xml><header>gosa_count_jobdb</header></xml>";
521 $this->connect();
522 if($this->connect()){
523 $this->o_sock->write($xml_msg);
524 $str = trim($this->o_sock->read());
525 $entries = $this->xml_to_array($str);
526 if(isset($entries['XML'])){
527 return($entries['XML']['COUNT']);
528 }
529 }
530 return(-1);
531 }
534 /*! \brief Returns an array containing all queued entries.
535 @return Array All queued entries as an array.
536 */
537 public function send($data, $answer_expected= FALSE)
538 {
539 $this->b_error = FALSE;
540 $this->s_error = "";
541 $ret = array();
543 if($this->connect()){
544 $this->o_sock->write($data);
545 if ($answer_exepcted){
546 $str = trim($this->o_sock->read());
547 $entries = $this->xml_to_array($str);
548 if(isset($entries['XML']) && is_array($entries['XML'])){
549 $ret = $entries;
550 }
551 }
552 }
553 return($ret);
554 }
556 }
558 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
559 ?>