<?php
include '../config.php';
// prevent XSS
$_GET   = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST  = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);


$enviado = $_GET['enviado'];
///////////ESPAÑOL

    $time = time();
    
    $titulo_es = trim($_POST['titulo_es']);
    $titulo_es = strip_tags($titulo_es);
    $titulo_es = htmlspecialchars($titulo_es);

    $texto_es = trim($_POST['texto_es']);
    $texto_es = urlencode($texto_es);//WYSIWYG Editor
    $texto_es = strip_tags($texto_es);
    $texto_es = htmlspecialchars($texto_es);

    $enlaceBoton_es = trim($_POST['enlaceBoton_es']);
    $enlaceBoton_es = strip_tags($enlaceBoton_es);
    $enlaceBoton_es = htmlspecialchars($enlaceBoton_es);

    $textoBoton_es = trim($_POST['textoBoton_es']);
    $textoBoton_es = strip_tags($textoBoton_es);
    $textoBoton_es = htmlspecialchars($textoBoton_es);

    $encabezado_es = '';
    $video_es = '';

    //////////////SUBIR LOGO
    if($_FILES['encabezado_es']['size'] > 0){

        if (isset($_FILES['encabezado_es']) && $_FILES['encabezado_es']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath = $_FILES['encabezado_es']['tmp_name'];
            $fileName = $_FILES['encabezado_es']['name'];
            $fileSize = $_FILES['encabezado_es']['size'];
            $fileType = $_FILES['encabezado_es']['type'];
            $fileNameCmps = explode(".", $fileName);
            $fileExtension = strtolower(end($fileNameCmps));
        
            // sanitize file-name
            $newFileName = "anuncio_es_".$time. '.' . $fileExtension;

            // check if file has one of the following extensions
            $allowedfileExtensions = array('jpg', 'gif', 'png', 'webp', 'svg');
        
            if (in_array($fileExtension, $allowedfileExtensions))
            {
            ///////////////SUBIR A AWS
                /// AWS API keys
                $aws_access_key_id = $aws_access_key_id;
                $aws_secret_access_key = $aws_secret_access_key;
                // Bucket
                $bucket_name = $bucket_name;
                // AWS region and Host Name (Host names are different for each AWS region)
                // As an example these are set to us-east-1 (US Standard)
                $aws_region = $aws_region;
                $host_name = $bucket_name . '.s3.amazonaws.com';

                // Server path where content is present. This is just an example
                //$content_path = $dest_path;
                $content = file_get_contents($fileTmpPath);

                // AWS file permissions
                $content_acl = 'public-read';

                // MIME type of file. Very important to set if you later plan to load the file from a S3 url in the browser (images, for example)
                $content_type = 'image/'.$fileExtension;

                if($content_type == 'image/svg' ){ $content_type = "image/svg+xml";}
                // Name of content on S3
                $content_title = 'img/inicio/anuncios/'.$newFileName;

                // Service name for S3
                $aws_service_name = 's3';

                // UTC timestamp and date
                $timestamp = gmdate('Ymd\THis\Z');
                $date = gmdate('Ymd');

                // HTTP request headers as key & value
                $request_headers = array();
                $request_headers['Content-Type'] = $content_type;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content);
                // Sort it in ascending order
                ksort($request_headers);

                // Canonical headers
                $canonical_headers = [];
                foreach($request_headers as $key => $value) {
                    $canonical_headers[] = strtolower($key) . ":" . $value;
                }
                $canonical_headers = implode("\n", $canonical_headers);

                // Signed headers
                $signed_headers = [];
                foreach($request_headers as $key => $value) {
                    $signed_headers[] = strtolower($key);
                }
                $signed_headers = implode(";", $signed_headers);

                // Cannonical request 
                $canonical_request = [];
                $canonical_request[] = "PUT";
                $canonical_request[] = "/" . $content_title;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content);
                $canonical_request = implode("\n", $canonical_request);
                $hashed_canonical_request = hash('sha256', $canonical_request);

                // AWS Scope
                $scope = [];
                $scope[] = $date;
                $scope[] = $aws_region;
                $scope[] = $aws_service_name;
                $scope[] = "aws4_request";

                // String to sign
                $string_to_sign = [];
                $string_to_sign[] = "AWS4-HMAC-SHA256"; 
                $string_to_sign[] = $timestamp; 
                $string_to_sign[] = implode('/', $scope);
                $string_to_sign[] = $hashed_canonical_request;
                $string_to_sign = implode("\n", $string_to_sign);

                // Signing key
                $kSecret = 'AWS4' . $aws_secret_access_key;
                $kDate = hash_hmac('sha256', $date, $kSecret, true);
                $kRegion = hash_hmac('sha256', $aws_region, $kDate, true);
                $kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
                $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);

                // Signature
                $signature = hash_hmac('sha256', $string_to_sign, $kSigning);

                // Authorization
                $authorization = [
                    'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
                    'SignedHeaders=' . $signed_headers,
                    'Signature=' . $signature
                ];
                $authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);

                // Curl headers
                $curl_headers = [ 'Authorization: ' . $authorization ];
                foreach($request_headers as $key => $value) {
                    $curl_headers[] = $key . ": " . $value;
                }

                $url = 'https://' . $host_name . '/' . $content_title;
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $encabezado_es = $content_title;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de imagen no válida. Verifica que tu archivo tenga uno de los siguientes formatos: ' . implode(', ', $allowedfileExtensions).'</p>';
                echo $message;
                exit();
            }
        }
        else
        {
            $message = '<p style="color:red; font-size:12px;">Error en subida de imagen (ES):<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['encabezado_es']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }

        //////////////SUBIR VIDEO
        if($_FILES['video_es']['size'] > 0){

            if (isset($_FILES['video_es']) && $_FILES['video_es']['error'] === UPLOAD_ERR_OK)
            {
                // get details of the uploaded file
                $fileTmpPathv = $_FILES['video_es']['tmp_name'];
                $fileNamev = $_FILES['video_es']['name'];
                $fileSizev = $_FILES['video_es']['size'];
                $fileTypev = $_FILES['video_es']['type'];
                $fileNameCmpsv = explode(".", $fileNamev);
                $fileExtensionv = strtolower(end($fileNameCmpsv));
            
                // sanitize file-name
                $newFileNamev = "Anuncio_".$time.'_es'.$lang. '.' . $fileExtensionv;
        
                // check if file has one of the following extensions
                $allowedfileExtensionsv = array('mp4', 'webp');
            
                if (in_array($fileExtensionv, $allowedfileExtensionsv))
                {
                ///////////////SUBIR A AWS
                    /// AWS API keys
                    $aws_access_key_id = $aws_access_key_id;
                    $aws_secret_access_key = $aws_secret_access_key;
                    // Bucket
                    $bucket_name = $bucket_name;
                    // AWS region and Host Name (Host names are different for each AWS region)
                    // As an example these are set to us-east-1 (US Standard)
                    $aws_region = $aws_region;
                    $host_name = $bucket_name . '.s3.amazonaws.com';
    
                    // Server path where content is present. This is just an example
                    //$content_path = $dest_path;
                    $contentv = file_get_contents($fileTmpPathv);
    
                    // AWS file permissions
                    $content_acl = 'public-read';
    
                    // MIME type of file. Very important to set if you later plan to load the file from a S3 url in the browser (images, for example)
                    $content_typev = 'video/'.$fileExtensionv;
                    // Name of content on S3
                    $content_titlev = 'videos/inicio/anuncios/'.$newFileNamev;
    
                    // Service name for S3
                    $aws_service_name = 's3';
    
                    // UTC timestamp and date
                    $timestamp = gmdate('Ymd\THis\Z');
                    $date = gmdate('Ymd');
    
                    // HTTP request headers as key & value
                    $request_headers = array();
                    $request_headers['Content-Type'] = $content_typev;
                    $request_headers['Date'] = $timestamp;
                    $request_headers['Host'] = $host_name;
                    $request_headers['x-amz-acl'] = $content_acl;
                    $request_headers['x-amz-content-sha256'] = hash('sha256', $contentv);
                    // Sort it in ascending order
                    ksort($request_headers);
    
                    // Canonical headers
                    $canonical_headers = [];
                    foreach($request_headers as $key => $value) {
                        $canonical_headers[] = strtolower($key) . ":" . $value;
                    }
                    $canonical_headers = implode("\n", $canonical_headers);
    
                    // Signed headers
                    $signed_headers = [];
                    foreach($request_headers as $key => $value) {
                        $signed_headers[] = strtolower($key);
                    }
                    $signed_headers = implode(";", $signed_headers);
    
                    // Cannonical request 
                    $canonical_request = [];
                    $canonical_request[] = "PUT";
                    $canonical_request[] = "/" . $content_titlev;
                    $canonical_request[] = "";
                    $canonical_request[] = $canonical_headers;
                    $canonical_request[] = "";
                    $canonical_request[] = $signed_headers;
                    $canonical_request[] = hash('sha256', $contentv);
                    $canonical_request = implode("\n", $canonical_request);
                    $hashed_canonical_request = hash('sha256', $canonical_request);
    
                    // AWS Scope
                    $scope = [];
                    $scope[] = $date;
                    $scope[] = $aws_region;
                    $scope[] = $aws_service_name;
                    $scope[] = "aws4_request";
    
                    // String to sign
                    $string_to_sign = [];
                    $string_to_sign[] = "AWS4-HMAC-SHA256"; 
                    $string_to_sign[] = $timestamp; 
                    $string_to_sign[] = implode('/', $scope);
                    $string_to_sign[] = $hashed_canonical_request;
                    $string_to_sign = implode("\n", $string_to_sign);
    
                    // Signing key
                    $kSecret = 'AWS4' . $aws_secret_access_key;
                    $kDate = hash_hmac('sha256', $date, $kSecret, true);
                    $kRegion = hash_hmac('sha256', $aws_region, $kDate, true);
                    $kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
                    $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);
    
                    // Signature
                    $signature = hash_hmac('sha256', $string_to_sign, $kSigning);
    
                    // Authorization
                    $authorization = [
                        'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
                        'SignedHeaders=' . $signed_headers,
                        'Signature=' . $signature
                    ];
                    $authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);
    
                    // Curl headers
                    $curl_headers = [ 'Authorization: ' . $authorization ];
                    foreach($request_headers as $key => $value) {
                        $curl_headers[] = $key . ": " . $value;
                    }
    
                    $url = 'https://' . $host_name . '/' . $content_titlev;
                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_HEADER, false);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $contentv);
                    curl_exec($ch);
                    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                    if($http_code != 200){
                        echo "Error AWS";
                    }
                    if($http_code == 200){
                        $video_es = $content_titlev;
                    }
            
                    
                }
                else
                {
                    $message = '<p style="color:red; font-size:12px;">Extensión de video no válida. Verifica que tu archivo tenga uno de los siguientes formatos: ' . implode(', ', $allowedfileExtensions).'</p>';
                    echo $message;
                    exit();
                }
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Error en subida de video:<br></p> ';
                $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['video_es']['error'].'</p>';
                echo $message;
                exit();
            }
            echo $message;
        }
    


    ///////////INGLÉS

    $time_en = time();

    $titulo_en = trim($_POST['titulo_en']);
    $titulo_en = strip_tags($titulo_en);
    $titulo_en = htmlspecialchars($titulo_en);

    $texto_en = trim($_POST['texto_en']);
    $texto_en = urlencode($texto_en);//WYSIWYG Editor
    $texto_en = strip_tags($texto_en);
    $texto_en = htmlspecialchars($texto_en);

    $enlaceBoton_en = trim($_POST['enlaceBoton_en']);
    $enlaceBoton_en = strip_tags($enlaceBoton_en);
    $enlaceBoton_en = htmlspecialchars($enlaceBoton_en);

    $textoBoton_en = trim($_POST['textoBoton_en']);
    $textoBoton_en = strip_tags($textoBoton_en);
    $textoBoton_en = htmlspecialchars($textoBoton_en);

    $encabezado_en = '';
    $video_en = '';

    //////////////SUBIR LOGO
    if($_FILES['encabezado_en']['size'] > 0){

        if (isset($_FILES['encabezado_en']) && $_FILES['encabezado_en']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath = $_FILES['encabezado_en']['tmp_name'];
            $fileName = $_FILES['encabezado_en']['name'];
            $fileSize = $_FILES['encabezado_en']['size'];
            $fileType = $_FILES['encabezado_en']['type'];
            $fileNameCmps = explode(".", $fileName);
            $fileExtension = strtolower(end($fileNameCmps));
        
            // sanitize file-name
            $newFileName = "anuncio_en_".$time. '.' . $fileExtension;

            // check if file has one of the following extensions
            $allowedfileExtensions = array('jpg', 'gif', 'png', 'webp', 'svg');
        
            if (in_array($fileExtension, $allowedfileExtensions))
            {
            ///////////////SUBIR A AWS
                /// AWS API keys
                $aws_access_key_id = $aws_access_key_id;
                $aws_secret_access_key = $aws_secret_access_key;
                // Bucket
                $bucket_name = $bucket_name;
                // AWS region and Host Name (Host names are different for each AWS region)
                // As an example these are set to us-east-1 (US Standard)
                $aws_region = $aws_region;
                $host_name = $bucket_name . '.s3.amazonaws.com';

                // Server path where content is present. This is just an example
                //$content_path = $dest_path;
                $content = file_get_contents($fileTmpPath);

                // AWS file permissions
                $content_acl = 'public-read';

                // MIME type of file. Very important to set if you later plan to load the file from a S3 url in the browser (images, for example)
                $content_type = 'image/'.$fileExtension;

                if($content_type == 'image/svg' ){ $content_type = "image/svg+xml";}
                // Name of content on S3
                $content_title = 'img/inicio/anuncios/'.$newFileName;

                // Service name for S3
                $aws_service_name = 's3';

                // UTC timestamp and date
                $timestamp = gmdate('Ymd\THis\Z');
                $date = gmdate('Ymd');

                // HTTP request headers as key & value
                $request_headers = array();
                $request_headers['Content-Type'] = $content_type;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content);
                // Sort it in ascending order
                ksort($request_headers);

                // Canonical headers
                $canonical_headers = [];
                foreach($request_headers as $key => $value) {
                    $canonical_headers[] = strtolower($key) . ":" . $value;
                }
                $canonical_headers = implode("\n", $canonical_headers);

                // Signed headers
                $signed_headers = [];
                foreach($request_headers as $key => $value) {
                    $signed_headers[] = strtolower($key);
                }
                $signed_headers = implode(";", $signed_headers);

                // Cannonical request 
                $canonical_request = [];
                $canonical_request[] = "PUT";
                $canonical_request[] = "/" . $content_title;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content);
                $canonical_request = implode("\n", $canonical_request);
                $hashed_canonical_request = hash('sha256', $canonical_request);

                // AWS Scope
                $scope = [];
                $scope[] = $date;
                $scope[] = $aws_region;
                $scope[] = $aws_service_name;
                $scope[] = "aws4_request";

                // String to sign
                $string_to_sign = [];
                $string_to_sign[] = "AWS4-HMAC-SHA256"; 
                $string_to_sign[] = $timestamp; 
                $string_to_sign[] = implode('/', $scope);
                $string_to_sign[] = $hashed_canonical_request;
                $string_to_sign = implode("\n", $string_to_sign);

                // Signing key
                $kSecret = 'AWS4' . $aws_secret_access_key;
                $kDate = hash_hmac('sha256', $date, $kSecret, true);
                $kRegion = hash_hmac('sha256', $aws_region, $kDate, true);
                $kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
                $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);

                // Signature
                $signature = hash_hmac('sha256', $string_to_sign, $kSigning);

                // Authorization
                $authorization = [
                    'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
                    'SignedHeaders=' . $signed_headers,
                    'Signature=' . $signature
                ];
                $authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);

                // Curl headers
                $curl_headers = [ 'Authorization: ' . $authorization ];
                foreach($request_headers as $key => $value) {
                    $curl_headers[] = $key . ": " . $value;
                }

                $url = 'https://' . $host_name . '/' . $content_title;
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $encabezado_en = $content_title;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de imagen no válida. Verifica que tu archivo tenga uno de los siguientes formatos: ' . implode(', ', $allowedfileExtensions).'</p>';
                echo $message;
                exit();
            }
        }
        else
        {
            $message = '<p style="color:red; font-size:12px;">Error en subida de imagen (EN):<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['encabezado_en']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }
    
    
    //////////////SUBIR VIDEO
    if($_FILES['video_en']['size'] > 0){

        if (isset($_FILES['video_en']) && $_FILES['video_en']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPathv = $_FILES['video_en']['tmp_name'];
            $fileNamev = $_FILES['video_en']['name'];
            $fileSizev = $_FILES['video_en']['size'];
            $fileTypev = $_FILES['video_en']['type'];
            $fileNameCmpsv = explode(".", $fileNamev);
            $fileExtensionv = strtolower(end($fileNameCmpsv));
        
            // sanitize file-name
            $newFileNamev = "Anuncio_".$time.'_en'.$lang. '.' . $fileExtensionv;
    
            // check if file has one of the following extensions
            $allowedfileExtensionsv = array('mp4', 'webp');
        
            if (in_array($fileExtensionv, $allowedfileExtensionsv))
            {
            ///////////////SUBIR A AWS
                /// AWS API keys
                $aws_access_key_id = $aws_access_key_id;
                $aws_secret_access_key = $aws_secret_access_key;
                // Bucket
                $bucket_name = $bucket_name;
                // AWS region and Host Name (Host names are different for each AWS region)
                // As an example these are set to us-east-1 (US Standard)
                $aws_region = $aws_region;
                $host_name = $bucket_name . '.s3.amazonaws.com';

                // Server path where content is present. This is just an example
                //$content_path = $dest_path;
                $contentv = file_get_contents($fileTmpPathv);

                // AWS file permissions
                $content_acl = 'public-read';

                // MIME type of file. Very important to set if you later plan to load the file from a S3 url in the browser (images, for example)
                $content_typev = 'video/'.$fileExtensionv;
                // Name of content on S3
                $content_titlev = 'videos/inicio/anuncios/'.$newFileNamev;

                // Service name for S3
                $aws_service_name = 's3';

                // UTC timestamp and date
                $timestamp = gmdate('Ymd\THis\Z');
                $date = gmdate('Ymd');

                // HTTP request headers as key & value
                $request_headers = array();
                $request_headers['Content-Type'] = $content_typev;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $contentv);
                // Sort it in ascending order
                ksort($request_headers);

                // Canonical headers
                $canonical_headers = [];
                foreach($request_headers as $key => $value) {
                    $canonical_headers[] = strtolower($key) . ":" . $value;
                }
                $canonical_headers = implode("\n", $canonical_headers);

                // Signed headers
                $signed_headers = [];
                foreach($request_headers as $key => $value) {
                    $signed_headers[] = strtolower($key);
                }
                $signed_headers = implode(";", $signed_headers);

                // Cannonical request 
                $canonical_request = [];
                $canonical_request[] = "PUT";
                $canonical_request[] = "/" . $content_titlev;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $contentv);
                $canonical_request = implode("\n", $canonical_request);
                $hashed_canonical_request = hash('sha256', $canonical_request);

                // AWS Scope
                $scope = [];
                $scope[] = $date;
                $scope[] = $aws_region;
                $scope[] = $aws_service_name;
                $scope[] = "aws4_request";

                // String to sign
                $string_to_sign = [];
                $string_to_sign[] = "AWS4-HMAC-SHA256"; 
                $string_to_sign[] = $timestamp; 
                $string_to_sign[] = implode('/', $scope);
                $string_to_sign[] = $hashed_canonical_request;
                $string_to_sign = implode("\n", $string_to_sign);

                // Signing key
                $kSecret = 'AWS4' . $aws_secret_access_key;
                $kDate = hash_hmac('sha256', $date, $kSecret, true);
                $kRegion = hash_hmac('sha256', $aws_region, $kDate, true);
                $kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
                $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);

                // Signature
                $signature = hash_hmac('sha256', $string_to_sign, $kSigning);

                // Authorization
                $authorization = [
                    'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
                    'SignedHeaders=' . $signed_headers,
                    'Signature=' . $signature
                ];
                $authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);

                // Curl headers
                $curl_headers = [ 'Authorization: ' . $authorization ];
                foreach($request_headers as $key => $value) {
                    $curl_headers[] = $key . ": " . $value;
                }

                $url = 'https://' . $host_name . '/' . $content_titlev;
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $contentv);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $video_en = $content_titlev;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de video no válida. Verifica que tu archivo tenga uno de los siguientes formatos: ' . implode(', ', $allowedfileExtensions).'</p>';
                echo $message;
                exit();
            }
        }
        else
        {
            $message = '<p style="color:red; font-size:12px;">Error en subida de video:<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['video_en']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }


    ///////////////////////PETICIÓN GRAPH CMS
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $endpoint_graph_cms,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',

        CURLOPT_POSTFIELDS =>'{"query":"mutation MyMutation {\\r\\n  createEnlacePaginaHome(data: {encabezado: \\"'.$encabezado_en.'\\", textoBoton: \\"'.$textoBoton_en.'\\", video: \\"'.$video_en.'\\", enlaceBoton: \\"'.$enlaceBoton_en.'\\", enviadoRevision: \\"'.$enviado.'\\", estatus: \\"borrador\\", localizations: {create: {data: {encabezado: \\"'.$encabezado_es.'\\", textoBoton: \\"'.$textoBoton_es.'\\", video: \\"'.$video_es.'\\", enlaceBoton: \\"'.$enlaceBoton_es.'\\", enviadoRevision: \\"'.$enviado.'\\", estatus: \\"borrador\\", texto: \\"'.$texto_es.'\\", titulo: \\"'.$titulo_es.'\\"}, locale: es_MX}}, titulo: \\"'.$titulo_en.'\\", texto: \\"'.$texto_en.'\\"}) {\\r\\n    id\\r\\n  }\\r\\n}","variables":{}}',


        CURLOPT_HTTPHEADER => array(
          'Content-Type: application/json',
          'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
        ),
      ));

    $response = curl_exec($curl);
    curl_close($curl);
    //echo $response;

    $jsonrespuesta = json_decode($response, true);
    $id = $jsonrespuesta['data']['createEnlacePaginaHome']['id'];
    //echo $id;

    if(strlen($id)>10){
        echo '
        <script>
            $("#formnew").trigger("reset");
            $("#formnew").trigger("reset");
            $("#respuestanew").append("<h5>Anuncio añadido correctamente.</h5>");
        
            setTimeout(() => {
                $(".popUpNew").fadeOut();        
            }, 3000);
        </script>';
    }

    else{
        $errorclass ="class='errormensaje'"; 
        echo '
        <script>
            $("#formnew").trigger("reset");
            $("#formnew").trigger("reset");
            $("#respuestanew").append("<p '.$errorclass.'>OCurrió un error al añadir el contenido. Verifica la información o intenta nuevamente en unos minutos.</p>");

            setTimeout(() => {
                //$(".popUpNew").fadeOut();        
            }, 3000);
        </script>';
    }



