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 $this->o_sock->setEncryptionKey($this->s_encryption_key);
43 if($this->o_sock->connected()){
44 $this->is_connected = TRUE;
45 }else{
46 $this->is_connected = FALSE;
47 }
48 }
51 /*! \brief Disconnect from gosa deamon.
52 */
53 public function disconnect()
54 {
55 $this->o_sock->close();
56 $this->is_connected = FALSE;
57 }
60 /*! \brief Sets an error message, which can be returned with get_error().
61 @param string The Error message,
62 */
63 private function set_error($str)
64 {
65 $this->b_error = TRUE;
66 $this->s_error = $str;
67 }
70 /*! \brief Checks if an error occured.
71 @return boolean returns TRUE or FALSE, whether there is an error or not.
72 */
73 public function is_error()
74 {
75 return($this->b_error);
76 }
79 /*! \brief Returns the last error.
80 @return Returns the last error.
81 */
82 public function get_error()
83 {
84 return($this->s_error);
85 }
88 /*! \brief Returns an array containing all queued entries.
89 @return Array All queued entries as an array.
90 */
91 public function get_queued_entries()
92 {
93 $this->b_error = FALSE;
94 $this->s_error = "";
96 $xml_msg = "<xml><header>gosa_query_jobdb</header><where><status>*</status></where></xml>";
97 $this->connect();
98 if($this->is_connected){
99 $this->o_sock->write($xml_msg);
100 $str = trim($this->o_sock->read());
101 $entries = $this->xml_to_array($str);
102 if(!isset($entries['XML'])){
103 $this->set_error("Couldn't parse xml.");
104 $this->disconnect();
105 return;
106 }else{
107 $ret = array_values($entries['XML']);
108 }
109 return($ret);
110 }
111 $this->set_error("Could not establish socket connection.");
112 $this->disconnect();
113 return;
114 }
117 /*! \brief Checks if the given id is in use.
118 @param Integer The ID of the entry.
119 @return Boolean TRUE if entry exists.
120 */
121 public function id_exists($id)
122 {
123 if(!is_numeric($id)){
124 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
125 return;
126 }
128 $this->b_error = FALSE;
129 $this->s_error = "";
130 $xml_msg = "<xml>
131 <header>gosa_query_jobdb</header>
132 <where>id</where>
133 <id>".$id."</id>
134 </xml>";
135 $this->connect();
136 if(!$this->is_connected){
137 $this->set_error("Could not establish socket connection.");
138 }else{
139 $this->o_sock->write($xml_msg);
140 $str = trim($this->o_sock->read());
141 $entries = $this->xml_to_array($str);
142 if(isset($entries['XML']['ANSWER1'])){
143 $this->disconnect();
144 return(TRUE);
145 }
146 }
147 $this->disconnect();
148 return(FALSE);
149 }
152 /*! \brief Returns an entry from the gosaSupportQueue
153 @param Integer The ID of the entry we want to return.
154 @return Array Of the requested entry.
155 */
156 public function get_entry($id)
157 {
158 if(!is_numeric($id)){
159 trigger_error("gosaSupportDaemon::get_entry() requires an integer value as ID parameter.");
160 return;
161 }
163 $this->b_error = FALSE;
164 $this->s_error = "";
165 $xml_msg = "<xml>
166 <header>gosa_query_jobdb</header>
167 <where>id</where>
168 <id>".$id."</id>
169 </xml>";
170 $this->connect();
171 if(!$this->is_connected){
172 $this->set_error("Could not establish socket connection.");
173 }else{
174 $this->o_sock->write($xml_msg);
175 $str = trim($this->o_sock->read());
176 $entries = $this->xml_to_array($str);
177 if(!isset($entries['XML']['ANSWER1'])){
178 $this->set_error("Entry with id (".$id.") not found.");
179 $this->disconnect();
180 }else{
181 $ret = $entries['XML']['ANSWER1'];
182 return($ret);
183 }
184 }
185 return;
186 }
189 /*! \brief Removes an entry from the GOsa support queue.
190 @param Integer The ID of the entry we want to remove.
191 @return Boolean True on success.
192 */
193 public function remove_entry($id)
194 {
195 $this->b_error = FALSE;
196 $this->s_error = "";
198 $xml_msg = "<xml>
199 <header>gosa_delete_jobdb_entry</header>
200 <where>id</where>
201 <id>".$id."</id>
202 </xml>";
203 $this->connect();
204 if($this->is_connected){
205 $this->o_sock->write($xml_msg);
206 return(TRUE);
207 }
208 $this->set_error("Could not establish socket connection.");
209 return(FALSE);
210 }
213 /*! \brief Parses the given xml string into an array
214 @param String XML string
215 @return Array Returns an array containing the xml structure.
216 */
217 function xml_to_array($xml)
218 {
219 $params = array();
220 $level = array();
221 $parser = xml_parser_create_ns();
222 xml_parse_into_struct($parser, $xml, $vals, $index);
224 $err_id = xml_get_error_code($parser);
225 if($err_id){
226 $this->set_error(xml_error_string(xml_get_error_code($parser)));
227 xml_parser_free($parser);
228 }else{
229 xml_parser_free($parser);
231 foreach ($vals as $xml_elem) {
232 if ($xml_elem['type'] == 'open') {
233 if (array_key_exists('attributes',$xml_elem)) {
234 list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
235 } else {
236 $level[$xml_elem['level']] = $xml_elem['tag'];
237 }
238 }
239 if ($xml_elem['type'] == 'complete') {
240 $start_level = 1;
241 $php_stmt = '$params';
242 while($start_level < $xml_elem['level']) {
243 $php_stmt .= '[$level['.$start_level.']]';
244 $start_level++;
245 }
246 $php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
247 eval($php_stmt);
248 }
249 }
250 }
251 return($params);
252 }
255 /*! \brief Updates an entry with a set of new values,
256 @param Integer The ID of the entry, we want to update.
257 @param Array The variables to update.
258 @return Boolean Returns TRUE on success.
259 */
260 public function update_entry($id,$entry)
261 {
262 $this->b_error = FALSE;
263 $this->s_error = "";
264 if(!is_numeric($id)){
265 trigger_error("Requires an integer value as ID parameter.");
266 return;
267 }
269 if(!is_array($entry)){
270 trigger_error("Requires an array as second parameter.");
271 return;
272 }
274 $attr = "";
275 foreach($entry as $name => $entry){
276 $attr.="<".strtolower($name).">".$entry."</".strtolower($name).">\n";
277 }
279 $xml_msg = "<xml>
280 <header>gosa_update_status_jobdb_entry</header>
281 <where>
282 <id>".$id."</id>
283 </where>
284 <update>
285 ".$attr."
286 </update>
287 </xml>";
288 $this->connect();
289 if($this->is_connected){
290 $this->o_sock->write($xml_msg);
291 $str = trim($this->o_sock->read());
292 $entries = $this->xml_to_array($str);
293 if(!empty($str)){
294 return(TRUE);
295 }
296 return(FALSE);
297 }
298 $this->set_error("Could not establish socket connection.");
299 return(FALSE);
300 }
301 }
303 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
304 ?>