forked from aaroneiche/do-want
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.class.php
133 lines (104 loc) · 3.21 KB
/
setup.class.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
<?php
if (session_id() == "") session_start();
class setup extends db{
/*
Method: checkDirectoryWriteable
Checks if a given directory can be written to. Returns true or false
dir - the relative path of the directory in question.
*/
function checkDirectoryWriteable($args){
error_log(is_writeable($args['dir']));
return is_writable($args['dir']);
}
/*
Method: generateConfigFile
Takes arguments and creates a config file based on the default config file.
host - the host for the db
dbname - the database name
dbuser - the database user
dbpass - the database password
table_prefix - a table prefix, if necessary
password_hasher - preferred password hashing method
filepath - relative path for file uploads
currency_symbol - The preferred currency symbol.
*/
function generateConfigFile($args){
$configFileName = "config.php"; //"config-test.php";
$response = array();
if(file_exists($configFileName)){
$response['result'] = false;
$response['message'] = "It appears you already have a config file.";
return $response;
}
$defaultConfig = file_get_contents("config.default.php");
$configItems = array(
"dbhost = 'localhost'",
"dbname = 'wishlist'",
"dbuser = 'wishlist'",
"dbpass = 'wishlist'",
"options['table_prefix'] = ''",
"options['password_hasher'] = 'MD5Hasher'",
"options['filepath'] = 'uploads/'",
"options['currency_symbol'] = '$'"
);
$populateItems = array(
"dbhost = '{$args['host']}'",
"dbname = '{$args['dbname']}'",
"dbuser = '{$args['dbuser']}'",
"dbpass = '{$args['dbpass']}'",
"options['table_prefix'] = '{$args['table_prefix']}'",
"options['password_hasher'] = '{$args['password_hasher']}'",
"options['filepath'] = '{$args['filepath']}'",
"options['currency_symbol'] = '{$args['currency_symbol']}'"
);
$fileReplace = str_replace ($configItems ,$populateItems , $defaultConfig);
$fileWrite = file_put_contents($configFileName,$fileReplace);
if($fileWrite !== false){
$response['result'] = true;
return $response;
}else{
$response['result'] = false;
return $response;
}
}
/*
Method: setupTables
Gets SQL queries out of files in the SQL directory and generates tables.
*/
function setupTables($args){
$queries = array();
if ($handle = opendir('sql/')) {
/* This is the correct way to loop over the directory. */
while (false !== ($entry = readdir($handle))) {
$fileNameEnd = strpos($entry,".sql");
if($fileNameEnd != false){
$queries[substr($entry,0,$fileNameEnd)] = file_get_contents("sql/".$entry);
}
}
closedir($handle);
}
$queryResults = array();
foreach($queries as $tableName => $queryToRun){
$queryResults[$tableName] = $this->dbQuery($queryToRun);
}
$results = array(
"result"=>true,
"message"=>"The following tables were created",
"tables" => $queryResults
);
return $results;
}
/*
testDBCredentials
*/
function testDBCredentials($args){
$this->dbhost = $args['host'];
$this->dbname = $args['dbname'];
$this->dbuser = $args['dbuser'];
$this->dbpass = $args['dbpass'];
$this->options = array("charSet"=>"utf8");
$response = $this->dbConnect();
return $response;
}
}
?>