Abend!
Ich bin gerade dabei eine Klasse zu schreiben, die mit Konfigurations-Dateien arbeiten soll. Kurz gesagt sie soll sie lesen und schreiben. Die Ausgabe funktioniert hervorragend, das Problem liegt also beim Schreiben. Da die Klasse natürlich nicht weiß, welche Felder in der Konfigurations-Datei existieren, muss vor jedem Eintrag geprüft werden, ob das Feld existiert. Ist das der Fall soll das Value für dieses Feld geändert werden. Meine Frage ist jetzt, ob es noch unsicher ist, einen Aufruf der Klasse so zu gestalten, oder ob genug Sicherheit durch die Verarbeitung der Daten inerhalb der Methode setConfig gegeben ist.
Wäre euch echt dankbar, wenn ihr da mal euren Senf zu geben würdet. :-)
MfG, Andy
Der Aufruf
PHP-Code:
<?php
// call Config-Class
require_once('includes/Config.php');
// set reply to an array
$config = $_GET[];
// set new configuration
$ih = new Config();
$ih->setConfig($config, 'config/config.ini');
unset($config);
// print out config
$config = getConfig('config/config.ini');
print_r($config);
?>
Config.php
PHP-Code:
<pre>
<?php
error_reporting(E_ALL);
/***
* The Config class allows reading the configuration and editting it
*
* @package Config
* @version 1.0
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class Config
{
/**
* getConfig() - Gets the config-file and stores it in an array
*
* @access: public
* @param Str $path
* @return Array
*/
public function getConfig($path)
{
// array that contains theconfig
$config = array();
// check if file exists and create an absolute path
if (file_exists($path))
{
$path = realpath($path);
}
else
{
throw new Exception('Path not found!');
}
// save each line of data to an array-element
$lines = file($path);
foreach($lines as $line => $value)
{
// save information to array
if( preg_match('/([^:]+)=(.+)/m', $value, $match) )
{
$config[$this->getName($match[1])] = trim($match[2]);
}
}
return $config;
}
/**
* getName() - Returns the name of a received field
*
* @access: private
* @param Str $str
* @return String
*/
private function getName($str)
{
return preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtolower("\0")', strtolower(trim($str)));
}
/**
* setConfig() - Writes the data from config-array into the configurtaion-file
*
* @access: private
* @param Arr $config
* @param Str $path
* @return String
*/
private function setConfig($config, $path)
{
// read configuration file
$file = file_get_contents($path);
// find the config-field and insert new value
foreach( $config as $name => $value)
{
// clean strings from php- and html-code
$name = trim(strip_tags($name));
$value = trim(strip_tags($value));
// search if data set entry exists and assign new value
preg_replace("/($name)=(.+)/m", "$name = $value", $file);
}
// write the new configuration to config-file
$fp = fopen($path, 'w');
fwrite($fp, $file);
fclose($fp);
return true;
}
}
?>
</pre>
config.ini
Code:
# database values
dbhost = your_database_host
db = your_db
dbuser = testuser
dbpwd = testpwd
# meta tags
title = title of your website
author = the author of the website
copyright = copyright by
discription = this is a great website
keywords = website, your, nice
favicon = favicon.ico
# error pages
error400 = Error 400 - Bad request.
error404 = Error 404 - Page could not be found.
error500 = Error 500 - Error occured.