Overview

Namespaces

  • Liberty
  • None

Classes

  • Liberty\BigInteger
  • Liberty\Block
  • Liberty\Blockchain
  • Liberty\Collection
  • Liberty\CollectionFileList
  • Liberty\File
  • Liberty\Folder
  • Liberty\INI
  • Liberty\LECDSA
  • Liberty\Onecrypt
  • Liberty\SSV
  • Liberty\Text
  • Liberty\Transaction
  • Liberty\Wallet
  • Liberty\WebPeer

Functions

  • __autoload
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Liberty;
  4: 
  5: 
  6: /**
  7:  * INI Class, A static class to manage INI files.
  8:  *
  9:  * ```
 10:  * Read file : $ini = INI::read('myfile.ini');
 11:  * ```
 12:  * ```
 13:  * Write file : INI::write('myfile.ini', $ini);
 14:  * ```
 15:  *
 16:  * Features:
 17:  * 
 18:  * - support [] syntax for arrays
 19:  * - support . in keys like bar.foo.something = value
 20:  * - true and false string are automatically converted in booleans
 21:  * - integers strings are automatically converted in integers
 22:  * - keys are sorted when writing
 23:  * - constants are replaced but they should be written in the ini file between braces : {MYCONSTANT}
 24:  *
 25:  * @category  Cryptocurrency
 26:  * @package   Liberty
 27:  * @license   https://en.wikipedia.org/wiki/Public_domain Public Domain.
 28:  * @version   1.0.0
 29:  * @since     2007-10-03
 30:  * @author    arnapou
 31:  */
 32: 
 33: 
 34: class INI {
 35: 
 36: 
 37:      /**
 38:       *  Read a INI configuration file.
 39:       */
 40:      public static function read($filename) {
 41:          $ini = array();
 42:          $lines = file($filename);
 43:          $section = 'default';
 44:          $multi = '';
 45:          foreach($lines as $line) {
 46:              if (substr($line, 0, 1) !== ';') {
 47:                  $line = str_replace("\r", "", str_replace("\n", "", $line));
 48:                  if (preg_match('/^\[(.*)\]/', $line, $m)) {
 49:                      $section = $m[1];
 50:                  } else if ($multi === '' && preg_match('/^([a-z0-9_.\[\]-]+)\s*=\s*(.*)$/i', $line, $m)) {
 51:                      $key = $m[1];
 52:                      $val = $m[2];
 53:                      if (substr($val, -1) !== "\\") {
 54:                          $val = trim($val);
 55:                          INI::manage_keys($ini[$section], $key, $val);
 56:                          $multi = '';
 57:                      } else {
 58:                          $multi = substr($val, 0, -1)."\n";
 59:                      }
 60:                  } else if ($multi !== '') {
 61:                      if (substr($line, -1) === "\\") {
 62:                          $multi .= substr($line, 0, -1)."\n";
 63:                      } else {
 64:                          INI::manage_keys($ini[$section], $key, $multi.$line);
 65:                          $multi = '';
 66:                      }
 67:                  }
 68:              }
 69:          }
 70:          
 71:          $buf = get_defined_constants(true);
 72:          $consts = array();
 73:         /*
 74:          foreach($buf['user'] as $key => $val) {
 75:              $consts['{'.$key.'}'] = $val;
 76:          }
 77:         
 78:          array_walk_recursive($ini, array('INI', 'replace_consts'), $consts);
 79:          */
 80:          return $ini;
 81:      }
 82: 
 83: 
 84: 
 85:      /**
 86:       *  Write a INI configuration file.
 87:       */
 88:      public static function write($filename, $ini) {
 89:          $string = '';
 90:          foreach(array_keys($ini) as $key) {
 91:              $string .= '['.$key."]\n";
 92:              $string .= INI::write_get_string($ini[$key], '')."\n";
 93:          }
 94:          file_put_contents($filename, $string);
 95:      }
 96: 
 97: 
 98: 
 99:      /**
100:       *  write get string
101:       */
102:      private static function write_get_string(& $ini, $prefix) {
103:          $string = '';
104:          ksort($ini);
105:          foreach($ini as $key => $val) {
106:              if (is_array($val)) {
107:                  $string .= INI::write_get_string($ini[$key], $prefix.$key.'.');
108:              } else {
109:                  $string .= $prefix.$key.' = '.str_replace("\n", "\\\n", INI::set_value($val))."\n";
110:              }
111:          }
112:          return $string;
113:      }
114: 
115: 
116:      /**
117:       *  manage keys
118:       */
119:      private static function set_value($val) {
120:          if ($val === true) { return 'true'; }
121:          else if ($val === false) { return 'false'; }
122:          return $val;
123:      }
124: 
125: 
126: 
127:      /**
128:       *  manage keys
129:       */
130:      private static function get_value($val) {
131:          if (preg_match('/^-?[0-9]$/i', $val)) { return intval($val); } 
132:          else if (strtolower($val) === 'true') { return true; }
133:          else if (strtolower($val) === 'false') { return false; }
134:          else if (preg_match('/^"(.*)"$/i', $val, $m)) { return $m[1]; }
135:          else if (preg_match('/^\'(.*)\'$/i', $val, $m)) { return $m[1]; }
136:          return $val;
137:      }
138: 
139: 
140:      /**
141:       *  manage keys
142:       */
143:      private static function get_key($val) {
144:          if (preg_match('/^[0-9]$/i', $val)) { return intval($val); }
145:          return $val;
146:      }
147: 
148: 
149:      /**
150:       *  manage keys
151:       */
152:      private static function manage_keys(& $ini, $key, $val) {
153:          if (preg_match('/^([a-z0-9_-]+)\.(.*)$/i', $key, $m)) {
154:              INI::manage_keys($ini[$m[1]], $m[2], $val);
155:          } else if (preg_match('/^([a-z0-9_-]+)\[(.*)\]$/i', $key, $m)) {
156:              if ($m[2] !== '') {
157:                  $ini[$m[1]][INI::get_key($m[2])] = INI::get_value($val);
158:              } else {
159:                  $ini[$m[1]][] = INI::get_value($val);
160:              }
161:          } else {
162:              $ini[INI::get_key($key)] = INI::get_value($val);
163:          }
164:      }
165: 
166: 
167:      /**
168:       *  replace utility
169:       */
170:      private static function replace_consts(& $item, $key, $consts) {
171:          if (is_string($item)) {
172:              $item = strtr($item, $consts);
173:          }
174:      }
175:  }
176: 
177: ?>
API documentation generated by ApiGen