forked from FiftyNine/scpper-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.php
224 lines (194 loc) · 6.1 KB
/
utils.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
require_once "auth.inc";
class KeepAliveMysqli
{
const MAX_TIMEOUT = 59; // 180000000;// 3*60*1000*1000;
/*** Fields ***/
private $link = null;
private $logger = null;
private $lastAccess = 0;
private $host;
private $login;
private $password;
private $database;
private $port;
public function __construct($host, $login, $password, $database, $port, $logger = null)
{
$this->host = $host;
$this->login = $login;
$this->password = $password;
$this->database = $database;
$this->port = $port;
$this->logger = $logger;
}
public function query($text)
{
return $this->getLink()->query($text);
}
public function prepare($text)
{
return $this->getLink()->prepare($text);
}
public function begin_transaction()
{
return $this->getLink()->begin_transaction();
}
public function commit()
{
return $this->getLink()->commit();
}
public function rollback()
{
return $this->getLink()->rollback();
}
public function insert_id()
{
return $this->getLink()->insert_id;
}
public function error()
{
return $this->getLink()->error;
}
public function getLink()
{
if (!$this->link || ((microtime(true) - $this->lastAccess) >= self::MAX_TIMEOUT)) {
if ($this->link) {
$this->link->close();
}
$this->link = new mysqli($this->host, $this->login, $this->password, $this->database, $this->port);
if ($this->link->connect_error) {
WikidotLogger::log($this->logger, 'Connect Error (' . $this->link->connect_errno . ') '. $this->link->connect_error);
die('');
}
$this->link->set_charset("utf8mb4");
}
$this->lastAccess = microtime(true);
return $this->link;
}
}
class iimysqli_result
{
public $stmt, $cols;
}
function iimysqli_stmt_get_result($stmt)
{
/** EXPLANATION:
* We are creating a fake "result" structure to enable us to have
* source-level equivalent syntax to a query executed via
* mysqli_query().
*
* $stmt = mysqli_prepare($conn, "");
* mysqli_bind_param($stmt, "types", ...);
*
* $param1 = 0;
* $param2 = 'foo';
* $param3 = 'bar';
* mysqli_execute($stmt);
* $result _mysqli_stmt_get_result($stmt);
* [ $arr = _mysqli_result_fetch_array($result);
* || $assoc = _mysqli_result_fetch_assoc($result); ]
* mysqli_stmt_close($stmt);
* mysqli_close($conn);
*
* At the source level, there is no difference between this and mysqlnd.
**/
$metadata = mysqli_stmt_result_metadata($stmt);
$ret = new iimysqli_result;
if (!$ret) return NULL;
$ret->cols = array();//mysqli_num_fields($metadata);
while ($field = $metadata->fetch_field()) {
$ret->cols[] = $field->name;
}
$ret->stmt = $stmt;
mysqli_free_result($metadata);
return $ret;
}
function iimysqli_result_fetch_array(&$result)
{
$ret = array();
$code = "return mysqli_stmt_bind_result(\$result->stmt ";
for ($i=0; $i<count($result->cols); $i++)
{
$ret[$i] = NULL;
$code .= ", \$ret['" .$result->cols[$i] ."']";
};
$code .= ");";
if (!eval($code)) { return NULL; };
// This should advance the "$stmt" cursor.
if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };
// Return the array we built.
return $ret;
}
function createElement($doc, $elemType, $text = '', $attrs = array())
{
$elem = $doc->createElement($elemType);
if (is_array($attrs)) {
foreach ($attrs as $attr => $value) {
$elem->setAttribute($attr, $value);
}
}
if ($text) {
$elem->appendChild($doc->createTextNode($text));
}
return $elem;
}
function createPage()
{
$x = new DOMImplementation();
$doctype = $x->createDocumentType('html', '', '');
$document = $x->createDocument('', 'html', $doctype);
$head = $document->createElement('head');
$metahttp = $document->createElement('meta');
$metahttp->setAttribute('http-equiv', 'Content-Type');
$metahttp->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($metahttp);
$title = $document->createElement('title', 'SCPper');
$head->appendChild($title);
$body = $document->createElement('body');
$html = $document->getElementsByTagName('html')->item(0);
$html->appendChild($head);
$html->appendChild($body);
return $document;
}
function getDbLink($mode = 0)
{
global $SCPPER_READER;
global $SCPPER_WRITER;
if ($mode == 1) {
$auth = $SCPPER_WRITER;
} else {
$auth = $SCPPER_READER;
}
$link = new mysqli($auth['HOST'], $auth['LOGIN'], $auth['PASSWORD'], $auth['DATABASE'], 3306);
if ($link->connect_error) {
return null;
}
$link->set_charset("utf8mb4");
return $link;
}
function generateCallTrace()
{
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++)
{
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
}
return "\t" . implode("\n\t", $result);
}
function showConnectionStatus(mysqli $link)
{
$result = $link->query('SHOW SESSION STATUS;', MYSQLI_USE_RESULT);
while ($row = $result->fetch_assoc()) {
$array[$row['Variable_name']] = $row['Value'];
}
$result->close();
print_r($array);
}
?>