Update due to significant improvements in new.php.
[openemr.git] / setup.php
blob4b93ab8b39d34e599b4a6c8a70dd6ff17f26d188
1 <?php
2 //required for normal operation because of recent changes in PHP:
3 extract($_GET);
4 extract($_POST);
5 //turn off PHP compatibility warnings
6 ini_set("session.bug_compat_warn","off");
8 $url = "";
9 $dumpfile = "sql/database.sql";
10 $icd9 = "sql/icd9.sql";
11 $conffile = "library/sqlconf.php";
12 $upgrade = 0;
13 $defhost = 'localhost';
14 $state = $_POST["state"];
16 include_once($conffile);
18 <HTML>
19 <HEAD>
20 <TITLE>OpenEMR Setup Tool</TITLE>
21 <LINK REL=STYLESHEET HREF="interface/themes/style_blue.css">
22 </HEAD>
23 <BODY>
25 <span class="title">OpenEMR Setup</span>
26 <br><br>
27 <span class="text">
29 <?php
30 if (ini_get('register_globals')) {
31 echo "It appears that you have register_globals enabled in your php.ini\n" .
32 "configuration file. This causes unacceptable security risks. You must\n" .
33 "turn it off before continuing with installation.\n";
34 exit();
38 <?php
39 if ($state == 5) {
42 <p>Congratulations! OpenEMR is now successfully installed.
44 <ul>
45 <li>Please Edit the 'interface/globals.php' file now to specify the correct
46 URL paths, and to select a theme.</li>
47 <li>Please make sure that the two folders underneath
48 'openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/'
49 exist and are writable by the web server. The two subdirectories are
50 'compiled' and 'cache'.<br>
51 Try "chown apache:apache -R openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/compiled"
52 and
53 "chown apache:apache -R openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/cache".
54 (If either subdirectory doesn't exist, create it first then do the chown above).<br>
55 The user name and group of apache may differ depending on your OS, i.e.
56 for Debian they are www-data and www-data.</li>
57 </ul>
58 <p>
59 In order to take full advantage of the documents capability you
60 must give your web server permissions on the document storage
61 directory. Try "chown apache:apache -R openemrwebroot/documents"
62 and then "chmod g+w openemrwebroot/documents".
63 You must also make sure your PHP installation (normally set in
64 your php.ini file) has "file_uploads enabled", that
65 "upload_max_filesize" is appropriate for your use and that
66 "upload_tmp_dir" is set to a correct value if the default of
67 "/tmp" won't work on your system.
68 </p>
69 <p>
70 There's much information and many extra tools bundled within the OpenEMR
71 installation directory. Please refer to openemr/Documentation.
72 <br>Many forms and other useful scripts can be found at openemr/contrib.
73 <br>OpenEMR now comes with optional GACL support, a fine grained access control
74 system. Please refer to openemr/Documentation/README.phpgacl for -easy-
75 installation.
76 </p>
77 <p>
78 Reading openemr/includes/config.php and openemr/interface/globals.php is a good
79 idea.
80 </p>
81 <p>
82 To ensure a consistent look and feel through out the application
83 using <a href='http://www.mozilla.org/products/firefox/'>Firefox</a>
84 is recommended.
85 </p>
86 <p>
87 <b>The initial OpenEMR user is "admin" and the password is "pass".</b>
88 You should change this password!
89 </p>
90 <p>
91 <a href='./'>Click here to start using OpenEMR. </a>
92 </p>
95 exit();
99 <?php
101 $server = $_POST["server"];
102 $port = $_POST["port"];
103 $dbname = $_POST["dbname"];
104 $login = $_POST["login"];
105 $pass = $_POST["pass"];
106 $loginhost = $_POST["loginhost"];
107 $rootpass = $_POST["rootpass"];
110 if ($config == 1) {
111 echo "OpenEMR is already configured. If you wish to re-configure the SQL server, edit $conffile, or change the 'config' variable to 0, and re-run this script.<br>\n";
113 else {
114 switch ($state) {
116 case 1:
117 echo "<b>Step $state</b><br><br>\n";
118 echo "Now I need to know whether you want me to create the databases on my own or if you have already created the database for me to use. If you are upgrading, you will want to select the latter function. For me to create the databases, you will need to supply the MySQL root password.\n
119 <span class='title'> <br />NOTE: clicking on \"Continue\" may delete or cause damage to data on your system. Before you continue please backup your data.</span>
120 <br><br>\n
121 <FORM METHOD='POST'>\n
122 <INPUT TYPE='HIDDEN' NAME='state' VALUE='2'>\n
123 <INPUT TYPE='RADIO' NAME='inst' VALUE='1' checked>Have setup create the databases<br>\n
124 <INPUT TYPE='RADIO' NAME='inst' VALUE='2'>I have already created the databases<br>\n
125 <br>\n
126 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
127 break;
129 case 2:
130 echo "<b>Step $state</b><br><br>\n";
131 echo "Now you need to supply the MySQL server information.
132 <br><br>
133 <FORM METHOD='POST'>
134 <INPUT TYPE='HIDDEN' NAME='state' VALUE='3'>
135 <INPUT TYPE='HIDDEN' NAME='inst' VALUE='$inst'>
136 <TABLE>\n
137 <TR><TD><font color='red'>SERVER:</font></TD></TR>
138 <TR><TD><span class='text'>Server Host: </span></TD><TD><INPUT TYPE='TEXT' VALUE='$defhost' NAME='server' SIZE='30'><span class='text'>(This is the IP address of the machine running MySQL)</span><br></TD></TR>
139 <TR><TD><span class='text'>Server Port: </span></TD><TD><INPUT TYPE='TEXT' VALUE='3306' NAME='port' SIZE='30'><span class='text'>(The default port for MySQL is 3306)</span><br></TD></TR>
140 <TR><TD><span class='text'>Database Name: </span></TD><TD><INPUT TYPE='TEXT' VALUE='openemr' NAME='dbname' SIZE='30'><span class='text'>(This is the name of the OpenEMR database - 'openemr' is the recommended)</span><br></TD></TR>
141 <TR><TD><span class='text'>Login Name: </span></TD><TD><INPUT TYPE='TEXT' VALUE='openemr' NAME='login' SIZE='30'><span class='text'>(This is the name of the OpenEMR login name - 'openemr' is the recommended)</span><br></TD></TR>
142 <TR><TD><span class='text'>Password: </span></TD><TD><INPUT TYPE='PASSWORD' VALUE='' NAME='pass' SIZE='30'><span class='text'>(This is the Login Password for when PHP accesses MySQL - it should be at least 8 characters long and composed of both numbers and letters)</span><br></TD></TR>\n";
143 if ($inst != 2) {
144 echo "<TR><TD><font color='red'>CLIENT:</font></TD></TR>";
145 echo "<TR><TD><span class='text'>User Hostname: </span></TD><TD><INPUT TYPE='TEXT' VALUE='$defhost' NAME='loginhost' SIZE='30'><span class='text'>(This is the IP address of the server machine running Apache and PHP - if you are setting up one computer, this is the same as the Server Host above)</span><br></TD></TR>
146 <TR><TD><span class='text'>Root Pass: </span></TD><TD><INPUT TYPE='PASSWORD' VALUE='' NAME='rootpass' SIZE='30'><span class='text'>(This is your MySQL root password. For localhost, it is usually ok to leave it blank.)</span><br></TD></TR>\n";
148 echo "<TR><TD><font color='red'>USER:</font></TD></TR>";
149 echo "<TR><TD COLSPAN=2></TD></TR>
150 <TR><TD><span class='text'>Initial User:</span></TD><TD><INPUT SIZE='30' TYPE='TEXT' NAME='iuser' VALUE='admin'><span class='text'>(This is the user that will be created for you. It will be an authorized user, so it should be for a Doctor or other Practitioner)</span></TD></TR>
151 <TR><TD><span class='text'>Initial User's Name:</span></TD><TD><INPUT SIZE='30' TYPE='TEXT' NAME='iuname' VALUE='Administrator'><span class='text'>(This is the real name of the initial user.)</span></TD></TR>
152 <TR><TD><span class='text'>Initial Group:</span></TD><TD><INPUT SIZE='30' TYPE='TEXT' NAME='igroup' VALUE='Default'><span class='text'>(This is the group that will be created for your users. This should be the name of your practice.)</span></TD></TR>
154 echo "</TABLE>
155 <br>
156 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
158 break;
161 case 3:
164 echo "<b>Step $state</b><br><br>\n";
165 if ($pass == "" || $login == "" || !isset($login) || !isset($pass)) {
166 echo "ERROR. Please pick a proper username and/or password.<br>\n";
167 break;
170 if ($inst != 2) {
171 echo "Connecting to MySQL Server...\n";
172 flush();
173 if ($server == "localhost")
174 $dbh = mysql_connect("$server","root","$rootpass");
175 else
176 $dbh = mysql_connect("$server:$port","root","$rootpass");
177 if ($dbh == FALSE) {
178 echo "ERROR. Check your login credentials.\n";
179 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
180 break;
182 else
183 echo "OK.<br>\n";
184 echo "Creating database...\n";
185 flush();
186 if (mysql_query("create database $dbname",$dbh) == FALSE) {
187 echo "ERROR. Check your login credentials.\n";
188 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
189 break;
191 else
192 echo "OK.<br>\n";
193 echo "Creating user with permissions for database...\n";
194 flush();
195 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'",$dbh) == FALSE) {
196 echo "ERROR when granting privileges to the specified user.\n";
197 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
198 echo "ERROR.\n";
199 break;
201 else
202 echo "OK.<br>\n";
203 echo "Reconnecting as new user...\n";
204 mysql_close($dbh);
206 else
207 echo "Connecting to MySQL Server...\n";
209 if ($server == "localhost")
210 $dbh = mysql_connect("$server","$login","$pass");
211 else
212 $dbh = mysql_connect("$server:$port","$login","$pass");
214 if ($dbh == FALSE) {
215 echo "ERROR. Check your login credentials.\n";
216 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
217 break;
219 else
220 echo "OK.<br>\n";
221 echo "Opening database...";
222 flush();
223 if (mysql_select_db("$dbname",$dbh) == FALSE) {
224 echo "ERROR. Check your login credentials.\n";
225 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
226 break;
228 else
229 echo "OK.<br>\n";
230 flush();
231 if ($upgrade != 1) {
232 echo "Creating initial tables...\n";
233 mysql_query("USE $dbname",$dbh);
234 flush();
235 $fd = fopen($dumpfile, 'r');
236 if ($fd == FALSE) {
237 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
238 flush();
239 break;
241 $query = "";
242 $line = "";
243 while (!feof ($fd)){
244 $line = fgets($fd,1024);
245 $line = rtrim($line);
246 if (substr($line,0,2) == "--") // Kill comments
247 continue;
248 if (substr($line,0,1) == "#") // Kill comments
249 continue;
250 if ($line == "")
251 continue;
252 $query = $query.$line; // Check for full query
253 $chr = substr($query,strlen($query)-1,1);
254 if ($chr == ";") { // valid query, execute
255 $query = rtrim($query,";");
256 mysql_query("$query",$dbh);
257 $query = "";
260 echo "OK<br>\n";
261 fclose($fd);
262 flush();
263 echo "Adding Initial User...\n";
264 flush();
265 $iuser = $_POST["iuser"];
266 $iuname = $_POST["iuname"];
267 $igroup = $_POST["igroup"];
268 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
269 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == FALSE) {
270 echo "ERROR. Could not run queries.\n";
271 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
272 flush();
273 break;
275 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','1a1dc91c907325c69271ddf0c944bc72',1,'$iuname','')") == FALSE) {
276 echo "ERROR. Could not run queries.\n";
277 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
278 flush();
279 break;
281 echo "OK<br>\n";
282 flush();
283 /* echo "Inserting ICD-9-CM Codes into Database...\n";
284 flush();
285 $fd = fopen($icd9, 'r');
286 if ($fd == FALSE) {
287 echo "ERROR. Could not open dumpfile.\n";
288 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
289 flush();
290 break;
292 $query = "";
293 $line = "";
294 while (!feof ($fd)){
295 $line = fgets($fd,1024);
296 $line = rtrim($line);
297 if (substr($line,0,2) == "--") // Kill comments
298 continue;
299 if (substr($line,0,1) == "#") // Kill comments
300 continue;
301 if ($line == "")
302 continue;
303 $query = $query.$line; // Check for full query
304 $chr = substr($query,strlen($query)-1,1);
305 if ($chr == ";") { // valid query, execute
306 $query = rtrim($query,";");
307 mysql_query("$query",$dbh);
308 $query = "";
311 echo "OK\n";
312 fclose($fd);*/
313 flush();
315 echo "\n<br>Please make sure 'library/sqlconf.php' is world-writeable for the next step.<br>\n";
318 echo "
319 <FORM METHOD='POST'>\n
320 <INPUT TYPE='HIDDEN' NAME='state' VALUE='4'>
321 <INPUT TYPE='HIDDEN' NAME='host' VALUE='$server'>
322 <INPUT TYPE='HIDDEN' NAME='dbname' VALUE='$dbname'>
323 <INPUT TYPE='HIDDEN' NAME='port' VALUE='$port'>
324 <INPUT TYPE='HIDDEN' NAME='login' VALUE='$login'>
325 <INPUT TYPE='HIDDEN' NAME='pass' VALUE='$pass'>
326 <br>\n
327 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
330 break;
332 case 4:
333 echo "<b>Step $state</b><br><br>\n";
334 echo "Writing SQL Configuration to disk...\n";
335 @touch($conffile); // php bug
336 $fd = @fopen($conffile, 'w');
337 if ($fd == FALSE) {
338 echo "ERROR. Could not open config file '$conffile' for writing.\n";
339 flush();
340 break;
342 $string = "<?
343 // OpenEMR
344 // MySQL Config
345 // Referenced from sql.inc
349 $it_died = 0; //fmg: variable keeps running track of any errors
351 fwrite($fd,$string) or $it_died++;
352 fwrite($fd,"\$host\t= '$host';\n") or $it_died++;
353 fwrite($fd,"\$port\t= '$port';\n") or $it_died++;
354 fwrite($fd,"\$login\t= '$login';\n") or $it_died++;
355 fwrite($fd,"\$pass\t= '$pass';\n") or $it_died++;
356 fwrite($fd,"\$dbase\t= '$dbname';\n") or $it_died++;
359 $string = '
361 $sqlconf = array();
362 $sqlconf["host"]= $host;
363 $sqlconf["port"] = $port;
364 $sqlconf["login"] = $login;
365 $sqlconf["pass"] = $pass;
366 $sqlconf["dbase"] = $dbase;
367 //////////////////////////
368 //////////////////////////
369 //////////////////////////
370 //////DO NOT TOUCH THIS///
371 $config = 1; /////////////
372 //////////////////////////
373 //////////////////////////
374 //////////////////////////
377 ?><? // done just for coloring
379 fwrite($fd,$string) or $it_died++;
381 //it's rather irresponsible to not report errors when writing this file.
382 if ($it_died != 0) {
383 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
384 flush();
385 break;
387 fclose($fd);
389 echo "OK<BR>\nPlease restore secure permissions on the 'library/sqlconf.php' file now.\n<br><FORM METHOD='POST'>\n
390 <INPUT TYPE='HIDDEN' NAME='state' VALUE='5'>\n
391 <br>\n
392 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
394 break;
396 case 0:
397 default:
398 echo "Welcome to OpenEMR. This utility will step you through the configuration of OpenEMR for your practice. Before proceeding, be sure that you have a properly installed and configured MySQL server available, and a PHP configured webserver.<br><br>\n";
400 Echo "<p>If you are upgrading from a previous version, please read the README file.<br><br>";
402 echo "<FORM METHOD='POST'><INPUT TYPE='HIDDEN' NAME='state' VALUE='1'><INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
409 </span>
411 </BODY>
412 </HTML>