• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

Auto Site Generator

in Project codes

This code, when executed will create sub-sites of pre-defined pattern set on the configuration page. The module will also imports the exported database to the newly created sub-sites, and sets the db url in settings.php, created in each sub site directory. But before you start using the module, you should fulfill the following requirements:
Requirements

  • You should have created the sub-domains matching the full sib site with sub-site pattern
  • You should have created database with full database name with database pattern.
  • Where ever you want to create the sub sites, the permissions of the directory must be set to 0777. You can change it later though.

<?php
//$Id$

/**
* @file
*
* Automtically generate sub site with prepopulated identical databases
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/




/**
* Imeplementation of hook_help
*
* @param string $path
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function auto_site_help($path) {
    switch (
$path) {
        case
'admin/settings/auto-site':
           
$output = "<p>". t("Configure paths for MySQL, pattern file and the path to the directory to which the exported db will be stored.") ."</p>";
            return
$output;
    }
}


/**
* Implementation of hook_menu
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function auto_site_menu() {
   
$items = array();
   
$items['admin/settings/auto-site'] = array(
   
'title' => 'Auto site generator',
     
'title callback' => 't',
     
'page callback' => 'drupal_get_form',
     
'page arguments' => array('auto_site_admin_form'),
     
'access arguments' => array('Administer auto site generator'),
    );
   
   
$items['admin/settings/auto-site/configure'] = array(
     
'type' => MENU_DEFAULT_LOCAL_TASK,
     
'title' => 'Configure',
     
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('auto_site_admin_form'),
   
'access arguments' => array('Administer auto site generator'),
     
'weight' => -10,
    );
   
   
$items['admin/settings/auto-site/generate'] = array(
     
'type' => MENU_LOCAL_TASK,
     
'title' => 'Generate sites',
     
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('auto_site_genrate_form'),
   
'access arguments' => array('Administer auto site generator'),
     
'weight' => -9
   
);
   
//    $items['admin/settings/auto-site/import'] = array(
//      'type' => MENU_LOCAL_TASK,
//      'title' => 'Import',
//      'title callback' => 't',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('auto_site_admin_form'),
//    'access arguments' => array('Administer auto site generator'),
//      'weight' => -8
//    );
//   
//    $items['admin/settings/auto-site/export'] = array(
//      'type' => MENU_LOCAL_TASK,
//      'title' => 'Export',
//      'title callback' => 't',
//    'page callback' => 'drupal_get_form',
//    'page arguments' => array('auto_site_admin_form'),
//    'access arguments' => array('Administer auto site generator'),
//      'weight' => -7
//    );
   
return $items;
}



/**
* Implementation of hook_perm
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function auto_site_perm() {
    return array(
t("Administer auto site generator"));
}



/**
* auto_site_admin_form
*
* Return configuration form for auto site
*
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function auto_site_admin_form() {
   
$form = array();
   
   
$form['auto_mysql_path'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Path to the MySQL executable"),
     
'#default_value' => variable_get('auto_mysql_path', 'mysql'),
     
'#description' => t("Provide the path to the MySQL executable.
      In most enviromnets, its just mysql."
),
    
'#required' => TRUE,
    
'#weight' => -10,
    );
   
   
$form['auto_site_pattern'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Sub site pattern"),
     
'#default_value' => variable_get("auto_site_pattern", ''),
     
'#description' => t("Please provide the ptattern to create new sub-sites.
        This can be a number like 1000. The sub sites will be create in pattern 1000.example.com, 1001.example.com etc. "
),
     
'#required' => TRUE,
     
'#weight' => -9,
    );
   
   
$form['auto_site_count'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Number of sub sites"),
     
'#default_value' => variable_get("auto_site_count", 10),
     
'#description' => t("Please provide a value for the number of sub sites you want to create at once."),
     
'#required' => TRUE,
     
'#weight' => -9,
    );
   
   
$form['auto_db_pattern'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Database pattern"),
     
'#default_value' => variable_get("auto_db_pattern", ''),
     
'#description' => t("Please provide the ptattern to create new databases. These databases should be created before auto site generator runs"),
     
'#required' => TRUE,
     
'#weight' => -8,
    );
   
   
$form['auto_domain'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Domain"),
     
'#default_value' => variable_get("auto_domain", ''),
     
'#description' => t("Please provide the domain name"),
     
'#required' => TRUE,
     
'#weight' => -7,
    );
   
//    $form['auto_mysql_import_path'] = array(
//      '#type' => 'textfield',
//      '#title' => t("Import directory path"),
//      '#default_value' => variable_get("auto_mysql_import_path", ''),
//      '#description' => t("Path to the directory from which the sql files will be imported."),
//      '#required' => TRUE,
//      '#weight' => -8,
//    );
   
   
$form['auto_upload_path'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Upload directory path"),
     
'#default_value' => variable_get("auto_upload_path", ''),
     
'#description' => t("Provide the path of the directory to which to export generated sub sites along with db snap."),
     
'#required' => TRUE,
     
'#weight' => -6,
    );
   
    return
system_settings_form($form);
}



function
auto_site_genrate_form() {
   
$form = array();
   
   
$form['auto_mysql_db'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Database file path"),
     
'#description' => t("Provide the path to the database file from which to create new databases."),
     
'#weight' => -10,
    );
   
   
$form['auto_mysql_username'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Database username"),
     
'#description' => t("Provide the username to access the databases."),
     
'#required' => TRUE,
     
'#weight' => -9,
    );
   
   
$form['auto_mysql_password'] = array(
     
'#type' => 'password',
     
'#title' => t("Database password"),
     
'#description' => t("Provide password to access the database."),
     
'#weight' => -8,
    );
   
   
$form['auto_mysql_host'] = array(
     
'#type' => 'textfield',
     
'#title' => t("Host"),
     
'#description' => t("Provide database host name."),
     
'#default_value' => 'localhost',
     
'#required' => TRUE,
     
'#weight' => -7,
    );
   
   
$form['generate'] = array(
     
'#type' => 'submit',
     
'#value' => t("Generate"),
    );
   
   
$form['#submit'] = array('auto_site_generate');
    return
$form;
}



/**
* auto_site_generate
*
* Form submit handler
*
* @param $form
* @param $form_values
* @author Drupal Developer <http://twitter.com/DrupalD>
*/
function auto_site_generate($form, $form_values) {
    require_once (
"includes/install.inc");
   
$ss__site_pattern = variable_get('auto_site_pattern', '');
   
$ss__site_count = variable_get('auto_site_count', 10);
   
$ss__domain = variable_get('auto_domain', '');
    if (
$ss__site_pattern) {
       
$ss__db_pattern = variable_get('auto_db_pattern', '');
       
$ss__mysql_path = variable_get('auto_mysql_path', '');
       
$ss__upload_path = variable_get('auto_upload_path', '');
       
$ss__db_path = $form_values['values']['auto_mysql_db'];
       
$ss__db_user = $form_values['values']['auto_mysql_username'];
       
$ss__db_password = $form_values['values']['auto_mysql_password'];
       
$ss__db_host = $form_values['values']['auto_mysql_host'];
       
$ss__source = "sites/default/default.settings.php";
       
$ss__db = $ss__db_pattern .".". $ss__domain;
    if (
$ss__db_password) {
     
$ss__db_string = '$db_url ='. "\'mysqli://$ss__db_user:$ss__db_password@$ss__db_host/$ss__db\';";
    }
    else {
     
$ss__db_string = '$db_url = '. "'mysqli://$ss__db_user@$ss__db_host/$ss__db';";
    }
        for (
$i = $ss__site_pattern; $i < ($ss__site_pattern + $ss__site_count); $i++) {
           
$ss__path = $ss__upload_path ."/". $i . ".". $ss__domain;
           
$ss__file_path = $ss__path ."/files";
           
file_check_directory($ss__path, 1);
           
$h__write_file = fopen($ss__path ."/settings.php", "w+");
           
$h__read_file = fopen($ss__source, "r");
            while (!
feof($h__read_file)) {
               
$output = fread($h__read_file, 4096);
               
               
$output = preg_replace('/\$db_url.*/', $ss__db_string, $output);
               
fwrite($h__write_file, $output, 4096);
            }
           
fclose($h__read_file);
           
fclose($h__write_file);
           
file_check_directory($ss__file_path, 1);
            if (
$ss__db_password) {
               
exec("$ss__mysql_path --default-character-set=utf8 -h $ss__db_host -u $ss__db_user --password= $ss__db_password ". $ss__site_pattern. "_dd" ." < $ss__db_path 2<&1", $output, $return);
            }
            else {
               
exec("$ss__mysql_path --default-character-set=utf8 -h $ss__db_host -u $ss__db_user ". $ss__site_pattern. "_dd" ." < $ss__db_path 2<&1" , $output, $return);
            }
     }
    }
}
?>

5
Your rating: None Average: 5 (3 votes)

Comments

joshi's picture
5

This is really great code to replicate the site. If integrated with cpanel/plesk panel API, there would be no need to manually creating database adn creating sub domains.

We have been working with cpanel and plesk panel api to achieve the same. We have used this code to start and it has saved our lot of time. Thank you Drupal Developer.

Regards.

[url=http://joshics.in]JoshicsIN[/url]

beautifulmind's picture
5

This is really great CodeLet. I would love to replicate sites.

Please remove the commented code and list all the requirements to make this CodeLet working

Regards.

DrupalD's picture

Thank you very much for your concern.
We will enahnce all the CodeLet details.

Thank you.

[url=http://twitter.com/DrupalD][i]Drupa Developer - A Comprehensive Guild to Drupal Developersl[/i][/url]

Syndicate content

Recent comments