forked from Open-Web-Analytics/Open-Web-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini_db.php
236 lines (196 loc) · 5.25 KB
/
ini_db.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
/**
* INI Database
*
* Searches INI files for matches based on various lookup methods.
*
* @author Peter Adams <[email protected]>
* @copyright Copyright © 2006 Peter Adams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GPL v2.0
* @category wa
* @package wa
* @version $Revision$
* @since wa 1.0.0
*/
class ini_db extends owa_base {
/**
* Data file
*
* @var unknown_type
*/
var $ini_file;
/**
* Result Format
*
* @var string
*/
var $return_format;
/**
* Cache flag
*
* @var boolean
*/
var $cache = true;
/**
* Database Access Object
*
* @var object
*/
var $db;
/**
* Constructor
*
* @param string $ini_file
* @param string_type $sections
* @param string $return_format
* @access public
* @return ini_db
*/
function __construct($ini_file, $sections = null, $return_format = 'object') {
parent::__construct();
$this->ini_file = $ini_file;
$this->return_format = $return_format;
if (!empty($sections)){
$this->db = $this->readINIfile($this->ini_file, ';');
} else {
$this->db = file($this->ini_file);
}
}
/**
* Returns a section from an ini file based on regex match rule
* contained as keys in an ini file.
*
* @param string
* @access public
*/
function fetch($haystack) {
$record = null;
foreach ($this->db as $key=>$value) {
if (($key!='#*#')&&(!array_key_exists('parent',$value))) continue;
$keyEreg = '#'.$key.'#';
if (preg_match($keyEreg, $haystack)) {
$record=array('regex'=>strtolower($keyEreg),'pattern'=>$key)+$value;
$maxDeep=8;
while (array_key_exists('parent',$value)&&(--$maxDeep>0))
$record+=($value = $this->db[strtolower($value['parent'])]);
break;
}
}
switch ($this->return_format) {
case "array":
return $record;
break;
case "object":
return ((object)$record);
break;
}
return $record;
}
/**
* Returns part of the passed string based on regex match rules
* contained as keys in an ini file.
*
* @param string
* @access public
* @return string
*/
function match($haystack) {
$needle = '';
if (!empty($haystack)):
$tmp = '';
foreach ($this->db as $key => $value) {
if (!empty($value)):
//$this->e->debug('ref db:'.print_r($this->db, true));
preg_match(trim($value), $haystack, $tmp);
if (!empty($tmp)):
$needle = $tmp;
//$this->e->debug('ref db:'.print_r($tmp, true));
endif;
endif;
}
return $needle;
else:
return;
endif;
}
function contains($haystack = '') {
$pos = false;
if ($haystack) {
foreach ($this->db as $k => $needle) {
$needle = substr(strtolower(trim($needle)),1,-1);
$pos = strpos(strtolower($haystack), $needle);
if ($pos) {
owa_coreAPI::debug(sprintf('Haystack contains "%s" at position %d', $needle, $pos));
return true;
}
}
return false;
}
}
/**
* Fetch a record set and perfrom a regex replace on the name
*
* @param string $haystack
* @return string
*/
function fetch_replace($haystack) {
$record = $this->fetch($haystack);
//print_r($record);
$new_record = preg_replace($record->regex, $record->name, $haystack);
return $new_record;
}
/**
* Reads INI file
*
* @param string $filename
* @param string $commentchar
* @return array
*/
function readINIfile ($filename, $commentchar) {
$array1 = file($filename);
$section = '';
foreach ($array1 as $filedata) {
$dataline = trim($filedata);
$firstchar = substr($dataline, 0, 1);
if ($firstchar!=$commentchar && $dataline!='') {
//It's an entry (not a comment and not a blank line)
if ($firstchar == '[' && substr($dataline, -1, 1) == ']') {
//It's a section
$section = strtolower(substr($dataline, 1, -1));
} else {
//It's a key...
$delimiter = strpos($dataline, '=');
if ($delimiter > 0) {
//...with a value
$key = strtolower(trim(substr($dataline, 0, $delimiter)));
$value = trim(substr($dataline, $delimiter + 1));
if (substr($value, 1, 1) == '"' && substr($value, -1, 1) == '"') { $value = substr($value, 1, -1); }
$array2[$section][$key] = stripcslashes($value);
} else {
//...without a value
$array2[$section][strtolower(trim($dataline))]='';
}
}
} else {
//It's a comment or blank line. Ignore.
}
}
return $array2;
}
}
?>