| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* I18N Utility file.
|
|
|
5 |
*
|
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
|
7 |
* it under the terms of the BSD License.
|
|
|
8 |
*
|
|
|
9 |
* Copyright(c) 2004 by Wei Zhuo. All rights reserved.
|
|
|
10 |
*
|
|
|
11 |
* To contact the author write to <weizhuo[at]gmail[dot]com>
|
|
|
12 |
* The latest version of PRADO can be obtained from:
|
|
|
13 |
* {@link http://prado.sourceforge.net/}
|
|
|
14 |
*
|
|
|
15 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
16 |
* @version $Revision: 1.3 $ $Date: 2005/08/27 03:21:12 $
|
|
|
17 |
* @package System.I18N.core
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* For a given DSN (database connection string), return some information
|
|
|
23 |
* about the DSN. This function comes from PEAR's DB package.
|
|
|
24 |
*
|
|
|
25 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
26 |
* that is available through the world-wide-web at the following URI:
|
|
|
27 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
28 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
29 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
30 |
*
|
|
|
31 |
* @param string DSN format, similar to PEAR's DB
|
|
|
32 |
* @return array DSN information.
|
|
|
33 |
* @author Stig Bakken <ssb@php.net>
|
|
|
34 |
* @author Tomas V.V.Cox <cox@idecnet.com>
|
|
|
35 |
* @author Daniel Convissor <danielc@php.net>
|
|
|
36 |
* @copyright 1997-2005 The PHP Group
|
|
|
37 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
38 |
* @link http://pear.php.net/package/DB
|
|
|
39 |
*/
|
|
|
40 |
function parseDSN($dsn)
|
|
|
41 |
{
|
|
|
42 |
if (is_array($dsn)) {
|
|
|
43 |
return $dsn;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
$parsed = array(
|
|
|
47 |
'phptype' => false,
|
|
|
48 |
'dbsyntax' => false,
|
|
|
49 |
'username' => false,
|
|
|
50 |
'password' => false,
|
|
|
51 |
'protocol' => false,
|
|
|
52 |
'hostspec' => false,
|
|
|
53 |
'port' => false,
|
|
|
54 |
'socket' => false,
|
|
|
55 |
'database' => false
|
|
|
56 |
);
|
|
|
57 |
|
|
|
58 |
// Find phptype and dbsyntax
|
|
|
59 |
if (($pos = strpos($dsn, '://')) !== false) {
|
|
|
60 |
$str = substr($dsn, 0, $pos);
|
|
|
61 |
$dsn = substr($dsn, $pos + 3);
|
|
|
62 |
} else {
|
|
|
63 |
$str = $dsn;
|
|
|
64 |
$dsn = null;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
// Get phptype and dbsyntax
|
|
|
68 |
// $str => phptype(dbsyntax)
|
|
|
69 |
if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
|
|
|
70 |
$parsed['phptype'] = $arr[1];
|
|
|
71 |
$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
|
|
|
72 |
} else {
|
|
|
73 |
$parsed['phptype'] = $str;
|
|
|
74 |
$parsed['dbsyntax'] = $str;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
if (empty($dsn)) {
|
|
|
78 |
return $parsed;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// Get (if found): username and password
|
|
|
82 |
// $dsn => username:password@protocol+hostspec/database
|
|
|
83 |
if (($at = strrpos($dsn,'@')) !== false) {
|
|
|
84 |
$str = substr($dsn, 0, $at);
|
|
|
85 |
$dsn = substr($dsn, $at + 1);
|
|
|
86 |
if (($pos = strpos($str, ':')) !== false) {
|
|
|
87 |
$parsed['username'] = rawurldecode(substr($str, 0, $pos));
|
|
|
88 |
$parsed['password'] = rawurldecode(substr($str, $pos + 1));
|
|
|
89 |
} else {
|
|
|
90 |
$parsed['username'] = rawurldecode($str);
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
// Find protocol and hostspec
|
|
|
95 |
|
|
|
96 |
// $dsn => proto(proto_opts)/database
|
|
|
97 |
if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
|
|
|
98 |
$proto = $match[1];
|
|
|
99 |
$proto_opts = (!empty($match[2])) ? $match[2] : false;
|
|
|
100 |
$dsn = $match[3];
|
|
|
101 |
|
|
|
102 |
// $dsn => protocol+hostspec/database (old format)
|
|
|
103 |
} else {
|
|
|
104 |
if (strpos($dsn, '+') !== false) {
|
|
|
105 |
list($proto, $dsn) = explode('+', $dsn, 2);
|
|
|
106 |
}
|
|
|
107 |
if (strpos($dsn, '/') !== false) {
|
|
|
108 |
list($proto_opts, $dsn) = explode('/', $dsn, 2);
|
|
|
109 |
} else {
|
|
|
110 |
$proto_opts = $dsn;
|
|
|
111 |
$dsn = null;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// process the different protocol options
|
|
|
116 |
$parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
|
|
|
117 |
$proto_opts = rawurldecode($proto_opts);
|
|
|
118 |
if ($parsed['protocol'] == 'tcp') {
|
|
|
119 |
if (strpos($proto_opts, ':') !== false) {
|
|
|
120 |
list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
|
|
|
121 |
} else {
|
|
|
122 |
$parsed['hostspec'] = $proto_opts;
|
|
|
123 |
}
|
|
|
124 |
} elseif ($parsed['protocol'] == 'unix') {
|
|
|
125 |
$parsed['socket'] = $proto_opts;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Get dabase if any
|
|
|
129 |
// $dsn => database
|
|
|
130 |
if (!empty($dsn)) {
|
|
|
131 |
// /database
|
|
|
132 |
if (($pos = strpos($dsn, '?')) === false) {
|
|
|
133 |
$parsed['database'] = $dsn;
|
|
|
134 |
// /database?param1=value1¶m2=value2
|
|
|
135 |
} else {
|
|
|
136 |
$parsed['database'] = substr($dsn, 0, $pos);
|
|
|
137 |
$dsn = substr($dsn, $pos + 1);
|
|
|
138 |
if (strpos($dsn, '&') !== false) {
|
|
|
139 |
$opts = explode('&', $dsn);
|
|
|
140 |
} else { // database?param1=value1
|
|
|
141 |
$opts = array($dsn);
|
|
|
142 |
}
|
|
|
143 |
foreach ($opts as $opt) {
|
|
|
144 |
list($key, $value) = explode('=', $opt);
|
|
|
145 |
if (!isset($parsed[$key])) { // don't allow params overwrite
|
|
|
146 |
$parsed[$key] = rawurldecode($value);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
return $parsed;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Convert strings to UTF-8 via iconv. NB, the result may not by UTF-8
|
|
|
158 |
* if the conversion failed.
|
|
|
159 |
* @param string string to convert to UTF-8
|
|
|
160 |
* @return string UTF-8 encoded string, original string if iconv failed.
|
|
|
161 |
*/
|
|
|
162 |
function I18N_toUTF8($string, $from)
|
|
|
163 |
{
|
|
|
164 |
if($from != 'UTF-8')
|
|
|
165 |
{
|
|
|
166 |
$s = iconv($from,'UTF-8',$string); //to UTF-8
|
|
|
167 |
return $s !== false ? $s : $string; //it could return false
|
|
|
168 |
}
|
|
|
169 |
return $string;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Convert UTF-8 strings to a different encoding. NB. The result
|
|
|
174 |
* may not have been encoded if iconv fails.
|
|
|
175 |
* @param string the UTF-8 string for conversion
|
|
|
176 |
* @return string encoded string.
|
|
|
177 |
*/
|
|
|
178 |
function I18N_toEncoding($string, $to)
|
|
|
179 |
{
|
|
|
180 |
if($to != 'UTF-8')
|
|
|
181 |
{
|
|
|
182 |
$s = iconv('UTF-8', $to, $string);
|
|
|
183 |
return $s !== false ? $s : $string;
|
|
|
184 |
}
|
|
|
185 |
return $string;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
?>
|