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