PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Alexandr Egorov   Config Editor   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Sample
Class: Config Editor
Manage configuration files stored as PHP scripts
Author: By
Last change: Some cosmetic changes ^_^
Date: 14 years ago
Size: 2,817 bytes


Class file image Download
* A sample code showing usage of ConfigEditor class
* @package ConfigEditor
* @author Alexandr [Amega] Egorov, amego2006(at)
* @version 1.0

define('ROOTDIR', dirname(__FILE__));
define('LIBDIR', ROOTDIR);
define('CONFIG_FILE', ROOTDIR.'/config.php');

define('CE_NEWLINE', "\r\n");
define('CE_WORDWRAP', 20);

// Creating an instance of ConfigEditor
$config = new ConfigEditor();

if (!
file_exists(CONFIG_FILE)) {
    // We do not have the config file yet. So we
    // should create one
    // Adding a variable
    // This will result in:
    // // Timeout ...
    // $cfg_curl_timeout = 10;
    // in the generated config.
    // Note: comments may be multiline.
$config->SetVar('cfg_curl_timeout', 10, 'Timeout in cURL operations');
// Values may be more complex, e.g. arrays
    // This will result in:
    // $cfg_banned_ips = array(0 => '', 1 => '');
$config->SetVar('cfg_banned_ips', array('', ''), 'Banned IPs');
// As you may have guessed, ConfigEditor uses var_export on
    // the values before putting them to config.
    // But sotimes it is not desirable. E.g. I want to add a variable:
    // $cfg_cachedir = ROOTDIR.'/maincache';
    // So, everytime I include the config, $cfg_cachedir should be
    // evaluated into 'D:/some/path/maincache';
    // To tell ConfigEditor NOT to var_export values, you should
    // pass true in the 4th argument, telling that the value is
    // in RAW format.
$config->SetVar('cfg_cachedir', "ROOTDIR.'/maincache'", 'ROOTDIR is defined before including config file', true);
// Save generates the config and returns it as a string.
    // If fname is passed, it will alse try to save the file
$config_php = $config->Save(CONFIG_FILE);
'Config created in '.CONFIG_FILE."<br />\n";
} else {
// we already have a config
// Listing all variables found in the config
echo '<table border=1><tr><td>Name</td><td>Value</td><td>RAW value</td><td>Comment</td></tr>';
$vars = $config->GetVarNames();
$vars as $var_name) {
// Note the defference between values for cfg_cachedir
$var = $config->GetVar($var_name);
$varRaw = $config->GetVarRaw($var_name);
'<td>'.htmlspecialchars(print_r($var[CE_VARVALUE], true)).'</td>';
'<td>'.htmlspecialchars(print_r($varRaw[CE_VARVALUE], true)).'</td>';