Bearsampp 2025.8.29
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 (https://github.com/crispy-computing-machine/Winbinder)
9
10 General-purpose supporting functions
11
12*******************************************************************************/
13
14//-------------------------------------------------------------------- FUNCTIONS
15
16/* Returns an array with all files of subdirectory $path. If $subdirs is TRUE,
17 includes subdirectories recursively. $mask is a PCRE regular expression.
18*/
19
20function get_folder_files($path, $subdirs=false, $fullname=true, $mask="", $forcelowercase=TRUE)
21{
22 // Correct path name, if needed
23
24 $path = str_replace('/', '\\', $path);
25 if(substr($path, -1) != '\\')
26 $path .= "\\";
27 if(!$path || !@is_dir($path))
28 return array();
29
30 // Browse the subdiretory list recursively
31
32 $dir = array();
33 if($handle = opendir($path)) {
34 while(($file = readdir($handle)) !== false) {
35 if(!is_dir($path.$file)) { // No directories / subdirectories
36 if($forcelowercase)
37 $file = strtolower($file);
38 if(!$mask) {
39 $dir[] = $fullname ? $path.$file : $file;
40 } else if($mask && preg_match($mask, $file)) {
41 $dir[] = $fullname ? $path.$file : $file;
42 }
43 } else if($subdirs && $file[0] != ".") { // Exclude "." and ".."
44 $dir = array_merge($dir, get_folder_files($path.$file, $subdirs, $fullname, $mask));
45 }
46 }
47 }
48 closedir($handle);
49 return $dir;
50}
51
52//-------------------------------------------------------------------- INI FILES
53
54/* Transforms the array $data in a text that can be saved as an INI file.
55 Escapes double-quotes as (\") */
56
57function generate_ini($data, $comments="")
58{
59 if(!is_array($data)) {
60 trigger_error(__FUNCTION__ . ": Cannot save INI file.");
61 return null;
62 }
63 $text = $comments;
64 foreach($data as $name=>$section) {
65 $text .= "\r\n[$name]\r\n";
66
67 foreach($section as $key=>$value) {
68 $value = trim($value);
69 if((string)((int)$value) == (string)$value) // Integer: does nothing
70 ;
71 elseif((string)((float)$value) == (string)$value) // Floating point: does nothing
72 ;
73 elseif($value === "") // Empty string
74 $value = '""';
75 elseif(strstr($value, '"')) // Escape double-quotes
76 $value = '"' . str_replace('"', '\"', $value) . '"';
77 else
78 $value = '"' . $value . '"';
79
80 $text .= "$key = " . $value . "\r\n";
81 }
82 }
83 return $text;
84}
85
86/*
87
88Replaces function parse_ini_file() so INI files may be processed more similarly to Windows.
89Replaces escaped double-quotes (\") with double-quotes ("). See manual for details.
90
91*/
92
93function parse_ini($initext, $changecase=TRUE, $convertwords=TRUE)
94{
95 $ini = preg_split("/\r\n|\n/", $initext);
96 $secpattern = "/^\[(.[^\]]*)\]/i";
97// $entrypattern = "/^([a-z_0-9]*)\s*=\s*\"?([^\"]*)?\"?" . '$' . "/i";
98// $strpattern = "/^\"?(.[^\"]*)\"?" . '$' . "/i";
99 $entrypattern = "/^([a-z_0-9]*)\s*=\s*\"?([^\"]*)?\"?\$/i";
100 $strpattern = "/^\"?(.[^\"]*)\"?\$/i";
101
102 $section = array();
103 $sec = "";
104
105 // Predefined words
106
107 static $words = array("yes", "on", "true", "no", "off", "false", "null");
108 static $values = array( 1, 1, 1, 0, 0, 0, null);
109
110 // Lines loop
111
112 for($i = 0; $i < count($ini); $i++) {
113
114 $line = trim($ini[$i]);
115
116 // Replaces escaped double-quotes (\") with special signal /%quote%/
117
118 if(strstr($line, '\"'))
119 $line = str_replace('\"', '/%quote%/', $line);
120
121 // Skips blank lines and comments
122
123 if($line == "" || preg_match("/^;/i", $line))
124 continue;
125
126 if(preg_match($secpattern, $line, $matches)) {
127
128 // It's a section
129
130 $sec = $matches[1];
131
132 if($changecase)
133 $sec = ucfirst(strtolower($sec));
134
135 $section[$sec] = array();
136
137 } elseif(preg_match($entrypattern, $line, $matches)) {
138
139 // It's an entry
140
141 $entry = $matches[1];
142
143 if($changecase)
144 $entry = strtolower($entry);
145
146 $value = preg_replace($entrypattern, "\\2", $line);
147
148 // Restores double-quotes (")
149
150 $value = str_replace('/%quote%/', '"', $value);
151
152 // Convert some special words to their respective values
153
154 if($convertwords) {
155 $index = array_search(strtolower($value), $words);
156 if($index !== false)
157 $value = $values[$index];
158 }
159
160 $section[$sec][$entry] = $value;
161
162 } else {
163
164 // It's a normal string
165
166 $section[$sec][] = preg_replace($strpattern, "\\1", $line);
167
168 }
169 }
170 return $section;
171}
get_folder_files($path, $subdirs=false, $fullname=true, $mask="", $forcelowercase=TRUE)
generate_ini($data, $comments="")
parse_ini($initext, $changecase=TRUE, $convertwords=TRUE)