

<?php
ini_set('max_execution_time', 300);

include '../config.php';
if($__testMode==true)
{
	return true;
}


function IsExist($path)
{
	if(is_dir($path))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function _NextIndex(int $_guid) 
{
	$isSExist=IsExist('userdata/'.$_guid);
	if($isSExist==true)
	{
		//echo "exist";
		do {
			$_guid=$_guid+1;
			$isSExist=IsExist('userdata/'.$_guid);
		} while ( $isSExist==  true);	
	}
	
	return $_guid;
}

$inp = file_get_contents('userdata/containts.json',true);
$tempArray = json_decode($inp)->sticker_packs;

$keys = array_keys($tempArray );
$last_key = array_pop($keys);

$guid = $tempArray[$last_key]->identifier +1;
$guid= _NextIndex($guid);
$realGuid=$guid;


$Identifire=$guid;

function resize_image($im, $dst_width, $dst_height) {
     $width = imagesx($im);
    $height = imagesy($im);

    $newImg = imagecreatetruecolor($dst_width, $dst_height);

    imagealphablending($newImg, false);
    imagesavealpha($newImg, true);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $width, $height, $transparent);
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);

    return $newImg;
}

 if(count($_POST['upload64']) > 0)
 {
	 
	 $guid='userdata/'.$guid;
	 
	 
	 $IsAnimated=false;
	 if( isset($_POST['IsAnimated']) )
	 {
		 $IsAnimated=$_POST['IsAnimated'] == "value1" ? true :false;
	 }

	 if (!file_exists($guid )) {
			mkdir($guid, 0777, true);
			mkdir($guid ."_png", 0777, true);
			mkdir($guid ."_small", 0777, true);
		}
	
	for($i=0;$i< count($_POST['upload64']) ; $i++)
	{
		
		$_i=$i + 1 ;
		$filePath = $guid ."_png/". sprintf("%02d", $_i) .".png";

		copy("userdata/temp/".$_POST['upload64'][$i].".png",$filePath);
		
		$files_[] = $guid. "/". sprintf("%02d", $_i).'.webp';
					
					$img = @imagecreatefrompng( $filePath);
				
					if($IsAnimated==false)
					{
						
						imagewebp($img, $guid. "/". sprintf("%02d", $_i).'.webp');
						$img = resize_image($img, 100, 100);
						imagewebp($img, $guid. "_small/". sprintf("%02d", $_i).'.webp');
						imagedestroy($img);
					}
					else{
					
						
						copy($filePath,$guid. "/". sprintf("%02d", $_i).'.png');
						copy($filePath,$guid. "_small/". sprintf("%02d", $_i).'.png');						
					}
					
					
					
					
	}
	
	$rootPath = realpath($guid);
		
		
	$existingPacks=json_decode($inp);

	$newObject = new stdClass;

	$newObject->identifier = "". $Identifire ."";
	$newObject->name = $_POST['StickerPackName'];
	$newObject->publisher = $_POST['publisher'];
	$newObject->tray_image_file = $IsAnimated==true? "01.png" : "01.webp";
	$newObject->publisher_email = '';	
	$newObject->IsAnimated = $IsAnimated;
	$newObject->publisher_website = '';
	$newObject->privacy_policy_website = "";
	$newObject->SignalStickerPackURL = $_POST['SignalStickerPackURL'];
	$newObject->license_agreement_website = "";
	$newObject->stickers =  array();
	
	if ($handle = opendir('./userdata/'.$Identifire )) {
    while (false !== ($entry = readdir($handle))) {


        if ($entry != "." && $entry != "..") {

			$sticker=new stdClass();
			

			$iparr = explode (".",$entry);
			$sticker->image_file=$iparr[0];
			$sticker->emojis=array();
			
			array_push($newObject->stickers,$sticker);

            //echo $iparr[0];
        }
    }

		closedir($handle);
	}

	$packs=json_decode($inp)->sticker_packs;
	
	array_push($packs,$newObject);
	
	$existingPacks->sticker_packs=$packs;
	//echo json_encode($existingPacks);
	
	file_put_contents('userdata/containts.json', json_encode($existingPacks));
	
	echo "<h1>Uploaded:</h1>";    
    if(is_array($files_)){
        echo "<ul>";
        foreach($files_ as $file){
            //echo "<li>$file</li>";
        }
        echo "</ul>";
    }
		
 }





?>