Skip to content

Commit bf60e37

Browse files
committed
Expand PHP interception to Windows, moving shared logic into PHP
1 parent b7ee4c8 commit bf60e37

File tree

3 files changed

+86
-22
lines changed

3 files changed

+86
-22
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
When run by php, this file grabs the current php.ini config,
5+
writes a new temporary one to disk that trusts your local
6+
HTTP Toolkit CA certificate, and then logs the path of that.
7+
8+
If future PHP instances are started with PHPRC=[that path],
9+
then they'll start exactly the same way as normal, except
10+
they'll automatically trust the certificate.
11+
*/
12+
13+
// PHP has ini reading built in, but not writing.
14+
// From https://gist.github.com/edvardHua/a9830a68ae68f57cd892a8a2903a1fb4
15+
function write_ini_file($assoc_arr, $path) {
16+
$content = "";
17+
foreach ($assoc_arr as $key => $elem) {
18+
$content .= "[" . $key . "]\n";
19+
foreach ($elem as $key2 => $elem2) {
20+
if (is_array($elem2)) {
21+
for ($i = 0; $i < count($elem2); $i++) {
22+
$content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n";
23+
}
24+
} else if ($elem2 == "") {
25+
$content .= $key2 . " = \n";
26+
} else {
27+
$content .= $key2 . " = \"" . $elem2 . "\"\n";
28+
}
29+
}
30+
}
31+
if (!$handle = fopen($path, 'w')) {
32+
return false;
33+
}
34+
if (!fwrite($handle, $content)) {
35+
return false;
36+
}
37+
fclose($handle);
38+
return true;
39+
}
40+
41+
// Get the contents of the current config file
42+
$phpIniLocation = php_ini_loaded_file();
43+
if ($phpIniLocation) {
44+
$phpIniContents = parse_ini_file($phpIniLocation, true);
45+
} else {
46+
$phpIniContents = array();
47+
}
48+
49+
// Edit the config to trust our HTTPS certificate
50+
if ($phpIniContents['openssl']) {
51+
$phpIniContents['openssl']['openssl.cafile'] = getenv('SSL_CERT_FILE');
52+
} else {
53+
$phpIniContents['openssl'] = array('openssl.cafile' => getenv('SSL_CERT_FILE'));
54+
}
55+
56+
// Create a new config file that the real PHP instance will use
57+
$newPhpIni = tempnam(sys_get_temp_dir(), 'httptoolkit-php.ini');
58+
write_ini_file($phpIniContents, $newPhpIni);
59+
60+
// Log the new config path to use
61+
echo $newPhpIni;
62+
?>
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
#!/usr/bin/env bash
2+
set -e
23

34
# Exclude ourselves from PATH within this script, to avoid recursing
45
PATH="${PATH/`dirname $0`:/}"
56

6-
# Get the php.ini that would be used
7-
PHP_INI_LOCATION=`php -r 'echo php_ini_loaded_file();'`
8-
9-
# Create a temp directory, and ensure it's cleaned up afterwards
10-
PHP_INI_OVERRIDE_DIR=$(mktemp -d)
11-
trap "rm -rf $PHP_INI_OVERRIDE_DIR" EXIT
12-
13-
# Copy the existing php.ini, and set PHPRC so our new copy is used
14-
export PHPRC=$PHP_INI_OVERRIDE_DIR/php.ini
15-
cp $PHP_INI_LOCATION $PHPRC
16-
17-
# Ensure the temporary php.ini trusts the HTTP Toolkit certificate
18-
if grep -q 'openssl.cafile=' $PHPRC; then
19-
# If there's an openssl.cafile line (commented or not), enable & set it
20-
sed -i -e "s/;\?openssl.cafile=.*/openssl.cafile=${SSL_CERT_FILE//\//\\/}/" $PHPRC
21-
elif grep -q '\[openssl\]' $PHPRC; then
22-
# If not, but there's an [openssl] section, add the setting there
23-
sed -i -e "s/\[openssl\]/[openssl]\nopenssl.cafile=${SSL_CERT_FILE//\//\\/}/" $PHPRC
24-
else
25-
# If that's missing too, add it ourselves
26-
printf "\n[openssl]\nopenssl.cafile=$SSL_CERT_FILE" >> $PHPRC
27-
fi
7+
# Build a new PHP config that trusts our certificate, and use PHPRC
8+
# to ensure that the next php command uses it.
9+
export PHPRC=`php $(dirname $0)/build-php-config.php`
2810

11+
# Pass everything through to PHP (but now using our tweaked config)
2912
php "$@"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@echo off
2+
SETLOCAL
3+
4+
REM Exclude ourselves from PATH within this script, to avoid recursing
5+
set ORIGINALPATH=%PATH%
6+
REM Get the current file's folder
7+
set THIS_PATH=%~dp0
8+
REM Strip the trailing slash from the folder
9+
set WRAPPER_FOLDER=%THIS_PATH:~0,-1%
10+
REM Remove that folder from PATH
11+
call set PATH=%%PATH:%WRAPPER_FOLDER%;=%%
12+
13+
REM Create a php.ini that trusts our cert, and set the path in PHPRC so PHP uses it
14+
FOR /F "tokens=* USEBACKQ" %%F IN (`php %WRAPPER_FOLDER%\build-php-config.php`) DO (
15+
SET PHPRC=%%F
16+
)
17+
18+
REM Start PHP for real (but now using our tweaked config)
19+
php %*

0 commit comments

Comments
 (0)