1<?php
2/**
3 * Provides functionality to parse key-value-pairs
4 * from an ini-like string.
5 *
6 * @package C4Masterserver
7 * @version 1.2.0-en
8 * @author  Benedict Etzel <b.etzel@live.de>
9 * @license http://creativecommons.org/licenses/by/3.0/ CC-BY 3.0
10 */
11abstract class ParseINI {
12
13	/**
14     * Normalizes the given text to unix line endings (lf).
15     *
16     * @param string $text
17     * @return string
18     */
19	public static function normalize($text) {
20		$text = str_replace("\r\n", "\n", $text);
21		$text = str_replace("\r", "\n", $text);
22		$text = preg_replace("/\n{2,}/", "\n\n", $text);
23		return $text;
24	}
25
26    /**
27     * Returns the value belonging to the key from an ini-like string.
28     *
29     * @param string $key
30     * @param string $string
31     * @return string
32     */
33    public static function parseValue($key, $string) {
34		if(!$key || !$string) {
35            return false;
36        }
37        $string = ParseINI::normalize($string);
38        if(strpos($string, $key) === false) {
39            return false;
40        }
41		$value = '';
42        $key_start = strpos($string, $key."=") + strlen($key) + 1;
43        $key_end = strpos($string, "\n", $key_start);
44        if($key_end === false) {
45            $value = substr($string, $key_start);
46        }
47        else {
48            $value = substr($string, $key_start, $key_end - $key_start);
49        }
50        $value = trim($value);
51        $value = trim($value, '"');
52        if($value == 'true') $value = 1;
53        if($value == 'false') $value = 0;
54        return $value;
55    }
56
57    /**
58     * Returns all values in a certain category and supports multiple appereances.
59     *
60     * @param string $key
61     * @param string $category
62     * @param string $string
63     * @return array
64     */
65    public static function parseValuesByCategory($key, $category, $string) {
66        if(!$key || !$string || !$category) {
67            return array();
68        }
69		$string = ParseINI::normalize($string);
70        if(strpos($string, $key) === false || strpos($string, '['.$category.']') === false) {
71            return array();
72        }
73        $values = array();
74        $lines = explode("\n", $string);
75        $current_category = '';
76        foreach($lines as $line) {
77            $line = trim($line);
78            if(strpos($line, '[') !== false && strpos($line, ']') !== false && strpos($line, '=') === false) {
79                $start = strpos($line, '[') + 1;
80                $end = strpos($line, ']') - 1;
81                $current_category = substr($line, $start, $end);
82            }
83            if($current_category != $category) continue; //wrong category
84            if(strpos($line, ';') === 0) continue; //comment
85            if(strpos($line, $key) === false) continue; //not needed
86            $values[] = ParseINI::parseValue($key, $line);
87        }
88        return $values;
89    }
90}
91?>
92