<?php
include '../config.php';  include '../checkSession.php';
$newimg = trim($_POST['newimg']);
$newfile1 = trim($_POST['newfilepfmIsArchivo']);
$newfile2 = trim($_POST['newfilecodigoDeEticaArchivo']);
$newfile3 = trim($_POST['newfilepoliticaAnticorrupcionArchivo']);
$newfile4 = trim($_POST['newfileavisoDePrivacidadArchivo']);

$oldimg = trim($_POST['oldimg']);
$oldimg2 = trim($_POST['oldimg2']);
$oldimg3 = trim($_POST['oldimg3']);
$oldimg4 = trim($_POST['oldimg4']);

$lang = $_GET['lang'];
$id = $_GET['id'];

$enviado = $_GET['enviado'];

$pfmIsArchivo = trim($_POST['pfmIsArchivo']);
$codigoDeEticaArchivo = trim($_POST['codigoDeEticaArchivo']);
$politicaAnticorrupcionArchivo = trim($_POST['politicaAnticorrupcionArchivo']);
$avisoDePrivacidadArchivo = trim($_POST['avisoDePrivacidadArchivo']);

$pfmIsSlug = trim($_POST['pfmIsSlug']);
$pfmIsTexto = trim($_GET['pfmIsTexto']);
$pfmIsTextoBoton = trim($_GET['pfmIsTextoBoton']);
$pfmIsTitulo = trim($_GET['pfmIsTitulo']);

$codigoDeEticaSlug = trim($_POST['codigoDeEticaSlug']);
$codigoDeEticaTexto = trim($_GET['codigoDeEticaTexto']);
$codigoDeEticaTextoBoton = trim($_GET['codigoDeEticaTextoBoton']);
$codigoDeEticaTitulo = trim($_GET['codigoDeEticaTitulo']);

//echo urlencode($codigoDeEticaTextoBoton);

$subtituloDePagina = trim($_POST['subtituloDePagina']);
$politicaAnticorrupcionArchivo = trim($_POST['politicaAnticorrupcionArchivo']);
$politicaAnticorrupcionSlug = trim($_POST['politicaAnticorrupcionSlug']);
$politicaAnticorrupcionTexto = trim($_GET['politicaAnticorrupcionTexto']);
$politicaAnticorrupcionTextoBoton = trim($_GET['politicaAnticorrupcionTextoBoton']);
$politicaAnticorrupcionTitulo = trim($_GET['politicaAnticorrupcionTitulo']);

$avisoDePrivacidadSlug = trim($_POST['avisoDePrivacidadSlug']);
$avisoDePrivacidadTexto = trim($_POST['avisoDePrivacidadTexto64']);
$avisoDePrivacidadTexto = base64_decode($avisoDePrivacidadTexto);
//echo $avisoDePrivacidadTexto;
$avisoDePrivacidadTextoBoton = trim($_GET['avisoDePrivacidadTextoBoton']);
$avisoDePrivacidadTitulo = trim($_GET['avisoDePrivacidadTitulo']);

$file1 = $newfile1;
$file2= $newfile2;
$file3 = $newfile3;
$file4 = $newfile4;

$pfmIsTexto  = urlencode($pfmIsTexto );
$pfmIsTextoBoton  = urlencode($pfmIsTextoBoton);
$pfmIsTitulo = urlencode($pfmIsTitulo);
$codigoDeEticaTexto = urlencode($codigoDeEticaTexto);


$codigoDeEticaTextoBoton = urlencode($codigoDeEticaTextoBoton);
$codigoDeEticaTitulo = urlencode($codigoDeEticaTitulo);

$politicaAnticorrupcionTexto = urlencode($politicaAnticorrupcionTexto);
$politicaAnticorrupcionTextoBoton = urlencode($politicaAnticorrupcionTextoBoton);


$politicaAnticorrupcionTitulo = urlencode($politicaAnticorrupcionTitulo);
$avisoDePrivacidadTexto = rawurlencode($avisoDePrivacidadTexto);

