7ae5e3ccd28d2dc02e0be6c267b42d3b043bd12b
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class gosaSupportDaemon
24 {
25 private $s_host = "";
26 private $i_port = 0;
27 private $s_encryption_key = "";
29 private $o_sock = NULL;
30 private $f_timeout = 2;
31 private $s_error = "";
32 private $b_error = FALSE;
34 private $is_connected = FALSE;
37 /*! \brief Creates a new gosaSupportDaemon object.
38 @param string Host The Host where the daemon is running on.
39 @param integer Port The port which the daemon use.
40 @param string Key The encryption string.
41 @param boolean Connect Directly connect to daemon socket.
42 @param float Timeout The timelimit for all socket actions.
43 */
44 public function __construct($connect=TRUE,$timeout=10)
45 {
46 #FIXME: bad idea about referencing global variables from within classes
47 global $config;
49 # load from config, store statically
50 if (isset($config->current['GOSA_SI'])){
52 if ($this->s_host == ""){
53 $this->s_host= preg_replace("/^.*@([^:]+):.*$/", "$1", $config->current['GOSA_SI']);
54 $this->i_port= preg_replace("/^.*@[^:]+:(.*)$/", "$1", $config->current['GOSA_SI']);
55 $this->s_encryption_key = preg_replace("/^(.*)@[^:]+:.*$/", "$1", $config->current['GOSA_SI']);
56 }
58 $this->f_timeout = $timeout;
59 if($connect){
60 $this->connect();
61 }
62 }
63 }
66 /*! \brief Establish daemon connection.
67 @return boolean Returns true if the connection was succesfully established.
68 */
69 public function connect()
70 {
71 if(!empty($this->s_host) && !empty($this->i_port)){
72 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
73 if($this->o_sock->connected()){
74 $this->o_sock->setEncryptionKey($this->s_encryption_key);
75 $this->is_connected = TRUE;
76 }else{
77 $this->set_error($this->o_sock->get_error());
78 $this->disconnect();
79 new log("debug","gosaSupportDaemon::connect()", "Cannot connect to si-server", array(),$this->get_error());
80 }
81 }else{
82 $this->set_error(msgPool::cmdnotfound("GOSA_SI",_("GOsa support daemon")));
83 }
84 return($this->is_connected);
85 }
88 /*! \brief Disconnect from gosa daemon.
89 */
90 public function disconnect()
91 {
92 $this->o_sock->close();
93 $this->is_connected = FALSE;
94 }
97 /*! \brief Sets an error message, which can be returned with get_error().
98 @param string The Error message,
99 */
100 private function set_error($str)
101 {
102 $this->b_error = TRUE;
103 $this->s_error = $str;
104 }
107 /*! \brief Sets an error message, which can be returned with get_error().
108 @param string The Error message,
109 */
110 private function reset_error()
111 {
112 $this->b_error = FALSE;
113 $this->s_error = "";
114 }
117 /*! \brief Checks if an error occured.
118 @return boolean returns TRUE or FALSE, whether there is an error or not.
119 */
120 public function is_error()
121 {
122 return($this->b_error);
123 }
126 /*! \brief Returns the last error.
127 @return Returns the last error.
128 */
129 public function get_error()
130 {
131 $str = $this->s_error;
132 $str = preg_replace("/ /"," ",$str);
133 return($str);
134 }
137 public function FAI_get_kernels($release)
138 {
139 $xml_msg =
140 "<xml>".
141 "<header>gosa_get_available_kernel</header>".
142 "<source>GOSA</source>".
143 "<target>GOSA</target>".
144 "<release>".$release."</release>".
145 "</xml>";
147 if($this->connect()){
148 $this->o_sock->write($xml_msg);
149 $str = trim($this->o_sock->read());
151 /* Check if something went wrong while reading */
152 if($this->o_sock->is_error()){
153 $this->set_error($this->o_sock->get_error());
154 return($ret);
155 }
157 $entries = $this->xml_to_array($str);
158 if(isset($entries['XML']) && is_array($entries['XML'])){
160 /* Check if returned values represent a valid answer */
161 if(isset($entries['XML'])){
162 if(isset($entries['XML']['ERROR_STRING'])) {
163 $this->set_error($entries['XML']['ERROR_STRING']);
164 new log("debug","GOsa-si",
165 get_class($this)."::".__FUNCTION__, array(),
166 "FAILED error was ".$this->get_error());
167 return($ret);
168 }
170 /* Unset header tags */
171 $ret = $entries['XML'];
172 foreach($ret as $key => $entry){
173 if(!preg_match("/^answer/i",$key)){
174 unset($ret[$key]);
175 }
176 }
177 }
178 }
179 }
180 return($ret);
181 }
184 public function FAI_get_package_sections($release)
185 {
186 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
187 "<select>distinct section</select>".
188 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
190 if($this->connect()){
191 $this->o_sock->write($xml_msg);
192 $str = trim($this->o_sock->read());
194 /* Check if something went wrong while reading */
195 if($this->o_sock->is_error()){
196 $this->set_error($this->o_sock->get_error());
197 return($ret);
198 }
200 $entries = $this->xml_to_array($str);
201 if(isset($entries['XML']) && is_array($entries['XML'])){
203 /* Check if returned values represent a valid answer */
204 if(isset($entries['XML'])){
205 if(isset($entries['XML']['ERROR_STRING'])) {
206 $this->set_error($entries['XML']['ERROR_STRING']);
207 new log("debug","GOsa-si",
208 get_class($this)."::".__FUNCTION__, array(),
209 "FAILED error was ".$this->get_error());
210 return($ret);
211 }
213 /* Unset header tags */
214 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
215 if(isset($entries['XML'][$type])){
216 unset($entries['XML'][$type]);
217 }
218 }
219 $ret = $entries['XML'];
220 }
221 }
222 }
223 return($ret);
224 }
227 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
228 {
229 $this->reset_error();
230 $ret = array();
232 /* Check Parameter */
233 if(!is_array($attrs) || !count($attrs)){
234 trigger_error("Second parameter must be an array. With at least one attribute name.");
235 return($ret);
236 }
238 /* Check Parameter */
239 if(!is_array($package)){
240 trigger_error("Third parameter must be an array. With at least one attribute name.");
241 return($ret);
242 }
244 /* Create list of attributes to fetch */
245 $attr = "";
246 foreach($attrs as $at){
247 $attr.= "<select>".$at."</select>";
248 }
250 /* If no package is given, search for all */
251 if(!count($package)) $package = array("%");
253 /* Create limit tag */
254 if($from == -1){
255 $limit ="";
256 }else{
257 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
258 }
260 /* Create list of attributes to fetch */
261 $pkgs = "";
262 foreach($package as $pkg){
263 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
264 }
266 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
267 $attr.
268 "<where>
269 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
270 <clause><connector>OR</connector>
271 ".$pkgs."
272 </clause>
273 </where>".
274 $limit.
275 "</xml>";
277 if($this->connect()){
278 $this->o_sock->write($xml_msg);
279 $str = trim($this->o_sock->read());
281 /* Check if something went wrong while reading */
282 if($this->o_sock->is_error()){
283 $this->set_error($this->o_sock->get_error());
284 return($ret);
285 }
287 $entries = $this->xml_to_array($str);
288 if(isset($entries['XML']) && is_array($entries['XML'])){
290 /* Check if returned values represent a valid answer */
291 if(isset($entries['XML'])){
292 if(isset($entries['XML']['ERROR_STRING'])) {
293 $this->set_error($entries['XML']['ERROR_STRING']);
294 new log("debug","GOsa-si",
295 get_class($this)."::".__FUNCTION__, array(),
296 "FAILED error was ".$this->get_error());
297 return($ret);
298 }
300 /* Unset header tags */
301 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
302 if(isset($entries['XML'][$type])){
303 unset($entries['XML'][$type]);
304 }
305 }
306 $ret = $entries['XML'];
307 }
308 }
309 }
310 return($ret);
313 }
316 public function FAI_get_server($name = "")
317 {
318 $this->reset_error();
320 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
321 $ret = array();
322 if($this->connect()){
323 $this->o_sock->write($xml_msg);
324 $str = trim($this->o_sock->read());
326 /* Check if something went wrong while reading */
327 if($this->o_sock->is_error()){
328 $this->set_error($this->o_sock->get_error());
329 return($ret);
330 }
332 $entries = $this->xml_to_array($str);
333 if(isset($entries['XML']) && is_array($entries['XML'])){
335 /* Check if returned values represent a valid answer */
336 if(isset($entries['XML'])){
337 if(isset($entries['XML']['ERROR_STRING'])) {
338 $this->set_error($entries['XML']['ERROR_STRING']);
339 new log("debug","GOsa-si",
340 get_class($this)."::".__FUNCTION__, array(),
341 "FAILED error was ".$this->get_error());
342 return($ret);
343 }
345 /* Unset header tags */
346 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
347 if(isset($entries['XML'][$type])){
348 unset($entries['XML'][$type]);
349 }
350 }
351 $ret = $entries['XML'];
352 }
353 }
354 }
355 return($ret);
356 }
359 public function FAI_get_classes($name)
360 {
361 $this->reset_error();
362 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
363 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
364 $ret = array();
365 if($this->connect()){
366 $this->o_sock->write($xml_msg);
367 $str = trim($this->o_sock->read());
369 /* Check if something went wrong while reading */
370 if($this->o_sock->is_error()){
371 $this->set_error($this->o_sock->get_error());
372 return($ret);
373 }
375 $entries = $this->xml_to_array($str);
376 if(isset($entries['XML']) && is_array($entries['XML'])){
378 /* Check if returned values represent a valid answer */
379 if(isset($entries['XML'])){
380 if(isset($entries['XML']['ERROR_STRING'])) {
381 $this->set_error($entries['XML']['ERROR_STRING']);
382 new log("debug","GOsa-si",
383 get_class($this)."::".__FUNCTION__, array($name),
384 "FAILED error was ".$this->get_error());
385 return($ret);
386 }
388 /* Unset header tags */
389 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
390 if(isset($entries['XML'][$type])){
391 unset($entries['XML'][$type]);
392 }
393 }
394 $ret = $entries['XML'];
395 }
396 }
397 }
398 return($ret);
399 }
402 /*! \brief Returns an array containing all queued entries.
403 @return Array All queued entries as an array.
404 */
405 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
406 {
407 $this->reset_error();
408 $ret = array();
410 $tags = "";
411 foreach($event_types as $type){
412 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
413 }
414 if(count($event_types) > 1){
415 $tags = "<connector>or</connector>".$tags;
416 }
417 if(count($event_types)){
418 $tags = "<where><clause>".$tags."</clause></where>";
419 }
421 $xml_msg =
422 "<xml>
423 <header>gosa_query_jobdb</header>
424 <target>GOSA</target>
425 <source>GOSA</source>
426 ".$tags."
428 <orderby>".$sort."</orderby>";
429 if($from != -1 && $to != -1){
430 $xml_msg.= "
431 <limit>
432 <from>".$from."</from>
433 <to>".$to."</to>
434 </limit>";
435 }
436 $xml_msg.= "
437 </xml>";
439 echo htmlentities($xml_msg);
441 if($this->connect()){
442 $this->o_sock->write($xml_msg);
443 $str = trim($this->o_sock->read());
445 /* Check if something went wrong while reading */
446 if($this->o_sock->is_error()){
447 $this->set_error($this->o_sock->get_error());
448 return($ret);
449 }
451 $entries = $this->xml_to_array($str);
452 if(isset($entries['XML']) && is_array($entries['XML'])){
454 /* Check if returned values represent a valid answer */
455 if(isset($entries['XML'])){
457 /* Unset header tags */
458 foreach(array("HEADER","SOURCE","TARGET") as $type){
459 unset($entries['XML'][$type]);
460 }
461 $ret = $entries['XML'];
462 }
463 }
464 }
466 /* Remove session ID. No one is interested in this... */
467 unset($ret['SESSION_ID']);
469 return($ret);
470 }
473 /*! \brief Checks if the given ids are used queue ids.
474 @param Array The ids we want to check..
475 @return Array An array containing all ids as index and TRUE/FALSE as value.
476 */
477 public function ids_exist($ids)
478 {
479 if(!is_array($ids)){
480 trigger_error("Requires an array as parameter.");
481 return;
482 }
483 $this->reset_error();
485 $ret = array();
487 $xml_msg = "<xml>
488 <header>gosa_query_jobdb</header>
489 <target>GOSA</target>
490 <source>GOSA</source>
491 <where>
492 <clause>
493 <connector>or</connector>";
494 foreach($ids as $id){
495 $xml_msg .= "<phrase>
496 <operator>eq</operator>
497 <id>".$id."</id>
498 </phrase>";
499 }
500 $xml_msg .= "</clause>
501 </where>
502 </xml>";
504 if($this->connect()){
505 $this->o_sock->write($xml_msg);
506 $str = trim($this->o_sock->read());
508 /* Check if something went wrong while reading */
509 if($this->o_sock->is_error()){
510 $this->set_error($this->o_sock->get_error());
511 return($ret);
512 }
514 $entries = $this->xml_to_array($str);
515 if(isset($entries['XML']) && is_array($entries['XML'])){
516 foreach($entries['XML'] as $entry){
517 if(isset($entry['ID'])){
518 $ret[] = $entry['ID'];
519 }
520 }
521 }
522 }
523 return($ret);
524 }
527 /*! \brief Returns an entry containing all requested ids.
528 @param Array The IDs of the entries we want to return.
529 @return Array Of the requested entries.
530 */
531 public function get_entries_by_mac($macs)
532 {
533 if(!is_array($macs)){
534 trigger_error("Requires an array as parameter.");
535 return;
536 }
537 $this->reset_error();
539 $ret = array();
541 $xml_msg = "<xml>
542 <header>gosa_query_jobdb</header>
543 <target>GOSA</target>
544 <source>GOSA</source>
545 <where>
546 <clause>
547 <connector>or</connector>";
548 foreach($macs as $mac){
549 $xml_msg .= "<phrase>
550 <operator>eq</operator>
551 <macaddress>".$mac."</macaddress>
552 </phrase>";
553 }
554 $xml_msg .= "</clause>
555 </where>
556 </xml>";
558 if($this->connect()){
559 $this->o_sock->write($xml_msg);
560 $str = trim($this->o_sock->read());
562 /* Check if something went wrong while reading */
563 if($this->o_sock->is_error()){
564 $this->set_error($this->o_sock->get_error());
565 return($ret);
566 }
568 $entries = $this->xml_to_array($str);
569 if(isset($entries['XML'])){
570 foreach($entries['XML'] as $name => $entry){
571 if(preg_match("/^ANSWER[0-9]*$/",$name)){
572 $ret[$name] = $entry;
573 }
574 }
575 }
576 }
577 return($ret);
578 }
581 /*! \brief Returns an entry containing all requested ids.
582 @param Array The IDs of the entries we want to return.
583 @return Array Of the requested entries.
584 */
585 public function get_entries_by_id($ids)
586 {
587 if(!is_array($ids)){
588 trigger_error("Requires an array as parameter.");
589 return;
590 }
591 $this->reset_error();
593 $ret = array();
595 $xml_msg = "<xml>
596 <header>gosa_query_jobdb</header>
597 <target>GOSA</target>
598 <source>GOSA</source>
599 <where>
600 <clause>
601 <connector>or</connector>";
602 foreach($ids as $id){
603 $xml_msg .= "<phrase>
604 <operator>eq</operator>
605 <id>".$id."</id>
606 </phrase>";
607 }
608 $xml_msg .= "</clause>
609 </where>
610 </xml>";
612 if($this->connect()){
613 $this->o_sock->write($xml_msg);
614 $str = trim($this->o_sock->read());
616 /* Check if something went wrong while reading */
617 if($this->o_sock->is_error()){
618 $this->set_error($this->o_sock->get_error());
619 return($ret);
620 }
622 $entries = $this->xml_to_array($str);
623 if(isset($entries['XML'])){
624 foreach($entries['XML'] as $name => $entry){
625 if(preg_match("/^ANSWER[0-9]*$/",$name)){
626 $ret[$name] = $entry;
627 }
628 }
629 }
630 }
631 return($ret);
632 }
635 /*! \brief Checks if the given id is in use.
636 @param Integer The ID of the entry.
637 @return Boolean TRUE if entry exists.
638 */
639 public function id_exists($id)
640 {
641 if(!is_numeric($id)){
642 trigger_error("Requires an integer as parameter.");
643 return;
644 }
646 $this->reset_error();
648 $xml_msg = "<xml>
649 <header>gosa_query_jobdb</header>
650 <target>GOSA</target>
651 <source>GOSA</source>
652 <where>
653 <clause>
654 <phrase>
655 <operator>eq</operator>
656 <id>".$id."</id>
657 </phrase>
658 </clause>
659 </where>
660 </xml>";
662 if($this->connect()){
663 $this->o_sock->write($xml_msg);
664 $str = trim($this->o_sock->read());
666 /* Check if something went wrong while reading */
667 if($this->o_sock->is_error()){
668 $this->set_error($this->o_sock->get_error());
669 return(FALSE);
670 }
672 $entries = $this->xml_to_array($str);
673 if( isset($entries['XML']['HEADER']) &&
674 $entries['XML']['HEADER']=="answer" &&
675 isset($entries['XML']['ANSWER1'])){
676 return(TRUE);
677 }
678 }
679 return(FALSE);
680 }
683 /*! \brief Returns an entry from the gosaSupportQueue
684 @param Integer The ID of the entry we want to return.
685 @return Array Of the requested entry.
686 */
687 public function get_entry_by_id($id)
688 {
689 if(!is_numeric($id)){
690 trigger_error("Requires an integer as parameter.");
691 return;
692 }
693 $this->reset_error();
695 $ret = array();
696 $xml_msg = "<xml>
697 <header>gosa_query_jobdb</header>
698 <target>GOSA</target>
699 <source>GOSA</source>
700 <where>
701 <clause>
702 <phrase>
703 <operator>eq</operator>
704 <id>".$id."</id>
705 </phrase>
706 </clause>
707 </where>
708 </xml>";
709 if($this->connect()){
710 $this->o_sock->write($xml_msg);
711 $str = trim($this->o_sock->read());
713 /* Check if something went wrong while reading */
714 if($this->o_sock->is_error()){
715 $this->set_error($this->o_sock->get_error());
716 return($ret);
717 }
719 $entries = $this->xml_to_array($str);
720 if( isset($entries['XML']['HEADER']) &&
721 $entries['XML']['HEADER']=="answer" &&
722 isset($entries['XML']['ANSWER1'])){
723 $ret = $entries['XML']['ANSWER1'];
724 }
725 }
726 return($ret);
727 }
730 /*! \brief Removes a set of entries from the GOsa support queue.
731 @param Array The IDs to remove.
732 @return Boolean True on success.
733 */
734 public function remove_entries($ids)
735 {
736 if(!is_array($ids)){
737 trigger_error("Requires an array as parameter.");
738 return;
739 }
741 $this->reset_error();
743 $ret = array();
745 $xml_msg = "<xml>
746 <header>gosa_delete_jobdb_entry</header>
747 <target>GOSA</target>
748 <source>GOSA</source>
749 <where>
750 <clause>
751 <connector>or</connector>";
752 foreach($ids as $id){
753 $xml_msg .= "<phrase>
754 <operator>eq</operator>
755 <id>".$id."</id>
756 </phrase>";
757 }
758 $xml_msg .= "</clause>
759 </where>
760 </xml>";
762 if($this->connect()){
763 $this->o_sock->write($xml_msg);
764 $str = $this->o_sock->read();
766 /* Check if something went wrong while reading */
767 if($this->o_sock->is_error()){
768 $this->set_error($this->o_sock->get_error());
769 return($ret);
770 }
772 $entries = $this->xml_to_array($str);
773 if(isset($entries['XML']) || isset($entries['COUNT'])){
774 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
775 return(TRUE);
776 }else{
777 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
778 }
779 }
780 return(FALSE);
781 }
785 /*! \brief Removes an entry from the GOsa support queue.
786 @param Integer The ID of the entry we want to remove.
787 @return Boolean True on success.
788 */
789 public function remove_entry($id)
790 {
791 return($this->remove_entries(array($id)));
792 }
795 /*! \brief Parses the given xml string into an array
796 @param String XML string
797 @return Array Returns an array containing the xml structure.
798 */
799 private function xml_to_array($xml)
800 {
801 $params = array();
802 $level = array();
803 $parser = xml_parser_create_ns();
804 xml_parse_into_struct($parser, $xml, $vals, $index);
806 $err_id = xml_get_error_code($parser);
807 if($err_id){
808 xml_parser_free($parser);
809 }else{
810 xml_parser_free($parser);
812 foreach ($vals as $xml_elem) {
813 if ($xml_elem['type'] == 'open') {
814 if (array_key_exists('attributes',$xml_elem)) {
815 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
816 } else {
817 $level[$xml_elem['level']] = $xml_elem['tag'];
818 }
819 }
820 if ($xml_elem['type'] == 'complete') {
821 $start_level = 1;
822 $php_stmt = '$params';
823 while($start_level < $xml_elem['level']) {
824 $php_stmt .= '[$level['.$start_level.']]';
825 $start_level++;
826 }
827 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
828 @eval($php_stmt);
829 }
830 }
831 }
833 if(!isset($params['XML'])){
834 if (!array_key_exists('XML', $params)){
835 $this->set_error(_("Cannot not parse XML!"));
836 }
837 $params = array("COUNT" => 0);
838 }
840 return($params);
841 }
844 /*! \brief Updates an entry with a set of new values,
845 @param Integer The ID of the entry, we want to update.
846 @param Array The variables to update.
847 @return Boolean Returns TRUE on success.
848 */
849 public function update_entries($ids,$data)
850 {
851 $this->reset_error();
852 if(!is_array($ids)){
853 trigger_error("Requires an array as first parameter.");
854 return;
855 }
857 if(!is_array($data)){
858 trigger_error("Requires an array as second parameter.");
859 return;
860 }
862 $attr = "";
863 foreach($data as $key => $value){
864 if(is_array($value)){
865 foreach($value as $sub_value){
866 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
867 }
868 }else{
869 $attr.= "<$key>".strtolower($value)."</$key>\n";
870 }
871 }
873 $xml_msg = "<xml>
874 <header>gosa_update_status_jobdb_entry</header>
875 <target>GOSA</target>
876 <source>GOSA</source>
877 <where>
878 <clause>
879 <connector>or</connector>";
880 foreach($ids as $id){
881 $xml_msg .= "<phrase>
882 <operator>eq</operator>
883 <id>".$id."</id>
884 </phrase>";
885 }
886 $xml_msg .= "</clause>
887 </where>
888 <update>
889 ".$attr."
890 </update>
891 </xml>";
893 if($this->connect()){
895 $this->o_sock->write($xml_msg);
896 $str = trim($this->o_sock->read());
898 /* Check if something went wrong while reading */
899 if($this->o_sock->is_error()){
900 $this->set_error($this->o_sock->get_error());
901 return(FALSE);
902 }
904 $entries = $this->xml_to_array($str);
905 if(isset($entries['XML'])){
906 if(isset($entries['XML']['ERROR_STRING'])) {
907 $this->set_error($entries['XML']['ERROR_STRING']);
908 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
909 return(FALSE);
910 }
911 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
912 return(TRUE);
913 }
914 }
915 return(FALSE);
916 }
919 /*! \brief Returns the number of currently queued objects.
920 @return Integer
921 */
922 public function number_of_queued_entries($event_types)
923 {
924 $tags = "";
925 foreach($event_types as $type){
926 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
927 }
928 if(count($event_types) > 1){
929 $tags = "<connector>or</connector>".$tags;
930 }
931 if(count($event_types)){
932 $tags = "<where><clause>".$tags."</clause></where>";
933 }
936 $xml_msg =
937 "<xml>".
938 "<header>gosa_query_jobdb</header>".
939 "<target>GOSA</target>".
940 "<source>GOSA</source>".
941 "<select> count ID</select>".
942 $tags.
943 "</xml>";
945 $this->connect();
946 if($this->connect()){
947 $this->o_sock->write($xml_msg);
948 $str = trim($this->o_sock->read());
950 /* Check if something went wrong while reading */
951 if($this->o_sock->is_error()){
952 $this->set_error($this->o_sock->get_error());
953 return(0);
954 }
956 $entries = $this->xml_to_array($str);
957 print_a($entries);
958 if(isset($entries['XML'])){
959 return($entries['XML']['COUNT']);
960 }
961 }
962 return(-1);
963 }
966 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
967 {
968 $xml_message= "";
970 /* Prepare data */
971 foreach ($data as $key => $value){
972 if(is_array($value)){
973 foreach($value as $sub_value){
974 $xml_message.= "<$key>$sub_value</$key>";
975 }
976 }else{
977 $xml_message.= "<$key>$value</$key>";
978 }
979 }
981 /* Multiple targets? */
982 if (!is_array($to)){
983 $to_targets= array($to);
984 } else {
985 $to_targets= $to;
986 }
988 /* Build target strings */
989 $target ="";
990 foreach($to_targets as $to){
991 $target.= "<target>$to</target>";
992 }
994 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
995 }
998 /* Allows simply appending a new DaemonEvent
999 */
1000 public function append($event)
1001 {
1002 if(!($event instanceof DaemonEvent)){
1003 return(FALSE);
1004 }
1006 $this->reset_error();
1008 /* Add to queue if new
1009 */
1010 if($event->is_new()){
1012 $request_answer = FALSE;
1013 if($event->get_type() == SCHEDULED_EVENT){
1014 $action = $event->get_schedule_action();
1015 }elseif($event->get_type() == TRIGGERED_EVENT){
1016 $action = $event->get_trigger_action();
1017 }else{
1018 trigger_error("Unknown type of queue event given.");
1019 return(FALSE);
1020 }
1022 /* Get event informations, like targets..
1023 */
1024 $targets = $event->get_targets();
1025 $data = $event->save();
1027 /* Append an entry for each target
1028 */
1029 foreach($targets as $target){
1030 $data['macaddress'] = $target;
1031 $this->send_data($action,$target,$data,$request_answer);
1033 if($this->is_error()){
1034 return(FALSE);
1035 }
1036 }
1037 return(TRUE);
1038 }else{
1040 /* Updated edited entry.
1041 */
1042 $id = $event->get_id();
1043 $data = $event->save();
1044 return($this->update_entries(array($id),$data));
1045 }
1047 return(FALSE);
1048 }
1051 /*! \brief Returns an array containing all queued entries.
1052 @return Array All queued entries as an array.
1053 */
1054 public function _send($data, $answer_expected= FALSE)
1055 {
1056 $this->reset_error();
1057 $ret = array();
1059 if($this->connect()){
1060 $this->o_sock->write($data);
1061 if ($answer_expected){
1062 $str = trim($this->o_sock->read());
1064 /* Check if something went wrong while reading */
1065 if($this->o_sock->is_error()){
1066 $this->set_error($this->o_sock->get_error());
1067 return($ret);
1068 }
1070 $entries = $this->xml_to_array($str);
1071 if(isset($entries['XML']) && is_array($entries['XML'])){
1072 $ret = $entries;
1073 if(isset($entries['XML']['ERROR_STRING'])) {
1074 $this->set_error($entries['XML']['ERROR_STRING']);
1075 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1076 }else{
1077 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1078 }
1079 }
1080 }else{
1081 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1082 }
1083 }
1084 return($ret);
1085 }
1088 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1089 {
1090 $xml_message= "";
1092 /* Get communication object */
1093 $d= new gosaSupportDaemon(TRUE,10);
1095 /* Prepare data */
1096 foreach ($data as $key => $value){
1097 if(is_array($value)){
1098 foreach($value as $sub_val){
1099 $xml_message.= "<$key>$sub_value</$key>";
1100 }
1101 }else{
1102 $xml_message.= "<$key>$value</$key>";
1103 }
1104 }
1106 /* Multiple targets? */
1107 if (!is_array($to)){
1108 $to_targets= array($to);
1109 } else {
1110 $to_targets= $to;
1111 }
1113 /* Build target strings */
1114 $target ="";
1115 foreach($to_targets as $to){
1116 $target.= "<target>$to</target>";
1117 }
1119 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1120 }
1123 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1124 @param String $mac The mac address for which we want to remove all jobs.
1125 */
1126 function clean_queue_from_mac($mac)
1127 {
1128 global $config;
1130 /* First of all we have to check which jobs are startet
1131 * for $mac
1132 */
1133 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1135 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1137 $data = $this->_send($xml_msg,TRUE);
1138 if(is_array($data) && isset($data['XML'])){
1139 $already_aborted = FALSE;
1140 foreach($data['XML'] as $name => $entry){
1141 if(preg_match("/answer[0-9]*/i",$name)){
1142 $entry['STATUS'] = strtoupper($entry['STATUS']);
1143 switch($entry['STATUS']){
1145 case 'PROCESSING' :
1147 /* Send abort event, but only once
1148 */
1149 if($already_aborted){
1150 break;
1151 }elseif(class_available("DaemonEvent_faireboot")){
1152 $already_aborted = TRUE;
1153 $tmp = new DaemonEvent_faireboot($config);
1154 $tmp->add_targets(array($mac));
1155 $tmp->set_type(TRIGGERED_EVENT);
1156 if(!$this->append($tmp)){
1157 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1158 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1159 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1160 }else{
1161 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1162 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1163 }
1164 ;break;
1165 }else{
1166 /* Couldn't find abort event, just remove entry */
1167 }
1169 case 'WAITING':
1170 case 'ERROR':
1171 default :
1173 /* Simply remove entries from queue.
1174 * Failed or waiting events, can be removed without any trouble.
1175 */
1176 if(!$this->remove_entries(array($entry['ID']))){
1177 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1178 }
1179 ;break;
1180 }
1182 }
1183 }
1184 }
1185 }
1188 static function ping($target)
1189 {
1190 if (tests::is_mac($target)){
1191 /* Get communication object */
1192 $d= new gosaSupportDaemon(TRUE,0.5);
1193 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1194 return (count($answer) ? TRUE:FALSE);
1195 }
1197 return (FALSE);
1198 }
1200 }
1202 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1203 ?>