f12ad0f8bcc77f0de704d79532a6409e11fbbd93
1 <?php
3 class servdns extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = FALSE;
7 var $attributes = array();
8 var $objectclasses = array("whatever");
10 var $RecordTypes = array();
11 var $Zones = array();
12 var $dialog = NULL;
14 var $orig_dn = "";
16 var $DNSinitially_was_account;
19 function servdns ($config, $dn= NULL, $parent= NULL)
20 {
21 plugin::plugin ($config, $dn, $parent);
23 $this->orig_dn = $dn;
25 /* Get record types for zones
26 */
27 $this->RecordTypes = getDnsRecordTypes(true);
29 /* Get all zone Informations
30 */
31 $this->Zones = getDNSZoneEntries($config,$dn);
33 /* If there is at least one entry in this -> types, we have DNS enabled
34 */
35 if(count($this->Zones) == 0){
36 $this->is_account = false;
37 }else{
38 $this->is_account = true;
39 }
40 $this->DNSinitially_was_account = $this->is_account;
41 }
44 function execute()
45 {
46 /* Call parent execute
47 */
48 plugin::execute();
50 /* Fill templating stuff
51 */
52 $smarty= get_smarty();
53 $display= "";
55 /* Do we need to flip is_account state?
56 */
57 if (isset($_POST['modify_state'])){
59 /* Only change account state if allowed */
60 if($this->is_account && $this->acl == "#all#"){
61 $this->is_account= !$this->is_account;
62 $this->is_modified = true;
63 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
64 $this->is_account= !$this->is_account;
65 $this->is_modified = true;
66 }
67 }
69 if ($this->is_account){
70 $display= $this->show_header(_("Remove DNS service"),
71 _("This server has DNS features enabled. You can disable them by clicking below."));
72 } else {
73 $display= $this->show_header(_("Add DNS service"),
74 _("This server has DNS features disabled. You can enable them by clicking below."));
75 return ($display);
76 }
79 /* Edited or Added zone
80 */
81 if((isset($_POST['SaveZoneChanges'])) && is_object($this->dialog)){
82 $this->dialog->save_object();
84 /* Check for errors
85 */
86 if(count($this->dialog->check())){
87 foreach($this->dialog->check() as $msgs){
88 print_red($msgs);
89 }
90 }else{
91 /* add new/edited zone
92 */
93 $ret = $this->dialog->save();
94 if(!$this->dialog->isNew){
95 unset($this->Zones[$this->dialog->OldZoneName]);
96 }
97 $this->Zones[$ret['zoneName']] = $ret;
98 $this->dialog = NULL;
99 }
100 }
102 /* Cancel zone edit / new
103 */
104 if(isset($_POST['CancelZoneChanges'])){
105 $this->dialog = NULL;
106 }
108 /* Add empty new zone
109 */
110 if(isset($_POST['AddZone']) && chkacl($this->acl,"servdns") == ""){
111 $this->dialog = new servdnseditZone($this->config,$this->dn);
112 }
114 /* Check for edit zone request
115 */
116 $once = false;
117 foreach( $_POST as $name => $value){
119 /* check all post for edit request
120 */
121 if(preg_match("/^editZone_/",$name)&&!$once && chkacl($this->acl,"servdns") == ""){
122 $once =true;
123 $tmp = preg_replace("/^editZone_/","",$name);
124 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
125 $this->dialog= new servdnseditZone($this->config,$this->dn,$this->Zones[$tmp]);
126 }
128 /* check posts for delete zone
129 */
130 if(preg_match("/^delZone_/",$name)&&!$once && chkacl($this->acl,"servdns") == ""){
132 $once =true;
133 $tmp = preg_replace("/^delZone_/","",$name);
134 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
136 /* Initiate deletion
137 */
138 $this->RemoveZone($tmp);
139 }
140 }
142 /* Show dialog
143 */
144 if($this->dialog!= NULL){
145 $this->dialog->save_object();
146 $this->dialog->parent = $this;
147 return($this->dialog->execute());
148 }
150 /* Create Listbox with existing Zones
151 */
152 $ZoneList = new divSelectBox("dNSZones");
153 $ZoneList -> SetHeight(300);
155 /* Add entries to divlist
156 */
157 $editImg = "<input type='image' src='images/edit.png' name='editZone_%s'>
158 <input type='image' src='images/edittrash.png' name='delZone_%s'>";
159 foreach($this->Zones as $zone => $values ){
160 $ZoneList->AddEntry(array(
161 array("string" => getNameFromMix($zone)),
162 array("string" => _("Reverse zone")." : ".getNameFromMix($values['ReverseZone'])),
163 array("string" => _("TTL")." : ".$values['sOAttl']),
164 array("string" => _("Class")." : ".$values['dNSClass']),
165 array("string" => str_replace("%s",base64_encode($zone),$editImg))
166 ));
167 }
169 $smarty->assign("servdnsACL",chkacl($this->acl,"servdns"));
171 /* Display tempalte
172 */
173 $smarty->assign("ZoneList",$ZoneList->DrawList());
174 $display.= $smarty->fetch(get_template_path('servdns.tpl', TRUE));
175 return($display);
176 }
179 /* Delete specified zone
180 */
181 function RemoveZone($id)
182 {
183 $zones = $this->getUsedZoneNames();
184 $rev ="";
186 if(isset($this->Zones[$id]['InitialReverseZone'])){
187 $rev = FlipIp(getNameFromMix($this->Zones[$id]['InitialReverseZone']));
188 }elseif(isset($this->Zones[$id]['ReverseZone'])){
189 $rev = FlipIp(getNameFromMix($this->Zones[$id]['ReverseZone']));
190 }
192 $zonename = "";
193 if(isset($this->Zones[$id]['InitialzoneName'])){
194 $zonename= getNameFromMix($this->Zones[$id]['InitialzoneName']);
195 }
197 $used = array();
199 /* Add Records which use this zoneName
200 */
201 if(isset($zones[$zonename])){
202 $used = array_merge($used,$zones[$zonename]);
203 }
205 /* Add Records which uses this reverse zone
206 */
207 if(isset($zones[$rev.".in-addr.arpa"])){
208 $used = array_merge($used,$zones[$rev.".in-addr.arpa"]);
209 }
211 /* There are still entries using this configuration
212 * Abort deletion
213 */
214 if(count($used)){
215 $i = 2;
216 $str ="";
217 foreach($used as $dn){
218 if($i > 0 ){
219 $i --;
220 $str.=$dn." ";
221 }
222 }
224 /* Only show 2 dns in the error message
225 */
226 if(count($used)> 2) {
227 $str .=" ... ";
228 }
229 print_red(sprintf(_("Can't delete the selected zone, because it is still in use by these entry/entries '%s'"),trim($str)));
231 }else{
232 unset($this->Zones[$id]);
233 return(true);
234 }
235 return(false);
236 }
239 /* This funtion returns all used Zonenames
240 */
241 function getUsedZoneNames()
242 {
243 $ret = array();
244 $ldap = $this->config->get_ldap_link();
245 $ldap->cd($this->config->current['BASE']);
246 $ldap->search("(&(objectClass=dNSZone)(!(relativeDomainName=@))(zoneName=*))",array("zoneName","relativeDomainName"));
247 while($attr = $ldap->fetch()){
248 $ret[$attr['zoneName'][0]][] = $attr['dn'];
249 }
250 return($ret);
251 }
255 /* Remove dns service
256 */
257 function remove_from_parent()
258 {
259 if($this->DNSinitially_was_account){
260 $bool = true;
261 foreach($this->Zones as $key => $zone){
262 $bool= $bool & $this->RemoveZone($key);
263 }
265 if($bool){
266 $this->save();
267 }
268 return($bool);
269 }
270 }
274 /* Save to LDAP */
275 function save()
276 {
277 $ldap = $this->config->get_ldap_link();
278 $ldap->cd($this->config->current['BASE']);
280 /* Get differences
281 */
283 $old_dn = $this->orig_dn;
284 if($old_dn == "new"){
285 $old_dn = $this->dn;
286 }
288 $tmp = getDNSZoneEntriesDiff($this->config,$this->Zones,$old_dn);
290 /* Updated zone entries if reverser or forward name has changed
291 * Must be done before moving entries, else the given dn is invalid
292 */
293 if(isset($tmp['zoneUpdates'])){
294 foreach($tmp['zoneUpdates'] as $dn => $attrs){
295 $ldap->cd($dn);
296 $ldap->modify($attrs);
297 show_ldap_error("Zone:".$ldap->get_error(), _("Updating DNS service failed"));
298 }
299 }
301 /* Delete dns
302 */
303 foreach($tmp['del'] as $dn => $del){
304 $ldap->cd($dn);
305 $ldap->rmdir_recursive($dn);
306 show_ldap_error($ldap->get_error(), _("Removing DNS entries failed"));
307 }
309 /* move follwoing entries
310 */
311 foreach($tmp['move'] as $src => $dst){
312 $this->recursive_move($src,$dst);
313 }
315 /* Add || Update new DNS entries
316 */
317 foreach($tmp['add'] as $dn => $attrs){
318 $ldap->cd($dn);
319 $ldap->cat($dn, array('dn'));
320 if(count($ldap->fetch())){
321 $ldap->cd($dn);
322 $ldap->modify ($attrs);
323 }else{
324 $ldap->cd($dn);
325 $ldap->add($attrs);
326 }
327 show_ldap_error($ldap->get_error(), _("Saving DNS entries failed"));
328 }
329 }
330 }
331 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
332 ?>