3 * GForge Config File edit page
8 * Copyright 2005 GForge, LLC
11 * Daniel A. PĂ©rez danielperez.arg@gmail.com
13 * This file is part of GForge.
15 * GForge is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * GForge is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with GForge; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 require_once('../env.inc.php');
32 require_once $gfwww.'include/pre.php';
33 require_once $gfwww.'admin/admin_utils.php';
35 site_admin_header(array('title'=>_('Site admin')));
37 $gfcgfile = get_absolute_filename($gfcgfile);
40 * printSelection - prints the select box for the user to get the files to edit
42 * @param string the checked item (if any)
43 * @param string the path to the plugins conf dir
45 function printSelection($checked,$pluginpath) {
46 global $feedback, $gfcgfile;
48 $config_files = array(); // array that'll have the config files
50 if (strlen($pluginpath)>=1){
51 if ($pluginpath[strlen($pluginpath)-1]!='/') {
56 // check if we can get local.inc
57 @$handle = fopen($gfcgfile,'r+');
59 // Open readonly but tell you can't write
60 $handle = fopen($gfcgfile,'r');
61 $feedback .= sprintf(_('Could not open %s file for read/write. Check the permissions for apache.'), $gfcgfile).'<br />';
64 $config_files['local.inc'] = $gfcgfile;
67 // say we couldn't open local.inc
68 $feedback .= sprintf(_('Could not open %s for read. Check the permissions for apache.'), $gfcgfile).'<br />';
71 //get the directories from the plugins dir
72 /*if (chdir($pluginpath)) {
73 $handle = opendir('.');
75 while ($filename = readdir($handle)) {
76 //Don't add special directories '..' or '.' to the list
77 if (($filename!='..') && ($filename!='.') && ($filename!="CVS") ) {
78 $handle2 = @opendir($filename); // open the etc dir of the plugin
80 while ($filename2 = readdir($handle2)) {
81 if (strstr($filename2,'.conf') || strstr($filename2,'.inc') || ($filename2=='config.php')) {
82 $config_files['(' . $filename . ') - ' . $filename2] = $pluginpath . $filename . '/' . $filename2;
91 // say we couldn't get into etc plugins dir
92 $feedback .= _('Could not open plugins etc dir for reading. Check the permissions for apache<br>');
95 echo '<br /><div align="center">';
96 echo html_build_select_box_from_assoc($config_files,'files',$checked,true);
97 echo ' <input type="submit" name="choose" value="' . _('Choose') .'"/>';
102 * getVars - gets the contents of the file and returns an associative array of field names / values
104 * @param string the contents of the file
105 * @return array the results
107 function getVars($filedata) {
109 $lines = explode("\n",$filedata);
111 foreach ($lines as $line) {
112 if ( substr(trim($line),0,1) == "$" ) {
113 $sep_var = explode("=",$line,2);
114 $sep_var[0] = trim($sep_var[0]);
115 $sep_var[1] = substr(trim($sep_var[1]),0,strlen(trim($sep_var[1]))-1);
116 $results[$sep_var[0]] = $sep_var[1];
123 * updateVars - updates the values of the vars of the file passed as an argument with the values of the array passed
125 * @param unknown_type $vars
126 * @param unknown_type $filepath
128 function updateVars($vars,$filepath) {
131 $filedata = file_get_contents($filepath);
132 $lines = explode("\n",$filedata);
133 $keys = array_keys($vars);
134 for($i=0;$i<(count($vars));$i++) {
135 $vars[$keys[$i]] = str_replace('\"','"',$vars[$keys[$i]]);
136 $vars[$keys[$i]] = str_replace("\'","'",$vars[$keys[$i]]);
137 $currline = $keys[$i] . "=" . $vars[$keys[$i]] . ";";
138 //$filedata = preg_replace('/(.*)(' . $keys[$i] . ')([^;]*);(.*)/','/\1\2='.$vars[$keys[$i]].';\n\4/',$filedata);
139 for ($j=0;$j<count($lines);$j++) {
140 $mykey = explode("=",$lines[$j]);
141 $mykey = trim($mykey[0]);
142 if ($mykey == $keys[$i]) {
143 $lines[$j] = $currline;
147 $filedata = implode("\n",$lines);
148 if (@$handle = fopen($filepath,'w')) {
149 if (fwrite($handle,$filedata)) {
151 $feedback .= sprintf(_('File %s wrote successfully.'), $filepath).'<br />';
154 $feedback .= sprintf(_('File %s wasn\'t written or is empty.'), $filepath).'<br />';
158 $feedback .= sprintf(_("Could not open %s for write. Check the permissions for apache."), $filepath).'<br />';
165 <form name="theform" action="<?php echo getStringFromServer('PHP_SELF'); ?>" method="post">
167 <!--<?php //echo _('Path were you store the plugin configuration folders. E.g. : /etc/gforge/plugins/'); ?>
168 <input type="text" size="55" width="55" name="pluginpath" value="<?php //echo getStringFromRequest('pluginpath')?>"/>
169 <input type="submit" name="changepath" value="<?php //echo _('Change'); ?>"/>
173 //if (getStringFromRequest('pluginpath')) {
175 // printSelection(getStringFromRequest('files'),getStringFromRequest('pluginpath'));
177 if (!getStringFromRequest('doedit')) {
179 $filepath = $gfcgfile;
181 @$handle = fopen($filepath,'r+');
183 // Open readonly but tell you can't write
184 $handle = fopen($filepath,'r');
186 $feedback .= sprintf(_("Could not open %s file for read/write. Check the permissions for apache."), $filepath).'<br />';
189 fclose($handle); // we had to open it in r+ because we need to check we'll be able to save it later
190 $filedata = file_get_contents($filepath);
191 $vars = getVars($filedata); // get the vars from local.inc
192 $keys = array_keys($vars);
194 echo '<h2>Configuration file: '. $filepath.'</h2>';
195 $title_arr = array(_('Attribute'),_('On'),_('Off'));
196 echo $HTML->listTableTop($title_arr);
198 for($i=0;$i<(count($keys));$i++) {
199 if ( ($vars[$keys[$i]]=="true") || ($vars[$keys[$i]]=="false") ) {
200 echo '<tr '. $HTML->boxGetAltRowStyle($j+1) .'>'.
201 '<td>'. $keys[$i] .'</td>';
204 ($vars[$keys[$i]]=="true")?$checkedtrue=' checked="checked" ':$checkedfalse=' checked="checked" ';
205 echo '<td style="text-align:center"><input type="radio" name="attributes[' . $keys[$i] . ']" value="true" ' . $checkedtrue . '/>' .'</td>'.
206 '<td style="text-align:center"><input type="radio" name="attributes[' . $keys[$i] . ']" value="false" ' . $checkedfalse . '/></td>';
210 echo $HTML->listTableBottom();
211 $title_arr = array(_('Attribute'),_('Value'));
212 echo $HTML->listTableTop($title_arr);
213 for($i=0;$i<(count($keys));$i++) {
215 if ( (substr($vars[$keys[$i]],0,1)=='"') ||(substr($vars[$keys[$i]],0,1)=="'") ) {
216 echo '<tr '. $HTML->boxGetAltRowStyle($j+1) .'>'.
217 '<td>'. $keys[$i] .'</td>';
218 echo '<td><input type="text" size=80 name="attributes[' . $keys[$i] . ']" value="'. str_replace('"',"'",$vars[$keys[$i]]) .'"></td>';
222 else if ( (ord(substr($vars[$keys[$i]],0,1)) >= 48) && (ord(substr($vars[$keys[$i]],0,1)) <= 57) ) {
223 echo '<tr '. $HTML->boxGetAltRowStyle($j+1) .'>'.
224 '<td>'. $keys[$i] .'</td>';
225 echo '<td><input type="text" size=80 name="attributes[' . $keys[$i] . ']" value="'. trim($vars[$keys[$i]],"'") .'"></td>';
228 // Others => Not supported
230 echo '<tr '. $HTML->boxGetAltRowStyle($j+1) .'>'.
231 '<td>'. $keys[$i] .'</td>';
232 echo '<td>Not supported</td>';
238 // Remove "Not supported" keys from the keys' array
240 for($i=0;$i<(count($array_rm));$i++) {
241 if ( $array_rm[$i] == 1 ) {
242 array_splice($array_rm, $i-$j);
246 echo $HTML->listTableBottom();
247 /*echo '<br><center>' . html_build_rich_textarea('filedata',30,150,$filedata,false) . '</center>';*/
250 echo '<div align="center"><input type="submit" name="doedit" value="' . _('Save') .'"/></div>';
253 // say we couldn't open the file
254 $feedback .= sprintf(_("Could not open %s for read. Check the permissions for apache."), $filepath).'<br />';
256 } elseif (getStringFromRequest('doedit')) {
257 updateVars(getArrayFromRequest('attributes'),$gfcgfile); // perhaps later we'll update something else, for now it's local.inc
258 /*$filedata = getStringFromRequest('filedata');
259 $filedata = str_replace('\"','"',$filedata);
260 $filedata = str_replace("\'","'",$filedata);
261 $filepath = getStringFromRequest('filepath');
262 if ($handle = fopen($filepath,'w')) {
263 if (fwrite($handle,$filedata)) {
265 $feedback .= _('File wrote successfully.<br>');
268 $feedback .= _('File wasn\'t written or is empty.<br>');
272 $feedback .= _('Could not open the file for read/write. Check the permissions for apache<br>');
285 site_admin_footer(array());
287 function get_absolute_filename($filename) {
288 // Check for absolute path
289 if (realpath($filename) == $filename) {
293 // Otherwise, treat as relative path
294 $paths = explode(':', get_include_path());
295 foreach ($paths as $path) {
296 if (substr($path, -1) == '/') {
297 $fullpath = $path.$filename;
299 $fullpath = $path.'/'.$filename;
301 if (file_exists($fullpath)) {
311 // c-file-style: "bsd"