Source for file mvblog_iniparser.php

Documentation is available at mvblog_iniparser.php

  1. <?php
  2. /**
  3.  * MvBlog -- An open source no-nosense blogtool
  4.  *
  5.  * Copyright (C) 2005-2007, Michiel van Baak
  6.  * Michiel van Baak <mvanbaak@users.sourceforge.net>
  7.  *
  8.  * See http://www.mvblog.org for more information on MvBlog.
  9.  * That page also provides Bugtrackers, Filereleases etc.
  10.  *
  11.  * This program is free software, distributed under the terms of
  12.  * the GNU General Public License Version 2. See the LICENSE file
  13.  * at the top of the source tree.
  14.  *
  15.  * @package MvBlog
  16.  * @author Ferry Boender
  17.  * @version %%VERSION%%
  18.  * @copyright 2005-2007 Ferry Boender
  19.  */
  20.  
  21.     const TYPE_STRING 1;
  22.     const TYPE_BOOL 2;
  23.     const TYPE_INT 3;
  24.  
  25.     public static $typeNames array(
  26.         mvblog_IniParser::TYPE_STRING => "String",
  27.         mvblog_IniParser::TYPE_BOOL   => "Boolean",
  28.         mvblog_IniParser::TYPE_INT    => "Integer",
  29.         );
  30.     protected $availSettings = array();
  31.     protected $settings = array();
  32.  
  33.     public function __construct($availSettings{
  34.         $this->availSettings = $availSettings;
  35.         $this->setDefaults($availSettings);
  36.     }
  37.  
  38.     protected function parse($contents{
  39.         $values array();
  40.  
  41.         // Find and replace dos line endings
  42.         $contents str_replace("\r\n""\n"$contents);
  43.         // Find and replace mac line endings
  44.         $contents str_replace("\r""\n"$contents);
  45.         // Set section to nothing by default
  46.         $section "";
  47.  
  48.         $contents explode("\n"$contents);
  49.         for ($i 0$i count($contents)$i++{
  50.             // Look for a section
  51.             if (preg_match('/^\[(.*?)\]*$/'$contents[$i]$match)) {
  52.                 $section $match[1];
  53.             }
  54.  
  55.             if (!preg_match("/^\s*[#;].*$/"$contents[$i]&& // Comment
  56.                 preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/'$contents[$i]$match)) {
  57.                 $key $match[1];
  58.                 $value $match[2];
  59.                 $values[$section][$key$value// FIXME: Strip 
  60.             }
  61.         }
  62.  
  63.         return($values);
  64.     }
  65.  
  66.     protected function setDefaults($availSettings{
  67.         foreach($availSettings as $section => $data)
  68.             foreach ($data as $key => $value)
  69.                 if (array_key_exists("default"$value)) 
  70.                     $this->setSetting($section$key$value["default"]);
  71.     }
  72.  
  73.     protected function setSetting($section$key$value{
  74.         if (!array_key_exists($key$this->availSettings[$section]))
  75.             throw new mvblog_IniParserException(1$key$section);
  76.  
  77.         $type $this->availSettings[$section][$key]["type"];
  78.         switch($type{
  79.             case mvblog_IniParser::TYPE_STRING:
  80.                 $this->settings[$section][$key= (string)$value;
  81.                 break;
  82.             case mvblog_IniParser::TYPE_BOOL:
  83.                 if (strcasecmp($value"on"=== ||
  84.                     strcasecmp($value"true"=== ||
  85.                     strcasecmp($value"yes"=== ||
  86.                     $value == '1'{
  87.                     $this->settings[$section][$keyTrue;
  88.                 else 
  89.                 if (strcasecmp($value"off"=== ||
  90.                     strcasecmp($value"false"=== ||
  91.                     strcasecmp($value"no"=== ||
  92.                     $value == '0'{
  93.                     $this->settings[$section][$keyFalse;
  94.                 else {
  95.                     throw new mvblog_IniParserException(2$value$keymvblog_IniParser::$typeNames[$type]$section);
  96.                 }
  97.                 break;
  98.             case mvblog_IniParser::TYPE_INT:
  99.                 if (is_int($value|| ctype_digit($value))
  100.                     $this->settings[$section][$key= (int)$value;
  101.                 else
  102.                     throw new mvblog_IniParserException(2$value$keymvblog_IniParser::$typeNames[$type]$section);
  103.                 break;
  104.             default:
  105.                 throw new mvblog_IniParserException(3$this->availSettings[$section][$key]);
  106.                 break;
  107.         }
  108.     }
  109.  
  110.     public function getSetting($section$key{
  111.         if (array_key_exists($key$this->settings[$section]))
  112.             return($this->settings[$section][$key]);
  113.         else
  114.             return(null);
  115.     }
  116.  
  117.     public function getSettings($section ""{
  118.         //if section is empty return the complete settings array
  119.         if (!$section)
  120.             return $this->settings;
  121.         else
  122.             if (array_key_exists($section$this->settings))
  123.                 return $this->settings[$section];
  124.             else
  125.                 return null;
  126.     }
  127. }
  128. ?>

Documentation generated on Fri, 28 Dec 2007 13:17:40 +0100 by phpDocumentor 1.4.1