<commit_before><?php

namespace App\Providers;

use App\Models\Admin\Formulario;
use Facebook\Exceptions\FacebookAuthenticationException;
use Facebook\Exceptions\FacebookAuthorizationException;
use Facebook\Exceptions\FacebookClientException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Exceptions\FacebookServerException;
use FacebookAds\Api;
use App\Models\Admin\Token;

use FacebookAds\Http\Exception\AuthorizationException;
use FacebookAds\Object\Lead;
use FacebookAds\Object\Page;
use FacebookAds\Object\LeadgenForm;
use Illuminate\Contracts\Validation\UnauthorizedException;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use FacebookAds\Cursor;
use Illuminate\Support\Facades\Mail;

class FacebookProvider
{
    public static function conexionFacebook()
    {
        $token = Token::first();
        Api::init(env('APP_ID'), env('APP_SECRET'), $token->token);
    }

    public static function fb_login()
    {
        if (!session_id()) {
            session_start();
        }

        $fb = new \Facebook\Facebook([
            'app_id' => env('APP_ID'),
            'app_secret' => env('APP_SECRET'),
            'default_graph_version' => env('API_V'),
        ]);

        $helper = $fb->getRedirectLoginHelper();

        $permissions = ['manage_pages', 'publish_pages']; // Optional permissions
//        manage_pages y publish_pages
        $loginUrl = $helper->getLoginUrl(url('callback'), $permissions);
//        $loginUrl = $helper->getLoginUrl('http://localhost:8888/mass-digital/public/callback', $permissions);
//        $loginUrl = $helper->getLoginUrl(route('callback', [$permissions]));

        return htmlspecialchars($loginUrl);
    }

    public static function callback()
    {
        if (!session_id()) {
            session_start();
        }
        $fb = new \Facebook\Facebook([
            'app_id' => env('APP_ID'),
            'app_secret' => env('APP_SECRET'),
            'default_graph_version' => env('API_V'),
        ]);

        $helper = $fb->getRedirectLoginHelper();

        try {
            $accessToken = $helper->getAccessToken();
        } catch
        (\Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

        if (!isset($accessToken)) {
            if ($helper->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                echo "Error: " . $helper->getError() . "\n";
                echo "Error Code: " . $helper->getErrorCode() . "\n";
                echo "Error Reason: " . $helper->getErrorReason() . "\n";
                echo "Error Description: " . $helper->getErrorDescription() . "\n";
            } else {
                header('HTTP/1.0 400 Bad Request');
                echo 'Bad request';
            }
            exit;
        }

// Logged in
//        echo '<h3>Access Token</h3>';
//        var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
//        echo '<h3>Metadata</h3>';
//        var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
        $tokenMetadata->validateAppId(env('APP_ID')); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
        $tokenMetadata->validateExpiration();

        if (!$accessToken->isLongLived()) {
            // Exchanges a short-lived access token for a long-lived one
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
                exit;
            }

//            echo '<h3>Long-lived</h3>';
//            var_dump($accessToken->getValue());
        }

        $token = Token::first();
        if (!$token) {
            $token = new Token();
        }
        $token->token = $accessToken->getValue();
        $token->expires_at = $accessToken->getExpiresAt();
        $token->save();

        $_SESSION['fb_access_token'] = (string)$accessToken;


// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

    }


