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