Location: PHPKode > projects > Easy Hosting Control Panel for Ubuntu > ehcp/localutils.php
<?php

if(!function_exists("andle")){
function andle($s1,$s2) { //iki string'in andlenmi halini bulur. bir bosa "and" kullanlmaz. delphiden aldim..:)
  if($s1=='')$s1=$s2;
  elseif ($s2<>'')$s1=$s1.' and '.$s2;
  return $s1;
}
}

function to_array($ar){ # convert a variable to array if it is not already,
    if(is_array($ar)) return $ar;  # if array, dont do anything
    if(!$ar) return array(); # bos ise, bos array dondur.
    if(!is_array($ar)) return array($ar); # array olmayan bir degisken ise, arraya dondur ve return et.
    return "(arraya cevirme yapilamadi.)"; # hicbiri degilse hata var zaten.
}

function array_merge2($ar1,$ar2){
    return array_merge(to_array($ar1),to_array($ar2));
}

    
if(!function_exists("writeoutput")){
function writeoutput($file, $string, $mode="w",$log=true) {
	if (!($fp = fopen($file, $mode))) {
			echo "hata: dosya acilamadi: $file (writeoutput) !";
			return false;
	}
	if (!fputs($fp, $string . "\n")) {
			fclose($fp);
			echo "hata: dosyaya yazilamadi: $file (writeoutput) !";
			return false;
	}
	fclose($fp);
	if($log) echo "\n(".__FILE__.") file written successfully: $file, mode:$mode \n";
	return true;
}
}

if(!function_exists("writeoutput2")){

function writeoutput2($file, $string, $mode="w",$debug=true) {
	if ($debug){
		echo "\n".__FUNCTION__.":Writing to file ($file) the contents:\n\n$string\n\n";
	}
	
	
	
                if (!($fp = fopen($file, $mode))) {
                        echo "hata: dosya acilamadi: $file (writeoutput) !";
                        return false;
                }
                if (!fputs($fp, $string . "\n")) {
                        fclose($fp);
                        echo "hata: dosyaya yazilamadi: $file (writeoutput) !";
                        return false;
                }
                fclose($fp);
                return true;
}
}


if(!function_exists("alanlarial")){
function alanlarial($db2,$tablo) { // adodb de calsyor.
        foreach($db2->MetaColumnNames($tablo) as $alan) $alanlar[]=$alan;
    return $alanlar;
}
}

if(!function_exists("strop")){
function strop($str,$bas,$son) {
        return $bas.$str.$son;
}
}

if(!function_exists("arrayop")){
function arrayop($arr,$op) {
        foreach($arr as $ar) $ret[]=$op($ar,"{","}");
    return $ret;
}
}

if(!function_exists("executeprog2")){
function executeprog2($prog){ // echoes output.
        passthru($prog, $val);
        return ($val==0);
}
}

if(!function_exists("executeprog")){
function executeprog($prog){ // does not echo output. only return it.
        $fp = popen("$prog", 'r');
        if(!$fp){
        	return "<br>Cannot Execute: $prog ".__FUNCTION__;
        }
        $read = fread($fp, 8192);
        pclose($fp);
        return $read;
}
}

if(!function_exists('degiskenal')){
function degiskenal($degiskenler) {
	$alansayisi=count($degiskenler);
	for ($i=0;$i<$alansayisi;$i++) {
		global ${$degiskenler[$i]};
		if($_POST[$degiskenler[$i]]<>"") ${$degiskenler[$i]}=$_POST[$degiskenler[$i]];
		else ${$degiskenler[$i]}=$_GET[$degiskenler[$i]];
		$degerler[]=${$degiskenler[$i]};
	};
	return $degerler;
}
}

if(!function_exists('replacelnieinfile')){
function replacelineinfile($find,$replace,$where,$addifnotexists=false) {
	// edit a line starting with $find, to edit especially conf files.. 
	
	debugecho("\nreplaceline: ($find -> $replace) in ($where) \n ");
	$bulundu=false;
	
	$filearr=@file($where);
	//if($find=='$dbrootpass=') print_r($filearr);
	
	if(!$filearr) {
		echo "cannot open file... returning...\n";
		return false;
	} //else print_r($file);

	$len=strlen($find);
	$newfile=array();
	
	foreach($filearr as $line){
		$line=trim($line)."\n";
		$sub=substr($line,0,$len);
		if($sub==$find) {
			$line=$replace."\n";
			$bulundu=true;
		}
		$newfile[]=$line;
		
	}
	
	if($addifnotexists and !$bulundu){
		echo "Line not found, adding at end: ($replace)\n";
		$newfile[]=$replace;
	}
	/*if($find=='$dbrootpass=') { 
		echo "yeni dosya:\n";
		print_r($newfile);
	}*/
	
	return arraytofile($where,$newfile);		
}

function replaceOrAddLineInFile($find,$replace,$where){
	return replacelineinfile($find,$replace,$where,true);
}

}


if(!function_exists("addifnotexists")){
function addifnotexists($what,$where) {
	debugecho("\naddifnotexists: ($what) -> ($where) \n ");
	$what.="\n"; 
	$filearr=@file($where);
	if(!$filearr) {
		echo "cannot open file, trying to create: ($where)\n";
		$fp = fopen($where,'w');
		fclose($fp); 
		$filearr=file($where);
		
	} //else print_r($file);

	if(array_search($what,$filearr)===false) {
		//echo "dosyada bulamadı ekliyor: $where -> $what \n";
		$filearr[]=$what;
		arraytofile($where,$filearr);		
		
	} else {
		//echo "buldu... sorun yok. \n";
		// already found, so, do not add
	}

}
}


if(!function_exists('getlocalip')){
function getlocalip($interface='eth0') {
	global $localip;
	$ipline=exec("ifconfig $interface | grep \"inet addr\"");
 	# echo $ipline."\n\n";
	$ipline=strstr($ipline,"addr:");
 	# echo $ipline."\n\n";
	$pos=strpos($ipline," ");
	$ipline=trim(substr($ipline,5,$pos-5));
 	# echo "($ipline)\n\n";
	$localip=$ipline;
	# echo "(getlocalip) your ip is determined to be ($localip) using interface $interface \n";
	return $ipline;
}
}

if(!function_exists("debugecho")){
function debugecho($str,$level=0) {
	$currentlevel=4;
	if($level>=$currentlevel) echo $str;

}
}


if(!function_exists("arraytofile")){
function arraytofile($file,$lines) {
	$new_content = join('',$lines);
	$fp = fopen($file,'w');
	$write = fwrite($fp, $new_content);
	fclose($fp); 
}
}

?>
Return current item: Easy Hosting Control Panel for Ubuntu