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: 15 years ago
Size: 2,817 bytes
 

Contents

Class file image Download
<?php
/**
* A sample code showing usage of ConfigEditor class
*
* @package ConfigEditor
* @author Alexandr [Amega] Egorov, amego2006(at)gmail.com
* @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);
require_once(
LIBDIR.'/confedit.class.php');

// 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 => '10.10.10.10', 1 => '10.20.30.40');
   
$config->SetVar('cfg_banned_ips', array('10.10.10.10', '10.20.30.40'), '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);
    echo
'Config created in '.CONFIG_FILE."<br />\n";
} else {
   
// we already have a config
   
$config->LoadFromFile(CONFIG_FILE);
   
   
// 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();
    foreach(
$vars as $var_name) {
       
// Note the defference between values for cfg_cachedir
       
$var = $config->GetVar($var_name);
       
$varRaw = $config->GetVarRaw($var_name);
        echo
'<tr><td>'.htmlspecialchars($var[CE_VARNAME]).'</td>';
        echo
'<td>'.htmlspecialchars(print_r($var[CE_VARVALUE], true)).'</td>';
        echo
'<td>'.htmlspecialchars(print_r($varRaw[CE_VARVALUE], true)).'</td>';
        echo
'<td>'.htmlspecialchars($var[CE_VARCOMMENT]).'</td></tr>';
    }
    echo
'</table>';
}

?>