root/trunk/ini_db.php

Revision 336, 4.8 kB (checked in by padams, 20 months ago)

fixed bug where referring search terms were not being regexed properly
and preg_match was complaining loudly.

Line 
1<?php
2
3//
4// Open Web Analytics - An Open Source Web Analytics Framework
5//
6// Copyright 2006 Peter Adams. All rights reserved.
7//
8// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// $Id$
17//
18
19/**
20 * INI Database
21 *
22 * Searches INI files for matches based on various lookup methods.
23 *
24 * @author      Peter Adams <peter@openwebanalytics.com>
25 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
26 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
27 * @category    wa
28 * @package     wa
29 * @version             $Revision$           
30 * @since               wa 1.0.0
31 */
32class ini_db extends owa_base {
33
34        /**
35         * Data file
36         *
37         * @var unknown_type
38         */
39        var $ini_file;
40       
41        /**
42         * Result Format
43         *
44         * @var string
45         */
46        var $return_format;
47       
48        /**
49         * Cache flag
50         *
51         * @var boolean
52         */
53        var $cache = true;
54       
55       
56        /**
57         * Database Access Object
58         *
59         * @var object
60         */
61        var $db;
62
63        /**
64         * Constructor
65         *
66         * @param string $ini_file
67         * @param string_type $sections
68         * @param string $return_format
69         * @access public
70         * @return ini_db
71         */
72        function ini_db($ini_file, $sections = null, $return_format = 'object') {
73                $this->owa_base();
74                $this->ini_file = $ini_file;           
75                $this->return_format = $return_format;
76               
77                if (!empty($sections)):
78                        $this->db = $this->readINIfile($this->ini_file, ';');   
79                else:
80                        $this->db = file($this->ini_file);     
81                endif;
82                return;
83        }
84
85        /**
86         * Returns a section from an ini file based on regex match rule
87         * contained as keys in an ini file.
88         *
89         * @param string
90         * @access public
91         */
92        function fetch($haystack) {
93               
94                $record = null;
95               
96                foreach ($this->db as $key=>$value) {
97                        if (($key!='#*#')&&(!array_key_exists('parent',$value))) continue;
98                               
99                                $keyEreg = '#'.$key.'#';
100                               
101                        if (preg_match($keyEreg, $haystack)) {
102                           $record=array('regex'=>strtolower($keyEreg),'pattern'=>$key)+$value;
103               
104                           $maxDeep=8;
105                           while (array_key_exists('parent',$value)&&(--$maxDeep>0))
106                           
107                                $record+=($value = $this->db[strtolower($value['parent'])]);
108                           break;
109                        }
110                }
111               
112                switch ($this->return_format) {
113                        case "array":
114                                return $record;
115                                break;
116                        case "object":
117                                return ((object)$record);
118                                break;
119                }
120                return $record;
121        }
122       
123        /**
124         * Returns part of the passed string based on regex match rules
125         * contained as keys in an ini file.
126         *
127         * @param string
128         * @access public
129         * @return string
130         */
131        function match($haystack) {
132               
133                if (!empty($haystack)):
134               
135                        $tmp = '';
136                       
137                        foreach ($this->db as $key => $value) {
138                               
139                                if (!empty($value)):
140                                //$this->e->debug('ref db:'.print_r($this->db, true));
141                                        preg_match(trim($value), $haystack, $tmp);
142                                        if (!empty($tmp)):
143                                $needle = $tmp;
144                                //$this->e->debug('ref db:'.print_r($tmp, true));
145                                        endif;
146                                endif;     
147                        }
148                       
149                        return $needle;
150               
151                else:
152                        return;
153                endif;
154        }
155       
156        /**
157         * Fetch a record set and perfrom a regex replace on the name
158         *
159         * @param       string $haystack
160         * @return      string
161         */
162        function fetch_replace($haystack) {
163               
164                $record = $this->fetch($haystack);
165               
166                //print_r($record);
167               
168                $new_record = preg_replace($record->regex, $record->name, $haystack);
169               
170                return $new_record;
171        }
172       
173        /**
174         * Reads INI file
175         *
176         * @param string $filename
177         * @param string $commentchar
178         * @return array
179         */
180        function readINIfile ($filename, $commentchar) {
181                  $array1 = file($filename);
182                  $section = '';
183                  foreach ($array1 as $filedata) {
184                   $dataline = trim($filedata);
185                   $firstchar = substr($dataline, 0, 1);
186                   if ($firstchar!=$commentchar && $dataline!='') {
187                         //It's an entry (not a comment and not a blank line)
188                         if ($firstchar == '[' && substr($dataline, -1, 1) == ']') {
189                           //It's a section
190                           $section = strtolower(substr($dataline, 1, -1));
191                         }else{
192                           //It's a key...
193                           $delimiter = strpos($dataline, '=');
194                           if ($delimiter > 0) {
195                                 //...with a value
196                                 $key = strtolower(trim(substr($dataline, 0, $delimiter)));
197                                 $value = trim(substr($dataline, $delimiter + 1));
198                                 if (substr($value, 1, 1) == '"' && substr($value, -1, 1) == '"') { $value = substr($value, 1, -1); }
199                                 $array2[$section][$key] = stripcslashes($value);
200                           }else{
201                                 //...without a value
202                                 $array2[$section][strtolower(trim($dataline))]='';
203                           }
204                         }
205                   }else{
206                         //It's a comment or blank line.  Ignore.
207                   }
208                  }
209                  return $array2;
210        }
211       
212}
213
214?>
Note: See TracBrowser for help on using the browser.