2024.8.23
Loading...
Searching...
No Matches
wb_generic.inc.php
Go to the documentation of this file.
1<?php
2
3/*******************************************************************************
4
5 WINBINDER - The native Windows binding for PHP for PHP
6
7 Copyright � Hypervisual - see LICENSE.TXT for details
8 Author: Rubem Pechansky (http://winbinder.org/contact.php)
9
10 General-purpose supporting functions
11
12*******************************************************************************/
13
14//-------------------------------------------------------------------- FUNCTIONS
15
16/*
17
18Mimics function file_put_contents from PHP 5
19
20int file_put_contents (string filename, string data [, int flags [, resource context]])
21
22*/
23
24if(PHP_VERSION < "5.0.0") {
25
26function file_put_contents($filename, $data, $flags=0, $zcontext=null)
27{
28 if($zcontext)
29 $fp = @fopen($filename, "w+b", $flags, $zcontext);
30 else
31 $fp = @fopen($filename, "w+b", $flags);
32 if(!$fp)
33 return FALSE;
34 fwrite($fp, $data);
35 fclose($fp);
36 return TRUE;
37}
38
39} // PHP_VERSION < "5.0.0"
40
41
42/* Returns an array with all files of subdirectory $path. If $subdirs is TRUE,
43 includes subdirectories recursively. $mask is a PCRE regular expression.
44*/
45
46function get_folder_files($path, $subdirs=false, $fullname=true, $mask="", $forcelowercase=TRUE)
47{
48 // Correct path name, if needed
49
50 $path = str_replace('/', '\\', $path);
51 if(substr($path, -1) != '\\')
52 $path .= "\\";
53 if(!$path || !@is_dir($path))
54 return array();
55
56 // Browse the subdiretory list recursively
57
58 $dir = array();
59 if($handle = opendir($path)) {
60 while(($file = readdir($handle)) !== false) {
61 if(!is_dir($path.$file)) { // No directories / subdirectories
62 if($forcelowercase)
63 $file = strtolower($file);
64 if(!$mask) {
65 $dir[] = $fullname ? $path.$file : $file;
66 } else if($mask && preg_match($mask, $file)) {
67 $dir[] = $fullname ? $path.$file : $file;
68 }
69 } else if($subdirs && $file[0] != ".") { // Exclude "." and ".."
70 $dir = array_merge($dir, get_folder_files($path.$file, $subdirs, $fullname, $mask));
71 }
72 }
73 }
74 closedir($handle);
75 return $dir;
76}
77
78//-------------------------------------------------------------------- INI FILES
79
80/* Transforms the array $data in a text that can be saved as an INI file.
81 Escapes double-quotes as (\") */
82
83function generate_ini($data, $comments="")
84{
85 if(!is_array($data)) {
86 trigger_error(__FUNCTION__ . ": Cannot save INI file.");
87 return null;
88 }
89 $text = $comments;
90 foreach($data as $name=>$section) {
91 $text .= "\r\n[$name]\r\n";
92
93 foreach($section as $key=>$value) {
94 $value = trim($value);
95 if((string)((int)$value) == (string)$value) // Integer: does nothing
96 ;
97 elseif((string)((float)$value) == (string)$value) // Floating point: does nothing
98 ;
99 elseif($value === "") // Empty string
100 $value = '""';
101 elseif(strstr($value, '"')) // Escape double-quotes
102 $value = '"' . str_replace('"', '\"', $value) . '"';
103 else
104 $value = '"' . $value . '"';
105
106 $text .= "$key = " . $value . "\r\n";
107 }
108 }
109 return $text;
110}
111
112/*
113
114Replaces function parse_ini_file() so INI files may be processed more similarly to Windows.
115Replaces escaped double-quotes (\") with double-quotes ("). See manual for details.
116
117*/
118
119function parse_ini($initext, $changecase=TRUE, $convertwords=TRUE)
120{
121 $ini = preg_split("/\r\n|\n/", $initext);
122 $secpattern = "/^\[(.[^\]]*)\]/i";
123// $entrypattern = "/^([a-z_0-9]*)\s*=\s*\"?([^\"]*)?\"?" . '$' . "/i";
124// $strpattern = "/^\"?(.[^\"]*)\"?" . '$' . "/i";
125 $entrypattern = "/^([a-z_0-9]*)\s*=\s*\"?([^\"]*)?\"?\$/i";
126 $strpattern = "/^\"?(.[^\"]*)\"?\$/i";
127
128 $section = array();
129 $sec = "";
130
131 // Predefined words
132
133 static $words = array("yes", "on", "true", "no", "off", "false", "null");
134 static $values = array( 1, 1, 1, 0, 0, 0, null);
135
136 // Lines loop
137
138 for($i = 0; $i < count($ini); $i++) {
139
140 $line = trim($ini[$i]);
141
142 // Replaces escaped double-quotes (\") with special signal /%quote%/
143
144 if(strstr($line, '\"'))
145 $line = str_replace('\"', '/%quote%/', $line);
146
147 // Skips blank lines and comments
148
149 if($line == "" || preg_match("/^;/i", $line))
150 continue;
151
152 if(preg_match($secpattern, $line, $matches)) {
153
154 // It's a section
155
156 $sec = $matches[1];
157
158 if($changecase)
159 $sec = ucfirst(strtolower($sec));
160
161 $section[$sec] = array();
162
163 } elseif(preg_match($entrypattern, $line, $matches)) {
164
165 // It's an entry
166
167 $entry = $matches[1];
168
169 if($changecase)
170 $entry = strtolower($entry);
171
172 $value = preg_replace($entrypattern, "\\2", $line);
173
174 // Restores double-quotes (")
175
176 $value = str_replace('/%quote%/', '"', $value);
177
178 // Convert some special words to their respective values
179
180 if($convertwords) {
181 $index = array_search(strtolower($value), $words);
182 if($index !== false)
183 $value = $values[$index];
184 }
185
186 $section[$sec][$entry] = $value;
187
188 } else {
189
190 // It's a normal string
191
192 $section[$sec][] = preg_replace($strpattern, "\\1", $line);
193
194 }
195 }
196 return $section;
197}
198
199//------------------------------------------------------------------ END OF FILE
200
201?>
if(PHP_VERSION< "5.0.0") get_folder_files($path, $subdirs=false, $fullname=true, $mask="", $forcelowercase=TRUE)
generate_ini($data, $comments="")
parse_ini($initext, $changecase=TRUE, $convertwords=TRUE)