-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_constants.py
34 lines (30 loc) · 1.01 KB
/
db_constants.py
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
'''
Fetch the $db_user, $db_pass, and $db_name PHP
strings from config.php.
Place them in the global namespace as db_user, db_pass,
and db_name, respectively.
'''
import os
current_directory = os.path.dirname(os.path.abspath(__file__))
def run():
for line in open(os.path.join(current_directory, "./config.php"), "r"):
line = line.split("#")[0].strip()
if len(line) == 0 or line.count("=") != 1:
continue;
left, right = line.split("=")
right = right.strip().split(";")[0].strip()
if not ((right[0] == "'" and right[-1] == "'") or (right[0] == '"' and right[-1] == '"')):
continue
right = right[1:-1]
left = left.strip()
if left[0] != "$":
continue
left = left[1:]
if left == "db_user":
db_user = right
elif left == "db_pass":
db_pass = right
elif left == "db_name":
db_name = right
return db_user, db_pass, db_name
db_user, db_pass, db_name = run()