<?php
//================================================================
// KOOBI FUNC-INSTALLER
// Version: 2.0
// www.koobi-erweiterungen.de
//================================================================

# ERSETZE STRING
function str_replace_count($search,$replace,$subject,$times) {

    $subject_original=$subject;
    
    $len=strlen($search);    
    $pos=0;
    for ($i=1;$i<=$times;$i++) {
        $pos=strpos($subject,$search,$pos);
        
        if($pos!==false) {                
            $subject=substr($subject_original,0,$pos);
            $subject.=$replace;
            $subject.=substr($subject_original,$pos+$len);
            $subject_original=$subject;
        } else {
            break;
        }
    }
    
    return($subject);

}

# EXISTIERT TABELLE
function table_exists($table) {

		$table = PREFIX . "_" .$table;
  		$found = false;

  		$sql = $GLOBALS['db']->Query("SHOW TABLES");
 		while (($row = $sql->fetcharray()) AND $found == false)
  		{
   		 if ($row[0] == $table)
    		 $found = true;
  		}

 		 if ($found) return true;
 		 else return false;
}

# ENTFERNT TAGS
function strip_content($xml) {

	preg_match_all("/<([a-zA-Z0-9]*)>(.*?)<\/([a-zA-Z0-9]*)>/is", $xml, $treffer);

	$num = count($treffer) + 1;
	for ($i = 0; $i <= $num; $i++)
	{
   		$xml = str_replace($treffer[0][$i],"", $xml);
	}
	
	return str_replace(array("\n","\r"),"",$xml);
}

# UNTAR FUNKTIONEN
function getarchivetype($filename)
{
	$archivetype="unknown";
	$filenameparts=explode(".",$filename);
	if (($filenameparts[count($filenameparts)-1]=="gz") || ($filenameparts[count($filenameparts)-1]=="tgz"))
		{
			$archivetype="gzip";
		}
	if ($filenameparts[count($filenameparts)-1]=="bz2")
		{
			$archivetype="bzip2";
		}
	if ($filenameparts[count($filenameparts)-1]=="tar")
		{
			$archivetype="tar";
		}
	return $archivetype;
}
function getuncompressedfilename($compressedfilename)
{
	$compressedfilenameparts=explode(".",$compressedfilename);
	for ($count=0;$count<count($compressedfilenameparts)-1;$count++)
		{
			$uncompressedfilenameparts[]=$compressedfilenameparts[$count];
		}
	if ($compressedfilenameparts[count($compressedfilenameparts)-1]=="tgz")
		{
			$uncompressedfilenameparts[]="tar";
		}
	$uncompressedfilename=implode(".",$uncompressedfilenameparts);
	return $uncompressedfilename;
}
function unbzip2($bz2file)
{
	$content="";
	$file=bzopen($bz2file,"r");
	while (!feof($file))
		{
			$content.=bzread($file,1);
		}
	bzclose($file);
	$file=fopen(getuncompressedfilename($bz2file),"wb+");
	fwrite($file,$content);
	fclose($file);
		
}
function ungzip($gzfile)
{
	$content="";
	$file=gzopen($gzfile,"r");
	while (!gzeof($file))
		{
			$content.=gzread($file,1);
		}
	gzclose($file);
	$file=fopen(getuncompressedfilename($gzfile),"wb+");
	fwrite($file,$content);
	fclose($file);
}
function untar($tarfile)
{
	$tarfile=fopen($tarfile,"r");
	$datainfo="";
	$data="";
	while (!feof($tarfile))
		{
			$readdata=fread($tarfile,512);
			if (substr($readdata,257,5)=="ustar")
				{
					if (!empty($datainfo))
						{
							$poscount=0;
							$name="";
							while (substr($datainfo,$poscount,1)!=chr(0))
								{
									$name.=substr($datainfo,$poscount,1);
									$poscount++;
								}
							if (!empty($name))
								{
									$name = BASE_DIR."/addons/" .$name;
								
									if (substr($name,-1)=="/")
										{
											mkdir($name);
										}
									else
										{
											$datasize=strlen($data)-1;
											while ((substr($data,$datasize,1)==chr(0)) && ($datasize>-1))
												{
													$datasize--;
												}
											$datasize++;
											$filedata="";
											for ($datacount=0;$datacount<$datasize;$datacount++)
												{
													$filedata.=substr($data,$datacount,1);
												}
											$file=fopen($name,"wb+");
											fwrite($file,$filedata);
											fclose($file);
										}
								}
							$datainfo=$readdata;
							$data="";
						}
					else
						{
							$datainfo=$readdata;
						}
				}
			else
				{
					$data.=$readdata;
				}
		}
	if (!empty($datainfo))
		{
			$poscount=0;
			$name="";
			while (substr($datainfo,$poscount,1)!=chr(0))
				{
					$name.=substr($datainfo,$poscount,1);
					$poscount++;
				}
			if (!empty($name))
				{
					$name = BASE_DIR."/addons/" .$name;
				
					if (substr($name,-1)=="/")
						{
							mkdir($name);
						}
					else
						{
							$datasize=strlen($data)-1;
							while ((substr($data,$datasize,1)==chr(0)) && ($datasize>-1))
								{
									$datasize--;
								}
							$datasize++;
							$filedata="";
							for ($datacount=0;$datacount<$datasize;$datacount++)
								{
									$filedata.=substr($data,$datacount,1);
								}
							$file=fopen($name,"wb+");
							fwrite($file,$filedata);
							fclose($file);
						}
				}
			$datainfo=$readdata;
			$data="";
		}	
	fclose($tarfile);
}
?>