/system/core/setup.php

https://github.com/fryed/Brick_cms · PHP · 212 lines · 115 code · 55 blank · 42 comment · 10 complexity · d80fa831aa1d7af8f8c0df47edf260fa MD5 · raw file

  1. <?php
  2. //-----CHECKS FOR DATABASE AND INIT SETUP IF NOT FOUND-----//
  3. class setup {
  4. //set vars
  5. var $host;
  6. var $posts;
  7. var $username;
  8. var $password;
  9. var $database;
  10. var $setupErrors;
  11. //setup
  12. public function setup(){
  13. //set error var
  14. $this->setupErrors = array();
  15. //custom error handler
  16. $this->ERRORS = new errorHandler();
  17. $this->ERRORS->logErrors = true;
  18. $this->ERRORS->userType = "developer";
  19. $this->ERRORS->sendEmail = false;
  20. set_error_handler(array(&$this->ERRORS,"customErrors"));
  21. }
  22. //check for db
  23. public function checkSetup() {
  24. //set db host
  25. $this->host = $_SERVER["SERVER_NAME"];
  26. //check if can connect to DB host
  27. mysql_connect($this->host,$this->username,$this->password) or $this->showSetup();
  28. //check if can select database
  29. mysql_select_db($this->database) or $this->showSetup();
  30. }
  31. //show setup
  32. public function showSetup(){
  33. //new smarty instance for setup
  34. $SETUP = new Smarty();
  35. $SETUP->compile_check = false;
  36. $SETUP->caching = false;
  37. $SETUP->debugging = false;
  38. $SETUP->template_dir = "system/admin_templates";
  39. $SETUP->compile_dir = "system/lib/php/smarty/compile";
  40. //get page url
  41. $url = explode("?",$_SERVER["REQUEST_URI"]);
  42. $this->url = $url[0];
  43. //make posts safe
  44. $this->posts = $this->validate($this->posts);
  45. //set setup stage var
  46. if(!isset($_SESSION["setup_stage"]))
  47. $_SESSION["setup_stage"] = 1;
  48. //check for add user
  49. if(isset($this->posts["DB_adduser"]) && !isset($this->setupErrors["blank_field"]))
  50. $this->addUser();
  51. //check create db
  52. if(isset($this->posts["DB_create"]) && !isset($this->setupErrors["blank_field"]))
  53. $this->setupDB();
  54. //catch all php errors
  55. $phpErrors = $this->ERRORS->getErrors();
  56. //loop errors and convert mysql user error to more readable error
  57. $i = 0;
  58. foreach($phpErrors as $error){
  59. $this->setupErrors[] = $error;
  60. }
  61. foreach($this->setupErrors as $error){
  62. $userError = strpos($error,"mysql_connect()");
  63. $dbError = strpos($error,"to database");
  64. $existsError = strpos($error,"database exists");
  65. if($userError !== false){
  66. $this->setupErrors[$i] = "Error: Access denied for user '".$this->username."' using password '".$this->password."'. Please check that the user exists and the password is correct.";
  67. $_SESSION["setup_stage"] = 1;
  68. }elseif($dbError !== false){
  69. $this->setupErrors[$i] = "Error: User '".$this->username."' does not have sufficient privilages to create database '".$this->database."'. Please make sure that the user has 'CREATE' privilages.";
  70. }elseif($existsError !== false){
  71. $this->setupErrors[$i] = $this->setupErrors[$i];
  72. }else{
  73. unset($this->setupErrors[$i]);
  74. }
  75. $i++;
  76. }
  77. //set setup array
  78. $setupArray = array();
  79. $setupArray["step"] = $_SESSION["setup_stage"];
  80. $setupArray["errors"] = $this->setupErrors;
  81. //set home path
  82. $home = "http://".$_SERVER["SERVER_NAME"].str_replace("/index.php","",$_SERVER["SCRIPT_NAME"]);
  83. //assign smarty vars
  84. $SETUP->assign("setup" , $setupArray);
  85. $SETUP->assign("HOME" , $home);
  86. //display page
  87. $SETUP->display("setup.tpl");
  88. exit;
  89. }
  90. //add user
  91. public function addUser(){
  92. //add user to config file
  93. $config = array();
  94. $config["DBusername"] = $this->posts["DB_user"];
  95. $config["DBpassword"] = $this->posts["DB_pass"];
  96. $config["DBname"] = "";
  97. //write to file
  98. $this->writeConfig($config);
  99. //set session stage 2
  100. $_SESSION["setup_stage"] = 2;
  101. //reload page
  102. header("Location: http://".$this->host.$this->url);
  103. exit;
  104. }
  105. //create db
  106. public function setupDB(){
  107. //set db var
  108. $this->database = $this->posts["DB_name"];
  109. //create db
  110. mysql_query("CREATE DATABASE $this->database") or $this->setupErrors[] = mysql_error();
  111. //select database
  112. mysql_select_db($this->database) or die("Error: Could not select database. " . mysql_error());
  113. //if no errors save db to config and create db
  114. if(!$this->setupErrors){
  115. //add db to config file
  116. $config = array();
  117. $config["DBusername"] = $this->username;
  118. $config["DBpassword"] = $this->password;
  119. $config["DBname"] = $this->posts["DB_name"];
  120. //write to file
  121. $this->writeConfig($config);
  122. //include file to write db
  123. include_once("system/core/setup_database.php");
  124. //unset session stage
  125. unset($_SESSION["setup_stage"]);
  126. //reload page
  127. header("Location: http://".$this->host.$this->url);
  128. exit;
  129. }
  130. }
  131. //write the config file
  132. public function writeConfig($data){
  133. //turn data to string
  134. $confData = "<?php\n";
  135. foreach($data as $key => $value){
  136. $confData = $confData."$".$key." = \"".$value."\";\n";
  137. }
  138. $confData = $confData."?>";
  139. //write to config file
  140. $config = "system/config/config.php";
  141. $fh = fopen($config,"w") or die("Error: error opening config file.");
  142. fwrite($fh,$confData);
  143. fclose($fh);
  144. }
  145. //function to make posts safe and check for empties
  146. public function validate($posts){
  147. //loop posts
  148. $newPosts = array();
  149. foreach($posts as $key => $post){
  150. if($post == "")
  151. $this->setupErrors["blank_field"] = "Error: Please ensure all fields are completed.";
  152. $newPosts[$key] = addslashes($post);
  153. }
  154. return $newPosts;
  155. }
  156. }
  157. ?>