]> dev.renevier.net Git - syp.git/blob - inc/templates_install.php
improve wizard (hand editing of settings.php is no more needed)
[syp.git] / inc / templates_install.php
1 <?php
2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
3    license. */
4 ?>
5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
6        "http://www.w3.org/TR/html4/loose.dtd">  
7 <html lang="<?php echo $lang?>">
8 <head>
9       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
10       <link rel="stylesheet" href="./media/install.css" type="text/css" />
11       <title><?php ptrans ('SYP wizard')?></title>
12       <script type="text/javascript">
13       function init () {
14         if (document.getElementById('db_host')) { 
15             document.getElementById('db_host').focus();
16             document.getElementById('db_host').select();
17         } else if (document.getElementById('admin_pass')) { 
18             document.getElementById('admin_pass').focus();
19             document.getElementById('admin_pass').select();
20         }
21       }
22       </script>
23 </head>
24 <body onload="init()">
25
26 <?php
27
28     define ("CONFIG_FILE", "./inc/settings.php");
29
30     require_once ("./inc/install_utils.php");
31
32     if (version_compare (PHP_VERSION, '5.0.0', '<')) {
33         par_error_and_leave (trans ("You need at least PHP version 5"));
34     }
35
36     function error_unwritable_config () {
37         par_error_and_leave (trans ("Cannot save config file. You need either to set inc/ writable, or use manual method. See README.txt for more informations."));
38     }
39
40     function create_install_form () {
41         if (isset ($_POST ["db_form_submit"])) {
42             $host = $_POST ["db_host"];
43             $name = $_POST ["db_name"];
44             $user = $_POST ["db_user"];
45             $prefix = $_POST ["db_prefix"];
46             $title = $_POST ["site_title"];
47         } else {
48             $host = "localhost";
49             $user = "syp";
50             $name = "syp";
51             $prefix = "syp_";
52             $title = "SYP";
53         }
54
55         print '<form method="post" action="' . $_SERVER ["REQUEST_URI"] .  '"><fieldset>' . "\n";
56         print '<legend>' . trans ("SYP configuration") . '</legend>' . "\n";
57
58         print '<div><label for="db_host" title="' .
59               trans ("address of the database server (example: localhost, db.myhost.com or 192.168.0.15).") .
60               '">' . trans ("database server hostname:") . '</label>' . "\n" .
61               '<input id="db_host" name="db_host" value="' . $host . '"></div>' . "\n";
62
63         print '<div><label for="db_name" title="' .
64               trans ("The name of the database that SYP will be installed into. The database must exist.") .
65               '">' . trans ("database name:") . '</label>' . "\n" .
66               '<input id="db_name" name="db_name" value="' . $name . '"></div>' . "\n";
67
68         print '<div><label for="db_user" title="' .
69               trans ("The username with which you connect to the database.") .
70               '">' . trans ("database user:") . '</label>' . "\n" .
71               '<input id="db_user" name="db_user" value="' . $user . '"></div>' . "\n";
72
73         print '<div><label for="db_pass" title="' .
74               trans ("The password with which you connect to the database.") .
75               '">' . trans ("database password:") . '</label>' . "\n" .
76               '<input id="db_pass" name="db_pass" type="password"></div>' . "\n";
77
78         print '<div><label for="db_prefix" title="' .
79               trans ("You can specify a table prefix. This way you can run multiple SYP installations in the same database, each with its own prefix.") .
80               '">' . trans ("tables prefix:") . '</label>' . "\n" .
81               '<input id="db_prefix" name="db_prefix" value="' . $prefix . '">' . "\n";
82
83         print '<div><label for="site_title" title="' .
84               trans ("The title you want to give to your SYP site.") .
85               '">' . trans ("site title:") . '</label>' . "\n" .
86               '<input id="site_title" name="site_title" value="' . $title . '">' . "\n";
87
88         print '<div><input id="db_form_submit" name="db_form_submit" type="submit" value="' . trans ("Start install") . '"></div>';
89         print '</fieldset></form>';
90     }
91
92     if (file_exists (CONFIG_FILE)) {
93         require_once (CONFIG_FILE);
94     } else if (isset ($_POST["db_form_submit"])) { // user has submitted form
95
96         function _unquote($gpc_str) {
97            if (!isset ($gpc_str)) {
98                return $gpc_str;
99            }
100            if (get_magic_quotes_gpc ()) {
101                 return stripslashes ($gpc_str);
102            } else {
103                return $gpc_str;
104            }
105         }
106
107         define (DBHOST, _unquote ($_POST ["db_host"]));
108         define (DBNAME, _unquote ($_POST ["db_name"]));
109         define (DBUSER, _unquote ($_POST ["db_user"]));
110         define (DBPWD, _unquote ($_POST ["db_pass"]));
111         define (DBPREFIX, _unquote ($_POST ["db_prefix"]));
112         define (SITETITLE, _unquote ($_POST ["site_title"]));
113
114         // default values
115         define (UPLOADDIR, "upload");
116         define (THUMBSDIR, "upload/_thumbs");
117     } else {
118         if (!is_writable (dirname (CONFIG_FILE))) {
119             error_unwritable_config ();
120         }
121
122         create_install_form ();
123         leave ();
124     }
125
126     require_once ("./inc/db/mysql.php");
127     require_once ("./inc/utils.php");
128
129     try {
130         $connection->connect (DBHOST, DBUSER, DBPWD, DBNAME, DBPREFIX);
131     } catch (Exception $e) {
132         switch ($e->getMessage ()) {
133             case anydbConnection::err_driver_unavailable:
134                 par_error ($connection->getdbname() . ': ' . trans ('not supported'));
135                 break;
136             case anydbConnection::err_connection:
137                 par_error (trans ('Could not connect to database.'));
138                 break;
139             case anydbConnection::err_unknown_database:
140                 par_error (trans ('Database does not exist.'));
141                 break;
142             default:
143                 par_error (trans ('Unknown error when connecting to database.'));
144                 break;
145         }
146
147         if (isset ($_POST ["db_form_submit"])) {
148             // user had submited database informations. They seem to be wrong.
149             // Ask again.
150             create_install_form ();
151         }
152         leave ();
153     }
154
155     // we can connect to table. If config file does not exist, try to create it now.
156     if (!file_exists (CONFIG_FILE)) {
157         $handle = fopen ("./inc/settings.php.in", "r");
158         $lines = array();
159         if ($handle) {
160             while (!feof ($handle)) {
161                 $line = fgets ($handle, 4096);
162                 foreach (array ("DBHOST", "DBNAME", "DBUSER", "DBPWD", "DBPREFIX", "SITETITLE") as $value) {
163                     $pattern = "(define\s+\(\s*\"$value\"\s*,\s*\")[^\"]*(\"\s*\)\s*;)";
164                     if (preg_match( "/$pattern/", $line, $match )) {
165                         $line = $match[1] . addslashes (constant ($value)) . $match[2];
166                         break;
167                     }
168                 }
169                 array_push ($lines, $line);
170             }
171             fclose ($handle);
172         } else {
173             par_error_and_leave (trans ('Could not read <code>inc/settings.php.in</code>'));
174         }
175
176         $handle = fopen(CONFIG_FILE, 'w');
177         if (!$handle) {
178             error_unwritable_config ();
179         }
180         fwrite ($handle, join($lines));
181         par_success (trans ('Config file created'));
182     } else {
183         par_success (trans ('Config file exists'));
184     }
185
186     try {
187         $users_table_exists = $connection->users_table_exists ();
188     } catch(Exception $e) {
189         par_error_and_leave (trans ('Unknown error when checking user table.'));
190     }
191
192     if ($users_table_exists) {
193         par_success (trans ('Found user table.'));
194     } else {
195         if (isset($_POST ["admin_pass"])) {
196             try {
197                 $connection->create_users_table (true);
198             } catch (Exception $e) {
199                 par_error_and_leave (trans ('Error when creating user table.'));
200             }
201             par_success (trans ('User table created.'));
202             try {
203                 $connection->setpwd ("admin", $_POST ["admin_pass"]);
204             } catch (Exception $e) {
205                 par_error_and_leave (trans ('Error when initializing password.'));
206             }
207             par_success (trans ('Admin password initialized.'));
208
209         } else {
210             print ('<form class="center" method="post" action="">
211                     <label for="admin_pass">' . trans ("choose admin password") . '</label>
212                     <input id="admin_pass" name="admin_pass" type="password">
213                     <input type="submit">
214                  </form>');
215             leave ();
216         }
217     }
218
219     try {
220         $items_table_exists = $connection->items_table_exists ();
221     } catch (Exception $e) {
222         par_error_and_leave (trans ('Unknown error when checking data table.'));
223     }
224     if ($items_table_exists) {
225         par_success (trans ('Found data table.'));
226     } else {
227         try {
228             $connection->create_items_table (true);
229         } catch (Exception $e) {
230             par_error_and_leave (trans ('Error when creating data table.'));
231         }
232         par_success (trans ('Data table created.'));
233     }
234
235     safe_create_writable_dir (UPLOADDIR);
236     safe_create_writable_dir (getthumbsdir ());
237
238     if (!function_exists ("gd_info")) {
239         par_warn (trans ('It looks like GD extension is not installed.'));
240     }
241
242     par_success (trans ('SYP is installed. You can now go to <a href="admin.en.php">admin area</a>'));
243 ?>
244
245 </body>
246 </html>