Code

First import from opensides branch, NOT WORKING
[gosa.git] / vhost-apache2 / class_apacheEditVhost.inc
1 <?php
3 class servapacheeditVhost 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 servapache2editVhost ($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 geiven 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 geiven Arraykey one position up*/
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 servAPACHE2editVhostEntries($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 record 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     user_error(print_r($recs,true));
294     foreach($recs as $key => $rec){
295       $div ->AddEntry(array(
296             array("string"=>$rec),
297 /*            array("string"=>$key,
298                   "attach"=>"style='width:20px;'"),*/
299             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
300                   "attach"=>"style='width:70px;border-right:0px;'")
301             ));
302     }
304       $smarty->assign("NotNew", false);
306     $smarty->assign("apacheServerAlias",  $div->DrawList());
308     
309     
310     $div = new DivSelectBox("apacheScriptAlias");
311     $div->setHeight(120);
312     $recs = $this->apacheScriptAlias;
314     $oneup    = "<input name='SCup_%s'    type='image' src='images/sort_up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
315     $onedown  = "<input name='SCdown_%s'  type='image' src='images/sort_down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
316     $onedel   = "<img src='images/empty.png' width='20' class='center'>
317                  <input name='SCdel_%s'   type='image' src='images/edittrash.png'  title='"._("Delete")."'  class='center'>"; 
318     
319     foreach($recs as $key => $rec){
320       $div ->AddEntry(array(
321             array("string"=>$rec),
322 /*            array("string"=>$key,
323                   "attach"=>"style='width:20px;'"),*/
324             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
325                   "attach"=>"style='width:70px;border-right:0px;'")
326             ));
327     }
330       $smarty->assign("NotNew", false);
332     $smarty->assign("apacheScriptAlias",  $div->DrawList());
333     
334     
335     
336     /* Display template */
337     $display.= $smarty->fetch(get_template_path('apache_editVhost.tpl', TRUE));
338     return($display);
339   }
341   function remove_from_parent()
342   {
343   }
345   /* Save data to object */
346   function save_object()
347   {
348     //plugin::save_object();
349     foreach($this->attributes as $attr){
350       if(isset($_POST[$attr])){
351         $this->$attr = $_POST[$attr];
352       }
353     }
355   }
358   /* Check supplied data */
359   function check()
360   {
361     /* Call common method to give check the hook */
362     $message= plugin::check();
363         
364     /* Check if apacheServerName is already in use */
365     $usedVhosts = $this->getUsedapacheServerNames();
366     if(($this->isNew == true)||($this->apacheServerName  != $this->InitialApacheServerName)){
367 /*      if((isset($usedVhosts[$this->apacheServerName]))&&($this->apacheServerName  != $this->InitialApacheServerName)){
368         $message[] =_("This apacheServerName is already in use");
369       }
370 */
371     }
373     if(!is_domain($this->apacheServerName) || empty($this->apacheServerName)){
374       $message[] =sprintf(_("Please choose a valid Virtual Host Name."));
375     }
377     if(!is_path($this->apacheDocumentRoot) || empty($this->apacheDocumentRoot)){
378       $message[] = _("Please choose a valid Path as Virtual Host Document Root.");
379     }
380     
381         if(!is_email($this->apacheServerAdmin) || empty($this->apacheServerAdmin)){
382       $message[] = _("Please choose a valid Email Account as Admin Mail address.");
383     }
385     if($this->apacheServerName != strtolower($this->apacheServerName)){
386       $message[] = _("Only lowercase strings are allowed as Virtual Host Name.");
387     }
388     
389         if($this->apacheDocumentRoot != strtolower($this->apacheDocumentRoot)){
390       $message[] = _("Only lowercase strings are allowed as Virtual Host Document Root.");
391     }
392     
393         if($this->apacheServerAdmin != strtolower($this->apacheServerAdmin)){
394       $message[] = _("Only lowercase strings are allowed as Admin Mail address.");
395     }
397     foreach($this->apacheServerAlias as $key => $line){
398                 $apacheServerAlias_ar=split(" ",$line);
399                 $url=$apacheServerAlias_ar[0];
400         if(!is_path($url)){
401                 $message[] = sprintf(_("Please choose a valid Path as URL Alias Path in line: %s"),$line);
402         }
403     }
404     
405         foreach($this->apacheScriptAlias as $key => $line){
406                 $apacheScriptAlias_ar=split(" ",$line);
407                 $url=$apacheScriptAlias_ar[0];
408         if(!is_path($url)){
409                 $message[] = sprintf(_("Please choose a valid Path as URL Alias Path in line: %s"),$line);
410         }
411     }
412     
413     return ($message);
414   }
416   /* This funtion returns all used apacheServerNames */
417   function getUsedapacheServerNames()
418   {
419     $ret = array();
420     $ldap = $this->config->get_ldap_link();
421     $ldap->cd($this->config->current['BASE']);
422     $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
423     while($attr = $ldap->fetch()){
424         $ret[$attr['apacheServerName'][0]]="";
425     }
426     return($ret);
427   }
429   /* Save to LDAP */
430   function save()
431   {
432     $ret =array();
433     foreach($this->attributes as $name){
434       $ret[$name] = $this->$name;
435     }
437     /* Create SA records 
438      */
439     foreach($this->apacheServerAlias as $key => $rec){
440       $rec['value']= $key." ".$rec['value'];
441       $this->Records [] = $rec;
442     }
443     
444   /* Create SC records 
445      */
446     foreach($this->apacheScriptAlias as $key => $rec){
447       $rec['value']= $key." ".$rec['value'];
448       $this->Records [] = $rec;
449     }
451     $ret['RECORDS'] = $this->Records; 
453     $ret['InitialApacheServerName']   =  $this->InitialApacheServerName;
455     return($ret);
456   }
460 ?>