90bf51bbe00de33c7e3fc32b4af8d7a823ab9d44
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 $APACHE2initially_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 APACHE2 enabled
30 */
31 if(count($this->Vhosts) == 0){
32 $this->is_account = false;
33 }else{
34 $this->is_account = true;
35 }
36 $this->APACHE2initially_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,"servapache2") == ""){
106 $this->dialog = new apacheEditVhost($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,"servapache2") == ""){
117 $once =true;
118 $tmp = preg_replace("/^editVhost_/","",$name);
119 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
120 $this->dialog= new apacheEditVhost($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,"servapache2") == ""){
127 $once =true;
128 $tmp = preg_replace("/^delVhost_/","",$name);
129 $tmp = base64_decode(preg_replace("/_.*$/","",$tmp));
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);
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 }
161 $smarty->assign("servapache2ACL",chkacl($this->acl,"servapache2"));
163 /* Display tempalte
164 */
165 $smarty->assign("VhostList",$VhostList->DrawList());
166 $display.= $smarty->fetch(get_template_path('apache_vhost.tpl', TRUE));
167 return($display);
168 }
171 /* Delete specified vhost
172 */
173 function RemoveVhost($id)
174 {
175 $vhosts = $this->getUsedServerNames();
177 $vhostname = "";
178 if(isset($this->Vhosts[$id]['InitialApacheServerName'])){
179 $vhostname= $this->Vhosts[$id]['InitialApacheServerName'];
180 }
182 $used = array();
184 /* Add Records which use this apacheServerName
185 */
186 if(isset($vhosts[$vhostname])){
187 $used = array_merge($used,$vhosts[$vhostname]);
188 }
191 /* There are still entries using this configuration
192 * Abort deletion
193 */
194 if(count($used)){
195 $i = 2;
196 $str ="";
197 foreach($used as $dn){
198 if($i > 0 ){
199 $i --;
200 $str.=$dn." ";
201 }
202 }
204 /* Only show 2 apache2 in the error message
205 */
206 if(count($used)> 2) {
207 $str .=" ... ";
208 }
209 print_red(sprintf(_("Can't delete the selected vhost, because it is still in use by these entry/entries '%s'"),trim($str)));
211 }else{
212 unset($this->Vhosts[$id]);
213 return(true);
214 }
215 return(false);
216 }
219 /* This function returns all used Vhostnames
220 */
221 function getUsedServerNames()
222 {
223 $ret = array();
224 $ldap = $this->config->get_ldap_link();
225 $ldap->cd($this->config->current['BASE']);
226 $ldap->search("(&(objectClass=apacheConfig)(apacheServerName=*))",array("apacheServerName"));
227 while($attr = $ldap->fetch()){
228 $ret[$attr['apacheServerName'][0]][] = $attr['dn'];
229 }
230 return($ret);
231 }
235 /* Remove apache service
236 */
237 function remove_from_parent()
238 {
239 if($this->APACHE2initially_was_account){
240 $bool = true;
241 foreach($this->Vhosts as $key => $vhost){
242 $bool= $bool & $this->RemoveVhost($key);
243 }
245 if($bool){
246 $this->save();
247 }
248 return($bool);
249 }
250 }
254 /* Save to LDAP */
255 function save()
256 {
257 $ldap = $this->config->get_ldap_link();
258 $ldap->cd($this->config->current['BASE']);
260 /* Get differences
261 */
263 $old_dn = $this->orig_dn;
264 if($old_dn == "new"){
265 $old_dn = $this->dn;
266 }
268 $tmp = getVhostEntriesDiff($this->config,$this->Vhosts,$old_dn);
270 /* Updated vhost entries if reverser or forward name has changed
271 * Must be done before moving entries, else the given dn is invalid
272 */
273 if(isset($tmp['vhostUpdates'])){
274 foreach($tmp['vhostUpdates'] as $dn => $attrs){
275 $ldap->cd($dn);
276 $ldap->modify($attrs);
277 show_ldap_error("Vhost:".$ldap->get_error(), _("Updating Apache service failed"));
278 }
279 }
281 /* Delete apache vhost
282 */
283 foreach($tmp['del'] as $dn => $del){
284 $ldap->cd($dn);
285 $ldap->rmdir_recursive($dn);
286 show_ldap_error($ldap->get_error(), _("Removing Apache entries failed"));
287 }
289 /* move follwoing entries
290 */
291 foreach($tmp['move'] as $src => $dst){
292 $this->recursive_move($src,$dst);
293 }
295 /* Add || Update new apache entries
296 */
297 foreach($tmp['add'] as $dn => $attrs){
298 $ldap->cd($dn);
299 user_error(print_r($dn,true));
300 $ldap->cat($dn, array('dn'));
301 if(count($ldap->fetch())){
302 user_error("MODIFY".print_r($attrs,true));
303 $ldap->cd($dn);
304 $ldap->modify ($attrs);
305 }else{
306 $ldap->cd($dn);
307 $ldap->add($attrs);
308 }
309 show_ldap_error($ldap->get_error(), _("Saving apache entries failed"));
310 }
311 }
312 }
313 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
314 ?>