6 function random($length = 16) {
7 $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
10 for ($i = 0; $i < $length; $i++
) {
11 $value .= $pool[mt_rand(0, 61)];
21 $fields = array('host', 'user', 'pass', 'db', 'name', 'description', 'theme', 'email', 'path', 'clean_urls');
26 foreach($fields as $field) {
27 $post[$field] = isset($_POST[$field]) ?
$_POST[$field] : false;
30 if(empty($post['db'])) {
31 $errors[] = 'Please specify a database name';
34 if(empty($post['host'])) {
35 $errors[] = 'Please specify a database host';
38 if(empty($post['name'])) {
39 $errors[] = 'Please enter a site name';
42 if(empty($post['theme'])) {
43 $errors[] = 'Please select a theme';
46 if(filter_var($post['email'], FILTER_VALIDATE_EMAIL
) === false) {
47 $errors[] = 'Please enter a valid email address';
50 if(version_compare(PHP_VERSION
, '5.3.0', '<')) {
51 $errors[] = 'TestCMS requires PHP 5.3 or newer, your current environment is running PHP ' . PHP_VERSION
;
57 $dsn = 'mysql:dbname=' . $post['db'] . ';host=' . $post['host'];
58 $dbh = new PDO($dsn, $post['user'], $post['pass']);
59 } catch(PDOException
$e) {
60 $errors[] = $e->getMessage();
66 $template = file_get_contents('../config.default.php');
68 $base_url = ($path = trim($post['path'], '/')) == '' ?
'' : $path . '/';
69 $index_page = ($post['clean_urls'] === false ?
'index.php' : '');
72 "'host' => 'localhost'",
73 "'username' => 'root'",
75 "'name' => 'testcms'",
79 "'index_page' => 'index.php'",
83 "'host' => '" . $post['host'] . "'",
84 "'username' => '" . $post['user'] . "'",
85 "'password' => '" . $post['pass'] . "'",
86 "'name' => '" . $post['db'] . "'",
89 "'base_url' => '/" . $base_url . "'",
90 "'index_page' => '" . $index_page . "'",
91 "'key' => '" . random(32) . "'"
93 $config = str_replace($search, $replace, $template);
95 if(file_put_contents('../config.php', $config) === false) {
96 $errors[] = 'Failed to create config file';
99 // if we have clean urls enabled let setup a
100 // basic htaccess file is there isnt one
101 if($post['clean_urls']) {
102 // dont overwrite existing htaccess file
103 if(file_exists('../.htaccess') === false) {
104 $htaccess = file_get_contents('../htaccess.txt');
105 $htaccess = str_replace('# RewriteBase /', 'RewriteBase /' . $base_url, $htaccess);
107 if(file_put_contents('../.htaccess', $htaccess) === false) {
108 $errors[] = 'Unable to create .htaccess file. Make to create one to enable clean urls.';
111 $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.';
118 // create a unique password for our installation
119 $password = random(8);
121 $sql = str_replace('[[now]]', time(), file_get_contents('test.sql'));
122 $sql = str_replace('[[password]]', crypt($password), $sql);
123 $sql = str_replace('[[email]]', strtolower(trim($post['email'])), $sql);
126 $dbh->beginTransaction();
129 $sql= "INSERT INTO `meta` (`key`, `value`) VALUES ('sitename', ?), ('description', ?), ('theme', ?);";
130 $statement = $dbh->prepare($sql);
131 $statement->execute(array($post['name'], $post['description'], $post['theme']));
134 } catch(PDOException
$e) {
135 $errors[] = $e->getMessage();
137 // rollback any changes
138 if($dbh->inTransaction()) {
145 header('Content-Type: application/json');
148 //no errors we're all gooood
149 $response['installed'] = true;
150 $response['password'] = $password;
151 $response['warnings'] = $warnings;
153 $response['installed'] = false;
154 $response['errors'] = $errors;
155 $response['warnings'] = $warnings;
158 // output json formatted string
159 echo json_encode($response);