-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconfig.php
More file actions
120 lines (94 loc) · 3.11 KB
/
Copy pathconfig.php
File metadata and controls
120 lines (94 loc) · 3.11 KB
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
<?php
mysqli_report(MYSQLI_REPORT_OFF);
class RaviKoQr
{
private $conn;
private $lastError = '';
private $config = array(
'host' => '127.0.0.1',
'user' => 'root',
'pass' => '',
'name' => 'qrcode',
);
public function __construct(array $config = array())
{
$envConfig = array(
'host' => getenv('QR_DB_HOST') ?: $this->config['host'],
'user' => getenv('QR_DB_USER') ?: $this->config['user'],
'pass' => getenv('QR_DB_PASS') !== false ? getenv('QR_DB_PASS') : $this->config['pass'],
'name' => getenv('QR_DB_NAME') ?: $this->config['name'],
);
$this->config = array_merge($this->config, $envConfig, $config);
$this->connect();
}
private function connect()
{
$connection = @new mysqli(
$this->config['host'],
$this->config['user'],
$this->config['pass'],
$this->config['name']
);
if ($connection instanceof mysqli && !$connection->connect_error) {
$connection->set_charset('utf8mb4');
$this->conn = $connection;
return;
}
$this->conn = null;
$this->lastError = $connection instanceof mysqli && $connection->connect_error
? $connection->connect_error
: 'Unable to connect to the database.';
}
public function isConnected()
{
return $this->conn instanceof mysqli;
}
public function getLastError()
{
return $this->lastError;
}
public function insertQr($qrUserName, $qrContent, $qrImage, $qrLink)
{
if (!$this->isConnected()) {
$this->lastError = 'Database archive is unavailable. The image was generated locally only.';
return false;
}
$statement = $this->conn->prepare(
'INSERT INTO qrcodes (qrUsername, qrContent, qrImg, qrlink) VALUES (?, ?, ?, ?)'
);
if (!$statement) {
$this->lastError = $this->conn->error ?: 'Unable to prepare the insert query.';
return false;
}
$statement->bind_param('ssss', $qrUserName, $qrContent, $qrImage, $qrLink);
$result = $statement->execute();
if (!$result) {
$this->lastError = $statement->error ?: 'Unable to save the QR record.';
}
$statement->close();
return (bool) $result;
}
public function getRecentQrs($limit = 8)
{
if (!$this->isConnected()) {
return array();
}
$limit = max(1, min(24, (int) $limit));
$query = sprintf(
'SELECT id, qrUsername, qrContent, qrImg, qrlink FROM qrcodes ORDER BY id DESC LIMIT %d',
$limit
);
$result = $this->conn->query($query);
if (!$result) {
$this->lastError = $this->conn->error ?: 'Unable to load the recent QR history.';
return array();
}
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
$result->free();
return $rows;
}
}
$meravi = new RaviKoQr();