5

I am running a PHP site on Windows using Wampserver. All throughout the site there is a hardcoded line such as:

$settings = parse_ini_file("/usr/local/apache2/myconfigs/settings.ini", true);

I know this is bad practice to begin with but it is out of my control.

When the site runs is there any possible way I can trick the site to point to C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini whenever the code is looking for /usr/local/apache2/myconfigs/settings.ini in the parse_ini_file function?

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231

5 Answers5

3
$settings = parse_ini_file(APACHE_INI_PATH, true);
// $settings = parse_ini_file("/usr/local/apache2/myconfigs/settings.ini", true);
cddoma
  • 48
  • 5
  • 2
    the goal is to keep the /usr/local path in the code since it is at the top of 100's of pages. – Blake Rivell Mar 19 '18 at 13:59
  • You could find and replace `"/usr/local/apache2/myconfigs/settings.ini"` with a PHP constant. Then you just configure it once when the environment changes. – cddoma Mar 19 '18 at 14:17
  • I like the suggestion, this is the way the code should be, however it is an existing large project under source control. If I make this change it will need to be accepted company wide in over 100 files. – Blake Rivell Mar 19 '18 at 19:10
  • I also like this suggestion, but I would add you can check the server address and swap them automatically with `if($_SERVER['SERVER_ADDR'] == '::1' || $_SERVER['SERVER_ADDR'] == '127.0.0.1')` for localhost. Also you are already seeing the maintenance nightmare this creates, it's better to fix it permanently and be done with it. – ArtisticPhoenix Mar 24 '18 at 03:27
1

This is a bit hackish but I think it's what you are looking for, so the trick here is to redefine the parse_ini_file function and make it ignore the invalid passed path ("/usr/local/apache2/myconfigs/settings.ini") and use your correct file instead.

This seems straightforward but a bit tricky since your new function should also call the original parse_ini_file function somehow, that's why you need to rename it first then override it

You will need PHP runkit extension enabled for this, have look at runkit_function_redefine and runkit_function_rename for reference.

Untested but should work, the code should be something around these lines :

runkit_function_rename('parse_ini_file','original_parse_ini_file');
runkit_function_redefine('parse_ini_file', function() {
    original_parse_ini_file("C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini", true);
});

Make sure the code above is executed at the start of your application script and any parse_ini_file call should use your file instead of the hardcoded one.

If there is no single entry point to your application where you can put the code above, you can put it in a separate script and make PHP load before running any script via the auto_prepend_file setting in your settings.ini file, also make sure that runkit.internal_override is set to On since parse_ini_file is not a user defined function.

mrbm
  • 2,164
  • 12
  • 11
1

Hello please check this

runkit_function_rename('parse_ini_file','o_parse_ini_file');
runkit_function_redefine('parse_ini_file', function($p1,$p2)  use($someCondition) {
    if($someCondition)
        o_parse_ini_file("C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini", true);
    else
        o_parse_ini_file($p1,$p2);

});

it can return

Call to undefined function runkit_function_rename()

to fix this error please read here

or here

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Armen Grigoryan
  • 521
  • 2
  • 9
0

If you don't want to do a find and replace as suggested by @cddoma, I propose that you create the directory /usr/local/apache2/myconfigs/ in your windows machine, and copy the file settings.ini from C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini to that directory.

Open your windows command line and enter the following

mkdir C:\usr\local\apache2\myconfigs\

copy C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini C:\usr\local\apache2\myconfigs\

Solomon A.
  • 491
  • 4
  • 7
  • this will not work. C:\usr\local\apache2\myconfigs\ will not be the same as /usr/local/apache2/myconfigs/. The path is absolute, not relative so it will not know to go to the C drive. – Blake Rivell Mar 22 '18 at 00:50
  • Wamp server defaults to the installation drive. Which is C:\ in your case. I have tried it and it worked in my test case. – Solomon A. Mar 23 '18 at 12:06
0

you may be able to do this with a Symbolic link on Windows

zakaria35
  • 857
  • 1
  • 7
  • 12