1 <?php
3 class servrepository 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;
12 var $attributes = array("FAIrepository");
13 var $objectclasses = array("FAIrepositoryServer");
15 /* Search filter */
16 var $regex = "*";
18 /* Configurationdialog for repositories */
19 var $dialog = NULL;
21 /* Repositories */
22 var $repositories = array();
23 var $FAIrepository = array();
25 var $fai_activated = FALSE;
27 function servrepository ($config, $dn= NULL, $parent= NULL)
28 {
29 plugin::plugin ($config, $dn, $parent);
31 /* Skip this if fai is deactivated */
32 $tmp = search_config($this->config->data,"faiManagement","CLASS");
33 if(!empty($tmp)){
34 $this->fai_activated = TRUE;
35 }else{
36 return;
37 }
39 $ui = get_userinfo();
40 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
41 $this->acl= get_module_permission($tmp, "FAIclass", $this->dn);
43 $this->repositories = array();
44 if(isset($this->attrs['FAIrepository'])){
45 for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
46 $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
47 $tmp2 = array();
48 $tmp3 = array();
50 if(isset($tmp[1])){
51 $tmp2['ParentServer'] = $tmp[1];
52 if(empty($tmp[1])){
53 $tmp2['ParentServer'] = "none";
54 }
55 }else{
56 $tmp2['ParentServer'] = "none";
57 }
59 if(isset($tmp[0])){
60 $tmp2['Url'] = $tmp[0];
61 }else{
62 $tmp2['Url'] = "";
63 }
65 if(isset($tmp[2])){
66 $tmp2['Release'] = $tmp[2];
67 }else{
68 $tmp2['Release'] = "";
69 }
71 if(isset($tmp[3])){
72 $tmp3 = split(",",$tmp[3]);
73 foreach($tmp3 as $sec){
74 $tmp2['Sections'][$sec]=$sec;
75 }
76 }else{
77 $tmp['Section']=array();
78 }
80 $this->repositories[$tmp[2]]=$tmp2;
81 }
82 }
83 }
85 function execute()
86 {
87 /* Call parent execute */
88 plugin::execute();
90 if(!$this->fai_activated){
91 $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
92 return $str;
93 }
95 /* Fill templating stuff */
96 $smarty= get_smarty();
97 $display= "";
99 $ui = get_userinfo();
100 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
101 $this->acl= get_module_permission($tmp, "FAIclass", $this->dn);
102 $allow_edit = !preg_match("/disabled/i",chkacl($this->acl,"FAIclass"));
105 /* Smarty vars*/
106 $smarty->assign("infoimage", get_template_path('images/info.png'));
107 $smarty->assign("search_image", get_template_path('images/search.png'));
108 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
109 $smarty->assign("tree_image", get_template_path('images/tree.png'));
110 $smarty->assign("alphabet", generate_alphabet());
111 $smarty->assign("apply", apply_filter());
112 $smarty->assign("regex", $this->regex);
115 /* Do we need to flip is_account state? */
116 if (isset($_POST['modify_state'])){
117 $this->is_account= !$this->is_account;
118 }
120 /* Show tab dialog headers */
121 if ($this->is_account){
122 $display= $this->show_header(_("Remove FAI repository extension."),
123 _("This server has FAI repository features enabled. You can disable them by clicking below."));
124 } else {
125 $display= $this->show_header(_("Add FAI repository extension."),
126 _("This server has FAI repository features disabled. You can enable them by clicking below."));
127 return ($display);
128 }
130 /*
131 ADD / EDIT Repository
132 Dialog Handling
133 */
135 $once = false;
136 if(isset($_POST['servRepository'])){
137 foreach($_POST as $name => $value){
139 if(preg_match("/AddRepository/",$name) && !$once && $allow_edit){
140 $once = true;
141 $this->dialog = new servRepositorySetup($this->config,$this->dn);
142 $this->dialog->acl = $this->acl;
143 }
145 if((preg_match("/^delete_/",$name))&&(!$once) && $allow_edit){
146 $once = true;
147 $value = preg_replace("/delete_/","",$name);
148 $value = base64_decode(preg_replace("/_.*$/","",$value));
150 $url = $this->repositories[$value]['Url'];
151 $release = $this->repositories[$value]['Release'];
153 $ldap = $this->config->get_ldap_link();
154 $ldap->cd ($this->config->current['BASE']);
156 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
158 $found = false;
159 $found_in = " ";
160 while($attrs = $ldap->fetch()){
161 foreach($attrs['FAIclass'] as $class){
162 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
163 $found = true;
164 $found_in .= $attrs['cn'][0]." ";
165 }
166 }
167 }
169 if($found){
170 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));
171 }else{
172 if(isset($this->repositories[$value])){
173 unset($this->repositories[$value]);
174 }
175 }
176 }
178 if((preg_match("/^edit_/",$name))&&(!$once) && $allow_edit){
179 $value = preg_replace("/edit_/","",$name);
180 $value = base64_decode(preg_replace("/_.$/","",$value));
182 if(isset($this->repositories[$value])){
184 $ldap = $this->config->get_ldap_link();
185 $ldap->cd ($this->config->current['BASE']);
187 $url = $this->repositories[$value]['Url'];
188 $release = $this->repositories[$value]['Release'];
190 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
192 $found = false;
193 $found_in = " ";
194 while($attrs = $ldap->fetch()){
195 foreach($attrs['FAIclass'] as $class){
196 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
197 $found = true;
198 $found_in .= $attrs['cn'][0]." ";
199 }
200 }
201 }
203 if($found){
204 print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
205 }
207 if(isset($this->repositories[$value])){
208 $once = true;
209 $obj = $this->repositories[$value];
211 /* to be able to detect if this was renamed */
212 $obj['initialy_was'] = $obj['Release'];
213 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
214 $this->dialog->acl = $this->acl;
215 }
216 }
217 }
218 }
219 }
220 if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id'])) && $allow_edit){
221 $obj = $this->repositories[base64_decode($_GET['id'])];
222 $obj['initialy_was'] = $obj['Release'];
223 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
224 $this->dialog->acl = $this->acl;
225 }
227 /*
228 if(isset($_POST['AddRepository'])){
229 $this->dialog = new servRepositorySetup($this->config,$this->dn);
230 $this->dialog->acl = $this->acl;
231 }
232 */
234 if(isset($_POST['repository_setup_save'])){
235 $this->dialog->save_object();
236 if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
237 print_red(_("This name is already in use."));
238 }else
240 if(count($this->dialog->check())!=0){
241 foreach($this->dialog->check() as $msg){
242 print_red($msg);
243 }
244 }else{
245 $obj = $this->dialog->save();
246 if($this->dialog->is_new_name()){
247 $oldname = $this->dialog->initialy_was;
248 $this->repositories[$obj['Release']]=$obj;
249 unset($this->repositories[$oldname]);
250 }else{
251 $this->repositories[$obj['Release']]=$obj;
252 }
253 $this->dialog = NULL;
254 $this->is_dialog= false;
255 }
256 }
258 if(isset($_POST['repository_setup_cancel'])){
259 $this->dialog=NULL;
260 $this->is_dialog = false;
261 }
263 if($this->dialog != NULL){
264 $this->dialog->save_object();
265 $this->is_dialog = true;
266 return($this->dialog->execute());
267 }
269 /*
270 Repository setup dialog handling /END
271 */
273 $divlist = new divList("repositories");
274 $divlist->SetEntriesPerPage(0);
275 $divlist->setHeight(400);
277 $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
278 array("string"=>_("Sections")),
279 array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
280 ) );
282 $link = "<a href='?plug=".$_GET['plug']."&act=open&id=%s'>%s</a>";
283 $edit = "<input type='image' value='%s' name='edit_%s' src='images/edit.png'> ";
284 $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
286 foreach($this->repositories as $name => $reps){
288 $str = " ";
289 if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
291 foreach($reps['Sections'] as $sec){
292 $str.=$sec." ";
293 }
295 if($allow_edit){
296 $link_str = sprintf($link,base64_encode($name),$name);
297 $sections = sprintf($link,base64_encode($name),_("Sections")." :".$str);
298 $options = preg_replace("/%s/",base64_encode($name),$edit.$delete);
299 }else{
300 $link_str = $name;
301 $sections = _("Sections")." :".$str;
302 $options = "";
303 }
306 $divlist->AddEntry(array(
307 array("string"=>$link_str,"attach"=>"style='width:80px;'"),
308 array("string"=>$sections),
309 array("string"=>$options,"attach"=>"style='border-right:0px;width:55px;text-align:right;'")));
310 }
311 }
313 $smarty -> assign("Repositories",$divlist->DrawList());
314 $smarty -> assign("FAIclassACL",chkacl($this->acl,"FAIclass"));
316 $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
317 return($display);
318 }
320 function remove_from_parent()
321 {
323 if(!$this->fai_activated) return;
325 /* Skip if not allowed */
326 $ui = get_userinfo();
327 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
328 $this->acl= get_module_permission($tmp, "FAIclass", $this->dn);
330 if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
332 plugin::remove_from_parent();
333 $ldap= $this->config->get_ldap_link();
334 $ldap->cd ($this->config->current['BASE']);
336 $ldap->cat($this->dn, array('dn'));
338 if($ldap->count()){
339 $ldap->cd($this->dn);
340 $this->cleanup();
341 $ldap->modify ($this->attrs);
343 $this->handle_post_events("modify");
344 }
345 }
348 /* Save data to object */
349 function save_object()
350 {
351 plugin::save_object();
352 if(isset($_POST['regex'])){
353 $this->regex = $_POST['regex'];
354 }
355 }
358 /* Check supplied data */
359 function check()
360 {
361 /* Call common method to give check the hook */
362 $message= plugin::check();
364 return ($message);
365 }
368 /* Save to LDAP */
369 function save()
370 {
371 if(!$this->fai_activated) return;
373 /* Skip if not allowed */
374 $ui = get_userinfo();
375 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
376 $this->acl= get_module_permission($tmp, "FAIclass", $this->dn);
378 if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
379 plugin::save();
381 $arr = array();
382 foreach($this->repositories as $servername => $conf){
383 $str = "";
384 foreach($conf['Sections'] as $sec){
385 $str.=$sec.",";
386 }
387 $str=preg_replace("/,$/","",$str);
389 if($conf['ParentServer']=="none"){
390 $conf['ParentServer'] ="";
391 }
393 $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
394 }
395 $this->attrs['FAIrepository'] = $arr;
397 $ldap= $this->config->get_ldap_link();
398 $ldap->cd ($this->config->current['BASE']);
400 $ldap->cat($this->dn, array('dn'));
402 if($ldap->count()){
403 $ldap->cd($this->dn);
404 $this->cleanup();
405 $ldap->modify ($this->attrs);
407 $this->handle_post_events("modify");
408 }else{
409 $ldap->cd ($this->config->current['BASE']);
410 $ldap->create_missing_trees($this->dn);
411 $ldap->cd($this->dn);
412 $ldap->add($this->attrs);
413 $this->handle_post_events("add");
414 }
415 }
417 }
419 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
420 ?>