getMessage(); } } // create config file if(empty($errors)) { $template = file_get_contents('../config.default.php'); $base_url = ($path = trim($post['path'], '/')) == '' ? '' : $path . '/'; $index_page = ($post['clean_urls'] === false ? 'index.php' : ''); $search = array( "'host' => 'localhost'", "'username' => 'root'", "'password' => ''", "'name' => 'testcms'", // apllication paths "'base_url' => '/'", "'index_page' => 'index.php'", "'key' => ''" ); $replace = array( "'host' => '" . $post['host'] . "'", "'username' => '" . $post['user'] . "'", "'password' => '" . $post['pass'] . "'", "'name' => '" . $post['db'] . "'", // apllication paths "'base_url' => '/" . $base_url . "'", "'index_page' => '" . $index_page . "'", "'key' => '" . random(32) . "'" ); $config = str_replace($search, $replace, $template); if(file_put_contents('../config.php', $config) === false) { $errors[] = 'Failed to create config file'; } // if we have clean urls enabled let setup a // basic htaccess file is there isnt one if($post['clean_urls']) { // dont overwrite existing htaccess file if(file_exists('../.htaccess') === false) { $htaccess = file_get_contents('../htaccess.txt'); $htaccess = str_replace('# RewriteBase /', 'RewriteBase /' . $base_url, $htaccess); if(file_put_contents('../.htaccess', $htaccess) === false) { $errors[] = 'Unable to create .htaccess file. Make to create one to enable clean urls.'; } } else { $warnings[] = 'It looks like you already have a htaccess file in place, to use clean URLs please copy and paste our sample htaccess.txt file, remember to update the RewriteBase option if you have installed TestCMS in a subfolder.'; } } } // create db if(empty($errors)) { // create a unique password for our installation $password = random(8); $sql = str_replace('[[now]]', time(), file_get_contents('test.sql')); $sql = str_replace('[[password]]', crypt($password), $sql); $sql = str_replace('[[email]]', strtolower(trim($post['email'])), $sql); try { $dbh->beginTransaction(); $dbh->exec($sql); $sql= "INSERT INTO `meta` (`key`, `value`) VALUES ('sitename', ?), ('description', ?), ('theme', ?);"; $statement = $dbh->prepare($sql); $statement->execute(array($post['name'], $post['description'], $post['theme'])); $dbh->commit(); } catch(PDOException $e) { $errors[] = $e->getMessage(); // rollback any changes if($dbh->inTransaction()) { $dbh->rollBack(); } } } // output response header('Content-Type: application/json'); if(empty($errors)) { //no errors we're all gooood $response['installed'] = true; $response['password'] = $password; $response['warnings'] = $warnings; } else { $response['installed'] = false; $response['errors'] = $errors; $response['warnings'] = $warnings; } // output json formatted string echo json_encode($response);