summaryrefslogtreecommitdiffstats
path: root/libglue/config.php
blob: c1ca07a87ca613507f4b63a987282632c0f1ea22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php

function read_config($config_file, $debug = 0) {
    $fp = fopen($config_file,'r');
    if(!is_resource($fp)) die("Can't open config file $config_file");
    $file_data = fread($fp,filesize($config_file));
    fclose($fp);

    // explode the var by \n's
    $data = explode("\n",$file_data);
    if($debug) echo "<pre>";

    $count = 0;
    $config_name = '';
    foreach($data as $value)
    {
        $count++;
        if (preg_match("/^\[([A-Za-z]+)\]$/",$value,$matches))
        {
            // If we have previous data put it into $results...
            if (!empty($config_name) && count(${$config_name})) $results[$config_name] = ${$config_name};
            $config_name = $matches[1];
        } // if it is a [section] name

        elseif ($config_name)
        {
            // if it's not a comment
            if (preg_match("/^(\w[\w\d]*)\s*=\s*\"{1}(.*?)\"{1};*$/",$value,$matches)
                || preg_match("/^(\w[\w\d]*)\s*=\s*\'{1}(.*?)\'{1};*$/", $value, $matches)
                || preg_match("/^(\w[\w\d]*)\s*=\s*[\'\"]{0}(.*)[\'\"]{0};*$/",$value,$matches))
            {
                if (isset(${$config_name}[$matches[1]]) && is_array(${$config_name}[$matches[1]]) && isset($matches[2]) )
                {
                    if($debug)
                        echo "Adding value <strong>$matches[2]</strong> to existing key <strong>$matches[1]</strong>\n";
                    array_push(${$config_name}[$matches[1]], $matches[2]);
                }
                elseif (isset(${$config_name}[$matches[1]]) && isset($matches[2]) )
                {
                    if($debug)
                        echo "Adding value <strong>$matches[2]</strong> to existing key $matches[1]</strong>\n";
                    ${$config_name}[$matches[1]] = array(${$config_name}[$matches[1]],$matches[2]);
                }
                elseif ($matches[2] !== "")
                {
                    if($debug)
                        echo "Adding value <strong>$matches[2]</strong> for key <strong>$matches[1]</strong>\n";
                    ${$config_name}[$matches[1]] = $matches[2];
                }

                // if there is something there and it's not a comment
                elseif ($value{0} !== "#" AND strlen(trim($value)) > 0)
                {
                    echo "Error Invalid Config Entry --> Line:$count"; die;
                } // else if it's not a comment and there is something there

                else
                {
                    if($debug)
                        echo "Key <strong>$matches[1]</strong> defined, but no value set\n";
                }
            } // end if it's not a comment

        } // else if no config_name


        elseif (preg_match("/^([\w\d]+)\s+=\s+[\"]{1}(.*?)[\"]{1}$/",$value,$matches)
                            || preg_match("/^([\w\d]+)\s+=\s+[\']{1}(.*?)[\']{1}$/", $value, $matches)
                            || preg_match("/^([\w\d]+)\s+=\s+[\'\"]{0}(.*)[\'\"]{0}$/",$value,$matches))
        {
            if (is_array($results[$matches[1]]) && isset($matches[2]) )
            {
                if($debug)
                    echo "Adding value <strong>$matches[2]</strong> to existing key <strong>$matches[1]</strong>\n";
                array_push($results[$matches[1]], $matches[2]);
            }
            elseif (isset($results[$matches[1]]) && isset($matches[2]) )
            {
                if($debug)
                    echo "Adding value <strong>$matches[2]</strong> to existing key $matches[1]</strong>\n";
                $results[$matches[1]] = array($results[$matches[1]],$matches[2]);
            }
            elseif ($matches[2] !== "")
            {
                if($debug)
                    echo "Adding value <strong>$matches[2]</strong> for key <strong>$matches[1]</strong>\n";
                $results[$matches[1]] = $matches[2];
            }

            // if there is something there and it's not a comment
            elseif ($value{0} !== "#" AND strlen(trim($value)) > 0)
            {
                echo "Error Invalid Config Entry --> Line:$count"; die;
            } // else if it's not a comment and there is something there

            else
            {
                if($debug)
                    echo "Key <strong>$matches[1]</strong> defined, but no value set\n";
            }

        } // end else

    } // foreach

    if (count(${$config_name}))
    {
        $results[$config_name] = ${$config_name};
    }

    if($debug) echo "</pre>";

    return $results;

} // end read_config

function libglue_param($param,$clobber=0)
{
	static $params = array();
	if(is_array($param))
	//meaning we are setting values
	{
		foreach ($param as $key=>$val)
		{
			if(!$clobber && isset($params[$key]))
			{
				echo "Error: attempting to clobber $key = $val\n";
				exit();
			}
			$params[$key] = $val;
		}
		return true;
	}
	else
	//meaning we are trying to retrieve a parameter
	{
		if(isset($params[$param])) return $params[$param];
		else return false;
	}
}

function conf($param,$clobber=0)
{
	static $params = array();
	if(is_array($param))
	//meaning we are setting values
	{
		foreach ($param as $key=>$val)
		{
			if(!$clobber && isset($params[$key]))
			{
				echo "Error: attempting to clobber $key = $val\n";
				exit();
			}
			$params[$key] = $val;
		}
		return true;
	}
	else
	//meaning we are trying to retrieve a parameter
	{
		if(isset($params[$param])) return $params[$param];
		else return false;
	}
}

function dbh($str='')
{
    if($str !== '') $dbh = libglue_param(libglue_param($str));
    else $dbh = libglue_param(libglue_param('dbh'));
    if(!is_resource($dbh)) die("Bad database handle: $dbh");
    else return $dbh;
}