5bbfd54f615f7a33aeb2452174f8269c0f9aecb0
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","FAIdebianMirror");
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 /* Specific attributes */
31 var $cn = ""; // The class name for this object
32 var $description = ""; // The description for this set of partitions
33 var $is_dialog = false; // specifies which buttons will be shown to save or abort
34 var $dialog = NULL; // a dialog, e.g. new disk dialog
35 var $SubObjects = array(); // All leafobjects of this object
37 var $FAIdebianRelease = ""; // The selected release
38 var $FAIdebianSection = ""; // selected section
39 var $FAIdebianMirror = ""; // selected mirror
40 var $mirror = ""; // selected mirror
42 var $servers = array(); // All available servers
43 var $releases = array(); // All possible releases
44 var $sections = array(); // All section types
46 var $list = NULL;
48 var $mirrors = array(); // The combination of server/release/section
49 var $confDir = "";
50 var $usedPackages = array();
51 var $buffer = NULL;
52 var $strID ="";
53 var $newDialogShown =false;
55 function faiPackage ($config, $dn= NULL)
56 {
57 /* Load Attributes */
58 plugin::plugin ($config, $dn);
60 /* If "dn==new" we try to create a new entry
61 * Else we must read all objects from ldap which belong to this entry.
62 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
63 */
64 if($dn != "new"){
65 $this->dn =$dn;
66 }
68 if(isset($this->attrs['FAIpackage'])){
69 unset($this->attrs['FAIpackage']['count']);
70 foreach($this->attrs['FAIpackage'] as $pkg){
71 $this->usedPackages[$pkg] = $pkg;
72 }
73 ksort($this->usedPackages);
74 }else{
75 $this->usedPackages = array();
76 }
78 unset($this->attrs['FAIdebianSection']['count']);
79 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
80 $this->FAIdebianSection = array();
81 foreach($this->attrs['FAIdebianSection'] as $sec){
82 $this->FAIdebianSection[$sec]=$sec;
83 }
84 }
86 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
87 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
88 }
89 $this->confDir = CONFIG_DIR."/fai/";
90 $this->FAIpackage = array();
92 /* Extract mirror location */
93 $this->mirror= preg_replace("!^[^/]+//([^/]+).*$!", '$1', $this->FAIdebianMirror);
94 }
96 function execute()
97 {
98 /* Call parent execute */
99 plugin::execute();
101 /* Fill templating stuff */
102 $smarty= get_smarty();
103 $display= "";
105 $this->genMirror();
107 /* Check if we exist already - no need to ask for revisions, etc. then */
108 if ($this->dn != "new"){
109 $this->newDialogShown= true;
110 }
112 if((!$this->is_account)&&(!$this->newDialogShown)){
114 if($this->dialog==NULL){
115 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
116 $this->is_dialog =true;
117 }
119 /* alert possible missconfigurations */
120 if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
121 print_red(_("There is no useable package list defined."));
122 }
124 /* Assign posible changes, for mirror combinations */
125 $this->dialog->save_object();
126 $this->dialog->releases = $this->releases;
127 $this->dialog->servers = $this->servers;
128 $this->dialog->sections = $this->sections;
129 $this->dialog->mirrors = $this->mirrors;
131 /* Assign Repository settings*/
132 if(isset($_POST['SaveObjectNew'])){
133 $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
134 $this->FAIdebianSection = $this->dialog->FAIdebianSection;
135 $this->FAIdebianMirror = $this->dialog->FAIdebianMirror;
136 unset($this->dialog);
137 $this->dialog = false;
138 $this->is_dialog=false;
139 $this->newDialogShown = true;
140 }
142 /* Draw dialog */
143 if($this->dialog){
144 $display=$this->dialog->execute();
145 return($display);
146 }
147 }
149 /* Assign variables */
150 foreach($this->attributes as $attrs){
151 $smarty->assign($attrs,$this->$attrs);
152 }
154 /* Set mirror if changed */
155 if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
156 $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
157 }
159 /* check servers matching release and section settings */
160 $availableServer = array();
161 foreach($this->mirrors as $mir){
162 if($mir['release'] == $this->FAIdebianRelease){
163 $pass = true;
164 foreach($this->FAIdebianSection as $sec){
165 if($sec != $mir['section']){
166 $pass = false;
167 }
168 if($pass){
169 $availableServer[$mir['mirror']]=$mir['mirror'];
170 }
171 }
172 }
173 }
175 /* Generate package list */
176 $this->list=$this->genPkgs();
178 /* + was pressed to open the package dialog */
179 if(isset($_POST['Addpkg'])){
180 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
181 $this->is_dialog =true;
182 }
184 /* Delte selected package */
185 if(isset($_POST['Delpkg'])){
186 unset($this->usedPackages[$_POST['usedPackages']]);
187 }
189 /* Abort package selection dialog */
190 if(isset($_POST['CancelSubObject'])){
191 $this->dialog = false;
192 $this->is_dialog=false;
193 }
195 /* attach new packages */
196 if(isset($_POST['SaveSubObject'])) {
197 $this->dialog->save_object();
198 if(count($this->dialog->check())){
199 foreach($this->dialog->check() as $msgs){
200 print_red($msgs);
201 }
202 }else{
203 $use = $this->dialog->save();
204 $this->usedPackages = $use;
205 $this->dialog = false;
206 $this->is_dialog=false;
207 ksort($this->usedPackages);
208 }
209 }
211 /* Configuration dialog open*/
212 if(isset($_POST['Conpkg'])){
213 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,"none");
214 $this->is_dialog =true;
215 }
217 /* Save Configuration */
218 if(isset($_POST['SaveObjectConfig'])){
219 $this->dialog = false;
220 $this->is_dialog=false;
221 }
223 /* cancel configuration */
224 if(isset($_POST['CancelObjectConfig'])){
225 $this->dialog = false;
226 $this->is_dialog=false;
227 }
229 /* Display dialog */
230 if($this->is_dialog){
231 return $this->dialog->execute();
232 }
234 /* Assign section to smarty */
235 $strsec = "";
236 foreach($this->FAIdebianSection as $sec){
237 $strsec .= $sec." ";
238 }
240 $smarty->assign("OptionsACL","");
241 if(empty($this->FAIdebianMirror)){
242 $smarty->assign("OptionsACL"," disabled ");
243 }
245 $smarty->assign("mirrors" ,$availableServer);
246 $smarty->assign("mirror" ,$this->FAIdebianMirror);
247 $smarty->assign("releases",$this->releases);
248 $smarty->assign("release" ,$this->FAIdebianRelease);
249 $smarty->assign("sections",$this->sections);
250 $smarty->assign("section" ,$strsec);
251 $smarty->assign("usedPackages",$this->printUsedPackages());
252 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
253 return($display);
254 }
256 function genMirror(){
257 $confDir = $this->confDir;
258 if(!is_readable($confDir)){
259 print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
260 }else{
262 /* Try to catch all available mirrors
263 Possibly check if each server is reachable
264 */
266 $this->servers= array();
267 $this->releases=array();
268 $this->sections=array();
269 $this->mirrors= array();
270 $fd = opendir($confDir);
271 while($mirror = readdir($fd)){
272 if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
274 $mirrorDir = $confDir.$mirror."/";
276 $fe = opendir($mirrorDir);
277 while($release = readdir($fe)){
278 if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
280 $releaseDir = $mirrorDir.$release."/";
282 $ff = opendir($releaseDir);
283 while($section = readdir($ff)){
284 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
286 $this->servers[$mirror]=$mirror;
287 $this->releases[$release]=$release;
288 $this->sections[$section]=$section;
290 $arr=array();
291 $arr['mirror'] = $mirror;
292 $arr['release']= $release;
293 $arr['section']= $section;
295 $this->mirrors[] = $arr ;
296 }
297 }
298 fclose($ff);
299 }
300 }
301 fclose($fe);
302 }
303 }
304 fclose($fd);
305 }
308 }
310 /* Delete me, and all my subtrees
311 */
312 function remove_from_parent()
313 {
314 $ldap = $this->config->get_ldap_link();
315 $ldap->cd ($this->dn);
316 $ldap->rmdir_recursive($this->dn);
317 $this->handle_post_events("remove");
318 }
321 /* Save data to object
322 */
323 function save_object()
324 {
326 plugin::save_object();
327 foreach($this->attributes as $attrs){
328 if(isset($_POST[$attrs])){
329 $this->$attrs = $_POST[$attrs];
330 }
331 }
332 }
335 /* Check supplied data */
336 function check()
337 {
338 $message= array();
340 if(count($this->usedPackages)==0){
341 $message[]=_("Please select a least one Package.");
342 }
344 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
345 $message[]=_("Please choose a valid combination for your repository setup.");
346 }
348 return ($message);
349 }
351 function printUsedPackages(){
352 $a_ret=array();
353 if(is_array($this->usedPackages)) {
354 foreach($this->usedPackages as $usedName){
355 if(isset($this->list[$usedName][1])){
356 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
357 }else{
358 $a_ret[$usedName] = $usedName;
359 }
360 }
361 }
362 return($a_ret);
363 }
365 function genPkgs(){
366 /* Generate a list off available packages for this mirror, section and release
367 */
368 /* Only read this file if it wasn't read before */
369 if($this->buffer==NULL){
370 $this->buffer=array();
371 $a_ret = array();
372 foreach($this->FAIdebianSection as $sec){
373 $strID= "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/".$sec;
375 if(!is_file($strID)){
376 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
377 $this->FAIdebianMirror="";
378 unset($this->buffer);
379 return(array());
380 }
381 $fp = fopen($strID,"r");
383 /* Parse every line and create an array */
384 while(!feof($fp)){
385 $str= fgets($fp,512);
386 $stra= split("\|",$str);
387 if(count($stra)==4){
388 $a_ret[$stra[0]] = $stra;
389 }
390 }
391 fclose($fp);
392 /* Save our Data, to avoid reading it again */
393 }
394 $this->buffer = $a_ret;
395 ksort($a_ret);
396 return($a_ret);
397 }else{
398 return $this->buffer;
399 }
400 }
403 /* Save to LDAP */
404 function save()
405 {
406 plugin::save();
408 $ldap = $this->config->get_ldap_link();
410 $this->attrs['FAIpackage'] = array();
411 foreach($this->usedPackages as $pkg => $obj){
412 $this->attrs['FAIpackage'][] = $pkg;
413 }
415 $this->attrs['FAIdebianSection'] = array();
416 foreach($this->FAIdebianSection as $sec){
417 $this->attrs['FAIdebianSection'][] = $sec;
418 }
420 $ldap->cat($this->dn);
421 if($ldap->count()!=0){
422 /* Write FAIscript to ldap*/
423 $ldap->cd($this->dn);
424 $ldap->modify($this->attrs);
425 }else{
426 /* Write FAIscript to ldap*/
427 $ldap->cd($this->config->current['BASE']);
428 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
429 $ldap->cd($this->dn);
430 $ldap->add($this->attrs);
431 }
432 show_ldap_error($ldap->get_error());
433 }
434 }
436 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
437 ?>