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 if($this->connect()){
440 $this->o_sock->write($xml_msg);
441 $str = trim($this->o_sock->read());
443 /* Check if something went wrong while reading */
444 if($this->o_sock->is_error()){
445 $this->set_error($this->o_sock->get_error());
446 return($ret);
447 }
449 $entries = $this->xml_to_array($str);
450 if(isset($entries['XML']) && is_array($entries['XML'])){
452 /* Check if returned values represent a valid answer */
453 if(isset($entries['XML'])){
455 /* Unset header tags */
456 foreach(array("HEADER","SOURCE","TARGET") as $type){
457 unset($entries['XML'][$type]);
458 }
459 $ret = $entries['XML'];
460 }
461 }
462 }
464 /* Remove session ID. No one is interested in this... */
465 unset($ret['SESSION_ID']);
467 return($ret);
468 }
471 /*! \brief Checks if the given ids are used queue ids.
472 @param Array The ids we want to check..
473 @return Array An array containing all ids as index and TRUE/FALSE as value.
474 */
475 public function ids_exist($ids)
476 {
477 if(!is_array($ids)){
478 trigger_error("Requires an array as parameter.");
479 return;
480 }
481 $this->reset_error();
483 $ret = array();
485 $xml_msg = "<xml>
486 <header>gosa_query_jobdb</header>
487 <target>GOSA</target>
488 <source>GOSA</source>
489 <where>
490 <clause>
491 <connector>or</connector>";
492 foreach($ids as $id){
493 $xml_msg .= "<phrase>
494 <operator>eq</operator>
495 <id>".$id."</id>
496 </phrase>";
497 }
498 $xml_msg .= "</clause>
499 </where>
500 </xml>";
502 if($this->connect()){
503 $this->o_sock->write($xml_msg);
504 $str = trim($this->o_sock->read());
506 /* Check if something went wrong while reading */
507 if($this->o_sock->is_error()){
508 $this->set_error($this->o_sock->get_error());
509 return($ret);
510 }
512 $entries = $this->xml_to_array($str);
513 if(isset($entries['XML']) && is_array($entries['XML'])){
514 foreach($entries['XML'] as $entry){
515 if(isset($entry['ID'])){
516 $ret[] = $entry['ID'];
517 }
518 }
519 }
520 }
521 return($ret);
522 }
525 /*! \brief Returns an entry containing all requested ids.
526 @param Array The IDs of the entries we want to return.
527 @return Array Of the requested entries.
528 */
529 public function get_entries_by_mac($macs)
530 {
531 if(!is_array($macs)){
532 trigger_error("Requires an array as parameter.");
533 return;
534 }
535 $this->reset_error();
537 $ret = array();
539 $xml_msg = "<xml>
540 <header>gosa_query_jobdb</header>
541 <target>GOSA</target>
542 <source>GOSA</source>
543 <where>
544 <clause>
545 <connector>or</connector>";
546 foreach($macs as $mac){
547 $xml_msg .= "<phrase>
548 <operator>eq</operator>
549 <macaddress>".$mac."</macaddress>
550 </phrase>";
551 }
552 $xml_msg .= "</clause>
553 </where>
554 </xml>";
556 if($this->connect()){
557 $this->o_sock->write($xml_msg);
558 $str = trim($this->o_sock->read());
560 /* Check if something went wrong while reading */
561 if($this->o_sock->is_error()){
562 $this->set_error($this->o_sock->get_error());
563 return($ret);
564 }
566 $entries = $this->xml_to_array($str);
567 if(isset($entries['XML'])){
568 foreach($entries['XML'] as $name => $entry){
569 if(preg_match("/^ANSWER[0-9]*$/",$name)){
570 $ret[$name] = $entry;
571 }
572 }
573 }
574 }
575 return($ret);
576 }
579 /*! \brief Returns an entry containing all requested ids.
580 @param Array The IDs of the entries we want to return.
581 @return Array Of the requested entries.
582 */
583 public function get_entries_by_id($ids)
584 {
585 if(!is_array($ids)){
586 trigger_error("Requires an array as parameter.");
587 return;
588 }
589 $this->reset_error();
591 $ret = array();
593 $xml_msg = "<xml>
594 <header>gosa_query_jobdb</header>
595 <target>GOSA</target>
596 <source>GOSA</source>
597 <where>
598 <clause>
599 <connector>or</connector>";
600 foreach($ids as $id){
601 $xml_msg .= "<phrase>
602 <operator>eq</operator>
603 <id>".$id."</id>
604 </phrase>";
605 }
606 $xml_msg .= "</clause>
607 </where>
608 </xml>";
610 if($this->connect()){
611 $this->o_sock->write($xml_msg);
612 $str = trim($this->o_sock->read());
614 /* Check if something went wrong while reading */
615 if($this->o_sock->is_error()){
616 $this->set_error($this->o_sock->get_error());
617 return($ret);
618 }
620 $entries = $this->xml_to_array($str);
621 if(isset($entries['XML'])){
622 foreach($entries['XML'] as $name => $entry){
623 if(preg_match("/^ANSWER[0-9]*$/",$name)){
624 $ret[$name] = $entry;
625 }
626 }
627 }
628 }
629 return($ret);
630 }
633 /*! \brief Checks if the given id is in use.
634 @param Integer The ID of the entry.
635 @return Boolean TRUE if entry exists.
636 */
637 public function id_exists($id)
638 {
639 if(!is_numeric($id)){
640 trigger_error("Requires an integer as parameter.");
641 return;
642 }
644 $this->reset_error();
646 $xml_msg = "<xml>
647 <header>gosa_query_jobdb</header>
648 <target>GOSA</target>
649 <source>GOSA</source>
650 <where>
651 <clause>
652 <phrase>
653 <operator>eq</operator>
654 <id>".$id."</id>
655 </phrase>
656 </clause>
657 </where>
658 </xml>";
660 if($this->connect()){
661 $this->o_sock->write($xml_msg);
662 $str = trim($this->o_sock->read());
664 /* Check if something went wrong while reading */
665 if($this->o_sock->is_error()){
666 $this->set_error($this->o_sock->get_error());
667 return(FALSE);
668 }
670 $entries = $this->xml_to_array($str);
671 if( isset($entries['XML']['HEADER']) &&
672 $entries['XML']['HEADER']=="answer" &&
673 isset($entries['XML']['ANSWER1'])){
674 return(TRUE);
675 }
676 }
677 return(FALSE);
678 }
681 /*! \brief Returns an entry from the gosaSupportQueue
682 @param Integer The ID of the entry we want to return.
683 @return Array Of the requested entry.
684 */
685 public function get_entry_by_id($id)
686 {
687 if(!is_numeric($id)){
688 trigger_error("Requires an integer as parameter.");
689 return;
690 }
691 $this->reset_error();
693 $ret = array();
694 $xml_msg = "<xml>
695 <header>gosa_query_jobdb</header>
696 <target>GOSA</target>
697 <source>GOSA</source>
698 <where>
699 <clause>
700 <phrase>
701 <operator>eq</operator>
702 <id>".$id."</id>
703 </phrase>
704 </clause>
705 </where>
706 </xml>";
707 if($this->connect()){
708 $this->o_sock->write($xml_msg);
709 $str = trim($this->o_sock->read());
711 /* Check if something went wrong while reading */
712 if($this->o_sock->is_error()){
713 $this->set_error($this->o_sock->get_error());
714 return($ret);
715 }
717 $entries = $this->xml_to_array($str);
718 if( isset($entries['XML']['HEADER']) &&
719 $entries['XML']['HEADER']=="answer" &&
720 isset($entries['XML']['ANSWER1'])){
721 $ret = $entries['XML']['ANSWER1'];
722 }
723 }
724 return($ret);
725 }
728 /*! \brief Removes a set of entries from the GOsa support queue.
729 @param Array The IDs to remove.
730 @return Boolean True on success.
731 */
732 public function remove_entries($ids)
733 {
734 if(!is_array($ids)){
735 trigger_error("Requires an array as parameter.");
736 return;
737 }
739 $this->reset_error();
741 $ret = array();
743 $xml_msg = "<xml>
744 <header>gosa_delete_jobdb_entry</header>
745 <target>GOSA</target>
746 <source>GOSA</source>
747 <where>
748 <clause>
749 <connector>or</connector>";
750 foreach($ids as $id){
751 $xml_msg .= "<phrase>
752 <operator>eq</operator>
753 <id>".$id."</id>
754 </phrase>";
755 }
756 $xml_msg .= "</clause>
757 </where>
758 </xml>";
760 if($this->connect()){
761 $this->o_sock->write($xml_msg);
762 $str = $this->o_sock->read();
764 /* Check if something went wrong while reading */
765 if($this->o_sock->is_error()){
766 $this->set_error($this->o_sock->get_error());
767 return($ret);
768 }
770 $entries = $this->xml_to_array($str);
771 if(isset($entries['XML']) || isset($entries['COUNT'])){
772 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
773 return(TRUE);
774 }else{
775 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
776 }
777 }
778 return(FALSE);
779 }
783 /*! \brief Removes an entry from the GOsa support queue.
784 @param Integer The ID of the entry we want to remove.
785 @return Boolean True on success.
786 */
787 public function remove_entry($id)
788 {
789 return($this->remove_entries(array($id)));
790 }
793 /*! \brief Parses the given xml string into an array
794 @param String XML string
795 @return Array Returns an array containing the xml structure.
796 */
797 private function xml_to_array($xml)
798 {
799 $params = array();
800 $level = array();
801 $parser = xml_parser_create_ns();
802 xml_parse_into_struct($parser, $xml, $vals, $index);
804 $err_id = xml_get_error_code($parser);
805 if($err_id){
806 xml_parser_free($parser);
807 }else{
808 xml_parser_free($parser);
810 foreach ($vals as $xml_elem) {
811 if ($xml_elem['type'] == 'open') {
812 if (array_key_exists('attributes',$xml_elem)) {
813 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
814 } else {
815 $level[$xml_elem['level']] = $xml_elem['tag'];
816 }
817 }
818 if ($xml_elem['type'] == 'complete') {
819 $start_level = 1;
820 $php_stmt = '$params';
821 while($start_level < $xml_elem['level']) {
822 $php_stmt .= '[$level['.$start_level.']]';
823 $start_level++;
824 }
825 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
826 @eval($php_stmt);
827 }
828 }
829 }
831 if(!isset($params['XML'])){
832 if (!array_key_exists('XML', $params)){
833 $this->set_error(_("Cannot not parse XML!"));
834 }
835 $params = array("COUNT" => 0);
836 }
838 return($params);
839 }
842 /*! \brief Updates an entry with a set of new values,
843 @param Integer The ID of the entry, we want to update.
844 @param Array The variables to update.
845 @return Boolean Returns TRUE on success.
846 */
847 public function update_entries($ids,$data)
848 {
849 $this->reset_error();
850 if(!is_array($ids)){
851 trigger_error("Requires an array as first parameter.");
852 return;
853 }
855 if(!is_array($data)){
856 trigger_error("Requires an array as second parameter.");
857 return;
858 }
860 $attr = "";
861 foreach($data as $key => $value){
862 if(is_array($value)){
863 foreach($value as $sub_value){
864 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
865 }
866 }else{
867 $attr.= "<$key>".strtolower($value)."</$key>\n";
868 }
869 }
871 $xml_msg = "<xml>
872 <header>gosa_update_status_jobdb_entry</header>
873 <target>GOSA</target>
874 <source>GOSA</source>
875 <where>
876 <clause>
877 <connector>or</connector>";
878 foreach($ids as $id){
879 $xml_msg .= "<phrase>
880 <operator>eq</operator>
881 <id>".$id."</id>
882 </phrase>";
883 }
884 $xml_msg .= "</clause>
885 </where>
886 <update>
887 ".$attr."
888 </update>
889 </xml>";
891 if($this->connect()){
893 $this->o_sock->write($xml_msg);
894 $str = trim($this->o_sock->read());
896 /* Check if something went wrong while reading */
897 if($this->o_sock->is_error()){
898 $this->set_error($this->o_sock->get_error());
899 return(FALSE);
900 }
902 $entries = $this->xml_to_array($str);
903 if(isset($entries['XML'])){
904 if(isset($entries['XML']['ERROR_STRING'])) {
905 $this->set_error($entries['XML']['ERROR_STRING']);
906 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
907 return(FALSE);
908 }
909 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
910 return(TRUE);
911 }
912 }
913 return(FALSE);
914 }
917 /*! \brief Returns the number of currently queued objects.
918 @return Integer
919 */
920 public function number_of_queued_entries($event_types)
921 {
922 $tags = "";
923 foreach($event_types as $type){
924 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
925 }
926 if(count($event_types) > 1){
927 $tags = "<connector>or</connector>".$tags;
928 }
929 if(count($event_types)){
930 $tags = "<where><clause>".$tags."</clause></where>";
931 }
934 $xml_msg =
935 "<xml>".
936 "<header>gosa_query_jobdb</header>".
937 "<target>GOSA</target>".
938 "<source>GOSA</source>".
939 "<select> count ID</select>".
940 $tags.
941 "</xml>";
943 $this->connect();
944 if($this->connect()){
945 $this->o_sock->write($xml_msg);
946 $str = trim($this->o_sock->read());
948 /* Check if something went wrong while reading */
949 if($this->o_sock->is_error()){
950 $this->set_error($this->o_sock->get_error());
951 return(0);
952 }
954 $entries = $this->xml_to_array($str);
955 if(isset($entries['XML'])){
956 return($entries['XML']['COUNT']);
957 }
958 }
959 return(-1);
960 }
963 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
964 {
965 $xml_message= "";
967 /* Prepare data */
968 foreach ($data as $key => $value){
969 if(is_array($value)){
970 foreach($value as $sub_value){
971 $xml_message.= "<$key>$sub_value</$key>";
972 }
973 }else{
974 $xml_message.= "<$key>$value</$key>";
975 }
976 }
978 /* Multiple targets? */
979 if (!is_array($to)){
980 $to_targets= array($to);
981 } else {
982 $to_targets= $to;
983 }
985 /* Build target strings */
986 $target ="";
987 foreach($to_targets as $to){
988 $target.= "<target>$to</target>";
989 }
991 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
992 }
995 /* Allows simply appending a new DaemonEvent
996 */
997 public function append($event)
998 {
999 if(!($event instanceof DaemonEvent)){
1000 return(FALSE);
1001 }
1003 $this->reset_error();
1005 /* Add to queue if new
1006 */
1007 if($event->is_new()){
1009 $request_answer = FALSE;
1010 if($event->get_type() == SCHEDULED_EVENT){
1011 $action = $event->get_schedule_action();
1012 }elseif($event->get_type() == TRIGGERED_EVENT){
1013 $action = $event->get_trigger_action();
1014 }else{
1015 trigger_error("Unknown type of queue event given.");
1016 return(FALSE);
1017 }
1019 /* Get event informations, like targets..
1020 */
1021 $targets = $event->get_targets();
1022 $data = $event->save();
1024 /* Append an entry for each target
1025 */
1026 foreach($targets as $target){
1027 $data['macaddress'] = $target;
1028 $this->send_data($action,$target,$data,$request_answer);
1030 if($this->is_error()){
1031 return(FALSE);
1032 }
1033 }
1034 return(TRUE);
1035 }else{
1037 /* Updated edited entry.
1038 */
1039 $id = $event->get_id();
1040 $data = $event->save();
1041 return($this->update_entries(array($id),$data));
1042 }
1044 return(FALSE);
1045 }
1048 /*! \brief Returns an array containing all queued entries.
1049 @return Array All queued entries as an array.
1050 */
1051 public function _send($data, $answer_expected= FALSE)
1052 {
1053 $this->reset_error();
1054 $ret = array();
1056 if($this->connect()){
1057 $this->o_sock->write($data);
1058 if ($answer_expected){
1059 $str = trim($this->o_sock->read());
1061 /* Check if something went wrong while reading */
1062 if($this->o_sock->is_error()){
1063 $this->set_error($this->o_sock->get_error());
1064 return($ret);
1065 }
1067 $entries = $this->xml_to_array($str);
1068 if(isset($entries['XML']) && is_array($entries['XML'])){
1069 $ret = $entries;
1070 if(isset($entries['XML']['ERROR_STRING'])) {
1071 $this->set_error($entries['XML']['ERROR_STRING']);
1072 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1073 }else{
1074 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1075 }
1076 }
1077 }else{
1078 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1079 }
1080 }
1081 return($ret);
1082 }
1085 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1086 {
1087 $xml_message= "";
1089 /* Get communication object */
1090 $d= new gosaSupportDaemon(TRUE,10);
1092 /* Prepare data */
1093 foreach ($data as $key => $value){
1094 if(is_array($value)){
1095 foreach($value as $sub_val){
1096 $xml_message.= "<$key>$sub_value</$key>";
1097 }
1098 }else{
1099 $xml_message.= "<$key>$value</$key>";
1100 }
1101 }
1103 /* Multiple targets? */
1104 if (!is_array($to)){
1105 $to_targets= array($to);
1106 } else {
1107 $to_targets= $to;
1108 }
1110 /* Build target strings */
1111 $target ="";
1112 foreach($to_targets as $to){
1113 $target.= "<target>$to</target>";
1114 }
1116 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1117 }
1120 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1121 @param String $mac The mac address for which we want to remove all jobs.
1122 */
1123 function clean_queue_from_mac($mac)
1124 {
1125 global $config;
1127 /* First of all we have to check which jobs are startet
1128 * for $mac
1129 */
1130 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1132 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1134 $data = $this->_send($xml_msg,TRUE);
1135 if(is_array($data) && isset($data['XML'])){
1136 $already_aborted = FALSE;
1137 foreach($data['XML'] as $name => $entry){
1138 if(preg_match("/answer[0-9]*/i",$name)){
1139 $entry['STATUS'] = strtoupper($entry['STATUS']);
1140 switch($entry['STATUS']){
1142 case 'PROCESSING' :
1144 /* Send abort event, but only once
1145 */
1146 if($already_aborted){
1147 break;
1148 }elseif(class_available("DaemonEvent_faireboot")){
1149 $already_aborted = TRUE;
1150 $tmp = new DaemonEvent_faireboot($config);
1151 $tmp->add_targets(array($mac));
1152 $tmp->set_type(TRIGGERED_EVENT);
1153 if(!$this->append($tmp)){
1154 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1155 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1156 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1157 }else{
1158 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1159 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1160 }
1161 ;break;
1162 }else{
1163 /* Couldn't find abort event, just remove entry */
1164 }
1166 case 'WAITING':
1167 case 'ERROR':
1168 default :
1170 /* Simply remove entries from queue.
1171 * Failed or waiting events, can be removed without any trouble.
1172 */
1173 if(!$this->remove_entries(array($entry['ID']))){
1174 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1175 }
1176 ;break;
1177 }
1179 }
1180 }
1181 }
1182 }
1185 static function ping($target)
1186 {
1187 if (tests::is_mac($target)){
1188 /* Get communication object */
1189 $d= new gosaSupportDaemon(TRUE,0.5);
1190 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1191 return (count($answer) ? TRUE:FALSE);
1192 }
1194 return (FALSE);
1195 }
1197 }
1199 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1200 ?>