Code

Added logging to some classes
[gosa.git] / plugins / admin / systems / class_servRepository.inc
1 <?php
3 require_once("class_goService.inc");
5 class servrepository extends goService
6 {
7   /* CLI vars */
8   var $cli_summary          = "Manage server basic objects";
9   var $cli_description      = "Some longer text\nfor help";
10   var $cli_parameters       = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
12   /* attribute list for save action */
13   //  var $ignore_account         = TRUE;
14   var $attributes             = array("FAIrepository");
15   var $objectclasses          = array("FAIrepositoryServer");
16   
17   /* Search filter */
18   var $regex                  = "*";
20   /* Configurationdialog for repositories */
21   var $dialog                 = NULL;
23   /* Repositories */
24   var $repositories          = array();
25   var $FAIrepository         = array();
26   var $conflicts             = array("FAIrepositoryServer");
27   var $DisplayName           = "";
28   var $StatusFlag            = "";
30   var $fai_activated          = FALSE;
32   function servrepository ($config, $dn= NULL, $parent= NULL)
33   {
34     plugin::plugin ($config, $dn, $parent);
36     /* Skip this if fai is deactivated */
37     $tmp = search_config($this->config->data,"faiManagement","CLASS");
38     if(!empty($tmp)){
39       $this->fai_activated = TRUE;    
40     }else{
41       return;
42     }
44     $this->DisplayName = _("Repository service");
46     $this->repositories = array();
47     if(isset($this->attrs['FAIrepository'])){
48       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
49         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
50         $tmp2 = array();  
51         $tmp3 = array();   
53         if(isset($tmp[1])){
54           $tmp2['ParentServer'] = $tmp[1];
55           if(empty($tmp[1])){
56             $tmp2['ParentServer'] = "none";
57           }
58         }else{
59           $tmp2['ParentServer'] = "none";
60         }
62         if(isset($tmp[0])){
63           $tmp2['Url']          = $tmp[0];
64         }else{
65           $tmp2['Url']          = "";
66         }
67   
68         if(isset($tmp[2])){
69           $tmp2['Release']      = $tmp[2];
70         }else{
71           $tmp2['Release']      = "";
72         }
74         if(isset($tmp[3])){
75           $tmp3 = split(",",$tmp[3]);
76           foreach($tmp3 as $sec){
77             $tmp2['Sections'][$sec]=$sec;
78           }    
79         }else{
80           $tmp['Section']=array();
81         }
83         $this->repositories[$tmp[2]]=$tmp2;      
84       }
85     }
87     if($this->is_account){
88       @log::log("view","server/".get_class($this),$this->dn);
89     }
90   }
92   function execute()
93   {
94     /* Call parent execute */
95     plugin::execute();
97     if(!$this->fai_activated){
98       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
99       return $str;
100     }
102     /* Fill templating stuff */
103     $smarty= get_smarty();
104     $smarty->assign("is_createable",$this->acl_is_createable());
105     $display= "";
107     /* Smarty vars*/
108     $smarty->assign("infoimage",    get_template_path('images/info.png'));
109     $smarty->assign("search_image", get_template_path('images/search.png'));
110     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
111     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
112     $smarty->assign("alphabet",     generate_alphabet());
113     $smarty->assign("apply",        apply_filter());
114     $smarty->assign("regex",        $this->regex);
116     /* Show tab dialog headers */
117     /*
118        ADD / EDIT Repository
119        Dialog Handling
120      */
121     $once = false;
122     if(isset($_POST['servRepository'])){
123       foreach($_POST as $name => $value){
125         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
126           $once = true;
127           $this->dialog = new servRepositorySetup($this->config,$this->dn);
128           $this->dialog->parent = $this;
129         }
131         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
132           $once = true;
133           $value = preg_replace("/delete_/","",$name);
134           $value = base64_decode(preg_replace("/_.*$/","",$value));
136           $url = $this->repositories[$value]['Url'];
137           $release = $this->repositories[$value]['Release'];
139           $ldap = $this->config->get_ldap_link();
140           $ldap->cd ($this->config->current['BASE']);
142           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
144           $found = false;
145           $found_in = " ";
146           while($attrs = $ldap->fetch()){
147             foreach($attrs['FAIclass'] as $class){
148               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
149                 $found = true;  
150                 $found_in .= $attrs['cn'][0]." ";
151               }
152             }
153           }
155           if($found){
156             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));
157           }else{
158             if(isset($this->repositories[$value])){
159               unset($this->repositories[$value]);
160             }
161           }
162         }
164         if((preg_match("/^edit_/",$name))&&(!$once)){
165           $value = preg_replace("/edit_/","",$name);
166           $value = base64_decode(preg_replace("/_.$/","",$value));
168           if(isset($this->repositories[$value])){
170             $ldap = $this->config->get_ldap_link();
171             $ldap->cd ($this->config->current['BASE']);
173             $url = $this->repositories[$value]['Url'];
174             $release = $this->repositories[$value]['Release'];
176             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
178             $found = false;
179             $found_in = " ";
180             while($attrs = $ldap->fetch()){
181               foreach($attrs['FAIclass'] as $class){
182                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
183                   $found = true;
184                   $found_in .= $attrs['cn'][0]." ";
185                 }
186               }
187             }
189             if($found){
190               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
191             } 
193             if(isset($this->repositories[$value])){
194               $once = true;
195               $obj = $this->repositories[$value];
197               /* to be able to detect if this was renamed */
198               $obj['initialy_was'] = $obj['Release'];
199               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
200               $this->dialog->parent = $this;
201             }
202           }
203         }
204       }
205     }
206     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id']))){
207       $obj = $this->repositories[base64_decode($_GET['id'])];
208       $obj['initialy_was'] = $obj['Release'];
209       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
210       $this->dialog->parent = $this;
211     }
213     if(isset($_POST['repository_setup_save'])){
214       $this->dialog->save_object();
215       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
216         print_red(_("This name is already in use."));
217       }else
219       if(count($this->dialog->check())!=0){
220         foreach($this->dialog->check() as $msg){
221           print_red($msg);
222         }
223       }else{
224         $obj = $this->dialog->save();
225         if($this->dialog->is_new_name()){
226           $oldname = $this->dialog->initialy_was;
227           $this->repositories[$obj['Release']]=$obj;        
228           unset($this->repositories[$oldname]);
229         }else{ 
230           $this->repositories[$obj['Release']]=$obj;        
231         }
232         $this->dialog = NULL;
233         $this->is_dialog= false;
234       }
235     }
237     if(isset($_POST['repository_setup_cancel'])){
238       $this->dialog=NULL;
239       $this->is_dialog = false;
240     }
241    
242     if($this->dialog != NULL){
243       $this->dialog->save_object();
244       $this->is_dialog = true;
245       return($this->dialog->execute());
246     }
248     /*
249       Repository setup dialog handling /END
250     */
252     $divlist = new divList("repositories");
253     $divlist->SetEntriesPerPage(0);
254     $divlist->setHeight(400);
256     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
257                               array("string"=>_("Sections")),
258                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
259                        )     );
261     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
262     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
263    
264     /* Hide delete icon, if delete is not allowed */ 
265     if($this->acl_is_removeable()){
266       $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
267     }else{
268       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
269     }
270   
271     foreach($this->repositories as $name => $reps){
273       $str = " ";
274       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
275       foreach($reps['Sections'] as $sec){
276         $str.=$sec." ";  
277       }    
278       $divlist->AddEntry(array(
279           array("string"=>sprintf($link,base64_encode($name),$name),"attach"=>"style='width:80px;'"),
280           array("string"=>sprintf($link,base64_encode($name),_("Sections")." :".$str)),
281           array("string"=>preg_replace("/%s/",base64_encode($name),$edit.$delete),"attach"=>"style='border-right:0px;width:55px;text-align:right;'")
282                               ));
283       }
284     }
285  
286     $smarty -> assign("Repositories",$divlist->DrawList());
288     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
289     return($display);
290   }
293   /* Save data to object */
294   function save_object()
295   {
296     plugin::save_object();
297     if(isset($_POST['regex'])){
298       $this->regex = $_POST['regex'];
299     }
300   }
303   /* Check supplied data */
304   function check()
305   {
306     /* Call common method to give check the hook */
307     $message= plugin::check();
308     return ($message);
309   }
312   /* Save to LDAP */
313   function save()
314   {
315     if(!$this->fai_activated) return;
317     plugin::save();
319     $arr = array();
320     foreach($this->repositories as $servername => $conf){
321       $str = "";
322       foreach($conf['Sections'] as $sec){
323         $str.=$sec.",";
324       }
325       $str=preg_replace("/,$/","",$str);
326         
327       if($conf['ParentServer']=="none"){
328         $conf['ParentServer'] ="";
329       }    
330     
331       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
332     }
333     $this->attrs['FAIrepository'] = $arr;
335     $ldap= $this->config->get_ldap_link();
336     $ldap->cd ($this->config->current['BASE']);
337     
338     $ldap->cat($this->dn, array('dn'));
339     
340     if($ldap->count()){
341       $ldap->cd($this->dn);
342       $this->cleanup();
343       $ldap->modify ($this->attrs);       
345       $this->handle_post_events("modify");
346     }else{
347       $ldap->cd ($this->config->current['BASE']);
348       $ldap->create_missing_trees($this->dn);
349       $ldap->cd($this->dn);
350       $ldap->add($this->attrs);
351       $this->handle_post_events("add");
352     }
354     if($this->initially_was_account){
355       @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356     }else{
357       @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
358     }
359   }
362   function getListEntry()
363   {
364     $fields = goService::getListEntry();
365     $fields['Message']    = _("Repository service");
366     $fields['AllowEdit']  = true;
367     $fields['AllowStart'] = $fields['AllowStop'] = $fields['AllowRestart'] = false;
368     return($fields);
369   }
371   /* Return plugin informations for acl handling */
372   function plInfo()
373   {
374     return (array(
375           "plShortName"   => _("Repository"),
376           "plDescription" => _("Repository service")." ("._("Services").")",
377           "plSelfModify"  => FALSE,
378           "plDepends"     => array(),
379           "plPriority"    => 84,
380           "plSection"     => array("administration"),
381           "plCategory"    => array("server"),
383           "plProvidedAcls"=> array(
384               "cn"            => _("Name"),
385               "start"         => _("Start"),
386               "stop"          => _("Stop"),
387               "restart"       => _("Restart"),
388               "Release"       => _("Releases"),
389               "Section"       => _("Sections"),
390               "ParentServer"  => _("Parent server"),
391               "Url"           => _("Url"))
392             ));
393   }
396 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
397 ?>