inital git commit
[phpns.git] / install / index.php
blob8ee05170a86225ec9ba62cf880a4e9d113022db6
1 <?php
3 /* Copyright (c) 2007-08 Kyle Osborn, Alec Henriksen
4 * phpns is free software; you can redistribute it and/or modify it under the
5 * terms of the GNU General Public Licence (GPL) as published by the Free
6 * Software Foundation; either version 2 of the Licence, or (at your option) any
7 * later version.
8 * Please see the GPL at http://www.gnu.org/copyleft/gpl.html for a complete
9 * understanding of what this license means and how to abide by it.
12 //check to see if it's installed already
13 include("../inc/config.php");
14 if ($globalvars['installed'] == "yes") {
15 die("Phpns seems to have been installed already. Switch the <code>\$globalvars['installed']</code> to \"no\" in <code>../inc/config.php</code>.");
18 //unset for include above, don't want any conflicts!
19 unset($databaseinfo);
20 unset($globalvars);
22 //constant proclaiming installation in progress
23 define("INSTALLING",1);
27 function db_form($data=NULL) {
28 global $error_message;
30 if (!$data['db_tableprefix']) {
31 $data['db_tableprefix'] = 'phpns_';
33 if (!$data['db_host']) {
34 $data['db_host'] = 'localhost';
37 $content = '
38 '.$error_message.'
39 <form action="index.php?step=2" method="post">
40 <div class="form">
41 <h3>Database information</h3>
43 <label for="type">Database type</label>
44 <select id="type" style="width: 400px">
45 <optgroup label="Select a database type...">
46 <option>mySQL database</option>
47 </optgroup>
48 </select>
49 <br />
50 <label for="db_host">Database host*</label>
51 <input type="text" name="db_host" value="'.$data['db_host'].'" /> (Usually "localhost")
52 <br />
54 <label for="db_user">Database user*</label>
55 <input type="text" name="db_user" value="'.$data['db_user'].'" />
56 <br />
58 <label for="db_password">DB user password</label>
59 <input type="password" name="db_password" value="'.$data['db_password'].'" />
60 <br />
62 <label for="db_name">Database name*</label>
63 <input type="text" name="db_name" value="'.$data['db_name'].'" />
64 <br />
66 <label for="db_tableprefix">Table prefix</label>
67 <input type="text" name="db_tableprefix" value="'.$data['db_tableprefix'].'" />
68 <br />
69 <h3>Admin information</h3>
70 <br />
71 <label for="username">Admin username*</label>
72 <input type="text" name="username" value="'.$data['username'].'" />
73 <br />
75 <label for="password">Admin password*</label>
76 <input type="password" name="password" value="'.$data['password'].'" />
77 <br />
79 <label for="c_password">Confirm password*</label>
80 <input type="password" name="c_password" value="'.$data['c_password'].'" />
81 </div>
83 <h4>Advanced (<a href="javascript:expand(\'adv\');">expand/collapse</a>)</h4>
84 <div id="adv" class="advanced" style="display: none;">
86 <label for="create">Create DB</label><input type="checkbox" name="create" id="create" value="create" /> Phpns will attempt to create the database from scratch.<br /><br />
87 <label for="existing">Existing DB</label><input type="checkbox" name="existing" id="existing" value="existing" /> This is an existing phpns database. Check so phpns will not erase/overwrite/append data to database.<br /><br />
88 <label for="overwrite">Overwrite DB</label><input type="checkbox" name="overwrite" id="overwrite" value="overwrite" /> Overwrite and destroy existing data in phpns database.<br />
90 </div>
92 <div class="alignr">
93 <input type="submit" id="submit" value="Continue" />
94 </div>
95 </form>
97 return $content;
101 //get step
102 $step = $_GET['step'];
104 if (!$step) {
105 $logo = '';
106 $license_txt = file_get_contents("../docs/LICENSE");
107 $content = '
108 <form action="index.php?step=1" method="post">
109 <textarea style="width: 100%; height: 200px;" readonly="readonly">
110 '.$license_txt.'
111 </textarea>
112 <br />
113 <div class="alignr">
114 <label><input type="checkbox" id="upgrade" name="upgrade" value="TRUE" />Upgrade from V1.x</label>
115 <input type="submit" id="submit" value="Continue" />
116 </div>
117 </form>
119 } elseif ($step == 1) { //step 1
120 //If upgrade is selected, then go directly to upgrade
121 if($_POST[upgrade] == TRUE){
122 header("location: upgrade.php");
125 $content = db_form();
127 } elseif ($step == 2) { //step 2 : check data.
128 //get data from post, now we'll verify...
129 $data = $_POST;
130 //set continue to TRUE, then we'll make sure everything is ok
131 $continue = TRUE;
132 foreach ($data as $key=>$value) {
133 if ($value == NULL && $key != 'db_tableprefix' && $key != 'db_password' && $key != 'existing') {
134 $continue = FALSE;
135 echo "";
136 $error_message = '<div class="warning">You need to fill out all the required fields.</div>';
140 if ($data['password'] && ($data['password'] != $data['c_password'])) {
141 $error_message .= '<div class="warning">Your passwords do not match.</div>';
142 $continue = FALSE;
143 } elseif ($data['password']) {
144 $data['password'] = sha1($data['password']);
147 if ($continue == TRUE) { //if all fields are full, we move on to verification. First, we'll do the db stuff.
148 if (!$connection = @mysql_connect($data['db_host'], $data['db_user'], $data['db_password'])) {
149 $error_message .= '<div class="warning">We could not connect to the database ('.$data['db_host'].') with the information you provided. This is most likely due to a misspelled username or password. If you are sure you provided correct information, the user you are using probably doesn\'t exist on the server you specified.</div>';
150 $continue = FALSE;
153 if ($continue == TRUE && $_POST['create'] != NULL) {
154 if (!mysql_query('CREATE DATABASE '.$data['db_name'].'', $connection)) {
155 $error_message .= '<div class="warning">Phpns tried to create a database named "'.$data['db_name'].'", but was unsuccessful. Mysql said: "'.mysql_error().'".</div>';
156 $continue = FALSE;
159 if (!@mysql_select_db($data['db_name'], $connection) && $continue == TRUE) {
160 $error_message .= '<div class="warning">We could not find the database ('.$data['db_name'].') you specified. You might have spelled the database wrong, or, you are not connected to a database (you would recieve a previous error if this was the case).</div>';
161 $continue = FALSE;
164 //if connection is active, insert data
165 if ($continue == TRUE && $_POST['existing'] == NULL) {
166 include_once("install.inc.php");
172 if ($continue == FALSE) {
173 //if there were errors, we're going to redisplay the form with errors (inside the function)
174 $content = db_form($data);
176 } elseif ($continue == TRUE) {
177 //else if we have no errors, we serialize the $data, and head over to ?step=3
178 $data = serialize($data);
179 $data = str_replace('"',"'", $data); //replace all "s with 's as to be xhtml compliant in hidden form
180 $url = str_replace('install/index.php','',$_SERVER['PHP_SELF']);
181 $content = '
182 <h3>Configuration finished</h3>
183 <p>You have filled in all the fields in the previous step successfully. Now, we need to write the configuration to the phpns config.php file, so it can be used globally across the whole system. But, before we can do this, we need you to give this script proper permissions. Please do the following:
184 <ol>
185 <li>Using your favorite FTP application, login to your website directory, and navigate to where you uploaded the phpns system ('.$url.').</li>
186 <li>Now, you need to navigate to: /inc</li>
187 <li>Select the config.php file</li>
188 <li>Change the permissions of the file so phpns can write to the file. Right click the file, and navigate to the file permissions tab/section. You should "tick" the boxes next to "read", and "write". If the server allows you to supply a numeric value, use 0777, or 777. Note, this may be unnessary; phpns might already have the correct permissions.
189 <ul>
190 <li>If you are using an FTP client, right click the config.php file and search for an option named "CHMOD". Give a value of 0777.</li>
191 </ul>
192 </li>
193 <li>Return to this installation guide, and press continue.</li>
194 </ol>
195 <form action="index.php?step=3" method="post">
196 <input type="hidden" name="config" value="'.$data.'" />
197 <div class="alignr">
198 <input type="submit" id="submit" value="Continue" />
199 </div>
200 </form>
204 } elseif ($step == 3) { //else if step 3
206 //unserialize string passed through form, get back array
207 $data = unserialize(str_replace("'",'"',stripslashes($_POST['config'])));
209 //form file contents
210 $fcontent = '<?php
212 /* Copyright (c) 2007-08 Alec Henriksen
213 * phpns is free software; you can redistribute it and/or modify it under the
214 * terms of the GNU General Public Licence (GPL) as published by the Free
215 * Software Foundation; either version 2 of the Licence, or (at your option) any
216 * later version.
217 * Please see the GPL at http://www.gnu.org/copyleft/gpl.html for a complete
218 * understanding of what this license means and how to abide by it.
221 //This is the file generated by the installation. Copy and paste this into the /inc/config.php file. If neceessary, you should edit anything needed below.
223 //The host is where the mySQL database is stored. Usually localhost.
224 $databaseinfo[\'host\'] = "'.$data['db_host'].'";
227 //The user your database is assigned to.
228 $databaseinfo[\'user\'] = "'.$data['db_user'].'";
231 //The password of your mysql user.
232 $databaseinfo[\'password\'] = "'.$data['db_password'].'";
235 //The database name that phpns will use.
236 $databaseinfo[\'dbname\'] = "'.$data['db_name'].'";
239 //The table prefix defined for each table in the database.
240 $databaseinfo[\'prefix\'] = "'.$data['db_tableprefix'].'";
243 //This should be "yes" if you have completed the phpns installation.
244 $globalvars[\'installed\'] = "yes";
247 //Activates the global debugging system for phpns. Change to "yes" to activate
248 $globalvars[\'debug\'] = "no";
250 ?>';
251 $continue = TRUE;
252 if ($filec = @fopen("../inc/config.php",'w')) { //if we can open
253 //if we can open the file for reading...
254 if (!@fwrite($filec, $fcontent)) {
255 $error_message .= '<div class="warning">We opened the config.php file, but we could not write to it. This is probably because you did not set the file permissions correctly.</div>';
256 $continue = FALSE;
258 } else {
259 $error_message .= '<div class="warning">We could not open the config.php file to edit with your configuration options. The file permissions probably are not set correctly.</div>';
260 $continue = FALSE;
263 //if problems with writing to file, give them the code to edit the file themselves.
264 if ($continue == FALSE) {
265 $content = '
266 <h3>Error writing to "inc/config.php"</h3>
267 <p>We could not edit the file with your configuration. This is probably due to wrong file permissions. You can try to set the permissions again (to 0777), and refresh this page. However, if you do not know how to set file permissions, you can open the file on the server, and replace everything in the file with the following:</p>
268 <textarea style="" readonly="readonly">'.$fcontent.'</textarea>
271 } else {
272 $content = '
273 <h3>Success</h3>
274 <p>We have successully edited the config.php file with your configuration, and created the database structure. Installation is complete, here is what you do:</p>
275 <ol>
276 <li>Open your FTP or File browser on the server</li>
277 <li>Navigate to the phpns directory, and <strong>DELETE</strong> the /install directory</li>
278 <li>Navigate to the <a href="../login.php">login page</a>, and login with your recently created admin account</li>
279 </ol>
280 <p>A copy of the config.php file is attached below for your reference.</p>
281 <textarea readonly="readonly">'.$fcontent.'</textarea>
286 include("install.tmp.php");