31ff4e97e1fac5bd12cabba7357962617299a5ba
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 echo $this->get_conf_data();
83 exit();
84 }
87 /* Try to save configuration */
88 $abort = FALSE;
89 $err_msg = "";
90 if(isset($_POST['saveconf'])){
91 if($exists && $this->create_backup){
92 if(!$this->create_backup()){
93 $abort = TRUE;
94 $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.");
95 }
96 }
98 if(!$abort){
100 /* Try to create file handle */
101 $fp = @fopen(CONFIG_DIR.$this->gosa_conf_name, "w");
103 if(!$fp){
104 $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);
105 $abort =TRUE;
106 }else{
108 $data = $this->get_conf_data();
109 if(!fwrite($fp,$data)){
110 $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);
111 $abort =TRUE;
112 }else{
114 @chgrp(CONFIG_DIR.$this->gosa_conf_name,$webgroup);
115 @chown(CONFIG_DIR.$this->gosa_conf_name,"root");
116 @chmod(CONFIG_DIR.$this->gosa_conf_name,0640);
117 }
118 }
119 }
120 }
123 if($exists && $this->is_world_readable(CONFIG_DIR.$this->gosa_conf_name)){
124 $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.");
125 }
128 $smarty = get_smarty();
129 $smarty->assign("save_requested", isset($_POST['saveconf']));
130 $smarty->assign("err_msg",$err_msg);
131 $smarty->assign("webgroup", $webgroup);
132 $smarty->assign("gosa_conf_name" , $this->gosa_conf_name);
133 $smarty->assign("create_backup" , $this->create_backup);
134 $smarty->assign("CONFIG_DIR",CONFIG_DIR);
135 $smarty->assign("exists",$exists);
136 $smarty->assign("last_backup_name",$this->last_backup_name);
137 $smarty->assign("writeable",$writeable);
138 $smarty->assign("cv",$this->parent->captured_values);
139 $smarty->assign("msg_permissions",
140 sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
141 return($smarty -> fetch (get_template_path("../setup/setup_step8.tpl")));
142 }
145 /* check if given file is world readable */
146 function is_world_readable($file)
147 {
148 clearstatcache();
149 $p = fileperms($file);
150 $w_r = (decbin($p & 4) == TRUE);
151 return($w_r);
152 }
155 /* Create a backup of the currently existing configuration file.
156 */
157 function create_backup()
158 {
159 $info= posix_getgrgid(posix_getgid());
160 $webgroup = $info['name'];
161 if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR.$this->gosa_conf_name)){
162 $src = CONFIG_DIR.$this->gosa_conf_name;
163 $dst = CONFIG_DIR.$this->gosa_conf_name."_".date("Ymd");
164 $dst_backup= $dst;
165 $i = 1;
166 while(file_exists($dst)){
167 $dst = $dst_backup."-".$i;
168 $i ++;
169 }
170 if(copy($src,$dst)){
171 $this->last_backup_name = $dst;
172 @chgrp($dst,$webgroup);
173 @chown($dst,"root");
174 @chmod($dst,0640);
175 return(TRUE);
176 }else{
177 return(FALSE);
178 }
179 }else{
180 return(FALSE);
181 }
182 }
185 function save_object()
186 {
187 if(isset($_POST['step8_posted'])){
189 /* Get attributes */
190 foreach($this->attributes as $attr){
191 if(isset($_POST[$attr])){
192 $this->$attr = validate($_POST[$attr]);
193 }
194 }
196 /* Backup toggle */
197 if(isset($_POST['create_backup_visible'])){
198 if(isset($_POST['create_backup'])){
199 $this->create_backup = TRUE;
200 }else{
201 $this->create_backup = FALSE;
202 }
203 }
204 }
205 }
206 }
208 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
209 ?>