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+ ?>
0 commit comments