Code

822940453d043f7fe692f7fd8747538fdfab2050
[gosa.git] / setup / class_setupStep8.inc
1 <?php
3 /*
4    This code is part of GOsa (https://gosa.gonicus.de)
5    Copyright (C) 2007 Fabian Hickert
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
23 class setup_step_8 extends setup_step
24 {
25   var $create_backup    = TRUE;
26   var $gosa_conf_name   = "/gosa.conf";
27   var $cfg_file_written = FALSE;
28   var $last_backup_name = "";
30   function setup_step_8()
31   {
32     $this->update_strings();
33   }
35   
36   function update_strings()
37   {
38     $this->s_title      = _("Configuration file");
39     $this->s_info       = _("In this step the configuration file will be created.");
40     $this->s_title_long = _("Saving configuration file");
41   }
44   function get_conf_data()
45   {
46     $smarty = get_smarty();
47     $smarty->assign("cv",$this->parent->captured_values);
48     $str =  $smarty->fetch(CONFIG_TEMPLATE_DIR.$this->gosa_conf_name);
49     return($str);
50   }  
53   function execute()
54   {
56     $info= posix_getgrgid(posix_getgid());
57     $webgroup = $info['name'];
58       
59     
60     /* Check if there is currently an active gosa.conf 
61      */
62     $exists = file_exists(CONFIG_DIR.$this->gosa_conf_name); 
64     /* Check if existing config file is writeable */
65     if($exists){
66       $writeable = is_writeable(CONFIG_DIR.$this->gosa_conf_name);
67     }else{
68       $writeable = is_writeable(CONFIG_DIR);
69     }
71     /* Downlaod config */
72     if(isset($_POST['getconf'])){
74       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
75       header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
76       header("Cache-Control: no-cache");
77       header("Pragma: no-cache");
78       header("Cache-Control: post-check=0, pre-check=0");
79       header("Content-type: text/plain");
80      
81       if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
82           preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
83         header('Content-Disposition: filename="gosa.conf"');
84       } else {
85         header('Content-Disposition: attachment; filename="gosa.conf"');
86       }
88       $str = $this->get_conf_data();
89       $str = preg_replace("/\n[ ]*\n/","",$str);
91       echo $this->get_conf_data();
92       exit();
93     }
94    
95     
96     /* Try to save configuration */
97     $abort    = FALSE;
98     $err_msg  = "";
99     if(isset($_POST['saveconf'])){
100       if($exists && $this->create_backup){
101         if(!$this->create_backup()){
102           $abort = TRUE;
103           $err_msg = _("Could not create requested configuration file backup. Aborted writing config file. Please check folder permission and try again. Or use the manual method if this can not be fixed anyway.");
104         }
105       }
106     
107       if(!$abort){
108         
109         /* Try to create file handle */
110         $fp = @fopen(CONFIG_DIR.$this->gosa_conf_name, "w");
111     
112         if(!$fp){
113           $err_msg = sprintf(_("Can not create handle on file '%s', the configuration could not be written. Please check folder permission and try again. Or use the manual method if this can not be fixed anyway."),CONFIG_DIR.$this->gosa_conf_name);
114           $abort  =TRUE;
115         }else{
117           $data = $this->get_conf_data();
118           if(!fwrite($fp,$data)){
119             $err_msg = sprintf(_("Can not write file '%s'. Please check folder permission and try again. Or use the manual method if this can not be fixed anyway."),CONFIG_DIR.$this->gosa_conf_name);
120             $abort  =TRUE;
121           }else{
122             
123             @chgrp(CONFIG_DIR.$this->gosa_conf_name,$webgroup);
124             @chown(CONFIG_DIR.$this->gosa_conf_name,"root");
125             @chmod(CONFIG_DIR.$this->gosa_conf_name,0640);    
126           }
127         }
128       }
129     } 
132     if($exists && $this->is_world_readable(CONFIG_DIR.$this->gosa_conf_name)){
133       $err_msg = _("Your configuration file is currently world readable. This is a big security risk. Please updated the file permissions as shown in the manual configuration part below.");
134     }
137     $smarty = get_smarty();
138     $smarty->assign("save_requested", isset($_POST['saveconf']));
139     $smarty->assign("err_msg",$err_msg);
140     $smarty->assign("webgroup", $webgroup);
141     $smarty->assign("gosa_conf_name" , $this->gosa_conf_name);
142     $smarty->assign("create_backup" , $this->create_backup);
143     $smarty->assign("CONFIG_DIR",CONFIG_DIR);
144     $smarty->assign("exists",$exists);
145     $smarty->assign("last_backup_name",$this->last_backup_name);
146     $smarty->assign("writeable",$writeable);
147     $smarty->assign("cv",$this->parent->captured_values);
148     $smarty->assign("msg_permissions",
149       sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
150     return($smarty -> fetch (get_template_path("../setup/setup_step8.tpl")));
151   }
154   /* check if given file is world readable */
155   function is_world_readable($file)
156   {
157     clearstatcache();
158     $p = fileperms($file);
159     $w_r = (decbin($p & 4) == TRUE);
160     return($w_r);
161   }
164   /* Create a backup of the currently existing configuration file. 
165    */
166   function create_backup()
167   {
168     $info= posix_getgrgid(posix_getgid());
169     $webgroup = $info['name'];
170     if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR.$this->gosa_conf_name)){
171       $src = CONFIG_DIR.$this->gosa_conf_name;
172       $dst = CONFIG_DIR.$this->gosa_conf_name."_".date("Ymd");
173       $dst_backup= $dst;
174       $i = 1;
175       while(file_exists($dst)){
176         $dst = $dst_backup."-".$i;
177         $i ++;
178       }
179       if(copy($src,$dst)){
180         $this->last_backup_name = $dst;
181         @chgrp($dst,$webgroup);
182         @chown($dst,"root");
183         @chmod($dst,0640);    
184         return(TRUE);
185       }else{
186         return(FALSE);
187       }
188     }else{
189       return(FALSE);
190     }
191   }
194   function save_object()
195   {
196     if(isset($_POST['step8_posted'])){
198       /* Get attributes */
199       foreach($this->attributes as $attr){
200         if(isset($_POST[$attr])){
201           $this->$attr = validate($_POST[$attr]);
202         }
203       }
205       /* Backup toggle */
206       if(isset($_POST['create_backup_visible'])){
207         if(isset($_POST['create_backup'])){
208           $this->create_backup = TRUE;
209         }else{
210           $this->create_backup = FALSE;
211         }
212       }
213     }
214   }
217 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
218 ?>