<?php
include("cpanelapi.php");

function chechkdns($domain,$ns){
	$dns = dns_get_record($domain, DNS_NS);
	return $dns[$ns]['target'];
}

function chechkIP($domain){
	$cIP = gethostbyname($domain);
	return $cIP;
}

function addcron($cpaneluser,$cpanelpass, $cpanelport, $command, $jmi="0", $jho="*", $jda="*", $jmo="*", $jwe="*"){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);
	$xmlapi->set_output('json');
	$domain = $_SERVER['SERVER_NAME'];
	$call=array( "command" => $command, "day" => $jda, "hour" => $jho, "minute" => $jmi, "month" => $jmo, "weekday" => $jwe);
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Cron", "add_line", $call);
		//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	//Print_r($result);
	if($result['cpanelresult']['data'][0]['status']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['statusmsg'];
	}
	return $str;
}
	//$cj=cronjob($cpaneluser, $cpanelpass,"*","0","*","*","*"); her gece saat 12'de

function ccron($cpaneluser, $cpanelpass, $cpanelport, $linekey, $jmi="0", $jho="*", $jda="*", $jmo="*", $jwe="*"){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);
	$xmlapi->set_output('json');
	$domain = $_SERVER['SERVER_NAME'];
	$command = "wget http://".$domain."/cronjob";
	$day = '0'; $hour = '*'; $minute = '*'; $month = '*'; $weekday = '*';
	$call=array( "linekey" => $linekey, "command" => $command, "day" => $jda, "hour" => $jho, "minute" => $jmi, "month" => $jmo, "weekday" => $jwe);
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Cron", "edit_line", $call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	Print_r($result);
	if($result['cpanelresult']['data'][0]['status']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['statusmsg'];
	}
	return $str;
}
	//$cj=cronjob($cpaneluser, $cpanelpass,"*","0","*","*","*"); her gece saat 12'de

function delcron($cpaneluser, $cpanelpass, $cpanelport, $line){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);
	$xmlapi->set_output('json');
	$domain = $_SERVER['SERVER_NAME'];
	
	$call=array( "line" => $line);
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Cron", "remove_line", $call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	Print_r($result);
	if($result['cpanelresult']['data'][0]['status']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['statusmsg'];
	}
	return $str;
}

