summaryrefslogtreecommitdiffstats
path: root/provision_mysql.module
blob: 538050c358f252d5437660e8425492b0b8d8beeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
 * @file
 *    Mysql provisioning module.
 *
 * The goal of this module is to create mysql databases and user accounts, for sites that are about to be created.
 * It uses the provision API to tie into the right places in the site creation work flow.
 */
 
/**
 * @ingroup provisionui
 * @{
 */
  
/**
 * Implementation of provision_service()
 */
function provision_mysql_provision_service() {
  return t("Mysql database server");
}

/**
 * Implementation of provision_configure
 */
function provision_mysql_provision_configure() {
  $form['provision_mysql_user'] = array(
    '#type' => 'textfield',
    '#required' => TRUE,
    '#title' => t('Mysql user account'),
    '#description' => t('The user that will be used to create users and databases for new sites.'),
    '#size' => 40,
    '#default_value' => variable_get('provision_mysql_user', 'root'),
    '#maxlength' => 255,
  );
  $form['provision_mysql_password'] = array(
    '#type' => 'password',
    '#required' => TRUE,
    '#title' => t('Mysql user password'),
    '#description' => t('The user account that will be used to create new mysql users and databases for new sites'),
    '#size' => 30,
    '#maxlength' => 64,
  );
  
  $form['provision_mysql_host'] = array(
    '#type' => 'textfield',
    '#title' => t('Mysql server hostname'),
    '#description' => t('The mysql server to connect to.'),
    '#size' => 30,
    '#default_value' => variable_get('provision_mysql_host', 'localhost'),
    '#maxlength' => 64,
  );
  return $form;
}
/**
 * @} end "ingroup provisionui"
 */


function provision_mysql_provision_pre_install($url, &$data) {
  $data['site-db-type'] = 'mysql'; # only support innodb. for now.
  $data['site-db-host'] = ($data['site-db-host']) ? $data['site-db-host'] : variable_get('provision_mysql_host', 'localhost');
  $data['site-db-passwd'] = user_password(); # generate a random password for use
  if ($data['site_id']) {
    $data['site-db-name'] = 'site_' . $data['site_id'];
    $data['site-db-username'] = $data['site-db-name']; // mysql has some really really stupid rules about who db / usernames, so site id is the safest.
  }
  else {
    $data['site-db-name'] = substr(ereg_replace("^www\.", "", str_replace(".", "", $url)), 0, 10);
    $data['site-db-username'] = $data['site-db-name'];
    // TODO : A reasonable fallback if the site id isn't available. This is going to make it a bit harder to test at first, but that's ok.
  }

  # For this to work, the user account the provisioning site has been set up with, requires CREATE database permissions.
  # TODO : Add additional configuration for a database account to use for these , but this is the quickest way to get the code up and running.
  $db_url = sprintf("mysqli://%s:%s@%s/mysql",  variable_get('provision_mysql_user', 'root'), variable_get('provision_mysql_password', 'root'), $data['site-db-host']  );  
  
  if ( db_result(db_query("SHOW DATABASES LIKE '%s'", $data['site-db-name'])) ) {
   db_query("DROP DATABASE %s", $data['site-db-name']);
  }
  
  db_query("CREATE DATABASE %s", $data['site-db-name']);
  
  if ( !db_result(db_query("SHOW DATABASES LIKE '%s'", $data['site-db-name'])) ) {
   provision_set_error(PROVISION_DB_ERROR);
   provision_log("error", "Database could not be created.");

   return FALSE;
  }
  
  db_query("GRANT ALL PRIVILEGES ON %s.* TO %s@`%%` IDENTIFIED BY '%s'", $data['site-db-name'], $data['site-db-username'], $data['mysql_passwd']);
  db_query("GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s'",$data['site-db-name'], $data['site-db-username'], $data['site-db-host'], $data['mysql_passwd']);


  if ($data['site-mysql-old-passwords']) {
    db_query("SET PASSWORD FOR '%s'@'%%' = OLD_PASSWORD('%s')", $data['site-db-username'], $data['site-db-passwd']);
    db_query("SET PASSWORD FOR %s@%s = OLD_PASSWORD('%s')", $data['site-db-username'], $data['site-db-host'], $data['site-db-passwd']);
  }
  db_query("FLUSH PRIVILEGES");
  #TODO : Test to confirm that the database is actually writeable. Taking this on faith for now.
}