PageRenderTime 165ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/install/libraries/installer_lib.php

https://gitlab.com/rameshgurung/emi
PHP | 301 lines | 243 code | 51 blank | 7 comment | 25 complexity | a791c179a715ea2dccc69ee05c1bbf9f MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Installer_lib {
  3. private $ci;
  4. public $php_version;
  5. public $mysql_server_version;
  6. public $mysql_client_version;
  7. public $gd_version;
  8. function __construct()
  9. {
  10. $this->ci =& get_instance();
  11. }
  12. function php_acceptable($version = NULL)
  13. {
  14. $this->php_version = phpversion();
  15. return ( version_compare(PHP_VERSION, $version, '>=') ) ? TRUE : FALSE;
  16. }
  17. /**
  18. * @param string $type The MySQL type, client or server
  19. * @return string The MySQL version of either the server or the client
  20. *
  21. * Function to retrieve the MySQL version (client/server)
  22. */
  23. public function mysql_acceptable($type = 'server')
  24. {
  25. if ($type == 'server')
  26. {
  27. $server = $this->ci->session->userdata('hostname') . ':' . $this->ci->session->userdata('port');
  28. $username = $this->ci->session->userdata('username');
  29. $password = $this->ci->session->userdata('password');
  30. if ( $db = @mysql_connect($server,$username,$password) )
  31. {
  32. $this->mysql_server_version = @mysql_get_server_info($db);
  33. @mysql_close($db);
  34. return ($this->mysql_server_version >= 5) ? TRUE : FALSE;
  35. }
  36. else
  37. {
  38. @mysql_close($db);
  39. return FALSE;
  40. }
  41. }
  42. else
  43. {
  44. $this->mysql_client_version = preg_replace('/[^0-9\.]/','', mysql_get_client_info());
  45. return ($this->mysql_client_version >= 5) ? TRUE : FALSE;
  46. }
  47. }
  48. public function gd_acceptable()
  49. {
  50. if (function_exists('gd_info'))
  51. {
  52. $gd_info = gd_info();
  53. $this->gd_version = preg_replace('/[^0-9\.]/','',$gd_info['GD Version']);
  54. return ($this->gd_version >= 1) ? TRUE : FALSE;
  55. }
  56. else
  57. {
  58. return FALSE;
  59. }
  60. }
  61. public function zlib_enabled()
  62. {
  63. return extension_loaded('zlib');
  64. }
  65. public function check_server($data)
  66. {
  67. if ( ! $this->php_acceptable($data->php_min_version) )
  68. {
  69. return FALSE;
  70. }
  71. if ( ! $this->mysql_acceptable('server') )
  72. {
  73. return FALSE;
  74. }
  75. if ( ! $this->mysql_acceptable('client') )
  76. {
  77. return FALSE;
  78. }
  79. if ( $this->gd_acceptable() === FALSE || $this->zlib_enabled() === FALSE)
  80. {
  81. return 'partial';
  82. }
  83. return TRUE;
  84. }
  85. public function verify_http_server($server_name)
  86. {
  87. if ($server_name == 'other')
  88. {
  89. return 'partial';
  90. }
  91. $supported_servers = $this->ci->config->item('supported_servers');
  92. return array_key_exists($server_name, $supported_servers);
  93. }
  94. public function validate_mysql_db_name($db_name)
  95. {
  96. $expr = '/[^A-Za-z0-9_-]+/';
  97. return !(preg_match($expr,$db_name)>0);
  98. }
  99. public function test_db_connection()
  100. {
  101. $hostname = $this->ci->session->userdata('hostname');
  102. $username = $this->ci->session->userdata('username');
  103. $password = $this->ci->session->userdata('password');
  104. $port = $this->ci->session->userdata('port');
  105. return @mysql_connect("$hostname:$port", $username, $password);
  106. }
  107. public function install($data)
  108. {
  109. $server = $this->ci->session->userdata('hostname') . ':' . $this->ci->session->userdata('port');
  110. $username = $this->ci->session->userdata('username');
  111. $password = $this->ci->session->userdata('password');
  112. $database = $data['database'];
  113. $user_salt = substr(md5(uniqid(rand(), true)), 0, 5);
  114. $data['user_password'] = sha1($data['user_password'] . $user_salt);
  115. include '../system/cms/config/migration.php';
  116. $user_sql = file_get_contents('./sql/default.sql');
  117. $user_sql = str_replace('{PREFIX}', $data['site_ref'].'_', $user_sql);
  118. $user_sql = str_replace('{EMAIL}', $data['user_email'], $user_sql);
  119. $user_sql = str_replace('{USER-NAME}', mysql_escape_string($data['user_name']), $user_sql);
  120. $user_sql = str_replace('{DISPLAY-NAME}', mysql_escape_string($data['user_firstname'] . ' ' . $data['user_lastname']), $user_sql);
  121. $user_sql = str_replace('{PASSWORD}', mysql_escape_string($data['user_password']), $user_sql);
  122. $user_sql = str_replace('{FIRST-NAME}', mysql_escape_string($data['user_firstname']), $user_sql);
  123. $user_sql = str_replace('{LAST-NAME}', mysql_escape_string($data['user_lastname']) , $user_sql);
  124. $user_sql = str_replace('{SALT}', $user_salt, $user_sql);
  125. $user_sql = str_replace('{NOW}', time(), $user_sql);
  126. $user_sql = str_replace('{MIGRATION}', $config['migration_version'], $user_sql);
  127. if ( ! $this->db = mysql_connect($server, $username, $password) )
  128. {
  129. return array('status' => FALSE,'message' => 'The installer could not connect to the MySQL server or the database, be sure to enter the correct information.');
  130. }
  131. if ($this->mysql_server_version >= '5.0.7')
  132. {
  133. @mysql_set_charset('utf8', $this->db);
  134. }
  135. if ( ! empty($data['create_db'] ))
  136. {
  137. mysql_query('CREATE DATABASE IF NOT EXISTS '.$database, $this->db);
  138. }
  139. if ( !mysql_select_db($database, $this->db) )
  140. {
  141. return array(
  142. 'status' => FALSE,
  143. 'message' => '',
  144. 'code' => 101
  145. );
  146. }
  147. if ( ! $this->_process_schema($user_sql, FALSE) )
  148. {
  149. return array(
  150. 'status' => FALSE,
  151. 'message' => mysql_error($this->db),
  152. 'code' => 104
  153. );
  154. }
  155. mysql_query(sprintf(
  156. "INSERT INTO core_sites (name, ref, domain, created_on) VALUES ('Default Site', '%s', '%s', '%s');",
  157. $data['site_ref'],
  158. preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']),
  159. time()
  160. ));
  161. mysql_close($this->db);
  162. if ( ! $this->write_db_file($database) )
  163. {
  164. return array(
  165. 'status' => FALSE,
  166. 'message' => '',
  167. 'code' => 105
  168. );
  169. }
  170. if ( ! $this->write_config_file() )
  171. {
  172. return array(
  173. 'status' => FALSE,
  174. 'message' => '',
  175. 'code' => 106
  176. );
  177. }
  178. return array('status' => TRUE);
  179. }
  180. private function _process_schema($schema_file, $is_file = TRUE)
  181. {
  182. if ( $is_file == TRUE )
  183. {
  184. $schema = file_get_contents('./sql/' . $schema_file . '.sql');
  185. }
  186. else
  187. {
  188. $schema = $schema_file;
  189. }
  190. $queries = explode('-- command split --', $schema);
  191. foreach($queries as $query)
  192. {
  193. $query = rtrim( trim($query), "\n;");
  194. @mysql_query($query, $this->db);
  195. if (mysql_errno($this->db) > 0)
  196. {
  197. return FALSE;
  198. }
  199. }
  200. return TRUE;
  201. }
  202. function write_db_file($database)
  203. {
  204. $server = $this->ci->session->userdata('hostname');
  205. $username = $this->ci->session->userdata('username');
  206. $password = $this->ci->session->userdata('password');
  207. $port = $this->ci->session->userdata('port');
  208. $template = file_get_contents('./assets/config/database.php');
  209. $replace = array(
  210. '__HOSTNAME__' => $server,
  211. '__USERNAME__' => $username,
  212. '__PASSWORD__' => $password,
  213. '__DATABASE__' => $database,
  214. '__PORT__' => $port ? $port : 3306
  215. );
  216. $new_file = str_replace(array_keys($replace), $replace, $template);
  217. $handle = @fopen('../system/cms/config/database.php','w+');
  218. if ($handle !== FALSE)
  219. {
  220. return @fwrite($handle, $new_file);
  221. }
  222. return FALSE;
  223. }
  224. function write_config_file()
  225. {
  226. $template = file_get_contents('./assets/config/config.php');
  227. $server_name = $this->ci->session->userdata('http_server');
  228. $supported_servers = $this->ci->config->item('supported_servers');
  229. if ($supported_servers[$server_name]['rewrite_support'] !== FALSE)
  230. {
  231. $index_page = '';
  232. }
  233. else
  234. {
  235. $index_page = 'index.php';
  236. }
  237. $new_file = str_replace('__INDEX__', $index_page, $template);
  238. $handle = @fopen('../system/cms/config/config.php','w+');
  239. if ($handle !== FALSE)
  240. {
  241. return fwrite($handle, $new_file);
  242. }
  243. return FALSE;
  244. }
  245. public function curl_enabled()
  246. {
  247. return (bool) function_exists('curl_init');
  248. }
  249. }
  250. /* End of file installer_lib.php */