Untitled

 avatar
unknown
plain_text
a year ago
5.6 kB
3
Indexable
<?php

namespace App\Http\Controllers;

use App\Models\GTMCodes;
use App\Models\Proxy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class HandleUrlController extends Controller
{
    protected string $proxy_id;
    public function proxy(Request $request)
    {
        $url = $request->query('url');
        try {
            // Make a request to the target URL using Laravel's HTTP client


            // Extract all GTM codes from the HTML response
            $response = Http::get($url);
            $html = $response->body();
            $gtmCodes = [];

            $pattern = '/GTM-([a-zA-Z0-9]+)/i';
            preg_match_all($pattern, $html, $matches);
            $this->extractAllGtmCodes($html);
            if (!empty($matches[1])) {
                $gtmCodes = $matches[1];
            }
            return response()->json(['gtmCodes' => array_unique($gtmCodes), 'response' => $html]);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }

    private function extractAllGtmCodes($html)
    {

        $pattern = '/googletagmanager\.com\/gtm\.js\?id=([^\s"\'<]+)/i';
        preg_match_all($pattern, $html, $matches);

        $gtmCodes = [];

        if (!empty($matches[1])) {

            foreach ($matches[1] as $containerId) {
                $gtmCode = $this->extractGtmCode($html, $containerId);
                if ($gtmCode !== null) {
                    $gtmCodes[$containerId] = $gtmCode;
                    Log::info($gtmCode);
                }
            }
        }

        return $gtmCodes;
    }

    private function extractGtmCode($html, $containerId)
    {
        $gtmCodePattern = '/<script[^>]*googletagmanager\.com\/gtm\.js\?id=([^"\'<]+)[^>]*>(.*?)<\/script>/is';
        preg_match($gtmCodePattern, $html, $codeMatches);


        var_dump($html);
        var_dump($codeMatches);

        if (!empty($codeMatches[2])) {
            Log::info($codeMatches[2]);
            return $codeMatches[2];

        }

        return null; // GTM code not found
    }



    public function storeProxy(Request $request)
    {
        try {
            $url = $request->url;
            $status = $request->status;

            // Create a new Proxy record
            $proxyhit = Proxy::create([
                'url' => $url,
                'status' => $status
            ]);
            $proxyID = $proxyhit->id;
            return response()->json(['success' => 'proxy added', 'id' => $proxyID], 200);
        } catch (\Throwable $err) {
            return response()->json(['error' => $err->getMessage()], 500);
        }
    }
    

    public function storeGTM(Request $request)
    {
        try {
            $gtm = $request->gtm;
            $proxyID = $request->proxyID;
            //Log::info($gtm);
            $this->addGTM($gtm, $proxyID);
        //if (is_array($gtm)) {
        //    foreach ($gtm as $singleGTM) {
                //Log::info($singleGTM);
               // GTMCodes::create([
               //     'gtm_codes' => $gtm,
               //     'proxy_id' => $proxyID
               // ]);
         //   }
       // } else {
            //Log::info($gtm);
        //    GTMCodes::create([
         //       'gtm_codes' => $gtm,
         //       'proxy_id' => $proxyID
         //   ]);
      //  }
        return response()->json(['success' => 'proxy added'], 200);
        } catch (\Throwable $th) {
        }
    }
    private function addGTM($gtm, $proxID){
        try {
            Log::info($gtm);
            Log::info($proxID);
            GTMCodes::create([
                       'gtm_codes' => $gtm,
                       'proxy_id' => $proxID
                   ]);
        return response()->json(['success' => 'proxy added'], 200);
        } catch (\Throwable $th) {
            return response()->json(['error' => $th->getMessage()], 500);
        }
    }

    public function checkDuplicateUrl(Request $request)
    {
        try {
            $url = $request->url;
            $url_data = Proxy::where('url', $url)->first();

            if ($url_data) {
                return response()->json(['success' => 'url exist', 'status_code' => 200], 200);
            } else {
                return response()->json(['error' => 'url did not exist ', 'status_code' => 404]);
            }
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function listAllUrl()
    {
        try {
            $list = Proxy::all();
            return view('index', compact('list'));
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function fetchData()
    {
        try {
            $list = Proxy::all();
            return view('list.url-list', compact('list'));
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    public function getGTM(Request $request) {
        try {
            $id = $request->id;
            Log::info('Requested ID: ' . $id);
            
            $gtmCodes = GTMCodes::where('proxy_id', $id)->pluck('gtm_codes');
            
            Log::info('GTM Codes: ' . $gtmCodes);
            
            return response()->json(['gtmCodes' => $gtmCodes], 200);
        } catch (\Throwable $th) {
            return response()->json(['error' => $th->getMessage()], 500);
        }
    }
    
}

Editor is loading...
Leave a Comment