$avisoDePrivacidadTextoBoton = urlencode($avisoDePrivacidadTextoBoton);
$avisoDePrivacidadTitulo = urlencode($avisoDePrivacidadTitulo);


    ////////////////SI HUBO CAMBIO DE IMAGEN
    
    if($newfile1 == 1){

        if (isset($_FILES['pfmIsArchivo']) && $_FILES['pfmIsArchivo']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath2 = $_FILES['pfmIsArchivo']['tmp_name'];
            $fileName2 = $_FILES['pfmIsArchivo']['name'];
            $fileSize2 = $_FILES['pfmIsArchivo']['size'];
            $fileType2 = $_FILES['pfmIsArchivo']['type'];
            $fileNameCmps2 = explode(".", $fileName2);
            $fileExtension2 = strtolower(end($fileNameCmps2));
        
            // sanitize file-name
            $newFileName2 = "PFMIs_archivo_".$lang. '.' . $fileExtension2;
    
            // check if file has one of the following extensions
            $allowedfileExtensions2 = array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'zip');
        
            if (in_array($fileExtension2, $allowedfileExtensions2))
            {
            ///////////////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;
                $content2 = file_get_contents($fileTmpPath2);

                // 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_type2 = $fileExtension;

                if($content_type2 == 'image/svg' ){ $content_type2 = "image/svg+xml";}
                if($content_type2 == 'zip' ){ $content_type2 = "application/zip";}
                if($content_type2 == 'doc' || $content_type2 == 'docx' ){ $content_type2 = "application/msword";}
                if($content_type2 == 'xls' || $content_type2 == 'xlsx' ){ $content_type2 = "application/vnd.ms-excel";}
                // Name of content on S3
                $content_title2 = 'docs/cumplimiento/'.$lang.'/'.$newFileName2;

                // 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_type2;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content2);
                // 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_title2;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content2);
                $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_title2;
                $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, $content2);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $file1 = $content_title2;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de archivo 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 archivo:<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['imagen']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }

    if($newfile2 == 1){

        if (isset($_FILES['codigoDeEticaArchivo']) && $_FILES['codigoDeEticaArchivo']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath3 = $_FILES['codigoDeEticaArchivo']['tmp_name'];
            $fileName3 = $_FILES['codigoDeEticaArchivo']['name'];
            $fileSize3 = $_FILES['codigoDeEticaArchivo']['size'];
            $fileType3 = $_FILES['codigoDeEticaArchivo']['type'];
            $fileNameCmps3 = explode(".", $fileName3);
            $fileExtension3 = strtolower(end($fileNameCmps3));
        
            // sanitize file-name
            $newFileName3 = "codigoEtica_".$lang. '.' . $fileExtension3;
    
            // check if file has one of the following extensions
            $allowedfileExtensions3 = array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'zip');
        
            if (in_array($fileExtension3, $allowedfileExtensions3))
            {
            ///////////////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;
                $content3 = file_get_contents($fileTmpPath3);

                // 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_type3 = $fileExtension3;

                if($content_type3 == 'image/svg' ){ $content_type3 = "image/svg+xml";}
                if($content_type3 == 'zip' ){ $content_type3 = "application/zip";}
                if($content_type3 == 'doc' || $content_type3 == 'docx' ){ $content_type3 = "application/msword";}
                if($content_type3 == 'xls' || $content_type3 == 'xlsx' ){ $content_type3 = "application/vnd.ms-excel";}
                // Name of content on S3
                $content_title3 = 'docs/cumplimiento/'.$lang.'/'.$newFileName3;

                // 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_type3;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content3);
                // 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_title3;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content3);
                $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_title3;
                $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, $content3);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $file2 = $content_title3;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de archivo 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 archivo:<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['imagen']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }

    if($newfile3 == 1){

        if (isset($_FILES['politicaAnticorrupcionArchivo']) && $_FILES['politicaAnticorrupcionArchivo']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath4 = $_FILES['politicaAnticorrupcionArchivo']['tmp_name'];
            $fileName4 = $_FILES['politicaAnticorrupcionArchivo']['name'];
            $fileSize4 = $_FILES['politicaAnticorrupcionArchivo']['size'];
            $fileType4 = $_FILES['politicaAnticorrupcionArchivo']['type'];
            $fileNameCmps4 = explode(".", $fileName4);
            $fileExtension4 = strtolower(end($fileNameCmps4));
        
            // sanitize file-name
            $newFileName4 = "politicaAnticorrupcion_".$lang. '.' . $fileExtension4;
    
            // check if file has one of the following extensions
            $allowedfileExtensions4 = array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'zip');
        
            if (in_array($fileExtension4, $allowedfileExtensions4))
            {
            ///////////////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;
                $content4 = file_get_contents($fileTmpPath4);

                // 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_type4 = $fileExtension4;

                if($content_type4 == 'image/svg' ){ $content_type4 = "image/svg+xml";}
                if($content_type4 == 'zip' ){ $content_type4 = "application/zip";}
                if($content_type4 == 'doc' || $content_type4 == 'docx' ){ $content_type4 = "application/msword";}
                if($content_type4 == 'xls' || $content_type4 == 'xlsx' ){ $content_type4 = "application/vnd.ms-excel";}
                // Name of content on S3
                $content_title4 = 'docs/cumplimiento/'.$lang.'/'.$newFileName4;

                // 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_type4;
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content4);
                // 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_title2;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content4);
                $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_title4;
                $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, $content4);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $file3 = $content_title4;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de archivo 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 archivo:<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['imagen']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }
    
    if($newfile4 == 1){

        if (isset($_FILES['avisoDePrivacidadArchivo']) && $_FILES['avisoDePrivacidadArchivo']['error'] === UPLOAD_ERR_OK)
        {
            // get details of the uploaded file
            $fileTmpPath5 = $_FILES['avisoDePrivacidadArchivo']['tmp_name'];
            $fileName5 = $_FILES['avisoDePrivacidadArchivo']['name'];
            $fileSize5 = $_FILES['avisoDePrivacidadArchivo']['size'];
            $fileType5 = $_FILES['avisoDePrivacidadArchivo']['type'];
            $fileNameCmps5 = explode(".", $fileName5);
            $fileExtension5 = strtolower(end($fileNameCmps5));
        
            // sanitize file-name
            $newFileName5 = "AvisoPrivacidad_".$lang. '.' . $fileExtension5;
    
            // check if file has one of the following extensions
            $allowedfileExtensions5 = array('doc', 'docx', 'xls', 'xlsx', 'pdf', 'zip');
        
            if (in_array($fileExtension5, $allowedfileExtensions5))
            {
            ///////////////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;
                $content5 = file_get_contents($fileTmpPath5);

                // 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_type5 = $fileExtension5;

                if($content_type5 == 'image/svg' ){ $content_type5 = "image/svg+xml";}
                if($content_type5 == 'zip' ){ $content_type5 = "application/zip";}
                if($content_type5 == 'doc' || $content_type5 == 'docx' ){ $content_type5 = "application/msword";}
                if($content_type5 == 'xls' || $content_type5 == 'xlsx' ){ $content_type5 = "application/vnd.ms-excel";}
                if($content_type2 == 'pdf' || $content_type2 == 'pdf' ){ $content_type2 = "application/pdf";}
                // Name of content on S3
                $content_title5 = 'docs/cumplimiento/'.$lang.'/'.$newFileName5;

                // 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_type5;
                $request_headers['Content-Disposition'] = 'inline';
                $request_headers['Date'] = $timestamp;
                $request_headers['Host'] = $host_name;
                $request_headers['x-amz-acl'] = $content_acl;
                $request_headers['x-amz-content-sha256'] = hash('sha256', $content5);
                // 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_title2;
                $canonical_request[] = "";
                $canonical_request[] = $canonical_headers;
                $canonical_request[] = "";
                $canonical_request[] = $signed_headers;
                $canonical_request[] = hash('sha256', $content5);
                $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_title5;
                $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, $content5);
                curl_exec($ch);
                $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                if($http_code != 200){
                    echo "Error AWS";
                }
                if($http_code == 200){
                    $file4 = $content_title5;
                }
        
                
            }
            else
            {
                $message = '<p style="color:red; font-size:12px;">Extensión de archivo 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 archivo:<br></p> ';
            $message .= '<p style="color:red; font-size:12px;">Error:' . $_FILES['imagen']['error'].'</p>';
            echo $message;
            exit();
        }
        echo $message;
    }

    $curl = curl_init();

    $id = 'cklaeou0g2t030a78vcjz373g';

    if($lang == "en"){

        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  updatePaginaCumplimiento(data: {enviadoRevision: \\"'.$enviado.'\\", avisoDePrivacidadArchivo: \\"'.$file4.'\\", avisoDePrivacidadSlug: \\"'.$avisoDePrivacidadSlug.'\\", avisoDePrivacidadImagen: \\"'.$avisoDePrivacidadImagen.'\\", avisoDePrivacidadTexto: \\"'.$avisoDePrivacidadTexto.'\\", avisoDePrivacidadTextoBoton: \\"'.$avisoDePrivacidadTextoBoton.'\\", avisoDePrivacidadTitulo: \\"'.$avisoDePrivacidadTitulo.'\\", codigoDeEticaArchivo: \\"'.$file2.'\\", codigoDeEticaImagen: \\"'.$codigoDeEticaImagen.'\\", codigoDeEticaSlug: \\"'.$codigoDeEticaSlug.'\\", codigoDeEticaTexto: \\"'.$codigoDeEticaTexto.'\\", codigoDeEticaTextoBoton: \\"'.$codigoDeEticaTextoBoton.'\\", codigoDeEticaTitulo: \\"'.$codigoDeEticaTitulo.'\\", pfmIsArchivo: \\"'.$file1.'\\", pfmIsImagen: \\"'.$pfmIsImagen.'\\", pfmIsSlug: \\"'.$pfmIsSlug.'\\", pfmIsTexto: \\"'.$pfmIsTexto.'\\", pfmIsTextoBoton: \\"'.$pfmIsTextoBoton.'\\", pfmIsTitulo: \\"'.$pfmIsTitulo.'\\", politicaAnticorrupcionArchivo: \\"'.$file3.'\\", politicaAnticorrupcionImagen: \\"'.$politicaAnticorrupcionImagen.'\\", politicaAnticorrupcionSlug: \\"'.$politicaAnticorrupcionSlug.'\\", politicaAnticorrupcionTextoBoton: \\"'.$politicaAnticorrupcionTextoBoton.'\\", politicaAnticorrupcionTexto: \\"'.$politicaAnticorrupcionTexto.'\\", politicaAnticorrupcionTitulo: \\"'.$politicaAnticorrupcionTitulo.'\\"}, where: {id: \\"'.$id.'\\"}) {\\r\\n    id\\r\\n  }\\r\\n  \\r\\n}","variables":{}}',
            CURLOPT_HTTPHEADER => array(
              'Content-Type: application/json',
              'Cookie: __cfduid=da2693cdfd39b7377a8a05e305da45b8f1613001694'
            ),
          ));

    }

    if($lang == "es"){

            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  updatePaginaCumplimiento(data: {localizations: {update: {data: {enviadoRevision: \\"'.$enviado.'\\", avisoDePrivacidadArchivo: \\"'.$file4.'\\", avisoDePrivacidadSlug: \\"'.$avisoDePrivacidadSlug.'\\", avisoDePrivacidadImagen: \\"'.$avisoDePrivacidadImagen.'\\", avisoDePrivacidadTexto: \\"'.$avisoDePrivacidadTexto.'\\", avisoDePrivacidadTextoBoton: \\"'.$avisoDePrivacidadTextoBoton.'\\", avisoDePrivacidadTitulo: \\"'.$avisoDePrivacidadTitulo.'\\", codigoDeEticaArchivo: \\"'.$file2.'\\", codigoDeEticaImagen: \\"'.$codigoDeEticaImagen.'\\", codigoDeEticaSlug: \\"'.$codigoDeEticaSlug.'\\", codigoDeEticaTexto: \\"'.$codigoDeEticaTexto.'\\", codigoDeEticaTextoBoton: \\"'.$codigoDeEticaTextoBoton.'\\", codigoDeEticaTitulo: \\"'.$codigoDeEticaTitulo.'\\", pfmIsArchivo: \\"'.$file1.'\\", pfmIsImagen: \\"'.$pfmIsImagen.'\\", pfmIsSlug: \\"'.$pfmIsSlug.'\\", pfmIsTexto: \\"'.$pfmIsTexto.'\\", pfmIsTextoBoton: \\"'.$pfmIsTextoBoton.'\\", pfmIsTitulo: \\"'.$pfmIsTitulo.'\\", politicaAnticorrupcionArchivo: \\"'.$file3.'\\", politicaAnticorrupcionImagen: \\"'.$politicaAnticorrupcionImagen.'\\", politicaAnticorrupcionSlug: \\"'.$politicaAnticorrupcionSlug.'\\", politicaAnticorrupcionTextoBoton: \\"'.$politicaAnticorrupcionTextoBoton.'\\", politicaAnticorrupcionTexto: \\"'.$politicaAnticorrupcionTexto.'\\", politicaAnticorrupcionTitulo: \\"'.$politicaAnticorrupcionTitulo.'\\"}, locale: es_MX}}}, where: {id: \\"'.$id.'\\"}) {\\r\\n    id\\r\\n  }\\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']['updatePaginaCumplimiento']['id'];

//echo $id;

  $sucess = "<p class='successmsg'>Cumplimiento actualizado correctamente</p>";
  $fail = "<p class='errormsg'>Falló la actualización..</p> ";


if(strlen($id)>10){

    echo $sucess;

    $string = $newstatus;
    $string = '"'.$string.'"';
    echo '<script>
        
        setTimeout(() => {
            $(".popUpCumplimiento").fadeOut();
        }, 1500);
    </script>';
}

else{
    echo $fail;
}

?>