Code

Updated repository list.
[gosa.git] / plugins / admin / systems / class_servRepository.inc
1 <?php
3 class servrepository extends goService
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("FAIrepository");
13   var $objectclasses          = array("FAIrepositoryServer");
14   
15   /* Repositories */
16   var $repositories          = array();
17   var $FAIrepository         = array();
18   var $conflicts             = array("FAIrepositoryServer");
19   var $DisplayName           = "";
20   var $StatusFlag            = "";
21   
22   var $view_logged            = FALSE;
23   var $fai_activated          = FALSE;
25   var $divlist = NULL;
27   function servrepository (&$config, $dn= NULL, $parent= NULL)
28   {
29     plugin::plugin ($config, $dn, $parent);
31     $this->DisplayName = _("Repository service");
33     /* Skip this if fai is deactivated */
34     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
35     if(!empty($tmp)){
36       $this->fai_activated = TRUE;    
37     }else{
38       return;
39     }
41     $this->repositories = array();
42     if(isset($this->attrs['FAIrepository'])){
43       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
44         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
45         $tmp2 = array();  
46         $tmp3 = array();   
48         if(isset($tmp[1])){
49           $tmp2['ParentServer'] = $tmp[1];
50           if(empty($tmp[1])){
51             $tmp2['ParentServer'] = "none";
52           }
53         }else{
54           $tmp2['ParentServer'] = "none";
55         }
57         if(isset($tmp[0])){
58           $tmp2['Url']          = $tmp[0];
59         }else{
60           $tmp2['Url']          = "";
61         }
62   
63         if(isset($tmp[2])){
64           $tmp2['Release']      = $tmp[2];
65         }else{
66           $tmp2['Release']      = "";
67         }
69         if(isset($tmp[3])){
70           $tmp3 = split(",",$tmp[3]);
71           foreach($tmp3 as $sec){
72             $tmp2['Sections'][$sec]=$sec;
73           }    
74         }else{
75           $tmp['Section']=array();
76         }
78         $this->repositories[$tmp[2]]=$tmp2;      
79       }
80     }
83     /* Create divlist */
84     $divlist = new MultiSelectWindow($this->config,"repositories","server/servRepository");
85     $divlist->SetTitle("repositories");
86     $divlist->SetSummary(_("List of configured repositories."));
87     $divlist->SetInformation(_("This menu allows you to create, delete and edit repository settings."));
88     $divlist->EnableCloseButton(FALSE);
89     $divlist->EnableSaveButton(FALSE);
90     $divlist->AddHeader(array("string"=>_("Release"),"attach"=>"style='width:80px;'"));
91     $divlist->AddHeader(array("string"=>_("Sections")));
92     $divlist->AddHeader(array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'"));
93     $divlist->AddRegex("regex",_("Regular expression for matching object names"),"*",TRUE);
94     $divlist->EnableAplhabet(TRUE);
95     if($this->acl_is_createable()){
96       $divlist->SetListHeader("<input type=\"image\" src=\"images/fai_new_packages.png\" title=\""._("Add repository")."\" name=\"AddRepository\">");
97     }
98     $this->divlist = $divlist;
99   }
101   function execute()
102   {
103     /* Call parent execute */
104     plugin::execute();
106     if($this->is_account && !$this->view_logged){
107       $this->view_logged = TRUE;
108       new log("view","server/".get_class($this),$this->dn);
109     }
111     if(!$this->fai_activated){
112       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
113       return $str;
114     }
116     /* Fill templating stuff */
117     $smarty= get_smarty();
118     $smarty->assign("is_createable",$this->acl_is_createable());
119     $display= "";
121     /* Show tab dialog headers */
122     /*
123        ADD / EDIT Repository
124        Dialog Handling
125      */
126     $once = false;
127     if(isset($_POST['servRepository'])){
128       foreach($_POST as $name => $value){
130         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
131           $once = true;
132           $this->dialog = new servRepositorySetup($this->config,$this->dn);
133           $this->dialog->parent = $this;
134         }
136         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
137           $once = true;
138           $value = preg_replace("/delete_/","",$name);
139           $value = base64_decode(preg_replace("/_.*$/","",$value));
141           $url = $this->repositories[$value]['Url'];
142           $release = $this->repositories[$value]['Release'];
144           $ldap = $this->config->get_ldap_link();
145           $ldap->cd ($this->config->current['BASE']);
147           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
149           $found = false;
150           $found_in = " ";
151           while($attrs = $ldap->fetch()){
152             foreach($attrs['FAIclass'] as $class){
153               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
154                 $found = true;  
155                 $found_in .= $attrs['cn'][0]." ";
156               }
157             }
158           }
160           if($found){
161             print_red(sprintf(_("You can't delete this release, it is still used by these workstations [%s]. Please solve this dependencies first, to keep data base consistency."),$found_in));
162           }else{
163             if(isset($this->repositories[$value])){
164               unset($this->repositories[$value]);
165             }
166           }
167         }
169         if((preg_match("/^edit_/",$name))&&(!$once)){
170           $value = preg_replace("/edit_/","",$name);
171           $value = base64_decode(preg_replace("/_.$/","",$value));
173           if(isset($this->repositories[$value])){
175             $ldap = $this->config->get_ldap_link();
176             $ldap->cd ($this->config->current['BASE']);
178             $url = $this->repositories[$value]['Url'];
179             $release = $this->repositories[$value]['Release'];
181             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
183             $found = false;
184             $found_in = " ";
185             while($attrs = $ldap->fetch()){
186               foreach($attrs['FAIclass'] as $class){
187                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
188                   $found = true;
189                   $found_in .= $attrs['cn'][0]." ";
190                 }
191               }
192             }
194             if($found){
195               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
196             } 
198             if(isset($this->repositories[$value])){
199               $once = true;
200               $obj = $this->repositories[$value];
202               /* to be able to detect if this was renamed */
203               $obj['initialy_was'] = $obj['Release'];
204               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
205               $this->dialog->parent = $this;
206             }
207           }
208         }
209       }
210     }
211     if((isset($_GET['act']))&&($_GET['act']=="open_repository")&&(isset($_GET['id']))){
212       $obj = $this->repositories[base64_decode($_GET['id'])];
213       $obj['initialy_was'] = $obj['Release'];
214       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
215       $this->dialog->parent = $this;
216     }
218     if(isset($_POST['repository_setup_save']) && is_object($this->dialog)){
219       $this->dialog->save_object();
220       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
221         print_red(_("This name is already in use."));
222       }else
224       if(count($this->dialog->check())!=0){
225         foreach($this->dialog->check() as $msg){
226           print_red($msg);
227         }
228       }else{
229         $obj = $this->dialog->save();
230         if($this->dialog->is_new_name()){
231           $oldname = $this->dialog->initialy_was;
232           $this->repositories[$obj['Release']]=$obj;        
233           unset($this->repositories[$oldname]);
234         }else{ 
235           $this->repositories[$obj['Release']]=$obj;        
236         }
237         $this->dialog = FALSE;
238         $this->is_dialog= false;
239       }
240     }
242     if(isset($_POST['repository_setup_cancel'])){
243       $this->dialog=FALSE;
244       $this->is_dialog = false;
245     }
246    
247     if(is_object($this->dialog)){
248       $this->dialog->save_object();
249       $this->is_dialog = true;
250       return($this->dialog->execute());
251     }
253     /*
254       Repository setup dialog handling /END
255     */
258     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open_repository&amp;id=%s'>%s</a>";
259     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
260    
261     /* Hide delete icon, if delete is not allowed */ 
262     if($this->acl_is_removeable()){
263       $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
264     }else{
265       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
266     }
267   
268     $this->divlist->ClearElementsList(); 
269     foreach($this->repositories as $name => $reps){
271       $str = " ";
272       if(preg_match("/".str_replace("*",".*",$this->divlist->regex)."/",$reps['Release'])){
273         foreach($reps['Sections'] as $sec){
274           $str.=$sec." ";  
275         }    
276         $this->divlist->AddElement(array(
277               array("string"=>sprintf($link,base64_encode($name),$name),"attach"=>"style='width:80px;'"),
278               array("string"=>sprintf($link,base64_encode($name),_("Sections")." :".$str)),
279               array("string"=>preg_replace("/%s/",base64_encode($name),$edit.$delete),"attach"=>"style='border-right:0px;width:55px;text-align:right;'")
280               ));
281       }
282     }
283  
284     $smarty->assign("Repositories",$this->divlist->Draw());
286     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
287     return($display);
288   }
291   /* Save data to object */
292   function save_object()
293   {
294     plugin::save_object();
295     if(is_object($this->divlist)){
296       $this->divlist->save_object();
297     }
298   }
301   /* Check supplied data */
302   function check()
303   {
304     /* Call common method to give check the hook */
305     $message= plugin::check();
306     return ($message);
307   }
310   /* Save to LDAP */
311   function save()
312   {
313     if(!$this->fai_activated) return;
315     plugin::save();
317     $arr = array();
318     foreach($this->repositories as $servername => $conf){
319       $str = "";
320       foreach($conf['Sections'] as $sec){
321         $str.=$sec.",";
322       }
323       $str=preg_replace("/,$/","",$str);
324         
325       if($conf['ParentServer']=="none"){
326         $conf['ParentServer'] ="";
327       }    
328     
329       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
330     }
331     $this->attrs['FAIrepository'] = $arr;
333     $ldap= $this->config->get_ldap_link();
334     $ldap->cd ($this->config->current['BASE']);
335     
336     $ldap->cat($this->dn, array('dn'));
337     
338     if($ldap->count()){
339       $ldap->cd($this->dn);
340       $this->cleanup();
341       $ldap->modify ($this->attrs);       
343       $this->handle_post_events("modify");
344     }else{
345       $ldap->cd ($this->config->current['BASE']);
346       $ldap->create_missing_trees($this->dn);
347       $ldap->cd($this->dn);
348       $ldap->add($this->attrs);
349       $this->handle_post_events("add");
350     }
352     if($this->initially_was_account){
353       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
354     }else{
355       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356     }
357   }
360   function getListEntry()
361   {
362     $fields = goService::getListEntry();
363     $fields['Message']    = _("Repository service");
364     $fields['AllowEdit']  = true;
365     $fields['AllowStart'] = $fields['AllowStop'] = $fields['AllowRestart'] = false;
366     return($fields);
367   }
369   /* Return plugin informations for acl handling */
370   static function plInfo()
371   {
372     return (array(
373           "plShortName"   => _("Repository"),
374           "plDescription" => _("Repository service")." ("._("Services").")",
375           "plSelfModify"  => FALSE,
376           "plDepends"     => array(),
377           "plPriority"    => 84,
378           "plSection"     => array("administration"),
379           "plCategory"    => array("server"),
381           "plProvidedAcls"=> array(
382               "cn"            => _("Name"),
383               "start"         => _("Start"),
384               "stop"          => _("Stop"),
385               "restart"       => _("Restart"),
386               "Release"       => _("Releases"),
387               "Section"       => _("Sections"),
388               "ParentServer"  => _("Parent server"),
389               "Url"           => _("Url"))
390             ));
391   }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>