-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathConnect.php
187 lines (162 loc) · 7.05 KB
/
Connect.php
1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
require_once 'Generic.php';
/**
* Converter: mysql_connect, mysql_pconnect.
*
* @category Functions
*
* @author Andrey Hristov <andrey@php.net>, Ulf Wendel <ulf.wendel@phpdoc.de>
* @copyright 1997-2006 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
*
* @version CVS: $Id:$, Release: @package_version@
*
* @link http://www.mysql.com
* @since Class available since Release 1.0
*/
class MySQLConverterTool_Function_Connect extends MySQLConverterTool_Function_Generic
{
public $new_name = 'mysqli_connect';
public function __construct()
{
}
public function handle(array $params = array())
{
static $last_params = array();
/*
Known deficiencies:
- will try to handle unix_socket but not the best solution
*/
// mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )
// mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] )
// mysqli_connect ( [string host [, string username [, string passwd [, string dbname [, int port [, string socket]]]]]] )
$warning = null;
$socket = null;
$port = null;
@list($server, $user, $password, $new_link, $client_flags) = $this->extractParamValues($params);
if (!is_null($server)) {
if ($params[0]['dynamic']) {
$warning = 'Cannot analyze server parameter to extract host, socket and port! Conversion cannot be performed automatically. You must manually check the result of the conversion.';
} else {
list($server, $socket, $port) = $this->expandServerParam($server);
}
}
$found = false;
if (is_null($new_link) || ($new_link == false)) {
// Maybe someone relies on:
// If a second call is made to mysql_connect() with the same arguments,
// no new link will be established, but instead, the link identifier of the already
// opened link will be returned. The new_link parameter modifies this behavior
// and makes mysql_connect() always open a new link, even if mysql_connect()
// was called before with the same parameters.
// The current converter version cannot catch this. All we can do is throw a warning
foreach ($last_params as $k => $lparams) {
if ($params == $lparams) {
$warning .= " You're calling mysql_connect() twice with the same parameters. We don't know for sure if you want a new connection or reuse the old connection. You must check your code.";
$found = true;
break;
}
}
}
if (!$found) {
$last_params[] = $params;
}
$dbname = 'NULL';
$ret = null;
$num_params = count($params);
if ($num_params < 5) {
if (!is_null($socket)) {
$ret = sprintf('(%s = %s(%s, %s, %s, %s, %d, \'%s\'))',
$this->ston_name,
$this->new_name,
is_null($server) ? 'NULL' : $server,
is_null($user) ? 'NULL' : $user,
is_null($password) ? 'NULL' : $password,
is_null($dbname) ? 'NULL' : $dbname,
is_null($port) ? 'NULL' : $port,
$socket);
} else {
if (is_null($server)) {
// mysql_connect()
$ret = sprintf('(%s = %s())',
$this->ston_name,
$this->new_name);
} elseif (!is_null($port)) {
// port used - no chance to generate pretty code
$ret = sprintf('(%s = %s(%s, %s, %s, %s, %d))',
$this->ston_name,
$this->new_name,
is_null($server) ? 'NULL' : $server,
is_null($user) ? 'NULL' : $user,
is_null($password) ? 'NULL' : $password,
is_null($dbname) ? 'NULL' : $dbname,
$port
);
} elseif (!is_null($password)) {
// mysql_connect(<server>, <username>, <password>)
$ret = sprintf('(%s = %s(%s, %s, %s))',
$this->ston_name,
$this->new_name,
is_null($server) ? 'NULL' : $server,
is_null($user) ? 'NULL' : $user,
$password);
} elseif (!is_null($user)) {
// mysql_connect(<server>, <username>
$ret = sprintf('(%s = %s(%s, %s))',
$this->ston_name,
$this->new_name,
is_null($server) ? 'NULL' : $server,
$user);
} else {
// mysql_connect(<server>) but not mysql_connect(<server:port>)
$ret = sprintf('(%s = %s(%s))',
$this->ston_name,
$this->new_name,
$server);
}
}
} elseif ($num_params == 5) {
$ret = sprintf('(((%s = mysqli_init()) && (mysqli_real_connect(%s, %s, %s, %s, %s, %d, %s, %s))) ? %s : FALSE)',
$this->ston_name,
$this->ston_name,
$server,
$user,
$password,
$dbname,
is_null($port) ? 3306 : $port,
is_null($socket) ? 'NULL' : $socket,
$client_flags,
$this->ston_name);
} else {
// too many parameters
$warning = self::PARSE_ERROR_WRONG_PARAMS;
$ret = null;
}
return array($warning, $ret);
}
protected function expandServerParam($server)
{
$socket = null;
$port = null;
$pos = strpos($server, ':/');
if (false !== $pos) {
// Unix Socket
$first_ch = $server[0];
$last_ch = substr($server, -1, 1);
if (($last_ch == '"' || $last_ch == "'") && $last_ch == $first_ch) {
$server = substr($server, 1, -1);
}
$socket = substr($server, $pos + 1);
$server = '"localhost"';
} elseif (preg_match('/(["\']*)([^:]+):(\d+)/iu', $server, $matches)) {
// host:port
$server = $matches[1].$matches[2].$matches[1];
$port = $matches[3];
}
return array($server, $socket, $port);
}
public function getConversionHint()
{
return 'mysql_connect can be mapped to mysqli_connect with some parameter swapping if no client_flags are used. If client_flags are used, mysqli_init()/mysqli_real_connect() are needed.';
}
}