Code

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