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 ksort($this->disks);
76 }
78 function execute()
79 {
80 /* Call parent execute */
81 plugin::execute();
83 /* Fill templating stuff */
84 $smarty= get_smarty();
85 $display= "";
87 /* Add Disk to this Partitionset
88 * This code adds a new HDD to the disks
89 * A new Dialog will be opened
90 */
91 if(isset($_POST['AddDisk'])){
92 $usedDiskNames =array();
93 foreach($this->disks as $key=>$disk){
94 $usedDiskNames[]= $key;
95 }
96 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames);
97 $this->is_dialog = true;
98 }
100 /* Edit disk.
101 * Open dialog which allows us to edit the selected entry
102 */
104 if($this->dn != "new"){
105 $_SESSION['objectinfo']= $this->dn;
106 }
108 if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
109 $usedDiskNames =array();
111 $Udisk = $_POST['disks'][0];
113 foreach($this->disks as $key=>$disk){
114 if($key != $Udisk){
115 $usedDiskNames[]= $key;
116 }
117 }
118 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$Udisk]);
119 $_SESSION['objectinfo'] = $this->disks[$Udisk]['dn'];
120 $this->is_dialog = true;
121 }
123 /* Edit aborted, close dialog, without saving anything
124 */
125 if(isset($_POST['CancelDisk'])){
126 unset($this->dialog);
127 $this->dialog = NULL;
128 $this->is_dialog=false;
129 }
131 /* Dialog saved
132 * Save given data from Dialog, if no error is occured
133 */
134 if(isset($_POST['SaveDisk'])){
135 $this->dialog->save_object();
136 if(count($this->dialog->check())){
137 foreach($this->dialog->check() as $msg){
138 print_red($msg);
139 }
140 }else{
141 $disk = $this->dialog->save();
142 if(isset($disk['rename'])){
143 if($this->disks[$disk['rename']['from']]['status']=="edited"){
144 $this->disks[$disk['rename']['from']]['status']="delete";
145 }else{
146 unset($this->disks[$disk['rename']['from']]);
147 }
149 foreach($disk['partitions'] as $key => $val){
150 if($disk['partitions'][$key]['status']!="delete"){
151 $disk['partitions'][$key]['status']= "new";
152 }
153 }
155 $disk['status']="new";
156 $disk['cn']= $disk['rename']['to'];
157 }
159 $this->disks[$disk['cn']]=$disk;
160 unset($this->dialog);
161 $this->dialog = NULL;
162 $this->is_dialog=false;
163 ksort($this->disks);
164 }
165 }
167 /* Delete selected disk drive from list
168 * Assign delete status for all its partitions
169 */
170 if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
172 foreach($_POST['disks'] as $disk) {
173 if($this->disks[$disk]['status']=="edited"){
174 $this->disks[$disk."-delete"]=$this->disks[$disk];
175 unset($this->disks[$disk]);
176 $disk = $disk."-delete";
177 $this->disks[$disk]['status']="delete";
178 foreach($this->disks[$disk]['partitions'] as $name => $value ){
179 if($value['status']=="edited"){
180 $this->disks[$disk]['partitions'][$name]['status']="delete";
181 }else{
182 unset($this->disks[$disk]['partitions'][$name]);
183 }
184 }
185 }else{
186 unset($this->disks[$disk]);
187 }
188 }
189 }
191 /* Display dialog if one is defined
192 */
193 if(isset($this->dialog)){
194 $this->dialog->save_object();
195 return($this->dialog->execute());
196 }
198 /* Assign all attributes to smarty engine
199 */
200 foreach($this->attributes as $attrs){
201 $smarty->assign($attrs,$this->$attrs);
202 if($this->$attrs){
203 $smarty->assign($attrs."CHK"," ");
204 }else{
205 $smarty->assign($attrs."CHK"," disabled ");
206 }
207 }
209 $disks = $this->getDisks();
210 $smarty->assign("disks" ,$disks);
211 $smarty->assign("diskKeys",array_flip($disks));
212 $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
213 return($display);
214 }
216 function getDisks(){
217 /* Return all available disks for this partition table
218 * Return in listBox friendly array
219 */
220 $a_return = array();
221 foreach($this->disks as $key => $disk){
222 if($disk['status'] != "delete"){
223 $cnt=0;
224 foreach($disk['partitions'] as $val){
225 if($val['status']!="delete"){
226 $cnt ++;
227 }
228 }
229 if(!empty($disk['description'])){
230 if($cnt == 1){
231 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
232 }else{
233 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
234 }
235 }else{
236 if($cnt == 1){
237 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
238 }else{
239 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
240 }
241 }
242 }
243 }
244 return($a_return);
245 }
248 /* Delete me, and all my subtrees
249 */
250 function remove_from_parent()
251 {
252 $ldap = $this->config->get_ldap_link();
253 $ldap->cd ($this->dn);
254 $ldap->rmdir_recursive($this->dn);
255 $this->handle_post_events("remove");
257 /* This cannot be removed... */
258 }
261 /* Save data to object
262 */
263 function save_object()
264 {
265 plugin::save_object();
266 foreach($this->attributes as $attrs){
267 if(isset($_POST[$attrs])){
268 $this->$attrs = $_POST[$attrs];
269 }
270 }
271 }
274 /* Check supplied data */
275 function check()
276 {
278 $message= array();
279 return ($message);
280 }
283 /* Save to LDAP */
284 function save()
285 {
287 plugin::save();
288 /* Save current settings.
289 * 1 : We must save the partition table, with its description and cn
290 * 2 : Append Disk with cn and description.
291 * 3 : Save partitions for each disk
292 */
294 $ldap = $this->config->get_ldap_link();
296 if($this->new){
297 $ldap->cd($this->config->current['BASE']);
298 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
299 $ldap->cd($this->dn);
300 $ldap->add($this->attrs);
301 show_ldap_error($ldap->get_error());
302 }else{
303 /* Add partition table to ldap
304 */
305 $ldap->cd($this->dn);
306 $ldap->modify($this->attrs);
307 show_ldap_error($ldap->get_error());
308 }
311 /* Sort entries, because we must delete entries with status="delete" first */
312 $order = array();
313 foreach($this->disks as $key => $disk){
314 if($disk['status'] == "delete"){
315 $order[$key] = $disk;
316 }
317 }
318 foreach($this->disks as $key => $disk){
319 if($disk['status'] != "delete"){
320 $order[$key] = $disk;
321 }
322 }
324 /* Append all disks to ldap */
325 foreach($order as $cn=>$disk){
326 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
327 $disk_attrs['cn'] = $disk['cn'];
328 $disk_attrs['description'] = $disk['description'];
329 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
331 if($disk['status']=="new"){
332 $ldap->cat($disk_dn);
333 if($ldap->count()){
334 $disk['status']="edited";
335 }
336 }
338 if($disk['status'] == "delete"){
339 $ldap->cd($disk_dn);
340 $ldap->rmdir_recursive($disk_dn);
341 }elseif($disk['status']== "edited"){
342 if(empty($disk_attrs['description'])){
343 $disk_attrs['description']=array();
344 }
345 $ldap->cd($disk_dn);
346 $ldap->modify($disk_attrs);
347 }elseif($disk['status']== "new"){
348 if(empty($disk_attrs['description'])){
349 unset($disk_attrs['description']);
350 }
351 $ldap->cd($this->config->current['BASE']);
352 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
353 $ldap->cd($disk_dn);
354 $ldap->add($disk_attrs);
355 }else{
356 print_red("unknown status while saving disks");
357 }
359 show_ldap_error($ldap->get_error());
360 if($disk['status']!="delete")
361 /* Add all partitions */
362 foreach($disk['partitions'] as $key => $partition){
363 $partition_attrs = array();
365 foreach($partition as $key => $value){
366 if(!empty($value)){
367 $partition_attrs[$key]=$value;
368 }else{
369 unset($partition_attrs[$key]);
370 }
371 }
373 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
374 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
375 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
377 unset($partition_attrs['status']);
378 unset($partition_attrs['old_cn']);
380 if($partition['status']=="new"){
381 $ldap->cat($partition_dn);
382 if($ldap->count()){
383 $partition['status']="edited";
384 }
385 }
387 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
388 $partition_attrs['FAImountPoint']="swap";
389 }
391 if(($partition['status'] == "delete")&&($disk['status']!="new")){
392 $ldap->cd($partition_dn);
393 $ldap->rmdir_recursive($partition_dn);
394 }elseif($partition['status'] == "new"){
395 if(empty($partition_attrs['description'])){
396 unset($partition_attrs['description']);
397 }
398 $ldap->cd($this->config->current['BASE']);
399 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
400 $ldap->cd($partition_dn);
401 $ldap->add($partition_attrs);
402 }elseif($partition['status'] == "edited"){
403 if(empty($partition_attrs['description'])){
404 $partition_attrs['description']=array();
405 }
406 $ldap->cd($partition_dn);
407 $ldap->modify($partition_attrs);
408 }
409 show_ldap_error($ldap->get_error());
410 }
411 }
412 $this->handle_post_events("add");
413 }
414 }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>