home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / PEAR / Config.php < prev    next >
PHP Script  |  2001-11-13  |  7KB  |  222 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Config.php,v 1.4.2.1 2001/11/13 01:26:48 ssb Exp $
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. /**
  25.  * This is a class for storing simple configuration values keeping
  26.  * track of which are system-defined (defaulted) and which are
  27.  * user-defined.  By default, only user-defined settings are stored
  28.  * back to the user's configuration file.
  29.  *
  30.  * Configuration member is a simple associative array.  Used keys:
  31.  *
  32.  *  master_server      which server to query for mirror lists etc.
  33.  *  server             which server/mirror we're currently using
  34.  *  username           PEAR username
  35.  *  password           PEAR password (stored base64-encoded)
  36.  *  php_dir            Where to install .php files
  37.  *  ext_dir            Directory to install compiled libs in
  38.  *  doc_dir            Directory to install documentation in
  39.  *
  40.  */
  41. class PEAR_Config extends PEAR
  42. {
  43.     // {{{ properties
  44.  
  45.     var $defaults_file = '';
  46.  
  47.     var $config_file = '';
  48.  
  49.     var $configuration = array();
  50.  
  51.     var $defaulted = array();
  52.  
  53.     // }}}
  54.  
  55.     // {{{ PEAR_Config([file], [defaults_file])
  56.  
  57.     function PEAR_Config($file = '', $defaults_file = '')
  58.     {
  59.         $this->PEAR();
  60.         $this->config_file = $file;
  61.         $this->defaults_file = $defaults_file;
  62.         if ($file && file_exists($file)) {
  63.             $this->readConfigFile($file);
  64.         }
  65.         if ($defaults_file && file_exists($defaults_file)) {
  66.             $this->mergeConfigFile($defaults_file, false, true);
  67.         }
  68.     }
  69.  
  70.     // }}}
  71.  
  72.     // {{{ readConfigFile([file], [defaults])
  73.  
  74.     function readConfigFile($file = null, $defaults = false)
  75.     {
  76.         if ($file === null) {
  77.             $file = $this->config_file;
  78.         }
  79.         $fp = @fopen($file, "r");
  80.         if (!$fp) {
  81.             return $this->raiseError($php_errormsg);
  82.         }
  83.         $size = filesize($file);
  84.         $contents = fread($fp, $size);
  85.         $data = unserialize($contents);
  86.         if ($data === false && $size > 1) {
  87.             return $this->raiseError("PEAR_Config::readConfigFile: bad data");
  88.         }
  89.         $this->configuration = $data;
  90.         if ($defaults) {
  91.             foreach ($data as $key => $value) {
  92.                 $this->defaulted[$key] = true;
  93.             }
  94.         }
  95.     }
  96.  
  97.     // }}}
  98.     // {{{ mergeConfigFile(file, [override], [defaults])
  99.  
  100.     function mergeConfigFile($file, $override = true, $defaults = false)
  101.     {
  102.         $fp = @fopen($file, "r");
  103.         if (!$fp) {
  104.             return $this->raiseError($php_errormsg);
  105.         }
  106.         $contents = fread($fp, filesize($file));
  107.         $data = unserialize($contents);
  108.         if ($data === false) {
  109.             return $this->raiseError("PEAR_Config::mergeConfigFile: bad data");
  110.         }
  111.         foreach ($data as $key => $value) {
  112.             if (isset($this->configuration[$key]) && !$override) {
  113.                 continue;
  114.             }
  115.             if ($defaults) {
  116.                 $this->defaulted[$key] = true;
  117.             }
  118.             $this->configuration[$key] = $value;
  119.         }
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ writeConfigFile([file], [what_keys])
  124.  
  125.     function writeConfigFile($file = null, $what_keys = 'userdefined')
  126.     {
  127.         if ($what_keys == 'both') {
  128.             $this->writeConfigFile($file, 'userdefined');
  129.             $this->writeConfigFile($file, 'default');
  130.             return;
  131.         }
  132.         if ($file === null) {
  133.             if ($what_keys == 'default') {
  134.                 $file = $this->defaults_file;
  135.             } else {
  136.                 $file = $this->config_file;
  137.             }
  138.         }
  139.         if ($what_keys == 'default') {
  140.             $keys_to_store = array_intersect(array_keys($this->configuration),
  141.                                              array_keys($this->defaulted));
  142.         } elseif ($what_keys == 'all') {
  143.             $keys_to_store = array_keys($this->configuration);
  144.         } else { // user-defined keys
  145.             $keys_to_store = array_diff(array_keys($this->configuration),
  146.                                         array_keys($this->defaulted));
  147.         }
  148.         $data = array();
  149.         foreach ($keys_to_store as $key) {
  150.             $data[$key] = $this->configuration[$key];
  151.         }
  152.         $fp = @fopen($file, "w");
  153.         if (!$fp) {
  154.             return $this->raiseError("PEAR_Config::writeConfigFile fopen('$file','w') failed");
  155.         }
  156.         if (!@fwrite($fp, serialize($data))) {
  157.             return $this->raiseError("PEAR_Config::writeConfigFile serialize failed");
  158.         }
  159.         return true;
  160.     }
  161.  
  162.     // }}}
  163.     // {{{ get(key)
  164.  
  165.     function get($key)
  166.     {
  167.         return @$this->configuration[$key];
  168.     }
  169.  
  170.     // }}}
  171.     // {{{ set(key, value, [default])
  172.  
  173.     function set($key, $value, $default = false)
  174.     {
  175.         $this->configuration[$key] = $value;
  176.         if ($default) {
  177.             $this->defaulted[$key] = true;
  178.         } elseif (isset($this->defaulted[$key])) {
  179.             unset($this->defaulted[$key]);
  180.         }
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ getKeys()
  185.  
  186.     function getKeys()
  187.     {
  188.         return array_keys($this->configuration);
  189.     }
  190.  
  191.     // }}}
  192.     // {{{ toDefault(key)
  193.  
  194.     function toDefault($key)
  195.     {
  196.         if (file_exists($this->defaults_file)) {
  197.             // re-reads the defaults file each time, but hey it works
  198.             unset($this->configuration[$key]);
  199.             $this->mergeConfigFile($this->defaults_file, false, true);
  200.         }
  201.     }
  202.  
  203.     // }}}
  204.     // {{{ isDefaulted(key)
  205.  
  206.     function isDefaulted($key)
  207.     {
  208.         return isset($this->defaulted[$key]);
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ isDefined(key)
  213.  
  214.     function isDefined($key)
  215.     {
  216.         return isset($this->configuration[$key]);
  217.     }
  218.  
  219.     // }}}
  220. }
  221.  
  222. ?>