1 <?php
3 class servrepository extends plugin
4 {
5 /* attribute list for save action */
6 // var $ignore_account = TRUE;
7 var $attributes = array("FAIrepository");
8 var $objectclasses = array("FAIrepositoryServer");
10 /* Search filter */
11 var $regex = "*";
13 /* Configurationdialog for repositories */
14 var $dialog = NULL;
16 /* Repositories */
17 var $repositories = array();
18 var $FAIrepository = array();
20 var $fai_activated = FALSE;
22 function servrepository ($config, $dn= NULL, $parent= NULL)
23 {
24 plugin::plugin ($config, $dn, $parent);
26 /* Skip this if fai is deactivated */
27 $tmp = search_config($this->config->data,"faiManagement","CLASS");
28 if(!empty($tmp)){
29 $this->fai_activated = TRUE;
30 }else{
31 return;
32 }
34 $ui = get_userinfo();
35 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
36 $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
38 $this->repositories = array();
39 if(isset($this->attrs['FAIrepository'])){
40 for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
41 $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
42 $tmp2 = array();
43 $tmp3 = array();
45 if(isset($tmp[1])){
46 $tmp2['ParentServer'] = $tmp[1];
47 if(empty($tmp[1])){
48 $tmp2['ParentServer'] = "none";
49 }
50 }else{
51 $tmp2['ParentServer'] = "none";
52 }
54 if(isset($tmp[0])){
55 $tmp2['Url'] = $tmp[0];
56 }else{
57 $tmp2['Url'] = "";
58 }
60 if(isset($tmp[2])){
61 $tmp2['Release'] = $tmp[2];
62 }else{
63 $tmp2['Release'] = "";
64 }
66 if(isset($tmp[3])){
67 $tmp3 = split(",",$tmp[3]);
68 foreach($tmp3 as $sec){
69 $tmp2['Sections'][$sec]=$sec;
70 }
71 }else{
72 $tmp['Section']=array();
73 }
75 $this->repositories[$tmp[2]]=$tmp2;
76 }
77 }
78 }
80 function execute()
81 {
82 /* Call parent execute */
83 plugin::execute();
85 if(!$this->fai_activated){
86 $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
87 return $str;
88 }
90 /* Fill templating stuff */
91 $smarty= get_smarty();
92 $display= "";
94 $ui = get_userinfo();
95 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
96 $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
97 $allow_edit = !preg_match("/disabled/i",chkacl($this->acl,"FAIclass"));
100 /* Smarty vars*/
101 $smarty->assign("infoimage", get_template_path('images/info.png'));
102 $smarty->assign("search_image", get_template_path('images/search.png'));
103 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
104 $smarty->assign("tree_image", get_template_path('images/tree.png'));
105 $smarty->assign("alphabet", generate_alphabet());
106 $smarty->assign("apply", apply_filter());
107 $smarty->assign("regex", $this->regex);
110 /* Do we need to flip is_account state? */
111 if (isset($_POST['modify_state'])){
113 /* Only change account state if allowed */
114 if($this->is_account && $this->acl == "#all#"){
115 $this->is_account= !$this->is_account;
116 $this->is_modified = true;
117 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
118 $this->is_account= !$this->is_account;
119 $this->is_modified = true;
120 }
121 }
123 /* Show tab dialog headers */
124 if ($this->is_account){
125 $display= $this->show_header(_("Remove FAI repository extension."),
126 _("This server has FAI repository features enabled. You can disable them by clicking below."));
127 } else {
128 $display= $this->show_header(_("Add FAI repository extension."),
129 _("This server has FAI repository features disabled. You can enable them by clicking below."));
130 return ($display);
131 }
133 /*
134 ADD / EDIT Repository
135 Dialog Handling
136 */
138 $once = false;
139 if(isset($_POST['servRepository'])){
140 foreach($_POST as $name => $value){
142 if(preg_match("/AddRepository/",$name) && !$once && $allow_edit){
143 $once = true;
144 $this->dialog = new servRepositorySetup($this->config,$this->dn);
145 $this->dialog->acl = $this->acl;
146 }
148 if((preg_match("/^delete_/",$name))&&(!$once) && $allow_edit){
149 $once = true;
150 $value = preg_replace("/delete_/","",$name);
151 $value = base64_decode(preg_replace("/_.*$/","",$value));
153 $url = $this->repositories[$value]['Url'];
154 $release = $this->repositories[$value]['Release'];
156 $ldap = $this->config->get_ldap_link();
157 $ldap->cd ($this->config->current['BASE']);
159 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
161 $found = false;
162 $found_in = " ";
163 while($attrs = $ldap->fetch()){
164 foreach($attrs['FAIclass'] as $class){
165 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
166 $found = true;
167 $found_in .= $attrs['cn'][0]." ";
168 }
169 }
170 }
172 if($found){
173 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));
174 }else{
175 if(isset($this->repositories[$value])){
176 unset($this->repositories[$value]);
177 }
178 }
179 }
181 if((preg_match("/^edit_/",$name))&&(!$once) && $allow_edit){
182 $value = preg_replace("/edit_/","",$name);
183 $value = base64_decode(preg_replace("/_.$/","",$value));
185 if(isset($this->repositories[$value])){
187 $ldap = $this->config->get_ldap_link();
188 $ldap->cd ($this->config->current['BASE']);
190 $url = $this->repositories[$value]['Url'];
191 $release = $this->repositories[$value]['Release'];
193 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
195 $found = false;
196 $found_in = " ";
197 while($attrs = $ldap->fetch()){
198 foreach($attrs['FAIclass'] as $class){
199 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
200 $found = true;
201 $found_in .= $attrs['cn'][0]." ";
202 }
203 }
204 }
206 if($found){
207 print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
208 }
210 if(isset($this->repositories[$value])){
211 $once = true;
212 $obj = $this->repositories[$value];
214 /* to be able to detect if this was renamed */
215 $obj['initialy_was'] = $obj['Release'];
216 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
217 $this->dialog->acl = $this->acl;
218 }
219 }
220 }
221 }
222 }
223 if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id'])) && $allow_edit){
224 $obj = $this->repositories[base64_decode($_GET['id'])];
225 $obj['initialy_was'] = $obj['Release'];
226 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
227 $this->dialog->acl = $this->acl;
228 }
230 /*
231 if(isset($_POST['AddRepository'])){
232 $this->dialog = new servRepositorySetup($this->config,$this->dn);
233 $this->dialog->acl = $this->acl;
234 }
235 */
237 if(isset($_POST['repository_setup_save'])){
238 $this->dialog->save_object();
239 if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
240 print_red(_("This name is already in use."));
241 }else
243 if(count($this->dialog->check())!=0){
244 foreach($this->dialog->check() as $msg){
245 print_red($msg);
246 }
247 }else{
248 $obj = $this->dialog->save();
249 if($this->dialog->is_new_name()){
250 $oldname = $this->dialog->initialy_was;
251 $this->repositories[$obj['Release']]=$obj;
252 unset($this->repositories[$oldname]);
253 }else{
254 $this->repositories[$obj['Release']]=$obj;
255 }
256 $this->dialog = NULL;
257 $this->is_dialog= false;
258 }
259 }
261 if(isset($_POST['repository_setup_cancel'])){
262 $this->dialog=NULL;
263 $this->is_dialog = false;
264 }
266 if($this->dialog != NULL){
267 $this->dialog->save_object();
268 $this->is_dialog = true;
269 return($this->dialog->execute());
270 }
272 /*
273 Repository setup dialog handling /END
274 */
276 $divlist = new divList("repositories");
277 $divlist->SetEntriesPerPage(0);
278 $divlist->setHeight(400);
280 $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
281 array("string"=>_("Sections")),
282 array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
283 ) );
285 $link = "<a href='?plug=".$_GET['plug']."&act=open&id=%s'>%s</a>";
286 $edit = "<input type='image' value='%s' name='edit_%s' src='images/edit.png'> ";
287 $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
289 foreach($this->repositories as $name => $reps){
291 $str = " ";
292 if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
294 foreach($reps['Sections'] as $sec){
295 $str.=$sec." ";
296 }
298 if($allow_edit){
299 $link_str = sprintf($link,base64_encode($name),$name);
300 $sections = sprintf($link,base64_encode($name),_("Sections")." :".$str);
301 $options = preg_replace("/%s/",base64_encode($name),$edit.$delete);
302 }else{
303 $link_str = $name;
304 $sections = _("Sections")." :".$str;
305 $options = "";
306 }
309 $divlist->AddEntry(array(
310 array("string"=>$link_str,"attach"=>"style='width:80px;'"),
311 array("string"=>$sections),
312 array("string"=>$options,"attach"=>"style='border-right:0px;width:55px;text-align:right;'")));
313 }
314 }
316 $smarty -> assign("Repositories",$divlist->DrawList());
317 $smarty -> assign("FAIclassACL",chkacl($this->acl,"FAIclass"));
319 $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
320 return($display);
321 }
323 function remove_from_parent()
324 {
326 if(!$this->fai_activated) return;
328 /* Skip if not allowed */
329 $ui = get_userinfo();
330 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
331 $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
333 if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
335 plugin::remove_from_parent();
336 $ldap= $this->config->get_ldap_link();
337 $ldap->cd ($this->config->current['BASE']);
339 $ldap->cat($this->dn, array('dn'));
341 if($ldap->count()){
342 $ldap->cd($this->dn);
343 $this->cleanup();
344 $ldap->modify ($this->attrs);
346 $this->handle_post_events("modify");
347 }
348 }
351 /* Save data to object */
352 function save_object()
353 {
354 plugin::save_object();
355 if(isset($_POST['regex'])){
356 $this->regex = $_POST['regex'];
357 }
358 }
361 /* Check supplied data */
362 function check()
363 {
364 /* Call common method to give check the hook */
365 $message= plugin::check();
367 return ($message);
368 }
371 /* Save to LDAP */
372 function save()
373 {
374 if(!$this->fai_activated) return;
376 /* Skip if not allowed */
377 $ui = get_userinfo();
378 $tmp= get_permissions ($this->dn, $ui->subtreeACL);
379 $this->acl= get_module_permission($tmp, "servrepository", $this->dn);
381 if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
382 plugin::save();
384 $arr = array();
385 foreach($this->repositories as $servername => $conf){
386 $str = "";
387 foreach($conf['Sections'] as $sec){
388 $str.=$sec.",";
389 }
390 $str=preg_replace("/,$/","",$str);
392 if($conf['ParentServer']=="none"){
393 $conf['ParentServer'] ="";
394 }
396 $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
397 }
398 $this->attrs['FAIrepository'] = $arr;
400 $ldap= $this->config->get_ldap_link();
401 $ldap->cd ($this->config->current['BASE']);
403 $ldap->cat($this->dn, array('dn'));
405 if($ldap->count()){
406 $ldap->cd($this->dn);
407 $this->cleanup();
408 $ldap->modify ($this->attrs);
410 $this->handle_post_events("modify");
411 }else{
412 $ldap->cd ($this->config->current['BASE']);
413 $ldap->create_missing_trees($this->dn);
414 $ldap->cd($this->dn);
415 $ldap->add($this->attrs);
416 $this->handle_post_events("add");
417 }
418 }
420 }
422 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
423 ?>