4f77466ffb5b0f6664de8e2aeb05992d50fb4be3
1 <?php
3 class servdnseditZone extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account= TRUE;
12 var $attributes = array("zoneName","ReverseZone","dNSTTL","dNSClass",
13 "sOAprimary","sOAmail","sOAserial","sOArefresh","sOAretry","sOAexpire","sOAttl");
14 var $objectclasses = array("whatever");
16 var $RecordTypes = array();
18 var $ReverseZone = "";
19 var $ReverseDN = "";
20 var $zoneName = "";
21 var $dNSTTL = "7200";
22 var $dNSClass = "IN";
23 var $status = "new";
25 var $sOAprimary = "";
26 var $sOAmail = "";
27 var $sOAserial = "";
28 var $sOArefresh = "3600";
29 var $sOAretry = "1800";
30 var $sOAexpire = "";
31 var $sOAttl = "6400";
33 var $Records = array();
35 var $InitiallyZoneName = "";
36 var $InitiallyReverseZone = "";
37 var $isNew = true;
39 function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
40 {
41 plugin::plugin ($config, $dn);
43 /* All types with required attrs */
44 $this->RecordTypes = $recordtypes;
46 if(!count($attrs)){
47 $this->InitiallyZoneName = "";
48 $this->InitiallyReverseZone = "";
49 $this->isNew = true;
50 }else{
51 $this->InitiallyZoneName = $attrs['zoneName'];
52 $this->InitiallyReverseZone = $attrs['ReverseZone'];
53 $this->isNew = false;
55 foreach($this->attributes as $value){
56 $this->$value = $attrs[$value];
57 }
58 $this->Records = $attrs['Records'];
59 }
60 }
62 function execute()
63 {
64 /* Call parent execute */
65 plugin::execute();
67 /* Fill templating stuff */
68 $smarty= get_smarty();
69 $display= "";
71 /* Handle Post events */
72 $once = true;
73 foreach($_POST as $name => $value){
75 /* Delete record if requested */
76 if((preg_match("/RemoveRecord_/",$name))&&($once)){
77 $once = false;
78 $id= preg_replace("/RemoveRecord_/","",$name);
79 if($this->Records[$id]['status']!= "new"){
80 $this->Records[$id]['status']= "deleted";
81 }else{
82 unset($this->Records[$id]);
83 }
84 }
85 }
87 /* Add new Zonerecord */
88 if(isset($_POST['AddNewRecord'])){
89 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
90 }
92 /* Fill in values */
93 foreach($this->attributes as $name){
94 $smarty->assign($name,$this->$name);
95 }
97 /* Assign records list */
98 $smarty->assign("records",$this->generateRecordsList());
100 /* Display tempalte */
101 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
102 return($display);
103 }
105 function remove_from_parent()
106 {
107 }
109 /* Save data to object */
110 function save_object()
111 {
112 //plugin::save_object();
113 foreach($this->attributes as $attr){
114 if(isset($_POST[$attr])){
115 $this->$attr = $_POST[$attr];
116 }
117 }
119 foreach($this->Records as $id => $value){
120 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
121 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
122 }
123 if(isset($_POST['RecordValue_'.$id])){
124 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
125 }
126 }
127 }
130 /* Check supplied data */
131 function check()
132 {
133 $message= array();
134 /* Check if zoneName is already in use */
135 $usedZones = $this->getUsedZoneNames();
136 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
137 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitiallyZoneName)){
138 $message[] =_("This zoneName is already in use");
139 }
140 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
141 $message[] =_("This reverse zone is already in use");
142 }
143 }
144 return ($message);
145 }
147 function getUsedZoneNames()
148 {
149 $ret = array();
150 $ldap = $this->config->get_ldap_link();
151 $ldap->cd($this->config->current['BASE']);
152 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
153 while($attr = $ldap->fetch()){
154 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
155 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
156 $ret[$zn] = preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]);
157 }else{
158 $ret[$attr['zoneName'][0]]="";
159 }
160 }
161 return($ret);
162 }
165 /* Save to LDAP */
166 function save()
167 {
168 $ret =array();
169 foreach($this->attributes as $name){
170 $ret[$name] = $this->$name;
171 }
172 $ret['Records'] = $this->Records;
173 return($ret);
174 }
176 function generateRecordsList($changeStateForRecords="")
177 {
178 $changeStateForRecords = "";
180 $str = "<table summary=''>";
181 foreach($this->Records as $key => $entry){
182 if($entry['status'] == "deleted") continue;
184 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
185 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
186 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
188 $str.=" <tr>".
189 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
190 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
191 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
192 "</tr>";
193 }
195 $str.= " <tr>".
196 " <td colspan=2></td><td>".
197 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
198 " </td>".
199 " </tr>".
200 "</table>";
201 return($str);
202 }
204 function generateRecordListBox($selected,$name)
205 {
206 $str = "<select name='".$name."' id='".$name."'>";
207 foreach($this->RecordTypes as $type => $value){
208 $use = "";
209 if($type == $selected){
210 $use = " selected ";
211 }
212 $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
213 }
214 $str.="</select>";
215 return($str);
216 }
217 }
219 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
220 ?>