<?php

/**
* @version $Id: admin.protconf.html.php 2007-01-11 18:42:50EET emav $
* @copyright Copyright (C) 2007 Efthimios Mavrogeorgiadis. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* This component is free software and parts of it may contain or be derived from works
* licensed under the GNU General Public License or other free or open source
* software licenses.
*/

defined('_JEXEC') or die('Direct Access to this location is not allowed.'); 

jimport('joomla.application.component.controller');

class protectConfigureNew {

// Define variables for use with both current and proposed configuration files
/*No search here!*/	var $file           = ''; // name only, e.g. configuration.php
	var $path           = ''; // path only, e.g. /path/to/configuration/file/
	var $pre_file       = ''; // prefix needed if configuration is to be found in a directory other than $mosConfig_absolute_path, e.g. ../../../other/directory/
	var $user_input     = ''; // the user's input, whatever it may be
/*No search here!*/	var $full_file_pn   = ''; // path+name, e.g. /path/to/configuration/file/configuration.php
/*No search here!*/	var $full_file_name = ''; // prefix+name, e.g. ../../../other/directory/configuration.php
    var $option         = ''; // Joomla's URL variable: always com_protconf
    var $act            = ''; // Joomla's URL variable: permit, renamed, protect, restore
    var $task           = ''; // Joomla's URL variable: apply, cancel
    var $new_dir        = ''; // Variable holds the first new folder created, if need be
	
// Define configuration file's name
	function set_file($file) {
		$this->file = $file;
	}

// Define user's input
	function set_user_input($input) {
		$this->user_input = $input;
	}

// Define path to configuration file
	function set_path($path) {
		$this->path = $this->get_path( $path );
	}

// Show messages to user
	function set_alarm($alarm) {
		global $mainframe; 
		$url = "index2.php?option=" . $this->option . "&act=" . $this->act;
		return $mainframe->redirect($url,$alarm);
		
	}

// Drop any ../../../ and get the real path to the configuration file
	function get_path($path) {
		return $this->correct_slash( realpath( $path ) );
	}

// Change backslashes into slashes and add a final slash to the path variable if missing
	function correct_slash($path) {
		if ( preg_match('/^\w:\\\/', $path) ) {
	        $path = str_replace("\\", "/", $path);
    	}

    	if ( strrpos($path, '/') + 1 < strlen($path) ) {
			$addslash = '/';
		}
		else {
			$addslash = '';
		}
		
		return $path . $addslash;
	}
    
// Check if user's input is fine
    function check_input($main_path) {

//Input is void
        if ( !$this->user_input ) {
            if ( $this->act == 'renamed' ) {
                $noun = "name";
            }
            else {
                $noun = "path";
            }
            $this->set_alarm("You failed to provide a new $noun for the configuration file. Please, try again!");
        }

// Input contains dots
        if ( $this->act == 'renamed' ) {
            if ( preg_match('/\./', $this->user_input) ) {
                $this->set_alarm("Please, don't append an extension. It will be added automatically.");
            }

// Input contains non-alphanumeric characters
            if ( preg_match('/\W/', $this->user_input) ) {
                $this->set_alarm("Please, use only alphanumeric characters and the underscore (_) in the new name for the configuration file.");
            }
        }
    }

// Check if proposed file already exists
    function check_existing() {
        if ( file_exists( $this->full_file_pn ) ) {
            $this->set_alarm("File " . $this->full_file_pn . " already exists. Operation failed!");
        }
    }

// Populate all the variables of the proposed new configuration file    
    function set_file_details($oldf) {
        $this->full_file_pn = $this->path . $this->file;

// Calculate how many dots and slashes should appear after $mosConfig_absolute_path and before the configuration file's name
        if ( $oldf->main_path != $this->path ) {
            $path_part_old = explode("/", $oldf->main_path);
            $path_part_new = explode("/", $this->path);
            $path_part_old_end = count($path_part_old) - 1;
            $path_part_new_end = count($path_part_new) - 1;

            $i = 0;
            if ( preg_match('/^\w:/', $oldf->main_path) ) {
                while ( strcasecmp($path_part_old[$i], $path_part_new[$i]) == 0 ) {
                    $i++;
                }
            }
            else {
                while ( $path_part_old[$i] == $path_part_new[$i] ) {
                    $i++;
                }
            }

            $dots = $path_part_old_end - $i;
   	        while ( $dots > 0 ) {
       	        $this->pre_file .= "../";
               	$dots--;
           	}

           	while ( $i < $path_part_new_end ) {
           		$this->pre_file .= $path_part_new[$i] . "/";
           		$i++;
           	}

            $this->full_file_name = $this->pre_file . $this->file;
        }
        else {
            $this->full_file_name = $this->file;
        }
    }
}

