Code

Udpated gotomasses
[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   /* Search filter */
16   var $regex                  = "*";
18   /* Repositories */
19   var $repositories          = array();
20   var $FAIrepository         = array();
21   var $conflicts             = array("FAIrepositoryServer");
22   var $DisplayName           = "";
23   var $StatusFlag            = "";
24   
25   var $view_logged            = FALSE;
26   var $fai_activated          = FALSE;
28   function servrepository (&$config, $dn= NULL, $parent= NULL)
29   {
30     plugin::plugin ($config, $dn, $parent);
32     $this->DisplayName = _("Repository service");
34     /* Skip this if fai is deactivated */
35     $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
36     if(!empty($tmp)){
37       $this->fai_activated = TRUE;    
38     }else{
39       return;
40     }
42     $this->repositories = array();
43     if(isset($this->attrs['FAIrepository'])){
44       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
45         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
46         $tmp2 = array();  
47         $tmp3 = array();   
49         if(isset($tmp[1])){
50           $tmp2['ParentServer'] = $tmp[1];
51           if(empty($tmp[1])){
52             $tmp2['ParentServer'] = "none";
53           }
54         }else{
55           $tmp2['ParentServer'] = "none";
56         }
58         if(isset($tmp[0])){
59           $tmp2['Url']          = $tmp[0];
60         }else{
61           $tmp2['Url']          = "";
62         }
63   
64         if(isset($tmp[2])){
65           $tmp2['Release']      = $tmp[2];
66         }else{
67           $tmp2['Release']      = "";
68         }
70         if(isset($tmp[3])){
71           $tmp3 = split(",",$tmp[3]);
72           foreach($tmp3 as $sec){
73             $tmp2['Sections'][$sec]=$sec;
74           }    
75         }else{
76           $tmp['Section']=array();
77         }
79         $this->repositories[$tmp[2]]=$tmp2;      
80       }
81     }
82   }
84   function execute()
85   {
86     /* Call parent execute */
87     plugin::execute();
89     if($this->is_account && !$this->view_logged){
90       $this->view_logged = TRUE;
91       new log("view","server/".get_class($this),$this->dn);
92     }
94     if(!$this->fai_activated){
95       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
96       return $str;
97     }
99     /* Fill templating stuff */
100     $smarty= get_smarty();
101     $smarty->assign("is_createable",$this->acl_is_createable());
102     $display= "";
104     /* Smarty vars*/
105     $smarty->assign("infoimage",    get_template_path('images/info.png'));
106     $smarty->assign("search_image", get_template_path('images/search.png'));
107     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
108     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
109     $smarty->assign("alphabet",     generate_alphabet());
110     $smarty->assign("apply",        apply_filter());
111     $smarty->assign("regex",        $this->regex);
113     /* Show tab dialog headers */
114     /*
115        ADD / EDIT Repository
116        Dialog Handling
117      */
118     $once = false;
119     if(isset($_POST['servRepository'])){
120       foreach($_POST as $name => $value){
122         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
123           $once = true;
124           $this->dialog = new servRepositorySetup($this->config,$this->dn);
125           $this->dialog->parent = $this;
126         }
128         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
129           $once = true;
130           $value = preg_replace("/delete_/","",$name);
131           $value = base64_decode(preg_replace("/_.*$/","",$value));
133           $url = $this->repositories[$value]['Url'];
134           $release = $this->repositories[$value]['Release'];
136           $ldap = $this->config->get_ldap_link();
137           $ldap->cd ($this->config->current['BASE']);
139           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
141           $found = false;
142           $found_in = " ";
143           while($attrs = $ldap->fetch()){
144             foreach($attrs['FAIclass'] as $class){
145               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
146                 $found = true;  
147                 $found_in .= $attrs['cn'][0]." ";
148               }
149             }
150           }
152           if($found){
153             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));
154           }else{
155             if(isset($this->repositories[$value])){
156               unset($this->repositories[$value]);
157             }
158           }
159         }
161         if((preg_match("/^edit_/",$name))&&(!$once)){
162           $value = preg_replace("/edit_/","",$name);
163           $value = base64_decode(preg_replace("/_.$/","",$value));
165           if(isset($this->repositories[$value])){
167             $ldap = $this->config->get_ldap_link();
168             $ldap->cd ($this->config->current['BASE']);
170             $url = $this->repositories[$value]['Url'];
171             $release = $this->repositories[$value]['Release'];
173             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
175             $found = false;
176             $found_in = " ";
177             while($attrs = $ldap->fetch()){
178               foreach($attrs['FAIclass'] as $class){
179                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
180                   $found = true;
181                   $found_in .= $attrs['cn'][0]." ";
182                 }
183               }
184             }
186             if($found){
187               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
188             } 
190             if(isset($this->repositories[$value])){
191               $once = true;
192               $obj = $this->repositories[$value];
194               /* to be able to detect if this was renamed */
195               $obj['initialy_was'] = $obj['Release'];
196               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
197               $this->dialog->parent = $this;
198             }
199           }
200         }
201       }
202     }
203     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id']))){
204       $obj = $this->repositories[base64_decode($_GET['id'])];
205       $obj['initialy_was'] = $obj['Release'];
206       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
207       $this->dialog->parent = $this;
208     }
210     if(isset($_POST['repository_setup_save'])){
211       $this->dialog->save_object();
212       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
213         print_red(_("This name is already in use."));
214       }else
216       if(count($this->dialog->check())!=0){
217         foreach($this->dialog->check() as $msg){
218           print_red($msg);
219         }
220       }else{
221         $obj = $this->dialog->save();
222         if($this->dialog->is_new_name()){
223           $oldname = $this->dialog->initialy_was;
224           $this->repositories[$obj['Release']]=$obj;        
225           unset($this->repositories[$oldname]);
226         }else{ 
227           $this->repositories[$obj['Release']]=$obj;        
228         }
229         $this->dialog = FALSE;
230         $this->is_dialog= false;
231       }
232     }
234     if(isset($_POST['repository_setup_cancel'])){
235       $this->dialog=FALSE;
236       $this->is_dialog = false;
237     }
238    
239     if(is_object($this->dialog)){
240       $this->dialog->save_object();
241       $this->is_dialog = true;
242       return($this->dialog->execute());
243     }
245     /*
246       Repository setup dialog handling /END
247     */
249     $divlist = new divlist("repositories");
250     $divlist->SetEntriesPerPage(0);
251     $divlist->setHeight(400);
252     $divlist->SetPluginMode();
254     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
255                               array("string"=>_("Sections")),
256                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
257                        )     );
259     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
260     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
261    
262     /* Hide delete icon, if delete is not allowed */ 
263     if($this->acl_is_removeable()){
264       $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
265     }else{
266       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
267     }
268   
269     foreach($this->repositories as $name => $reps){
271       $str = " ";
272       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
273       foreach($reps['Sections'] as $sec){
274         $str.=$sec." ";  
275       }    
276       $divlist->AddEntry(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",$divlist->DrawList());
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(isset($_POST['regex'])){
296       $this->regex = $_POST['regex'];
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   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 ?>