Code

- French locale
[gosa.git] / vhost-apache2 / src / 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 template for all fetched vhosts 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 vhosts 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 vhosts 
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 vhosts 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 apache vhosts 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 vhosts with default values
90    */
91   $zoneBase                       = array();
92   $zoneBase['objectClass']        = array("top","apacheConfig");
93   $zoneBase['apacheServerName']           = "";
95   /* Contains all renamed apacheServerNames 
96    * For vhosts entry udpdates
97    */
98   $PrePareVhostEntries = array();
100   /* Walk through all vhosts and detect renamed/added/deleted vhosts ... 
101    */
102   foreach($newVhosts as $name => $zone){
103     
104     /* This vhosts was renamed 
105      */
106     if((!empty($zone['InitialApacheServerName'])) && ($zone['InitialApacheServerName'] != $zone['apacheServerName'])){
107       
108       /* Move old vhosts 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 vhosts 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 vhosts 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'];
132  
133                 if(!empty($zone['apacheServerAlias'])) {
134                 
135                         foreach($zone['apacheServerAlias'] as $rec){
136         $obj['apacheServerAlias'][] = $rec;
137         }
138     }
139     
140                 if(!empty($zone['apacheScriptAlias'])) {
141                         foreach($zone['apacheScriptAlias'] as $rec){
142         $obj['apacheScriptAlias'][] = $rec;
143         }
144                 }
145                 
146     /* Append udpated Vhost Entry to our add queue
147      */    
148     $add[$newDn] = $obj;   
149  
150     /* Remove currently managed vhosts from oldVhosts.
151      *  this gives us the ability to detect removed vhosts
152      */
153     if(isset($oldVhosts[$zone['InitialApacheServerName']])){
154       unset($oldVhosts[$zone['InitialApacheServerName']]);
155     }
156   }
157  
158   /* The rest of our oldVhosts must be deleted
159    *  because they are no longer available in newVhosts anymore.
160    */
161   foreach($oldVhosts as $zone)  {
162     $oldDn = "apacheServerName=".$zone['InitialApacheServerName'].",".$HostDn;
163     $del[$oldDn] = "";
164   }
166   /* Check for entries which must be updated 
167    */
168   $zoneUpdates = array();
169   $udpate = array();
170   if(count($PrePareVhostEntries)){
171     $ldap = $config->get_ldap_link();
172     foreach($PrePareVhostEntries as $FromVhostName => $ToVhostName){
173       $ldap->cd($HostDn);
174       $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=".$FromVhostName."))",array("apacheServerName"));
175       while($attrs = $ldap->fetch()){
176         $zoneUpdates[$attrs['dn']] = array("apacheServerName"=>$ToVhostName);
177       }
178     }
179   }
181   $ret = array("del" => $del , "move" => $move , "add" => $add,"vhostUpdates"=>$zoneUpdates);
182 //      user_error(print_r($ret,true));
183   return($ret);
186 /* returns the dn for a specified zone
187  */
188 function getVhostDN($config,$apacheServerNameMix)
190   $ret = "";
191   if(!strstr($apacheServerNameMix, '/')) {
192     print_red(sprintf(_("Undefined vhost name '%s'. Vhost name must look like this 'www.example.com or example.com'."),$apacheServerNameMix));
193     return($ret);
194   }
196   $apacheServerNameIndex        = split("/",$apacheServerNameMix); 
197   $apacheServerName           = $apacheServerNameIndex[1];
198   $nameServer                   = strtolower($apacheServerNameIndex[0]);
199   $ldap               = $config->get_ldap_link();
201   /* search for the nameserver */
202   $ldap-> cd($config->current['BASE']);
203   $ldap->search("(&(objectClass=goServer)(cn=".$nameServer."))",array("cn"));
204   if($ldap->count()){
205     $attr = $ldap->fetch();
206   } else {
207     return($ret);
208   }
209   
210   $ldap-> cd($attr['dn']);
211   $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=".$apacheServerName."))",array("apacheServerName"));
212   if($ldap->count()){
213     $attr = $ldap->fetch();
214     return($attr['dn']);
215   }
216   
217   return($ret);
221 /* returns all available vhosts
222  *  array[reverseName] = apacheServerName;
223  */
224 function getAvailableVhosts($config)
226   $ret = array();
227   $ldap = $config->get_ldap_link();
228   $ldap->cd ($config->current['BASE']);
230   /* Search for vhosts ...
231    */
232   $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
234   $ForwardVhosts = array();
235   $zones = array();
237   while($at = $ldap->fetch()){
238     $ForwardVhosts[$at['dn']] = $at;
239   }
241   foreach($ForwardVhosts as $dn => $obj){
242     
243       if(preg_match("/".$dn."/",$Rdn)){
244         $zones[$Robj['apacheServerName'][0]] =$obj['apacheServerName'][0];
245       }   
246   }
247   return($zones);
249   
250 /* Simple is domain check, it checks if the given string looks like "string.string.string"
251    or string.string */
252 function GetVhostsDomain($str)
254   return(preg_match("/[^\.\/]+\.[^\.\/]+$/",$str));
257 ?>