766344ea88f5feebc94c7d13638d6e257c2cfe52
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 if(isset($attrs['Records'])){
59 $this->Records = $attrs['Records'];
60 }else{
61 $this->Records = array();
62 }
63 }
64 }
66 function execute()
67 {
68 /* Call parent execute */
69 plugin::execute();
71 /* Fill templating stuff */
72 $smarty= get_smarty();
73 $display= "";
75 /* Handle Post events */
76 $once = true;
77 foreach($_POST as $name => $value){
79 /* Delete record if requested */
80 if((preg_match("/RemoveRecord_/",$name))&&($once)){
81 $once = false;
82 $id= preg_replace("/RemoveRecord_/","",$name);
83 if($this->Records[$id]['status']!= "new"){
84 $this->Records[$id]['status']= "deleted";
85 }else{
86 unset($this->Records[$id]);
87 }
88 }
89 }
91 /* Add new Zonerecord */
92 if(isset($_POST['AddNewRecord'])){
93 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
94 }
96 /* Fill in values */
97 foreach($this->attributes as $name){
98 $smarty->assign($name,$this->$name);
99 }
101 /* Assign records list */
102 $smarty->assign("records",$this->generateRecordsList());
104 /* Display tempalte */
105 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
106 return($display);
107 }
109 function remove_from_parent()
110 {
111 }
113 /* Save data to object */
114 function save_object()
115 {
116 //plugin::save_object();
117 foreach($this->attributes as $attr){
118 if(isset($_POST[$attr])){
119 $this->$attr = $_POST[$attr];
120 }
121 }
123 foreach($this->Records as $id => $value){
124 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
125 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
126 }
127 if(isset($_POST['RecordValue_'.$id])){
128 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
129 }
130 }
131 }
134 /* Check supplied data */
135 function check()
136 {
137 $message= array();
138 /* Check if zoneName is already in use */
139 $usedZones = $this->getUsedZoneNames();
140 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)||($this->ReverseZone != $this->InitiallyReverseZone)){
141 if((isset($usedZones[$this->zoneName]))&&($this->zoneName != $this->InitiallyZoneName)){
142 $message[] =_("This zoneName is already in use");
143 }
144 if((in_array($this->ReverseZone,$usedZones))&&($this->ReverseZone != $this->InitiallyReverseZone)){
145 $message[] =_("This reverse zone is already in use");
146 }
147 }
148 return ($message);
149 }
151 function getUsedZoneNames()
152 {
153 $ret = array();
154 $ldap = $this->config->get_ldap_link();
155 $ldap->cd($this->config->current['BASE']);
156 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
157 while($attr = $ldap->fetch()){
158 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
159 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
160 $ret[$zn] = preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]);
161 }else{
162 $ret[$attr['zoneName'][0]]="";
163 }
164 }
165 return($ret);
166 }
169 /* Save to LDAP */
170 function save()
171 {
172 $ret =array();
173 foreach($this->attributes as $name){
174 $ret[$name] = $this->$name;
175 }
176 $ret['Records'] = $this->Records;
177 return($ret);
178 }
180 function generateRecordsList($changeStateForRecords="")
181 {
182 $changeStateForRecords = "";
184 $str = "<table summary=''>";
185 foreach($this->Records as $key => $entry){
186 if($entry['status'] == "deleted") continue;
188 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
189 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
190 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
192 $str.=" <tr>".
193 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
194 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
195 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
196 "</tr>";
197 }
199 $str.= " <tr>".
200 " <td colspan=2></td><td>".
201 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
202 " </td>".
203 " </tr>".
204 "</table>";
205 return($str);
206 }
208 function generateRecordListBox($selected,$name)
209 {
210 $str = "<select name='".$name."' id='".$name."'>";
211 foreach($this->RecordTypes as $type => $value){
212 $use = "";
213 if($type == $selected){
214 $use = " selected ";
215 }
216 $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
217 }
218 $str.="</select>";
219 return($str);
220 }
221 }
223 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
224 ?>