487fa5cf407c03716b2e9179b7b18bda5384b13c
1 <?php
3 class gosaSupportDaemon
4 {
5 private $o_sock = NULL;
6 private $s_host = "";
7 private $i_port = 0;
8 private $f_timeout = 0.2;
10 private $is_connected = FALSE;
11 private $s_encryption_key = "";
13 private $s_error = "";
14 private $b_error = FALSE;
17 /*! \brief Creates a new gosaSupportDaemon object.
18 @param string Host The Host where the deamon is running on.
19 @param integer Port The port which the deamon use.
20 @param string Key The encryption string.
21 @param boolean Connect Directly connect to deamon socket.
22 @param float Timeout The timelimit for all socket actions.
23 */
24 public function __construct($host,$port,$key="secret-gosa-password",$connect=TRUE,$timeout=0.2)
25 {
26 $this->s_host = $host;
27 $this->i_port = $port;
28 $this->f_timeout = $timeout;
29 $this->s_encryption_key = $key;
30 if($connect){
31 $this->connect();
32 }
33 }
36 /*! \brief Establish deamon connection.
37 @return boolean Returns true if the connection was succesfully established.
38 */
39 public function connect()
40 {
41 $this->o_sock = new Socket_Client($this->s_host,$this->i_port,TRUE,$this->f_timeout);
42 if($this->o_sock->connected()){
43 $this->o_sock->setEncryptionKey($this->s_encryption_key);
44 $this->is_connected = TRUE;
45 }else{
46 $this->error = $this->o_sock->get_error();
47 $this->is_connected = FALSE;
48 }
49 }
52 /*! \brief Disconnect from gosa deamon.
53 */
54 public function disconnect()
55 {
56 $this->o_sock->close();
57 $this->is_connected = FALSE;
58 }
61 /*! \brief Sets an error message, which can be returned with get_error().
62 @param string The Error message,
63 */
64 private function set_error($str)
65 {
66 $this->b_error = TRUE;
67 $this->s_error = $str;
68 }
71 /*! \brief Checks if an error occured.
72 @return boolean returns TRUE or FALSE, whether there is an error or not.
73 */
74 public function is_error()
75 {
76 return($this->b_error);
77 }
80 /*! \brief Returns the last error.
81 @return Returns the last error.
82 */
83 public function get_error()
84 {
85 return($this->s_error);
86 }
89 /*! \brief Returns an array containing all queued entries.
90 @return Array All queued entries as an array.
91 */
92 public function get_queued_entries()
93 {
94 $this->b_error = FALSE;
95 $this->s_error = "";
98 $xml_msg = "<xml>
99 <header>gosa_query_jobdb</header>
100 <where>
101 <clause>
102 <phrase>
103 <operator>gt</operator>
104 <id>-1</id>
105 </phrase>
106 </clause>
107 </where>
108 </xml>";
110 $this->connect();
111 if($this->is_connected){
112 $this->o_sock->write($xml_msg);
113 $str = trim($this->o_sock->read());
114 $entries = $this->xml_to_array($str);
116 if(!array_key_exists("XML",$entries)){
117 $this->set_error("!!!Couldn't parse xml.");
118 $this->disconnect();
119 return;
120 }else{
121 if(!is_array($entries['XML'])) {
122 $ret = array();
123 }else{
124 $ret = $entries['XML'];
125 }
126 }
127 return($ret);
128 }
129 $this->set_error("Could not establish socket connection.");
130 $this->disconnect();
131 return;
132 }
135 /*! \brief Checks if the given id is in use.
136 @param Integer The ID of the entry.
137 @return Boolean TRUE if entry exists.
138 */
139 public function id_exists($id)
140 {
141 if(!is_numeric($id)){
142 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
143 return;
144 }
146 $this->b_error = FALSE;
147 $this->s_error = "";
148 $xml_msg = "<xml>
149 <header>gosa_query_jobdb</header>
150 <where>id</where>
151 <id>".$id."</id>
152 </xml>";
153 $this->connect();
154 if(!$this->is_connected){
155 $this->set_error("Could not establish socket connection.");
156 }else{
157 $this->o_sock->write($xml_msg);
158 $str = trim($this->o_sock->read());
159 $entries = $this->xml_to_array($str);
160 if(isset($entries['XML']['ANSWER1'])){
161 $this->disconnect();
162 return(TRUE);
163 }
164 }
165 $this->disconnect();
166 return(FALSE);
167 }
170 /*! \brief Returns an entry from the gosaSupportQueue
171 @param Integer The ID of the entry we want to return.
172 @return Array Of the requested entry.
173 */
174 public function get_entry($id)
175 {
176 if(!is_numeric($id)){
177 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
178 return;
179 }
181 $this->b_error = FALSE;
182 $this->s_error = "";
183 $xml_msg = "<xml>
184 <header>gosa_query_jobdb</header>
185 <where>id</where>
186 <id>".$id."</id>
187 </xml>";
188 $this->connect();
189 if(!$this->is_connected){
190 $this->set_error("Could not establish socket connection.");
191 }else{
192 $this->o_sock->write($xml_msg);
193 $str = trim($this->o_sock->read());
194 $entries = $this->xml_to_array($str);
195 if(!isset($entries['XML']['ANSWER1'])){
196 $this->set_error("Entry with id (".$id.") not found.");
197 $this->disconnect();
198 }else{
199 $ret = $entries['XML']['ANSWER1'];
200 return($ret);
201 }
202 }
203 return;
204 }
207 /*! \brief Removes an entry from the GOsa support queue.
208 @param Integer The ID of the entry we want to remove.
209 @return Boolean True on success.
210 */
211 public function remove_entry($id)
212 {
213 $this->b_error = FALSE;
214 $this->s_error = "";
216 $xml_msg = "<xml>
217 <header>gosa_delete_jobdb_entry</header>
218 <where>id</where>
219 <id>".$id."</id>
220 </xml>";
221 $this->connect();
222 if($this->is_connected){
223 $this->o_sock->write($xml_msg);
224 return(TRUE);
225 }
226 $this->set_error("Could not establish socket connection.");
227 return(FALSE);
228 }
231 /*! \brief Parses the given xml string into an array
232 @param String XML string
233 @return Array Returns an array containing the xml structure.
234 */
235 function xml_to_array($xml)
236 {
237 $params = array();
238 $level = array();
239 $parser = xml_parser_create_ns();
240 xml_parse_into_struct($parser, $xml, $vals, $index);
242 $err_id = xml_get_error_code($parser);
243 if($err_id){
244 $this->set_error(xml_error_string(xml_get_error_code($parser)));
245 xml_parser_free($parser);
246 }else{
247 xml_parser_free($parser);
249 foreach ($vals as $xml_elem) {
250 if ($xml_elem['type'] == 'open') {
251 if (array_key_exists('attributes',$xml_elem)) {
252 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
253 } else {
254 $level[$xml_elem['level']] = $xml_elem['tag'];
255 }
256 }
257 if ($xml_elem['type'] == 'complete') {
258 $start_level = 1;
259 $php_stmt = '$params';
260 while($start_level < $xml_elem['level']) {
261 $php_stmt .= '[$level['.$start_level.']]';
262 $start_level++;
263 }
264 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
265 @eval($php_stmt);
266 }
267 }
268 }
269 return($params);
270 }
273 /*! \brief Updates an entry with a set of new values,
274 @param Integer The ID of the entry, we want to update.
275 @param Array The variables to update.
276 @return Boolean Returns TRUE on success.
277 */
278 public function update_entry($id,$entry)
279 {
280 $this->b_error = FALSE;
281 $this->s_error = "";
282 if(!is_numeric($id)){
283 trigger_error("Requires an integer value as ID parameter.");
284 return;
285 }
287 if(!is_array($entry)){
288 trigger_error("Requires an array as second parameter.");
289 return;
290 }
292 $attr = "";
293 foreach($entry as $name => $entry){
294 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
295 }
297 $xml_msg = "<xml>
298 <header>gosa_update_status_jobdb_entry</header>
299 <where>
300 <id>".$id."</id>
301 </where>
302 <update>
303 ".$attr."
304 </update>
305 </xml>";
306 $this->connect();
307 if($this->is_connected){
308 $this->o_sock->write($xml_msg);
309 $str = trim($this->o_sock->read());
310 $entries = $this->xml_to_array($str);
311 if(!empty($str)){
312 return(TRUE);
313 }
314 return(FALSE);
315 }
316 $this->set_error("Could not establish socket connection.");
317 return(FALSE);
318 }
319 }
321 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
322 ?>