Code

- Cleanup before release
[gosa.git] / vhost-apache2 / systems / apache2 / class_servApacheEditVhost.inc
1 <?php
3 class servapacheVhostEdit extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account= TRUE;
7   
8   var $attributes       = array("apacheServerName","apacheDocumentRoot","apacheServerAlias",
9         "apacheServerAdmin","apacheScriptAlias","apacheSuexecUid","apacheSuexecGid");
10       
11   var $objectclasses  = array("whatever");
13   
14   var $apacheServerName         = "";
15   var $apacheDocumentRoot       = "";
16   var $apacheServerAdmin        = "";
17   var $apacheSuexecUid          = "";
18   var $apacheSuexecGid          = "";
19     
20   var $apacheServerAlias                = array();
21   var $apacheScriptAlias                = array();
23   var $OldApacheServerName              = ""; // To detect changes made with this edit
24   var $InitialApacheServerName          = "";
26   var $Records                  = array();
27   
28   var $dialog                   = false;
30   var $isNew                    = true;
31   var $cn;
32   var $VhostObject               = array();
34   function servapacheVhostEdit ($config, $dn= NULL,$attrs = array())
35   {
36     plugin::plugin ($config, $dn);
37  
38     if(!count($attrs)){
39       $this->OldApacheServerName        = "";
40       $this->isNew              = true;
41       
42       $this->InitialApacheServerName    = "";//$attrs['InitialApacheServerName'];
43     }else{
44       $this->VhostObject         = $attrs;
46       $this->OldApacheServerName        = $attrs['apacheServerName'];
48       $this->InitialApacheServerName    = $attrs['InitialApacheServerName'];
50       $this->isNew                  = false;
52       foreach($this->attributes as $value){
53         if(isset($attrs[$value])){
54           $this->$value = $attrs[$value];
55         }
56       }
58       if(!isset($attrs['apacheServerAlias'])) $this->apacheServerAlias  = array();
59       if(!isset($attrs['apacheScriptAlias'])) $this->apacheScriptAlias  = array();
61     }
62   }
64   /* Transports the given Arraykey one position up*/
65   function ArrayUp($atr,$attrs)
66   {
67     $ret = $attrs;
68     $pos = $atr ;
69     $cn = count($attrs);
70     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
71       $before = array_slice($attrs,0,($pos-2));
72       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
73       $unten  = array_slice($attrs,$pos);
74       $ret = array();
75       $ret = $this->combineArrays($before,$mitte,$unten);
76     }
77     return($ret);
78   }
81   /* Transports the given Arraykey one position down*/
82   function ArrayDown($atr,$attrs)
83   {
84     $ret = $attrs;
85     $pos = $atr ;
86     $cn = count($attrs);
87     if(!(($pos == -1)||($pos == $cn))){
88       $before = array_slice($attrs,0,($pos-1));
89       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
90       $unten  = array_slice($attrs,($pos+1));
91       $ret = array();
92       $ret = $this->combineArrays($before,$mitte,$unten);
93     }
94     return($ret);
95   }
97   /* Combine new array */
98   function combineArrays($ar0,$ar1,$ar2)
99   {
100     $ret = array();
101     if(is_array($ar0))
102     foreach($ar0 as $ar => $a){
103         $ret[]=$a;
104     }
105     if(is_array($ar1))
106     foreach($ar1 as $ar => $a){
107         $ret[]=$a;
108     }
109     if(is_array($ar2))
110     foreach($ar2 as $ar => $a){
111         $ret[]=$a;
112     }
113     return($ret);
114   }
115   
116   function getpos($atr,$attrs)
117   {
118     $i = 0;
119     foreach($attrs as $attr => $name)    {
120       $i++;
121       if($attr == $atr){
122         return($i);
123       }
124     }
125     return(-1);
126   }
129   function execute()
130   {
131     /* Call parent execute */
132     plugin::execute();
135     /* Fill templating stuff */
136     $smarty= get_smarty();
137     $display= "";
139     /* Open Vhost Entry Edit Dialog
140      */
141     if(!count($this->VhostObject)){
142       $smarty->assign("AllowVhostEdit" , false);
143     }else{
144       $smarty->assign("AllowVhostEdit" , true);
145       if(isset($_POST['EditVhostEntries'])){
146         $this->dialog= new apacheVhostEditEntries($this->config,$this->dn,$this->VhostObject);
147       }
148     }
150     /* Save Vhost Entry Edit Dialog
151      */
152     if(isset($_POST['SaveVhostEntryChanges'])){
153       $this->dialog->save_object();
154       if(count($this->dialog->check())){
155         $msgs = $this->dialog->check();
156         foreach($msgs as $msg){
157           print_red($msg);
158         }
159       }else{
160         $this->dialog->save();
161         $this->dialog = false;
162       }
163     }
165     /* Cancel Vhost Entrie Edit Dialog
166     */
167     if(isset($_POST['CancelVhostEntryChanges'])){
168       $this->dialog = false;
169     }
171     /* Display any type of open dialogs 
172      */
173     if($this->dialog){
174       $this->dialog->save_object();
175       return($this->dialog->execute());
176     }
178     $once =true;
179     foreach($_POST as $name => $value){
180       if((preg_match("/^SAup_/",$name)) && ($once)){
181         $once = false;
183         $id = preg_replace("/^SAup_/","",$name);
184         $id = preg_replace("/_.*$/","",$id);
185         $id = base64_decode($id);
186     
187         $this->apacheServerAlias = $this->ArrayUp(($id+1),$this->apacheServerAlias);
188       }
189       if((preg_match("/^SAdown_/",$name)) && ($once)){
190         $once = false;
191         
192         $id = preg_replace("/^SAdown_/","",$name);
193         $id = preg_replace("/_.*$/","",$id);
194         $id = base64_decode($id);
195   
196         $this->apacheServerAlias = $this->ArrayDown(($id+1),$this->apacheServerAlias);
197       }
198       if((preg_match("/^SAdel_/",$name)) && ($once)){
199         $once = false;
200         
201         $id = preg_replace("/^SAdel_/","",$name);
202         $id = preg_replace("/_.*$/","",$id);
203         $id = base64_decode($id);
204         
205         unset($this->apacheServerAlias[$id]);
207         $tmp  =array();
208         foreach($this->apacheServerAlias as $entry){
209           $tmp[] = $entry;
210         }
211  
212         $this->apacheServerAlias = $tmp; 
213       }
215       if((preg_match("/^SCup_/",$name)) && ($once)){
216         $once = false;
218         $id = preg_replace("/^SCup_/","",$name);
219         $id = preg_replace("/_.*$/","",$id);
220         $id = base64_decode($id);
221     
222         $this->apacheScriptAlias = $this->ArrayUp(($id+1),$this->apacheScriptAlias);
223       }
224       if((preg_match("/^SCdown_/",$name)) && ($once)){
225         $once = false;
226         
227         $id = preg_replace("/^SCdown_/","",$name);
228         $id = preg_replace("/_.*$/","",$id);
229         $id = base64_decode($id);
230   
231         $this->apacheScriptAlias = $this->ArrayDown(($id+1),$this->apacheScriptAlias);
232       }
233       if((preg_match("/^SCdel_/",$name)) && ($once)){
234         $once = false;
235         
236         $id = preg_replace("/^SCdel_/","",$name);
237         $id = preg_replace("/_.*$/","",$id);
238         $id = base64_decode($id);
239         
240         unset($this->apacheScriptAlias[$id]);
242         $tmp  =array();
243         foreach($this->apacheScriptAlias as $entry){
244           $tmp[] = $entry;
245         }
246  
247         $this->apacheScriptAlias = $tmp; 
248       }
249       
250     }
252     if((isset($_POST['AddSARecord'])) && (!empty($_POST['StrSAAlias'])) && (!empty($_POST['StrSADir']))){
253       $this->apacheServerAlias[] = trim($_POST['StrSAAlias']." ".$_POST['StrSADir']);      
254     }
255     
256         if((isset($_POST['AddSCRecord'])) && (!empty($_POST['StrSCAlias'])) && (!empty($_POST['StrSCDir']))){
257       $this->apacheScriptAlias[] = trim($_POST['StrSCAlias']." ".$_POST['StrSCDir']);      
258     }
260     /* Handle Post events */
261     $once = true;
262     foreach($_POST as $name => $value){
264       /* Delete vhosts if requested */
265       if((preg_match("/RemoveRecord_/",$name))&&($once)){
266         $once = false;
267         $id= preg_replace("/RemoveRecord_/","",$name);
268         unset($this->Records[$id]);
269       }
270     }
272     /* Add new Vhostrecord */
273  /*   if(isset($_POST['AddNewRecord'])){
274       $this->Records[] = array("type"=>"aRecord","value"=>"");
275     }*/
277     /* Fill in values */
278     foreach($this->attributes as $name){
279       $smarty->assign($name,$this->$name);
280     }
282     /* Set apacheServerNames without server suffix */
283       $smarty->assign("apacheServerName",$this->apacheServerName);
285     $div = new DivSelectBox("apacheServerAlias");
286     $div->setHeight(120);
287     $recs = $this->apacheServerAlias;
289     $oneup    = "<input name='SAup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
290     $onedown  = "<input name='SAdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
291     $onedel   = "<img src='images/empty.png' width='20' class='center'>
292                  <input name='SAdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>";
293     foreach($recs as $key => $rec){
294       $div ->AddEntry(array(
295             array("string"=>$rec),
296 /*            array("string"=>$key,
297                   "attach"=>"style='width:20px;'"),*/
298             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
299                   "attach"=>"style='width:70px;border-right:0px;'")
300             ));
301     }
303       $smarty->assign("NotNew", false);
305     $smarty->assign("apacheServerAlias",  $div->DrawList());
307     $div = new DivSelectBox("apacheScriptAlias");
308     $div->setHeight(120);
309     $recs = $this->apacheScriptAlias;
311     $oneup    = "<input name='SCup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
312     $onedown  = "<input name='SCdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
313     $onedel   = "<img src='images/empty.png' width='20' class='center'>
314                  <input name='SCdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
315     
316     foreach($recs as $key => $rec){
317       $div ->AddEntry(array(
318             array("string"=>$rec),
319 /*            array("string"=>$key,
320                   "attach"=>"style='width:20px;'"),*/
321             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
322                   "attach"=>"style='width:70px;border-right:0px;'")
323             ));
324     }
326       $smarty->assign("NotNew", false);
328     $smarty->assign("apacheScriptAlias",  $div->DrawList());    
329     
330     /* Display template */
331     $display.= $smarty->fetch(get_template_path('servApacheVhostEdit.tpl', TRUE));
332     return($display);
333   }
335   function remove_from_parent()
336   {
337   }
339   /* Save data to object */
340   function save_object()
341   {
342     //plugin::save_object();
343     foreach($this->attributes as $attr){
344       if(isset($_POST[$attr])){
345         $this->$attr = $_POST[$attr];
346       }
347     }
349   }
352   /* Check supplied data */
353   function check()
354   {
355     /* Call common method to give check the hook */
356     $message= plugin::check();
357         
358     /* Check if apacheServerName is already in use */
359     $usedVhosts = $this->getUsedServerNames();
360     if(($this->isNew == true)||($this->apacheServerName  != $this->InitialApacheServerName)){
361 /*      if((isset($usedVhosts[$this->apacheServerName]))&&($this->apacheServerName  != $this->InitialApacheServerName)){
362         $message[] =_("This apacheServerName is already in use");
363       }
364 */
365     }
367     if(!GetVhostsDomain($this->apacheServerName) || empty($this->apacheServerName)){
368       $message[] =sprintf(_("Please choose a valid Virtual Host Name."));
369     }
371     if(!is_path($this->apacheDocumentRoot) || empty($this->apacheDocumentRoot)){
372       $message[] = _("Please choose a valid Path as Virtual Host Document Root.");
373     }
374     
375         if(!is_email($this->apacheServerAdmin) || empty($this->apacheServerAdmin)){
376       $message[] = _("Please choose a valid Email Account as Admin Mail address.");
377     }
379     if($this->apacheServerName != strtolower($this->apacheServerName)){
380       $message[] = _("Only lowercase strings are allowed as Virtual Host Name.");
381     }
382     
383         if($this->apacheDocumentRoot != strtolower($this->apacheDocumentRoot)){
384       $message[] = _("Only lowercase strings are allowed as Virtual Host Document Root.");
385     }
386     
387         if($this->apacheServerAdmin != strtolower($this->apacheServerAdmin)){
388       $message[] = _("Only lowercase strings are allowed as Admin Mail address.");
389     }
391     foreach($this->apacheServerAlias as $key => $line){
392                 $apacheServerAlias_ar=split(" ",$line);
393                 $url=$apacheServerAlias_ar[0];
394         if(!is_path($url)){
395                 $message[] = sprintf(_("Please choose a valid Path as URL Alias Path in line: %s"),$line);
396         }
397     }
398     
399         foreach($this->apacheScriptAlias as $key => $line){
400                 $apacheScriptAlias_ar=split(" ",$line);
401                 $url=$apacheScriptAlias_ar[0];
402         if(!is_path($url)){
403                 $message[] = sprintf(_("Please choose a valid Path as URL Alias Path in line: %s"),$line);
404         }
405     }
406     
407     return ($message);
408   }
410   /* This funtion returns all used apacheServerNames */
411   function getUsedServerNames()
412   {
413     $ret = array();
414     $ldap = $this->config->get_ldap_link();
415     $ldap->cd($this->config->current['BASE']);
416     $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
417     while($attr = $ldap->fetch()){
418         $ret[$attr['apacheServerName'][0]]="";
419     }
420     return($ret);
421   }
423   /* Save to LDAP */
424   function save()
425   {
426     $ret =array();
427     foreach($this->attributes as $name){
428       $ret[$name] = $this->$name;
429     }
431     /* Create SA records 
432      */
433     foreach($this->apacheServerAlias as $key => $rec){
434       $rec['value']= $key." ".$rec['value'];
435       $this->Records [] = $rec;
436     }
437     
438   /* Create SC records 
439      */
440     foreach($this->apacheScriptAlias as $key => $rec){
441       $rec['value']= $key." ".$rec['value'];
442       $this->Records [] = $rec;
443     }
445     $ret['RECORDS'] = $this->Records; 
447     $ret['InitialApacheServerName']   =  $this->InitialApacheServerName;
449     return($ret);
450   }
454 ?>