47e989cc29d45085396a9257b63368c008bd079c
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 if($cnt == 1){
219 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition"), $cnt);
220 }else{
221 $a_return[$key]= $disk['cn']." [".$disk['description']."], ".sprintf(_("%s partition(s)"), $cnt);
222 }
223 }else{
224 if($cnt == 1){
225 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition"), $cnt);
226 }else{
227 $a_return[$key]= $disk['cn'].", ".sprintf(_("%s partition(s)"), $cnt);
228 }
229 }
230 }
231 }
232 return($a_return);
233 }
236 /* Delete me, and all my subtrees
237 */
238 function remove_from_parent()
239 {
240 $ldap = $this->config->get_ldap_link();
241 $ldap->cd ($this->dn);
242 $ldap->rmdir_recursive($this->dn);
243 $this->handle_post_events("remove");
245 /* This cannot be removed... */
246 }
249 /* Save data to object
250 */
251 function save_object()
252 {
253 plugin::save_object();
254 foreach($this->attributes as $attrs){
255 if(isset($_POST[$attrs])){
256 $this->$attrs = $_POST[$attrs];
257 }
258 }
259 }
262 /* Check supplied data */
263 function check()
264 {
266 $message= array();
267 return ($message);
268 }
271 /* Save to LDAP */
272 function save()
273 {
275 plugin::save();
276 /* Save current settings.
277 * 1 : We must save the partition table, with its description and cn
278 * 2 : Append Disk with cn and description.
279 * 3 : Save partitions for each disk
280 */
282 $ldap = $this->config->get_ldap_link();
284 if($this->new){
285 $ldap->cd($this->config->current['BASE']);
286 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
287 $ldap->cd($this->dn);
288 $ldap->add($this->attrs);
289 show_ldap_error($ldap->get_error());
290 }else{
291 /* Add partition table to ldap
292 */
293 $ldap->cd($this->dn);
294 $ldap->modify($this->attrs);
295 show_ldap_error($ldap->get_error());
296 }
299 /* Sort entries, because we must delete entries with status="delete" first */
300 $order = array();
301 foreach($this->disks as $key => $disk){
302 if($disk['status'] == "delete"){
303 $order[$key] = $disk;
304 }
305 }
306 foreach($this->disks as $key => $disk){
307 if($disk['status'] != "delete"){
308 $order[$key] = $disk;
309 }
310 }
312 /* Append all disks to ldap */
313 foreach($order as $cn=>$disk){
314 $disk_dn = "cn=".$disk['cn'].",".$this->dn;
315 $disk_attrs['cn'] = $disk['cn'];
316 $disk_attrs['description'] = $disk['description'];
317 $disk_attrs['objectClass'] = array("top","FAIclass","FAIpartitionDisk");
319 if($disk['status']=="new"){
320 $ldap->cat($disk_dn);
321 if($ldap->count()){
322 $disk['status']="edited";
323 }
324 }
326 if($disk['status'] == "delete"){
327 $ldap->cd($disk_dn);
328 $ldap->rmdir_recursive($disk_dn);
329 }elseif($disk['status']== "edited"){
330 if(empty($disk_attrs['description'])){
331 $disk_attrs['description']=array();
332 }
333 $ldap->cd($disk_dn);
334 $ldap->modify($disk_attrs);
335 }elseif($disk['status']== "new"){
336 if(empty($disk_attrs['description'])){
337 unset($disk_attrs['description']);
338 }
339 $ldap->cd($this->config->current['BASE']);
340 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $disk_dn));
341 $ldap->cd($disk_dn);
342 $ldap->add($disk_attrs);
343 }else{
344 print_red("unknown status while saving disks");
345 }
347 show_ldap_error($ldap->get_error());
348 if($disk['status']!="delete")
349 /* Add all partitions */
350 foreach($disk['partitions'] as $key => $partition){
351 $partition_attrs = array();
353 foreach($partition as $key => $value){
354 if(!empty($value)){
355 $partition_attrs[$key]=$value;
356 }else{
357 unset($partition_attrs[$key]);
358 }
359 }
361 $partition_dn= "FAIpartitionNr=".$partition_attrs['FAIpartitionNr'].",".$disk_dn;
362 $partition_attrs['objectClass']= array("top","FAIclass","FAIpartitionEntry");
363 $partition_attrs['cn']= $partition_attrs['FAIpartitionNr'];
365 unset($partition_attrs['status']);
366 unset($partition_attrs['old_cn']);
368 if($partition['status']=="new"){
369 $ldap->cat($partition_dn);
370 if($ldap->count()){
371 $partition['status']="edited";
372 }
373 }
375 if((!isset($partition['FAImountPoint']))||(empty($partition['FAImountPoint']))){
376 $partition_attrs['FAImountPoint']="swap";
377 }
379 if(($partition['status'] == "delete")&&($disk['status']!="new")){
380 $ldap->cd($partition_dn);
381 $ldap->rmdir_recursive($partition_dn);
382 }elseif($partition['status'] == "new"){
383 if(empty($partition_attrs['description'])){
384 unset($partition_attrs['description']);
385 }
386 $ldap->cd($this->config->current['BASE']);
387 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $partition_dn));
388 $ldap->cd($partition_dn);
389 $ldap->add($partition_attrs);
390 }elseif($partition['status'] == "edited"){
391 if(empty($partition_attrs['description'])){
392 $partition_attrs['description']=array();
393 }
394 $ldap->cd($partition_dn);
395 $ldap->modify($partition_attrs);
396 }
397 show_ldap_error($ldap->get_error());
398 }
399 }
400 $this->handle_post_events("add");
401 }
402 }
404 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
405 ?>