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 $_SESSION['objectinfo'] = $this->dn;
100 if((isset($_POST['EditDisk']))&&(isset($_POST['disks']))){
101 $usedDiskNames =array();
102 foreach($this->disks as $key=>$disk){
103 if($key != $_POST['disks']){
104 $usedDiskNames[]= $key;
105 }
106 }
107 $this->dialog = new faiPartitionTableEntry($this->config,$this->dn,$usedDiskNames,$this->disks[$_POST['disks']]);
108 $_SESSION['objectinfo'] = $this->disks[$_POST['disks']]['dn'];
109 $this->is_dialog = true;
110 }
112 /* Edit aborted, close dialog, without saving anything
113 */
114 if(isset($_POST['CancelDisk'])){
115 unset($this->dialog);
116 $this->dialog = NULL;
117 $this->is_dialog=false;
118 }
120 /* Dialog saved
121 * Save given data from Dialog, if no error is occured
122 */
123 if(isset($_POST['SaveDisk'])){
124 $this->dialog->save_object();
125 if(count($this->dialog->check())){
126 foreach($this->dialog->check() as $msg){
127 print_red($msg);
128 }
129 }else{
130 $disk = $this->dialog->save();
131 if(isset($disk['rename'])){
132 if($this->disks[$disk['rename']['from']]['status']=="edited"){
133 $this->disks[$disk['rename']['from']]['status']="delete";
134 }else{
135 unset($this->disks[$disk['rename']['from']]);
136 }
138 foreach($disk['partitions'] as $key => $val){
139 if($disk['partitions'][$key]['status']!="delete"){
140 $disk['partitions'][$key]['status']= "new";
141 }
142 }
144 $disk['status']="new";
145 $disk['cn']= $disk['rename']['to'];
146 }
148 $this->disks[$disk['cn']]=$disk;
149 unset($this->dialog);
150 $this->dialog = NULL;
151 $this->is_dialog=false;
152 }
153 }
155 /* Delete selected disk drive from list
156 * Assign delete status for all its partitions
157 */
158 if((isset($_POST['DelDisk']))&&(!empty($_POST['disks']))){
159 $disk = $_POST['disks'];
160 if($this->disks[$disk]['status']=="edited"){
161 $this->disks[$disk."-delete"]=$this->disks[$disk];
162 unset($this->disks[$disk]);
163 $disk = $disk."-delete";
164 $this->disks[$disk]['status']="delete";
165 foreach($this->disks[$disk]['partitions'] as $name => $value ){
166 if($value['status']=="edited"){
167 $this->disks[$disk]['partitions'][$name]['status']="delete";
168 }else{
169 unset($this->disks[$disk]['partitions'][$name]);
170 }
171 }
172 }else{
173 unset($this->disks[$disk]);
174 }
175 }
177 /* Display dialog if one is defined
178 */
179 if(isset($this->dialog)){
180 $this->dialog->save_object();
181 return($this->dialog->execute());
182 }
184 /* Assign all attributes to smarty engine
185 */
186 foreach($this->attributes as $attrs){
187 $smarty->assign($attrs,$this->$attrs);
188 if($this->$attrs){
189 $smarty->assign($attrs."CHK"," ");
190 }else{
191 $smarty->assign($attrs."CHK"," disabled ");
192 }
193 }
195 $disks = $this->getDisks();
196 $smarty->assign("disks" ,$disks);
197 $smarty->assign("diskKeys",array_flip($disks));
198 $display.= $smarty->fetch(get_template_path('faiPartitionTable.tpl', TRUE));
199 return($display);
200 }
202 function getDisks(){
203 /* Return all available disks for this partition table
204 * Return in listBox friendly array
205 */
206 $a_return = array();
207 foreach($this->disks as $key => $disk){
208 if($disk['status'] != "delete"){
209 $cnt=0;
210 foreach($disk['partitions'] as $val){
211 if($val['status']!="delete"){
212 $cnt ++;
213 }
214 }
215 if(!empty($disk['description'])){
216 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
217 }else{
218 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
219 }
220 }
221 }
222 return($a_return);
223 }
226 /* Delete me, and all my subtrees
227 */
228 function remove_from_parent()
229 {
230 $ldap = $this->config->get_ldap_link();
231 $ldap->cd ($this->dn);
232 $ldap->rmdir_recursive($this->dn);
233 $this->handle_post_events("remove");
235 /* This cannot be removed... */
236 }
239 /* Save data to object
240 */
241 function save_object()
242 {
243 plugin::save_object();
244 foreach($this->attributes as $attrs){
245 if(isset($_POST[$attrs])){
246 $this->$attrs = $_POST[$attrs];
247 }
248 }
249 }
252 /* Check supplied data */
253 function check()
254 {
256 $message= array();
257 return ($message);
258 }
261 /* Save to LDAP */
262 function save()
263 {
265 plugin::save();
266 /* Save current settings.
267 * 1 : We must save the partition table, with its description and cn
268 * 2 : Append Disk with cn and description.
269 * 3 : Save partitions for each disk
270 */
272 $ldap = $this->config->get_ldap_link();
274 if($this->new){
275 $ldap->cd($this->config->current['BASE']);
276 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
277 $ldap->cd($this->dn);
278 $ldap->add($this->attrs);
279 show_ldap_error($ldap->get_error());
280 }else{
281 /* Add partition table to ldap
282 */
283 $ldap->cd($this->dn);
284 $ldap->modify($this->attrs);
285 show_ldap_error($ldap->get_error());
286 }
289 /* Sort entries, because we must delete entries with status="delete" first */
290 $order = array();
291 foreach($this->disks as $key => $disk){
292 if($disk['status'] == "delete"){
293 $order[$key] = $disk;
294 }
295 }
296 foreach($this->disks as $key => $disk){
297 if($disk['status'] != "delete"){
298 $order[$key] = $disk;
299 }
300 }
302 /* Append all disks to ldap */
303 foreach($order as $cn=>$disk){
304 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
305 $disk_attrs['cn'] = $disk['cn'];
306 $disk_attrs['description'] = $disk['description'];
307 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
309 if($disk['status']=="new"){
310 $ldap->cat($disk_dn);
311 if($ldap->count()){
312 $disk['status']="edited";
313 }
314 }
316 if($disk['status'] == "delete"){
317 $ldap->cd($disk_dn);
318 $ldap->rmdir_recursive($disk_dn);
319 }elseif($disk['status']== "edited"){
320 if(empty($disk_attrs['description'])){
321 $disk_attrs['description']=array();
322 }
323 $ldap->cd($disk_dn);
324 $ldap->modify($disk_attrs);
325 }elseif($disk['status']== "new"){
326 if(empty($disk_attrs['description'])){
327 unset($disk_attrs['description']);
328 }
329 $ldap->cd($this->config->current['BASE']);
330 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
331 $ldap->cd($disk_dn);
332 $ldap->add($disk_attrs);
333 }else{
334 print_red("unknown status while saving disks");
335 }
337 show_ldap_error($ldap->get_error());
338 if($disk['status']!="delete")
339 /* Add all partitions */
340 foreach($disk['partitions'] as $key => $partition){
341 $partition_attrs = array();
343 foreach($partition as $key => $value){
344 if(!empty($value)){
345 $partition_attrs[$key]=$value;
346 }else{
347 unset($partition_attrs[$key]);
348 }
349 }
351 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
352 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
353 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
355 unset($partition_attrs['status']);
356 unset($partition_attrs['old_cn']);
358 if($partition['status']=="new"){
359 $ldap->cat($partition_dn);
360 if($ldap->count()){
361 $partition['status']="edited";
362 }
363 }
365 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
366 $partition_attrs['FAImountPoint']="swap";
367 }
369 if(($partition['status'] == "delete")&&($disk['status']!="new")){
370 $ldap->cd($partition_dn);
371 $ldap->rmdir_recursive($partition_dn);
372 }elseif($partition['status'] == "new"){
373 if(empty($partition_attrs['description'])){
374 unset($partition_attrs['description']);
375 }
376 $ldap->cd($this->config->current['BASE']);
377 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
378 $ldap->cd($partition_dn);
379 $ldap->add($partition_attrs);
380 }elseif($partition['status'] == "edited"){
381 if(empty($partition_attrs['description'])){
382 $partition_attrs['description']=array();
383 }
384 $ldap->cd($partition_dn);
385 $ldap->modify($partition_attrs);
386 }
387 show_ldap_error($ldap->get_error());
388 }
389 }
390 $this->handle_post_events("add");
391 }
392 }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>