I’m trying to finish up a PHP routine which allows for the restoration of files which have been tinkered with (see the thread on "self-healing index.php").
I’ve got it working, but would like to know if there is a fairly straightforward way of determining the path to the point just above a website’s root? This is where I’d like to place the file backups and in testing on XAMPP and Linux I can’t help but to think I’ve got something too complicated :
<?php //this for highlighting
//Let's try to figure out where we are and where we want things to go
define('BASE_PATH',realpath('.').'/'); //figure out where we are
echo 'Base path : '.BASE_PATH.'<br/>'; //same as MODX_BASE_PATH
$temproot = dirname($_SERVER["DOCUMENT_ROOT"]).'/'; //figure out document root path
echo 'Temp root : '.$temproot.'<br/>';
if (strpos(BASE_PATH,'public_html/')>0)
{
$prefix = 'public_html/';
$numchars = strpos(BASE_PATH,$prefix);
$newroot = substr(BASE_PATH, 0, $numchars); //trim that off
$tempwhereweare = substr(BASE_PATH, $numchars+strlen($prefix), strlen(BASE_PATH));
}
else
{
$newroot = $temproot; //assume we are on Windows/XAMPP
$tempwhereweare = substr(BASE_PATH, strlen($temproot));
}
define('BASE_ROOT', $newroot);
echo 'BASE_ROOT : '.BASE_ROOT.'<br/>';
echo 'Temp where we are : '.$tempwhereweare.'<br/>';
if (strpos($tempwhereweare,'htdocs\\') !== FALSE)
{
$prefix = 'htdocs/';
$newwhereweare = str_replace($prefix, '', $tempwhereweare);
}
else
$newwhereweare = $tempwhereweare;
define('WHEREWEARE', $newwhereweare);
echo 'Base root : '.BASE_ROOT.'<br/>';
echo 'Where we are : '.WHEREWEARE.'<br/>';
//define file structures for backup location
$structure1 = BASE_ROOT.$backupfolder.WHEREWEARE.'manager/includes/';
$structure2 = BASE_ROOT.$backupfolder.WHEREWEARE.'assets/';
echo 'Structure 1 : '.$structure1.'<br/>';
echo 'Structure 2 : '.$structure2.'<br/>';
//At this point we should have enough info to create backup directories
// above the root and we should know where we are putting files
//try creating file structures for backup location
if (!mkdir($structure1, 0755, true)) {
echo 'Failed to create folders in first list. They may already exist?<br/>';
}
if (!mkdir($structure2, 0755, true)) {
echo 'Failed to create folders in second list. They may already exist?<br/><br/>';
}
//make sure file structures exist
if (file_exists($structure1) && file_exists($structure2))
{
//where files are going to be coming from
$backup_locn = BASE_ROOT.$backupfolder.WHEREWEARE;
//where files are going to be going
$target_locn = BASE_ROOT.$prefix.WHEREWEARE;
The complexity has arisen from trying to get something that works on both systems, even if the installation is buried down a level or more from the public folder (ie. either public_html or htdocs).
Another problem seems to be getting PHP to do chmod on a Windows Vista + XAMPP install. Any tips there would be appreciated as well.
TIA,
Matt