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