PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/library/classes/Installer.class.php

https://github.com/prabhupathak/openemr
PHP | 372 lines | 342 code | 24 blank | 6 comment | 31 complexity | 551fe7e8bc648320223476d6a13c0b87 MD5 | raw file
  1. <?php
  2. /* Copyright © 2010 by Andrew Moore */
  3. /* Licensing information appears at the end of this file. */
  4. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) );
  5. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/..');
  6. require_once 'acl.inc';
  7. class Installer
  8. {
  9. public function __construct( $cgi_variables, $manualPath )
  10. {
  11. $this->login = $cgi_variables['login'];
  12. $this->iuser = $cgi_variables['iuser'];
  13. $this->iuname = $cgi_variables['iuname'];
  14. $this->igroup = $cgi_variables['igroup'];
  15. $this->pass = $cgi_variables['pass'];
  16. $this->server = $cgi_variables['server'];
  17. $this->port = $cgi_variables['port'];
  18. $this->root = $cgi_variables['root'];
  19. $this->rootpass = $cgi_variables['rootpass'];
  20. $this->dbname = $cgi_variables['dbname'];
  21. $this->collate = $cgi_variables['collate'];
  22. $this->loginhost = 'localhost';
  23. $this->manualPath = $manualPath;
  24. $this->conffile = $this->manualPath . 'library/sqlconf.php';
  25. $this->openemrBasePath = $cgi_variables["openemrBasePath"];
  26. $this->openemrWebPath = $cgi_variables["openemrWebPath"];
  27. $this->gaclSetupScript1 = $this->manualPath . "gacl/setup.php";
  28. $this->gaclSetupScript2 = $this->manualPath . "acl_setup.php";
  29. // Make this true for IPPF.
  30. $this->ippf_specific = false;
  31. $this->error_message = '';
  32. $this->dbh = false;
  33. include_once($this->conffile);
  34. }
  35. public function login_is_valid()
  36. {
  37. if ( ($this->login == '') || (! isset( $this->login )) ) {
  38. $this->error_message = "login is invalid: '$this->login'";
  39. return FALSE;
  40. }
  41. return TRUE;
  42. }
  43. public function iuser_is_valid()
  44. {
  45. if ( strpos($this->iuser, " ") ) {
  46. $this->error_message = "Initial user is invalid: '$this->iuser'";
  47. return FALSE;
  48. }
  49. return TRUE;
  50. }
  51. public function password_is_valid()
  52. {
  53. if ( $this->pass == "" || !isset($this->pass) ) {
  54. $this->error_message = "Initial user password is invalid: '$this->pass'";
  55. return FALSE;
  56. }
  57. return TRUE;
  58. }
  59. public function root_database_connection()
  60. {
  61. $this->dbh = $this->connect_to_database( $this->server, $this->root, $this->rootpass );
  62. if ( $this->dbh ) {
  63. return $this->dbh;
  64. } else {
  65. $this->error_message = 'unable to connect to database as root';
  66. return False;
  67. }
  68. }
  69. public function user_database_connection()
  70. {
  71. $this->dbh = $this->connect_to_database( $this->server, $this->login, $this->pass );
  72. if ( ! $this->dbh ) {
  73. $this->error_message = "unable to connect to database as user: '$this->login'";
  74. return FALSE;
  75. }
  76. if ( ! $this->set_collation() ) {
  77. return FALSE;
  78. }
  79. if ( ! mysql_select_db($this->dbname, $this->dbh) ) {
  80. $this->error_message = "unable to select database: '$this->dbname'";
  81. return FALSE;
  82. }
  83. return $this->dbh;
  84. }
  85. public function create_database() {
  86. $sql = "create database $this->dbname";
  87. if ($this->collate) {
  88. $sql .= " character set utf8 collate $this->collate";
  89. $this->set_collation();
  90. }
  91. return $this->execute_sql($sql);
  92. }
  93. public function drop_database() {
  94. $sql = "drop database if exists $this->dbname";
  95. return $this->execute_sql($sql);
  96. }
  97. public function grant_privileges() {
  98. return $this->execute_sql( "GRANT ALL PRIVILEGES ON $this->dbname.* TO '$this->login'@'$this->loginhost' IDENTIFIED BY '$this->pass'" );
  99. }
  100. public function disconnect() {
  101. return mysql_close($this->dbh);
  102. }
  103. public function load_dumpfiles() {
  104. $sql_results = ''; // information string which is returned
  105. foreach ($this->dumpfiles() as $filename => $title) {
  106. $sql_results .= "Creating $title tables...\n";
  107. // mysql_query("USE $dbname",$dbh);
  108. $fd = fopen($filename, 'r');
  109. if ($fd == FALSE) {
  110. $this->error_message = "ERROR. Could not open dumpfile '$filename'.\n";
  111. return FALSE;
  112. }
  113. $query = "";
  114. $line = "";
  115. while (!feof ($fd)){
  116. $line = fgets($fd,1024);
  117. $line = rtrim($line);
  118. if (substr($line,0,2) == "--") // Kill comments
  119. continue;
  120. if (substr($line,0,1) == "#") // Kill comments
  121. continue;
  122. if ($line == "")
  123. continue;
  124. $query = $query.$line; // Check for full query
  125. $chr = substr($query,strlen($query)-1,1);
  126. if ($chr == ";") { // valid query, execute
  127. $query = rtrim($query,";");
  128. $this->execute_sql( $query );
  129. $query = "";
  130. }
  131. }
  132. $sql_results .= "OK<br>\n";
  133. fclose($fd);
  134. }
  135. return $sql_results;
  136. }
  137. public function add_initial_user() {
  138. //echo "INSERT INTO groups VALUES (1,'$igroup','$iuser')<br>\n";
  139. if ($this->execute_sql("INSERT INTO groups (id, name, user) VALUES (1,'$this->igroup','$this->iuser')") == FALSE) {
  140. $this->error_message = "ERROR. Unable to add initial user group\n" .
  141. "<p>".mysql_error()." (#".mysql_errno().")\n";
  142. return FALSE;
  143. }
  144. if ($this->execute_sql("INSERT INTO users (id, username, password, authorized, lname, fname, facility_id, calendar, cal_ui) VALUES (1,'$this->iuser','1a1dc91c907325c69271ddf0c944bc72',1,'$this->iuname','',3,1,3)") == FALSE) {
  145. $this->error_message = "ERROR. Unable to add initial user\n" .
  146. "<p>".mysql_error()." (#".mysql_errno().")\n";
  147. return FALSE;
  148. }
  149. return TRUE;
  150. }
  151. public function write_configuration_file() {
  152. @touch($this->conffile); // php bug
  153. $fd = @fopen($this->conffile, 'w');
  154. $string = '<?php
  155. // OpenEMR
  156. // MySQL Config
  157. // Referenced from sql.inc
  158. ';
  159. $it_died = 0; //fmg: variable keeps running track of any errors
  160. fwrite($fd,$string) or $it_died++;
  161. fwrite($fd,"\$host\t= '$this->server';\n") or $it_died++;
  162. fwrite($fd,"\$port\t= '$this->port';\n") or $it_died++;
  163. fwrite($fd,"\$login\t= '$this->login';\n") or $it_died++;
  164. fwrite($fd,"\$pass\t= '$this->pass';\n") or $it_died++;
  165. fwrite($fd,"\$dbase\t= '$this->dbname';\n\n") or $it_died++;
  166. fwrite($fd,"//Added ability to disable\n") or $it_died++;
  167. fwrite($fd,"//utf8 encoding - bm 05-2009\n") or $it_died++;
  168. fwrite($fd,"\$disable_utf8_flag = false;\n") or $it_died++;
  169. $string = '
  170. $sqlconf = array();
  171. $sqlconf["host"]= $host;
  172. $sqlconf["port"] = $port;
  173. $sqlconf["login"] = $login;
  174. $sqlconf["pass"] = $pass;
  175. $sqlconf["dbase"] = $dbase;
  176. //////////////////////////
  177. //////////////////////////
  178. //////////////////////////
  179. //////DO NOT TOUCH THIS///
  180. $config = 1; /////////////
  181. //////////////////////////
  182. //////////////////////////
  183. //////////////////////////
  184. ?>
  185. ';
  186. ?><?php // done just for coloring
  187. fwrite($fd,$string) or $it_died++;
  188. //it's rather irresponsible to not report errors when writing this file.
  189. if ($it_died != 0) {
  190. $this->error_message = "ERROR. Couldn't write $it_died lines to config file '$this->conffile'.\n";
  191. return FALSE;
  192. }
  193. fclose($fd);
  194. return TRUE;
  195. }
  196. public function insert_globals() {
  197. require_once("library/globals.inc.php");
  198. foreach ($GLOBALS_METADATA as $grpname => $grparr) {
  199. foreach ($grparr as $fldid => $fldarr) {
  200. list($fldname, $fldtype, $flddef, $flddesc) = $fldarr;
  201. if (substr($fldtype, 0, 2) !== 'm_') {
  202. $res = $this->execute_sql("SELECT count(*) AS count FROM globals WHERE gl_name = '$fldid'");
  203. $row = @mysql_fetch_array($res, MYSQL_ASSOC);
  204. if (empty($row['count'])) {
  205. $this->execute_sql("INSERT INTO globals ( gl_name, gl_index, gl_value ) " .
  206. "VALUES ( '$fldid', '0', '$flddef' )");
  207. }
  208. }
  209. }
  210. }
  211. }
  212. public function install_gacl()
  213. {
  214. $install_results_1 = $this->get_require_contents($this->gaclSetupScript1);
  215. if (! $install_results_1 ) {
  216. $this->error_message = "install_gacl failed: unable to require gacl script 1";
  217. return FALSE;
  218. }
  219. $install_results_2 = $this->get_require_contents($this->gaclSetupScript2);
  220. if (! $install_results_2 ) {
  221. $this->error_message = "install_gacl failed: unable to require gacl script 2";
  222. return FALSE;
  223. }
  224. // return TRUE;
  225. return $install_results_1 . $install_results_2;;
  226. }
  227. public function configure_gacl()
  228. {
  229. //give the administrator user admin priviledges
  230. $groupArray = array("Administrators");
  231. return set_user_aro($groupArray,$this->iuser,$this->iuname,"","");
  232. }
  233. public function quick_install() {
  234. if ( ! $this->login_is_valid() ) {
  235. return False;
  236. }
  237. if ( ! $this->iuser_is_valid() ) {
  238. return False;
  239. }
  240. if ( ! $this->password_is_valid() ) {
  241. return False;
  242. }
  243. $dbh = $this->root_database_connection();
  244. if ($dbh == FALSE) {
  245. return False;
  246. }
  247. if ( ! $this->create_database()) {
  248. return False;
  249. }
  250. if ( ! $this->grant_privileges() ) {
  251. return False;
  252. }
  253. $this->disconnect();
  254. if ( ! $this->user_database_connection() ) {
  255. return False;
  256. }
  257. $dump_results = $this->load_dumpfiles();
  258. if (! $dump_results ) {
  259. return False;
  260. }
  261. if ( ! $this->add_initial_user() ) {
  262. return False;
  263. }
  264. if ( ! $this->write_configuration_file() ) {
  265. return False;
  266. }
  267. require 'sqlconf.php';
  268. require_once 'translation.inc.php';
  269. $this->insert_globals();
  270. return True;
  271. }
  272. private function execute_sql( $sql ) {
  273. $this->error_message = '';
  274. if ( ! $this->dbh ) {
  275. $this->user_database_connection();
  276. }
  277. $results = mysql_query($sql, $this->dbh);
  278. if ( $results ) {
  279. return $results;
  280. } else {
  281. $this->error_message = "unable to execute SQL: '$sql' due to: " . mysql_error();
  282. return False;
  283. }
  284. }
  285. private function connect_to_database( $server, $user, $password, $port='3306' )
  286. {
  287. if ($server == "localhost")
  288. $dbh = mysql_connect($server, $user, $password);
  289. else
  290. $dbh = mysql_connect("$server:$port", $user, $password);
  291. return $dbh;
  292. }
  293. private function set_collation()
  294. {
  295. if ($this->collate) {
  296. return $this->execute_sql("SET NAMES 'utf8'");
  297. }
  298. return TRUE;
  299. }
  300. // These are the dumpfiles that are loaded into database
  301. // including the correct translation dumpfile
  302. // The keys are the paths of the dumpfiles, and the values are the titles
  303. private function dumpfiles() {
  304. $dumpfiles = array( $this->manualPath."sql/database.sql" => 'Main',
  305. $this->manualPath."contrib/util/language_translations/currentLanguage_utf8.sql" => "Language Translation (utf8)" );
  306. if ($this->ippf_specific) {
  307. $dumpfiles[ $this->manualPath."sql/ippf_layout.sql" ] = "IPPF Layout";
  308. }
  309. return $dumpfiles;
  310. }
  311. // http://www.php.net/manual/en/function.include.php
  312. private function get_require_contents($filename) {
  313. if (is_file($filename)) {
  314. ob_start();
  315. require $filename;
  316. $contents = ob_get_contents();
  317. ob_end_clean();
  318. return $contents;
  319. }
  320. return false;
  321. }
  322. }
  323. /*
  324. This file is free software: you can redistribute it and/or modify it under the
  325. terms of the GNU General Public License as publish by the Free Software
  326. Foundation.
  327. This file is distributed in the hope that it will be useful, but WITHOUT ANY
  328. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  329. PARTICULAR PURPOSE. See the GNU Gneral Public License for more details.
  330. You should have received a copy of the GNU General Public Licence along with
  331. this file. If not see <http://www.gnu.org/licenses/>.
  332. */
  333. ?>