forked from flack/openpsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_init.php
187 lines (168 loc) · 5.94 KB
/
quick_init.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
<?php
class openpsa_installer
{
protected $_project_name;
protected $_user;
public function __construct($args)
{
if (!extension_loaded('midgard2'))
{
$this->fail("Midgard2 is not installed in your PHP environment.\n");
}
else if (version_compare(mgd_version(), '10.05.4', '<='))
{
$this->fail('OpenPSA requires midgard 10.05.5 or later (found version: ' . mgd_version() . ')');
}
if (!class_exists('midgard_topic'))
{
$iterator = new DirectoryIterator('./schemas');
foreach ($iterator as $fileinfo)
{
if ( $fileinfo->isFile()
&& strpos($fileinfo->getFilename(), 'xml'))
{
//@todo: how to determine correct schema dir?
$this->_link_file('schemas', $fileinfo->getFilename(), '/usr/share/midgard2/schema');
}
}
$this->output('Schemas linked. Re-run installer to continue');
exit(0);
}
if (empty($argv[1]))
{
$this->_project_name = $this->prompt('Enter project name', 'openpsa');
}
else
{
$this->_project_name = $argv[1];
}
}
public function fail($message, $error_code = 1)
{
$this->output($message);
exit($error_code);
}
public function output($message)
{
echo $message . "\n";
}
public function prompt($message, $default = null, array $options = array())
{
if (!empty($options))
{
foreach ($options as $i => $option)
{
if ($option == $default)
{
$options[$i] = strtoupper($option);
}
else
{
$options[$i] = strtolower($option);
}
}
$message .= ' [' . implode('|', $options) . ']';
}
else if (null !== $default)
{
$message .= ' [' . $default . ']';
}
$this->output($message);
$handle = fopen('php://stdin', 'r');
$input = trim(fgets($handle));
if ( empty($input)
&& null !== $default)
{
return $default;
}
return $input;
}
protected function _check_dir($directory)
{
if (null === $this->_user)
{
$this->_user = $this->prompt('Set permissions for', 'www-data');
}
if ( !is_dir($directory)
&& !mkdir($directory)
&& !chown($directory, $this->_user))
{
$this->fail("Failed to create directory " . $directory);
}
}
protected function _link_file($path, $target, $link_directory)
{
$this->_check_dir($link_directory);
if ( !file_exists($link_directory . '/' . $target)
&& !link(dirname(__FILE__) . '/' . $path . '/' . $target, $link_directory . '/' . $target))
{
$this->fail("Failed to create link " . $link_directory . '/' . $target);
}
}
public function run()
{
$config_file = "/etc/midgard2/conf.d/" . $this->_project_name;
if (file_exists($config_file))
{
switch ($this->prompt($config_file . " already exists, override?", 'n', array('y', 'n')))
{
case 'y':
unlink($config_file);
$config = $this->_create_config($config_file);
break;
default:
$config = new midgard_config();
if (!$config->read_file($this->_project_name, false))
{
$this->fail('Could not read config file ' . $config_file);
}
}
}
else
{
$config = $this->_create_config($config_file);
}
// Open a DB connection with the config
$midgard = midgard_connection::get_instance();
if (!$midgard->open_config($config))
{
$this->fail("Failed to open Midgard database connection to {$this->_project_name}: " . $midgard->get_error_string());
}
require_once 'tools/bootstrap.php';
openpsa_prepare_database($config);
openpsa_prepare_topics();
}
private function _create_config($config_file)
{
$this->_check_dir('/var/lib/' . $this->_project_name);
$this->_check_dir('/var/cache/' . $this->_project_name);
$this->_link_file('config', 'midgard_auth_types.xml', '/var/lib/' . $this->_project_name . '/share');
$this->_link_file('config', 'MidgardObjects.xml', '/var/lib/' . $this->_project_name . '/share');
$this->_check_dir('/var/lib/' . $this->_project_name . '/share/views');
$this->_check_dir('/var/lib/' . $this->_project_name . '/rcs');
// Create a config file
$config = new midgard_config();
$config->dbtype = 'SQLite';
$config->database = $this->_project_name;
$config->dbdir = '/var/lib/' . $this->_project_name;
$config->blobdir = '/var/lib/' . $this->_project_name . '/blobs';
$config->sharedir = '/var/lib/' . $this->_project_name . '/share';
$config->vardir = '/var/lib/' . $this->_project_name;
$config->cachedir = '/var/cache/' . $this->_project_name;
$config->logfilename = '/var/log/' . $this->_project_name . '/midgard.log';
$config->loglevel = 'debug';
if (!$config->save_file($this->_project_name, false))
{
$this->fail("Failed to save " . $config_file);
}
if (!chown($config_file, $this->_user))
{
$this->fail("Failed to set permissions to " . $this->_user . ' on ' . $config_file);
}
$this->output("Configuration file " . $config_file . " created.");
return $config;
}
}
$installer = new openpsa_installer($argv);
$installer->run();
?>