This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
configurationProxy.php
executable file
·369 lines (313 loc) · 11.6 KB
/
configurationProxy.php
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/php
<?php
// TYPO3 Configuration Manipulation Helper
// written by Oliver Salzburg
define( "SELF", basename( __FILE__ ) );
define( "INVNAME", $argv[ 0 ] );
/**
* Show the help for this script
* @name string The name of this script as invoked by the user (usually $argv[0])
*/
function showHelp( $name ) {
echo <<<EOS
Usage: $name [OPTIONS] [--extension=]EXTKEY
Core:
--help Display this help and exit.
--verbose Display more detailed messages.
--quiet Do not display anything.
--force Perform actions that would otherwise abort the script.
--update Tries to update the script to the latest version.
--update-check Checks if a newer version of the script is available.
--export-config Prints the default configuration of this script.
--extract-config Extracts configuration parameters from TYPO3.
--base=PATH The name of the base path where TYPO3 is
installed. If no base is supplied, "typo3" is used.
Options:
--dump Prints out a dump of the current configuration.
--get=SETTING Retrieves the value for the configuration item SETTING.
--set=SETTING Marks SETTING to be changed by --value.
--value=VALUE Sets previously marked setting to VALUE.
--merge Merge the AdditionalConfiguration file into the
LocalConfiguration when applicable.
EOS;
}
/**
* Print the default configuration to ease creation of a config file.
*/
function exportConfig() {
$config = "";
preg_match_all( "/# Script Configuration start.+?# Script Configuration end/ms", file_get_contents( INVNAME ), $config );
$_configuration = preg_replace( "/\\$(?P<name>[^=]+)\s*=\s*\"(?P<value>[^\"]*)\";/", "\\1=\\2", $config[ 0 ][ 1 ] );
echo $_configuration;
}
function extractConfig() {
echo "Extracting a configuration is currently not supported!\n";
echo "Please use another script to extract the configuration.\n";
return;
}
# Script Configuration start
# Should the script give more detailed feedback?
$VERBOSE="false";
# Should the script surpress all feedback?
$QUIET="false";
# Should the script ignore reasons that would otherwise cause it to abort?
$FORCE="false";
# The base directory where Typo3 is installed
$BASE="typo3";
# The hostname of the MySQL server that Typo3 uses
$HOST="localhost";
# The username used to connect to that MySQL server
$USER="*username*";
# The password for that user
$PASS="*password*";
# The name of the database in which Typo3 is stored
$DB="typo3";
# The extension key for which to retrieve the changelog
# Should the whole configuration be printed?
$DUMP="false";
# The name of the variable for which the value should be retrieved.
$GET_VARIABLE="";
# The name of the variable that should be set to a given value.
$SET_VARIABLE="";
# The value for the variable that should be set.
$VARIABLE_VALUE="";
# Merge both configuration files.
$MERGE="false";
# Script Configuration end
function consoleWrite( $args ) {
global $QUIET;
if( "false" == "$QUIET" ) file_put_contents( "php://stderr", $args );
return 0;
}
function consoleWriteLine( $args ) {
global $QUIET;
if( "false" == "$QUIET" ) file_put_contents( "php://stderr", $args . "\n" );
return 0;
}
function consoleWriteVerbose( $args ) {
global $VERBOSE;
if( "true" == $VERBOSE ) consoleWrite( $args );
return 0;
}
function consoleWriteLineVerbose( $args ) {
global $VERBOSE;
if( "true" == $VERBOSE ) consoleWriteLine( $args );
return 0;
}
define( "REQUIRED_ARGUMENT_COUNT", 1 );
if( $argc <= REQUIRED_ARGUMENT_COUNT ) {
consoleWriteLine( "Insufficient command line arguments!" );
consoleWriteLine( "Use " . INVNAME . " --help to get additional information." );
exit( 1 );
}
// The base location from where to retrieve new versions of this script
$UPDATE_BASE = "https://raw.github.com/oliversalzburg/typo3scripts/master";
/**
* Update check
*/
function updateCheck() {
$_contentVersions = file_get_contents( $UPDATE_BASE . "/versions" );
$_contentSelf = split( "\n", file_get_contents( INVNAME ), 2 );
$_sumSelf = md5( $_contentSelf[ 1 ] );
consoleWriteLineVerbose( "Remote hash source: '" . $UPDATE_BASE . "/versions'" );
consoleWriteLineVerbose( "Own hash: '" . $SUM_SELF . "' Remote hash: '" . $SUM_LATEST . "'" );
$_isListed = preg_match( "/^" . SELF . " (?P<sum>[0-9a-zA-Z]{32})/ms", $_contentVersions, $_sumLatest );
if( !$_isListed ) {
consoleWriteLine( "No update information is available for '" . SELF . "'." );
consoleWriteLine( "Please check the project home page https://github.com/oliversalzburg/typo3scripts." );
return 2;
} else if( $_sumSelf != $_sumLatest[ 1 ] ) {
consoleWriteLine( "NOTE: New version available!" );
return 1;
}
return 0;
}
/**
* Self-update
*/
function runSelfUpdate() {
echo "Performing self-update...\n";
$_tempFileName = INVNAME . ".tmp";
// Download new version
echo "Downloading latest version...";
global $UPDATE_BASE;
$_fileContents = @file_get_contents( $UPDATE_BASE . "/" . SELF );
if( strlen( $_fileContents ) <= 0 ) {
echo "Failed: Error while trying to download new version!\n";
echo "File requested: " . $UPDATE_BASE . "/" . SELF . "\n";
exit( 1 );
}
$_payload = split( "\n", $_fileContents, 2 );
echo "Done.\n";
// Restore shebang
$_selfContent = split( "\n", file_get_contents( INVNAME ), 2 );
$_interpreter = $_selfContent[ 0 ];
file_put_contents( $_tempFileName, $_interpreter . "\n" );
file_put_contents( $_tempFileName, $_payload[ 1 ], FILE_APPEND );
// Copy over modes from old version
$_octalMode = fileperms( INVNAME );
if( FALSE == chmod( $_tempFileName, $_octalMode ) ) {
echo "Failed: Error while trying to set mode on $_tempFileName.\n";
exit( 1 );
}
// Spawn update script
$_name = INVNAME;
$_updateScript = <<<EOS
#!/bin/bash
# Overwrite old file with new
if mv "$_tempFileName" "$_name"; then
echo "Done."
echo "Update complete."
rm -- $0
else
echo "Failed!"
fi
EOS;
file_put_contents( "updateScript.sh", $_updateScript );
echo "Inserting update process...";
file_put_contents( "updateScript.sh", $_updateScript );
chmod( "updateScript.sh", 0700 );
if( function_exists( "pcntl_exec" ) ) {
pcntl_exec( "/bin/bash", array( "./updateScript.sh" ) );
} else if( function_exists( "passthru" ) ) {
die( passthru( "./updateScript.sh" ) );
} else {
die( "Please execute ./updateScript.sh now." );
}
}
# Make a quick run through the command line arguments to see if the user wants
# to print the help. This saves us a lot of headache with respecting the order
# in which configuration parameters have to be overwritten.
foreach( $argv as $_option ) {
if( 0 === strpos( $_option, "--help" ) || 0 === strpos( $_option, "-h" ) ) {
showHelp( $argv[ 0 ] );
exit( 0 );
}
}
// Read external configuration - Stage 1 - typo3scripts.conf (overwrites default, hard-coded configuration)
$BASE_CONFIG_FILENAME = "typo3scripts.conf";
if( file_exists( $BASE_CONFIG_FILENAME ) ) {
if( !is_readable( $BASE_CONFIG_FILENAME ) ) {
consoleWriteLine( "Unable to read '" . $BASE_CONFIG_FILENAME . "'. Check permissions." );
exit( 1 );
}
consoleWriteVerbose( "Sourcing script configuration from " . $BASE_CONFIG_FILENAME . "..." );
$_baseConfig = file_get_contents( $BASE_CONFIG_FILENAME );
$_baseConfigFixed = preg_replace( "/^(?!\s*$)(?P<name>[^#][^=]+)\s*=\s*(?P<value>[^$]*?)$/ms", "$\\1=\"\\2\";", $_baseConfig );
eval( $_baseConfigFixed );
consoleWriteLineVerbose( "Done." );
}
// Read external configuration - Stage 2 - script-specific (overwrites default, hard-coded configuration)
$CONFIG_FILENAME = substr( SELF, 0, -4 ) . ".conf";
if( file_exists( $CONFIG_FILENAME ) ) {
if( is_readable( $CONFIG_FILENAME ) ) {
consoleWriteLine( "Unable to read '" . $CONFIG_FILENAME . "'. Check permissions." );
exit( 1 );
}
consoleWriteVerbose( "Sourcing script configuration from " . $CONFIG_FILENAME . "..." );
$_config = file_get_contents( $CONFIG_FILENAME );
$_configFixed = preg_replace( "/^(?!\s*$)(?P<name>[^#][^=]+)\s*=\s*(?P<value>[^$]*?)$/ms", "$\\1=\"\\2\";", $_config );
eval( $_configFixed );
consoleWriteLineVerbose( "Done." );
}
foreach( $argv as $_option ) {
if( $_option === $argv[ 0 ] ) continue;
if( $_option == "--verbose" ) {
$VERBOSE = "true";
} else if( $_option == "--quiet" ) {
$QUIET = "true";
} else if( $_option == "--force" ) {
$FORCE = "true";
} else if( $_option == "--update" ) {
runSelfUpdate();
} else if( $_option == "--update-check" ) {
$returnValue = updateCheck();
exit( $returnValue );
} else if( 0 === strpos( $_option, "--base=" ) ) {
$BASE = substr( $_option, strpos( $_option, "=" ) + 1 );
} else if( $_option == "--export-config" ) {
exportConfig();
exit( 0 );
} else if( $_option == "--extract-config" ) {
extractConfig();
exit( 0 );
} else if( $_option == "--dump" ) {
$DUMP = "true";
} else if( 0 === strpos( $_option, "--get=" ) ) {
$GET_VARIABLE = substr( $_option, strpos( $_option, "=" ) + 1 );
} else if( 0 === strpos( $_option, "--set=" ) ) {
$SET_VARIABLE = substr( $_option, strpos( $_option, "=" ) + 1 );
} else if( 0 === strpos( $_option, "--value=" ) ) {
$VARIABLE_VALUE = substr( $_option, strpos( $_option, "=" ) + 1 );
} else if( $_option == "--merge" ) {
$MERGE = "true";
} else {
$GET_VARIABLE = $_option;
}
}
// Begin main operation
if( "" != $GET_VARIABLE && "false" != $DUMP ) {
consoleWriteLine( "--get and --dump can't be used together." );
exit( 1 );
}
function readConfiguration( $loadAdditional = true ) {
// Grab main configuration
$GLOBALS[ "TYPO3_CONF_VARS" ] = require( "typo3/typo3conf/LocalConfiguration.php" );
if( $loadAdditional ) {
// Grab additional configuration
$_additionalConfiguration = "typo3/typo3conf/AdditionalConfiguration.php";
if( is_file( $_additionalConfiguration ) ) {
require $_additionalConfiguration;
}
}
}
// Should we dump?
if( "true" == $DUMP ) {
readConfiguration();
var_export( $GLOBALS[ "TYPO3_CONF_VARS" ] );
if( "true" == $MERGE ) {
consoleWriteLineVerbose( "Configuration files won't be merged when using --dump." );
}
exit( 0 );
} else if( "" != $GET_VARIABLE || "" != $SET_VARIABLE ) {
// Load the configuration.
// Only when setting a variable and not merging, ignore the additional configuration.
readConfiguration( ( "" == $SET_VARIABLE || "true" == $MERGE ) );
// Retrieve a reference to the variable in the settings array
$variableName = ( "" != $GET_VARIABLE ) ? $GET_VARIABLE : $SET_VARIABLE;
$variablePath = explode( ".", $variableName );
$reference = &$GLOBALS;
foreach( $variablePath as $pathElement ) {
if( isset( $reference[ $pathElement ] ) ) {
$reference = &$reference[ $pathElement ];
} else {
$reference[ $pathElement ] = "undefined";
$reference = &$reference[ $pathElement ];
}
}
//$accessor = implode( "' ][ '", $variablePath );
//$variable = "\$GLOBALS[ '". $accessor . "' ]";
//echo $variable . " = " . $reference . "\n";
if( "" != $GET_VARIABLE ) {
// Get a variable
echo $reference . "\n";
exit( 0 );
} else {
// Set a variable
if( "" == $VARIABLE_VALUE ) {
$reference = null;
} else {
$reference = $VARIABLE_VALUE;
}
}
} else {
readConfiguration();
}
// If needed, write the configuration back to disk
if( "" != $SET_VARIABLE || "true" == $MERGE ) {
echo "<?php\n";
echo "return ";
var_export( $GLOBALS[ "TYPO3_CONF_VARS" ] );
echo ";";
}
# vim:ts=2:sw=2:expandtab: