-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinstall.php
119 lines (105 loc) · 3.69 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
<?php
/**
* PHP Rocker - Install
* ---------------------------------
* Load this file in the console to install Rocker (php -f install.php)
*
* @package rocker/server
* @author Victor Jonsson (http://victorjonsson.se)
* @license MIT license (http://opensource.org/licenses/MIT)
*/
// Only accessible via command line
if( !empty($_SERVER['REMOTE_ADDR']) ) {
die('cli only...');
}
$app_path = getcwd().'/';
$rocker_path = __DIR__.'/';
// Copy files into place
$files = array('index.php file'=>'index.php', 'htaccess file'=>'.htaccess', 'config file'=>array('config-sample.php', 'config.php'), 'console file'=>'console');
foreach($files as $desc => $file) {
if( is_array($file) ) {
list($from_file, $to_file) = $file;
} else {
$from_file = $file;
$to_file = $file;
}
if( file_exists($app_path . $to_file) ) {
fwrite(STDOUT, '* '.$desc.' file already exist in app path, you may want to copy vendor/rocker/server/'.$from_file.' manually to your application'.PHP_EOL);
} else {
copy($rocker_path.$from_file, $app_path.$to_file) or die('Unable to copy vendor/rocker/server/'.$from_file.' to '.$app_path.$to_file);
}
}
// Load and validate config
$config = require $app_path.'config.php';
if( !is_array($config) ) {
fwrite(STDOUT, '\033[31m! config.php seems corrupt, Rocker expects the file to return an array, but it does not\033[0m'.PHP_EOL);
die;
}
else {
// Check that we have edited the config
$db_params = array('host', 'dbname', 'username');
foreach($db_params as $param) {
if( empty($config['application.db'][$param]) ) {
fwrite(STDOUT, "\033[31m! Config paramater [application.db][".$param.'] is empty.'.PHP_EOL.'Have you updated '.$app_path."config.php with your own database settings?\033[0m".PHP_EOL);
die;
}
}
}
// Load cli utilities and vendor libraries
require $app_path.'vendor/autoload.php';
if( function_exists('\cli\register_autoload') ) {
// old cli must be autoladed using this function
\cli\register_autoload();
}
// Shorthand for \cli\line()
if( !function_exists('_') ) {
function _($str) { \cli\line($str); }
}
// Check that we can connect
$db = \Rocker\Object\DB::instance($config['application.db']);
try {
$tables = $db->executeQuery('SHOW TABLES')->fetchAll();
} catch(\Exception $e) {
_('%rERROR: Database connection failed%n');
_($e->getMessage());
return;
}
// Run class installations
foreach($config['application.install'] as $class) {
/* @var \Rocker\Utils\InstallableInterface $obj */
$obj = new $class($db);
if( $obj->isInstalled() ) {
_('* '.$class.' already installed');
} else {
$obj->install();
_('- installed '.$class);
}
}
$userFactory = new \Rocker\Object\User\UserFactory($db);
// todo: check if it exists an admin
$hasAdmin = $userFactory->metaSearch(array('admin'=>1))->getNumMatching() > 0;
// Ask for e-mail
_('## Create admin user');
while( empty($email) ) {
$email = Rocker\Console\Utils::promptAllowingEmpty('E-mail');
if( empty($email) && !$hasAdmin ) {
_('%rYou must create an admin user%n');
}
elseif( empty($email) && $hasAdmin ) {
$email = 'skip';
}
else {
if( filter_var($email, FILTER_VALIDATE_EMAIL) === false ) {
_('%rNot a valid e-mail%n');
$email = null;
}
}
}
// Create admin user
if( $email != 'skip' ) {
$nick = \cli\prompt('Nick name');
$password = \Rocker\Console\Utils::promptPassword('Password: ');
$user = $userFactory->createUser($email, $nick, $password);
$userFactory->setAdminPrivileges($user, true);
}
_('%gRocker Server v'.\Rocker\Server::VERSION.' was successfully installed :) %n');