json_encode($request)));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
$response = json_decode($response, true);
print json_encode(true);
die();
}
// Check to see if we're being called from CLI mode, to check if we can run PHP scripts via cli
if(isset($argv) && sizeof($argv) > 0) {
// We need to make sure we can do the things we need in CLI mode.
$results = array();
$results['mysql'] = extension_loaded("pdo_mysql");
$results['json'] = function_exists("json_encode");
$results['simplexml'] = function_exists("simplexml_load_file");
$results['pcntl'] = function_exists("pcntl_fork");
// memory limit check
$memoryLimit = ini_get("memory_limit");
// Default to true
$results['memorylimit'] = true;
// It's in bytes, unless it's using some shorthand.
if(preg_match("/^(\d+)([a-zA-Z])$/", $memoryLimit, $matches)) {
// Using shorthand, convert to bytes
if($matches[2] == 'm' || $matches[2] == 'M') {
// Megabytes
$memorySize = (1048576 * $matches[1]);
}
else if($matches[2] == 'K' || $matches[2] == 'k') {
$memorySize = (1024 * $matches[1]);
}
else if($matches[2] == 'g' || $matches[2] == 'G') {
$memorySize = (1073741824 * $matches[1]);
}
else {
// Unknown shorthand
$results['memorylimit'] = false;
}
}
else if($memoryLimit != "-1") {
$memorySize = $memoryLimit;
}
if($memoryLimit != "-1" && $memorySize < 67108864) {
// Memory set is less than 64megs. This probably won't do.
$results['memorylimit'] = false;
}
$results['memorysize'] = $memoryLimit;
if(function_exists("json_encode")) {
print(json_encode($results));
}
else {
print("fail");
}
die();
}
if(!isset($_POST['stage'])) {
$stage = "1";
}
else {
$stage = $_POST['stage'];
}
if($stage == 2) {
$error = false;
if(!isset($_POST['mysqlRootUsername'])) {
$mysqlRootUsername = 'root';
$mysqlRootPassword = '';
$mysqlHostname = 'localhost';
$mysqlUsername = '';
$mysqlPassword = '';
$mysqlDatabase = 'lilac';
$mysqlPopulate = true;
$mysqlCreateUserDatabase = false;
}
else {
$mysqlRootUsername = trim($_POST['mysqlRootUsername']);
$mysqlRootPassword = trim($_POST['mysqlRootPassword']);
$mysqlHostname = trim($_POST['mysqlHostname']);
$mysqlUsername = trim($_POST['mysqlUsername']);
$mysqlPassword = trim($_POST['mysqlPassword']);
$mysqlDatabase = trim($_POST['mysqlDatabase']);
$mysqlPopulate = trim($_POST['mysqlPopulate']);
if(isset($_POST['mysqlCreateUserDatabase'])) {
$mysqlCreateUserDatabase = true;
}
else {
$mysqlCreateUserDatabase = false;
}
// Check for required parameters
if($mysqlCreateUserDatabase) {
if(empty($mysqlRootUsername)) {
$error = "MySQL Administrator username cannot be blank if you want to create user and database.";
}
}
if(!$error) {
if(empty($mysqlHostname)) {
$error = "MySQL Hostname cannot be blank.";
}
else if(empty($mysqlUsername)) {
$error = "MySQL Username cannot be blank.";
}
else if(empty($mysqlDatabase)) {
$error = "MySQL Database cannot be blank.";
}
}
if(!$error) {
// Okay, breathe, we're going to do the grunt of the work now.
// Check to see if we need to create the user and database
if($mysqlCreateUserDatabase) {
// Attempt to connect as admin
$dbConn = @mysql_connect($mysqlHostname, $mysqlRootUsername, $mysqlRootPassword);
if(!$dbConn) {
$error = "Failed to connect to MySQL server with Administrator login.";
}
else {
if(!mysql_select_db("mysql", $dbConn)) {
$error = "Failed creating user and database. Check your Admin credentials. Error was: " . mysql_error($dbConn) . "";
}
else {
// Create database
if(!mysql_query("create database " . $mysqlDatabase, $dbConn)) {
$error = "Failed to create database. Error was: " . mysql_error($dbConn) . "";
}
else {
// Okay, db is selected, let's grant privileges.
//
// NOTE TO SELF. TICKET #10
//
// k
if(in_array(strtolower($mysqlHostname), array('127.0.0.1', 'localhost'))) {
// Assign rights to localhost
if(!mysql_query("grant all privileges on " . $mysqlDatabase . ".* to '" . $mysqlUsername . "'@localhost identified by '" . $mysqlPassword . "'")) {
$error = "Failed to create user. Error was: " . mysql_error($dbConn) . "";
}
}
else {
// Assign rights via our hostname
if(!mysql_query("grant all privileges on " . $mysqlDatabase . ".* to '" . $mysqlUsername . "'@'" . $_SERVER['SERVER_NAME'] . "' identified by '" . $mysqlPassword . "'")) {
$error = "Failed to create user. Error was: " . mysql_error($dbConn) . "";
}
}
mysql_query("flush privileges");
}
}
}
}
if(!$error && $mysqlPopulate) {
// Okay, we need to populate the database. Attempt to connect as our user.
$dbConn = @mysql_connect($mysqlHostname, $mysqlUsername, $mysqlPassword);
if(!$dbConn) {
$error = "Failed to connect to MySQL server with " . $mysqlUsername . " user.";
}
else {
// Select db.
if(!mysql_select_db($mysqlDatabase, $dbConn)) {
$error = "Failed to use " . $mysqlDatabase . " database. Check your User credentials. Error was: " . mysql_error($dbConn) . "";
}
else {
// Load the data
exec("mysql -h " . $mysqlHostname . " -u " . $mysqlUsername . " -p" . $mysqlPassword . " " . $mysqlDatabase . " < " . dirname(__FILE__) . "/sqldata/schema.sql", $output, $retVal);
if($retVal != 0) {
$error = "Failed to import database schema. Make sure the mysql binary is in the search path for the web user.";
}
else {
// Import labels
exec("mysql -h " . $mysqlHostname . " -u " . $mysqlUsername . " -p" . $mysqlPassword . " " . $mysqlDatabase . " < " . dirname(__FILE__) . "/sqldata/lilac-nagios-en-label.sql", $output, $retVal);
if($retVal != 0) {
$error = "Failed to import Nagios labels. Error was:
" . str_replace("\n", "
", $output[count($output)]);
}
else {
// Import Seed
exec("mysql -h " . $mysqlHostname . " -u " . $mysqlUsername . " -p" . $mysqlPassword . " " . $mysqlDatabase . " < " . dirname(__FILE__) . "/sqldata/seed.sql", $output, $retVal);
if($retVal != 0) {
$error = "Failed to import seed data. Error was:
" . str_replace("\n", "
", $output[count($output)]);
}
}
}
}
}
}
// Create PDO connection to perform upgrades
try {
$dbConn = new PDO("mysql:host=" . $mysqlHostname . ";dbname=" . $mysqlDatabase, $mysqlUsername, $mysqlPassword);
} catch(PDOException $e) {
$error = "Failed to connect to MySQL server with " . $mysqlUsername . " user:" . $e->getMessage();
}
if(!$error) {
if(!perform_upgrade($dbConn)) {
$error = true;
}
}
if(!$error) {
// Okay, write to the configuration file!
$conf = file_get_contents(dirname(__FILE__) . "/includes/lilac-conf.php.dist");
$conf = str_replace("%%DSN%%", "mysql:host=" . $mysqlHostname . ";dbname=" . $mysqlDatabase, $conf);
$conf = str_replace("%%USERNAME%%",$mysqlUsername, $conf);
$conf = str_replace("%%PASSWORD%%",$mysqlPassword, $conf);
$conf = str_replace("%%DATABASE%%",$mysqlDatabase, $conf);
// We have the new conf
$ret = file_put_contents(dirname(__FILE__) . "/includes/lilac-conf.php", $conf);
if($ret == false) {
$error = "Failed to write to includes/lilac-conf.php. Check that the web user can write to the includes directory and try again.";
}
$success = "Completed Database Setup.";
}
}
}
}
if(file_exists(dirname(__FILE__) . "/NOTICE")) {
// Notice exists, we should display it here.
$noticeContents = file_get_contents(dirname(__FILE__) . "/NOTICE");
$warning = $noticeContents;
}
print_header("Installer");
if($stage == 1) {
$fatalErrors = false;
// Dependency checking
print_window_header("Dependency Checks");
?>