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