Code

7ea32fe9dab41349aab4aa322a20ff90f93d55fc
[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            = "";
29   
30   var $view_logged            = FALSE;
31   var $fai_activated          = FALSE;
33   function servrepository ($config, $dn= NULL, $parent= NULL)
34   {
35     plugin::plugin ($config, $dn, $parent);
37     /* Skip this if fai is deactivated */
38     $tmp = search_config($this->config->data,"faiManagement","CLASS");
39     if(!empty($tmp)){
40       $this->fai_activated = TRUE;    
41     }else{
42       return;
43     }
45     $this->DisplayName = _("Repository service");
47     $this->repositories = array();
48     if(isset($this->attrs['FAIrepository'])){
49       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
50         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
51         $tmp2 = array();  
52         $tmp3 = array();   
54         if(isset($tmp[1])){
55           $tmp2['ParentServer'] = $tmp[1];
56           if(empty($tmp[1])){
57             $tmp2['ParentServer'] = "none";
58           }
59         }else{
60           $tmp2['ParentServer'] = "none";
61         }
63         if(isset($tmp[0])){
64           $tmp2['Url']          = $tmp[0];
65         }else{
66           $tmp2['Url']          = "";
67         }
68   
69         if(isset($tmp[2])){
70           $tmp2['Release']      = $tmp[2];
71         }else{
72           $tmp2['Release']      = "";
73         }
75         if(isset($tmp[3])){
76           $tmp3 = split(",",$tmp[3]);
77           foreach($tmp3 as $sec){
78             $tmp2['Sections'][$sec]=$sec;
79           }    
80         }else{
81           $tmp['Section']=array();
82         }
84         $this->repositories[$tmp[2]]=$tmp2;      
85       }
86     }
87   }
89   function execute()
90   {
91     /* Call parent execute */
92     plugin::execute();
94     if($this->is_account && !$this->view_logged){
95       $this->view_logged = TRUE;
96       new log("view","server/".get_class($this),$this->dn);
97     }
99     if(!$this->fai_activated){
100       $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
101       return $str;
102     }
104     /* Fill templating stuff */
105     $smarty= get_smarty();
106     $smarty->assign("is_createable",$this->acl_is_createable());
107     $display= "";
109     /* Smarty vars*/
110     $smarty->assign("infoimage",    get_template_path('images/info.png'));
111     $smarty->assign("search_image", get_template_path('images/search.png'));
112     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
113     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
114     $smarty->assign("alphabet",     generate_alphabet());
115     $smarty->assign("apply",        apply_filter());
116     $smarty->assign("regex",        $this->regex);
118     /* Show tab dialog headers */
119     /*
120        ADD / EDIT Repository
121        Dialog Handling
122      */
123     $once = false;
124     if(isset($_POST['servRepository'])){
125       foreach($_POST as $name => $value){
127         if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
128           $once = true;
129           $this->dialog = new servRepositorySetup($this->config,$this->dn);
130           $this->dialog->parent = $this;
131         }
133         if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
134           $once = true;
135           $value = preg_replace("/delete_/","",$name);
136           $value = base64_decode(preg_replace("/_.*$/","",$value));
138           $url = $this->repositories[$value]['Url'];
139           $release = $this->repositories[$value]['Release'];
141           $ldap = $this->config->get_ldap_link();
142           $ldap->cd ($this->config->current['BASE']);
144           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
146           $found = false;
147           $found_in = " ";
148           while($attrs = $ldap->fetch()){
149             foreach($attrs['FAIclass'] as $class){
150               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
151                 $found = true;  
152                 $found_in .= $attrs['cn'][0]." ";
153               }
154             }
155           }
157           if($found){
158             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));
159           }else{
160             if(isset($this->repositories[$value])){
161               unset($this->repositories[$value]);
162             }
163           }
164         }
166         if((preg_match("/^edit_/",$name))&&(!$once)){
167           $value = preg_replace("/edit_/","",$name);
168           $value = base64_decode(preg_replace("/_.$/","",$value));
170           if(isset($this->repositories[$value])){
172             $ldap = $this->config->get_ldap_link();
173             $ldap->cd ($this->config->current['BASE']);
175             $url = $this->repositories[$value]['Url'];
176             $release = $this->repositories[$value]['Release'];
178             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
180             $found = false;
181             $found_in = " ";
182             while($attrs = $ldap->fetch()){
183               foreach($attrs['FAIclass'] as $class){
184                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
185                   $found = true;
186                   $found_in .= $attrs['cn'][0]." ";
187                 }
188               }
189             }
191             if($found){
192               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
193             } 
195             if(isset($this->repositories[$value])){
196               $once = true;
197               $obj = $this->repositories[$value];
199               /* to be able to detect if this was renamed */
200               $obj['initialy_was'] = $obj['Release'];
201               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
202               $this->dialog->parent = $this;
203             }
204           }
205         }
206       }
207     }
208     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id']))){
209       $obj = $this->repositories[base64_decode($_GET['id'])];
210       $obj['initialy_was'] = $obj['Release'];
211       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
212       $this->dialog->parent = $this;
213     }
215     if(isset($_POST['repository_setup_save'])){
216       $this->dialog->save_object();
217       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
218         print_red(_("This name is already in use."));
219       }else
221       if(count($this->dialog->check())!=0){
222         foreach($this->dialog->check() as $msg){
223           print_red($msg);
224         }
225       }else{
226         $obj = $this->dialog->save();
227         if($this->dialog->is_new_name()){
228           $oldname = $this->dialog->initialy_was;
229           $this->repositories[$obj['Release']]=$obj;        
230           unset($this->repositories[$oldname]);
231         }else{ 
232           $this->repositories[$obj['Release']]=$obj;        
233         }
234         $this->dialog = NULL;
235         $this->is_dialog= false;
236       }
237     }
239     if(isset($_POST['repository_setup_cancel'])){
240       $this->dialog=NULL;
241       $this->is_dialog = false;
242     }
243    
244     if($this->dialog != NULL){
245       $this->dialog->save_object();
246       $this->is_dialog = true;
247       return($this->dialog->execute());
248     }
250     /*
251       Repository setup dialog handling /END
252     */
254     $divlist = new divList("repositories");
255     $divlist->SetEntriesPerPage(0);
256     $divlist->setHeight(400);
257     $divlist->SetPluginMode();
259     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
260                               array("string"=>_("Sections")),
261                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
262                        )     );
264     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
265     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
266    
267     /* Hide delete icon, if delete is not allowed */ 
268     if($this->acl_is_removeable()){
269       $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
270     }else{
271       $delete = "<img src='images/empty.png' alt='&nbsp;'>";
272     }
273   
274     foreach($this->repositories as $name => $reps){
276       $str = " ";
277       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
278       foreach($reps['Sections'] as $sec){
279         $str.=$sec." ";  
280       }    
281       $divlist->AddEntry(array(
282           array("string"=>sprintf($link,base64_encode($name),$name),"attach"=>"style='width:80px;'"),
283           array("string"=>sprintf($link,base64_encode($name),_("Sections")." :".$str)),
284           array("string"=>preg_replace("/%s/",base64_encode($name),$edit.$delete),"attach"=>"style='border-right:0px;width:55px;text-align:right;'")
285                               ));
286       }
287     }
288  
289     $smarty -> assign("Repositories",$divlist->DrawList());
291     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
292     return($display);
293   }
296   /* Save data to object */
297   function save_object()
298   {
299     plugin::save_object();
300     if(isset($_POST['regex'])){
301       $this->regex = $_POST['regex'];
302     }
303   }
306   /* Check supplied data */
307   function check()
308   {
309     /* Call common method to give check the hook */
310     $message= plugin::check();
311     return ($message);
312   }
315   /* Save to LDAP */
316   function save()
317   {
318     if(!$this->fai_activated) return;
320     plugin::save();
322     $arr = array();
323     foreach($this->repositories as $servername => $conf){
324       $str = "";
325       foreach($conf['Sections'] as $sec){
326         $str.=$sec.",";
327       }
328       $str=preg_replace("/,$/","",$str);
329         
330       if($conf['ParentServer']=="none"){
331         $conf['ParentServer'] ="";
332       }    
333     
334       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
335     }
336     $this->attrs['FAIrepository'] = $arr;
338     $ldap= $this->config->get_ldap_link();
339     $ldap->cd ($this->config->current['BASE']);
340     
341     $ldap->cat($this->dn, array('dn'));
342     
343     if($ldap->count()){
344       $ldap->cd($this->dn);
345       $this->cleanup();
346       $ldap->modify ($this->attrs);       
348       $this->handle_post_events("modify");
349     }else{
350       $ldap->cd ($this->config->current['BASE']);
351       $ldap->create_missing_trees($this->dn);
352       $ldap->cd($this->dn);
353       $ldap->add($this->attrs);
354       $this->handle_post_events("add");
355     }
357     if($this->initially_was_account){
358       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
359     }else{
360       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
361     }
362   }
365   function getListEntry()
366   {
367     $fields = goService::getListEntry();
368     $fields['Message']    = _("Repository service");
369     $fields['AllowEdit']  = true;
370     $fields['AllowStart'] = $fields['AllowStop'] = $fields['AllowRestart'] = false;
371     return($fields);
372   }
374   /* Return plugin informations for acl handling */
375   function plInfo()
376   {
377     return (array(
378           "plShortName"   => _("Repository"),
379           "plDescription" => _("Repository service")." ("._("Services").")",
380           "plSelfModify"  => FALSE,
381           "plDepends"     => array(),
382           "plPriority"    => 84,
383           "plSection"     => array("administration"),
384           "plCategory"    => array("server"),
386           "plProvidedAcls"=> array(
387               "cn"            => _("Name"),
388               "start"         => _("Start"),
389               "stop"          => _("Stop"),
390               "restart"       => _("Restart"),
391               "Release"       => _("Releases"),
392               "Section"       => _("Sections"),
393               "ParentServer"  => _("Parent server"),
394               "Url"           => _("Url"))
395             ));
396   }
399 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
400 ?>