Ticket #377: install.php.patch2

File install.php.patch2, 18.8 KB (added by Robin Jones, 14 years ago)

Sample Database Patch (might need a bit of work)

Line 
1Index: wifidog/install.php
2===================================================================
3--- wifidog/install.php (revision 1323)
4+++ wifidog/install.php (working copy)
5@@ -51,7 +51,30 @@
6 empty ($_REQUEST['action']) ? $action = '' : $action = $_REQUEST['action']; # The action to be done (in page)
7 empty ($_REQUEST['debug']) ? $debug = 0 : $debug = $_REQUEST['debug']; # Use for MySQL debugging
8 empty ($_REQUEST['config']) ? $config = '' : $config = $_REQUEST['config']; # Store data to be saved in config.php
9+//The path to the inital sql data directory
10+$dirpath = WIFIDOG_ABS_FILE_PATH . "../sql/initaldata/";
11 
12+
13+
14+# SQL are executed with PHP, some lines need to be commented out.
15+        $file_db_version = 'UNKNOW';
16+        $patterns[0] = '/CREATE DATABASE wifidog/';
17+        $patterns[1] = '/\\\connect/';
18+        //The following is strictly for compatibility with postgresql 7.4
19+        $patterns[2] = '/COMMENT/';
20+        $patterns[3] = '/^SET /m';
21+        $patterns[4] = '/CREATE PROCEDURAL LANGUAGE/';
22+        $patterns[5] = '/ALTER SEQUENCE/';
23+        $patterns[6] = '/::regclass/';//To fix incompatibility of postgres < 8.1 with later nextval() calling convention
24+        $replacements[0] = '-- ';
25+        $replacements[1] = '-- ';
26+        $replacements[2] = '-- ';
27+        $replacements[3] = '-- ';
28+        $replacements[4] = '-- ';
29+        $replacements[5] = '-- ';
30+        $replacements[6] = '::text';
31+
32+
33 # Security : Minimal access validation is use by asking user to retreive a random password in a local file. This prevent remote user to access unprotected installation script. It's dummy, easy to implement and better than nothing.
34 
35 # Random password generator
36@@ -61,7 +84,7 @@
37     srand(date("s"));
38     $possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39     $password = "";
40-    while (strlen($random_password) < 8) {
41+    while (strlen($random_password) < 16) {
42         $random_password .= substr($possible_charactors, rand() % (strlen($possible_charactors)), 1);
43     }
44     $fd = fopen($password_file, 'w');
45@@ -155,8 +178,8 @@
46 $pageindex = array("Welcome"=>"Welcome",
47                   "Prerequisites"=>"Prerequisites",
48                   "Permissions"=>"Permissions",
49-                  "Dependencies"=> "Dependencies",               
50-                  "Database Access"=> "Database",
51+              "Dependencies"=> "Dependencies",           
52+              "Database Access"=> "Database",
53                   "Database Connection"=>"testdatabase",
54                   "Database Initialisation"=>"dbinit",
55                   "Global Options"=>"options",
56@@ -175,7 +198,7 @@
57 print<<<EndHTML
58                </div>
59                <div id="left_area_bottom">
60-                       <p><a href="../CHANGELOG">Change Log</a><BR>
61+               <p><A HREF="#" ONCLICK="javascript: document.myform.page.value='changelog'; document.myform.submit();">Change Log</A><BR>
62                        <a href="http://dev.wifidog.org/report/10">Known issues</a></p>
63                </div>
64        </div>
65@@ -265,14 +288,13 @@
66         $configArray[$key] = $value;
67     }
68 }
69-//echo '<pre>';print_r($configArray);echo '</pre>';
70+
71 # Database connections variables
72 $CONF_DATABASE_HOST = $configArray['CONF_DATABASE_HOST'];
73 $CONF_DATABASE_NAME = $configArray['CONF_DATABASE_NAME'];
74 $CONF_DATABASE_USER = $configArray['CONF_DATABASE_USER'];
75 $CONF_DATABASE_PASSWORD = $configArray['CONF_DATABASE_PASSWORD'];
76 
77-//foreach($configArray as $key => $value) { print "K=$key V=$value<BR>"; } exit(); # DEBUG
78 
79 ###################################
80 # array (array1(name1, page1), array2(name2, page2), ..., arrayN(nameN, pageN));
81@@ -306,16 +328,8 @@
82 
83 ###################################
84 #
85-/*function debugButton() {
86- print <<<EndHTML
87- <p><INPUT TYPE="button" VALUE="Debug" ONCLICK="javascript: window.location.reload(true);"></p>
88- EndHTML;
89- } */
90-
91-###################################
92-#
93 function saveConfig($data) {
94-    print "<!-- saveConfig DATA=($data) -->\n"; # DEBUG
95+    print "<!-- saveConfig DATA=($data) -->\n";
96 
97     global $CONFIG_FILE;
98 
99@@ -328,11 +342,9 @@
100     foreach ($defineArrayToken as $nameValue) {
101         list ($name, $value) = explode('=', $nameValue);
102         $defineArray[$name] = $value; # New define value ($name and value)
103-        #print "K=$name V=$value<BR>"; # DEBUG
104     }
105 
106     foreach ($contentArray as $line) {
107-        #print "L=$line<BR>\n";
108         if (preg_match("/^define\((.+)\);/", $line, $matchesArray)) {
109             list ($key, $value) = explode(',', $matchesArray[1]);
110             $pattern = array (
111@@ -344,11 +356,8 @@
112                 ''
113                 );
114                 $key = preg_replace($pattern, $replacement, trim($key));
115-                //$value = preg_replace($pattern, $replacement, trim($value));
116 
117                 if (array_key_exists($key, $defineArray)) { // A new value is defined
118-                    #print "$key EXISTS<BR>";
119-                    #print "define => (" . $defineArray[$key] . ")<BR>";
120                     $pattern = array (
121                     "/^\\\'/",
122                     "/\\\'$/"
123@@ -358,7 +367,6 @@
124                     "'"
125                     );
126                     $value = preg_replace($pattern, $replacement, trim($defineArray[$key]));
127-                    #print "(define('$key', $value);)<BR>";
128                     fwrite($fd, "define('$key', $value);\n"); # Write the new define($name, $value)
129                 }
130                 else { // The key does not exist (no new value to be saved)
131@@ -380,8 +388,8 @@
132  *********************************************************************************/
133 switch ($page) {
134     case 'Permissions' :
135-        print "<h1>Permissions</h1>";
136-
137+        print "<h1>Folder Permissions</h1>";
138+       
139         $process_info_user_id = posix_getpwuid(posix_getuid());
140 
141         if($process_info_user_id){
142@@ -410,7 +418,7 @@
143         foreach ($dir_array as $dir) {
144             print "<tr><td>$dir</td>";
145             if (!file_exists(WIFIDOG_ABS_FILE_PATH . "$dir")) {
146-                print "<TD COLSPAN=\"2\" STYLE=\"text-align:center;\">Missing</td></tr>\n";
147+                print "<TD COLSPAN=\"2\" STYLE=\"text-align:center; background:red;\">Missing</td></tr>\n";
148                 $cmd_mkdir .= WIFIDOG_ABS_FILE_PATH . "$dir ";
149                 $cmd_chown .= WIFIDOG_ABS_FILE_PATH . "$dir ";
150                 $error = 1;
151@@ -428,10 +436,10 @@
152             print "<td>$dir_owner_username</td>";
153 
154             if (is_writable(WIFIDOG_ABS_FILE_PATH . "$dir")) {
155-                print "<td>YES</td>";
156+                print "<td STYLE='background:lime;'>YES</td>";
157             }
158             else {
159-                print "<td>NO</td>";
160+                print "<td STYLE='background:red;'>NO</td>";
161                 $cmd_chown .= WIFIDOG_ABS_FILE_PATH . "$dir ";
162                 $error = 1;
163             }
164@@ -494,9 +502,8 @@
165 
166         ###########################################
167     case 'Database' :
168-        ### TODO : Valider en javascript que les champs soumit ne sont pas vide
169-        #          Pouvoir choisir le port de la DB ???
170-        print<<< EndHTML
171+        ### TODO : allow choosing the port of the database
172+print<<< EndHTML
173 <h1>Database Access Configuration</h1>
174 <BR>
175 <table border="1">
176@@ -524,7 +531,7 @@
177             "title" => "Back",
178             "page" => "Dependencies"
179             ),
180-            )); #, array("title" => "Next", "page" => "testdatabase")));
181+            ));
182             print<<< EndHTML
183 <A HREF="#" ONCLICK="javascript: document.myform.page.value='testdatabase'; submitDatabaseValue(); document.myform.submit();" CLASS="submit">Next</A>
184 
185@@ -534,8 +541,6 @@
186             ###########################################
187     case 'testdatabase' :
188         print "<h1>Database connection</h1>";
189-        /* TODO : Tester la version minimale requise de Postgresql                */
190-
191         print "<UL><LI>Trying to open a Postgresql database connection : ";
192 
193         $conn_string = "host=$CONF_DATABASE_HOST dbname=$CONF_DATABASE_NAME user=$CONF_DATABASE_USER password=$CONF_DATABASE_PASSWORD";
194@@ -557,7 +562,6 @@
195         $postgresql_info = pg_version();
196         printf ("PostgreSQL server version: %s", $postgresql_info['server']);        print "</li>";
197 
198-        #        if ($postgresql_info['server'] > $requiredPostgeSQLVersion) { Todo : Do something }
199 
200 
201         print "</UL>";
202@@ -576,27 +580,12 @@
203             ###########################################
204     case 'dbinit' :
205         print "<h1>Database Initialisation</h1>";
206-        # SQL are executed with PHP, some lines need to be commented out.
207-        $file_db_version = 'UNKNOW';
208-        $patterns[0] = '/CREATE DATABASE wifidog/';
209-        $patterns[1] = '/\\\connect/';
210-        //The following is strictly for compatibility with postgresql 7.4
211-        $patterns[2] = '/COMMENT/';
212-        $patterns[3] = '/^SET /m';
213-        $patterns[4] = '/CREATE PROCEDURAL LANGUAGE/';
214-        $patterns[5] = '/ALTER SEQUENCE/';
215-        $patterns[6] = '/::regclass/';//To fix incompatibility of postgres < 8.1 with later nextval() calling convention
216-        $replacements[0] = '-- ';
217-        $replacements[1] = '-- ';
218-        $replacements[2] = '-- ';
219-        $replacements[3] = '-- ';
220-        $replacements[4] = '-- ';
221-        $replacements[5] = '-- ';
222-        $replacements[6] = '::text';
223-
224+       
225         $content_schema_array = file(WIFIDOG_ABS_FILE_PATH . "../sql/wifidog-postgres-schema.sql") or die("<em>Error</em>: Can not open $basepath/../sql/wifidog-postgres-schema.sql"); # Read SQL schema file
226         $content_schema = implode("", $content_schema_array);
227-        $content_data_array = file(WIFIDOG_ABS_FILE_PATH . "../sql/wifidog-postgres-initial-data.sql"); # Read SQL initial data file
228+
229+        $content_data_array = file(WIFIDOG_ABS_FILE_PATH . "../sql/initaldata/wifidog-postgres-initial-data.sql"); # Read SQL initial data file
230+
231         $content_data = implode("", $content_data_array);
232 
233         $db_schema_version = ''; # Schema version query from database
234@@ -619,25 +608,7 @@
235 
236         # Get current database schema version (if defined)
237         $schemaVersionSql = "SELECT * FROM schema_info WHERE tag='schema_version'";
238-
239-        if (!@ pg_query($connection, $schemaVersionSql)) { # The @ remove warning display
240-
241-            print "<UL><LI>Database did not exist, creating wifidog database schema : ";
242-            $content_schema = preg_replace($patterns, $replacements, $content_schema); # Comment bad SQL lines
243-            //echo "<pre>$content_schema</pre>";
244-            $result = pg_query($connection, $content_schema) or die("<em>" . pg_last_error() . "</em> <=<BR>");
245-            print "OK</li>";
246-
247-            print "<LI>Adding wifidog initial data to database: ";
248-            $content_data = preg_replace($patterns, $replacements, $content_data); # Comment bad SQL lines
249-
250-            $result = pg_query($connection, $content_data) or die("<em>" . pg_last_error() . "</em> <=<BR>");
251-            print "OK</li>";
252-            print "</UL>";
253-
254-        }
255-
256-        if ($result = @ pg_query($connection, $schemaVersionSql)) { # The @ remove warning display
257+            if ($result = @ pg_query($connection, $schemaVersionSql)) { # The @ remove warning display
258             $result_array = pg_fetch_all($result);
259             $db_shema_version = $result_array[0]['value'];
260 
261@@ -663,7 +634,58 @@
262 
263         }
264 
265-        navigation(array (
266+    if (!@ pg_query($connection, $schemaVersionSql)) { # The @ remove warning display
267+
268+            print "<UL><LI>Database did not exist, creating wifidog database schema : ";
269+            $content_schema = preg_replace($patterns, $replacements, $content_schema); # Comment bad SQL lines
270+            //echo "<pre>$content_schema</pre>";
271+            $result = pg_query($connection, $content_schema) or die("<em>" . pg_last_error() . "</em> <BR>");
272+            print "OK</li>";
273+
274+            print "<LI>Checking the wifidog initial sql data directory: ";
275+
276+            print "</li></ul><BR><H3>Please select the inital data to populate the database with: </H3><br>";
277+           
278+
279+            //Looks into the directory and returns the files, no subdirectories
280+                print "<select name='inital_db_data'>";
281+
282+                $dh = opendir($dirpath);
283+                while (false !== ($file = readdir($dh))) {
284+                //Don't list subdirectories
285+                        if (!is_dir("$dirpath/$file")) {
286+                                if (htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) == "wifidog-postgres-initial-data.sql") {
287+                                    $_selected = (' selected="selected"');
288+                                } else {#else leave it alone
289+                                    $_selected = "";
290+                                }
291+                        //Truncate the file extension and capitalize the first letter
292+                        echo '<option value="' . $file . '"' . $_selected . '>' . htmlspecialchars(ucfirst(preg_replace('/\..*$/', '', $file))) . '</option>';
293+                        }
294+                }
295+                closedir($dh);
296+            #     echo "<option value='custom'>import a different file</option>";
297+        //Close Select
298+
299+        print "</select><br><br>";
300+       
301+//OK button to continue installing
302+
303+print<<<EndHTML
304+
305+<A HREF='#' ONCLICK='datavalue(document.myform.inital_db_data.value);' CLASS='submit'>ok</A><br><br>
306+<DIV style="border:solid black;">If you would like to backup your wifidog database run the following terminal command: <Strong>/usr/bin/pg_dump -h $CONF_DATABASE_HOST -p 5432 -U $CONF_DATABASE_USER -F p -O -d -f "/save_dir/filename.sql" $CONF_DATABASE_NAME</strong>. When you reinstall WiFiDog add the file to the following directory: <strong>$dirpath</strong> </DIV>
307+<SCRIPT type="text/javascript">
308+function datavalue(data){
309+document.myform.action.value = data;
310+document.myform.page.value = 'dbinitdata';
311+document.myform.submit(); 
312+}
313+</SCRIPT>
314+EndHTML;
315+    }
316+else {
317+navigation(array (
318         array (
319             "title" => "Back",
320             "page" => "testdatabase"
321@@ -673,7 +695,46 @@
322             "page" => "options"
323             )
324             ));
325-            break;
326+
327+            }
328+
329+
330+
331+break;
332+         case 'dbinitdata' :   
333+print "<UL><LI>Adding wifidog initial data to database... ";
334+            $conn_string = "host=$CONF_DATABASE_HOST dbname=$CONF_DATABASE_NAME user=$CONF_DATABASE_USER password=$CONF_DATABASE_PASSWORD";
335+            $connection = pg_connect($conn_string) or die(); # or die("Couldn't Connect ==".pg_last_error()."==<BR>");
336+        $content_data_array = file("$dirpath$action"); # Read SQL data file
337+
338+        $content_data = implode("", $content_data_array);
339+
340+        if (preg_match("/\('schema_version', '(\d+)'\);/", $content_data, $matchesArray)) # Get schema_version from the data file
341+        $file_db_version = $matchesArray[1];
342+
343+        $contentArray = file(WIFIDOG_ABS_FILE_PATH . "include/schema_validate.php");
344+        foreach ($contentArray as $line) {
345+            if (preg_match("/^define\('REQUIRED_SCHEMA_VERSION', (\d+)\);/", $line, $matchesArray)) {
346+                $file_schema_version = $matchesArray[1];
347+            }
348+        }
349+
350+            $content_data = preg_replace($patterns, $replacements, $content_data); # Comment bad SQL lines
351+
352+            $result = pg_query($connection, $content_data) or die("<em>" . pg_last_error() . "</em> <=<BR>");
353+            print "OK</li>";
354+            print "</UL>";
355+
356+
357+ navigation(array (
358+            array (
359+            "title" => "Next",
360+            "page" => "dbinit"
361+            )
362+            ));
363+
364+break;
365+
366             ###########################################
367     case 'options' :
368         # TODO : Tester que la connection SSL est fonctionnelle
369@@ -812,8 +873,7 @@
370             ###########################################
371     case 'admin' :
372         print "<h1>Administration accounts</h1>";
373-        # TODO : Allow to create more than one admin account and list the current admin users
374-        #        Allow admin to choose to show or not is username
375+
376         empty ($_REQUEST['username']) ? $username = 'admin' : $username = $_REQUEST['username'];
377         empty ($_REQUEST['password']) ? $password = '' : $password = $_REQUEST['password'];
378         empty ($_REQUEST['password2']) ? $password2 = '' : $password2 = $_REQUEST['password2'];
379@@ -875,7 +935,7 @@
380         </tr>
381         </table>
382 
383-        <script type="text/javascript"> // TODO: check whether user already exists
384+        <script type="text/javascript">
385           function submitValue() {
386            if (document.myform.username.value == '') {
387               alert('Please enter a username');
388@@ -944,22 +1004,23 @@
389                |\   /|              _
390                |A\_/A|            z   z
391             ___|     |           ( (o) )
392-           o    6     \           z _ z
393+           o    @     \           z _ z
394            |___        \           /|
395                |        \         / |
396                 \        \_______/  |
397                 |                   |
398-                |      WIFIDOG      |
399+                |      WiFiDog      |
400                 |   Captive Portal  |
401                 |   _____________   |
402                 |  /             \  |
403                 |  |             |  |
404                 |  |             |  |
405               _/   |           _/   |
406-             ?_____|          ?_____|
407+             #_____|          #_____|
408 </pre>
409 EndHTML;
410         navigation(array(array("title" => "Back", "page" => "admin")));
411+        $auth = false;
412         break;
413         ###########################################
414     case 'review' :
415@@ -1010,6 +1071,22 @@
416                 break;
417 
418                 ###########################################
419+
420+    case 'changelog' :
421+        echo "<h1>Change log</h1>";
422+
423+print"<pre>";
424+        include WIFIDOG_ABS_FILE_PATH . '../CHANGELOG';
425+print"</pre>";
426+
427+        navigation(array (
428+        array (
429+                "title" => "Back",
430+                "page" => "Welcome"
431+                ),
432+               ));
433+               break;
434+                ###########################################
435     default :
436         $WIFIDOG_VERSION = $configArray['WIFIDOG_VERSION'];
437         print<<<EndHTML
438@@ -1020,7 +1097,7 @@
439 
440 <h3>THIS PROGRAM IS STILL IN A BETA STAGE, PLEASE REPORT ANY BUGS AT <a href="http://dev.wifidog.org">http://dev.wifidog.org</a></h3>
441 
442-<p>To Continue this installation you will need to enter the password found in the file: <em>$password_file</em> on your server's filesystem. Please enter it when prompted. This is to stop accidental or malicious activity from users gaining access to the install.php file if you dont move it from the base directory. <strong>PLEASE LEAVE THE USERNAME FIELD BLANK</strong></p>
443+<p>To Continue this installation you will need to enter the password found in the file: <em>$password_file</em> on your servers filesystem. Please enter it when prompted. This is to stop accidental or malicious activity from users gaining access to the install.php file if you dont move it from the base directory. <strong>PLEASE LEAVE THE USERNAME FIELD BLANK</strong></p>
444 
445 EndHTML;
446