a993a3ad3046dba24de7dfdfd26cae8c8bd64bcc
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 = "<xml><header>gosa_get_available_kernel</header><source>GOSA</source><target>GOSA</target><release>".$release."</release></xml>";
141 if($this->connect()){
142 $this->o_sock->write($xml_msg);
143 $str = trim($this->o_sock->read());
145 /* Check if something went wrong while reading */
146 if($this->o_sock->is_error()){
147 $this->set_error($this->o_sock->get_error());
148 return($ret);
149 }
151 $entries = $this->xml_to_array($str);
152 if(isset($entries['XML']) && is_array($entries['XML'])){
154 /* Check if returned values represent a valid answer */
155 if(isset($entries['XML'])){
156 if(isset($entries['XML']['ERROR_STRING'])) {
157 $this->set_error($entries['XML']['ERROR_STRING']);
158 new log("debug","GOsa-si",
159 get_class($this)."::".__FUNCTION__, array(),
160 "FAILED error was ".$this->get_error());
161 return($ret);
162 }
164 /* Unset header tags */
165 $ret = $entries['XML'];
166 foreach($ret as $key => $entry){
167 if(!preg_match("/^answer/i",$key)){
168 unset($ret[$key]);
169 }
170 }
171 }
172 }
173 }
174 return($ret);
175 }
178 public function FAI_get_package_sections($release)
179 {
180 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
181 "<select>distinct section</select>".
182 "<where><clause><phrase><distribution>".$release."</distribution></phrase></clause></where></xml>";
184 if($this->connect()){
185 $this->o_sock->write($xml_msg);
186 $str = trim($this->o_sock->read());
188 /* Check if something went wrong while reading */
189 if($this->o_sock->is_error()){
190 $this->set_error($this->o_sock->get_error());
191 return($ret);
192 }
194 $entries = $this->xml_to_array($str);
195 if(isset($entries['XML']) && is_array($entries['XML'])){
197 /* Check if returned values represent a valid answer */
198 if(isset($entries['XML'])){
199 if(isset($entries['XML']['ERROR_STRING'])) {
200 $this->set_error($entries['XML']['ERROR_STRING']);
201 new log("debug","GOsa-si",
202 get_class($this)."::".__FUNCTION__, array(),
203 "FAILED error was ".$this->get_error());
204 return($ret);
205 }
207 /* Unset header tags */
208 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
209 if(isset($entries['XML'][$type])){
210 unset($entries['XML'][$type]);
211 }
212 }
213 $ret = $entries['XML'];
214 }
215 }
216 }
217 return($ret);
218 }
221 public function FAI_get_packages($release,$attrs,$package,$from=-1,$to=-1)
222 {
223 $this->reset_error();
224 $ret = array();
226 /* Check Parameter */
227 if(!is_array($attrs) || !count($attrs)){
228 trigger_error("Second parameter must be an array. With at least one attribute name.");
229 return($ret);
230 }
232 /* Check Parameter */
233 if(!is_array($package)){
234 trigger_error("Third parameter must be an array. With at least one attribute name.");
235 return($ret);
236 }
238 /* Create list of attributes to fetch */
239 $attr = "";
240 foreach($attrs as $at){
241 $attr.= "<select>".$at."</select>";
242 }
244 /* If no package is given, search for all */
245 if(!count($package)) $package = array("%");
247 /* Create limit tag */
248 if($from == -1){
249 $limit ="";
250 }else{
251 $limit = "<limit><from>".$from."</from><to>".$to."</to></limit>";
252 }
254 /* Create list of attributes to fetch */
255 $pkgs = "";
256 foreach($package as $pkg){
257 $pkgs .="<phrase><operator>like</operator><package>".$pkg."</package></phrase>";
258 }
260 $xml_msg = "<xml><header>gosa_query_packages_list</header><target>GOSA</target><source>GOSA</source>".
261 $attr.
262 "<where>
263 <clause><phrase><distribution>".$release."</distribution></phrase></clause>
264 <clause><connector>OR</connector>
265 ".$pkgs."
266 </clause>
267 </where>".
268 $limit.
269 "</xml>";
271 if($this->connect()){
272 $this->o_sock->write($xml_msg);
273 $str = trim($this->o_sock->read());
275 /* Check if something went wrong while reading */
276 if($this->o_sock->is_error()){
277 $this->set_error($this->o_sock->get_error());
278 return($ret);
279 }
281 $entries = $this->xml_to_array($str);
282 if(isset($entries['XML']) && is_array($entries['XML'])){
284 /* Check if returned values represent a valid answer */
285 if(isset($entries['XML'])){
286 if(isset($entries['XML']['ERROR_STRING'])) {
287 $this->set_error($entries['XML']['ERROR_STRING']);
288 new log("debug","GOsa-si",
289 get_class($this)."::".__FUNCTION__, array(),
290 "FAILED error was ".$this->get_error());
291 return($ret);
292 }
294 /* Unset header tags */
295 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
296 if(isset($entries['XML'][$type])){
297 unset($entries['XML'][$type]);
298 }
299 }
300 $ret = $entries['XML'];
301 }
302 }
303 }
304 return($ret);
307 }
310 public function FAI_get_server($name = "")
311 {
312 $this->reset_error();
314 $xml_msg = "<xml><header>gosa_query_fai_server</header><target>GOSA</target><source>GOSA</source></xml>";
315 $ret = array();
316 if($this->connect()){
317 $this->o_sock->write($xml_msg);
318 $str = trim($this->o_sock->read());
320 /* Check if something went wrong while reading */
321 if($this->o_sock->is_error()){
322 $this->set_error($this->o_sock->get_error());
323 return($ret);
324 }
326 $entries = $this->xml_to_array($str);
327 if(isset($entries['XML']) && is_array($entries['XML'])){
329 /* Check if returned values represent a valid answer */
330 if(isset($entries['XML'])){
331 if(isset($entries['XML']['ERROR_STRING'])) {
332 $this->set_error($entries['XML']['ERROR_STRING']);
333 new log("debug","GOsa-si",
334 get_class($this)."::".__FUNCTION__, array(),
335 "FAILED error was ".$this->get_error());
336 return($ret);
337 }
339 /* Unset header tags */
340 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
341 if(isset($entries['XML'][$type])){
342 unset($entries['XML'][$type]);
343 }
344 }
345 $ret = $entries['XML'];
346 }
347 }
348 }
349 return($ret);
350 }
353 public function FAI_get_classes($name)
354 {
355 $this->reset_error();
356 $xml_msg = "<xml><header>gosa_query_fai_release</header><target>GOSA</target><source>GOSA</source>".
357 "<where><clause><phrase><release>".$name."</release></phrase></clause></where></xml>";;
358 $ret = array();
359 if($this->connect()){
360 $this->o_sock->write($xml_msg);
361 $str = trim($this->o_sock->read());
363 /* Check if something went wrong while reading */
364 if($this->o_sock->is_error()){
365 $this->set_error($this->o_sock->get_error());
366 return($ret);
367 }
369 $entries = $this->xml_to_array($str);
370 if(isset($entries['XML']) && is_array($entries['XML'])){
372 /* Check if returned values represent a valid answer */
373 if(isset($entries['XML'])){
374 if(isset($entries['XML']['ERROR_STRING'])) {
375 $this->set_error($entries['XML']['ERROR_STRING']);
376 new log("debug","GOsa-si",
377 get_class($this)."::".__FUNCTION__, array($name),
378 "FAILED error was ".$this->get_error());
379 return($ret);
380 }
382 /* Unset header tags */
383 foreach(array("HEADER","SOURCE","TARGET","SESSION_ID") as $type){
384 if(isset($entries['XML'][$type])){
385 unset($entries['XML'][$type]);
386 }
387 }
388 $ret = $entries['XML'];
389 }
390 }
391 }
392 return($ret);
393 }
396 /*! \brief Returns an array containing all queued entries.
397 @return Array All queued entries as an array.
398 */
399 public function get_queued_entries($event_types = array("*"),$from=-1,$to=-1,$sort="timestamp DESC")
400 {
401 $this->reset_error();
402 $ret = array();
404 $tags = "";
405 foreach($event_types as $type){
406 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
407 }
408 if(count($event_types) > 1){
409 $tags = "<connector>or</connector>".$tags;
410 }
411 if(count($event_types)){
412 $tags = "<where><clause>".$tags."</clause></where>";
413 }
415 $xml_msg =
416 "<xml>
417 <header>gosa_query_jobdb</header>
418 <target>GOSA</target>
419 <source>GOSA</source>
420 ".$tags."
422 <orderby>".$sort."</orderby>";
423 if($from != -1 && $to != -1){
424 $xml_msg.= "
425 <limit>
426 <from>".$from."</from>
427 <to>".$to."</to>
428 </limit>";
429 }
430 $xml_msg.= "
431 </xml>";
433 echo htmlentities($xml_msg);
435 if($this->connect()){
436 $this->o_sock->write($xml_msg);
437 $str = trim($this->o_sock->read());
439 /* Check if something went wrong while reading */
440 if($this->o_sock->is_error()){
441 $this->set_error($this->o_sock->get_error());
442 return($ret);
443 }
445 $entries = $this->xml_to_array($str);
446 if(isset($entries['XML']) && is_array($entries['XML'])){
448 /* Check if returned values represent a valid answer */
449 if(isset($entries['XML'])){
451 /* Unset header tags */
452 foreach(array("HEADER","SOURCE","TARGET") as $type){
453 unset($entries['XML'][$type]);
454 }
455 $ret = $entries['XML'];
456 }
457 }
458 }
460 /* Remove session ID. No one is interested in this... */
461 unset($ret['SESSION_ID']);
463 return($ret);
464 }
467 /*! \brief Checks if the given ids are used queue ids.
468 @param Array The ids we want to check..
469 @return Array An array containing all ids as index and TRUE/FALSE as value.
470 */
471 public function ids_exist($ids)
472 {
473 if(!is_array($ids)){
474 trigger_error("Requires an array as parameter.");
475 return;
476 }
477 $this->reset_error();
479 $ret = array();
481 $xml_msg = "<xml>
482 <header>gosa_query_jobdb</header>
483 <target>GOSA</target>
484 <source>GOSA</source>
485 <where>
486 <clause>
487 <connector>or</connector>";
488 foreach($ids as $id){
489 $xml_msg .= "<phrase>
490 <operator>eq</operator>
491 <id>".$id."</id>
492 </phrase>";
493 }
494 $xml_msg .= "</clause>
495 </where>
496 </xml>";
498 if($this->connect()){
499 $this->o_sock->write($xml_msg);
500 $str = trim($this->o_sock->read());
502 /* Check if something went wrong while reading */
503 if($this->o_sock->is_error()){
504 $this->set_error($this->o_sock->get_error());
505 return($ret);
506 }
508 $entries = $this->xml_to_array($str);
509 if(isset($entries['XML']) && is_array($entries['XML'])){
510 foreach($entries['XML'] as $entry){
511 if(isset($entry['ID'])){
512 $ret[] = $entry['ID'];
513 }
514 }
515 }
516 }
517 return($ret);
518 }
521 /*! \brief Returns an entry containing all requested ids.
522 @param Array The IDs of the entries we want to return.
523 @return Array Of the requested entries.
524 */
525 public function get_entries_by_mac($macs)
526 {
527 if(!is_array($macs)){
528 trigger_error("Requires an array as parameter.");
529 return;
530 }
531 $this->reset_error();
533 $ret = array();
535 $xml_msg = "<xml>
536 <header>gosa_query_jobdb</header>
537 <target>GOSA</target>
538 <source>GOSA</source>
539 <where>
540 <clause>
541 <connector>or</connector>";
542 foreach($macs as $mac){
543 $xml_msg .= "<phrase>
544 <operator>eq</operator>
545 <macaddress>".$mac."</macaddress>
546 </phrase>";
547 }
548 $xml_msg .= "</clause>
549 </where>
550 </xml>";
552 if($this->connect()){
553 $this->o_sock->write($xml_msg);
554 $str = trim($this->o_sock->read());
556 /* Check if something went wrong while reading */
557 if($this->o_sock->is_error()){
558 $this->set_error($this->o_sock->get_error());
559 return($ret);
560 }
562 $entries = $this->xml_to_array($str);
563 if(isset($entries['XML'])){
564 foreach($entries['XML'] as $name => $entry){
565 if(preg_match("/^ANSWER[0-9]*$/",$name)){
566 $ret[$name] = $entry;
567 }
568 }
569 }
570 }
571 return($ret);
572 }
575 /*! \brief Returns an entry containing all requested ids.
576 @param Array The IDs of the entries we want to return.
577 @return Array Of the requested entries.
578 */
579 public function get_entries_by_id($ids)
580 {
581 if(!is_array($ids)){
582 trigger_error("Requires an array as parameter.");
583 return;
584 }
585 $this->reset_error();
587 $ret = array();
589 $xml_msg = "<xml>
590 <header>gosa_query_jobdb</header>
591 <target>GOSA</target>
592 <source>GOSA</source>
593 <where>
594 <clause>
595 <connector>or</connector>";
596 foreach($ids as $id){
597 $xml_msg .= "<phrase>
598 <operator>eq</operator>
599 <id>".$id."</id>
600 </phrase>";
601 }
602 $xml_msg .= "</clause>
603 </where>
604 </xml>";
606 if($this->connect()){
607 $this->o_sock->write($xml_msg);
608 $str = trim($this->o_sock->read());
610 /* Check if something went wrong while reading */
611 if($this->o_sock->is_error()){
612 $this->set_error($this->o_sock->get_error());
613 return($ret);
614 }
616 $entries = $this->xml_to_array($str);
617 if(isset($entries['XML'])){
618 foreach($entries['XML'] as $name => $entry){
619 if(preg_match("/^ANSWER[0-9]*$/",$name)){
620 $ret[$name] = $entry;
621 }
622 }
623 }
624 }
625 return($ret);
626 }
629 /*! \brief Checks if the given id is in use.
630 @param Integer The ID of the entry.
631 @return Boolean TRUE if entry exists.
632 */
633 public function id_exists($id)
634 {
635 if(!is_numeric($id)){
636 trigger_error("Requires an integer as parameter.");
637 return;
638 }
640 $this->reset_error();
642 $xml_msg = "<xml>
643 <header>gosa_query_jobdb</header>
644 <target>GOSA</target>
645 <source>GOSA</source>
646 <where>
647 <clause>
648 <phrase>
649 <operator>eq</operator>
650 <id>".$id."</id>
651 </phrase>
652 </clause>
653 </where>
654 </xml>";
656 if($this->connect()){
657 $this->o_sock->write($xml_msg);
658 $str = trim($this->o_sock->read());
660 /* Check if something went wrong while reading */
661 if($this->o_sock->is_error()){
662 $this->set_error($this->o_sock->get_error());
663 return(FALSE);
664 }
666 $entries = $this->xml_to_array($str);
667 if( isset($entries['XML']['HEADER']) &&
668 $entries['XML']['HEADER']=="answer" &&
669 isset($entries['XML']['ANSWER1'])){
670 return(TRUE);
671 }
672 }
673 return(FALSE);
674 }
677 /*! \brief Returns an entry from the gosaSupportQueue
678 @param Integer The ID of the entry we want to return.
679 @return Array Of the requested entry.
680 */
681 public function get_entry_by_id($id)
682 {
683 if(!is_numeric($id)){
684 trigger_error("Requires an integer as parameter.");
685 return;
686 }
687 $this->reset_error();
689 $ret = array();
690 $xml_msg = "<xml>
691 <header>gosa_query_jobdb</header>
692 <target>GOSA</target>
693 <source>GOSA</source>
694 <where>
695 <clause>
696 <phrase>
697 <operator>eq</operator>
698 <id>".$id."</id>
699 </phrase>
700 </clause>
701 </where>
702 </xml>";
703 if($this->connect()){
704 $this->o_sock->write($xml_msg);
705 $str = trim($this->o_sock->read());
707 /* Check if something went wrong while reading */
708 if($this->o_sock->is_error()){
709 $this->set_error($this->o_sock->get_error());
710 return($ret);
711 }
713 $entries = $this->xml_to_array($str);
714 if( isset($entries['XML']['HEADER']) &&
715 $entries['XML']['HEADER']=="answer" &&
716 isset($entries['XML']['ANSWER1'])){
717 $ret = $entries['XML']['ANSWER1'];
718 }
719 }
720 return($ret);
721 }
724 /*! \brief Removes a set of entries from the GOsa support queue.
725 @param Array The IDs to remove.
726 @return Boolean True on success.
727 */
728 public function remove_entries($ids)
729 {
730 if(!is_array($ids)){
731 trigger_error("Requires an array as parameter.");
732 return;
733 }
735 $this->reset_error();
737 $ret = array();
739 $xml_msg = "<xml>
740 <header>gosa_delete_jobdb_entry</header>
741 <target>GOSA</target>
742 <source>GOSA</source>
743 <where>
744 <clause>
745 <connector>or</connector>";
746 foreach($ids as $id){
747 $xml_msg .= "<phrase>
748 <operator>eq</operator>
749 <id>".$id."</id>
750 </phrase>";
751 }
752 $xml_msg .= "</clause>
753 </where>
754 </xml>";
756 if($this->connect()){
757 $this->o_sock->write($xml_msg);
758 $str = $this->o_sock->read();
760 /* Check if something went wrong while reading */
761 if($this->o_sock->is_error()){
762 $this->set_error($this->o_sock->get_error());
763 return($ret);
764 }
766 $entries = $this->xml_to_array($str);
767 if(isset($entries['XML']) || isset($entries['COUNT'])){
768 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"SUCCESS");
769 return(TRUE);
770 }else{
771 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::remove_entries()", $ids,"FAILED ".$this->get_error());
772 }
773 }
774 return(FALSE);
775 }
779 /*! \brief Removes an entry from the GOsa support queue.
780 @param Integer The ID of the entry we want to remove.
781 @return Boolean True on success.
782 */
783 public function remove_entry($id)
784 {
785 return($this->remove_entries(array($id)));
786 }
789 /*! \brief Parses the given xml string into an array
790 @param String XML string
791 @return Array Returns an array containing the xml structure.
792 */
793 private function xml_to_array($xml)
794 {
795 $params = array();
796 $level = array();
797 $parser = xml_parser_create_ns();
798 xml_parse_into_struct($parser, $xml, $vals, $index);
800 $err_id = xml_get_error_code($parser);
801 if($err_id){
802 xml_parser_free($parser);
803 }else{
804 xml_parser_free($parser);
806 foreach ($vals as $xml_elem) {
807 if ($xml_elem['type'] == 'open') {
808 if (array_key_exists('attributes',$xml_elem)) {
809 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
810 } else {
811 $level[$xml_elem['level']] = $xml_elem['tag'];
812 }
813 }
814 if ($xml_elem['type'] == 'complete') {
815 $start_level = 1;
816 $php_stmt = '$params';
817 while($start_level < $xml_elem['level']) {
818 $php_stmt .= '[$level['.$start_level.']]';
819 $start_level++;
820 }
821 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
822 @eval($php_stmt);
823 }
824 }
825 }
827 if(!isset($params['XML'])){
828 if (!array_key_exists('XML', $params)){
829 $this->set_error(_("Cannot not parse XML!"));
830 }
831 $params = array("COUNT" => 0);
832 }
834 return($params);
835 }
838 /*! \brief Updates an entry with a set of new values,
839 @param Integer The ID of the entry, we want to update.
840 @param Array The variables to update.
841 @return Boolean Returns TRUE on success.
842 */
843 public function update_entries($ids,$data)
844 {
845 $this->reset_error();
846 if(!is_array($ids)){
847 trigger_error("Requires an array as first parameter.");
848 return;
849 }
851 if(!is_array($data)){
852 trigger_error("Requires an array as second parameter.");
853 return;
854 }
856 $attr = "";
857 foreach($data as $key => $value){
858 if(is_array($value)){
859 foreach($value as $sub_value){
860 $attr.= "<$key>".strtolower($sub_value)."</$key>\n";
861 }
862 }else{
863 $attr.= "<$key>".strtolower($value)."</$key>\n";
864 }
865 }
867 $xml_msg = "<xml>
868 <header>gosa_update_status_jobdb_entry</header>
869 <target>GOSA</target>
870 <source>GOSA</source>
871 <where>
872 <clause>
873 <connector>or</connector>";
874 foreach($ids as $id){
875 $xml_msg .= "<phrase>
876 <operator>eq</operator>
877 <id>".$id."</id>
878 </phrase>";
879 }
880 $xml_msg .= "</clause>
881 </where>
882 <update>
883 ".$attr."
884 </update>
885 </xml>";
887 if($this->connect()){
889 $this->o_sock->write($xml_msg);
890 $str = trim($this->o_sock->read());
892 /* Check if something went wrong while reading */
893 if($this->o_sock->is_error()){
894 $this->set_error($this->o_sock->get_error());
895 return(FALSE);
896 }
898 $entries = $this->xml_to_array($str);
899 if(isset($entries['XML'])){
900 if(isset($entries['XML']['ERROR_STRING'])) {
901 $this->set_error($entries['XML']['ERROR_STRING']);
902 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"FAILED setting (".$attr.") error was ".$this->get_error());
903 return(FALSE);
904 }
905 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::update_entries()", $ids,"SUCCESS");
906 return(TRUE);
907 }
908 }
909 return(FALSE);
910 }
913 /*! \brief Returns the number of currently queued objects.
914 @return Integer
915 */
916 public function number_of_queued_entries($event_types)
917 {
918 $tags = "";
919 foreach($event_types as $type){
920 $tags .= "<phrase><headertag>".$type."</headertag></phrase>";
921 }
922 if(count($event_types) > 1){
923 $tags = "<connector>or</connector>".$tags;
924 }
925 if(count($event_types)){
926 $tags = "<where><clause>".$tags."</clause></where>";
927 }
930 $xml_msg =
931 "<xml>".
932 "<header>gosa_query_jobdb</header>".
933 "<target>GOSA</target>".
934 "<source>GOSA</source>".
935 "<select> count ID</select>".
936 $tags.
937 "</xml>";
939 $this->connect();
940 if($this->connect()){
941 $this->o_sock->write($xml_msg);
942 $str = trim($this->o_sock->read());
944 /* Check if something went wrong while reading */
945 if($this->o_sock->is_error()){
946 $this->set_error($this->o_sock->get_error());
947 return(0);
948 }
950 $entries = $this->xml_to_array($str);
951 print_a($entries);
952 if(isset($entries['XML'])){
953 return($entries['XML']['COUNT']);
954 }
955 }
956 return(-1);
957 }
960 public function send_data($header, $to, $data= array(), $answer_expected = FALSE)
961 {
962 $xml_message= "";
964 /* Prepare data */
965 foreach ($data as $key => $value){
966 if(is_array($value)){
967 foreach($value as $sub_value){
968 $xml_message.= "<$key>$sub_value</$key>";
969 }
970 }else{
971 $xml_message.= "<$key>$value</$key>";
972 }
973 }
975 /* Multiple targets? */
976 if (!is_array($to)){
977 $to_targets= array($to);
978 } else {
979 $to_targets= $to;
980 }
982 /* Build target strings */
983 $target ="";
984 foreach($to_targets as $to){
985 $target.= "<target>$to</target>";
986 }
988 return $this->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
989 }
992 /* Allows simply appending a new DaemonEvent
993 */
994 public function append($event)
995 {
996 if(!($event instanceof DaemonEvent)){
997 return(FALSE);
998 }
1000 $this->reset_error();
1002 /* Add to queue if new
1003 */
1004 if($event->is_new()){
1006 $request_answer = FALSE;
1007 if($event->get_type() == SCHEDULED_EVENT){
1008 $action = $event->get_schedule_action();
1009 }elseif($event->get_type() == TRIGGERED_EVENT){
1010 $action = $event->get_trigger_action();
1011 }else{
1012 trigger_error("Unknown type of queue event given.");
1013 return(FALSE);
1014 }
1016 /* Get event informations, like targets..
1017 */
1018 $targets = $event->get_targets();
1019 $data = $event->save();
1021 /* Append an entry for each target
1022 */
1023 foreach($targets as $target){
1024 $data['macaddress'] = $target;
1025 $this->send_data($action,$target,$data,$request_answer);
1027 if($this->is_error()){
1028 return(FALSE);
1029 }
1030 }
1031 return(TRUE);
1032 }else{
1034 /* Updated edited entry.
1035 */
1036 $id = $event->get_id();
1037 $data = $event->save();
1038 return($this->update_entries(array($id),$data));
1039 }
1041 return(FALSE);
1042 }
1045 /*! \brief Returns an array containing all queued entries.
1046 @return Array All queued entries as an array.
1047 */
1048 public function _send($data, $answer_expected= FALSE)
1049 {
1050 $this->reset_error();
1051 $ret = array();
1053 if($this->connect()){
1054 $this->o_sock->write($data);
1055 if ($answer_expected){
1056 $str = trim($this->o_sock->read());
1058 /* Check if something went wrong while reading */
1059 if($this->o_sock->is_error()){
1060 $this->set_error($this->o_sock->get_error());
1061 return($ret);
1062 }
1064 $entries = $this->xml_to_array($str);
1065 if(isset($entries['XML']) && is_array($entries['XML'])){
1066 $ret = $entries;
1067 if(isset($entries['XML']['ERROR_STRING'])) {
1068 $this->set_error($entries['XML']['ERROR_STRING']);
1069 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"FAILED ".$this->get_error());
1070 }else{
1071 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"SUCCESS");
1072 }
1073 }
1074 }else{
1075 new log("debug","DaemonEvent (IDS) ", "gosaSupportDaemon::_send()", array($data=>$data),"Fire & forget, not result.! ".$this->get_error());
1076 }
1077 }
1078 return($ret);
1079 }
1082 static function send($header, $to, $data= array(), $answer_expected = FALSE)
1083 {
1084 $xml_message= "";
1086 /* Get communication object */
1087 $d= new gosaSupportDaemon(TRUE,10);
1089 /* Prepare data */
1090 foreach ($data as $key => $value){
1091 if(is_array($value)){
1092 foreach($value as $sub_val){
1093 $xml_message.= "<$key>$sub_value</$key>";
1094 }
1095 }else{
1096 $xml_message.= "<$key>$value</$key>";
1097 }
1098 }
1100 /* Multiple targets? */
1101 if (!is_array($to)){
1102 $to_targets= array($to);
1103 } else {
1104 $to_targets= $to;
1105 }
1107 /* Build target strings */
1108 $target ="";
1109 foreach($to_targets as $to){
1110 $target.= "<target>$to</target>";
1111 }
1113 return $d->_send("<xml><header>$header</header><source>GOSA</source>$target".$xml_message."</xml>",$answer_expected);
1114 }
1117 /*! \brief Removes all jobs from the queue that are tiggered with a specific macAddress.
1118 @param String $mac The mac address for which we want to remove all jobs.
1119 */
1120 function clean_queue_from_mac($mac)
1121 {
1122 global $config;
1124 /* First of all we have to check which jobs are startet
1125 * for $mac
1126 */
1127 $xml_msg ="<xml><header>gosa_query_jobdb</header><target>GOSA</target><source>GOSA</source><where><clause><phrase><macaddress>".$mac."</macaddress></phrase></clause></where></xml>";
1129 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac)," start cleaning.");
1131 $data = $this->_send($xml_msg,TRUE);
1132 if(is_array($data) && isset($data['XML'])){
1133 $already_aborted = FALSE;
1134 foreach($data['XML'] as $name => $entry){
1135 if(preg_match("/answer[0-9]*/i",$name)){
1136 $entry['STATUS'] = strtoupper($entry['STATUS']);
1137 switch($entry['STATUS']){
1139 case 'PROCESSING' :
1141 /* Send abort event, but only once
1142 */
1143 if($already_aborted){
1144 break;
1145 }elseif(class_available("DaemonEvent_faireboot")){
1146 $already_aborted = TRUE;
1147 $tmp = new DaemonEvent_faireboot($config);
1148 $tmp->add_targets(array($mac));
1149 $tmp->set_type(TRIGGERED_EVENT);
1150 if(!$this->append($tmp)){
1151 msg_dialog::display(_("Error"), sprintf(_("Cannot send abort event for entry %s!"),$entry['ID']) , ERROR_DIALOG);
1152 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1153 "FAILED, could not send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].") - ".$this->get_error());
1154 }else{
1155 new log("debug","DaemonEvent ", "gosaSupportDaemon::clean_queue_from_mac()", array($mac => $mac),
1156 "SUCCESS, send 'DaemonEvent_faireboot' for entry ID (".$entry['ID'].")");
1157 }
1158 ;break;
1159 }else{
1160 /* Couldn't find abort event, just remove entry */
1161 }
1163 case 'WAITING':
1164 case 'ERROR':
1165 default :
1167 /* Simply remove entries from queue.
1168 * Failed or waiting events, can be removed without any trouble.
1169 */
1170 if(!$this->remove_entries(array($entry['ID']))){
1171 msg_dialog::display(_("Error"), sprintf(_("Cannot remove entry %s!"),$entry['ID']) , ERROR_DIALOG);
1172 }
1173 ;break;
1174 }
1176 }
1177 }
1178 }
1179 }
1182 static function ping($target)
1183 {
1184 if (tests::is_mac($target)){
1185 /* Get communication object */
1186 $d= new gosaSupportDaemon(TRUE,0.5);
1187 $answer= $d->_send("<xml><header>gosa_ping</header><source>GOSA</source><target>$target</target></xml>", TRUE);
1188 return (count($answer) ? TRUE:FALSE);
1189 }
1191 return (FALSE);
1192 }
1194 }
1196 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1197 ?>