cbb0716cb040aff2eabe3779bec97f157e330f23
2 /* Copyright © 2010 by Andrew Moore */
3 /* Licensing information appears at the end of this file. */
8 public function __construct( $cgi_variables )
10 // Installation variables
11 // For a good explanation of these variables, see documentation in
12 // the contrib/util/installScripts/InstallerAuto.php file.
13 $this->iuser
= $cgi_variables['iuser'];
14 $this->iuserpass
= $cgi_variables['iuserpass'];
15 $this->iuname
= $cgi_variables['iuname'];
16 $this->igroup
= $cgi_variables['igroup'];
17 $this->server
= $cgi_variables['server']; // mysql server (usually localhost)
18 $this->loginhost
= $cgi_variables['loginhost']; // php/apache server (usually localhost)
19 $this->port
= $cgi_variables['port'];
20 $this->root
= $cgi_variables['root'];
21 $this->rootpass
= $cgi_variables['rootpass'];
22 $this->login
= $cgi_variables['login'];
23 $this->pass
= $cgi_variables['pass'];
24 $this->dbname
= $cgi_variables['dbname'];
25 $this->collate
= $cgi_variables['collate'];
26 $this->site
= $cgi_variables['site'];
27 $this->source_site_id
= $cgi_variables['source_site_id'];
28 $this->clone_database
= $cgi_variables['clone_database'];
29 $this->development_translations
= $cgi_variables['development_translations'];
31 // Make this true for IPPF.
32 $this->ippf_specific
= false;
34 // Record name of sql access file
35 $GLOBALS['OE_SITES_BASE'] = dirname(__FILE__
) . '/../../sites';
36 $GLOBALS['OE_SITE_DIR'] = $GLOBALS['OE_SITES_BASE'] . '/' . $this->site
;
37 $this->conffile
= $GLOBALS['OE_SITE_DIR'] . '/sqlconf.php';
39 // Record names of sql table files
40 $this->main_sql
= dirname(__FILE__
) . '/../../sql/database.sql';
41 $this->translation_sql
= dirname(__FILE__
) . '/../../contrib/util/language_translations/currentLanguage_utf8.sql';
42 $this->devel_translation_sql
= "http://github.com/openemr/translations_development_openemr/raw/master/languageTranslations_utf8.sql";
43 $this->ippf_sql
= dirname(__FILE__
) . "/../../sql/ippf_layout.sql";
44 $this->icd9
= dirname(__FILE__
) . "/../../sql/icd9.sql";
45 $this->cvx
= dirname(__FILE__
) . "/../../sql/cvx_codes.sql";
46 $this->additional_users
= dirname(__FILE__
) . "/../../sql/official_additional_users.sql";
48 // Record name of php-gacl installation files
49 $this->gaclSetupScript1
= dirname(__FILE__
) . "/../../gacl/setup.php";
50 $this->gaclSetupScript2
= dirname(__FILE__
) . "/../../acl_setup.php";
52 // Prepare the dumpfile list
53 $this->initialize_dumpfile_list();
55 // Entities to hold error and debug messages
56 $this->error_message
= '';
57 $this->debug_message
= '';
59 // Entity to hold sql connection
63 public function login_is_valid()
65 if ( ($this->login
== '') ||
(! isset( $this->login
)) ) {
66 $this->error_message
= "login is invalid: '$this->login'";
72 public function iuser_is_valid()
74 if ( strpos($this->iuser
, " ") ) {
75 $this->error_message
= "Initial user is invalid: '$this->iuser'";
81 public function password_is_valid()
83 if ( $this->pass
== "" ||
!isset($this->pass
) ) {
84 $this->error_message
= "The password for the new database account is invalid: '$this->pass'";
90 public function user_password_is_valid()
92 if ( $this->iuserpass
== "" ||
!isset($this->iuserpass
) ) {
93 $this->error_message
= "The password for the user is invalid: '$this->iuserpass'";
99 public function root_database_connection()
101 $this->dbh
= $this->connect_to_database( $this->server
, $this->root
, $this->rootpass
, $this->port
);
105 $this->error_message
= 'unable to connect to database as root';
110 public function user_database_connection()
112 $this->dbh
= $this->connect_to_database( $this->server
, $this->login
, $this->pass
, $this->port
);
113 if ( ! $this->dbh
) {
114 $this->error_message
= "unable to connect to database as user: '$this->login'";
117 if ( ! $this->set_collation() ) {
120 if ( ! mysql_select_db($this->dbname
, $this->dbh
) ) {
121 $this->error_message
= "unable to select database: '$this->dbname'";
127 public function create_database() {
128 $sql = "create database $this->dbname";
129 if ($this->collate
) {
130 $sql .= " character set utf8 collate $this->collate";
131 $this->set_collation();
133 return $this->execute_sql($sql);
136 public function drop_database() {
137 $sql = "drop database if exists $this->dbname";
138 return $this->execute_sql($sql);
141 public function grant_privileges() {
142 return $this->execute_sql( "GRANT ALL PRIVILEGES ON $this->dbname.* TO '$this->login'@'$this->loginhost' IDENTIFIED BY '$this->pass'" );
145 public function disconnect() {
146 return mysql_close($this->dbh
);
150 * This method creates any dumpfiles necessary.
151 * This is actually only done if we're cloning an existing site
152 * and we need to dump their database into a file.
153 * @return bool indicating success
155 public function create_dumpfiles() {
156 return $this->dumpSourceDatabase();
159 public function load_dumpfiles() {
160 $sql_results = ''; // information string which is returned
161 foreach ($this->dumpfiles
as $filename => $title) {
162 $sql_results .= $this->load_file($filename,$title);
163 if ($sql_results == FALSE) return FALSE;
168 public function load_file($filename,$title) {
169 $sql_results = ''; // information string which is returned
170 $sql_results .= "Creating $title tables...\n";
171 $fd = fopen($filename, 'r');
173 $this->error_message
= "ERROR. Could not open dumpfile '$filename'.\n";
179 $line = fgets($fd,1024);
180 $line = rtrim($line);
181 if (substr($line,0,2) == "--") // Kill comments
183 if (substr($line,0,1) == "#") // Kill comments
187 $query = $query.$line; // Check for full query
188 $chr = substr($query,strlen($query)-1,1);
189 if ($chr == ";") { // valid query, execute
190 $query = rtrim($query,";");
191 $this->execute_sql( $query );
195 $sql_results .= "OK<br>\n";
200 public function add_version_info() {
201 include dirname(__FILE__
) . "/../../version.php";
202 if ($this->execute_sql("UPDATE version SET v_major = '$v_major', v_minor = '$v_minor', v_patch = '$v_patch', v_realpatch = '$v_realpatch', v_tag = '$v_tag', v_database = '$v_database', v_acl = '$v_acl'") == FALSE) {
203 $this->error_message
= "ERROR. Unable insert version information into database\n" .
204 "<p>".mysql_error()." (#".mysql_errno().")\n";
210 public function add_initial_user() {
211 if ($this->execute_sql("INSERT INTO groups (id, name, user) VALUES (1,'$this->igroup','$this->iuser')") == FALSE) {
212 $this->error_message
= "ERROR. Unable to add initial user group\n" .
213 "<p>".mysql_error()." (#".mysql_errno().")\n";
216 $password_hash = sha1( $this->iuserpass
);
217 if ($this->execute_sql("INSERT INTO users (id, username, password, authorized, lname, fname, facility_id, calendar, cal_ui) VALUES (1,'$this->iuser','$password_hash',1,'$this->iuname','',3,1,3)") == FALSE) {
218 $this->error_message
= "ERROR. Unable to add initial user\n" .
219 "<p>".mysql_error()." (#".mysql_errno().")\n";
222 // Add the official openemr users (services)
223 if ($this->load_file($this->additional_users
,"Additional Official Users") == FALSE) return FALSE;
229 * Create site directory if it is missing.
230 * @global string $GLOBALS['OE_SITE_DIR'] contains the name of the site directory to create
231 * @return name of the site directory or False
233 public function create_site_directory() {
234 if (!file_exists($GLOBALS['OE_SITE_DIR'])) {
235 $source_directory = $GLOBALS['OE_SITES_BASE'] . "/" . $this->source_site_id
;
236 $destination_directory = $GLOBALS['OE_SITE_DIR'];
237 if ( ! $this->recurse_copy( $source_directory, $destination_directory ) ) {
238 $this->error_message
= "unable to copy directory: '$source_directory' to '$destination_directory'. " . $this->error_message
;
245 public function write_configuration_file() {
246 @touch
($this->conffile
); // php bug
247 $fd = @fopen
($this->conffile
, 'w');
249 $this->error_message
= 'unable to open configuration file for writing: ' . $this->conffile
;
258 $it_died = 0; //fmg: variable keeps running track of any errors
260 fwrite($fd,$string) or $it_died++
;
261 fwrite($fd,"\$host\t= '$this->server';\n") or $it_died++
;
262 fwrite($fd,"\$port\t= '$this->port';\n") or $it_died++
;
263 fwrite($fd,"\$login\t= '$this->login';\n") or $it_died++
;
264 fwrite($fd,"\$pass\t= '$this->pass';\n") or $it_died++
;
265 fwrite($fd,"\$dbase\t= '$this->dbname';\n\n") or $it_died++
;
266 fwrite($fd,"//Added ability to disable\n") or $it_died++
;
267 fwrite($fd,"//utf8 encoding - bm 05-2009\n") or $it_died++
;
268 fwrite($fd,"global \$disable_utf8_flag;\n") or $it_died++
;
269 fwrite($fd,"\$disable_utf8_flag = false;\n") or $it_died++
;
274 $sqlconf["host"]= $host;
275 $sqlconf["port"] = $port;
276 $sqlconf["login"] = $login;
277 $sqlconf["pass"] = $pass;
278 $sqlconf["dbase"] = $dbase;
279 //////////////////////////
280 //////////////////////////
281 //////////////////////////
282 //////DO NOT TOUCH THIS///
283 $config = 1; /////////////
284 //////////////////////////
285 //////////////////////////
286 //////////////////////////
289 ?
><?php
// done just for coloring
291 fwrite($fd,$string) or $it_died++
;
292 fclose($fd) or $it_died++
;
294 //it's rather irresponsible to not report errors when writing this file.
296 $this->error_message
= "ERROR. Couldn't write $it_died lines to config file '$this->conffile'.\n";
303 public function insert_globals() {
304 function xl($s) { return $s; }
305 require(dirname(__FILE__
) . '/../globals.inc.php');
306 foreach ($GLOBALS_METADATA as $grpname => $grparr) {
307 foreach ($grparr as $fldid => $fldarr) {
308 list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
309 if (substr($fldtype, 0, 2) !== 'm_') {
310 $res = $this->execute_sql("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
311 $row = @mysql_fetch_array
($res, MYSQL_ASSOC
);
312 if (empty($row['count'])) {
313 $this->execute_sql("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
314 "VALUES ( '$fldid', '0', '$flddef' )");
322 public function install_gacl()
324 $install_results_1 = $this->get_require_contents($this->gaclSetupScript1
);
325 if (! $install_results_1 ) {
326 $this->error_message
= "install_gacl failed: unable to require gacl script 1";
330 $install_results_2 = $this->get_require_contents($this->gaclSetupScript2
);
331 if (! $install_results_2 ) {
332 $this->error_message
= "install_gacl failed: unable to require gacl script 2";
335 $this->debug_message
.= $install_results_1 . $install_results_2;
339 public function quick_install() {
340 // Validation of OpenEMR user settings
341 // (applicable if not cloning from another database)
342 if (empty($this->clone_database
)) {
343 if ( ! $this->login_is_valid() ) {
346 if ( ! $this->iuser_is_valid() ) {
349 if ( ! $this->user_password_is_valid() ) {
353 // Validation of mysql database password
354 if ( ! $this->password_is_valid() ) {
357 // Connect to mysql via root user
358 if (! $this->root_database_connection() ) {
361 // Create the dumpfile
362 // (applicable if cloning from another database)
363 if (! empty($this->clone_database
)) {
364 if ( ! $this->create_dumpfiles() ) {
368 // Create the site directory
369 // (applicable if mirroring another local site)
370 if ( ! empty($this->source_site_id
) ) {
371 if ( ! $this->create_site_directory() ) {
375 // Create the mysql database
376 if ( ! $this->create_database()) {
379 // Grant user privileges to the mysql database
380 if ( ! $this->grant_privileges() ) {
383 // Connect to mysql via created user
385 if ( ! $this->user_database_connection() ) {
388 // Build the database
389 if ( ! $this->load_dumpfiles() ) {
392 // Write the sql configuration file
393 if ( ! $this->write_configuration_file() ) {
396 // Load the version information, globals settings,
397 // initial user, and set up gacl access controls.
398 // (applicable if not cloning from another database)
399 if (empty($this->clone_database
)) {
400 if ( ! $this->add_version_info() ) {
403 if ( ! $this->insert_globals() ) {
406 if ( ! $this->add_initial_user() ) {
409 if ( ! $this->install_gacl()) {
417 private function execute_sql( $sql ) {
418 $this->error_message
= '';
419 if ( ! $this->dbh
) {
420 $this->user_database_connection();
422 $results = mysql_query($sql, $this->dbh
);
426 $this->error_message
= "unable to execute SQL: '$sql' due to: " . mysql_error();
431 private function connect_to_database( $server, $user, $password, $port )
433 if ($server == "localhost")
434 $dbh = mysql_connect($server, $user, $password);
436 $dbh = mysql_connect("$server:$port", $user, $password);
440 private function set_collation()
442 if ($this->collate
) {
443 return $this->execute_sql("SET NAMES 'utf8'");
449 * innitialize $this->dumpfiles, an array of the dumpfiles that will
450 * be loaded into the database, including the correct translation
452 * The keys are the paths of the dumpfiles, and the values are the titles
455 private function initialize_dumpfile_list() {
456 if ( $this->clone_database
) {
457 $this->dumpfiles
= array( $this->get_backup_filename() => 'clone database' );
459 $dumpfiles = array( $this->main_sql
=> 'Main' );
460 if (! empty($this->development_translations
)) {
461 // Use the online development translation set
462 $dumpfiles[ $this->devel_translation_sql
] = "Online Development Language Translations (utf8)";
465 // Use the local translation set
466 $dumpfiles[ $this->translation_sql
] = "Language Translation (utf8)";
468 if ($this->ippf_specific
) {
469 $dumpfiles[ $this->ippf_sql
] = "IPPF Layout";
471 // Load ICD-9 codes if present.
472 if (file_exists( $this->icd9
)) {
473 $dumpfiles[ $this->icd9
] = "ICD-9";
475 // Load CVX codes if present
476 if (file_exists( $this->cvx
)) {
477 $dumpfiles[ $this->cvx
] = "CVX Immunization Codes";
479 $this->dumpfiles
= $dumpfiles;
481 return $this->dumpfiles
;
484 // http://www.php.net/manual/en/function.include.php
485 private function get_require_contents($filename) {
486 if (is_file($filename)) {
489 $contents = ob_get_contents();
498 * Directory copy logic borrowed from a user comment at
499 * http://www.php.net/manual/en/function.copy.php
500 * @param string $src name of the directory to copy
501 * @param string $dst name of the destination to copy to
502 * @return bool indicating success
504 private function recurse_copy($src, $dst) {
505 $dir = opendir($src);
506 if ( ! @mkdir
($dst) ) {
507 $this->error_message
= "unable to create directory: '$dst'";
510 while(false !== ($file = readdir($dir))) {
511 if ($file != '.' && $file != '..') {
512 if (is_dir($src . '/' . $file)) {
513 $this->recurse_copy($src . '/' . $file, $dst . '/' . $file);
516 copy($src . '/' . $file, $dst . '/' . $file);
526 * dump a site's database to a temporary file.
527 * @param string $source_site_id the site_id of the site to dump
528 * @return filename of the backup
530 private function dumpSourceDatabase() {
531 global $OE_SITES_BASE;
532 $source_site_id = $this->source_site_id
;
534 include("$OE_SITES_BASE/$source_site_id/sqlconf.php");
536 if (empty($config)) die("Source site $source_site_id has not been set up!");
538 $backup_file = $this->get_backup_filename();
539 $cmd = "mysqldump -u " . escapeshellarg($login) .
540 " -p" . escapeshellarg($pass) .
541 " --opt --skip-extended-insert --quote-names -r $backup_file " .
542 escapeshellarg($dbase);
544 $tmp0 = exec($cmd, $tmp1=array(), $tmp2);
545 if ($tmp2) die("Error $tmp2 running \"$cmd\": $tmp0 " . implode(' ', $tmp1));
551 * @return filename of the source backup database for cloning
553 private function get_backup_filename() {
554 if (stristr(PHP_OS
, 'WIN')) {
555 $backup_file = 'C:/windows/temp/setup_dump.sql';
558 $backup_file = '/tmp/setup_dump.sql';
566 This file is free software: you can redistribute it and/or modify it under the
567 terms of the GNU General Public License as publish by the Free Software
570 This file is distributed in the hope that it will be useful, but WITHOUT ANY
571 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
572 PARTICULAR PURPOSE. See the GNU Gneral Public License for more details.
574 You should have received a copy of the GNU General Public Licence along with
575 this file. If not see <http://www.gnu.org/licenses/>.