PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/installer/libraries/installer_lib.php

http://github.com/pyrocms/pyrocms
PHP | 426 lines | 247 code | 66 blank | 113 comment | 27 complexity | 484564d9a1b7328f7c2a3496a4d95118 MD5 | raw file
Possible License(s): CC0-1.0, MIT
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * @author Phil Sturgeon - PyroCMS development team
  4. * @author Victor Michnowicz
  5. * @package PyroCMS
  6. * @subpackage Installer
  7. *
  8. * @since v0.9.8
  9. *
  10. */
  11. class Installer_lib {
  12. private $ci;
  13. public $php_version;
  14. public $mysql_server_version;
  15. public $mysql_client_version;
  16. public $gd_version;
  17. function __construct()
  18. {
  19. $this->ci =& get_instance();
  20. }
  21. // Functions used in Step 1
  22. /**
  23. * @return bool
  24. *
  25. * Function to see if the PHP version is acceptable (at least version 5)
  26. */
  27. function php_acceptable($version = NULL)
  28. {
  29. // Set the PHP version
  30. $this->php_version = phpversion();
  31. // Is this version of PHP greater than minimum version required?
  32. return ( version_compare(PHP_VERSION, $version, '>=') ) ? TRUE : FALSE;
  33. }
  34. /**
  35. * @param string $type The MySQL type, client or server
  36. * @return string The MySQL version of either the server or the client
  37. *
  38. * Function to retrieve the MySQL version (client/server)
  39. */
  40. public function mysql_acceptable($type = 'server')
  41. {
  42. // Server version
  43. if ($type == 'server')
  44. {
  45. // Retrieve the database settings from the session
  46. $server = $this->ci->session->userdata('hostname') . ':' . $this->ci->session->userdata('port');
  47. $username = $this->ci->session->userdata('username');
  48. $password = $this->ci->session->userdata('password');
  49. // Connect to MySQL
  50. if ( $db = @mysql_connect($server,$username,$password) )
  51. {
  52. $this->mysql_server_version = @mysql_get_server_info($db);
  53. // Close the connection
  54. @mysql_close($db);
  55. // If the MySQL server version is at least version 5 return TRUE, else FALSE
  56. return ($this->mysql_server_version >= 5) ? TRUE : FALSE;
  57. }
  58. else
  59. {
  60. @mysql_close($db);
  61. return FALSE;
  62. }
  63. }
  64. // Client version
  65. else
  66. {
  67. // Get the version
  68. $this->mysql_client_version = preg_replace('/[^0-9\.]/','', mysql_get_client_info());
  69. // If the MySQL client version is at least version 5 return TRUE, else FALSE
  70. return ($this->mysql_client_version >= 5) ? TRUE : FALSE;
  71. }
  72. }
  73. /**
  74. * @return string The GD library version.
  75. *
  76. * Function to retrieve the GD library version
  77. */
  78. public function gd_acceptable()
  79. {
  80. // Get if the gd_info() function exists
  81. if (function_exists('gd_info'))
  82. {
  83. $gd_info = gd_info();
  84. $this->gd_version = preg_replace('/[^0-9\.]/','',$gd_info['GD Version']);
  85. // If the GD version is at least 1.0 return TRUE, else FALSE
  86. return ($this->gd_version >= 1) ? TRUE : FALSE;
  87. }
  88. // Homeboy is not rockin GD at all
  89. else
  90. {
  91. return FALSE;
  92. }
  93. }
  94. /**
  95. * @return bool
  96. *
  97. * Function to check if zlib is installed
  98. */
  99. public function zlib_enabled()
  100. {
  101. return extension_loaded('zlib');
  102. }
  103. /**
  104. * @param string $data The data that contains server related information.
  105. * @return bool
  106. *
  107. * Function to validate the server settings.
  108. */
  109. public function check_server($data)
  110. {
  111. // Check PHP
  112. if ( ! $this->php_acceptable() )
  113. {
  114. return FALSE;
  115. }
  116. // Check MySQL server
  117. if ( ! $this->mysql_acceptable('server') )
  118. {
  119. return FALSE;
  120. }
  121. // Check MySQL client
  122. if ( ! $this->mysql_acceptable('client') )
  123. {
  124. return FALSE;
  125. }
  126. if ($data->http_server->supported === FALSE)
  127. {
  128. return FALSE;
  129. }
  130. // If PHP, MySQL, etc is good but either server, GD, and/or Zlib is unknown, say partial
  131. if ( $data->http_server->supported === 'partial' || $this->gd_acceptable() === FALSE || $this->zlib_enabled() === FALSE)
  132. {
  133. return 'partial';
  134. }
  135. // Must be fine
  136. return TRUE;
  137. }
  138. /**
  139. * @param string $server_name The name of the HTTP server such as Abyss, Cherokee, Apache, etc
  140. * @return array
  141. *
  142. * Function to validate whether the specified server is a supported server. The function returns an array with two keys: supported and non_apache.
  143. * The supported key will be set to TRUE whenever the server is supported. The non_apache server will be set to TRUE whenever the user is using a server other than Apache.
  144. * This enables the system to determine whether mod_rewrite should be used or not.
  145. */
  146. public function verify_http_server($server_name)
  147. {
  148. // Set all the required variables
  149. if ($server_name == 'other')
  150. {
  151. return 'partial';
  152. }
  153. $supported_servers = $this->ci->config->item('supported_servers');
  154. return array_key_exists($server_name, $supported_servers);
  155. }
  156. /**
  157. * @return bool
  158. * Make sure the database name is a valid mysql identifier
  159. *
  160. */
  161. public function validate_mysql_db_name($db_name)
  162. {
  163. $expr = '/[^A-Za-z0-9_-]+/';
  164. return !(preg_match($expr,$db_name)>0);
  165. }
  166. /**
  167. * @return mixed
  168. *
  169. * Make sure we can connect to the database
  170. */
  171. public function test_db_connection()
  172. {
  173. $hostname = $this->ci->session->userdata('hostname');
  174. $username = $this->ci->session->userdata('username');
  175. $password = $this->ci->session->userdata('password');
  176. $port = $this->ci->session->userdata('port');
  177. return @mysql_connect("$hostname:$port", $username, $password);
  178. }
  179. /**
  180. * @param string $data The data from the form
  181. * @return array
  182. *
  183. * Install the PyroCMS database and write the database.php file
  184. */
  185. public function install($data)
  186. {
  187. // Retrieve the database server, username and password from the session
  188. $server = $this->ci->session->userdata('hostname') . ':' . $this->ci->session->userdata('port');
  189. $username = $this->ci->session->userdata('username');
  190. $password = $this->ci->session->userdata('password');
  191. $database = $data['database'];
  192. // User settings
  193. $user_salt = substr(md5(uniqid(rand(), true)), 0, 5);
  194. $data['user_password'] = sha1($data['user_password'] . $user_salt);
  195. // Include migration config to know which migration to start from
  196. include '../system/cms/config/migration.php';
  197. // Get the SQL for the default data and parse it
  198. $user_sql = file_get_contents('./sql/default.sql');
  199. $user_sql = str_replace('{PREFIX}', $data['site_ref'].'_', $user_sql);
  200. $user_sql = str_replace('{EMAIL}', $data['user_email'], $user_sql);
  201. $user_sql = str_replace('{USER-NAME}', mysql_escape_string($data['user_name']), $user_sql);
  202. $user_sql = str_replace('{DISPLAY-NAME}', mysql_escape_string($data['user_firstname'] . ' ' . $data['user_lastname']), $user_sql);
  203. $user_sql = str_replace('{PASSWORD}', mysql_escape_string($data['user_password']), $user_sql);
  204. $user_sql = str_replace('{FIRST-NAME}', mysql_escape_string($data['user_firstname']), $user_sql);
  205. $user_sql = str_replace('{LAST-NAME}', mysql_escape_string($data['user_lastname']) , $user_sql);
  206. $user_sql = str_replace('{SALT}', $user_salt, $user_sql);
  207. $user_sql = str_replace('{NOW}', time(), $user_sql);
  208. $user_sql = str_replace('{MIGRATION}', $config['migration_version'], $user_sql);
  209. // Create a connection
  210. if ( ! $this->db = mysql_connect($server, $username, $password) )
  211. {
  212. return array('status' => FALSE,'message' => 'The installer could not connect to the MySQL server or the database, be sure to enter the correct information.');
  213. }
  214. if ($this->mysql_server_version >= '5.0.7')
  215. {
  216. @mysql_set_charset('utf8', $this->db);
  217. }
  218. // Do we want to create the database using the installer ?
  219. if ( ! empty($data['create_db'] ))
  220. {
  221. mysql_query('CREATE DATABASE IF NOT EXISTS '.$database, $this->db);
  222. }
  223. // Select the database we created before
  224. if ( !mysql_select_db($database, $this->db) )
  225. {
  226. return array(
  227. 'status' => FALSE,
  228. 'message' => '',
  229. 'code' => 101
  230. );
  231. }
  232. if ( ! $this->_process_schema($user_sql, FALSE) )
  233. {
  234. return array(
  235. 'status' => FALSE,
  236. 'message' => mysql_error($this->db),
  237. 'code' => 104
  238. );
  239. }
  240. mysql_query(sprintf(
  241. "INSERT INTO core_sites (name, ref, domain, created_on) VALUES ('Default Site', '%s', '%s', '%s');",
  242. $data['site_ref'],
  243. preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']),
  244. time()
  245. ));
  246. // If we got this far there can't have been any errors. close and bail!
  247. mysql_close($this->db);
  248. // Write the database file
  249. if ( ! $this->write_db_file($database) )
  250. {
  251. return array(
  252. 'status' => FALSE,
  253. 'message' => '',
  254. 'code' => 105
  255. );
  256. }
  257. // Write the config file.
  258. if ( ! $this->write_config_file() )
  259. {
  260. return array(
  261. 'status' => FALSE,
  262. 'message' => '',
  263. 'code' => 106
  264. );
  265. }
  266. return array('status' => TRUE);
  267. }
  268. private function _process_schema($schema_file, $is_file = TRUE)
  269. {
  270. // String or file?
  271. if ( $is_file == TRUE )
  272. {
  273. $schema = file_get_contents('./sql/' . $schema_file . '.sql');
  274. }
  275. else
  276. {
  277. $schema = $schema_file;
  278. }
  279. // Parse the queries
  280. $queries = explode('-- command split --', $schema);
  281. foreach($queries as $query)
  282. {
  283. $query = rtrim( trim($query), "\n;");
  284. @mysql_query($query, $this->db);
  285. if (mysql_errno($this->db) > 0)
  286. {
  287. return FALSE;
  288. }
  289. }
  290. return TRUE;
  291. }
  292. /**
  293. * @param string $database The name of the database
  294. *
  295. * Writes the database file based on the provided database settings
  296. */
  297. function write_db_file($database)
  298. {
  299. // First retrieve all the required data from the session and the $database variable
  300. $server = $this->ci->session->userdata('hostname');
  301. $username = $this->ci->session->userdata('username');
  302. $password = $this->ci->session->userdata('password');
  303. $port = $this->ci->session->userdata('port');
  304. // Open the template file
  305. $template = file_get_contents('./assets/config/database.php');
  306. $replace = array(
  307. '__HOSTNAME__' => $server,
  308. '__USERNAME__' => $username,
  309. '__PASSWORD__' => $password,
  310. '__DATABASE__' => $database,
  311. '__PORT__' => $port ? $port : 3306
  312. );
  313. // Replace the __ variables with the data specified by the user
  314. $new_file = str_replace(array_keys($replace), $replace, $template);
  315. // Open the database.php file, show an error message in case this returns false
  316. $handle = @fopen('../system/cms/config/database.php','w+');
  317. // Validate the handle results
  318. if ($handle !== FALSE)
  319. {
  320. return @fwrite($handle, $new_file);
  321. }
  322. return FALSE;
  323. }
  324. /**
  325. * @return bool
  326. *
  327. * Writes the config file.n
  328. */
  329. function write_config_file()
  330. {
  331. // Open the template
  332. $template = file_get_contents('./assets/config/config.php');
  333. $server_name = $this->ci->session->userdata('http_server');
  334. $supported_servers = $this->ci->config->item('supported_servers');
  335. // Able to use clean URLs?
  336. if ($supported_servers[$server_name]['rewrite_support'] !== FALSE)
  337. {
  338. $index_page = '';
  339. }
  340. else
  341. {
  342. $index_page = 'index.php';
  343. }
  344. // Replace the __INDEX__ with index.php or an empty string
  345. $new_file = str_replace('__INDEX__', $index_page, $template);
  346. // Open the database.php file, show an error message in case this returns false
  347. $handle = @fopen('../system/cms/config/config.php','w+');
  348. // Validate the handle results
  349. if ($handle !== FALSE)
  350. {
  351. return fwrite($handle, $new_file);
  352. }
  353. return FALSE;
  354. }
  355. public function curl_enabled()
  356. {
  357. return (bool) function_exists('curl_init');
  358. }
  359. }
  360. /* End of file installer_lib.php */