forked from kalamuna/terminatur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminatur.settings.inc
158 lines (141 loc) · 5.54 KB
/
terminatur.settings.inc
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @file
* settings.inc: Helper functions to manipulate settings.php
*/
// Constants
define('TERMINATUR_VALIDATION_STRING', 'Terminatur: 1b f3 18 cd 6b 59 45 55 d1 ae');
/**
* Validates the settings file
*/
function _terminatur_settings_validate($settings_file) {
// Check for the terminatur signature
$settings = file_get_contents($settings_file);
if (strpos($settings, TERMINATUR_VALIDATION_STRING) !== false) {
// Settings have been touched by the terminatur before
// But let's verify they still work
$databases = array();
$drupal_6 = FALSE;
// Get Drupal 6 stuff
if (strpos($settings, "db_url") !== false) {
$drupal_6 = TRUE;
$db_url = '';
$db_prefix = '';
}
//Stub function to avoid error on validations.
function drupal_fast_404(){}
// Load settings file
require $settings_file;
// Parse drupal 6
if ($drupal_6) {
$databases = _terminatur_data_parse_db_url($db_url, $db_prefix);
}
// Return false if there are no DB settings to be found
if (!isset($databases)) {
return FALSE;
}
return _terminatur_data_test_db_connection($databases);
}
return FALSE;
}
/**
* Append local settings to a drupal settings file
*/
function _terminatur_settings_build($settings_file, &$databases) {
if (!_terminatur_data_test_db_connection($databases)) {
return FALSE;
}
// Determine drupal version
$settings = file_get_contents($settings_file);
$drupal_version = '7';
if (strpos($settings, "db_url") !== false) {
$drupal_version = '6';
}
$build_settings_func = '_terminatur_settings_build_' . $drupal_version;
// Make sure the file is writable
if (!chmod($settings_file, 0644)) {
return drush_set_error('TERMINATUR_ERROR', 'Settings file needs to be writable.');
}
$settings_file = $build_settings_func($settings_file, $databases);
return _terminatur_settings_validate($settings_file);
}
/**
* Helper file to append Drupal 6 style settings
*/
function _terminatur_settings_build_6($settings_file, &$databases) {
// Open this way so we can append
$fh = fopen($settings_file, 'a') or die("can't open file");
$output = '';
// Build the DB string
$db_url = $databases['default']['default']['driver'] . '://' . $databases['default']['default']['username'] . ':' . $databases['default']['default']['password'] . '@' . $databases['default']['default']['host']. ':' . $databases['default']['default']['port'] . '/' . $databases['default']['default']['database'];
// Generate the output
$output .= "\n";
$output .= "/**\n";
$output .= " * " . TERMINATUR_VALIDATION_STRING . "\n";
$output .= " *\n";
$output .= " * These local settings were generated by terminatur.\n";
$output .= " * You may see them if you use Kalastack, Kalabox, Proviso or other local dev tools.\n";
$output .= " *\n";
$output .= " */\n";
$output .= TERMINATUR_SETTINGS_CONDITIONAL . "\n";
$output .= " // DB String\n";
$output .= " \$db_url = '" . $db_url . "';\n";
$output .= "\n";
$output .= " Set some common desirable local vars\n";
$output .= " \$conf['cache'] = 0;\n";
$output .= " \$conf['css_gzip_compression'] = FALSE;\n";
$output .= " \$conf['js_gzip_compression'] = FALSE;\n";
$output .= " \$conf['googleanalytics_account'] = '';\n";
$output .= "}\n";
$output .= "\n";
$settings = $output;
fwrite($fh, $settings);
fclose($fh);
return $settings_file;
}
/**
* Helper file to append Drupal 7 style settings
*/
function _terminatur_settings_build_7($settings_file, &$databases) {
// Open this way so we can append
$fh = fopen($settings_file, 'a') or die("can't open file");
$output = '';
// Generate the output
$output .= "\n";
$output .= "/**\n";
$output .= " * " . TERMINATUR_VALIDATION_STRING . "\n";
$output .= " *\n";
$output .= " * These local settings were generated by terminatur.\n";
$output .= " * You may see them if you use Kalastack, Kalabox, Proviso or other local dev tools.\n";
$output .= " *\n";
$output .= " */\n";
$output .= TERMINATUR_SETTINGS_CONDITIONAL . "\n";
$output .= " // DB Array and some common conf\n";
$output .= " \$databases['default']['default'] = array(\n";
$output .= " 'driver' => '" . $databases['default']['default']['driver'] . "',\n";
$output .= " 'database' => '" . $databases['default']['default']['database'] . "',\n";
$output .= " 'username' => '" . $databases['default']['default']['username'] . "',\n";
$output .= " 'password' => '" . $databases['default']['default']['password'] . "',\n";
$output .= " 'host' => '" . $databases['default']['default']['host'] . "',\n";
$output .= " 'port' => '" . $databases['default']['default']['port'] . "',\n";
$output .= " 'prefix' => '',\n";
$output .= " );\n";
$output .= "\n";
$output .= " // Set some common desirable local vars\n";
$output .= " \$conf['file_temporary_path'] = '/tmp';\n";
$output .= " \$conf['file_public_path'] = 'sites/default/files';\n";
$output .= " \$conf['file_private_path'] = 'sites/default/files/private';\n";
$output .= " \$conf['reroute_email_enable'] = 1;\n";
$output .= " \$conf['cache'] = 0;\n";
$output .= " \$conf['css_gzip_compression'] = FALSE;\n";
$output .= " \$conf['js_gzip_compression'] = FALSE;\n";
$output .= " \$conf['preprocess_css'] = FALSE;\n";
$output .= " \$conf['preprocess_js'] = FALSE;\n";
$output .= " \$conf['googleanalytics_account'] = '';\n";
$output .= " \$conf['views_ui_show_sql_query'] = 1;\n";
$output .= "}\n";
$settings = $output;
fwrite($fh, $settings);
fclose($fh);
return $settings_file;
}