[SOLVED][BUG] Service.php match Config File Parameter

Max2Play Home 2016 (en) Forums Max2Play Development (Ideas, Wishes) [SOLVED][BUG] Service.php match Config File Parameter

This topic contains 1 reply, has 2 voices, and was last updated by  flysurfer Moderator 9 months, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • 21. December 2016 at 11:55 #25103

    Hello!
    I’ve found a bug in the function getConfigFileParameter and saveConfigFileParameter in PHP File Service.php

    If you have two or more parameters in the configfile which are named very similar,
    (for example: ABCDEF1 and ABCDEF10)
    both of them where found in the function getConfigFileParameter.
    If you save the Parameter ABCDEF1 the parameter ABCDEF10 will be overwritten with the parameter name ABCDEF1 and the given value for this parameter.
    Afterwards you will find the the parameter ABCDEF1 twice in the config file.

    The function deleteConfigFileParameter will delete ABCDEF10 too!!!

    Maybe you can add the spearator in the matching pattern …
    for example in function getConfigFileParameter:
    $output = trim(shell_exec(‘grep -aP “^[ \t]*’.$parameter.'[ \t]*’.$separator.'” ‘.$configfile.’ | sed -n -e “s/^[ \t]*[A-Za-z_0-9\.]*’.$separator.’//p”‘));

    Please make sure that this will be corrected in the next release!

    br

    Sample Code (this works for me):

    
    /**
    	 * Function to save specific Parameter to specified Configfile
    	 * @param string $configfile
    	 * @param string $parameter
    	 * @param string $value
    	 */
    	public function saveConfigFileParameter($configfile = '', $parameter = '', $value = '', $separator = '='){		
    		if(file_exists($configfile)){
    			$old_parameter = trim($this->getConfigFileParameter($configfile, $parameter));
    			
    			if($old_parameter == $value){
    				//No changes
    				return false;
    			}
    			//Check for empty entry
    			//$param_exists = shell_exec('grep -aP "^[ \t]*'.$parameter.'" '.$configfile.' | wc -l');
    			$param_exists = shell_exec('grep -aP "^[ \t]*'.$parameter.'[ \t]*'.$seperator.'" '.$configfile.' | wc -l');
    			if($old_parameter != '' || $param_exists > 0){
    				//$this->writeDynamicScript(array('sed -i "s/^[ \t]*'.$parameter.'.*$/'.$parameter.$separator.str_replace(array('/'),array('\/'),$value).'/g" '.$configfile));
    				$this->writeDynamicScript(array('sed -i "s/^[ \t]*'.$parameter.'[ \t]*'.$separator.'.*$/'.$parameter.$separator.str_replace(array('/'),array('\/'),$value).'/g" '.$configfile));
    				$this->view->message[] = _("Update Configfile - existing Entry changed");
    			}else{
    				//check for Newline in Last Line in config file
    				if(strpos(shell_exec('xxd -p '.$configfile.' | tail -c 3'), '0a') === FALSE){
    					//Newline missing -> add one
    					$parameter = "\n".$parameter;
    				}
    				$this->writeDynamicScript(array('echo "'.$parameter.$separator.$value.'" >> '.$configfile));
    				$this->view->message[] = _("Update Configfile - new Entry created");
    			}
    		}
    		else{
    			$this->writeDynamicScript(array('echo "'.$parameter.$separator.$value.'" > '.$configfile));
    			$this->view->message[] = _("Update Configfile - new Configfile created");
    		}
    		return true;
    	}
    	
    	/**
    	 * Function to delete specific Parameter from specified Configfile
    	 * @param string $configfile
    	 * @param string $parameter
    	 */
    	public function deleteConfigFileParameter($configfile = '', $parameter = '', $separator = '\='){
    		if(!file_exists($configfile))
    			return false;
    		//$param_exists = shell_exec('grep -aP "^[ \t]*'.$parameter.'" '.$configfile.' | wc -l');
    		$param_exists = shell_exec('grep -aP "^[ \t]*'.$parameter.'[ \t]*'.$seperator.'" '.$configfile.' | wc -l');
    		if($param_exists > 0){
    			//$this->writeDynamicScript(array('sed -i "s/^[ \t]*'.$parameter.'.*$//g" '.$configfile));
    			$this->writeDynamicScript(array('sed -i "s/^[ \t]*'.$parameter.'[ \t]*'.$separator.'.*$//g" '.$configfile));
    		}
    		return true;
    	}
    	
    	/**
    	 * Function to get specific Parameter from specified Configfile
    	 * @return boolean
    	 */
    	public function getConfigFileParameter($configfile = '', $parameter = '', $separator = '\='){
    		if(!file_exists($configfile))
    			return false;
    		//$output = trim(shell_exec('grep -aP "^[ \t]*'.$parameter.'" '.$configfile.' | sed -n -e "s/^[ \t]*[A-Za-z_0-9\.]*'.$separator.'//p"'));
    		$output = trim(shell_exec('grep -aP "^[ \t]*'.$parameter.'[ \t]*'.$separator.'" '.$configfile.' | sed -n -e "s/^[ \t]*[A-Za-z_0-9\.]*'.$separator.'//p"'));
    		return $output;
    	}
    
    • This topic was modified 9 months, 4 weeks ago by  preslmayer.
    • This topic was modified 9 months ago by  Christoph.
    3. January 2017 at 15:59 #25407

    Thanks a lot for posting this info! I will change the code to fix it!

    Already added this to the current Beta version of Max2Play.

    All the Best,
    Stefan

    • This reply was modified 9 months, 2 weeks ago by  flysurfer.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

Register here