function listmail($cpaneluser, $cpanelpass, $cpanelport){		// aktif değil
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$email_domain = $_SERVER['SERVER_NAME'];
	$call = array(domain=>$email_domain, nearquotaonly=>'0', no_validate=>'0');
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Email", "listpopswithdisk" , $call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	return $result;
}
function createmail($cpaneluser, $cpanelpass, $cpanelport, $email, $pass){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$email_domain = $_SERVER['SERVER_NAME'];
	$email_user = $email;
	$email_password = $pass;
	$email_query = '10';
	$email_quota = '500';
	
	$call = array(domain=>$email_domain, email=>$email_user, password=>$email_password, quota=>$email_quota);
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Email", "addpop", $call);
		//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}	
	//Print_r($result);
	if($result['cpanelresult']['data'][0]['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['reason'];
	}
	return $str;
}

function cpassmail($cpaneluser, $cpanelpass, $cpanelport, $email, $pass){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);    
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false  
	$xmlapi->set_output('json');//set this for browser output
	$email_domain = $_SERVER['SERVER_NAME'];
	$email_user = $email;
	$email_password = $pass;
	$email_query = '10';
	$email_quota = '500';
	$call = array(domain=>$email_domain, email=>$email_user, password=>$email_password, quota=>$email_quota);
	try{
		$result = $xmlapi->api2_query($cpaneluser, "Email", "passwdpop", $call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	Print_r($result);
	if($result['cpanelresult']['data'][0]['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['reason'];
	}
	return $str;
}

function delmail($cpaneluser, $cpanelpass, $cpanelport, $email){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$email_domain = $_SERVER['SERVER_NAME'];
	$call = array('domain' => $email_domain, 'email' => $email);
	try{
		$result = $xmlapi->api2_query($cpaneluser, 'Email', 'delpop', $call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	print_r($result);
	if($result['cpanelresult']['data'][0]['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['reason'];
	}
	return  $str;
}

function listsubdomain($cpaneluser, $cpanelpass, $cpanelport){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$domain = $_SERVER['SERVER_NAME'];	
	try{
		$result = $xmlapi->api2_query($cpaneluser, 'SubDomain', 'listsubdomains');
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	return $result;
}

function createsubdomain($cpaneluser, $cpanelpass, $cpanelport, $sub, $subfolder="public_html/"){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$domain = $_SERVER['SERVER_NAME'];
	
	$call=array($sub,$domain,0,0,$subfolder);
	try{
		$result = $xmlapi->api1_query($cpaneluser,"SubDomain","addsubdomain",$call);
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	Print_r($result);
	if($result['event']['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['data']['result'];
	}
	return $str;
}

function delsubdomain($cpaneluser, $cpanelpass, $cpanelport, $subdomain){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	$domain = $_SERVER['SERVER_NAME'];
	try{
		$result = $xmlapi->api2_query($cpaneluser, 'SubDomain', 'delsubdomain', array( "domain" => $subdomain ) );
	//print_r($result);
		$result = json_decode($result,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	print_r($result);
	if($result['cpanelresult']['data'][0]['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['reason'];
	}
	return $str;
}

function createdomain($cpaneluser, $cpanelpass, $cpanelport, $domain, $subfolder = "public_html/"){
	$db_host = $_SERVER['SERVER_NAME'];
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);
	$xmlapi->set_output('json');
	$adcall = array('newdomain' => $domain, 'dir' => $subfolder, 'subdomain' => 'subdomain');
	try {
		$result = $xmlapi->api2_query($cpaneluser, 'AddonDomain', 'addaddondomain', $adcall);
		$result = json_decode($result, true);
	} catch (Exception $e) {
		echo "<strong>Error : </strong>".$e->getMessage();
	}
    //echo "<pre>", print_r($result), "</pre>";
	if($result['cpanelresult']['data'][0]['result']==1){
		$str = "İşlem Başarılı";
	}else{
		$str = $result['cpanelresult']['data'][0]['reason'];
	}
	return $str;
}

function listcron($cpaneluser, $cpanelpass, $cpanelport) {
    $db_host = $_SERVER['SERVER_NAME'];
    $xmlapi = new xmlapi($db_host);
    $xmlapi->password_auth($cpaneluser, $cpanelpass);
    $xmlapi->set_port($cpanelport);
    $xmlapi->set_debug(0);
    $xmlapi->set_output('json');

    try {
        $result = $xmlapi->api2_query($cpaneluser, "Cron", "listcron");
        $result = json_decode($result, true);

        // Check if the result contains the expected data
        if (isset($result['cpanelresult']['data'])) {
            return $result['cpanelresult']['data'];
        } else {
            return []; // Return an empty array if no data is found
        }
    } catch (Exception $e) {
        echo "<strong>Error : </strong>" . $e->getMessage();
        return []; // Return an empty array on error
    }
}


function listmysql($cpaneluser, $cpanelpass, $cpanelport){
    // Server adı
    $db_host = $_SERVER['SERVER_NAME']; 
    // XMLAPI nesnesini oluştur
    $xmlapi = new xmlapi($db_host); 
    
    // CPanel kimlik doğrulaması
    $xmlapi->password_auth($cpaneluser, $cpanelpass); 
    
    // CPanel port ayarlarını yap
    $xmlapi->set_port($cpanelport);
    
    // Yanıt formatını JSON olarak belirle
    $xmlapi->set_output('json'); 
    
    try {
        // listdatabases fonksiyonu ile veritabanlarını al
        $result = $xmlapi->api2_query($cpaneluser, "Mysql", "listdatabases");
        
        // JSON cevabını dizilere çevir
        $result = json_decode($result, true);
        
        // Veritabanı listesini döndür
        return $result['cpanelresult']['data'];
        
    } catch (Exception $e) {
        // Hata mesajını yazdır
        echo "<strong>Error : </strong>" . $e->getMessage();
    }
}

function createmysql($cpaneluser, $cpanelpass, $cpanelport, $sqloneki, $domain){
	$db_host = "localhost";
	//$domain = $_SERVER['SERVER_NAME'];
	$gpass=generateSPassword(6);
	if($sqloneki==""){	//önek gelmezse "cuser_domain5_Sd5f4D"
		$sqloneki = substr($domain,0,5)."_".generateSPassword(6);
	}
	$databasename = $cpaneluser."_".$sqloneki;
	$databaseuser = $cpaneluser."_".$sqloneki;
	$databasepass = generateSPassword(10);
	$xmlapi = new xmlapi($db_host);
	$xmlapi->password_auth($cpaneluser, $cpanelpass);
	$xmlapi->set_port($cpanelport);
	$xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false
	$xmlapi->set_output('json');//set this for browser output
	try{
		$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));
		$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));  
		$addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));
	//print_r($result);
		$createdb = json_decode($createdb,true);
		$usr = json_decode($usr,true);
		$addusr = json_decode($addusr,true);
	}
	catch(Exception $e){
		echo "<strong>Error : </strong>" . $e->getMessage();
	}
	if($addusr['event']['result']==1){
		$dexplode = explode(".", $domain); $cexplode = count($dexplode);
		if($cexplode==3){ $DomainName=$dexplode[1]; }elseif($cexplode==2){ $DomainName=$dexplode[0]; }
		$str = "ok";
		$sqlfile = fopen("config/sql.php", "w") or die("Unable to open file!");
		$data = '<?php'  . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = 'if($_GET["resetsql"]<>""){$_SESSION["V3Domain"]=""; $_SESSION["V3Server"]=""; $_SESSION["V3User"]=""; $_SESSION["V3Pass"]=""; $_SESSION["V3Db"]="";}' . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = "\n";
		fwrite($sqlfile, $data);
		$data = 'if($_SESSION["V3Server"]==""){' . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = '$V3Server="'.$db_host.'"; $V3User="'.$databaseuser.'"; $V3Pass="'.$databasepass.'"; $V3Db="'.$databasename.'";' . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = '}else{' . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = '$V3Server = $_SESSION["V3Server"]; $V3User = $_SESSION["V3User"]; $V3Pass = $_SESSION["V3Pass"]; $V3Db = $_SESSION["V3Db"];' . PHP_EOL;
		fwrite($sqlfile, $data);
		$data = '}';
		fwrite($sqlfile, $data);
		$data = "\n";
		fwrite($sqlfile, $data);
		$data = 'if(substr(phpversion(),0,1)>5){$ver=substr(phpversion(),0,1);}else{$ver="";}';
		fwrite($sqlfile, $data);
		$data = "\n";
		fwrite($sqlfile, $data);
		$data = '?>';
		fwrite($sqlfile, $data);
		fclose($sqlfile);
	}else{
		$str = $addusr['event']['reason'];
	}
	return $str;
}

?>