-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbcon.php
executable file
·295 lines (273 loc) · 9.53 KB
/
dbcon.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
sessionStart();
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}
function sessionStart()
{
//$sessionStuffTimerStart = microtime(true);
//ini_set("session.cookie_lifetime","86400"); //10800
//ini_set('session.use_only_cookies',1);
//setcookie(session_name(),session_id(),time()+86400);
//$lifetime = 86400;
//$cookieParams = session_get_cookie_params();
//session_set_cookie_params($lifetime, $cookieParams["path"], $cookieParams["domain"], false, true);
//session_write_close();
session_name("VadwebID");
session_start();
session_regenerate_id();
/*$_SESSION["sessionOldID"] = session_id();
session_regenerate_id();
$_SESSION["sessionID"] = session_id();
$sessionStuffTimerEnd = microtime(true);
$_SESSION["sessionConfigTime"] = $sessionStuffTimerEnd - $sessionStuffTimerStart;*/
incrementPerfCount("Session Initialized");
//setcookie(session_name(),session_id(),time()+$lifetime);*/
/*sec_session_start();
function sec_session_start()
{
$session_name = 'sec_session_id'; // Set a custom session name
$secure = false;
$domain = 'vadweb.us';
// This stops JavaScript being able to access the session id.
$httponly = true;
// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
ini_set("session.cookie_lifetime","10800");
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"] + 10800,
$cookieParams["path"],
$domain,
$secure,
$httponly);
// Sets the session name to the one set above.
session_name($session_name);
session_start(); // Start the PHP session
session_regenerate_id(); // regenerated the session, delete the old one.
}*/
}
function incrementPerfCount($name = NULL)
{
if ($name == NULL)
return;
$sql = SQLCon::getSQL();
$count = count($sql->sQuery("select * from PerformanceDebug where Type='$name'")->fetchAll());
if ($count == 0)
$sql->sQuery("insert into PerformanceDebug (Type, Value) values ('$name', 0)");
$sql->sQuery("UPDATE PerformanceDebug SET Value=Value+1 WHERE Type='$name'");
}
class Constants
{
const DEFAULT_FILE_STORAGE_PATH = "/home/vadwebData/";
const MULTI_FILE_UPLOAD_NUM_LIMIT = 10;
const LISTING_MODE = 1;
const VIEWING_MODE = 2;
const GROUP_NONE = 0;
const GROUP_REGISTERED = 1;
const GROUP_FRIENDS = 2;
const GROUP_ADMIN = 4;
const FILE_PHP = 1;
const VIEW_PHP = 2;
}
class SQLCon
{
private $user;
private $pass;
public $dbc = NULL;
private static $sqlPointer = NULL;
private function __construct()
{
# $this->user = getenv("DB_USER");
# $this->pass = getenv("DB_PASS");
$this->user = 'vadim';
$this->pass = 'swagOut789601234';
try
{
$this->dbc = new PDO('mysql:host=localhost;dbname=vadweb;charset=utf8', $this->user, $this->pass, array(PDO::ATTR_PERSISTENT => true));
$this->dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die("MYSQL ERROR");
}
}
public static function getSQL()
{
if (SQLCon::$sqlPointer == NULL)
{
SQLCon::$sqlPointer = new SQLCon();
//apc_store("newSQLPointer", apc_fetch("newSQLPointer") + 1);
}
//apc_store("cachedSQLPointer", apc_fetch("cachedSQLPointer") + 1);
return SQLCon::$sqlPointer;
}
function transaction()
{
try
{
$this->dbc->beginTransaction();
$arg_list = func_get_args();
foreach ($arg_list as &$a)
{
$this->dbc->exec($a);
}
$this->dbc->commit();
}
catch (Exception $e)
{
$this->dbc->rollBack();
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function prepStmt($statement)
{
try
{
return $this->dbc->prepare($statement); //returns a prepared statement
}
catch (Exception $e)
{
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function execute($statement)
{
try
{
if ($statement->execute()) //returns stmt object on success
return $statement;
return false;
}
catch (Exception $e)
{
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function bindParam($stmt, $placeholder, $value)
{
try
{
$stmt->bindParam($placeholder, $value);
}
catch (Exception $e)
{
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function quote($query)
{
try
{
return $this->dbc->quote($query);
}
catch (Exception $e)
{
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function sQuery($sqlQuery) //simple sql query, should be safe in terms of MYSQL injection
{
try
{
//$sqlQuery = $this->dbc->quote($sqlQuery);
$pdostmt = $this->dbc->prepare($sqlQuery);
$pdostmt->execute();
return $pdostmt;
}
catch (Exception $e)
{
echo "<br><br><b>MYSQL Error: " . $e->getMessage() . "</b><br><br>";
}
}
function configTables()
{
$Files = "CREATE TABLE IF NOT EXISTS `Files` (
`File_ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`User_ID` BIGINT(20) NOT NULL,
`FilePath` VARCHAR(128) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Permission` VARCHAR(16000) NOT NULL DEFAULT '*' COLLATE 'utf8mb4_unicode_ci',
`Type` VARCHAR(10) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`CreatedTime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`NSFW` BIT(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`File_ID`),
INDEX `FK_Files_UserData` (`User_ID`),
CONSTRAINT `FK_Files_UserData` FOREIGN KEY (`User_ID`) REFERENCES `UserData` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;";
$FileViews = "CREATE TABLE IF NOT EXISTS `FileViews` (
`View_ID` BIGINT(11) NOT NULL AUTO_INCREMENT,
`File_ID` BIGINT(11) NOT NULL,
`User_ID` BIGINT(11) NULL,
`IP` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IPwithProxy` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Device` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`ViewSource` TINYINT(3) UNSIGNED NULL DEFAULT NULL,
`Duration` TIME NULL DEFAULT NULL,
`Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`View_ID`),
FOREIGN KEY (`File_ID`) REFERENCES `Files` (`File_ID`) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (`User_ID`) REFERENCES `UserData` (`ID`) ON UPDATE CASCADE ON DELETE CASCADE
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;";
$GeneralViews = "CREATE TABLE IF NOT EXISTS `GeneralViews` (
`ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`Page` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IP` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IPwithProxy` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Device` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IsLoggedIn` BIT(1) NOT NULL DEFAULT b'0',
`Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;";
$LoginAttempts = "CREATE TABLE IF NOT EXISTS `LoginAttempts` (
`Login_ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Password` CHAR(129) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IP` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`IPwithProxy` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Device` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
`Success` BIT(1) NOT NULL DEFAULT b'0',
`Time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`Login_ID`)
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;";
$UserData = "CREATE TABLE IF NOT EXISTS `UserData` (
`ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`Username` CHAR(50) NOT NULL,
`Email` CHAR(50) NOT NULL,
`YOB` INT(11) UNSIGNED NOT NULL,
`Password` CHAR(129) NOT NULL,
`GroupVal` TINYINT(4) NOT NULL DEFAULT '1',
`IP` VARCHAR(255) NOT NULL,
`IPwithProxy` VARCHAR(255) NOT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;";
$PerformanceDebug = "CREATE TABLE IF NOT EXISTS `PerformanceDebug` (
`Type` tinytext COLLATE utf8mb4_unicode_ci NOT NULL,
`Value` bigint(20) NOT NULL
)
ENGINE=InnoDB
COLLATE=utf8mb4_unicode_ci;";
$this->transaction($PerformanceDebug, $UserData, $LoginAttempts, $GeneralViews, $Files, $FileViews, $PerformanceDebug);
}
}
?>