/application/modules/install/controllers/install.php

https://github.com/arimogi/Chimera · PHP · 112 lines · 82 code · 18 blank · 12 comment · 8 complexity · 5b59138a4e1170e7a9d7d1a888867844 MD5 · raw file

  1. <?php (defined('BASEPATH')) OR exit('No direct script access allowed');
  2. if ( ! class_exists('Controller'))
  3. {
  4. class Controller extends CI_Controller {}
  5. }
  6. /**
  7. * Description of install
  8. *
  9. * @author gofrendi
  10. */
  11. class install extends Controller {
  12. private $db;
  13. public function __construct(){
  14. parent::__construct();
  15. $this->load->library('form_validation');
  16. $this->load->library('session');
  17. $this->load->helper('html');
  18. $this->load->helper('url');
  19. $this->load->helper('form');
  20. }
  21. public function index(){
  22. //view configuration form
  23. $data = array ("message" => $this->session->flashdata('message'));
  24. $this->load->view('install', $data);
  25. }
  26. public function apply(){
  27. $correct = true;
  28. $message = "";
  29. //examine configuration input
  30. $this->session->set_userdata($this->input->post());
  31. if(!is_writeable('./application/config')){
  32. $correct = false;
  33. $message .= 'Directory application/config is not writeable'.br();
  34. }
  35. if(!$this->test_db_connection()){
  36. $correct = false;
  37. $message .= 'Cannot connect to database'.br();
  38. }
  39. $this->session->set_flashdata('message',$message);
  40. //if correct, install, else, redirect to index
  41. if($correct){
  42. //config.php
  43. $str = file_get_contents('./application/modules/install/assets/config.php');
  44. file_put_contents('./application/config/config.php', $str);
  45. //database.php
  46. $server = $this->session->userdata('server');
  47. $username = $this->session->userdata('username');
  48. $password = $this->session->userdata('password');
  49. $port = $this->session->userdata('port');
  50. $database = $this->session->userdata('database');
  51. $str = file_get_contents('./application/modules/install/assets/database.php');
  52. $str = str_replace('@SERVER', "$server:$port", $str);
  53. $str = str_replace('@DATABASE', $database, $str);
  54. $str = str_replace('@USERNAME', $username, $str);
  55. $str = str_replace('@PASSWORD', $password, $str);
  56. file_put_contents('./application/config/database.php', $str);
  57. //route.php
  58. $str = file_get_contents('./application/modules/install/assets/routes.php');
  59. file_put_contents('./application/config/routes.php', $str);
  60. //database
  61. $str = file_get_contents('./application/modules/install/assets/data.sql');
  62. $queries = explode('--split--', $str);
  63. foreach($queries as $query)
  64. {
  65. $query = rtrim( trim($query), "\n;");
  66. @mysql_query($query, $this->db);
  67. if (mysql_errno($this->db) > 0)
  68. {
  69. echo "database installation failed, please install by yourself".br();
  70. }
  71. }
  72. echo "Installed successfully".br();
  73. echo "delete /application/modules/install".br();
  74. echo "perform chmod 755 * -R".br();
  75. echo anchor("main","Click this link to see the chimera");
  76. }
  77. else{
  78. echo "not correct";
  79. redirect('install/index', 'refresh');
  80. }
  81. }
  82. private function test_db_connection(){
  83. $server = $this->session->userdata('server');
  84. $username = $this->session->userdata('username');
  85. $password = $this->session->userdata('password');
  86. $port = $this->session->userdata('port');
  87. $database = $this->session->userdata('database');
  88. $this->db = @mysql_connect("$server:$port", $username, $password);
  89. if (!$this->db) return false;
  90. if (@mysql_select_db($database, $this->db)) return true;
  91. else return false;
  92. }
  93. }
  94. ?>