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