php-GACL embedding continued.
[openemr.git] / setup.php
blob86e06f12c638a61c0f5d9203caee21b0681a2710
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 $writableFileList = array($conffile, $gaclConfigFile1, $gaclConfigFile2);
21 $writableDirList = array($gaclWritableDirectory);
23 include_once($conffile);
25 <HTML>
26 <HEAD>
27 <TITLE>OpenEMR Setup Tool</TITLE>
28 <LINK REL=STYLESHEET HREF="interface/themes/style_blue.css">
29 </HEAD>
30 <BODY>
32 <span class="title">OpenEMR Setup</span>
33 <br><br>
34 <span class="text">
36 <?php
37 if (strtolower(ini_get('register_globals')) != 'off' && (bool) ini_get('register_globals')) {
38 echo "It appears that you have register_globals enabled in your php.ini\n" .
39 "configuration file. This causes unacceptable security risks. You must\n" .
40 "turn it off before continuing with installation.\n";
41 exit();
45 <?php
46 if ($state == 5) {
49 <p>Congratulations! OpenEMR is now successfully installed.
51 <ul>
52 <li>Please Edit the 'interface/globals.php' file now to specify the correct
53 URL paths, and to select a theme.</li>
54 <li>Please make sure that the two folders underneath
55 'openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/'
56 exist and are writable by the web server. The two subdirectories are
57 'compiled' and 'cache'.<br>
58 Try "chown apache:apache -R openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/compiled"
59 and
60 "chown apache:apache -R openemrwebroot/interface/main/calendar/modules/PostCalendar/pntemplates/cache".
61 (If either subdirectory doesn't exist, create it first then do the chown above).<br>
62 The user name and group of apache may differ depending on your OS, i.e.
63 for Debian they are www-data and www-data.</li>
64 <li>Please restore secure permissions on the configuration files: /openemr/library/sqlconf.php,<br>
65 /openemr/gacl/gacl.ini.php, and /openemr/gacl/gacl.class.php files.</li>
66 </ul>
67 <p>
68 In order to take full advantage of the documents capability you
69 must give your web server permissions on the document storage
70 directory. Try "chown apache:apache -R openemrwebroot/documents"
71 and then "chmod g+w openemrwebroot/documents".
72 You must also make sure your PHP installation (normally set in
73 your php.ini file) has "file_uploads enabled", that
74 "upload_max_filesize" is appropriate for your use and that
75 "upload_tmp_dir" is set to a correct value if the default of
76 "/tmp" won't work on your system.
77 </p>
78 <p>
79 Access controls (php-GACL) are installed for fine-grained security,
80 and can be administered in OpenEMR's admin->acl menu.
81 </p>
82 <p>
83 There's much information and many extra tools bundled within the OpenEMR
84 installation directory. Please refer to openemr/Documentation.
85 <br>Many forms and other useful scripts can be found at openemr/contrib.
86 </p>
87 <p>
88 Reading openemr/includes/config.php and openemr/interface/globals.php is a good
89 idea.
90 </p>
91 <p>
92 To ensure a consistent look and feel through out the application
93 using <a href='http://www.mozilla.org/products/firefox/'>Firefox</a>
94 is recommended.
95 </p>
96 <p>
97 <b>The initial OpenEMR user is "<?php echo $iuser; ?>" and the password is "pass".</b>
98 You should change this password!
99 </p>
101 <a href='./'>Click here to start using OpenEMR. </a>
102 </p>
105 exit();
109 <?php
111 $server = $_POST["server"];
112 $port = $_POST["port"];
113 $dbname = $_POST["dbname"];
114 $root = $_POST["root"];
115 $login = $_POST["login"];
116 $pass = $_POST["pass"];
117 $loginhost = $_POST["loginhost"];
118 $rootpass = $_POST["rootpass"];
121 if ($config == 1) {
122 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";
124 else {
125 switch ($state) {
127 case 1:
128 echo "<b>Step $state</b><br><br>\n";
129 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
130 <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>
131 <br><br>\n
132 <FORM METHOD='POST'>\n
133 <INPUT TYPE='HIDDEN' NAME='state' VALUE='2'>\n
134 <INPUT TYPE='RADIO' NAME='inst' VALUE='1' checked>Have setup create the databases<br>\n
135 <INPUT TYPE='RADIO' NAME='inst' VALUE='2'>I have already created the databases<br>\n
136 <br>\n
137 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
138 break;
140 case 2:
141 echo "<b>Step $state</b><br><br>\n";
142 echo "Now you need to supply the MySQL server information.
143 <br><br>
144 <FORM METHOD='POST'>
145 <INPUT TYPE='HIDDEN' NAME='state' VALUE='3'>
146 <INPUT TYPE='HIDDEN' NAME='inst' VALUE='$inst'>
147 <TABLE>\n
148 <TR><TD><font color='red'>SERVER:</font></TD></TR>
149 <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>
150 <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>
151 <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>
152 <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>
153 <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";
154 if ($inst != 2) {
155 echo "<TR><TD><font color='red'>CLIENT:</font></TD></TR>";
156 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>
157 <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>
158 <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";
160 echo "<TR><TD><font color='red'>USER:</font></TD></TR>";
161 echo "<TR><TD COLSPAN=2></TD></TR>
162 <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>
163 <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>
164 <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>
166 echo "</TABLE>
167 <br>
168 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
170 break;
173 case 3:
176 echo "<b>Step $state</b><br><br>\n";
177 if ($pass == "" || $login == "" || !isset($login) || !isset($pass)) {
178 echo "ERROR. Please pick a proper username and/or password.<br>\n";
179 break;
182 if ($inst != 2) {
183 echo "Connecting to MySQL Server...\n";
184 flush();
185 if ($server == "localhost")
186 $dbh = mysql_connect("$server","$root","$rootpass");
187 else
188 $dbh = mysql_connect("$server:$port","$root","$rootpass");
189 if ($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 database...\n";
197 flush();
198 if (mysql_query("create database $dbname",$dbh) == FALSE) {
199 echo "ERROR. Check your login credentials.\n";
200 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
201 break;
203 else
204 echo "OK.<br>\n";
205 echo "Creating user with permissions for database...\n";
206 flush();
207 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'",$dbh) == FALSE) {
208 echo "ERROR when granting privileges to the specified user.\n";
209 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
210 echo "ERROR.\n";
211 break;
213 else
214 echo "OK.<br>\n";
215 echo "Reconnecting as new user...\n";
216 mysql_close($dbh);
218 else
219 echo "Connecting to MySQL Server...\n";
221 if ($server == "localhost")
222 $dbh = mysql_connect("$server","$login","$pass");
223 else
224 $dbh = mysql_connect("$server:$port","$login","$pass");
226 if ($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 echo "Opening database...";
234 flush();
235 if (mysql_select_db("$dbname",$dbh) == FALSE) {
236 echo "ERROR. Check your login credentials.\n";
237 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
238 break;
240 else
241 echo "OK.<br>\n";
242 flush();
243 if ($upgrade != 1) {
244 echo "Creating initial tables...\n";
245 mysql_query("USE $dbname",$dbh);
246 flush();
247 $fd = fopen($dumpfile, 'r');
248 if ($fd == FALSE) {
249 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
250 flush();
251 break;
253 $query = "";
254 $line = "";
255 while (!feof ($fd)){
256 $line = fgets($fd,1024);
257 $line = rtrim($line);
258 if (substr($line,0,2) == "--") // Kill comments
259 continue;
260 if (substr($line,0,1) == "#") // Kill comments
261 continue;
262 if ($line == "")
263 continue;
264 $query = $query.$line; // Check for full query
265 $chr = substr($query,strlen($query)-1,1);
266 if ($chr == ";") { // valid query, execute
267 $query = rtrim($query,";");
268 mysql_query("$query",$dbh);
269 $query = "";
272 echo "OK<br>\n";
273 fclose($fd);
274 flush();
275 echo "Adding Initial User...\n";
276 flush();
277 $iuser = $_POST["iuser"];
278 $iuname = $_POST["iuname"];
279 $igroup = $_POST["igroup"];
280 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
281 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == FALSE) {
282 echo "ERROR. Could not run queries.\n";
283 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
284 flush();
285 break;
287 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','1a1dc91c907325c69271ddf0c944bc72',1,'$iuname','')") == FALSE) {
288 echo "ERROR. Could not run queries.\n";
289 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
290 flush();
291 break;
293 echo "OK<br>\n";
294 flush();
295 /* echo "Inserting ICD-9-CM Codes into Database...\n";
296 flush();
297 $fd = fopen($icd9, 'r');
298 if ($fd == FALSE) {
299 echo "ERROR. Could not open dumpfile.\n";
300 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
301 flush();
302 break;
304 $query = "";
305 $line = "";
306 while (!feof ($fd)){
307 $line = fgets($fd,1024);
308 $line = rtrim($line);
309 if (substr($line,0,2) == "--") // Kill comments
310 continue;
311 if (substr($line,0,1) == "#") // Kill comments
312 continue;
313 if ($line == "")
314 continue;
315 $query = $query.$line; // Check for full query
316 $chr = substr($query,strlen($query)-1,1);
317 if ($chr == ";") { // valid query, execute
318 $query = rtrim($query,";");
319 mysql_query("$query",$dbh);
320 $query = "";
323 echo "OK\n";
324 fclose($fd);*/
325 flush();
327 echo "\n<br>Next step will ensure the following files or directories are world-writeable:<br>\n";
328 foreach ($writableFileList as $tempFile) {
329 echo "&nbsp;'openemr/$tempFile' file<br>";
331 foreach ($writableDirList as $tempDir) {
332 echo "&nbsp;'openemr/$tempDir' directory<br>";
335 echo "
336 <FORM METHOD='POST'>\n
337 <INPUT TYPE='HIDDEN' NAME='state' VALUE='4'>
338 <INPUT TYPE='HIDDEN' NAME='host' VALUE='$server'>
339 <INPUT TYPE='HIDDEN' NAME='dbname' VALUE='$dbname'>
340 <INPUT TYPE='HIDDEN' NAME='port' VALUE='$port'>
341 <INPUT TYPE='HIDDEN' NAME='login' VALUE='$login'>
342 <INPUT TYPE='HIDDEN' NAME='pass' VALUE='$pass'>
343 <INPUT TYPE='HIDDEN' NAME='iuser' VALUE='$iuser'>
344 <INPUT TYPE='HIDDEN' NAME='iuname' VALUE='$iuname'>
345 <br>\n
346 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
349 break;
351 case 4:
352 echo "<b>Step $state</b><br><br>\n";
353 echo "Checking to ensure files are ready...<br>";
355 //ensure required files and directories are writable before moving on
356 $errorWritable = 0;
357 foreach ($writableFileList as $tempFile) {
358 if (is_writable($tempFile)) {
359 echo "'openemr/$tempFile' file is ready.<br>";
361 else {
362 echo "<br>UNABLE to open configuration file 'openemr/$tempFile' for writing.<br>";
363 echo "(ensure 'openemr/$tempFile' file is world-writeable)<br><br>";
364 flush();
365 $errorWritable = 1;
369 foreach ($writableDirList as $tempDir) {
370 if (is_writable($tempDir)) {
371 echo "'openemr/$tempDir' directory is ready.<br>";
373 else {
374 echo "<br>UNABLE to open directory 'openemr/$tempDir' for writing.<br>";
375 echo "(ensure 'openemr/$tempDir' directory is world-writeable)<br><br>";
376 flush();
377 $errorWritable = 1;
380 if ($errorWritable) {
381 echo "You can't proceed until all files are ready.<br>";
382 echo "Fix above file permissions and then click the 'Check Again' button to re-check files.<br>";
383 flush();
384 echo "
385 <FORM METHOD='POST'>\n
386 <INPUT TYPE='HIDDEN' NAME='state' VALUE='4'>
387 <INPUT TYPE='HIDDEN' NAME='host' VALUE='$server'>
388 <INPUT TYPE='HIDDEN' NAME='dbname' VALUE='$dbname'>
389 <INPUT TYPE='HIDDEN' NAME='port' VALUE='$port'>
390 <INPUT TYPE='HIDDEN' NAME='login' VALUE='$login'>
391 <INPUT TYPE='HIDDEN' NAME='pass' VALUE='$pass'>
392 <INPUT TYPE='HIDDEN' NAME='iuser' VALUE='$iuser'>
393 <INPUT TYPE='HIDDEN' NAME='iuname' VALUE='$iuname'>
394 <br>\n
395 <INPUT TYPE='SUBMIT' VALUE='Check Again'><br></FORM><br>\n";
396 break;
399 //passed all file tests, now can write sql configuration and configure php-GACL
400 echo "<br>Files are all ready, now writing SQL Configuration to disk and configuring access controls (php-GACL)...<br><br>";
401 echo "Writing SQL Configuration...<br>";
402 @touch($conffile); // php bug
403 $fd = @fopen($conffile, 'w');
404 $string = "<?
405 // OpenEMR
406 // MySQL Config
407 // Referenced from sql.inc
411 $it_died = 0; //fmg: variable keeps running track of any errors
413 fwrite($fd,$string) or $it_died++;
414 fwrite($fd,"\$host\t= '$host';\n") or $it_died++;
415 fwrite($fd,"\$port\t= '$port';\n") or $it_died++;
416 fwrite($fd,"\$login\t= '$login';\n") or $it_died++;
417 fwrite($fd,"\$pass\t= '$pass';\n") or $it_died++;
418 fwrite($fd,"\$dbase\t= '$dbname';\n") or $it_died++;
421 $string = '
423 $sqlconf = array();
424 $sqlconf["host"]= $host;
425 $sqlconf["port"] = $port;
426 $sqlconf["login"] = $login;
427 $sqlconf["pass"] = $pass;
428 $sqlconf["dbase"] = $dbase;
429 //////////////////////////
430 //////////////////////////
431 //////////////////////////
432 //////DO NOT TOUCH THIS///
433 $config = 1; /////////////
434 //////////////////////////
435 //////////////////////////
436 //////////////////////////
439 ?><? // done just for coloring
441 fwrite($fd,$string) or $it_died++;
443 //it's rather irresponsible to not report errors when writing this file.
444 if ($it_died != 0) {
445 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
446 flush();
447 break;
449 fclose($fd);
451 echo "Successfully wrote SQL configuration.<BR><br>";
453 echo "Installing and Configuring Access Controls (php-GACL)<br>";
455 //first, edit two gacl config files
456 // edit gacl.ini.php
457 $data = file($gaclConfigFile1) or die("Could not read ".$gaclConfigFile1." file.");
458 $finalData = "";
459 foreach ($data as $line) {
460 $isHit = 0;
461 if ((strpos($line,"db_host")) === false) {
463 else {
464 $isHit = 1;
465 $finalData .= "db_host = \"${host}\"\n";
467 if ((strpos($line,"db_user")) === false) {
469 else {
470 $isHit = 1;
471 $finalData .= "db_user = \"${login}\"\n";
473 if ((strpos($line,"db_password")) === false) {
475 else {
476 $isHit = 1;
477 $finalData .= "db_password = \"${pass}\"\n";
479 if ((strpos($line,"db_name")) === false) {
481 else {
482 $isHit = 1;
483 $finalData .= "db_name = \"${dbname}\"\n";
485 if (!$isHit) {
486 $finalData .= $line;
489 $fd = @fopen($gaclConfigFile1, 'w') or die("Could not open ".$gaclConfigFile1." file.");
490 fwrite($fd, $finalData);
491 fclose($fd);
493 // edit gacl.class.php
494 $data = file($gaclConfigFile2) or die("Could not read ".$gaclConfigFile2." file.");
495 $finalData = "";
496 foreach ($data as $line) {
497 $isHit = 0;
498 if ((strpos($line,"var \$_db_host = ")) === false) {
500 else {
501 $isHit = 1;
502 $finalData .= "var \$_db_host = '$host';\n";
504 if ((strpos($line,"var \$_db_user = ")) === false) {
506 else {
507 $isHit = 1;
508 $finalData .= "var \$_db_user = '$login';\n";
510 if ((strpos($line,"var \$_db_password = ")) === false) {
512 else {
513 $isHit = 1;
514 $finalData .= "var \$_db_password = '$pass';\n";
516 if ((strpos($line,"var \$_db_name = ")) === false) {
518 else {
519 $isHit = 1;
520 $finalData .= "var \$_db_name = '$dbname';\n";
522 if (!$isHit) {
523 $finalData .= $line;
526 $fd = @fopen($gaclConfigFile2, 'w') or die("Could not open ".$gaclConfigFile2." file.");
527 fwrite($fd, $finalData);
528 fclose($fd);
530 //second, run gacl config scripts
531 require $gaclSetupScript1;
532 require $gaclSetupScript2;
533 echo "<br>";
535 //third, give the administrator user admin priviledges
536 $groupArray = array("Administrators");
537 set_user_aro($groupArray,$iuser,$iuname,"","");
538 echo "Gave the '$iuser' user (password is 'pass') administrator access.<br>";
540 echo "<br><FORM METHOD='POST'>\n
541 <INPUT TYPE='HIDDEN' NAME='state' VALUE='5'>\n
542 <INPUT TYPE='HIDDEN' NAME='iuser' VALUE='$iuser'>\n
543 <br>\n
544 <INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>\n";
546 break;
548 case 0:
549 default:
550 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";
552 Echo "<p>If you are upgrading from a previous version, please read the README file.<br><br>";
554 echo "<FORM METHOD='POST'><INPUT TYPE='HIDDEN' NAME='state' VALUE='1'><INPUT TYPE='SUBMIT' VALUE='Continue'><br></FORM><br>";
561 </span>
563 </BODY>
564 </HTML>