forked from permag/PHP-audio-recorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__install__app.php
212 lines (171 loc) · 4.88 KB
/
__install__app.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
<?php
require_once('database/DBConfig.php');
require_once('database/Database.php');
###################################################################
# INSTALL AND UNINSTALL #
###################################################################
$installationManager = new InstallationManager();
$installationMangerView = new InstallationManagerView();
// display menu
echo $installationMangerView->doManagerBox();
// if cliced Install button: install
if ($installationMangerView->doInstall()) {
$installationManager->install();
}
// if cliced Uninstall button: uninstall
if ($installationMangerView->doUninstall()) {
$installationManager->uninstall();
}
###################################################################
/**
* MANAGER
*/
class InstallationManager {
// table names
const TABLE_MEMBER = 'member';
const TABLE_RECORDING = 'recording';
private $m_db = null;
public function __construct() {
// DB
$dbConfig = new \permag\database\DBConfig();
$db = new \permag\database\Database($dbConfig);
$db->connect();
$this->m_db = $db;
}
public function install() {
$install = new Install($this->m_db);
if ($install->init()) {
echo '<h2>Installation succeeded.</h2>
<p><a href="./">Go to application</a><p>';
} else {
echo '<h2>Installation failed.</h2>';
}
}
public function uninstall() {
$uninstall = new Uninstall($this->m_db);
if ($uninstall->init()) {
echo '<h2>Uninstallation succeeded.</h2>';
} else {
echo '<h2>Uninstallation failed.</h2>';
}
}
} // end class
/**
* MANAGER VIEW
*/
class InstallationManagerView {
private static $m_installButton = 'install';
private static $m_uninstallButton = 'uninstall';
public function doManagerBox() {
return "
<h1>Installation manager</h1>
<p>Choose to install or uninstall:</p>
<form method='post'>
<input type='submit' name='".self::$m_installButton."' value='Install' />
<input type='submit' name='".self::$m_uninstallButton."' value='Uninstall' />
</form>";
}
public function doInstall() {
if (isset($_POST[self::$m_installButton])) {
return true;
}
}
public function doUninstall() {
if (isset($_POST[self::$m_uninstallButton])) {
return true;
}
}
}
###########################################################################################
/**
* INSTALL
* create tables
* set write permission on directory for recordings
*/
class Install {
private $m_db = null;
public function __construct(\permag\database\Database $db) {
$this->m_db = $db;
}
public function init() {
// create tables
// set write permisson to dir with recordings
if ($this->createMemberTable() &&
$this->createRecordingTable() &&
$this->setDirPermission()) {
return true;
} else {
return false;
}
}
public function createMemberTable() {
$sql = "CREATE TABLE IF NOT EXISTS ".InstallationManager::TABLE_MEMBER." (
`memberId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(30) NOT NULL,
`password` varchar(32) NOT NULL,
`email` varchar(40) NOT NULL,
PRIMARY KEY (`memberId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
try {
$pdo = $this->m_db->returnPDO();
$stmt = $pdo->prepare($sql);
$stmt->execute();
return true;
} catch (Exception $e) {
return false;
}
}
public function createRecordingTable() {
$sql = "CREATE TABLE IF NOT EXISTS ".InstallationManager::TABLE_RECORDING." (
`recordingId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`filename` varchar(29) NOT NULL,
`memberId` int(11) unsigned NOT NULL,
`toMemberId` int(10) unsigned NOT NULL,
`new` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`recordingId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
try {
$pdo = $this->m_db->returnPDO();
$stmt = $pdo->prepare($sql);
$stmt->execute();
return true;
} catch (Exception $e) {
return false;
}
}
public function setDirPermission() {
if (chmod('recs', 0777)) {
return true;
}
}
} // end class
###########################################################################################
/**
* UNINSTALL
* drop tables
*/
class Uninstall {
private $m_db = null;
public function __construct(\permag\database\Database $db) {
$this->m_db = $db;
}
public function init() {
if ($this->dropTables()) {
return true;
} else {
return false;
}
}
public function dropTables() {
$sql = "DROP TABLES ".InstallationManager::TABLE_MEMBER.", ".InstallationManager::TABLE_RECORDING."";
try {
$pdo = $this->m_db->returnPDO();
$stmt = $pdo->prepare($sql);
$stmt->execute();
return true;
} catch (Exception $e) {
return false;
}
}
} // end class