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->s_title = _("Configuration file");
33 $this->s_info = _("In this step the configuration file will be created.");
34 $this->s_title_long = _("Saving configuration file");
35 }
38 function get_conf_data()
39 {
40 $smarty = get_smarty();
41 $smarty->assign("cv",$this->parent->captured_values);
42 $str = $smarty->fetch(CONFIG_TEMPLATE_DIR.$this->gosa_conf_name);
43 return($str);
44 }
47 function execute()
48 {
50 $info= posix_getgrgid(posix_getgid());
51 $webgroup = $info['name'];
54 /* Check if there is currently an active gosa.conf
55 */
56 $exists = file_exists(CONFIG_DIR.$this->gosa_conf_name);
58 /* Check if existing config file is writeable */
59 if($exists){
60 $writeable = is_writeable(CONFIG_DIR.$this->gosa_conf_name);
61 }else{
62 $writeable = is_writeable(CONFIG_DIR);
63 }
65 /* Downlaod config */
66 if(isset($_POST['getconf'])){
68 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
69 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
70 header("Cache-Control: no-cache");
71 header("Pragma: no-cache");
72 header("Cache-Control: post-check=0, pre-check=0");
73 header("Content-type: text/plain");
75 if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
76 preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
77 header('Content-Disposition: filename="gosa.conf"');
78 } else {
79 header('Content-Disposition: attachment; filename="gosa.conf"');
80 }
82 $str = $this->get_conf_data();
83 $str = preg_replace("/\n[ ]*\n/","",$str);
85 echo $this->get_conf_data();
86 exit();
87 }
90 /* Try to save configuration */
91 $abort = FALSE;
92 $err_msg = "";
93 if(isset($_POST['saveconf'])){
94 if($exists && $this->create_backup){
95 if(!$this->create_backup()){
96 $abort = TRUE;
97 $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.");
98 }
99 }
101 if(!$abort){
103 /* Try to create file handle */
104 $fp = @fopen(CONFIG_DIR.$this->gosa_conf_name, "w");
106 if(!$fp){
107 $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);
108 $abort =TRUE;
109 }else{
111 $data = $this->get_conf_data();
112 if(!fwrite($fp,$data)){
113 $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);
114 $abort =TRUE;
115 }else{
117 @chgrp(CONFIG_DIR.$this->gosa_conf_name,$webgroup);
118 @chown(CONFIG_DIR.$this->gosa_conf_name,"root");
119 @chmod(CONFIG_DIR.$this->gosa_conf_name,0640);
120 }
121 }
122 }
123 }
126 if($exists && $this->is_world_readable(CONFIG_DIR.$this->gosa_conf_name)){
127 $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.");
128 }
131 $smarty = get_smarty();
132 $smarty->assign("save_requested", isset($_POST['saveconf']));
133 $smarty->assign("err_msg",$err_msg);
134 $smarty->assign("webgroup", $webgroup);
135 $smarty->assign("gosa_conf_name" , $this->gosa_conf_name);
136 $smarty->assign("create_backup" , $this->create_backup);
137 $smarty->assign("CONFIG_DIR",CONFIG_DIR);
138 $smarty->assign("exists",$exists);
139 $smarty->assign("last_backup_name",$this->last_backup_name);
140 $smarty->assign("writeable",$writeable);
141 $smarty->assign("cv",$this->parent->captured_values);
142 $smarty->assign("msg_permissions",
143 sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
144 return($smarty -> fetch (get_template_path("../setup/setup_step8.tpl")));
145 }
148 /* check if given file is world readable */
149 function is_world_readable($file)
150 {
151 clearstatcache();
152 $p = fileperms($file);
153 $w_r = (decbin($p & 4) == TRUE);
154 return($w_r);
155 }
158 /* Create a backup of the currently existing configuration file.
159 */
160 function create_backup()
161 {
162 $info= posix_getgrgid(posix_getgid());
163 $webgroup = $info['name'];
164 if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR.$this->gosa_conf_name)){
165 $src = CONFIG_DIR.$this->gosa_conf_name;
166 $dst = CONFIG_DIR.$this->gosa_conf_name."_".date("Ymd");
167 $dst_backup= $dst;
168 $i = 1;
169 while(file_exists($dst)){
170 $dst = $dst_backup."-".$i;
171 $i ++;
172 }
173 if(copy($src,$dst)){
174 $this->last_backup_name = $dst;
175 @chgrp($dst,$webgroup);
176 @chown($dst,"root");
177 @chmod($dst,0640);
178 return(TRUE);
179 }else{
180 return(FALSE);
181 }
182 }else{
183 return(FALSE);
184 }
185 }
188 function save_object()
189 {
190 if(isset($_POST['step8_posted'])){
192 /* Get attributes */
193 foreach($this->attributes as $attr){
194 if(isset($_POST[$attr])){
195 $this->$attr = validate($_POST[$attr]);
196 }
197 }
199 /* Backup toggle */
200 if(isset($_POST['create_backup_visible'])){
201 if(isset($_POST['create_backup'])){
202 $this->create_backup = TRUE;
203 }else{
204 $this->create_backup = FALSE;
205 }
206 }
207 }
208 }
209 }
211 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
212 ?>