| 1 |
<?php |
|---|
| 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 |
abstract class sfMessageSource_Database extends sfMessageSource |
|---|
| 32 |
{ |
|---|
| 33 |
|
|---|
| 34 |
* For a given DSN (database connection string), return some information about the DSN. |
|---|
| 35 |
* |
|---|
| 36 |
* This function comes from PEAR's DB package. |
|---|
| 37 |
* |
|---|
| 38 |
* @param string $dns DSN format, similar to PEAR's DB |
|---|
| 39 |
* @return array DSN information. |
|---|
| 40 |
*/ |
|---|
| 41 |
protected function parseDSN($dsn) |
|---|
| 42 |
{ |
|---|
| 43 |
if (is_array($dsn)) |
|---|
| 44 |
{ |
|---|
| 45 |
return $dsn; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
$parsed = array( |
|---|
| 49 |
'phptype' => false, |
|---|
| 50 |
'dbsyntax' => false, |
|---|
| 51 |
'username' => false, |
|---|
| 52 |
'password' => false, |
|---|
| 53 |
'protocol' => false, |
|---|
| 54 |
'hostspec' => false, |
|---|
| 55 |
'port' => false, |
|---|
| 56 |
'socket' => false, |
|---|
| 57 |
'database' => false |
|---|
| 58 |
); |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
if (($pos = strpos($dsn, '://')) !== false) |
|---|
| 62 |
{ |
|---|
| 63 |
$str = substr($dsn, 0, $pos); |
|---|
| 64 |
$dsn = substr($dsn, $pos + 3); |
|---|
| 65 |
} |
|---|
| 66 |
else |
|---|
| 67 |
{ |
|---|
| 68 |
$str = $dsn; |
|---|
| 69 |
$dsn = NULL; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
// $str => phptype(dbsyntax) |
|---|
| 74 |
if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) |
|---|
| 75 |
{ |
|---|
| 76 |
$parsed['phptype'] = $arr[1]; |
|---|
| 77 |
$parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2]; |
|---|
| 78 |
} |
|---|
| 79 |
else |
|---|
| 80 |
{ |
|---|
| 81 |
$parsed['phptype'] = $str; |
|---|
| 82 |
$parsed['dbsyntax'] = $str; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
if (empty($dsn)) |
|---|
| 86 |
{ |
|---|
| 87 |
return $parsed; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
// $dsn => username:password@protocol+hostspec/database |
|---|
| 92 |
if (($at = strrpos($dsn,'@')) !== false) |
|---|
| 93 |
{ |
|---|
| 94 |
$str = substr($dsn, 0, $at); |
|---|
| 95 |
$dsn = substr($dsn, $at + 1); |
|---|
| 96 |
if (($pos = strpos($str, ':')) !== false) |
|---|
| 97 |
{ |
|---|
| 98 |
$parsed['username'] = rawurldecode(substr($str, 0, $pos)); |
|---|
| 99 |
$parsed['password'] = rawurldecode(substr($str, $pos + 1)); |
|---|
| 100 |
} |
|---|
| 101 |
else |
|---|
| 102 |
{ |
|---|
| 103 |
$parsed['username'] = rawurldecode($str); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
// $dsn => proto(proto_opts)/database |
|---|
| 110 |
if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) |
|---|
| 111 |
{ |
|---|
| 112 |
$proto = $match[1]; |
|---|
| 113 |
$proto_opts = (!empty($match[2])) ? $match[2] : false; |
|---|
| 114 |
$dsn = $match[3]; |
|---|
| 115 |
|
|---|
| 116 |
} |
|---|
| 117 |
else |
|---|
| 118 |
{ |
|---|
| 119 |
if (strpos($dsn, '+') !== false) |
|---|
| 120 |
{ |
|---|
| 121 |
list($proto, $dsn) = explode('+', $dsn, 2); |
|---|
| 122 |
} |
|---|
| 123 |
if (strpos($dsn, '/') !== false) |
|---|
| 124 |
{ |
|---|
| 125 |
list($proto_opts, $dsn) = explode('/', $dsn, 2); |
|---|
| 126 |
} |
|---|
| 127 |
else |
|---|
| 128 |
{ |
|---|
| 129 |
$proto_opts = $dsn; |
|---|
| 130 |
$dsn = null; |
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
$parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp'; |
|---|
| 136 |
$proto_opts = rawurldecode($proto_opts); |
|---|
| 137 |
if ($parsed['protocol'] == 'tcp') |
|---|
| 138 |
{ |
|---|
| 139 |
if (strpos($proto_opts, ':') !== false) |
|---|
| 140 |
{ |
|---|
| 141 |
list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts); |
|---|
| 142 |
} |
|---|
| 143 |
else |
|---|
| 144 |
{ |
|---|
| 145 |
$parsed['hostspec'] = $proto_opts; |
|---|
| 146 |
} |
|---|
| 147 |
} |
|---|
| 148 |
else if ($parsed['protocol'] == 'unix') |
|---|
| 149 |
{ |
|---|
| 150 |
$parsed['socket'] = $proto_opts; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
// $dsn => database |
|---|
| 155 |
if (!empty($dsn)) |
|---|
| 156 |
{ |
|---|
| 157 |
|
|---|
| 158 |
if (($pos = strpos($dsn, '?')) === false) |
|---|
| 159 |
{ |
|---|
| 160 |
$parsed['database'] = $dsn; |
|---|
| 161 |
|
|---|
| 162 |
} |
|---|
| 163 |
else |
|---|
| 164 |
{ |
|---|
| 165 |
$parsed['database'] = substr($dsn, 0, $pos); |
|---|
| 166 |
$dsn = substr($dsn, $pos + 1); |
|---|
| 167 |
if (strpos($dsn, '&') !== false) |
|---|
| 168 |
{ |
|---|
| 169 |
$opts = explode('&', $dsn); |
|---|
| 170 |
} |
|---|
| 171 |
else |
|---|
| 172 |
{ |
|---|
| 173 |
$opts = array($dsn); |
|---|
| 174 |
} |
|---|
| 175 |
foreach ($opts as $opt) |
|---|
| 176 |
{ |
|---|
| 177 |
list($key, $value) = explode('=', $opt); |
|---|
| 178 |
if (!isset($parsed[$key])) |
|---|
| 179 |
{ |
|---|
| 180 |
$parsed[$key] = rawurldecode($value); |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
return $parsed; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
* Gets all the variants of a particular catalogue. |
|---|
| 191 |
* |
|---|
| 192 |
* @param string $catalogue catalogue name |
|---|
| 193 |
* @return array list of all variants for this catalogue. |
|---|
| 194 |
*/ |
|---|
| 195 |
public function getCatalogueList($catalogue) |
|---|
| 196 |
{ |
|---|
| 197 |
$variants = explode('_', $this->culture); |
|---|
| 198 |
|
|---|
| 199 |
$catalogues = array($catalogue); |
|---|
| 200 |
|
|---|
| 201 |
$variant = null; |
|---|
| 202 |
|
|---|
| 203 |
for ($i = 0, $max = count($variants); $i < $max; $i++) |
|---|
| 204 |
{ |
|---|
| 205 |
if (strlen($variants[$i]) > 0) |
|---|
| 206 |
{ |
|---|
| 207 |
$variant .= $variant ? '_'.$variants[$i] : $variants[$i]; |
|---|
| 208 |
$catalogues[] = $catalogue.'.'.$variant; |
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
return array_reverse($catalogues); |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
public function getId() |
|---|
| 216 |
{ |
|---|
| 217 |
return md5($this->source); |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|