class protectConfigureOld extends protectConfigureNew {

// Define variables for use with the current configuration file
    var $main_path      = '';
	var $file_list      = array();
    var $last_error     = '';
    var $errors         = 0 ;

// $this->main_path = $mosConfig_absolute_path
	function set_main_path($path) {
		$this->main_path = $this->get_path( $path );
	}
	
// mon change the functions
	function set_main_file($file)	{
		$this->full_file_name = $this->main_path.$file;
	}

// Populate all the variables of the current configuration file    
	function set_old_file_details() {
    
// Find the location of the current configuration file
	 //mon edit
		$file_index_admin = $this->main_path . '/includes/framework.php';
		
		$file_index_admin_lines = fread($fp = fopen($file_index_admin, "r"), filesize($file_index_admin));
	    fclose($fp);
	    
    	preg_match("/\(\s*\JPATH_CONFIGURATION\s*\.\s*\DS\s*\.\s*\'\s*[\w\/\.]+\s*\'\s*\);/", $file_index_admin_lines, $match);

    	if ( !$match[0] ) {
            $this->set_alarm("Could not find configuration file. Operation failed! $file_index_admin");
    	}
	
	
// Find the prefix+name of the current configuration file

	//mon edit
	    $find_conf = explode("'", $match[0]);
    	if ( substr($find_conf[1], 0, 1) == '/' ) {
    		$this->full_file_name = substr($find_conf[1], 1, strlen($find_conf[1]) - 1);
	    }
    	else {
    		$this->full_file_name = $find_conf[1];
	    }

// Find the prefix of the current configuration file
	    if ( preg_match('/\//', $this->full_file_name) ) {
    	    $this->file = substr(strrchr($this->full_file_name, "/"), 1);
    	    $this->pre_file = substr($this->full_file_name, 0, strrpos($this->full_file_name, "/")) . "/";
	    }
    	else {
        	$this->file = $this->full_file_name;
        	$this->pre_file = '';
	    }
	
		//$this->file= 'configuration.php';
// Find the real path to the current configuration file
	    $this->set_path($this->main_path . $this->pre_file);

// Check if the current configuration file actually exists
    	$this->full_file_pn = $this->path . $this->file;
	    if ( !is_file($this->full_file_pn) ) {
            $this->set_alarm("Could not find configuration file: $fulloldname. Operation failed!");
	    }
	}

// Get a list of *.php under $mosConfig_absolute_path
    function set_file_list($path) {
        $this->file_list = $this->get_file_list($path);
    }

// Make a list of *.php under $mosConfig_absolute_path
	function get_file_list($path) {
	    $file_list = array();
	    
    	$handle = opendir($path);
	    while ($file = readdir($handle)) {
	    
    	    if ( !($file === '.' or $file === '..') ) {
    	    
        	    $fullfile = $path . $file;
            	if ( is_dir($fullfile) ) {
                	$filesnew = array();
	                $dir = $fullfile . "/";
    	            $filesnew = $this->get_file_list($dir);
        	        foreach ($filesnew as $file) {
            	        $file_list[] = $file;
                	}
	            }
    	        elseif ( is_file($fullfile) & stristr($fullfile, '.php') == '.php' ) {
        	        $file_list[] = $fullfile;
            	}
	            else {
    	            continue;
        	    }
        	    
	        }
	        
    	}
        return $file_list;
	}

// Replace all instances of the old configuration file in Joomla's *.php files    
    function find_replace($newf) {
        //$this->set_file_list($this->main_path);
        //$exclude = array('/*No search here!*/');
		$file = substr(strrchr($this->full_file_pn,'/'),1);
		
		$file_conf = array (
							0 => $this->main_path.'/includes/framework.php',
							1 => $this->main_path.'/administrator/includes/framework.php',
							2 => $this->main_path.'/libraries/joomla/application/application.php'
							); 
					
		$this->replace($file_conf[0],$file,$newf->file);
		
		$this->replace($file_conf[1],$file,$newf->file);
		
		$this->replace($file_conf[2],$file,$newf->file);
		
		//rewrite the config file name
		/*
		$oldpath = $file;
		$newpath = $newf->file; //sfsdf.php
		rename($this->main_path.$oldpath,$this->main_path.$newpath);
		*/
		
		/*
        $sr = new search_replace($this->full_file_name, $newf->full_file_name, $this->file_list, '', 0, $exclude);
        $sr->set_search_function('normal');
        $sr->do_search();
        if ( $sr->get_last_error() ) {
            $this->errors++;
            $this->last_error = $sr->get_last_error();
        }
		*/
    }

// Undo the effect of the routine above in case of error
    function undo_find_replace($newf) {
		$file_conf = array (
							0 => $this->main_path.'/includes/framework.php',
							1 => $this->main_path.'/administrator/includes/framework.php',
							2 => $this->main_path.'/libraries/joomla/application/application.php'
							);
		
		$file = substr(strrchr($this->full_file_pn,'/'),1); 
					
		$this->replace($file_conf[0],$newf->file,$file);
		
		$this->replace($file_conf[1],$newf->file,$file);
		
		$this->replace($file_conf[2],$newf->file,$file);
	
	/*
        $sr = new search_replace($newf->full_file_name, $this->full_file_name, $this->file_list, '', 0, $exclude);
        $sr->set_search_function('normal');
        $sr->do_search();
	*/
    }
	
	function recover()	{
	
		$file_conf = array (
							0 => $this->main_path.'/includes/framework.php',
							1 => $this->main_path.'/administrator/includes/framework.php',
							2 => $this->main_path.'/libraries/joomla/application/application.php'
							);
							
		$file = substr(strrchr($this->full_file_pn,'/'),1);
		
		$this->replace($file_conf[0],$file,'configuration.php');
		
		$this->replace($file_conf[1],$file,'configuration.php');
		
		$this->replace($file_conf[2],$file,'configuration.php');
	}
	
	function replace($path,$oldfile,$newfile){
		
		$file_conf = $path;
							
		$file_conf_lines = fread($fp = fopen($file_conf,'r'), filesize($file_conf));
		fclose($fp);
		
		$file_conf_lines = str_replace($oldfile,$newfile,$file_conf_lines);
		
		fwrite($fp = fopen($file_conf,"w"),$file_conf_lines);
		fclose($fp);
	}
}
?>