7e72481f99b84beb4fe34b651cc44e2977d07add
1 <?php
3 class faiPackage 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;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIpackageList","FAIrepository");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description");
28 var $sub64coded = array();
30 var $ConfiguredPackages = array();
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIdebianRelease = ""; // The selected release
40 var $FAIdebianSection = ""; // selected section
41 var $FAIinstallMethod = "aptitude"; // hard coded
42 var $mirror = ""; // selected mirror
44 var $servers = array(); // All available servers
45 var $releases = array(); // All possible releases
46 var $sections = array(); // All section types
48 var $list = NULL;
50 var $mirrors = array(); // The combination of server/release/section
51 var $confDir = "";
52 var $usedPackages = array();
53 var $buffer = NULL;
54 var $strID ="";
55 var $newDialogShown =false;
57 var $FAIstate = "";
59 var $FAIinstallMethods = array( "install", "ninstall", "remove",
60 "dselect-upgrade", "taskinst", "taskrm",
61 "hold", "clean", "aptitude", "aptitude-r",
62 "pending", "dpkgc" );
65 function faiPackage ($config, $dn= NULL)
66 {
67 /* Load Attributes */
68 plugin::plugin ($config, $dn);
70 $this->acl ="#all#";
72 /* If "dn==new" we try to create a new entry
73 * Else we must read all objects from ldap which belong to this entry.
74 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75 */
76 if($dn != "new"){
77 $this->dn =$dn;
79 /* Set acls
80 */
81 $ui = get_userinfo();
82 $acl = get_permissions ($this->dn, $ui->subtreeACL);
83 $acli = get_module_permission($acl, "FAIclass", $this->dn);
84 $this->acl=$acli;
86 /* Get FAIstate
87 */
88 if(isset($this->attrs['FAIstate'][0])){
89 $this->FAIstate = $this->attrs['FAIstate'][0];
90 }
91 }
93 if(isset($this->attrs['FAIpackage'])){
94 unset($this->attrs['FAIpackage']['count']);
95 foreach($this->attrs['FAIpackage'] as $pkg){
96 $this->usedPackages[$pkg] = $pkg;
97 }
98 ksort($this->usedPackages);
99 }else{
100 $this->usedPackages = array();
101 }
104 /* Create one filter with all package names,
105 instead of calling $ldap->search for every single package
106 */
107 $PackageFilter = "";
108 foreach($this->usedPackages as $name){
109 $PackageFilter .= "(FAIpackage=".$name.")";
110 }
111 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
113 /* Search for configuration objects */
114 $ldap = $this->config->get_ldap_link();
115 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
117 /* Walk through configurations and append them to our list of ConfiguredPackages */
118 while($attr = $ldap->fetch()){
119 $tmp =array();
120 $tmp['Name'] = $attr['FAIvariable'][0];
121 $tmp['Type'] = $attr['FAIvariableType'][0];
123 if (isset($attr['FAIvariableContent'][0])){
124 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
125 $tmp['Value'] = $attr['FAIvariableContent'][0];
126 }else{
127 $content = array();
128 unset($attr['FAIvariableContent']['count']);
129 foreach($attr['FAIvariableContent'] as $attr){
130 $tmp['Value'][] = $attr;
131 }
132 }
133 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
134 }
135 }
137 if (isset($this->attrs['FAIdebianSection']['count'])){
138 unset($this->attrs['FAIdebianSection']['count']);
139 }
140 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
141 $this->FAIdebianSection = array();
142 foreach($this->attrs['FAIdebianSection'] as $sec){
143 $this->FAIdebianSection[$sec]=$sec;
144 }
145 }
147 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
148 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
149 }
150 $this->confDir = CONFIG_DIR."/fai/";
151 $this->FAIpackage = array();
154 $methods = array();
155 foreach($this->FAIinstallMethods as $method){
156 $methods[$method] = $method;
157 }
158 $this->FAIinstallMethods = $methods;
159 /* Check if we exist already - no need to ask for revisions, etc. then */
160 if ($this->dn != "new"){
161 $this->newDialogShown= true;
162 }
164 }
166 function execute()
167 {
168 /* Call parent execute */
170 plugin::execute();
172 /* Fill templating stuff */
173 $smarty= get_smarty();
174 $display= "";
176 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
178 if((!$this->is_account)&&(!$this->newDialogShown)){
180 if($this->dialog==NULL){
181 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
182 $this->is_dialog =true;
183 }
185 /* Assign posible changes, for mirror combinations */
186 $this->dialog->save_object();
188 /* Assign Repository settings*/
189 if(isset($_POST['SaveObjectNew'])){
190 $obj = $this->dialog->save();
192 $this->FAIdebianSection = $obj['FAIdebianSection'];
193 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
195 unset($this->dialog);
196 $this->dialog = false;
197 $this->is_dialog = false;
198 $this->newDialogShown = true;
199 $this->is_account = true;
200 }
202 /* Draw dialog */
203 if($this->dialog){
204 $display=$this->dialog->execute();
205 return($display);
206 }
207 }
209 /* Assign variables */
210 foreach($this->attributes as $attrs){
211 $smarty->assign($attrs,$this->$attrs);
212 }
214 /* Generate package list */
215 $this->list=$this->genPkgs();
217 /* + was pressed to open the package dialog */
218 if(isset($_POST['Addpkg'])){
219 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
220 $this->is_dialog =true;
221 }
223 /* Delte selected package */
224 if(isset($_POST['Delpkg'])){
225 if($this->FAIstate != "freeze"){
226 foreach($_POST['usedPackages'] as $del){
227 if(isset($this->usedPackages[$del])){
228 unset($this->usedPackages[$del]);
229 }
230 }
231 }
232 }
234 /* Abort package selection dialog */
235 if(isset($_POST['CancelSubObject'])){
236 $this->dialog = false;
237 $this->is_dialog=false;
238 }
240 /* attach new packages */
241 if(isset($_POST['SaveSubObject'])) {
242 if($this->FAIstate != "freeze"){
243 $this->dialog->save_object();
244 if(count($this->dialog->check())){
245 foreach($this->dialog->check() as $msgs){
246 print_red($msgs);
247 }
248 }else{
249 $use = $this->dialog->save();
250 $this->usedPackages = $use;
251 $this->dialog = false;
252 $this->is_dialog=false;
253 ksort($this->usedPackages);
254 }
255 }else{
256 $this->dialog = false;
257 $this->is_dialog=false;
258 }
259 }
261 /* Configuration dialog open*/
262 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
263 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
264 $pkg_config = array();
265 $pkg = $_POST['usedPackages'][0];
267 if(isset($this->ConfiguredPackages[$pkg])){
268 $pkg_config = $this->ConfiguredPackages[$pkg];
269 }
271 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
272 $this->dialog ->acl = $this->acl;
273 $this->is_dialog =true;
274 }
276 /* Save Configuration */
277 if(isset($_POST['SaveObjectConfig'])){
278 if($this->FAIstate != "freeze"){
279 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
280 }
281 $this->dialog = false;
282 $this->is_dialog=false;
283 }
285 /* cancel configuration */
286 if(isset($_POST['CancelObjectConfig'])){
287 $this->dialog = false;
288 $this->is_dialog=false;
289 }
291 /* Display dialog */
292 if($this->is_dialog){
293 return $this->dialog->execute();
294 }
296 /* Assign section to smarty */
297 $strsec = "";
298 foreach($this->FAIdebianSection as $sec){
299 $strsec .= $sec." ";
300 }
302 foreach($this->attributes as $attr){
303 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
304 }
306 $smarty->assign("OptionsACL","");
308 $smarty->assign("releases",$this->releases);
309 $smarty->assign("release" ,$this->FAIdebianRelease);
310 $smarty->assign("sections",$this->sections);
311 $smarty->assign("section" ,$strsec);
312 $smarty->assign("usedPackages",$this->printUsedPackages());
313 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
314 return($display);
315 }
317 /* Delete me, and all my subtrees
318 */
319 function remove_from_parent()
320 {
321 $ldap = $this->config->get_ldap_link();
322 $ldap->cd ($this->dn);
323 $ldap->rmdir_recursive($this->dn);
324 $this->handle_post_events("remove");
325 }
328 /* Save data to object
329 */
330 function save_object()
331 {
333 if($this->FAIstate == "freeze") return;
334 plugin::save_object();
337 foreach($this->attributes as $attrs){
338 if(isset($_POST[$attrs])){
339 $this->$attrs = $_POST[$attrs];
340 }
341 }
342 }
345 /* Check supplied data */
346 function check()
347 {
348 /* Call common method to give check the hook */
349 $message= plugin::check();
351 if(count($this->usedPackages)==0){
352 $message[]=_("Please select a least one Package.");
353 }
355 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
356 $message[]=_("Please choose a valid combination for your repository setup.");
357 }
359 return ($message);
360 }
362 function printUsedPackages(){
363 $a_ret=array();
364 if(is_array($this->usedPackages)) {
365 foreach($this->usedPackages as $usedName){
367 $config = 0;
369 foreach($this->ConfiguredPackages as $name => $value){
370 if($name == $usedName){
371 $config ++;
372 }
373 }
375 $c_str ="";
376 if($config){
377 $c_str = " - "._("package is configured");
378 }
380 if(isset($this->list[$usedName][1])){
381 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
382 }else{
383 $a_ret[$usedName] = $usedName.$c_str;
384 }
385 }
386 }
387 return($a_ret);
388 }
390 function genPkgs(){
391 /* Generate a list off available packages for this mirror, section and release
392 */
393 /* Only read this file if it wasn't read before */
394 if($this->buffer==NULL){
395 $this->buffer=array();
396 $a_ret = array();
397 foreach($this->FAIdebianSection as $sec){
398 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
400 if(!is_file($strID)){
401 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
402 unset($this->buffer);
403 return(array());
404 }
405 $fp = fopen($strID,"r");
407 /* Parse every line and create an array */
408 while(!feof($fp)){
409 $str= fgets($fp,512);
410 $stra= split("\|",$str);
411 if(count($stra)==4){
412 $a_ret[$stra[0]] = $stra;
413 }
414 }
415 fclose($fp);
416 /* Save our Data, to avoid reading it again */
417 }
418 $this->buffer = $a_ret;
419 ksort($a_ret);
420 return($a_ret);
421 }else{
422 return $this->buffer;
423 }
424 }
427 /* Save to LDAP */
428 function save()
429 {
430 plugin::save();
432 $ldap = $this->config->get_ldap_link();
434 $this->attrs['FAIpackage'] = array();
435 foreach($this->usedPackages as $pkg => $obj){
436 $this->attrs['FAIpackage'][] = $pkg;
437 }
439 $this->attrs['FAIdebianSection'] = array();
440 foreach($this->FAIdebianSection as $sec){
441 $this->attrs['FAIdebianSection'][] = $sec;
442 }
444 // $this->attrs["FAIinstallMethod"]= "aptitude";
446 $ldap->cat($this->dn);
447 if($ldap->count()!=0){
448 /* Write FAIscript to ldap*/
449 $ldap->cd($this->dn);
450 $this->cleanup();
451 $ldap->modify ($this->attrs);
453 }else{
454 /* Write FAIscript to ldap*/
455 $ldap->cd($this->config->current['BASE']);
456 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
457 $ldap->cd($this->dn);
458 $ldap->add($this->attrs);
459 }
460 show_ldap_error($ldap->get_error());
462 /* Save Package configurations */
463 foreach($this->ConfiguredPackages as $pkgname => $attrs){
464 foreach($attrs as $name => $attr){
466 foreach($attr as $n=>$v){
467 if(empty($v)) $v = array();
468 }
470 /* Set attributes */
471 $pkgattrs['objectClass'] = "FAIdebconfInfo";
472 $pkgattrs['FAIpackage'] = $pkgname;
473 $pkgattrs['FAIvariable'] = $name;
474 $pkgattrs['FAIvariableType'] = $attr['Type'];
475 $pkgattrs['FAIvariableContent'] = $attr['Value'];
476 $pkgdn = "FAIvariable=".$name.",".$this->dn;
478 /* cehck if object already exists */
479 $ldap->cat($pkgdn);
480 if($ldap->count()!=0){
481 $ldap->cd($pkgdn);
482 $this->cleanup();
483 $ldap->modify ($pkgattrs);
485 }else{
486 $ldap->cd($this->config->current['BASE']);
487 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
488 $ldap->cd($pkgdn);
489 $ldap->add($pkgattrs);
490 }
491 show_ldap_error($ldap->get_error());
492 }
493 }
498 }
499 }
501 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
502 ?>