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");
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 $mirror = ""; // selected mirror
43 var $servers = array(); // All available servers
44 var $releases = array(); // All possible releases
45 var $sections = array(); // All section types
47 var $list = NULL;
49 var $mirrors = array(); // The combination of server/release/section
50 var $confDir = "";
51 var $usedPackages = array();
52 var $buffer = NULL;
53 var $strID ="";
54 var $newDialogShown =false;
56 function faiPackage ($config, $dn= NULL)
57 {
58 /* Load Attributes */
59 plugin::plugin ($config, $dn);
61 /* If "dn==new" we try to create a new entry
62 * Else we must read all objects from ldap which belong to this entry.
63 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
64 */
65 if($dn != "new"){
66 $this->dn =$dn;
67 }
69 if(isset($this->attrs['FAIpackage'])){
70 unset($this->attrs['FAIpackage']['count']);
71 foreach($this->attrs['FAIpackage'] as $pkg){
72 $this->usedPackages[$pkg] = $pkg;
73 }
74 ksort($this->usedPackages);
75 }else{
76 $this->usedPackages = array();
77 }
79 $ldap = $this->config->get_ldap_link();
80 foreach($this->usedPackages as $name){
81 $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))");
82 while($attr = $ldap->fetch()){
84 $tmp =array();
85 $tmp['Name'] = $attr['FAIvariable'][0];
86 $tmp['Type'] = $attr['FAIvariableType'][0];
88 if (isset($attr['FAIvariableContent'][0])){
89 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
90 $tmp['Value'] = $attr['FAIvariableContent'][0];
91 }else{
92 $content = array();
93 unset($attr['FAIvariableContent']['count']);
94 foreach($attr['FAIvariableContent'] as $attr){
95 $tmp['Value'][] = $attr;
96 }
97 }
98 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
99 }
100 }
101 }
103 if (isset($this->attrs['FAIdebianSection']['count'])){
104 unset($this->attrs['FAIdebianSection']['count']);
105 }
106 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
107 $this->FAIdebianSection = array();
108 foreach($this->attrs['FAIdebianSection'] as $sec){
109 $this->FAIdebianSection[$sec]=$sec;
110 }
111 }
113 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
114 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
115 }
116 $this->confDir = CONFIG_DIR."/fai/";
117 $this->FAIpackage = array();
118 }
120 function execute()
121 {
122 /* Call parent execute */
124 plugin::execute();
126 /* Fill templating stuff */
127 $smarty= get_smarty();
128 $display= "";
130 /* Check if we exist already - no need to ask for revisions, etc. then */
131 if ($this->dn != "new"){
132 $this->newDialogShown= true;
133 }
135 if((!$this->is_account)&&(!$this->newDialogShown)){
137 if($this->dialog==NULL){
138 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
139 $this->is_dialog =true;
140 }
142 /* Assign posible changes, for mirror combinations */
143 $this->dialog->save_object();
145 /* Assign Repository settings*/
146 if(isset($_POST['SaveObjectNew'])){
147 $obj = $this->dialog->save();
149 $this->FAIdebianSection = $obj['FAIdebianSection'];
150 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
152 unset($this->dialog);
153 $this->dialog = false;
154 $this->is_dialog=false;
155 $this->newDialogShown = true;
156 }
158 /* Draw dialog */
159 if($this->dialog){
160 $display=$this->dialog->execute();
161 return($display);
162 }
163 }
165 /* Assign variables */
166 foreach($this->attributes as $attrs){
167 $smarty->assign($attrs,$this->$attrs);
168 }
170 /* Generate package list */
171 $this->list=$this->genPkgs();
173 /* + was pressed to open the package dialog */
174 if(isset($_POST['Addpkg'])){
175 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
176 $this->is_dialog =true;
177 }
179 /* Delte selected package */
180 if(isset($_POST['Delpkg'])){
181 unset($this->usedPackages[$_POST['usedPackages']]);
182 }
184 /* Abort package selection dialog */
185 if(isset($_POST['CancelSubObject'])){
186 $this->dialog = false;
187 $this->is_dialog=false;
188 }
190 /* attach new packages */
191 if(isset($_POST['SaveSubObject'])) {
192 $this->dialog->save_object();
193 if(count($this->dialog->check())){
194 foreach($this->dialog->check() as $msgs){
195 print_red($msgs);
196 }
197 }else{
198 $use = $this->dialog->save();
199 $this->usedPackages = $use;
200 $this->dialog = false;
201 $this->is_dialog=false;
202 ksort($this->usedPackages);
203 }
204 }
206 /* Configuration dialog open*/
207 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
208 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
209 $pkg_config = array();
210 if(isset($this->ConfiguredPackages[$_POST['usedPackages']])){
211 $pkg_config = $this->ConfiguredPackages[$_POST['usedPackages']];
212 }
214 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$_POST['usedPackages'], $path, $pkg_config);
215 $this->is_dialog =true;
216 }
218 /* Save Configuration */
219 if(isset($_POST['SaveObjectConfig'])){
220 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
221 $this->dialog = false;
222 $this->is_dialog=false;
223 }
225 /* cancel configuration */
226 if(isset($_POST['CancelObjectConfig'])){
227 $this->dialog = false;
228 $this->is_dialog=false;
229 }
231 /* Display dialog */
232 if($this->is_dialog){
233 return $this->dialog->execute();
234 }
236 /* Assign section to smarty */
237 $strsec = "";
238 foreach($this->FAIdebianSection as $sec){
239 $strsec .= $sec." ";
240 }
242 $smarty->assign("OptionsACL","");
244 $smarty->assign("releases",$this->releases);
245 $smarty->assign("release" ,$this->FAIdebianRelease);
246 $smarty->assign("sections",$this->sections);
247 $smarty->assign("section" ,$strsec);
248 $smarty->assign("usedPackages",$this->printUsedPackages());
249 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
250 return($display);
251 }
253 /* Delete me, and all my subtrees
254 */
255 function remove_from_parent()
256 {
257 $ldap = $this->config->get_ldap_link();
258 $ldap->cd ($this->dn);
259 $ldap->rmdir_recursive($this->dn);
260 $this->handle_post_events("remove");
261 }
264 /* Save data to object
265 */
266 function save_object()
267 {
269 plugin::save_object();
270 foreach($this->attributes as $attrs){
271 if(isset($_POST[$attrs])){
272 $this->$attrs = $_POST[$attrs];
273 }
274 }
275 }
278 /* Check supplied data */
279 function check()
280 {
281 $message= array();
283 if(count($this->usedPackages)==0){
284 $message[]=_("Please select a least one Package.");
285 }
287 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
288 $message[]=_("Please choose a valid combination for your repository setup.");
289 }
291 return ($message);
292 }
294 function printUsedPackages(){
295 $a_ret=array();
296 if(is_array($this->usedPackages)) {
297 foreach($this->usedPackages as $usedName){
299 $config = 0;
301 foreach($this->ConfiguredPackages as $name => $value){
302 if($name == $usedName){
303 $config ++;
304 }
305 }
307 $c_str ="";
308 if($config){
309 $c_str = " - "._("package is configured");
310 }
312 if(isset($this->list[$usedName][1])){
313 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str;
314 }else{
315 $a_ret[$usedName] = $usedName.$c_str;
316 }
317 }
318 }
319 return($a_ret);
320 }
322 function genPkgs(){
323 /* Generate a list off available packages for this mirror, section and release
324 */
325 /* Only read this file if it wasn't read before */
326 if($this->buffer==NULL){
327 $this->buffer=array();
328 $a_ret = array();
329 foreach($this->FAIdebianSection as $sec){
330 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
332 if(!is_file($strID)){
333 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
334 unset($this->buffer);
335 return(array());
336 }
337 $fp = fopen($strID,"r");
339 /* Parse every line and create an array */
340 while(!feof($fp)){
341 $str= fgets($fp,512);
342 $stra= split("\|",$str);
343 if(count($stra)==4){
344 $a_ret[$stra[0]] = $stra;
345 }
346 }
347 fclose($fp);
348 /* Save our Data, to avoid reading it again */
349 }
350 $this->buffer = $a_ret;
351 ksort($a_ret);
352 return($a_ret);
353 }else{
354 return $this->buffer;
355 }
356 }
359 /* Save to LDAP */
360 function save()
361 {
362 plugin::save();
364 $ldap = $this->config->get_ldap_link();
366 $this->attrs['FAIpackage'] = array();
367 foreach($this->usedPackages as $pkg => $obj){
368 $this->attrs['FAIpackage'][] = $pkg;
369 }
371 $this->attrs['FAIdebianSection'] = array();
372 foreach($this->FAIdebianSection as $sec){
373 $this->attrs['FAIdebianSection'][] = $sec;
374 }
376 $ldap->cat($this->dn);
377 if($ldap->count()!=0){
378 /* Write FAIscript to ldap*/
379 $ldap->cd($this->dn);
380 $ldap->modify($this->attrs);
381 }else{
382 /* Write FAIscript to ldap*/
383 $ldap->cd($this->config->current['BASE']);
384 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
385 $ldap->cd($this->dn);
386 $ldap->add($this->attrs);
387 }
388 show_ldap_error($ldap->get_error());
390 /* Save Package configurations */
391 foreach($this->ConfiguredPackages as $pkgname => $attrs){
392 foreach($attrs as $name => $attr){
394 foreach($attr as $n=>$v){
395 if(empty($v)) $v = array();
396 }
398 /* Set attributes */
399 $pkgattrs['objectClass'] = "FAIdebconfInfo";
400 $pkgattrs['FAIpackage'] = $pkgname;
401 $pkgattrs['FAIvariable'] = $name;
402 $pkgattrs['FAIvariableType'] = $attr['Type'];
403 $pkgattrs['FAIvariableContent'] = $attr['Value'];
404 $pkgdn = "FAIvariable=".$name.",".$this->dn;
406 /* cehck if object already exists */
407 $ldap->cat($pkgdn);
408 if($ldap->count()!=0){
409 $ldap->cd($pkgdn);
410 $ldap->modify($pkgattrs);
411 }else{
412 $ldap->cd($this->config->current['BASE']);
413 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
414 $ldap->cd($pkgdn);
415 $ldap->add($pkgattrs);
416 }
417 show_ldap_error($ldap->get_error());
418 }
419 }
424 }
425 }
427 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
428 ?>