source: subversion/trunk/roundcubemail/installer/test.php @ 1142

Last change on this file since 1142 was 1142, checked in by thomasb, 5 years ago

Add SMTP test to installer script

File size: 5.8 KB
Line 
1<form action="index.php?_step=3" method="post">
2
3<h3>Check config files</h3>
4<?php
5
6// load local config files
7$RCI->load_config();
8
9$read_main = is_readable('../config/main.inc.php');
10$read_db = is_readable('../config/db.inc.php');
11
12if ($read_main && !empty($RCI->config)) {
13  $RCI->pass('main.inc.php');
14}
15else if ($read_main) {
16  $RCI->fail('main.inc.php', 'Syntax error');
17}
18else if (!$read_main) {
19  $RCI->fail('main.inc.php', 'Unable to read file. Did you create the config files?');
20}
21echo '<br />';
22
23if ($read_db && !empty($RCI->config['db_table_users'])) {
24  $RCI->pass('db.inc.php');
25}
26else if ($read_db) {
27  $RCI->fail('db.inc.php', 'Syntax error');
28}
29else if (!$read_db) {
30  $RCI->fail('db.inc.php', 'Unable to read file. Did you create the config files?');
31}
32
33?>
34
35<h3>Check configured database settings</h3>
36<?php
37
38$db_working = false;
39if (!empty($RCI->config)) {
40    if (!empty($RCI->config['db_backend']) && !empty($RCI->config['db_dsnw'])) {
41
42        echo 'Backend: ';
43        echo 'PEAR::' . strtoupper($RCI->config['db_backend']) . '<br />';
44
45        $_class = 'rcube_' . strtolower($RCI->config['db_backend']);
46        require_once 'include/' . $_class . '.inc';
47
48        $DB = new $_class($RCI->config['db_dsnw'], '', false);
49        $DB->db_connect('w');
50        if (!($db_error_msg = $DB->is_error())) {
51            $RCI->pass('DSN (write)');
52            echo '<br />';
53            $db_working = true;
54        }
55        else {
56            $RCI->fail('DSN (write)', $db_error_msg);
57            echo '<p class="hint">Make sure that the configured database extists and that the user as write privileges<br />';
58            echo 'DSN: ' . $RCI->config['db_dsnw'] . '</p>';
59        }
60    }
61    else {
62        $RCI->fail('DSN (write)', 'not set');
63    }
64}
65else {
66    $RCI->fail('Config', 'Could not read config files');
67}
68
69// initialize db with schema found in /SQL/*
70if ($db_working && $_POST['initdb']) {
71    if (!($success = $RCI->init_db($DB))) {
72        $db_working = false;
73        echo '<p class="warning">Please try to inizialize the database manually as described in the INSTALL guide.
74          Make sure that the configured database extists and that the user as write privileges</p>';
75    }
76}
77
78// test database
79if ($db_working) {
80    $db_read = $DB->query("SELECT count(*) FROM {$RCI->config['db_table_users']}");
81    if (!$db_read) {
82        $RCI->fail('DB Schema', "Database not initialized");
83        $db_working = false;
84        echo '<p><input type="submit" name="initdb" value="Initialize database" /></p>';
85    }
86    else {
87        $RCI->pass('DB Schema');
88    }
89    echo '<br />';
90}
91
92// more database tests
93if ($db_working) {
94    // write test
95    $db_write = $DB->query("INSERT INTO {$RCI->config['db_table_cache']} (session_id, cache_key, data, user_id) VALUES (?, ?, ?, 0)", '1234567890abcdef', 'test', 'test');
96    $insert_id = $DB->insert_id($RCI->config['db_sequence_cache']);
97   
98    if ($db_write && $insert_id) {
99      $RCI->pass('DB Write');
100      $DB->query("DELETE FROM {$RCI->config['db_table_cache']} WHERE cache_id=?", $insert_id);
101    }
102    else {
103      $RCI->fail('DB Write', $RCI->get_error());
104    }
105    echo '<br />';   
106   
107    // check timezone settings
108    $tz_db = 'SELECT ' . $DB->unixtimestamp($DB->now()) . ' AS tz_db';
109    $tz_db = $DB->query($tz_db);
110    $tz_db = $DB->fetch_assoc($tz_db);
111    $tz_db = (int) $tz_db['tz_db'];
112    $tz_local = (int) time();
113    $tz_diff  = $tz_local - $tz_db;
114
115    // sometimes db and web servers are on separate hosts, so allow a 30 minutes delta
116    if (abs($tz_diff) > 1800) {
117        $RCI->fail('DB Time', "Database time differs {$td_ziff}s from PHP time");
118    }
119    else {
120        $RCI->pass('DB Time');
121    }
122}
123
124?>
125
126<h3>Test SMTP settings</h3>
127
128<p>
129Server: <?php echo $RCI->getprop('smtp_server', 'PHP mail()'); ?><br />
130Port: <?php echo $RCI->getprop('smtp_port'); ?><br />
131User: <?php echo $RCI->getprop('smtp_user', '(none)'); ?><br />
132Password: <?php echo $RCI->getprop('smtp_pass', '(none)'); ?><br />
133</p>
134
135<?php
136
137if (isset($_POST['sendmail']) && !empty($_POST['_from']) && !empty($_POST['_to'])) {
138 
139  require_once 'lib/rc_mail_mime.inc';
140  require_once 'include/rcube_smtp.inc';
141 
142  echo '<p>Trying to send email...<br />';
143 
144  if (preg_match('/^' . $RCI->email_pattern . '$/i', trim($_POST['_from'])) &&
145      preg_match('/^' . $RCI->email_pattern . '$/i', trim($_POST['_to']))) {
146 
147    $recipients = trim($_POST['_to']);
148
149    $headers = array(
150      'From' => trim($_POST['_from']),
151      'To'  => $recipients,
152      'Subject' => 'Test message from RoundCube',
153    );
154
155    $body = 'This is a test to confirm that RoundCube can send email.';
156
157    $mail_object  = new rc_mail_mime();
158    $send_headers = $mail_object->headers($headers);
159
160    $smtp_response = array();
161    $status = smtp_mail($headers['From'], $recipients,
162        ($foo = $mail_object->txtHeaders($send_headers)),
163        $body, $smtp_response);
164
165    if ($status) {
166        $RCI->pass('SMTP send');
167    }
168    else {
169        $RCI->fail('SMTP send', join('; ', $smtp_response));
170    }
171  }
172  else {
173    $RCI->fail('SMTP send', 'Invalid sender or recipient');
174  }
175}
176
177echo '</p>';
178
179?>
180
181<table>
182<tbody>
183  <tr><td><label for="sendmailfrom">Sender</label></td><td><input type="text" name="_from" value="" id="sendmailfrom" /></td></tr>
184  <tr><td><label for="sendmailto">Recipient</label></td><td><input type="text" name="_to" value="" id="sendmailto" /></td></tr>
185</tbody>
186</table>
187
188<p><input type="submit" name="sendmail" value="Send test mail" /></p>
189
190
191<p>[@todo Add tests for IMAP settings]</p>
192
193</form>
194
195<p class="warning">
196
197After completing the installation and the final tests please <b>remove</b> the whole
198installer folder from the document root of the webserver.<br />
199<br />
200
201These files may expose sensitive configuration data like server passwords and encryption keys
202to the public. Make sure you cannot access this installer from your browser.
203
204</p>
Note: See TracBrowser for help on using the repository browser.