-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.php
371 lines (327 loc) · 11.4 KB
/
install.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/*
* Frac
* Copyright (c) 2009 Frac Development Team
*
* See COPYING for license conditions.
*/
define("IN_FRAC_", true);
define("IN_FWORK_", true);
// Verify PHP version.
if(!defined('PHP_VERSION_ID'))
{
$version = explode('.',PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
if(PHP_VERSION_ID < 50000)
die('Frac requires PHP 5.');
// Perform some extension sanity checks.
// Though if you don't have these, your PHP install is messed up.
if(!function_exists('spl_autoload_register'))
die('Frac requires the Standard PHP Library extension to be loaded.');
if(!function_exists('session_start'))
die('Frac requires PHP Session support to be enabled.');
if(!class_exists('PDO'))
die('Frac requires the PHP Data Objects extension to be loaded.');
// Find an action.
if(!isset($_GET['do']))
{
$_GET['do'] = 'step1';
}
// Do the action.
switch($_GET['do'])
{
case 'step1':
doCreateConfig();
die;
case 'step2':
if($errors = doWriteConfig())
doCreateConfig($errors);
die;
case 'step3':
doPopulateDatabase();
die;
default:
die;
}
function doCreateConfig($error = array())
{
if(file_exists("config.php"))
{
header("Location: " . $_SERVER['SCRIPT_NAME'] . "?do=step3"); return;
}
$flash = '';
if(is_array($error) && (count($error) > 0))
{
$flash = '<div style="border: 1px solid black; padding: 4px 4px 4px 4px;"><span style="color: red; font-size: 110%; font-weight: bold;">There were one or more errors with your configuration:</span><ul>';
foreach($error as $err)
{
$flash .= '<li>' . $err . '</li>';
}
$flash .= '</ul></div>';
}
echo <<<EOS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Frac :: Install</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h2>Frac Installer</h2>
$flash
<p>You have not yet created a configuration file. Please create one from <code>config.php.dist</code> or use the form below.</p>
<form action="install.php?do=step2" method="post">
<h3>Database Options</h3>
EOS;
_printInputField('sqlHost', 'Hostname:', 'The host running the database. This is usually <code>localhost</code>.', 'text', array('size' => 30, 'value' => 'localhost'));
_printInputField('sqlUser', 'Username:', 'The SQL username.', 'text', array('size' => 30));
_printInputField('sqlPass', 'Password:', 'The SQL password.', 'password', array('size' => 30));
_printInputField('sqlDb', 'Database Name:', 'The name of the SQL database. The user above must have read/write access.', 'text', array('size' => 30));
_printInputField('sqlPrefix', 'Database Prefix:', 'The database prefix. When in doubt, keep as default.', 'text', array('size' => 30, 'value' => 'frac_'));
$pdoDrivers = array();
foreach(PDO::getAvailableDrivers() as $driver)
$pdoDrivers[$driver] = $driver;
ksort($pdoDrivers);
_printDropDown('sqlDriver', 'Database Driver:', 'The driver to use when connecting to the database.', $pdoDrivers);
/* // NO SITE OPTIONS ANYMORE
echo '<h3>Site Options</h3>';
_printYesNo('siteGzip', 'Use GZIP Compression:', 'Choosing "yes" here enables the option to use GZIP compression, decreasing bandwidth but increasing CPU usage. This is only used if the browser supports it.');
_printYesNo('siteGentime', 'Display Generation Time:', 'Choosing "yes" here will display generation time statistics as an HTML comment.');
*/
echo <<<EOS
<br />
<input type="submit" name="button" value="Generate Config" />
</form>
</body>
</html>
EOS;
}
function doWriteConfig()
{
// Initialize errors array.
$errors = array();
// Check to make sure an existing config doesn't exist.
if(file_exists('config.php'))
die('An existing configuration file exists, this script will not overwrite a previous configuration. If you wish to use this script, please remove the configuration file.'); // this will only happen if you try to access install.php?do=step2 by hand. If you do, you sir, are an idiot.
$configTmpl = array(
'sqlHost' => 'localhost',
'sqlUser' => null,
'sqlPass' => null,
'sqlDb' => null,
'sqlPrefix' => 'frac_',
'sqlDriver' => null,
// 'siteGzip' => true,
// 'siteGentime' => true,
);
foreach($configTmpl as $key => &$value)
{
$value = urldecode($_POST[$key]);
}
// First bad test. :\
if(count($errors) > 0)
return $errors;
// Construct the config array.
$configArray = array( 'database' => array( 'dsn' => "{$configTmpl['sqlDriver']}://{$configTmpl['sqlUser']}:{$configTmpl['sqlPass']}@{$configTmpl['sqlHost']}/{$configTmpl['sqlDb']}",
'prefix' => $configTmpl['sqlPrefix']
),
// 'site' => array( 'gzip' => $configTmpl['siteGzip'],
// 'gentime' => $configTmpl['siteGentime']
// )
);
// Test various things.
try {
$pdoTest = @new PDO(sprintf('%s:host=%s;port=%s;dbname=%s', $configTmpl['sqlDriver'], $configTmpl['sqlHost'], 3306, $configTmpl['sqlDb']), $configTmpl['sqlUser'], $configTmpl['sqlPass']);
} catch (PDOException $e) {
$errors[] = 'Unable to connect to the database: ' . $e->getMessage();
}
// Last chance for something to go wrong.
if(count($errors) > 0)
return $errors;
// Dump to string.
$config = "<?php\n\nif(!defined('IN_FRAC_')) die('This file cannot be invoked directly.');\n\n\$config = ";
$config .= var_export($configArray, true);
$config .= ";";
$confFile = @fopen('config.php', 'w');
if(!$confFile)
{
echo <<<EOS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Frac :: Install</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h2>Frac Installer</h2>
<p>I was unable to open the configuration file for writing. Please create the file <code>config.php</code> in the frac root directory with the following contents:</p>
<textarea rows="25" cols="125">$config</textarea>
<p>When you have finished, proceed to the <a href="install.php?do=step3">final step</a>.</p>
</body>
</html>
EOS;
}
else
{
fwrite($confFile, $config);
fclose($confFile);
echo <<<EOS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Frac :: Install</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="refresh" content="3;url=install.php?do=step3" />
</head>
<body>
<h2>Frac Installer</h2>
<p>Configuration written successfully, redirecting you to the final step...</p>
<p><a href="install.php?do=step3">Click here if your browser does not redirect you</a></p>
</body>
</html>
EOS;
}
return false;
}
function doPopulateDatabase()
{
// Check for a config file, if it doesn't exist go through the steps to create it.
if(!file_exists(dirname(__FILE__) . '/config.php'))
{
header("Location: " . $_SERVER["SCRIPT_NAME"] . "?do=step1");
return;
}
require_once(dirname(__FILE__) . "/config.php");
require_once(dirname(__FILE__) . "/fwork/lib/Doctrine/Doctrine.php");
spl_autoload_register(array("Doctrine", "autoload"));
Doctrine_Manager::connection($config["database"]["dsn"]);
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_TBLNAME_FORMAT, $config["database"]["prefix"] . "%s");
Doctrine::createTablesFromModels(dirname(__FILE__) . "/models");
$time = date("Y-m-d H:i:s");
// roles
$role_admin = new Role();
$role_admin->name = "Admin";
$role_admin->auth = 0xFFFFFFFF;
$role_admin->created = $time;
$role_admin->save();
$role_staff = new Role();
$role_staff->name = "Staff";
$role_staff->auth = 0x0007C700;
$role_staff->created = $time;
$role_staff->save();
$role_guest = new Role();
$role_guest->name = "Guest";
$role_guest->auth = 0x00000000;
$role_guest->created = $time;
$role_guest->save();
// staff
$staff = new Staff();
$staff->nickname = "admin";
$staff->setPassword("admin");
$staff->Role = $role_admin;
$staff->admin = true;
$staff->comment = "Administrator";
$staff->created = $time;
$staff->save();
// task types
$tasktypes = array(
'Raw Cap',
'Translate',
'Time',
'Translation Check',
'Typeset',
'Edit',
'Encode',
'Quality Check',
'Karaoke',
'Miscellaneous',
'Translate Signs',
'Release',
);
foreach ($tasktypes AS $key => $name)
{
$tasktype = new TaskType();
$tasktype->name = $name;
$tasktype->created = $time;
$tasktype->save();
$tasktypes[$key] = $tasktype;
}
// template
$template = new Template();
$template->name = "Default";
$template->model =
$tasktypes[0]->id . ":0->" . $tasktypes[1]->id . ":0; " .
$tasktypes[0]->id . ":0->" . $tasktypes[10]->id . ":0; " .
$tasktypes[10]->id . ":0->" . $tasktypes[4]->id . ":0; " .
$tasktypes[4]->id . ":0->" . $tasktypes[3]->id . ":0; " .
$tasktypes[1]->id . ":0->" . $tasktypes[2]->id . ":0; " .
$tasktypes[1]->id . ":0->" . $tasktypes[3]->id . ":0; " .
$tasktypes[3]->id . ":0->" . $tasktypes[5]->id . ":0; " .
$tasktypes[5]->id . ":0->" . $tasktypes[6]->id . ":0; " .
$tasktypes[6]->id . ":0->" . $tasktypes[7]->id . ":0; " .
$tasktypes[7]->id . ":0->" . $tasktypes[11]->id . ":0; " .
$tasktypes[2]->id . ":0->" . $tasktypes[5]->id . ":0; ";
$template->created = $time;
$template->save();
// settings
$settings = array(
'site.gzip' => true,
'site.gentime' => true
);
foreach ($settings AS $name => $value)
{
$setting = new Setting();
$setting->name = $name;
$setting->value = $value;
$setting->save();
unset($setting);
}
echo <<<EOS
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Frac :: Install</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h2>Frac Installer</h2>
<p>Table data populated successfully. You may now proceed to your <a href="./">Frac installation</a>. The username and password are "admin".</p>
</body>
</html>
EOS;
}
// =============================================
// =============================================
function _printInputField($name, $label, $description, $inputType = 'text', $extra = array())
{
echo '<label for="' . $name . '">' . $label . '</label> ';
echo '<input type="' . $inputType . '" name="' . $name . '"';
foreach($extra as $key => $value)
{
echo " $key=\"$value\"";
}
echo ' /> ' . $description . "<br />\n";
}
function _printDropDown($name, $label, $description, $elements, $default = 0)
{
echo '<label for="' . $name . '">' . $label . '</label> ';
echo '<select name="' . $name . '">';
$i = 0;
foreach($elements as $key => $value)
{
echo '<option value="' . $key . '" ' . ($default === $i++ ? 'selected="selected" ' : '') . '>' . $value . '</option>';
}
unset($i);
echo '</select> ' . $description . "<br />\n";
}
function _printYesNo($name, $label, $description, $default = true)
{
echo '<label for="' . $name . '">' . $label . '</label> ';
echo '<input type="radio" name="' . $name . '" value="yes" ' . ($default ? 'checked="checked" ' : '') . '/> Yes ';
echo '<input type="radio" name="' . $name . '" value="no" ' . (!$default ? 'checked="checked" ' : '') . '/> No';
echo ' ' . $description . "<br />\n";
}