72b6dffbbb6c4d331d579b51a7d3f0dadcd238e9
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 }
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'];
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 /* 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="gosa.conf"');
91 } else {
92 header('Content-Disposition: attachment; filename="gosa.conf"');
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.$this->gosa_conf_name, "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.$this->gosa_conf_name);
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.$this->gosa_conf_name);
127 $abort =TRUE;
128 }else{
130 @chgrp(CONFIG_DIR.$this->gosa_conf_name,$webgroup);
131 @chown(CONFIG_DIR.$this->gosa_conf_name,"root");
132 @chmod(CONFIG_DIR.$this->gosa_conf_name,0640);
133 }
134 }
135 }
136 }
139 if($exists && $this->is_world_readable(CONFIG_DIR.$this->gosa_conf_name)){
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 }
144 $smarty = get_smarty();
145 $smarty->assign("save_requested", isset($_POST['saveconf']));
146 $smarty->assign("err_msg",$err_msg);
147 $smarty->assign("webgroup", $webgroup);
148 $smarty->assign("gosa_conf_name" , $this->gosa_conf_name);
149 $smarty->assign("create_backup" , $this->create_backup);
150 $smarty->assign("CONFIG_DIR",CONFIG_DIR);
151 $smarty->assign("exists",$exists);
152 $smarty->assign("last_backup_name",$this->last_backup_name);
153 $smarty->assign("writeable",$writeable);
154 $smarty->assign("cv",$this->parent->captured_values);
155 $smarty->assign("msg_permissions",
156 sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
157 return($smarty -> fetch (get_template_path("../setup/setup_step8.tpl")));
158 }
161 /* check if given file is world readable */
162 function is_world_readable($file)
163 {
164 clearstatcache();
165 $p = fileperms($file);
166 $w_r = (decbin($p & 4) == TRUE);
167 return($w_r);
168 }
171 /* Create a backup of the currently existing configuration file.
172 */
173 function create_backup()
174 {
175 $info= posix_getgrgid(posix_getgid());
176 $webgroup = $info['name'];
177 if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR.$this->gosa_conf_name)){
178 $src = CONFIG_DIR.$this->gosa_conf_name;
179 $dst = CONFIG_DIR.$this->gosa_conf_name."_".date("Ymd");
180 $dst_backup= $dst;
181 $i = 1;
182 while(file_exists($dst)){
183 $dst = $dst_backup."-".$i;
184 $i ++;
185 }
186 if(copy($src,$dst)){
187 $this->last_backup_name = $dst;
188 @chgrp($dst,$webgroup);
189 @chown($dst,"root");
190 @chmod($dst,0640);
191 return(TRUE);
192 }else{
193 return(FALSE);
194 }
195 }else{
196 return(FALSE);
197 }
198 }
201 function save_object()
202 {
203 if(isset($_POST['step8_posted'])){
205 /* Get attributes */
206 foreach($this->attributes as $attr){
207 if(isset($_POST[$attr])){
208 $this->$attr = validate($_POST[$attr]);
209 }
210 }
212 /* Backup toggle */
213 if(isset($_POST['create_backup_visible'])){
214 if(isset($_POST['create_backup'])){
215 $this->create_backup = TRUE;
216 }else{
217 $this->create_backup = FALSE;
218 }
219 }
220 }
221 }
222 }
224 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
225 ?>