Code

4b97104b33b865a7c9a64b5571f958fa3630246d
[gosa.git] / vhost-apache2 / src / class_servApacheVhost.inc
1 <?php
3 class servapache extends plugin
4 {
5   /* attribute list for save action */
6   var $ignore_account   = FALSE;
7   var $attributes       = array(); 
8   var $objectclasses    = array("whatever");
10   var $RecordTypes      = array();
11   var $Vhosts            = array();
12   var $dialog           = NULL;
14   var $orig_dn          = "";
16   var $APACHEinitially_was_account;
19   function servapache ($config, $dn= NULL, $parent= NULL)
20   {
21     plugin::plugin ($config, $dn, $parent);
23     $this->orig_dn = $dn;
25     /* Get all vhost Informations
26      */
27     $this->Vhosts = getVhostEntries($config,$dn);
29     /* If there is at least one entry in this -> types, we have apache vhosts enabled 
30      */
31     if(count($this->Vhosts) == 0){
32       $this->is_account = false;
33     }else{
34       $this->is_account = true;
35     }
36     $this->APACHEinitially_was_account = $this->is_account;
37   }
40   function execute()
41   {
42     /* Call parent execute 
43      */
44     plugin::execute();
46     /* Fill templating stuff 
47      */
48     $smarty= get_smarty();
49     $display= "";
51     /* Do we need to flip is_account state? 
52      */
53     if (isset($_POST['modify_state'])){
55       /* Only change account state if allowed */
56       if($this->is_account && $this->acl == "#all#"){
57         $this->is_account= !$this->is_account;
58         $this->is_modified = true;
59       }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
60         $this->is_account= !$this->is_account;
61         $this->is_modified = true;
62       }
63     }
65     if ($this->is_account){
66       $display= $this->show_header(_("Remove Apache service"),
67           _("This server has Apache features enabled. You can disable them by clicking below."));
68     } else {
69       $display= $this->show_header(_("Add Apache service"),
70           _("This server has Apache features disabled. You can enable them by clicking below."));
71       return ($display);
72     }
74     /* Edited or Added vhost 
75      */
76     if((isset($_POST['SaveVhostChanges'])) && is_object($this->dialog)){
77       $this->dialog->save_object();
79       /* Check for errors  
80        */
81       if(count($this->dialog->check())){
82         foreach($this->dialog->check() as $msgs){
83           print_red($msgs); 
84         }
85       }else{
86         /* add new/edited vhost 
87          */
88         $ret = $this->dialog->save();
89         if(!$this->dialog->isNew){
90           unset($this->Vhosts[$this->dialog->OldApacheServerName]);
91         }
92         $this->Vhosts[$ret['apacheServerName']] = $ret;
93         $this->dialog = NULL;
94       }
95     }
97     /* Cancel vhost edit / new 
98      */
99     if(isset($_POST['CancelVhostChanges'])){
100       $this->dialog = NULL;
101     }
103     /* Add empty new vhost 
104      */
105     if(isset($_POST['AddVhost']) && chkacl($this->acl,"servapache") == ""){
106       $this->dialog = new servapacheVhostEdit($this->config,$this->dn);
107     }
109     /* Check for edit vhost request 
110      */
111     $once = false;
112     foreach( $_POST as $name => $value){
113         //user_error(print_r($this->Vhosts,true));
114       /* check all post for edit request 
115        */
116       if(preg_match("/^editVhost_/",$name)&&!$once && chkacl($this->acl,"servapache") == ""){
117         $once =true;
118         $tmp = preg_replace("/^editVhost_/","",$name);
119         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
120         $this->dialog= new servapacheVhostEdit($this->config,$this->dn,$this->Vhosts[$tmp]);
121       }
123       /* check posts for delete vhost 
124        */
125       if(preg_match("/^delVhost_/",$name)&&!$once && chkacl($this->acl,"servapache") == ""){
127         $once =true;
128         $tmp = preg_replace("/^delVhost_/","",$name);
129         $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
130      
131         /* Initiate deletion
132          */ 
133         $this->RemoveVhost($tmp);
134       }
135     }
137     /* Show dialog 
138      */
139     if($this->dialog!= NULL){
140       $this->dialog->save_object();
141       $this->dialog->parent = $this;
142       return($this->dialog->execute());
143     }
145     /* Create Listbox with existing Vhosts 
146      */
147     $VhostList = new divSelectBox("apacheConfigs");
148     $VhostList -> SetHeight(254);
149     
150     /* Add entries to divlist
151      */
152     $editImg = "<input type='image' src='images/edit.png' name='editVhost_%s'>
153       <input type='image' src='images/edittrash.png' name='delVhost_%s'>";
154     foreach($this->Vhosts as $vhost => $values ){
155       $VhostList->AddEntry(array(
156             array("string" => $vhost),
157             array("string" => str_replace("%s",base64_encode($vhost),$editImg))
158             ));
159     }    
160   
161     $smarty->assign("servapacheACL",chkacl($this->acl,"servapache"));
162   
163     /* Display template 
164      */
165     $smarty->assign("VhostList",$VhostList->DrawList());
166     $display.= $smarty->fetch(get_template_path('servApacheVhost.tpl', TRUE));
167     return($display);
168   }
171   /* Delete specified vhost
172    */  
173   function RemoveVhost($id)
174   {
175       unset($this->Vhosts[$id]);
176       return(true);
177   }
180   /* This function returns all used Vhostnames 
181    */
182   function getUsedServerNames()
183   {
184     $ret = array();
185     $ldap = $this->config->get_ldap_link();
186     $ldap->cd($this->config->current['BASE']);
187     $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
188     while($attr = $ldap->fetch()){
189       $ret[$attr['apacheServerName'][0]][] = $attr['dn'];
190     }
191     return($ret);
192   }
195     /* Remove apache service
196    */
197   function remove_from_parent()
198   {
199     if($this->APACHEinitially_was_account){
200       $bool = true;
201       foreach($this->Vhosts as $key => $vhost){
202         $bool= $bool & $this->RemoveVhost($key);
203       }
205       if($bool){
206         $this->save();
207       }
208       return($bool);
209     }
210   }
213   /* Save to LDAP */
214   function save()
215   {
216     $ldap = $this->config->get_ldap_link();
217     $ldap->cd($this->config->current['BASE']);  
218   
219     /* Get differences 
220      */
222     $old_dn =  $this->orig_dn;
223     if($old_dn == "new"){
224       $old_dn = $this->dn;
225     }
227     $tmp = getVhostEntriesDiff($this->config,$this->Vhosts,$old_dn);
229     /* Updated vhost entries if reverser or forward name has changed  
230      * Must be done before moving entries, else the given dn is invalid
231      */
232     if(isset($tmp['vhostUpdates'])){
233       foreach($tmp['vhostUpdates'] as $dn => $attrs){
234         $ldap->cd($dn);
235         $ldap->modify($attrs);
236         show_ldap_error("Vhost:".$ldap->get_error(), _("Updating Apache service failed"));
237       }
238     }
240     /* Delete apache vhost
241      */
242     foreach($tmp['del'] as $dn => $del){
243       $ldap->cd($dn);
244       $ldap->rmdir_recursive($dn);
245       show_ldap_error($ldap->get_error(), _("Removing Apache entries failed"));
246     }
248     /* move follwoing entries
249      */
250     foreach($tmp['move'] as $src => $dst){
251       $this->recursive_move($src,$dst);
252     }
254     /* Add || Update new apache entries
255      */
256     foreach($tmp['add'] as $dn => $attrs){
257       $ldap->cd($dn);
258 //      user_error(print_r($dn,true));
259       $ldap->cat($dn, array('dn'));
260       if(count($ldap->fetch())){
261         $ldap->cd($dn);
262         $ldap->modify ($attrs);
263       }else{
264         $ldap->cd($dn);
265         $ldap->add($attrs);
266       }
267       show_ldap_error($ldap->get_error(), _("Saving apache entries failed"));
268     }
269   }
271 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
272 ?>