/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
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
<?php

class OPTIONS {
 var $props;		// const, modification is not allowed
 var $options;

 function __construct(&$req = NULL, &$override = NULL) {
    global $OPTIONS;

    if ($req instanceof REQUEST) $props = &$req->props;
    else $props = &$req;

    $this->props = &$props;

    $this->options = array();
    if ($override) $this->options[] = &$override;

    if (($req)&&(isset($props['db_server']))) {
	$src = array($props['db_server']);
	if (isset($props['db_name'])) { 
	    $src[] = $props['db_name'];
	    if (isset($props['db_group'])) {
		$src[] = $props['db_group'];
	    }
	}

	if (!($req instanceof SERVERRequest))
	    $req = new SERVERRequest($props);

	$server = $req->GetServerInfo(REQUEST::NEED_INFO);
	if (is_string($server['multibase'])) $multibase = $server['multibase'];
	else if ($server['multibase']) $multibase = ".*";
	else $multibase = false;

	$opts = array(array(), array(), array());
	foreach (array_keys($OPTIONS) as $regex) {
	    $src_regex = explode("__", $regex, 3);

		// Handle multibase sources automatically
	    if (($multibase)&&(sizeof($src_regex) > 1)) {
		$src_regex[1] .= $multibase;
	    }

		// option is two specific for current request
	    if (sizeof($src_regex) > sizeof($src)) continue;

		// check if all parts of regex match
	    for ($i = 0; $i < sizeof($src_regex); $i++) {
		//echo "checking $i (of ".sizeof($src_regex)."): {$src_regex[$i]}\n";
		if (!preg_match("/^{$src_regex[$i]}$/", $src[$i])) break;
	    }
	    if ($i < sizeof($src_regex)) continue;

		// update options, the later defined options override earlier defined ones
	    $pos = sizeof($src_regex) - 1;
	    $opts[$pos] = array_merge($opts[$pos], $OPTIONS[$regex]);
	}

	    // Putting in reverse order to give proper priorities
	for ($i = 0; $i < 3; $i++) {
	    if ($opts[2 - $i]) $this->options[] = &$opts[2 - $i];
	}
    }

    if (isset($OPTIONS['default'])) $this->options[] = &$OPTIONS['default'];
 }
 
 function Get($prop, $default=NULL) {
    foreach ($this->options as &$opts) {
	if (isset($opts[$prop])) return $opts[$prop];
    }
    return $default;
 }


 function GetDateLimit($default_from = false, $default_to = false) {
    $date_limit = $this->Get('date_limit');
    if ($date_limit) {
	if (is_array($date_limit)) {
	    if (is_int($date_limit[0])) $start_date = $date_limit[0];
	    else $start_date = ADEI::ParseDate($date_limit[0]);
	    if (is_int($date_limit[1])) $end_date = $date_limit[1];
	    else $end_date = ADEI::ParseDate($date_limit[1]);
	} else {
	    if (is_int($date_limit)) $start_date = $date_limit;
	    else $start_date = ADEI::ParseDate($date_limit);
	    $end_date = $default_to;
	}
    } else {
	if (is_string($default_from))
	    $start_date = ADEI::ParseDate($default_from);
	else 
	    $start_date = $default_from;
	$end_date = $default_to;
    }

    return array($start_date, $end_date);
 }

 static function GetSpecific($prop, $default=NULL, $db_server = false, $db_name = false, $db_group = false) {
    global $OPTIONS;
    
    if ($db_server !== false) {
	$path = $db_server;
	if ($db_name !== false) {
	    $path .= "__$db_name";
	    if ($db_group !== false) {
		$path .= "__$db_group";
	    }
	}
    } else {
	$path = "default";
    }
    
    if (isset($OPTIONS[$path][$prop])) return $OPTIONS[$path][$prop];

    return $default;
 }
 
 function ListConfiguredDatabases() {
    global $OPTIONS;

    if ((!$this->props)||(!isset($this->props['db_server']))) {
	throw new ADEIException(translate("The server should be specified in order to list configured databases"));
    }
 
    $list = array();

    $name = $this->props['db_server'] . "__";
    $namelen = strlen($name);

    foreach (array_keys($OPTIONS) as $regex) {
	if (!strncmp($regex, $name, $namelen)) {
	    array_push($list, substr($regex, $namelen));
	}
    }

    return array_unique($list);
 }

 function ListConfiguredGroups() {
    global $OPTIONS;

    if ((!$this->props)||(!isset($this->props['db_server']))||(!isset($this->props['db_server']))) {
	throw new ADEIException(translate("The server and database should be specified in order to list configured groups"));
    }
 
    $list = array();

    $name = $this->props['db_server'] . "__" . $this->props['db_name'] . "__";
    $namelen = strlen($name);

    foreach (array_keys($OPTIONS) as $regex) {
	if (!strncmp($regex, $name, $namelen)) {
	    $dbgr = substr($regex, $namelen);
	    list($db) = preg_split("/__/", $dbgr, 2);
	    array_push($list, $db);
	}
    }

    return $list;
 }
}


?>