Code

* Created "old" branch and moved stuff
[gosa.git] / branches / old / gosa-plugins / apache2 / admin / systems / services / apache2 / class_servApacheEditVhost.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_gosaSupportDaemon.inc 10788 2008-05-06 11:15:57Z $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
24 class servapacheVhostEdit extends plugin
25 {
26   /* attribute list for save action */
27   var $ignore_account= TRUE;
28   
29   var $attributes       = array("apacheServerName","apacheDocumentRoot","apacheServerAlias",
30                 "apacheServerAdmin","apacheScriptAlias","apacheSuexecUid","apacheSuexecGid");
31       
32   var $objectclasses  = array("whatever");
34   var $apacheServerName         = "";
35   var $apacheDocumentRoot       = "";
36   var $apacheServerAdmin        = "";
37   var $apacheSuexecUid          = "";
38   var $apacheSuexecGid          = "";
39     
40   var $apacheServerAlias                = array();
41   var $apacheScriptAlias                = array();
43   var $OldApacheServerName              = ""; // To detect changes made with this edit
44   var $InitialApacheServerName          = "";
46   var $Records                  = array();
47   
48   var $dialog                   = false;
50   var $isNew                    = true;
51   var $cn;
52   var $VhostObject               = array();
55   function servapacheVhostEdit ($config, $dn= NULL,$attrs = array())
56   {
57     plugin::plugin ($config, $dn);
58     if(!count($attrs)){
59       $this->OldApacheServerName        = "";
60       $this->isNew              = true;
61       $this->InitialApacheServerName    = "";//$attrs['InitialApacheServerName'];
62     }else{
63       $this->VhostObject         = $attrs;
64       $this->OldApacheServerName        = $attrs['apacheServerName'];
65       $this->InitialApacheServerName    = $attrs['InitialApacheServerName'];
66       $this->isNew                  = false;
67       foreach($this->attributes as $value){
68         if(isset($attrs[$value])){
69           $this->$value = $attrs[$value];
70         }
71       }
73       if(!isset($attrs['apacheServerAlias'])) $this->apacheServerAlias  = array();
74       if(!isset($attrs['apacheScriptAlias'])) $this->apacheScriptAlias  = array();
75     }
76   }
78   /* Transports the given Arraykey one position up*/
79   function ArrayUp($atr,$attrs)
80   {
81     $ret = $attrs;
82     $pos = $atr ;
83     $cn = count($attrs);
84     if(!(($pos == -1)||($pos == 1)||($pos >$cn))){
85       $before = array_slice($attrs,0,($pos-2));
86       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
87       $unten  = array_slice($attrs,$pos);
88       $ret = array();
89       $ret = $this->combineArrays($before,$mitte,$unten);
90     }
91     return($ret);
92   }
95   /* Transports the given Arraykey one position down*/
96   function ArrayDown($atr,$attrs)
97   {
98     $ret = $attrs;
99     $pos = $atr ;
100     $cn = count($attrs);
101     if(!(($pos == -1)||($pos == $cn))){
102       $before = array_slice($attrs,0,($pos-1));
103       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
104       $unten  = array_slice($attrs,($pos+1));
105       $ret = array();
106       $ret = $this->combineArrays($before,$mitte,$unten);
107     }
108     return($ret);
109   }
111   /* Combine new array */
112   function combineArrays($ar0,$ar1,$ar2)
113   {
114     $ret = array();
115     if(is_array($ar0))
116     foreach($ar0 as $ar => $a){
117         $ret[]=$a;
118     }
119     if(is_array($ar1))
120     foreach($ar1 as $ar => $a){
121         $ret[]=$a;
122     }
123     if(is_array($ar2))
124     foreach($ar2 as $ar => $a){
125         $ret[]=$a;
126     }
127     return($ret);
128   }
129   
130   function getpos($atr,$attrs)
131   {
132     $i = 0;
133     foreach($attrs as $attr => $name)    {
134       $i++;
135       if($attr == $atr){
136         return($i);
137       }
138     }
139     return(-1);
140   }
143   function execute()
144   {
145     /* Call parent execute */
146     plugin::execute();
149     /* Fill templating stuff */
150     $smarty= get_smarty();
151     $display= "";
153 // Outdated?
155 #   /* Open Vhost Entry Edit Dialog
156 #    */
157 #   if(!count($this->VhostObject)){
158 #     $smarty->assign("AllowVhostEdit" , false);
159 #   }else{
160 #     $smarty->assign("AllowVhostEdit" , true);
161 #     if(isset($_POST['EditVhostEntries'])){
162 #       $this->dialog= new apacheVhostEditEntries($this->config,$this->dn,$this->VhostObject);
163 #     }
164 #   }
166 #   /* Save Vhost Entry Edit Dialog
167 #    */
168 #   if(isset($_POST['SaveVhostEntryChanges'])){
169 #     $this->dialog->save_object();
170 #     if(count($this->dialog->check())){
171 #        msg_dialog::displayChecks($this->dialog->check());
172 #     }else{
173 #       $this->dialog->save();
174 #       $this->dialog = false;
175 #     }
176 #   }
178 #   /* Cancel Vhost Entrie Edit Dialog
179 #   */
180 #   if(isset($_POST['CancelVhostEntryChanges'])){
181 #     $this->dialog = false;
182 #   }
184 #   /* Display any type of open dialogs 
185 #    */
186 #   if($this->dialog){
187 #     $this->dialog->save_object();
188 #     return($this->dialog->execute());
189 #   }
192     /* Check posts 
193      */
194     $once =true;
195     foreach($_POST as $name => $value){
197       /* Move Server Aliases one position up 
198        */
199       if((preg_match("/^SAup_/",$name)) && ($once)){
200         $once = false;
201         $id = preg_replace("/^SAup_/","",$name);
202         $id = preg_replace("/_.*$/","",$id);
203         $id = base64_decode($id);
204         $this->apacheServerAlias = $this->ArrayUp(($id+1),$this->apacheServerAlias);
205       }
207       /* Move Server Aliases one position down 
208        */
209       if((preg_match("/^SAdown_/",$name)) && ($once)){
210         $once = false;
211         
212         $id = preg_replace("/^SAdown_/","",$name);
213         $id = preg_replace("/_.*$/","",$id);
214         $id = base64_decode($id);
215   
216         $this->apacheServerAlias = $this->ArrayDown(($id+1),$this->apacheServerAlias);
217       }
219       /* Delete a "Server Alias"  
220        */
221       if((preg_match("/^SAdel_/",$name)) && ($once)){
222         $once = false;
223         $id = preg_replace("/^SAdel_/","",$name);
224         $id = preg_replace("/_.*$/","",$id);
225         $id = base64_decode($id);
226         unset($this->apacheServerAlias[$id]);
227         $tmp  =array();
228         foreach($this->apacheServerAlias as $entry){
229           $tmp[] = $entry;
230         }
231         $this->apacheServerAlias = $tmp; 
232       }
234       /* Move "Script Alias" one position up 
235        */
236       if((preg_match("/^SCup_/",$name)) && ($once)){
237         $once = false;
238         $id = preg_replace("/^SCup_/","",$name);
239         $id = preg_replace("/_.*$/","",$id);
240         $id = base64_decode($id);
241         $this->apacheScriptAlias = $this->ArrayUp(($id+1),$this->apacheScriptAlias);
242       }
244       /* Move "Script Alias" one position down
245        */
246       if((preg_match("/^SCdown_/",$name)) && ($once)){
247         $once = false;
248         $id = preg_replace("/^SCdown_/","",$name);
249         $id = preg_replace("/_.*$/","",$id);
250         $id = base64_decode($id);
251         $this->apacheScriptAlias = $this->ArrayDown(($id+1),$this->apacheScriptAlias);
252       }
254       /* Remove "Script Alias"
255        */
256       if((preg_match("/^SCdel_/",$name)) && ($once)){
257         $once = false;
258         $id = preg_replace("/^SCdel_/","",$name);
259         $id = preg_replace("/_.*$/","",$id);
260         $id = base64_decode($id);
261         unset($this->apacheScriptAlias[$id]);
262         $tmp  =array();
263         foreach($this->apacheScriptAlias as $entry){
264           $tmp[] = $entry;
265         }
266         $this->apacheScriptAlias = $tmp; 
267       }
268     }
270     /* Add a new "Server Alias" 
271      */
272     if((isset($_POST['AddSARecord'])) && (!empty($_POST['StrSAAlias'])) && (!empty($_POST['StrSADir']))){
273       $this->apacheServerAlias[] = trim($_POST['StrSAAlias']." ".$_POST['StrSADir']);      
274     }
275     
276     /* Add a new "Script Alias" 
277      */
278         if((isset($_POST['AddSCRecord'])) && (!empty($_POST['StrSCAlias'])) && (!empty($_POST['StrSCDir']))){
279       $this->apacheScriptAlias[] = trim($_POST['StrSCAlias']." ".$_POST['StrSCDir']);      
280     }
282     /* Fill in values */
283     foreach($this->attributes as $name){
284       $smarty->assign($name,$this->$name);
285     }
287     /* Set apacheServerNames without server suffix */
288     $smarty->assign("apacheServerName",$this->apacheServerName);
289     $smarty->assign("NotNew", false);
292     /* Create Server Aliases divlist 
293      */
294     $div = new divSelectBox("apacheServerAlias");
295     $div->setHeight(120);
296     $recs = $this->apacheServerAlias;
298     $oneup    = "<input name='SAup_%s'    type='image' src='images/lists/sort-up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
299     $onedown  = "<input name='SAdown_%s'  type='image' src='images/lists/sort-down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
300     $onedel   = "<input name='SAdel_%s'   type='image' src='images/lists/trash.png'      title='"._("Delete")."'  class='center'>";
301     foreach($recs as $key => $rec){
302       $div ->AddEntry(array(
303             array("string"=>$rec),
304             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
305                   "attach"=>"style='width:70px;border-right:0px;'")
306             ));
307     }
308     $smarty->assign("apacheServerAlias",  $div->DrawList());
311     /* Create Server Aliases divlist 
312      */
313     $div = new DivSelectBox("apacheScriptAlias");
314     $div->setHeight(120);
315     $recs = $this->apacheScriptAlias;
317     $oneup    = "<input name='SCup_%s'    type='image' src='images/lists/sort-up.png'    title='"._("Up")."'      class='center'>&nbsp;"; 
318     $onedown  = "<input name='SCdown_%s'  type='image' src='images/lists/sort-down.png'  title='"._("Down")."'    class='center'>&nbsp;"; 
319     $onedel   = "<input name='SCdel_%s'   type='image' src='images/lists/trash.png'      title='"._("Delete")."'  class='center'>"; 
320     
321     foreach($recs as $key => $rec){
322       $div ->AddEntry(array(
323             array("string"=>$rec),
324             array("string"=>str_replace("%s",base64_encode($key),$oneup.$onedown.$onedel),
325                   "attach"=>"style='width:70px;border-right:0px;'")
326             ));
327     }
328     $smarty->assign("apacheScriptAlias",  $div->DrawList());    
329     
330     /* Display template */
331     $display.= $smarty->fetch(get_template_path('servApacheVhostEdit.tpl', TRUE, dirname(__FILE__)));
332     return($display);
333   }
336   function remove_from_parent()
337   {
338   }
341   /* Save data to object */
342   function save_object()
343   {
344     plugin::save_object();
345   }
348   /* Check supplied data */
349   function check()
350   {
351     /* Call common method to give check the hook */
352     $message= plugin::check();
353         
354     /* Check if apacheServerName is already in use */
355     $usedVhosts = $this->getUsedServerNames();
356     if(($this->isNew == true)||($this->apacheServerName  != $this->InitialApacheServerName)){
357       if((isset($usedVhosts[$this->apacheServerName]))&&($this->apacheServerName  != $this->InitialApacheServerName)){
358         $message[] = msgPool::duplicated(_("Name"));
359       }
360     }
362     /* Check given virtual server name 
363      */
364     if(!tests::is_domain($this->apacheServerName) || empty($this->apacheServerName)){
365       $message[] = msgPool::invalid(_("Virtual host name"),$this->apacheServerName,"",
366           _("Only lowercase strings are allowed as Virtual Host Name.")."&nbsp;"._("e.g. example.com"));
367     }
369     /* check if there is a valid document root given 
370      */
371     if(!tests::is_path($this->apacheDocumentRoot) || empty($this->apacheDocumentRoot)){
372       $message[] = msgPool::invalid(_("Document root"),$this->apacheDocumentRoot);
373     }
374    
375     /* Check given mail address 
376      */ 
377         if(!tests::is_email($this->apacheServerAdmin) || empty($this->apacheServerAdmin)){
378       $message[] = msgPool::invalid(_("Admin mail address"),$this->apacheServerAdmin);
379     }
381     /* Check server aliases 
382      */    
383     foreach($this->apacheServerAlias as $key => $line){
384       $apacheServerAlias_ar=split(" ",$line);
385       $url=$apacheServerAlias_ar[1];
386       if(!tests::is_path($url)){
387         $message[] = msgPool::invalid(_("Server alias"));
388       }
389     }
390    
391     /* Check script aliases 
392      */    
393     foreach($this->apacheScriptAlias as $key => $line){
394       $apacheScriptAlias_ar=split(" ",$line);
395       $url=$apacheScriptAlias_ar[1];
396       if(!tests::is_path($url)){
397         $message[] = msgPool::invalid(_("Script alias"));
398       }
399     }
401     return ($message);
402   }
405   /* This funtion returns all used apacheServerNames */
406   function getUsedServerNames()
407   {
408     $ret = array();
409     $ldap = $this->config->get_ldap_link();
410     $ldap->cd($this->config->current['BASE']);
411     $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
412     while($attr = $ldap->fetch()){
413         $ret[$attr['apacheServerName'][0]]="";
414     }
415     return($ret);
416   }
419   /* Save to LDAP */
420   function save()
421   {
422     $ret =array();
423     foreach($this->attributes as $name){
424       $ret[$name] = $this->$name;
425     }
427     /* Create SA records 
428      */
429     foreach($this->apacheServerAlias as $key => $rec){
430       $rec['value']= $key." ".$rec['value'];
431       $this->Records [] = $rec;
432     }
434     /* Create SC records 
435      */
436     foreach($this->apacheScriptAlias as $key => $rec){
437       $rec['value']= $key." ".$rec['value'];
438       $this->Records [] = $rec;
439     }
441     $ret['RECORDS'] = $this->Records; 
442     $ret['InitialApacheServerName']   =  $this->InitialApacheServerName;
443     return($ret);
444   }
447 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
448 ?>