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