PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/dotcloud-scripts/feed-wp-config.php

https://bitbucket.org/jcsarda/wordpress-dotcloud-deployable
PHP | 117 lines | 93 code | 5 blank | 19 comment | 4 complexity | 0165baa5ac979ffe37b1730160f306ba MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of the Wordpress On Dotcloud package.
  5. *
  6. * (c) Quentin Pleplé <quentin.pleple@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. define("ENVIRONMENT_FILE_NAME", dirname(__FILE__) . '/../../environment.json');
  12. define("WP_CONFIG_FILE_NAME", dirname(__FILE__) . '/../wp-config.php');
  13. define("WP_CONFIG_sample_FILE_NAME", dirname(__FILE__) . '/../wp-config-sample.php');
  14. // the name of the database that will be created for wordpress
  15. define("DB_NAME", "wordpress");
  16. define("MSG_PREFIX", "[wordpress-on-dotcloud]  ");
  17. /**********************************
  18. Reading environment variables
  19. **********************************/
  20. if (!file_exists(ENVIRONMENT_FILE_NAME)) {
  21. die(MSG_PREFIX . "Error: File environment.json does not exists. Looking at: " . ENVIRONMENT_FILE_NAME . "\n");
  22. }
  23. $json = @file_get_contents(ENVIRONMENT_FILE_NAME);
  24. if (empty($json)) {
  25. die(MSG_PREFIX . "Error: Can't read environment.json file.\n");
  26. }
  27. echo MSG_PREFIX . "File environment.json found and read\n";
  28. $environment = @json_decode($json);
  29. if (empty($environment)) {
  30. die(MSG_PREFIX . "Error: Content of environment.json is not valid json.\n");
  31. }
  32. $properties = array("DOTCLOUD_DB_MYSQL_LOGIN", "DOTCLOUD_DB_MYSQL_PASSWORD", "DOTCLOUD_DB_MYSQL_HOST", "DOTCLOUD_DB_MYSQL_PORT");
  33. $errorStr = "";
  34. foreach ($properties as $property) {
  35. if (!property_exists($environment, $property)) {
  36. $errorStr .= MSG_PREFIX . "Error: Missing property $property in file environment.json\n";
  37. }
  38. }
  39. if ($errorStr != "") {
  40. die($errorStr);
  41. }
  42. echo MSG_PREFIX . "File environment.json parsed\n";
  43. /**********************************
  44. Opening wp-config.php
  45. **********************************/
  46. if (file_exists(WP_CONFIG_FILE_NAME)) {
  47. $content = @file_get_contents(WP_CONFIG_FILE_NAME);
  48. if (empty($content)) {
  49. echo MSG_PREFIX . "File wp-config.php not found (looking at: " . WP_CONFIG_FILE_NAME . "). Trying to find wp-config-sample.php\n";
  50. }
  51. echo MSG_PREFIX . "File wp-config.php found and read\n";
  52. } else {
  53. echo MSG_PREFIX . "File wp-config.php empty (looking at: " . WP_CONFIG_FILE_NAME . "). Trying to find wp-config-sample.php\n";
  54. }
  55. if (empty($content)) {
  56. if (!file_exists(WP_CONFIG_sample_FILE_NAME)) {
  57. die(MSG_PREFIX . "Error: File wp-config-sample.php not found. Looking at: " . WP_CONFIG_sample_FILE_NAME . "\n");
  58. }
  59. $content = @file_get_contents(WP_CONFIG_sample_FILE_NAME);
  60. if (empty($content)) {
  61. die(MSG_PREFIX . "Error: Can't read wp-config-sample.php file.\n");
  62. }
  63. echo MSG_PREFIX . "File wp-config-sample.php found and read\n";
  64. }
  65. /**********************************
  66. Replacing config values
  67. **********************************/
  68. $configValues = array(
  69. "DB_NAME" => DB_NAME,
  70. "DB_USER" => $environment->DOTCLOUD_DB_MYSQL_LOGIN,
  71. "DB_PASSWORD" => $environment->DOTCLOUD_DB_MYSQL_PASSWORD,
  72. "DB_HOST" => $environment->DOTCLOUD_DB_MYSQL_HOST . ":" . $environment->DOTCLOUD_DB_MYSQL_PORT
  73. );
  74. foreach ($configValues as $property => $value) {
  75. echo MSG_PREFIX . "Setting $property = $value\n";
  76. $count = 0;
  77. $content = preg_replace('/(define\(\'' . $property . '\', \')(.*)(\'\);)/', '${1}' . $value . '${3}', $content, -1, &$count);
  78. if ($count == 0) {
  79. die(MSG_PREFIX . "Error: Property $property not found in wp-config.\n");
  80. }
  81. }
  82. /**********************************
  83. Writing wp-config.php
  84. **********************************/
  85. echo MSG_PREFIX . "Saving modifications\n";
  86. $handler = fopen(WP_CONFIG_FILE_NAME, 'w') or die(MSG_PREFIX . "Error: can't open file wp-config.php to save changes.\n");
  87. fwrite($handler, $content);
  88. fclose($handler);
  89. echo MSG_PREFIX . "Modifications saved.\n";
  90. /**********************************
  91. Creating DB if not exists
  92. **********************************/
  93. echo MSG_PREFIX . "Creating database '" . DB_NAME . "' if not exists\n";
  94. $mysqli = new mysqli(
  95. $environment->DOTCLOUD_DB_MYSQL_HOST,
  96. $environment->DOTCLOUD_DB_MYSQL_LOGIN,
  97. $environment->DOTCLOUD_DB_MYSQL_PASSWORD,
  98. "",
  99. $environment->DOTCLOUD_DB_MYSQL_PORT
  100. );
  101. $mysqli->query('CREATE DATABASE IF NOT EXISTS ' . DB_NAME . ';') or die(MSG_PREFIX . "Error while creating database " . DB_NAME . "\n");
  102. echo MSG_PREFIX . "Database created (if did not exist already)\n";
  103. echo MSG_PREFIX . "Ready to blog!\n";