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>
151 <clause>
152 <phrase>
153 <operator>eq</operator>
154 <id>".$id."</id>
155 </phrase>
156 </clause>
157 </where>
158 </xml>";
159 $this->connect();
160 if(!$this->is_connected){
161 $this->set_error("Could not establish socket connection.");
162 }else{
163 $this->o_sock->write($xml_msg);
164 $str = trim($this->o_sock->read());
165 $entries = $this->xml_to_array($str);
166 if(isset($entries['XML']['ANSWER1'])){
167 $this->disconnect();
168 return(TRUE);
169 }
170 }
171 $this->disconnect();
172 return(FALSE);
173 }
176 /*! \brief Returns an entry from the gosaSupportQueue
177 @param Integer The ID of the entry we want to return.
178 @return Array Of the requested entry.
179 */
180 public function get_entry($id)
181 {
182 if(!is_numeric($id)){
183 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
184 return;
185 }
187 $this->b_error = FALSE;
188 $this->s_error = "";
189 $xml_msg = "<xml>
190 <header>gosa_query_jobdb</header>
191 <where>
192 <clause>
193 <phrase>
194 <operator>eq</operator>
195 <id>".$id."</id>
196 </phrase>
197 </clause>
198 </where>
199 </xml>";
200 $this->connect();
201 if(!$this->is_connected){
202 $this->set_error("Could not establish socket connection.");
203 }else{
204 $this->o_sock->write($xml_msg);
205 $str = trim($this->o_sock->read());
206 $entries = $this->xml_to_array($str);
207 if(!isset($entries['XML']['ANSWER1'])){
208 $this->set_error("Entry with id (".$id.") not found.");
209 $this->disconnect();
210 }else{
211 $ret = $entries['XML']['ANSWER1'];
212 return($ret);
213 }
214 }
215 return;
216 }
219 /*! \brief Removes an entry from the GOsa support queue.
220 @param Integer The ID of the entry we want to remove.
221 @return Boolean True on success.
222 */
223 public function remove_entry($id)
224 {
225 $this->b_error = FALSE;
226 $this->s_error = "";
228 $xml_msg = "<xml>
229 <header>gosa_delete_jobdb_entry</header>
230 <where>
231 <clause>
232 <phrase>
233 <operator>eq</operator>
234 <id>".$id."</id>
235 </phrase>
236 </clause>
237 </where>
238 </xml>";
239 $this->connect();
240 if($this->is_connected){
241 $this->o_sock->write($xml_msg);
242 return(TRUE);
243 }
244 $this->set_error("Could not establish socket connection.");
245 return(FALSE);
246 }
249 /*! \brief Parses the given xml string into an array
250 @param String XML string
251 @return Array Returns an array containing the xml structure.
252 */
253 function xml_to_array($xml)
254 {
255 $params = array();
256 $level = array();
257 $parser = xml_parser_create_ns();
258 xml_parse_into_struct($parser, $xml, $vals, $index);
260 $err_id = xml_get_error_code($parser);
261 if($err_id){
262 $this->set_error(xml_error_string(xml_get_error_code($parser)));
263 xml_parser_free($parser);
264 }else{
265 xml_parser_free($parser);
267 foreach ($vals as $xml_elem) {
268 if ($xml_elem['type'] == 'open') {
269 if (array_key_exists('attributes',$xml_elem)) {
270 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
271 } else {
272 $level[$xml_elem['level']] = $xml_elem['tag'];
273 }
274 }
275 if ($xml_elem['type'] == 'complete') {
276 $start_level = 1;
277 $php_stmt = '$params';
278 while($start_level < $xml_elem['level']) {
279 $php_stmt .= '[$level['.$start_level.']]';
280 $start_level++;
281 }
282 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
283 @eval($php_stmt);
284 }
285 }
286 }
287 return($params);
288 }
291 /*! \brief Updates an entry with a set of new values,
292 @param Integer The ID of the entry, we want to update.
293 @param Array The variables to update.
294 @return Boolean Returns TRUE on success.
295 */
296 public function update_entry($id,$entry)
297 {
298 $this->b_error = FALSE;
299 $this->s_error = "";
300 if(!is_numeric($id)){
301 trigger_error("Requires an integer value as ID parameter.");
302 return;
303 }
305 if(!is_array($entry)){
306 trigger_error("Requires an array as second parameter.");
307 return;
308 }
310 $attr = "";
311 foreach($entry as $name => $entry){
312 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
313 }
315 $xml_msg = "<xml>
316 <header>gosa_update_status_jobdb_entry</header>
317 <where>
318 <id>".$id."</id>
319 </where>
320 <update>
321 ".$attr."
322 </update>
323 </xml>";
324 $this->connect();
325 if($this->is_connected){
326 $this->o_sock->write($xml_msg);
327 $str = trim($this->o_sock->read());
328 $entries = $this->xml_to_array($str);
329 if(!empty($str)){
330 return(TRUE);
331 }
332 return(FALSE);
333 }
334 $this->set_error("Could not establish socket connection.");
335 return(FALSE);
336 }
337 }
339 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
340 ?>