April 14, 2014

April 14, 2014
2
In this article, we are going to discuss about How to develop a web installer for a CodeIgniter (CI) application. When you installing a CMS on your server, usually you need to find a form for setting up your database settings, admin user credentials, SMTP details..etc. This option can be found on popular open source CMS by default. This article might help after you finish create your own custom web project and don't want to make confuse your client with manual install by editing php file.

This script will change some of default CodeIgniter (CI) setting, they are

  1. - index.php
  2. - application/config/config.php
  3. - application/config/database.php

Just make sure they are writable (I'm using windows, no permission needs to be changed)

Step 1 : Create MY_Install library (MY_Install.php)

Create a file named "MY_Install.php" on your codeigniter application library, I assume we have a default codeigniter database setting, so this script will check if database name are blank we can continue on installation, by redirecting the page to install folder.

<?php
class MY_Install {
    public function __construct() {
        $CI =& get_instance();
        $CI->load->database();
        if ($CI->db->database == "") {
            header('location:install/');
        } else {
            if (is_dir('install')) {
                echo '<i>Plese delete or rename <b>Install</b> folder</i>';
                exit;
            }
        }
    }
}

Step 2 : Include the library on autoload.php

$autoload['libraries'] = array('database', 'MY_Install');

Step 3 : Create an install folder


After an install folder is created, create an index file which consists of form script for database setting and php script for replacing the default codeigniter settings.

Form script

<?php
$error = 0;
if (isset($_POST['btn-install'])) {
    
    // validation
    if ($_POST['inputDBhost'] == '' || $_POST['inputDBusername'] == '' || 
            $_POST['inputDBname'] == '' ||
            $_POST['inputSiteurl'] == '' || $_POST['inputAppfolder'] == '' || 
            $_POST['inputSystemfolder'] == '' || 
            ($_POST['inputAppfolder'] == $_POST['inputSystemfolder'])) {
        $error = 1;
    } else {
        
        @$con = mysql_connect($_POST['inputDBhost'], $_POST['inputDBusername'], $_POST['inputDBpassword']);
        @$db_selected = mysql_select_db($_POST['inputDBname'], $con);

        if (!$con) {
            $error = 1;
        } else if (!$db_selected) {  
            $error = 1;
        } else {
            // setting site url
            $file_config = "../application/config/config.php";
            $content_config = file_get_contents($file_config);
            $content_config .= "\n\$config['base_url'] = '".$_POST['inputSiteurl']."';";
            file_put_contents($file_config, $content_config);

            // setting database
            $file_db = "../application/config/database.php";
            $content_db = file_get_contents($file_db);
            $content_db .= "\n\$db['default']['hostname'] = '".$_POST['inputDBhost']."';";
            $content_db .= "\n\$db['default']['username'] = '".$_POST['inputDBusername']."';";
            $content_db .= "\n\$db['default']['password'] = '".$_POST['inputDBpassword']."';";
            $content_db .= "\n\$db['default']['database'] = '".$_POST['inputDBname']."';";
            file_put_contents($file_db, $content_db);

            // setting folder name
            $file_index = "../index.php";
            $content_index = str_replace("\$system_path = 'system';", "\$system_path = '".$_POST['inputSystemfolder']."';", file_get_contents($file_index));
            file_put_contents($file_index, $content_index);
            $content_index = str_replace("\$application_folder = 'application';", "\$application_folder = '".$_POST['inputAppfolder']."';", file_get_contents($file_index));
            file_put_contents($file_index, $content_index);

            // rename app folder
            $index = str_replace('install', '', dirname(__FILE__));
            rename($index.'application', $index.$_POST['inputAppfolder']);
            rename($index.'system',      $index.$_POST['inputSystemfolder']);
            header('location:../');
        }
    }
}

?>

PHP Script

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="bootstrap.css"/>
</head>
<body>
<div class="container">
    <div class="row-fluid">
    <h4>Install Your Codeigniter App</h4>
    <form class="form-horizontal" action="" method="post" style="margin-top:30px;">

        <?php if ($error == 1): ?>
        <div class="alert alert-error" style="font-size:11px;">               
            <b>Opps error ... </b> please check: 
            <br/> - <i>Each fields cannot be blank</i>
            <br/> - <i>App Folder and System Folder cannot the same</i>
            <br/> - <i>Database name must exist on your MySQL</i>
        </div>
        <?php endif; ?>
        <legend>Database Settings</legend>
        <div class="control-group">
            <label class="control-label">DB Host</label>
            <div class="controls">
                <input type="text" name="inputDBhost">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Name</label>
            <div class="controls">
                <input type="text" name="inputDBname">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Username</label>
            <div class="controls">
                <input type="text" name="inputDBusername">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Password</label>
            <div class="controls">
                <input type="password" name="inputDBpassword">
            </div>
        </div>

        <legend>App Settings</legend>
        <div class="control-group">
            <label class="control-label">Site URL</label>
            <div class="controls">
                <input type="text" name="inputSiteurl">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">App Folder</label>
            <div class="controls">
                <input type="text" name="inputAppfolder">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">System Folder</label>
            <div class="controls">
                <input type="text" name="inputSystemfolder">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input type="reset" class="btn" name="btn" value="Reset"/>
                <input type="submit" class="btn btn-primary" name="btn-install" value="Install"/>
            </div>
        </div>
    </form>
    </div>
</div>
</body>
</html>

That's all after the installation finish it will redirect to index page again and ask you to rename or remove install folder, you can add more validation by yourself or even other settings for common web application, below are the example screenshot.

2 comments:

  1. how to set application cation folder, mean to say what we should have to putt in app folder and system folder. path or any thing else

    ReplyDelete