Skip to content

Instantly share code, notes, and snippets.

@pepebe
Last active April 23, 2022 14:54
Show Gist options
  • Save pepebe/e5294a4f09cc7f902fdd93283fb74c1e to your computer and use it in GitHub Desktop.
Save pepebe/e5294a4f09cc7f902fdd93283fb74c1e to your computer and use it in GitHub Desktop.
ConfigurePHP recursively searches all directories below its own position for php files. If a directory with at least one php file is found, a symlink to ./php.ini will be created.

Found this script years ago on the 1und1 forum. It was a nice workaround to make sure that the php.ini file would work inside root and all directories below it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
/*
Name: ConfigurePHP
Version: 1.0
Copyright (c) 2006 Tingletangle-Bob (tingletangle-bob(at)gmx.de)
Description
ConfigurePHP recursively searches all directories below its own position for php files.
If a directory with at least one php file is found, a symlink to ./php.ini will be created.
License
This script is freeware. You can do what you like, as long as you keep the original credits in.
Please use the history section below to track your changes. Please send me your updated version
(see e-mail address above)!
IMPORTANT:
YOU ACKNOWLEDGE THAT THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, AND TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW.
NO ONE MAKES ANY REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR
THAT THE SOFTWARE WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS
OR OTHER RIGHTS. THERE IS NO THAT THE FUNCTIONS CONTAINED IN THE SOFTWARE WILL MEET
YOUR REQUIREMENTS OR THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE.
YOU ASSUME ALL RESPONSIBILITY AND RISK FOR THE SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
INTENDED RESULTS AND FOR THE INSTALLATION, USE AND RESULTS OBTAINED FROM IT.
--- REMOVE THE SOFTWARE AFTER USE! IT MAY BE A POTENTIAL SECURITY RISK! ---
History:
1.0 Initial release.
*/
$my_Version = "1.0";
$my_ProductName = "PHP configuration file replicator";
function GetDirectories($dir, $mask)
{
static $i = 0;
$directories = Array();
$d = opendir($dir);
$FoundPHPFile = FALSE; // a .php file was found in this directory
$AlreadyAdded = FALSE; // the directory has already been added to the list
while ($file = readdir($d))
{
if ( $file == '.' || $file == '..' ) continue;
if ( eregi($mask, $file) !== FALSE )
{
$FoundPHPFile = TRUE;
}
if (is_dir($dir.'/'.$file))
{
// start recursively
$directories += GetDirectories($dir.'/'.$file, $mask);
}
if ( ($FoundPHPFile == TRUE) && ($AlreadyAdded == FALSE) )
{
$directories[$i++] = $dir;
$AlreadyAdded = TRUE;
}
}
return $directories;
}
// Init global stuff
$request_var_arr = array('command');
foreach ($request_var_arr as $val)
{
if(isset($_POST[$val]))
{
$$val = $_POST[$val];
} elseif(isset($_GET[$val]))
{
$$val = $_GET[$val];
} elseif(!isset($$val))
{
$$val = '';
}
}
$my_name = basename(__FILE__);
$my_path = realpath("./");
//clearstatcache();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $my_ProductName; ?></title>
<style type="text/css">
<!--
body
{
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
-->
</style>
</head>
<body>
<h2 style="text-align: center;"><?php echo $my_ProductName; ?></h2>
<h3 style="text-align: center;">Version <?php echo $my_Version; ?></h3>
<p>&nbsp;</p>
<form action="<?php echo $my_name; ?>" method="post">
<?php
switch ($command)
{
/* ----- Scripts starts for the first time ----- */
default:
?>
<p>
This script will create symlinks to a centralized <b>php.ini</b> file (the "master" file) in all directories that contain .php scripts.
</p>
<b>Master php.ini file information:</b>
<table>
<tr>
<td>
Path:
</td>
<td>
<?php echo $my_path."/php.ini"; ?>
</td>
</tr>
<tr>
<td>
Status:
</td>
<td>
<b>
<?php
if ( file_exists($my_path."/php.ini") )
{
echo "<font color=\"#00CC00\">Found";
echo " (".filetype($my_path."/php.ini").")</font>";
$CreateLinksOK = TRUE;
} else
{
echo "<font color=\"#CC0000\">Missing</font>";
$CreateLinksOK = FALSE;
}
?>
</b>
</td>
</tr>
</table>
<?php if ($_SERVER["WINDIR"]) echo "<p style=\"color: red;\"><b>Your web server runs on Windows. You will not be able to create symlinks!</b></p>"; ?>
<p><b>Please choose a command:</b></p>
<table>
<tr>
<td>
<input type="submit" name="command" value="Scan" />
</td>
<td>
Scans the status of all folders containing .php files.
</td>
</tr>
<?php
if ($CreateLinksOK)
{
?>
<tr>
<td>
<input type="submit" name="command" value="Create links" />
</td>
<td>
Creates symlinks to the main configuration file in all folders that contain .php files.
</td>
</tr>
<?php
}
?>
<tr>
<td>
<input type="submit" name="command" value="Remove links" />
</td>
<td>
Removes all links to the master PHP configuration file.
</td>
</tr>
</table>
<p>&nbsp;</p>
<table>
<tr>
<td style="vertical-align: top;">
<b>Security notice:</b>
</td>
<td>
Please delete this script from your server after use.<br/>
The script is a potential security risk!<br/>
You can also click this button:<br/>
<input type="submit" name="command" value="Delete script" />
</td>
</tr>
</table>
<?php
break;
/* ----- User selected the scan command -----*/
case "Scan":
?>
<p>Scan complete. The directories listed below contain .php files.</p>
<table>
<tr>
<th>Directory</th>
<th>Status</th>
</tr>
<?php
$f = GetDirectories('.', ".php$");
for ($i=0;$i<sizeof($f);$i++)
{
?>
<tr>
<td>
<?php echo "$f[$i]"; ?>
</td>
<td>
<b>
<?php
if ( file_exists($f[$i].'/php.ini') )
{
echo "<font color=\"#00CC00\">Found";
echo " (".filetype($f[$i].'/php.ini').")</font>";
} else
echo "<font color=\"#CC0000\">Missing</font>";
?>
</b>
</td>
</tr>
<?php
} // end for
?>
</table>
<p>&nbsp;</p>
<input type="hidden" name="command" value="" />
<input type="submit" value="Back" />
<?php
break;
/* ----- User selected the create links command ----- */
case "Create links":
?>
<p>Symlinks have been created. Please verify the results in the list below.</p>
<table>
<tr>
<th>
Directory
</th>
<th>
Old status
</th>
<th>
New status
</th>
</tr>
<?php
$f = GetDirectories('.', ".php$");
for ($i=0;$i<sizeof($f);$i++)
{
?>
<tr>
<td>
<?php echo "$f[$i]"; ?>
</td>
<td>
<b>
<?php
if ( file_exists($f[$i].'/php.ini') )
{
echo "<font color=\"#00CC00\">Found";
echo " (".filetype($f[$i].'/php.ini').")</font>";
} else
echo "<font color=\"#CC0000\">Missing</font>";
?>
</b>
</td>
<td>
<b>
<?php
if ( file_exists($f[$i].'/php.ini') )
{
echo "<font color=\"#FFAA00\">Skipping</font>";
} else
{
if ( symlink($my_path.'/php.ini', $f[$i].'/php.ini') )
echo "<font color=\"#00CC00\">Link created</font>";
else
echo "<font color=\"#CC0000\">Error!</font>";
}
?>
</b>
</td>
</tr>
<?php
} // end for
?>
</table>
<p>&nbsp;</p>
<input type="hidden" name="command" value="" />
<input type="submit" value="Back" />
<?php
break;
/* ----- User selected the remove links command ----- */
case "Remove links":
?>
<p>Links removed. Please verify the results in the list below.</p>
<p>&nbsp;</p>
<table>
<tr>
<th>File</th>
<th>Config file status</th>
</tr>
<?php
$f = GetDirectories('.', "php.ini$");
for ($i=0;$i<sizeof($f);$i++)
{
?>
<tr>
<td>
<?php echo $f[$i].'/php.ini'; ?>
</td>
<td>
<b>
<?php
if ( file_exists($f[$i].'/php.ini') )
{
if ( filetype($f[$i].'/php.ini') == "link" )
{
if ( unlink($f[$i].'/php.ini') )
echo "<font color=\"#00CC00\">Link removed</font>";
else
echo "<font color=\"#CC0000\">Error!</font>";
} else
echo "<font color=\"#FFAA00\">Skipping</font>";
} else
echo "<font color=\"#CC0000\">Missing</font>";
?>
</b>
</td>
</tr>
<?php
} // end for
?>
</table>
<p>&nbsp;</p>
<input type="hidden" name="command" value="" />
<input type="submit" value="Back" />
<?php
break;
/* ----- User selected delete command ----- */
case "Delete script":
if ( unlink($my_name) )
echo "Thank you for using $my_ProductName.<br>This script hast just been deleted.";
else
echo "Could not delete script. Please remove the file manually.";
break;
} // switch
?>
</form>
<p>&nbsp;</p>
<hr/>
<p style="text-align: center;">Copyright &copy; 2006 Tingletangle-Bob</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment