<?php

function m_url_post($url,$post,$ref=false)
{
		if(!$ref)
		{
		$ref = $url;
		}
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_REFERER,$ref);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
		// Bağlantı ve toplam zaman aşımı: panelin bloklanmasını engeller
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3);
		curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
		$data= curl_exec($ch);
		curl_close($ch);
		return $data;
}
function m_url_get($url,$ref=false)
{
		if(!$ref)
		{
		$ref = $url;
		}
		$ch = curl_init();
		$timeout = 5;
		curl_setopt ($ch, CURLOPT_URL, $url);
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_REFERER,$ref);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$data= curl_exec($ch);
		curl_close($ch);
		return $data;
}

function m_explode($data,$start,$finish,$s,$f)
{
	$return = explode($start,$data);
	$return = explode($finish,$return[$s]);
	return $return[$f];
}

// Büyük güncelleme paketlerini güvenle indirmek için: akış bazlı ve diske yazan yardımcı
// Kullanım: list($ok, $out) = m_url_download_update($url, $destinationFilePath, $maxBytesOptional, $refOptional);
function m_url_download_update($url, $destPath, $maxBytes = null, $ref=false)
{
		if(!$ref) { $ref = $url; }
		$ch = curl_init();
		$timeout = 10;
		$defaultMax = 512 * 1024 * 1024; // 512MB varsayılan limit (güncellemeler için büyük tutuldu)
		$limit = $maxBytes !== null ? (int)$maxBytes : (defined('M_URL_UPDATE_MAX_BYTES') ? (int)M_URL_UPDATE_MAX_BYTES : $defaultMax);
		$fp = @fopen($destPath, 'wb');
		if ($fp === false) { return [false, 'file_open_failed']; }
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_REFERER, $ref);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_TIMEOUT, 1200); // büyük dosya için daha uzun süre
		$downloaded = 0;
		curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $chunk) use (&$downloaded, $limit, $fp) {
			$len = strlen($chunk);
			$downloaded += $len;
			if ($downloaded > $limit) { return 0; }
			fwrite($fp, $chunk);
			return $len;
		});
		$ok = curl_exec($ch);
		$err = null;
		if ($ok === false) { $err = curl_error($ch); }
		curl_close($ch);
		fclose($fp);
		if ($ok === false) { @unlink($destPath); return [false, 'curl_error: '.$err]; }
		if ($downloaded > $limit) { @unlink($destPath); return [false, 'size_limit_exceeded']; }
		return [true, $destPath];
}

// ZIP çıkarma yardımcısı: ZipArchive ile güvenli extract
// Dönüş: [bool success, string message]
function m_zip_extract($zipFile, $targetDir)
{
		if (!class_exists('ZipArchive')) { return [false, 'ziparchive_missing']; }
		if (!is_file($zipFile)) { return [false, 'zip_not_found']; }
		if (!is_dir($targetDir)) { @mkdir($targetDir, 0775, true); }
		$zip = new ZipArchive();
		$open = $zip->open($zipFile);
		if ($open !== true) { return [false, 'zip_open_failed: '.$open]; }
		$ok = $zip->extractTo($targetDir);
		$zip->close();
		return [$ok === true, $ok ? 'ok' : 'zip_extract_failed'];
}

?>