Highway to PSR2
[openemr.git] / contrib / util / express.php
blobbefb92a3f4af415cd2ce1177f6e820970572fa06
1 <?php
2 /**
3 * Generated DocBlock
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author bradymiller <bradymiller>
8 * @author bradymiller <bradymiller@users.sourceforge.net>
9 * @author bradymiller <bradymiller@users.sourceforge.net>
10 * @author Robert Down <robertdown@live.com>
11 * @author cornfeed <jdough823@gmail.com>
12 * @copyright Copyright (c) 2010 bradymiller <bradymiller>
13 * @copyright Copyright (c) 2011 bradymiller <bradymiller@users.sourceforge.net>
14 * @copyright Copyright (c) 2011 bradymiller <bradymiller@users.sourceforge.net>
15 * @copyright Copyright (c) 2017 Robert Down <robertdown@live.com>
16 * @copyright Copyright (c) 2011 cornfeed <jdough823@gmail.com>
17 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 <html>
21 <head>
22 <title>
23 </title>
24 </head>
25 <body>
26 <?php
28 // This exit is to avoid malicious use of this script.
29 // (before running this script, comment out the exit command)
30 // (when you are done running the script, remember to uncomment the exit command)
31 exit;
33 if (!$_POST['submit']) {
35 <form method=post>
36 <p>
37 This script will take the name that you give and create an OpenEMR database with this as the database name, username, password, group name. It will also rename the directory this OpenEMR installation is under to the new name. THIS ONLY WORKS WITH XAMPP AND HAS VERY LIMITED TESTING.
38 </p>
39 <p>
40 Make sure that you close Text Editors, Windows Explorer, etc... as these things may stop directory rename that occurs in this script.
41 </p>
42 <p>
43 Enter the name you wish to use for this OpenEMR installation.
44 </p>
45 <input type=text name=newname>
46 <input type=submit name=submit value=submit>
47 </form>
48 <?php
49 exit(0);
52 if ($_POST['submit']) {
53 $newname = $_POST['newname'];
54 //handle the database stuff
55 $dumpfile = "sql/database.sql";
56 $icd9 = "sql/icd9.sql";
57 $conffile = "library/sqlconf.php";
58 $server = "localhost";
59 $port = "3306";
60 $dbname = $newname;
61 $root = "root";
62 $rootpass = "";
63 $login = $newname;
64 $pass = $newname;
65 $loginhost = "localhost";
67 //setup of database
69 echo "Connecting to MySQL Server...\n";
70 flush();
71 if ($server == "localhost") {
72 $dbh = mysql_connect("$server", "$root", "$rootpass");
73 } else {
74 $dbh = mysql_connect("$server:$port", "$root", "$rootpass");
77 if ($dbh == false) {
78 echo "ERROR. Check your login credentials.\n";
79 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
80 break;
81 } else {
82 echo "OK.<br>\n";
85 echo "Creating database...\n";
86 flush();
87 if (mysql_query("create database $dbname", $dbh) == false) {
88 echo "ERROR. Check your login credentials.\n";
89 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
90 break;
91 } else {
92 echo "OK.<br>\n";
95 echo "Creating user with permissions for database...\n";
96 flush();
97 if (mysql_query("GRANT ALL PRIVILEGES ON $dbname.* TO '$login'@'$loginhost' IDENTIFIED BY '$pass'", $dbh) == false) {
98 echo "ERROR when granting privileges to the specified user.\n";
99 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
100 echo "ERROR.\n";
101 break;
102 } else {
103 echo "OK.<br>\n";
106 echo "Reconnecting as new user...\n";
107 mysql_close($dbh);
108 } else {
109 echo "Connecting to MySQL Server...\n";
112 if ($server == "localhost") {
113 $dbh = mysql_connect("$server", "$login", "$pass");
114 } else {
115 $dbh = mysql_connect("$server:$port", "$login", "$pass");
118 if ($dbh == false) {
119 echo "ERROR. Check your login credentials.\n";
120 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
121 break;
122 } else {
123 echo "OK.<br>\n";
126 echo "Opening database...";
127 flush();
128 if (mysql_select_db("$dbname", $dbh) == false) {
129 echo "ERROR. Check your login credentials.\n";
130 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
131 break;
132 } else {
133 echo "OK.<br>\n";
136 flush();
137 if ($upgrade != 1) {
138 echo "Creating initial tables...\n";
139 mysql_query("USE $dbname", $dbh);
140 flush();
141 $fd = fopen($dumpfile, 'r');
142 if ($fd == false) {
143 echo "ERROR. Could not open dumpfile '$dumpfile'.\n";
144 flush();
145 break;
148 $query = "";
149 $line = "";
150 while (!feof($fd)) {
151 $line = fgets($fd, 1024);
152 $line = rtrim($line);
153 if (substr($line, 0, 2) == "--") { // Kill comments
154 continue;
157 if (substr($line, 0, 1) == "#") { // Kill comments
158 continue;
161 if ($line == "") {
162 continue;
165 $query = $query.$line; // Check for full query
166 $chr = substr($query, strlen($query)-1, 1);
167 if ($chr == ";") { // valid query, execute
168 $query = rtrim($query, ";");
169 mysql_query("$query", $dbh);
170 $query = "";
174 echo "OK<br>\n";
175 fclose($fd);
176 flush();
177 echo "Adding Initial User...\n";
178 flush();
179 $iuser = "admin";
180 $iuname = "admin";
181 $igroup = $newname;
182 //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
183 if (mysql_query("INSERT INTO groups (id, name, user) VALUES (1,'$igroup','$iuser')") == false) {
184 echo "ERROR. Could not run queries.\n";
185 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
186 flush();
187 break;
190 //// ViCareplus : As per NIST standard, SHA1 hash/digest of 'pass' is used
191 if (mysql_query("INSERT INTO users (id, username, password, authorized, lname,fname) VALUES (1,'$iuser','9d4e1e23bd5b727046a9e3b4b7db57bd8d6ee684',1,'$iuname','')") == false) {
192 echo "ERROR. Could not run queries.\n";
193 echo "<p>".mysql_error()." (#".mysql_errno().")\n";
194 flush();
195 break;
198 echo "OK<br>\n";
199 flush();
201 //Now write sqlconf file
202 echo "Writing SQL Configuration to disk...\n";
203 @touch($conffile); // php bug
204 $fd = @fopen($conffile, 'w');
205 if ($fd == false) {
206 echo "ERROR. Could not open config file '$conffile' for writing.\n";
207 flush();
208 break;
211 $string = "<?php\n\n// OpenEMR\n// MySQL Config\n// Referenced from sql.inc\n\n";
213 $it_died = 0; //fmg: variable keeps running track of any errors
215 fwrite($fd, $string) or $it_died++;
216 fwrite($fd, "\$host\t= '$host';\n") or $it_died++;
217 fwrite($fd, "\$port\t= '$port';\n") or $it_died++;
218 fwrite($fd, "\$login\t= '$login';\n") or $it_died++;
219 fwrite($fd, "\$pass\t= '$pass';\n") or $it_died++;
220 fwrite($fd, "\$dbase\t= '$dbname';\n") or $it_died++;
223 $string = '\n\n' .
224 '$sqlconf = array();\n' .
225 '$sqlconf["host"]= $host;\n' .
226 '$sqlconf["port"] = $port;\n' .
227 '$sqlconf["login"] = $login;\n' .
228 '$sqlconf["pass"] = $pass;\n' .
229 '$sqlconf["dbase"] = $dbase;\n' .
230 '//////////////////////////\n' .
231 '//////////////////////////\n' .
232 '//////////////////////////\n' .
233 '//////DO NOT TOUCH THIS///\n' .
234 '$config = 1; /////////////\n' .
235 '//////////////////////////\n' .
236 '//////////////////////////\n' .
237 '//////////////////////////\n' .
238 '?>';
240 <?php // done just for coloring
242 fwrite($fd, $string) or $it_died++;
244 //it's rather irresponsible to not report errors when writing this file.
245 if ($it_died != 0) {
246 echo "ERROR. Couldn't write $it_died lines to config file '$conffile'.\n";
247 flush();
248 break;
251 fclose($fd);
253 //Now, use new name and fix globals.php and rename directory!!!
254 $d = getcwd();
255 $dn = dirname($d);
256 $contents = file_get_contents($d.'/interface/globals.php');
257 $contents = preg_replace(
258 '/\$webserver_root\s+=\s+[\"\'].*?[\"\'];/',
259 "\$webserver_root = '".$dn."/".$newname."';",
260 $contents
262 $contents = preg_replace(
263 '/\$web_root\s+=\s+[\"\'].*?[\"\'];/',
264 "\$web_root = '/".$newname."';",
265 $contents
267 file_put_contents($d.'/interface/globals.php', $contents);
268 if (rename($d, $dn.'/'.$newname)) {
269 echo "<br/><a href='http://localhost/".$newname."'>click here</a>";
273 </body>
274 </html>