Code

9f11882f16a796dc28437c20524c88d27b33b28a
[gosa.git] / vhost-apache2 / functions_apache.inc
1 <?php
4 /* This function returns the vhost entries specified for given host
5  */
6 function getVhostEntries($config,$HostDn,$silent = false)
7 {
9   $ldap = $config->get_ldap_link();
10   $ldap->cd($config->current['BASE']); 
12   /* Get host entry */
13   $ldap->cat($HostDn);
14   $host_attr = $ldap->fetch();
16   /* Create tempalte for all fetched zone Data 
17    */
18   $VhostBase = array();
19   $VhostBase['exists']  = false;
20   //$VhostBase['RECORDS'] = array();
21   $VhostBase['apacheServerName'] = array();
22   $VhostBase['apacheConfig'] = array();
23     
24   $Vhosts    = array();
26   /* Get & Parse all zone entries 
27    */
28   $ldap->ls("(&(objectClass=apacheConfig)(apacheServerName=*))",$HostDn,array("*"));
29   $tmp_res = array();
30   while($attrs = $ldap->fetch()) {
31     $tmp_res[] = $attrs;
32   }
34   /* Parse fetched zones 
35    */
36   foreach($tmp_res as $attrs){
38     $apacheServerName                    = $attrs['apacheServerName'][0];
39     $apacheDocumentRoot                  = $attrs['apacheDocumentRoot'][0];
40     $apacheServerAdmin                   = $attrs['apacheServerAdmin'][0];
41     $Vhosts[$apacheServerName]           = $VhostBase;
42     $Vhosts[$apacheServerName]['exists'] = true;
44     /* Set basic attributes 
45      */
46     if(isset($attrs["apacheConfig"][0])){
47       $Vhosts[$apacheServerName]["apacheConfig"] = $attrs["apacheConfig"][0];
48     }
50     /* Set initial zone name, to be able to detect if this entry was renamed 
51      */
52     $Vhosts[$apacheServerName]['InitialApacheServerName'] = $apacheServerName;
53     $Vhosts[$apacheServerName]['apacheServerName'] = $apacheServerName;
54     $Vhosts[$apacheServerName]['apacheDocumentRoot'] = $apacheDocumentRoot;
55     $Vhosts[$apacheServerName]['apacheServerAdmin'] = $apacheServerAdmin;
57   
58     if (isset($attrs['apacheServerAlias'])){
59         for($i = 0 ; $i < $attrs['apacheServerAlias']['count']; $i ++){
60                 $Vhosts[$apacheServerName]['apacheServerAlias'][] =  $attrs['apacheServerAlias'][$i];
61         }
62     }
63     
64         if (isset($attrs['apacheScriptAlias'])){
65         for($i = 0 ; $i < $attrs['apacheScriptAlias']['count']; $i ++){
66                 $Vhosts[$apacheServerName]['apacheScriptAlias'][] =  $attrs['apacheScriptAlias'][$i];
67         }
68     }
70   }     
71   return($Vhosts);
72 }
75 /* This function compares two dns zone objects and returns an 
76  *  array with following indexes 
77  *   - delete, for vhost entries which must be deleted (only if vhost entries is removed)
78  *   - rename, if a dn must be renamed, for example, the apacheServerName has changed
79  *   - add,    if there is a new vhost entries created    
80  */
81 function getVhostEntriesDiff($config,$newVhosts,$HostDn)
82 {
83   $oldVhosts = getVhostEntries($config,$HostDn,true);
85   $move   = array();
86   $add    = array();
87   $del    = array();
89   /* Generate a template for zones with default values
90    */
91   $zoneBase                       = array();
92   $zoneBase['objectClass']        = array("top","apacheConfig");
93   $zoneBase['apacheServerName']           = "";
95   /* Contains all renamed apacheServerNames 
96    * For zone entry udpdates
97    */
98   $PrePareVhostEntries = array();
100   /* Walk through all zones and detect renamed/added/deleted zones ... 
101    */
102   foreach($newVhosts as $name => $zone){
103     
104     /* This zone was renamed 
105      */
106     if((!empty($zone['InitialApacheServerName'])) && ($zone['InitialApacheServerName'] != $zone['apacheServerName'])){
107       
108       /* Move old zone to new position 
109        */ 
110       $oldDn = "apacheServerName=".$zone['InitialApacheServerName'].",".$HostDn;
111       $newDn = "apacheServerName=".$zone['apacheServerName'].",".$HostDn;
112       $PrePareVhostEntries[$zone['InitialApacheServerName']] = $zone['apacheServerName'];
113       $move [$oldDn] = $newDn;      
114     }
116     /* Get old zone if available
117      */
118     $oldVhost=array();
119     if(isset($oldVhosts[$zone['InitialApacheServerName']])){
120             if(!empty($oldVhosts[$zone['InitialApacheServerName']])){
121               $oldVhost = $oldVhosts[$zone['InitialApacheServerName']];
122             }
123     }
125     /* Create forward zone entry and put it in our add queue
126      */
127     $newDn  = "apacheServerName=".$zone['apacheServerName'].",".$HostDn;
128     $obj    =  $zoneBase;
129     $obj['apacheServerName'] = $zone['apacheServerName'];
130         $obj['apacheDocumentRoot'] = $zone['apacheDocumentRoot'];
131         $obj['apacheServerAdmin'] = $zone['apacheServerAdmin'];
133  
134         foreach($zone['apacheServerAlias'] as $rec){
135       $obj['apacheServerAlias'][] = $rec;
136     }
137         foreach($zone['apacheScriptAlias'] as $rec){
138       $obj['apacheScriptAlias'][] = $rec;
139     }
141     /* Append udpated Vhost Forward Entry to our add queue
142      */    
143     $add[$newDn] = $obj;   
144  
145     /* Remove currently managed zone from oldVhosts.
146      *  this gives us the ability to detect removed zones
147      */
148     if(isset($oldVhosts[$zone['InitialApacheServerName']])){
149       unset($oldVhosts[$zone['InitialApacheServerName']]); 
150     }
151   }
152  
153   /* The rest of our oldVhosts must be deleted
154    *  because they are no longer available in newVhosts anymore.
155    */
156   foreach($oldVhosts as $zone)  {
157     $oldDn = "apacheServerName=".$zone['InitialApacheServerName'].",".$HostDn;
158     $del[$oldDn] = "";
159   }
161   /* Check for entries which must be updated 
162    */
163   $zoneUpdates = array();
164   $udpate = array();
165   if(count($PrePareVhostEntries)){
166     $ldap = $config->get_ldap_link();
167     foreach($PrePareVhostEntries as $FromVhostName => $ToVhostName){
168       $ldap->cd($HostDn);
169       $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=".$FromVhostName."))",array("apacheServerName"));
170       while($attrs = $ldap->fetch()){
171         $zoneUpdates[$attrs['dn']] = array("apacheServerName"=>$ToVhostName);
172       }
173     }
174   }
176   $ret = array("del" => $del , "move" => $move , "add" => $add,"vhostUpdates"=>$zoneUpdates);
177         user_error(print_r($ret,true));
178   return($ret);
181 /* returns the dn for a specified zone
182  */
183 function getVhostDN($config,$apacheServerNameMix)
185   $ret = "";
186   if(!strstr($apacheServerNameMix, '/')) {
187     print_red(sprintf(_("Undefined zone name '%s'. Vhost name must look like this 'server/zone.com'."),$apacheServerNameMix));
188     return($ret);
189   }
191   $apacheServerNameIndex        = split("/",$apacheServerNameMix); 
192   $apacheServerName           = $apacheServerNameIndex[1];
193   $nameServer                   = strtolower($apacheServerNameIndex[0]);
194   $ldap               = $config->get_ldap_link();
196   /* search for the nameserver */
197   $ldap-> cd($config->current['BASE']);
198   $ldap->search("(&(objectClass=goServer)(cn=".$nameServer."))",array("cn"));
199   if($ldap->count()){
200     $attr = $ldap->fetch();
201   } else {
202     return($ret);
203   }
204   
205   $ldap-> cd($attr['dn']);
206   $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=".$apacheServerName."))",array("apacheServerName"));
207   if($ldap->count()){
208     $attr = $ldap->fetch();
209     return($attr['dn']);
210   }
211   
212   return($ret);
216 /* returns all available zones 
217  *  array[reverseName] = apacheServerName;
218  */
219 function getAvailableVhosts($config)
221   $ret = array();
222   $ldap = $config->get_ldap_link();
223   $ldap->cd ($config->current['BASE']);
225   /* Search for zones ...
226    */
227   $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
229   $ForwardVhosts = array();
230   $zones = array();
232   while($at = $ldap->fetch()){
233     $ForwardVhosts[$at['dn']] = $at;
234   }
236   foreach($ForwardVhosts as $dn => $obj){
237     
238       if(preg_match("/".$dn."/",$Rdn)){
239         $zones[$Robj['apacheServerName'][0]] =$obj['apacheServerName'][0];
240       }   
241   }
242   return($zones);
245 ?>