Code

8a296489f5c55e29de9b03e9c06b6fdea9f459ae
[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->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     return("currently not implemented.");
41   }
44   function execute()
45   {
47     $info= posix_getgrgid(posix_getgid());
48     $webgroup = $info['name'];
49       
50     
51     /* Check if there is currently an active gosa.conf 
52      */
53     $exists = file_exists(CONFIG_DIR.$this->gosa_conf_name); 
55     /* Check if existing config file is writeable */
56     if($exists){
57       $writeable = is_writeable(CONFIG_DIR.$this->gosa_conf_name);
58     }else{
59       $writeable = is_writeable(CONFIG_DIR);
60     }
62     /* Downlaod config */
63     if(isset($_POST['getconf'])){
64       header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
65       header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
66       header("Cache-Control: no-cache");
67       header("Pragma: no-cache");
68       header("Cache-Control: post-check=0, pre-check=0");
69       header("Content-type: text/plain");
70       header('Content-Disposition: attachment; filename="gosa.conf"');
71       echo $this->get_conf_data();
72       exit();
73     }
74    
75     
76     /* Try to save configuration */
77     $abort    = FALSE;
78     $err_msg  = "";
79     if(isset($_POST['saveconf'])){
80       if($exists && $this->create_backup){
81         if(!$this->create_backup()){
82           $abort = TRUE;
83           $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.");
84         }
85       }
86     
87       if(!$abort){
88         
89         /* Try to create file handle */
90         $fp = @fopen(CONFIG_DIR.$this->gosa_conf_name, "w");
91     
92         if(!$fp){
93           $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);
94           $abort  =TRUE;
95         }else{
97           $data = $this->get_conf_data();
98           if(!fwrite($fp,strlen($data))){
99             $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);
100             $abort  =TRUE;
101           }else{
102             
103             @chgrp(CONFIG_DIR.$this->gosa_conf_name,$webgroup);
104             @chown(CONFIG_DIR.$this->gosa_conf_name,"root");
105             @chmod(CONFIG_DIR.$this->gosa_conf_name,0640);    
106           }
107         }
108       }
109     } 
112     if($exists && $this->is_world_readable(CONFIG_DIR.$this->gosa_conf_name)){
113       $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.");
114     }
117     $smarty = get_smarty();
118     $smarty->assign("save_requested", isset($_POST['saveconf']));
119     $smarty->assign("err_msg",$err_msg);
120     $smarty->assign("webgroup", $webgroup);
121     $smarty->assign("gosa_conf_name" , $this->gosa_conf_name);
122     $smarty->assign("create_backup" , $this->create_backup);
123     $smarty->assign("CONFIG_DIR",CONFIG_DIR);
124     $smarty->assign("exists",$exists);
125     $smarty->assign("last_backup_name",$this->last_backup_name);
126     $smarty->assign("writeable",$writeable);
127     $smarty->assign("cv",$this->parent->captured_values);
128     $smarty->assign("msg_permissions",
129       sprintf(_("The following file(s), folders(s) must be writeable for the web-user '%s'."),$info['name']));
130     return($smarty -> fetch (get_template_path("../setup/setup_step8.tpl")));
131   }
134   /* check if given file is world readable */
135   function is_world_readable($file)
136   {
137     clearstatcache();
138     $p = fileperms($file);
139     $w_r = (decbin($p & 4) == TRUE);
140     return($w_r);
141   }
144   /* Create a backup of the currently existing configuration file. 
145    */
146   function create_backup()
147   {
148     $info= posix_getgrgid(posix_getgid());
149     $webgroup = $info['name'];
150     if(is_writeable(CONFIG_DIR) && is_writeable(CONFIG_DIR.$this->gosa_conf_name)){
151       $src = CONFIG_DIR.$this->gosa_conf_name;
152       $dst = CONFIG_DIR.$this->gosa_conf_name."_".date("Ymd");
153       $dst_backup= $dst;
154       $i = 1;
155       while(file_exists($dst)){
156         $dst = $dst_backup."-".$i;
157         $i ++;
158       }
159       if(copy($src,$dst)){
160         $this->last_backup_name = $dst;
161         @chgrp($dst,$webgroup);
162         @chown($dst,"root");
163         @chmod($dst,0640);    
164         return(TRUE);
165       }else{
166         return(FALSE);
167       }
168     }else{
169       return(FALSE);
170     }
171   }
174   function save_object()
175   {
176     if(isset($_POST['step8_posted'])){
178       /* Get attributes */
179       foreach($this->attributes as $attr){
180         if(isset($_POST[$attr])){
181           $this->$attr = validate($_POST[$attr]);
182         }
183       }
185       /* Backup toggle */
186       if(isset($_POST['create_backup_visible'])){
187         if(isset($_POST['create_backup'])){
188           $this->create_backup = TRUE;
189         }else{
190           $this->create_backup = FALSE;
191         }
192       }
193     }
194   }
197 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
198 ?>