supporting data peculiar to ippf issue types
[openemr.git] / setup.php
blob29e79802590a8c28b1d332e7aea3a277bec0a381
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"];
15 $gaclConfigFile1 = "gacl/gacl.ini.php";
16 $gaclConfigFile2 = "gacl/gacl.class.php";
17 $gaclWritableDirectory = "gacl/admin/templates_c";
18 $gaclSetupScript1 = "./gacl/setup.php";
19 $gaclSetupScript2 = "./acl_setup.php";
20 $requiredDirectory1 = "interface/main/calendar/modules/PostCalendar/pntemplates/compiled";
21 $requiredDirectory2 = "interface/main/calendar/modules/PostCalendar/pntemplates/cache";
22 $writableFileList = array($conffile, $gaclConfigFile1, $gaclConfigFile2);
23 $writableDirList = array($gaclWritableDirectory, $requiredDirectory1, $requiredDirectory2);
25 include_once($conffile);
27 <HTML>
28 <HEAD>
29 <TITLE>OpenEMR Setup Tool</TITLE>
30 <LINK REL=STYLESHEET HREF="interface/themes/style_blue.css">
31 </HEAD>
32 <BODY>
34 <span class="title">OpenEMR Setup</span>
35 <br><br>
36 <span class="text">
38 <?php
39 if (strtolower(ini_get('register_globals')) != 'off' && (bool) ini_get('register_globals')) {
40 echo "It appears that you have register_globals enabled in your php.ini\n" .
41 "configuration file. This causes unacceptable security risks. You must\n" .
42 "turn it off before continuing with installation.\n";
43 exit();
47 <?php
48 if ($state == 5) {
51 <p>Congratulations! OpenEMR is now successfully installed.
53 <ul>
54 <li>Please Edit the 'interface/globals.php' file now to specify the correct
55 URL paths, and to select a theme.</li>
56 <li>Please restore secure permissions on the configuration files: /openemr/library/sqlconf.php, /openemr/gacl/gacl.ini.php, and /openemr/gacl/gacl.class.php files.</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 Access controls (php-GACL) are installed for fine-grained security,
71 and can be administered in OpenEMR's admin->acl menu.
72 </p>
73 <p>
74 There's much information and many extra tools bundled within the OpenEMR
75 installation directory. Please refer to openemr/Documentation.
76 <br>Many forms and other useful scripts can be found at openemr/contrib.
77 </p>
78 <p>
79 Reading openemr/includes/config.php and openemr/interface/globals.php is a good
80 idea.
81 </p>
82 <p>
83 To ensure a consistent look and feel through out the application
84 using <a href='http://www.mozilla.org/products/firefox/'>Firefox</a>
85 is recommended.
86 </p>
87 <p>
88 <b>The initial OpenEMR user is "<?php echo $iuser; ?>" and the password is "pass".</b>
89 You should change this password!
90 </p>
91 <p>
92 <a href='./'>Click here to start using OpenEMR. </a>
93 </p>
96 exit();
100 <?php
102 $server = $_POST["server"];
103 $port = $_POST["port"];
104 $dbname = $_POST["dbname"];
105 $root = $_POST["root"];
106 $login = $_POST["login"];
107 $pass = $_POST["pass"];
108 $loginhost = $_POST["loginhost"];
109 $rootpass = $_POST["rootpass"];
112 if ($config == 1) {
113 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";
115 else {
116 switch ($state) {
118 case 1:
119 echo "<b>Step $state</b><br><br>\n";
120 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
121 <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>
122 <br><br>\n
123 <FORM METHOD='POST'>\n
124 <INPUT TYPE='HIDDEN' NAME='state' VALUE='2'>\n
125 <INPUT TYPE='RADIO' NAME='inst' VALUE='1' checked>Have setup create the databases<br>\n
126 <INPUT TYPE='RADIO' NAME='inst' VALUE='2'>I have already created the databases<br>\n
127 <br>\n
128 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
129 break;
131 case 2:
132 echo "<b>Step $state</b><br><br>\n";
133 echo "Now you need to supply the MySQL server information.
134 <br><br>
135 <FORM METHOD='POST'>
136 <INPUT TYPE='HIDDEN' NAME='state' VALUE='3'>
137 <INPUT TYPE='HIDDEN' NAME='inst' VALUE='$inst'>
138 <TABLE>\n
139 <TR><TD><font color='red'>SERVER:</font></TD></TR>
140 <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>
141 <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>
142 <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>
143 <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>
144 <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";
145 if ($inst != 2) {
146 echo "<TR><TD><font color='red'>CLIENT:</font></TD></TR>";
147 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>
148 <TR><TD><span class='text'>Name for Root Account: </span></TD><TD><INPUT TYPE='TEXT' VALUE='root' NAME='root' SIZE='30'><span class='text'>(This is name for MySQL root account. For localhost, it is usually ok to leave it 'root'.)</span><br></TD></TR>
149 <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";
151 echo "<TR><TD><font color='red'>USER:</font></TD></TR>";
152 echo "<TR><TD COLSPAN=2></TD></TR>
153 <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>
154 <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>
155 <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>
157 echo "</TABLE>
158 <br>
159 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
161 break;
164 case 3:
167 echo "<b>Step $state</b><br><br>\n";
168 if ($pass == "" || $login == "" || !isset($login) || !isset($pass)) {
169 echo "ERROR. Please pick a proper username and/or password.<br>\n";
170 break;
173 if ($inst != 2) {
174 echo "Connecting to MySQL Server...\n";
175 flush();
176 if ($server == "localhost")
177 $dbh = mysql_connect("$server","$root","$rootpass");
178 else
179 $dbh = mysql_connect("$server:$port","$root","$rootpass");
180 if ($dbh == FALSE) {
181 echo "ERROR. Check your login credentials.\n";
182 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
183 break;
185 else
186 echo "OK.<br>\n";
187 echo "Creating database...\n";
188 flush();
189 if (mysql_query("create database $dbname",$dbh) == FALSE) {
190 echo "ERROR. Check your login credentials.\n";
191 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
192 break;
194 else
195 echo "OK.<br>\n";
196 echo "Creating user with permissions for database...\n";
197 flush();
198 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'",$dbh) == FALSE) {
199 echo "ERROR when granting privileges to the specified user.\n";
200 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
201 echo "ERROR.\n";
202 break;
204 else
205 echo "OK.<br>\n";
206 echo "Reconnecting as new user...\n";
207 mysql_close($dbh);
209 else
210 echo "Connecting to MySQL Server...\n";
212 if ($server == "localhost")
213 $dbh = mysql_connect("$server","$login","$pass");
214 else
215 $dbh = mysql_connect("$server:$port","$login","$pass");
217 if ($dbh == FALSE) {
218 echo "ERROR. Check your login credentials.\n";
219 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
220 break;
222 else
223 echo "OK.<br>\n";
224 echo "Opening database...";
225 flush();
226 if (mysql_select_db("$dbname",$dbh) == FALSE) {
227 echo "ERROR. Check your login credentials.\n";
228 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
229 break;
231 else
232 echo "OK.<br>\n";
233 flush();
234 if ($upgrade != 1) {
235 echo "Creating initial tables...\n";
236 mysql_query("USE $dbname",$dbh);
237 flush();
238 $fd = fopen($dumpfile, 'r');
239 if ($fd == FALSE) {
240 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
241 flush();
242 break;
244 $query = "";
245 $line = "";
246 while (!feof ($fd)){
247 $line = fgets($fd,1024);
248 $line = rtrim($line);
249 if (substr($line,0,2) == "--") // Kill comments
250 continue;
251 if (substr($line,0,1) == "#") // Kill comments
252 continue;
253 if ($line == "")
254 continue;
255 $query = $query.$line; // Check for full query
256 $chr = substr($query,strlen($query)-1,1);
257 if ($chr == ";") { // valid query, execute
258 $query = rtrim($query,";");
259 mysql_query("$query",$dbh);
260 $query = "";
263 echo "OK<br>\n";
264 fclose($fd);
265 flush();
266 echo "Adding Initial User...\n";
267 flush();
268 $iuser = $_POST["iuser"];
269 $iuname = $_POST["iuname"];
270 $igroup = $_POST["igroup"];
271 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
272 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == FALSE) {
273 echo "ERROR. Could not run queries.\n";
274 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
275 flush();
276 break;
278 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','1a1dc91c907325c69271ddf0c944bc72',1,'$iuname','')") == FALSE) {
279 echo "ERROR. Could not run queries.\n";
280 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
281 flush();
282 break;
284 echo "OK<br>\n";
285 flush();
286 /* echo "Inserting ICD-9-CM Codes into Database...\n";
287 flush();
288 $fd = fopen($icd9, 'r');
289 if ($fd == FALSE) {
290 echo "ERROR. Could not open dumpfile.\n";
291 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
292 flush();
293 break;
295 $query = "";
296 $line = "";
297 while (!feof ($fd)){
298 $line = fgets($fd,1024);
299 $line = rtrim($line);
300 if (substr($line,0,2) == "--") // Kill comments
301 continue;
302 if (substr($line,0,1) == "#") // Kill comments
303 continue;
304 if ($line == "")
305 continue;
306 $query = $query.$line; // Check for full query
307 $chr = substr($query,strlen($query)-1,1);
308 if ($chr == ";") { // valid query, execute
309 $query = rtrim($query,";");
310 mysql_query("$query",$dbh);
311 $query = "";
314 echo "OK\n";
315 fclose($fd);*/
316 flush();
318 echo "\n<br>Next step will finalize SQL setup and install/configure access controls (php-GACL).<br>\n";
320 echo "
321 <FORM METHOD='POST'>\n
322 <INPUT TYPE='HIDDEN' NAME='state' VALUE='4'>
323 <INPUT TYPE='HIDDEN' NAME='host' VALUE='$server'>
324 <INPUT TYPE='HIDDEN' NAME='dbname' VALUE='$dbname'>
325 <INPUT TYPE='HIDDEN' NAME='port' VALUE='$port'>
326 <INPUT TYPE='HIDDEN' NAME='login' VALUE='$login'>
327 <INPUT TYPE='HIDDEN' NAME='pass' VALUE='$pass'>
328 <INPUT TYPE='HIDDEN' NAME='iuser' VALUE='$iuser'>
329 <INPUT TYPE='HIDDEN' NAME='iuname' VALUE='$iuname'>
330 <br>\n
331 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
334 break;
336 case 4:
337 echo "<b>Step $state</b><br><br>\n";
338 echo "Writing SQL configuration file and setting up access controls(php-GACL)...<br><br>";
340 echo "Writing SQL Configuration...<br>";
341 @touch($conffile); // php bug
342 $fd = @fopen($conffile, 'w');
343 $string = "<?
344 // OpenEMR
345 // MySQL Config
346 // Referenced from sql.inc
350 $it_died = 0; //fmg: variable keeps running track of any errors
352 fwrite($fd,$string) or $it_died++;
353 fwrite($fd,"\$host\t= '$host';\n") or $it_died++;
354 fwrite($fd,"\$port\t= '$port';\n") or $it_died++;
355 fwrite($fd,"\$login\t= '$login';\n") or $it_died++;
356 fwrite($fd,"\$pass\t= '$pass';\n") or $it_died++;
357 fwrite($fd,"\$dbase\t= '$dbname';\n") or $it_died++;
360 $string = '
362 $sqlconf = array();
363 $sqlconf["host"]= $host;
364 $sqlconf["port"] = $port;
365 $sqlconf["login"] = $login;
366 $sqlconf["pass"] = $pass;
367 $sqlconf["dbase"] = $dbase;
368 //////////////////////////
369 //////////////////////////
370 //////////////////////////
371 //////DO NOT TOUCH THIS///
372 $config = 1; /////////////
373 //////////////////////////
374 //////////////////////////
375 //////////////////////////
378 ?><? // done just for coloring
380 fwrite($fd,$string) or $it_died++;
382 //it's rather irresponsible to not report errors when writing this file.
383 if ($it_died != 0) {
384 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
385 flush();
386 break;
388 fclose($fd);
390 echo "Successfully wrote SQL configuration.<BR><br>";
392 echo "Installing and Configuring Access Controls (php-GACL)<br>";
394 //first, edit two gacl config files
395 // edit gacl.ini.php
396 $data = file($gaclConfigFile1) or die("Could not read ".$gaclConfigFile1." file.");
397 $finalData = "";
398 foreach ($data as $line) {
399 $isHit = 0;
400 if ((strpos($line,"db_host")) === false) {
402 else {
403 $isHit = 1;
404 $finalData .= "db_host = \"${host}\"\n";
406 if ((strpos($line,"db_user")) === false) {
408 else {
409 $isHit = 1;
410 $finalData .= "db_user = \"${login}\"\n";
412 if ((strpos($line,"db_password")) === false) {
414 else {
415 $isHit = 1;
416 $finalData .= "db_password = \"${pass}\"\n";
418 if ((strpos($line,"db_name")) === false) {
420 else {
421 $isHit = 1;
422 $finalData .= "db_name = \"${dbname}\"\n";
424 if (!$isHit) {
425 $finalData .= $line;
428 $fd = @fopen($gaclConfigFile1, 'w') or die("Could not open ".$gaclConfigFile1." file.");
429 fwrite($fd, $finalData);
430 fclose($fd);
432 // edit gacl.class.php
433 $data = file($gaclConfigFile2) or die("Could not read ".$gaclConfigFile2." file.");
434 $finalData = "";
435 foreach ($data as $line) {
436 $isHit = 0;
437 if ((strpos($line,"var \$_db_host = ")) === false) {
439 else {
440 $isHit = 1;
441 $finalData .= "var \$_db_host = '$host';\n";
443 if ((strpos($line,"var \$_db_user = ")) === false) {
445 else {
446 $isHit = 1;
447 $finalData .= "var \$_db_user = '$login';\n";
449 if ((strpos($line,"var \$_db_password = ")) === false) {
451 else {
452 $isHit = 1;
453 $finalData .= "var \$_db_password = '$pass';\n";
455 if ((strpos($line,"var \$_db_name = ")) === false) {
457 else {
458 $isHit = 1;
459 $finalData .= "var \$_db_name = '$dbname';\n";
461 if (!$isHit) {
462 $finalData .= $line;
465 $fd = @fopen($gaclConfigFile2, 'w') or die("Could not open ".$gaclConfigFile2." file.");
466 fwrite($fd, $finalData);
467 fclose($fd);
469 //second, run gacl config scripts
470 require $gaclSetupScript1;
471 require $gaclSetupScript2;
472 echo "<br>";
474 //third, give the administrator user admin priviledges
475 $groupArray = array("Administrators");
476 set_user_aro($groupArray,$iuser,$iuname,"","");
477 echo "Gave the '$iuser' user (password is 'pass') administrator access.<br>";
479 echo "<br><FORM METHOD='POST'>\n
480 <INPUT TYPE='HIDDEN' NAME='state' VALUE='5'>\n
481 <INPUT TYPE='HIDDEN' NAME='iuser' VALUE='$iuser'>\n
482 <br>\n
483 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
485 break;
487 case 0:
488 default:
489 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";
491 Echo "<p>If you are upgrading from a previous version, please read the README file.<br><br>";
493 echo "We will now ensure correct file permissions and directories before starting installation:<br><br>\n";
494 echo "<FONT COLOR='blue'>Ensuring following files are world-writable...</FONT><br>\n";
495 $errorWritable = 0;
496 foreach ($writableFileList as $tempFile) {
497 if (is_writable($tempFile)) {
498 echo "'openemr/$tempFile' file is <FONT COLOR='blue'>ready</FONT>.<br>";
500 else {
501 echo "<br><FONT COLOR='red'>UNABLE</FONT> to open file 'openemr/$tempFile' for writing.<br>";
502 echo "(ensure 'openemr/$tempFile' file is world-writeable)<br>";
503 $errorWritable = 1;
506 if ($errorWritable) {
507 echo "<br><FONT COLOR='red'>You can't proceed until all above files are ready.</FONT><br>";
508 echo "Fix above file permissions and then click the 'Check Again' button to re-check files.<br>";
509 echo "In linux, recommend changing file permissions temporarily with the 'chmod 666 filename' command.<br>";
510 echo "<FORM METHOD='POST'><INPUT TYPE='SUBMIT' VALUE='Check Again'><br></FORM><br>\n";
511 break;
514 echo "<br><FONT COLOR='blue'>Ensuring following directories exist...</FONT><br>\n";
515 $errorWritable = 0;
516 foreach ($writableDirList as $tempDir) {
517 if (file_exists($tempDir)) {
518 echo "'openemr/$tempDir' directory <FONT COLOR='blue'>exists</FONT>.<br>";
520 else {
521 echo "<br><FONT COLOR='red'>UNABLE</FONT> to find directory 'openemr/$tempDir'.<br>";
522 echo "(please create 'openemr/$tempDir' directory)<br>";
523 $errorWritable = 1;
526 if ($errorWritable) {
527 echo "<br><FONT COLOR='red'>You can't proceed until all above directories exist.</FONT><br>";
528 echo "Add above marked directories and then click the 'Check Again' button to re-check for directories.<br>";
529 echo "In linux, recommend using the 'mkdir directory_name' command<br><br>\n";
530 echo "<FORM METHOD='POST'><INPUT TYPE='SUBMIT' VALUE='Check Again'><br></FORM><br>\n";
531 break;
534 echo "<br><FONT COLOR='blue'>Ensuring following directories have proper permissions...</FONT><br>\n";
535 $errorWritable = 0;
536 foreach ($writableDirList as $tempDir) {
537 if (is_writable($tempDir)) {
538 echo "'openemr/$tempDir' directory is <FONT COLOR='blue'>ready</FONT>.<br>";
540 else {
541 echo "<br><FONT COLOR='red'>UNABLE</FONT> to open directory 'openemr/$tempDir' for writing.<br>";
542 echo "(ensure 'openemr/$tempDir' directory is world-writeable)<br>";
543 $errorWritable = 1;
546 if ($errorWritable) {
547 echo "<br><FONT COLOR='red'>You can't proceed until all directories are ready.</FONT><br>";
548 echo "Fix above directory permissions and then click the 'Check Again' button to re-check directories.<br>";
549 echo "In linux, recommend changing owners of directories permanently to the web server. For example, in many linux OS's the web server user is 'apache', 'nobody', or 'www-data'. So if 'apache' were the web server user name, could use the command 'chown -R apache:apache directory_name' command.<br><br>\n";
550 echo "<FORM METHOD='POST'><INPUT TYPE='SUBMIT' VALUE='Check Again'><br></FORM><br>\n";
551 break;
554 echo "<br><br>All required files and directories have been verified. Click to continue installation.<br>\n";
555 echo "<FORM METHOD='POST'><INPUT TYPE='HIDDEN' NAME='state' VALUE='1'><INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
562 </span>
564 </BODY>
565 </HTML>