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 Step_Finish extends setup_step
24 {
25 var $create_backup = TRUE;
26 var $gosa_conf_contrib = "/gosa.conf";
27 var $cfg_file_written = FALSE;
28 var $last_backup_name = "";
30 function Step_Finish()
31 {
32 $this->update_strings();
33 }
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_contrib);
49 return($str);
50 }
53 function execute()
54 {
56 $info= posix_getgrgid(posix_getgid());
57 $webgroup = $info['name'];
60 /* Check if there is currently an active gosa.conf
61 */
62 $exists = file_exists(CONFIG_DIR."/".CONFIG_FILE);
64 /* Check if existing config file is writeable */
65 if($exists){
66 $writeable = is_writeable(CONFIG_DIR."/".CONFIG_FILE);
67 }else{
68 $writeable = is_writeable(CONFIG_DIR);
69 }
71 /* Redirect to GOsa login */
72 if(isset($_POST['use_gosa'])){
73 session_destroy();
74 header("Location: index.php");
75 exit();
76 }
78 /* Downlaod config */
79 if(isset($_POST['getconf'])){
81 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
82 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
83 header("Cache-Control: no-cache");
84 header("Pragma: no-cache");
85 header("Cache-Control: post-check=0, pre-check=0");
86 header("Content-type: text/plain");
88 if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
89 preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
90 header('Content-Disposition: filename="'.CONFIG_FILE.'"');
91 } else {
92 header('Content-Disposition: attachment; filename="'.CONFIG_FILE.'"');
93 }
95 $str = $this->get_conf_data();
96 $str = preg_replace("/\n[ ]*\n/","",$str);
98 echo $this->get_conf_data();
99 exit();
100 }
103 /* Try to save configuration */
104 $abort = FALSE;
105 $err_msg = "";
106 if(isset($_POST['saveconf'])){
107 if($exists && $this->create_backup){
108 if(!$this->create_backup()){
109 $abort = TRUE;
110 $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.");
111 }
112 }
114 if(!$abort){
116 /* Try to create file handle */
117 $fp = @fopen(CONFIG_DIR."/".CONFIG_FILE, "w");
119 if(!$fp){
120 $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."/".CONFIG_FILE);
121 $abort =TRUE;
122 }else{
124 $data = $this->get_conf_data();
125 if(!fwrite($fp,$data)){
126 $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."/".CONFIG_FILE);
127 $abort =TRUE;
128 }else{
130 @chgrp(CONFIG_DIR."/".CONFIG_FILE,$webgroup);
131 @chown(CONFIG_DIR."/".CONFIG_FILE,"root");
132 @chmod(CONFIG_DIR."/".CONFIG_FILE,0640);
133 }
134 }
135 }
136 }
139 if($exists && $this->is_world_readable(CONFIG_DIR."/".CONFIG_FILE)){
140 $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.");
141 }
143 $smarty = get_smarty();
144 $smarty->assign("save_requested", isset($_POST['saveconf']));
145 $smarty->assign("err_msg",$err_msg);
146 $smarty->assign("webgroup", $webgroup);
147 $smarty->assign("create_backup" , $this->create_backup);
148 $smarty->assign("CONFIG_DIR",CONFIG_DIR);
149 $smarty->assign("CONFIG_FILE" , CONFIG_FILE);
150 $smarty->assign("exists",$exists);
152 $smarty->assign("msg1", sprintf(_("If you want the setup routine to write the configuration file, use the 'Save configuration' button below. If you prefer to copy the '%s' manually to '%s' you can download the configuration file by using the 'Download configuration' button."), CONFIG_FILE,CONFIG_DIR));
154 $smarty->assign("msg2", sprintf(_("After placing the file under %s, place make sure that the webserver user is able to read %s, while other users shouldn't. You may want to execute these commands to achieve this requirement"),CONFIG_DIR,CONFIG_FILE));
156 $smarty->assign("last_backup_name",$this->last_backup_name);
157 $smarty->assign("writeable",$writeable);
158 $smarty->assign("cv",$this->parent->captured_values);
159 $smarty->assign("msg_permissions",
160 sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
161 return($smarty -> fetch (get_template_path("../setup/setup_finish.tpl")));
162 }
165 /* check if given file is world readable */
166 function is_world_readable($file)
167 {
168 clearstatcache();
169 $p = fileperms($file);
170 $w_r = (decbin($p & 4) == TRUE);
171 return($w_r);
172 }
175 /* Create a backup of the currently existing configuration file.
176 */
177 function create_backup()
178 {
179 $info= posix_getgrgid(posix_getgid());
180 $webgroup = $info['name'];
181 if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR."/".CONFIG_FILE)){
182 $src = CONFIG_DIR."/".CONFIG_FILE;
183 $dst = CONFIG_DIR."/".CONFIG_FILE."_".date("Ymd");
184 $dst_backup= $dst;
185 $i = 1;
186 while(file_exists($dst)){
187 $dst = $dst_backup."-".$i;
188 $i ++;
189 }
190 if(copy($src,$dst)){
191 $this->last_backup_name = $dst;
192 @chgrp($dst,$webgroup);
193 @chown($dst,"root");
194 @chmod($dst,0640);
195 return(TRUE);
196 }else{
197 return(FALSE);
198 }
199 }else{
200 return(FALSE);
201 }
202 }
205 function save_object()
206 {
207 if(isset($_POST['step8_posted'])){
209 /* Get attributes */
210 foreach($this->attributes as $attr){
211 if(isset($_POST[$attr])){
212 $this->$attr = validate($_POST[$attr]);
213 }
214 }
216 /* Backup toggle */
217 if(isset($_POST['create_backup_visible'])){
218 if(isset($_POST['create_backup'])){
219 $this->create_backup = TRUE;
220 }else{
221 $this->create_backup = FALSE;
222 }
223 }
224 }
225 }
226 }
228 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
229 ?>