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 $isNew = true;
38 function servdnseditZone ($config, $dn= NULL,$recordtypes,$attrs = array())
39 {
40 plugin::plugin ($config, $dn);
42 /* All types with required attrs */
43 $this->RecordTypes = $recordtypes;
45 if(!count($attrs)){
46 $this->InitiallyZoneName = "";
47 $this->isNew = true;
48 }else{
49 $this->InitiallyZoneName = $attrs['zoneName'];
50 $this->isNew = false;
52 foreach($this->attributes as $value){
53 $this->$value = $attrs[$value];
54 }
55 $this->Records = $attrs['Records'];
56 }
57 }
59 function execute()
60 {
61 /* Call parent execute */
62 plugin::execute();
64 /* Fill templating stuff */
65 $smarty= get_smarty();
66 $display= "";
68 /* Handle Post events */
69 $once = true;
70 foreach($_POST as $name => $value){
72 /* Delete record if requested */
73 if((preg_match("/RemoveRecord_/",$name))&&($once)){
74 $once = false;
75 $id= preg_replace("/RemoveRecord_/","",$name);
76 if($this->Records[$id]['status']!= "new"){
77 $this->Records[$id]['status']= "deleted";
78 }else{
79 unset($this->Records[$id]);
80 }
81 }
82 }
84 /* Add new Zonerecord */
85 if(isset($_POST['AddNewRecord'])){
86 $this->Records[] = array("type"=>"aRecord","inittype"=>"","value"=>"","status"=>"new");
87 }
89 /* Fill in values */
90 foreach($this->attributes as $name){
91 $smarty->assign($name,$this->$name);
92 }
94 /* Assign records list */
95 $smarty->assign("records",$this->generateRecordsList());
97 /* Display tempalte */
98 $display.= $smarty->fetch(get_template_path('servdnseditzone.tpl', TRUE));
99 return($display);
100 }
102 function remove_from_parent()
103 {
104 }
106 /* Save data to object */
107 function save_object()
108 {
109 //plugin::save_object();
110 foreach($this->attributes as $attr){
111 if(isset($_POST[$attr])){
112 $this->$attr = $_POST[$attr];
113 }
114 }
116 foreach($this->Records as $id => $value){
117 if(isset($_POST['RecordTypeSelectedFor_'.$id])){
118 $this->Records[$id]['type'] = $_POST['RecordTypeSelectedFor_'.$id];
119 }
120 if(isset($_POST['RecordValue_'.$id])){
121 $this->Records[$id]['value'] = $_POST['RecordValue_'.$id];
122 }
123 }
124 }
127 /* Check supplied data */
128 function check()
129 {
130 $message= array();
131 /* Check if zoneName is already in use */
132 $usedZones = $this->getUsedZoneNames();
133 if(($this->isNew == true)||($this->zoneName != $this->InitiallyZoneName)){
134 if(isset($usedZones[$this->zoneName])){
135 $message[] =_("This zoneName is already in use");
136 }
137 if(in_array($this->ReverseZone,$usedZones)){
138 $message[] =_("This reverse zone is already in use");
139 }
140 }
141 return ($message);
142 }
144 function getUsedZoneNames()
145 {
146 $ret = array();
147 $ldap = $this->config->get_ldap_link();
148 $ldap->cd($this->config->current['BASE']);
149 $ldap->search("(&(objectClass=dNSZone)(relativeDomainName=@)(zoneName=*))",array("zoneName","tXTRecord"));
150 while($attr = $ldap->fetch()){
151 if(preg_match("/in-addr\.arpa/",$attr['zoneName'][0])){
152 $zn = preg_replace("/zoneName\=/","",$attr['tXTRecord'][0]);
153 $ret[$zn] = preg_replace("/\.in-addr\.arpa/","",$attr['zoneName'][0]);
154 }else{
155 $ret[$attr['zoneName'][0]]="";
156 }
157 }
158 return($ret);
159 }
162 /* Save to LDAP */
163 function save()
164 {
165 $ret =array();
166 foreach($this->attributes as $name){
167 $ret[$name] = $this->$name;
168 }
169 $ret['Records'] = $this->Records;
170 return($ret);
171 }
173 function generateRecordsList($changeStateForRecords="")
174 {
175 $changeStateForRecords = "";
177 $str = "<table summary=''>";
178 foreach($this->Records as $key => $entry){
179 if($entry['status'] == "deleted") continue;
181 $changeStateForRecords.= "changeState('RecordTypeSelectedFor_".$key."');\n";
182 $changeStateForRecords.= "changeState('RecordValue_".$key."');\n";
183 $changeStateForRecords.= "changeState('RemoveRecord_".$key."');\n";
185 $str.=" <tr>".
186 " <td>".$this->generateRecordListBox($entry['type'],"RecordTypeSelectedFor_".$key)."</td>".
187 " <td><input type='text' value='".$entry['value']."' name='RecordValue_".$key."' id='RecordValue_".$key."'></td>".
188 " <td><input type='submit' name='RemoveRecord_".$key."' value='"._("Delete")."' id='RemoveRecord_".$key."'></td>".
189 "</tr>";
190 }
192 $str.= " <tr>".
193 " <td colspan=2></td><td>".
194 " <input type='submit' value='"._("Add")."' name='AddNewRecord'>".
195 " </td>".
196 " </tr>".
197 "</table>";
198 return($str);
199 }
201 function generateRecordListBox($selected,$name)
202 {
203 $str = "<select name='".$name."' id='".$name."'>";
204 foreach($this->RecordTypes as $type => $value){
205 $use = "";
206 if($type == $selected){
207 $use = " selected ";
208 }
209 $str.="\n <option value='".$type."' ".$use.">".$type."</option>";
210 }
211 $str.="</select>";
212 return($str);
213 }
214 }
216 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
217 ?>