Added general comment stubs
[ssproject1617.git] / testcms-final-anon / install / installer.php
1 <?php
2
3 /*
4 Helper functions
5 */
6 function random($length = 16) {
7 $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
8 $value = '';
9
10 for ($i = 0; $i < $length; $i++) {
11 $value .= $pool[mt_rand(0, 61)];
12 }
13
14 return $value;
15 }
16
17 /*
18 Installer
19 */
20
21 $fields = array('host', 'user', 'pass', 'db', 'name', 'description', 'theme', 'email', 'path', 'clean_urls');
22 $post = array();
23 $warnings = array();
24 $errors = array();
25
26 foreach($fields as $field) {
27 $post[$field] = isset($_POST[$field]) ? $_POST[$field] : false;
28 }
29
30 if(empty($post['db'])) {
31 $errors[] = 'Please specify a database name';
32 }
33
34 if(empty($post['host'])) {
35 $errors[] = 'Please specify a database host';
36 }
37
38 if(empty($post['name'])) {
39 $errors[] = 'Please enter a site name';
40 }
41
42 if(empty($post['theme'])) {
43 $errors[] = 'Please select a theme';
44 }
45
46 if(filter_var($post['email'], FILTER_VALIDATE_EMAIL) === false) {
47 $errors[] = 'Please enter a valid email address';
48 }
49
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;
52 }
53
54 // test database
55 if(empty($errors)) {
56 try {
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();
61 }
62 }
63
64 // create config file
65 if(empty($errors)) {
66 $template = file_get_contents('../config.default.php');
67
68 $base_url = ($path = trim($post['path'], '/')) == '' ? '' : $path . '/';
69 $index_page = ($post['clean_urls'] === false ? 'index.php' : '');
70
71 $search = array(
72 "'host' => 'localhost'",
73 "'username' => 'root'",
74 "'password' => ''",
75 "'name' => 'testcms'",
76
77 // apllication paths
78 "'base_url' => '/'",
79 "'index_page' => 'index.php'",
80 "'key' => ''"
81 );
82 $replace = array(
83 "'host' => '" . $post['host'] . "'",
84 "'username' => '" . $post['user'] . "'",
85 "'password' => '" . $post['pass'] . "'",
86 "'name' => '" . $post['db'] . "'",
87
88 // apllication paths
89 "'base_url' => '/" . $base_url . "'",
90 "'index_page' => '" . $index_page . "'",
91 "'key' => '" . random(32) . "'"
92 );
93 $config = str_replace($search, $replace, $template);
94
95 if(file_put_contents('../config.php', $config) === false) {
96 $errors[] = 'Failed to create config file';
97 }
98
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);
106
107 if(file_put_contents('../.htaccess', $htaccess) === false) {
108 $errors[] = 'Unable to create .htaccess file. Make to create one to enable clean urls.';
109 }
110 } else {
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.';
112 }
113 }
114 }
115
116 // create db
117 if(empty($errors)) {
118 // create a unique password for our installation
119 $password = random(8);
120
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);
124
125 try {
126 $dbh->beginTransaction();
127 $dbh->exec($sql);
128
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']));
132
133 $dbh->commit();
134 } catch(PDOException $e) {
135 $errors[] = $e->getMessage();
136
137 // rollback any changes
138 if($dbh->inTransaction()) {
139 $dbh->rollBack();
140 }
141 }
142 }
143
144 // output response
145 header('Content-Type: application/json');
146
147 if(empty($errors)) {
148 //no errors we're all gooood
149 $response['installed'] = true;
150 $response['password'] = $password;
151 $response['warnings'] = $warnings;
152 } else {
153 $response['installed'] = false;
154 $response['errors'] = $errors;
155 $response['warnings'] = $warnings;
156 }
157
158 // output json formatted string
159 echo json_encode($response);