    public function sincronizarLeads()
    {
        Cursor::setDefaultUseImplicitFetch(true);

        echo " Comienza sincronizacion" . chr(10) . chr(13);
        $this::conexionFacebook();
        $formularios = Formulario::where('activo', true)->where('con_estructura', true)->get();
        foreach ($formularios as $formulario) {
            $inicio = true;
//            Cursor::setDefaultUseImplicitFetch(true);
//            Lead::setDefaultReadFields();
            $form = new LeadgenForm($formulario->form_id);
            if ($formulario->fecha_ultimo_lead != null) {
                try {
                    $leads = $form->getLeads();

                } catch (AuthorizationException $e) {
                    if ($e->getCode() == 190) {
                        $mensaje = "Hubo un error al conectarse con Facebook, debe actualizar el token desde el administrador del sistema.";
                    } else {
                        $mensaje = "Hubo un error al conectarse con facebook, si continua recibiendo este mensaje en los proximos 4 minutos, comuniquese con el administrador del sistema.";
                    }
                    $this->sendMail($mensaje);
                    echo " No se pudo conectar a Facebook" . chr(10) . chr(13);
                    exit;
                }


//                $time_from = (new \DateTime($formulario->fecha_ultimo_lead))->getTimestamp();
//                $leads = $form->getLeads(array(), array(
//                    AdReportRunFields::FILTERING => array(
//                        array(
//                            'field' => 'time_created',
//                            'operator' => 'GREATER_THAN',
//                            'value' => $time_from,
//                        ),
//                    ),<commit_msg>fix control de acceso a facebook<commit_after><?php

namespace App\Providers;

use App\Models\Admin\Formulario;
use Facebook\Exceptions\FacebookAuthenticationException;
use Facebook\Exceptions\FacebookAuthorizationException;
use Facebook\Exceptions\FacebookClientException;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Exceptions\FacebookServerException;
use FacebookAds\Api;
use App\Models\Admin\Token;

use FacebookAds\Http\Exception\AuthorizationException;
use FacebookAds\Object\Lead;
use FacebookAds\Object\Page;
use FacebookAds\Object\LeadgenForm;
use Illuminate\Contracts\Validation\UnauthorizedException;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Schema\Blueprint;
use FacebookAds\Cursor;
use Illuminate\Support\Facades\Mail;

class FacebookProvider
{
    public static function conexionFacebook()
    {
        $token = Token::first();
        Api::init(env('APP_ID'), env('APP_SECRET'), $token->token);
    }

    public static function fb_login()
    {
        if (!session_id()) {
            session_start();
        }

        $fb = new \Facebook\Facebook([
            'app_id' => env('APP_ID'),
            'app_secret' => env('APP_SECRET'),
            'default_graph_version' => env('API_V'),
        ]);

        $helper = $fb->getRedirectLoginHelper();

        $permissions = ['manage_pages', 'publish_pages']; // Optional permissions
//        manage_pages y publish_pages
        $loginUrl = $helper->getLoginUrl(url('callback'), $permissions);
//        $loginUrl = $helper->getLoginUrl('http://localhost:8888/mass-digital/public/callback', $permissions);
//        $loginUrl = $helper->getLoginUrl(route('callback', [$permissions]));

        return htmlspecialchars($loginUrl);
    }

    public static function callback()
    {
        if (!session_id()) {
            session_start();
        }
        $fb = new \Facebook\Facebook([
            'app_id' => env('APP_ID'),
            'app_secret' => env('APP_SECRET'),
            'default_graph_version' => env('API_V'),
        ]);

        $helper = $fb->getRedirectLoginHelper();

        try {
            $accessToken = $helper->getAccessToken();
        } catch
        (\Facebook\Exceptions\FacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch (\Facebook\Exceptions\FacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }

        if (!isset($accessToken)) {
            if ($helper->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                echo "Error: " . $helper->getError() . "\n";
                echo "Error Code: " . $helper->getErrorCode() . "\n";
                echo "Error Reason: " . $helper->getErrorReason() . "\n";
                echo "Error Description: " . $helper->getErrorDescription() . "\n";
            } else {
                header('HTTP/1.0 400 Bad Request');
                echo 'Bad request';
            }
            exit;
        }

// Logged in
//        echo '<h3>Access Token</h3>';
//        var_dump($accessToken->getValue());

// The OAuth 2.0 client handler helps us manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();

// Get the access token metadata from /debug_token
        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
//        echo '<h3>Metadata</h3>';
//        var_dump($tokenMetadata);

// Validation (these will throw FacebookSDKException's when they fail)
        $tokenMetadata->validateAppId(env('APP_ID')); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
        $tokenMetadata->validateExpiration();

        if (!$accessToken->isLongLived()) {
            // Exchanges a short-lived access token for a long-lived one
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
            } catch (\Facebook\Exceptions\FacebookSDKException $e) {
                echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
                exit;
            }

//            echo '<h3>Long-lived</h3>';
//            var_dump($accessToken->getValue());
        }

        $token = Token::first();
        if (!$token) {
            $token = new Token();
        }
        $token->token = $accessToken->getValue();
        $token->expires_at = $accessToken->getExpiresAt();
        $token->save();

        $_SESSION['fb_access_token'] = (string)$accessToken;


// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');

    }


    public function sincronizarLeads()
    {
        Cursor::setDefaultUseImplicitFetch(true);

        echo " Comienza sincronizacion" . chr(10) . chr(13);
        $this::conexionFacebook();
        $formularios = Formulario::where('activo', true)->where('con_estructura', true)->get();
        foreach ($formularios as $formulario) {
            $inicio = true;
//            Cursor::setDefaultUseImplicitFetch(true);
//            Lead::setDefaultReadFields();
            $form = new LeadgenForm($formulario->form_id);
            if ($formulario->fecha_ultimo_lead != null) {
                try {
                    $leads = $form->getLeads();

                } catch (AuthorizationException $e) {
                    if ($e->getCode() == 190) {
                        $mensaje = "Hubo un error al conectarse con Facebook, debe actualizar el token desde el administrador del sistema.";
                    } else {
                        $mensaje = "Hubo un error al conectarse con facebook, si continua recibiendo este mensaje en los proximos 4 minutos, comuniquese con el administrador del sistema. Formulario = " . $formulario->form_id;
                    }
                    $this->sendMail($mensaje);
                    echo " No se pudo conectar a Facebook" . chr(10) . chr(13);
                    $leads = null;
//                    exit;
                }


//                $time_from = (new \DateTime($formulario->fecha_ultimo_lead))->getTimestamp();
//                $leads = $form->getLeads(array(), array(
//                    AdReportRunFields::FILTERING => array(
//                        array(
//                            'field' => 'time_created',
//                            'operator' => 'GREATER_THAN',
//                            'value' => $time_from,
//                        ),
//                    ),
{'fraction_non_alphanumeric': 0.13407988055244494, 'fraction_numerical': 0.0061216871967151925, 'mean_word_length': 2.0724770642201835, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 118, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765701', 'n_tokens_mistral': 3793, 'n_tokens_neox': 3623, 'n_words': 817}