
<?php

function replace_string_in_file($f_src, $f_dst, $string_to_replace, $replace_with) {
	
	$temp_file  = "/var/www/html/v2/tmp_files/" . "koko_" . date("y_m_d_H_i_s") . "_A.txt" ;
	
	$content=file_get_contents($f_src);
	$content_chunks=explode($string_to_replace, $content);
	$content=implode($replace_with, $content_chunks);
	file_put_contents($temp_file, $content);

	if (file_exists($f_dst)) {
		$cmd_1 = "sudo chmod 666 " . $f_dst ;
		$result_1 = exec($cmd_1, $exec_output_1, $exec_return_1);
	}

	$cmd_2 = "sudo cp " . $temp_file . " " . $f_dst ;
	$result_2 = exec($cmd_2, $exec_output_2, $exec_return_2);					
}

function replace_5_string_in_file($f_src, $f_dst, $string_to_replace, $replace_with) {
	
	$temp_file  = "/var/www/html/v2/tmp_files/" . "koko_" . date("y_m_d_H_i_s") . "_B.txt" ;
	
    $content=file_get_contents($f_src);
	
	for ($i=0;$i<5;$i++) {
		$content_chunks=explode($string_to_replace[$i], $content);
		$content=implode($replace_with[$i], $content_chunks);
	}

	file_put_contents($temp_file, $content);

	if (file_exists($f_dst)) {
		$cmd_1 = "sudo chmod 666 " . $f_dst ;
		$result_1 = exec($cmd_1, $exec_output_1, $exec_return_1);
	}

	$cmd_2 = "sudo cp " . $temp_file . " " . $f_dst ;
	$result_2 = exec($cmd_2, $exec_output_2, $exec_return_2);					

}

function copy_file($f_src, $f_dst) {

	if (file_exists($f_dst)) {
		$cmd_1 = "sudo chmod 666 " . $f_dst ;
		$result_1 = exec($cmd_1, $exec_output_1, $exec_return_1);
	}

	$cmd_2 = "sudo cp " . $f_src . " " . $f_dst ;
	$result_2 = exec($cmd_2, $exec_output_2, $exec_return_2);					

}

function replace_N_strings_in_file($f_src, $f_dst, $string_to_replace, $replace_with) {
	
	$temp_file  = "/var/www/html/v2/tmp_files/" . "koko_" . date("y_m_d_H_i_s") . "_C.txt" ;

	$nb_strings = sizeof($string_to_replace) ;
	
    $content=file_get_contents($f_src);
	
	for ($i=0; $i < $nb_strings; $i++) {
		$content_chunks=explode($string_to_replace[$i], $content);
		$content=implode($replace_with[$i], $content_chunks);
	}

	file_put_contents($temp_file, $content);

	if (file_exists($f_dst)) {
		$cmd_1 = "sudo chmod 666 " . $f_dst ;
		$result_1 = exec($cmd_1, $exec_output_1, $exec_return_1);
	}

	$cmd_2 = "sudo cp " . $temp_file . " " . $f_dst ;
	$result_2 = exec($cmd_2, $exec_output_2, $exec_return_2);					

}

function CarAleatoire_X($taille) {
	  // Liste des caractères possibles
	  $cars="abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
	  $mdp='';
	  $long=strlen($cars);

	  srand((double)microtime()*1000000); //Initialise le générateur de nombres aléatoires

	  for($i=0;$i<$taille;$i++) $mdp = $mdp.substr($cars,rand(0,$long-1),1);

	  return $mdp;
}


?>