We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18913
    • 654 Posts
    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
      • 3749
      • 24,544 Posts
      How about just making the full path to the safe version a system setting. That way users can specify both the name and the location and you can get it with (assuming it’s for Revolution):

      $backupIndex = $modx->getOption('backup_index_path')
      ;

      The target index path will always be available with:

      $targetIndex = MODX_BASE_PATH . ’index.php’;

      unless the user has renamed index.php.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 18913
        • 654 Posts
        I think I can probably do something like this :
        $backupfolder = ".blah/";
        $whereweare = dirname(__FILE__);
        
        echo $whereweare."<br/>";
        
        if (strpos($whereweare, 'public_html/') !== FALSE)
        {
        	$prefix = 'public_html/';
        
        }
        elseif (strpos($whereweare, 'htdocs\\') !== FALSE)
        {
        	$prefix = 'htdocs\\';
        }
        else
        {
        	echo "Unknown<br/>";
        }
        
        $pos = strpos($whereweare, $prefix);
        $upper_locn = substr_replace($whereweare, $backupfolder, $pos, strlen($whereweare));
        echo $upper_locn;
        


        Thanks for the help!
        Matt
          • 3749
          • 24,544 Posts
          Lots of people have MODx in a subdirectory and the root directory isn’t guaranteed to be htdocs or public_html, although those are common.

          The index.php file should always be at MODX_BASE_PATH . ’index.php’ and I don’t think you need to care about the actual directory name.



            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting