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 if((empty($this->cn))||(preg_match("/[^a-z0-9]/i",$this->cn))){
257 $message[]=_("Please enter a valid name.");
258 }
260 return ($message);
261 }
264 /* Save to LDAP */
265 function save()
266 {
268 plugin::save();
269 /* Save current settings.
270 * 1 : We must save the partition table, with its description and cn
271 * 2 : Append Disk with cn and description.
272 * 3 : Save partitions for each disk
273 */
275 $ldap = $this->config->get_ldap_link();
277 if($this->new){
278 $ldap->cd($this->config->current['BASE']);
279 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
280 $ldap->cd($this->dn);
281 $ldap->add($this->attrs);
282 show_ldap_error($ldap->get_error());
283 }else{
284 /* Add partition table to ldap
285 */
286 $ldap->cd($this->dn);
287 $ldap->modify($this->attrs);
288 show_ldap_error($ldap->get_error());
289 }
292 /* Sort entries, because we must delete entries with status="delete" first */
293 $order = array();
294 foreach($this->disks as $key => $disk){
295 if($disk['status'] == "delete"){
296 $order[$key] = $disk;
297 }
298 }
299 foreach($this->disks as $key => $disk){
300 if($disk['status'] != "delete"){
301 $order[$key] = $disk;
302 }
303 }
305 /* Append all disks to ldap */
306 foreach($order as $cn=>$disk){
307 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
308 $disk_attrs['cn'] = $disk['cn'];
309 $disk_attrs['description'] = $disk['description'];
310 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
312 if($disk['status']=="new"){
313 $ldap->cat($disk_dn);
314 if($ldap->count()){
315 $disk['status']="edited";
316 }
317 }
319 if($disk['status'] == "delete"){
320 $ldap->cd($disk_dn);
321 $ldap->rmdir_recursive($disk_dn);
322 }elseif($disk['status']== "edited"){
323 if(empty($disk_attrs['description'])){
324 $disk_attrs['description']=array();
325 }
326 $ldap->cd($disk_dn);
327 $ldap->modify($disk_attrs);
328 }elseif($disk['status']== "new"){
329 if(empty($disk_attrs['description'])){
330 unset($disk_attrs['description']);
331 }
332 $ldap->cd($this->config->current['BASE']);
333 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
334 $ldap->cd($disk_dn);
335 $ldap->add($disk_attrs);
336 }else{
337 print_red("unknown status while saving disks");
338 }
340 show_ldap_error($ldap->get_error());
341 if($disk['status']!="delete")
342 /* Add all partitions */
343 foreach($disk['partitions'] as $key => $partition){
344 $partition_attrs = array();
346 foreach($partition as $key => $value){
347 if(!empty($value)){
348 $partition_attrs[$key]=$value;
349 }else{
350 unset($partition_attrs[$key]);
351 }
352 }
354 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
355 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
356 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
358 unset($partition_attrs['status']);
359 unset($partition_attrs['old_cn']);
361 if($partition['status']=="new"){
362 $ldap->cat($partition_dn);
363 if($ldap->count()){
364 $partition['status']="edited";
365 }
366 }
368 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
369 $partition_attrs['FAImountPoint']="swap";
370 }
372 if(($partition['status'] == "delete")&&($disk['status']!="new")){
373 $ldap->cd($partition_dn);
374 $ldap->rmdir_recursive($partition_dn);
375 }elseif($partition['status'] == "new"){
376 if(empty($partition_attrs['description'])){
377 unset($partition_attrs['description']);
378 }
379 $ldap->cd($this->config->current['BASE']);
380 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
381 $ldap->cd($partition_dn);
382 $ldap->add($partition_attrs);
383 }elseif($partition['status'] == "edited"){
384 if(empty($partition_attrs['description'])){
385 $partition_attrs['description']=array();
386 }
387 $ldap->cd($partition_dn);
388 $ldap->modify($partition_attrs);
389 }
390 show_ldap_error($ldap->get_error());
391 }
392 }
393 $this->handle_post_events("add");
394 }
395 }
397 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
398 ?>