1 <?php
3 class faiPartitionTable 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("cn","description");
13 var $objectclasses = array("top","FAIclass","FAIpartitionTable");
15 /* Specific attributes */
16 var $cn = ""; // The class name for this object
17 var $description = ""; // The description for this set of partitions
18 var $disks = array(); // All defined Disks
19 var $is_dialog = false; // specifies which buttons will be shown to save or abort
20 var $dialog = NULL; // a dialog, e.g. new disk dialog
22 function faiPartitionTable ($config, $dn= NULL)
23 {
24 /* Load Attributes */
25 plugin::plugin ($config, $dn);
27 /* If "dn==new" we try to create a new entry
28 * Else we must read all objects from ldap which belong to this entry.
29 * First read disks from ldap ... and then the partition definitions for the disks.
30 */
31 if($dn != "new"){
32 $this->dn =$dn;
34 /* Read all disks from ldap taht are defined fot this partition table
35 */
36 $ldap = $this->config->get_ldap_link();
37 $ldap->cd ($this->dn);
38 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionDisk))",array("*"));
39 while($object = $ldap->fetch()){
40 $this->disks[$object['cn'][0]]['status'] = "edited";
41 $this->disks[$object['cn'][0]]['dn'] = $object['dn'];
42 $this->disks[$object['cn'][0]]['cn'] = $object['cn'][0];
43 if(isset($object['description'][0])){
44 $this->disks[$object['cn'][0]]['description'] = $object['description'][0];
45 }else{
46 $this->disks[$object['cn'][0]]['description'] = "";
47 }
48 $this->disks[$object['cn'][0]]['partitions'] = array();
49 }
51 /* read all partitions for each disk
52 */
53 foreach($this->disks as $name => $disk){
54 $ldap->cd ($disk['dn']);
55 $ldap->search("(&(objectClass=FAIclass)(objectClass=FAIpartitionEntry))",array("*"));
56 while($partition = $ldap->fetch()){
58 /* remove count ... from ldap result
59 */
60 foreach($partition as $key=>$val){
61 if((is_numeric($key))||($key=="count")||($key=="dn")){
62 unset($partition[$key]);
63 }else{
64 $partition[$key] = $val[0];
65 }
66 }
68 /* Append fetched partitions
69 */
70 $partition['status']="edited";
71 $this->disks[$name]['partitions'][$partition['FAIpartitionNr']] = $partition;
72 }
73 }
74 }
75 }
77 function execute()
78 {
79 /* Fill templating stuff */
80 $smarty= get_smarty();
81 $display= "";
83 /* Add Disk to this Partitionset
84 * This code adds a new HDD to the disks
85 * A new Dialog will be opened
86 */
87 if(isset($_POST['AddDisk'])){
88 $usedDiskNames =array();
89 foreach($this->disks as $key=>$disk){
90 $usedDiskNames[]= $key;
91 }
92 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames);
93 $this->is_dialog = true;
94 }
96 /* Edit disk.
97 * Open dialog which allows us to edit the selected entry
98 */
99 if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
100 $usedDiskNames =array();
101 foreach($this->disks as $key=>$disk){
102 if($key != $_POST['disks']){
103 $usedDiskNames[]= $key;
104 }
105 }
106 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]);
107 $this->is_dialog = true;
108 }
110 /* Edit aborted, close dialog, without saving anything
111 */
112 if(isset($_POST['CancelDisk'])){
113 unset($this->dialog);
114 $this->dialog = NULL;
115 $this->is_dialog=false;
116 }
118 /* Dialog saved
119 * Save given data from Dialog, if no error is occured
120 */
121 if(isset($_POST['SaveDisk'])){
122 $this->dialog->save_object();
123 if(count($this->dialog->check())){
124 foreach($this->dialog->check() as $msg){
125 print_red($msg);
126 }
127 }else{
128 $disk = $this->dialog->save();
129 if(isset($disk['rename'])){
130 if($this->disks[$disk['rename']['from']]['status']=="edited"){
131 $this->disks[$disk['rename']['from']]['status']="delete";
132 }else{
133 unset($this->disks[$disk['rename']['from']]);
134 }
136 foreach($disk['partitions'] as $key => $val){
137 if($disk['partitions'][$key]['status']!="delete"){
138 $disk['partitions'][$key]['status']= "new";
139 }
140 }
142 $disk['status']="new";
143 $disk['cn']= $disk['rename']['to'];
144 }
146 $this->disks[$disk['cn']]=$disk;
147 unset($this->dialog);
148 $this->dialog = NULL;
149 $this->is_dialog=false;
150 }
151 }
153 /* Delete selected disk drive from list
154 * Assign delete status for all its partitions
155 */
156 if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
157 $disk = $_POST['disks'];
158 if($this->disks[$disk]['status']=="edited"){
159 $this->disks[$disk."-delete"]=$this->disks[$disk];
160 unset($this->disks[$disk]);
161 $disk = $disk."-delete";
162 $this->disks[$disk]['status']="delete";
163 foreach($this->disks[$disk]['partitions'] as $name => $value ){
164 if($value['status']=="edited"){
165 $this->disks[$disk]['partitions'][$name]['status']="delete";
166 }else{
167 unset($this->disks[$disk]['partitions'][$name]);
168 }
169 }
170 }else{
171 unset($this->disks[$disk]);
172 }
173 }
175 /* Display dialog if one is defined
176 */
177 if(isset($this->dialog)){
178 $this->dialog->save_object();
179 return($this->dialog->execute());
180 }
182 /* Assign all attributes to smarty engine
183 */
184 foreach($this->attributes as $attrs){
185 $smarty->assign($attrs,$this->$attrs);
186 if($this->$attrs){
187 $smarty->assign($attrs."CHK"," ");
188 }else{
189 $smarty->assign($attrs."CHK"," disabled ");
190 }
191 }
193 $disks = $this->getDisks();
194 $smarty->assign("disks" ,$disks);
195 $smarty->assign("diskKeys",array_flip($disks));
196 $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
197 return($display);
198 }
200 function getDisks(){
201 /* Return all available disks for this partition table
202 * Return in listBox friendly array
203 */
204 $a_return = array();
205 foreach($this->disks as $key => $disk){
206 if($disk['status'] != "delete"){
207 $cnt=0;
208 foreach($disk['partitions'] as $val){
209 if($val['status']!="delete"){
210 $cnt ++;
211 }
212 }
213 if(!empty($disk['description'])){
214 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
215 }else{
216 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
217 }
218 }
219 }
220 return($a_return);
221 }
224 /* Delete me, and all my subtrees
225 */
226 function remove_from_parent()
227 {
228 $ldap = $this->config->get_ldap_link();
229 $ldap->cd ($this->dn);
230 $ldap->rmdir_recursive($this->dn);
231 $this->handle_post_events("remove");
233 /* This cannot be removed... */
234 }
237 /* Save data to object
238 */
239 function save_object()
240 {
241 plugin::save_object();
242 foreach($this->attributes as $attrs){
243 if(isset($_POST[$attrs])){
244 $this->$attrs = $_POST[$attrs];
245 }
246 }
247 }
250 /* Check supplied data */
251 function check()
252 {
254 $message= array();
256 $str = utf8_encode("üöä");
257 if((empty($this->description))||(preg_match("/[^a-z0-9".$str."\.,;:\-_\? ]/i",$this->description))){
258 $message[]=_("Please enter a valid description.");
259 }
261 if((empty($this->cn))||(preg_match("/[^a-z0-9]/i",$this->cn))){
262 $message[]=_("Please enter a valid name.");
263 }
265 return ($message);
266 }
269 /* Save to LDAP */
270 function save()
271 {
273 plugin::save();
274 /* Save current settings.
275 * 1 : We must save the partition table, with its description and cn
276 * 2 : Append Disk with cn and description.
277 * 3 : Save partitions for each disk
278 */
280 $ldap = $this->config->get_ldap_link();
282 if($this->new){
283 $ldap->cd($this->dn);
284 $ldap->create_missing_trees($this->dn);
285 $ldap->cd($this->dn);
286 $ldap->add($this->attrs);
287 show_ldap_error($ldap->get_error());
288 }else{
289 /* Add partition table to ldap
290 */
291 $ldap->cd($this->dn);
292 $ldap->modify($this->attrs);
293 show_ldap_error($ldap->get_error());
294 }
297 /* Sort entries, because we must delete entries with status="delete" first */
298 $order = array();
299 foreach($this->disks as $key => $disk){
300 if($disk['status'] == "delete"){
301 $order[$key] = $disk;
302 }
303 }
304 foreach($this->disks as $key => $disk){
305 if($disk['status'] != "delete"){
306 $order[$key] = $disk;
307 }
308 }
310 /* Append all disks to ldap */
311 foreach($order as $cn=>$disk){
312 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
313 $disk_attrs['cn'] = $disk['cn'];
314 $disk_attrs['description'] = $disk['description'];
315 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
317 if($disk['status'] == "delete"){
318 $ldap->cd($disk_dn);
319 $ldap->rmdir_recursive($disk_dn);
320 }elseif($disk['status']== "edited"){
321 $ldap->cd($disk_dn);
322 $ldap->modify($disk_attrs);
323 }elseif($disk['status']== "new"){
324 $ldap->cd($disk_dn);
325 $ldap->create_missing_trees($disk_dn);
326 $ldap->cd($disk_dn);
327 $ldap->add($disk_attrs);
328 }else{
329 print_red("unknown status while saving disks");
330 }
332 show_ldap_error($ldap->get_error());
333 if($disk['status']!="delete")
334 /* Add all partitions */
335 foreach($disk['partitions'] as $key => $partition){
336 $partition_attrs = array();
338 foreach($partition as $key => $value){
339 if(!empty($value)){
340 $partition_attrs[$key]=$value;
341 }else{
342 unset($partition_attrs[$key]);
343 }
344 }
346 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
347 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
348 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
350 unset($partition_attrs['status']);
351 unset($partition_attrs['old_cn']);
353 if(($partition['status'] == "delete")&&($disk['status']!="new")){
354 $ldap->cd($partition_dn);
355 $ldap->rmdir_recursive($partition_dn);
356 }elseif($partition['status'] == "new"){
357 $ldap->cd($partition_dn);
358 $ldap->create_missing_trees($partition_dn);
359 $ldap->cd($partition_dn);
360 $ldap->add($partition_attrs);
361 }elseif($partition['status'] == "edited"){
362 $ldap->cd($partition_dn);
363 $ldap->modify($partition_attrs);
364 }
365 show_ldap_error($ldap->get_error());
366 }
367 }
368 $this->handle_post_events("add");
369 }
370 }
372 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
373 ?>