Passed
Push — master ( 52d10f...990f47 )
by Darko
11:27
created

clearAdultSiteCookies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Enhanced getRawHtml function with automatic age verification
5
 *
6
 * This is a replacement/enhancement for the existing getRawHtml() in helpers.php
7
 * that automatically handles age verification for adult sites.
8
 */
9
10
use Blacklight\processing\adult\AgeVerificationManager;
11
use GuzzleHttp\Client;
12
use GuzzleHttp\Exception\RequestException;
13
14
if (! function_exists('getRawHtmlWithAgeVerification')) {
15
    /**
16
     * Get raw HTML with automatic age verification handling
17
     *
18
     * @param  string  $url  URL to fetch
19
     * @param  string|false  $cookie  Optional cookie string (legacy support)
20
     * @param  string|null  $postData  Optional POST data
21
     * @return string|array|false
22
     */
23
    function getRawHtmlWithAgeVerification($url, $cookie = false, $postData = null)
24
    {
25
        static $ageVerificationManager = null;
26
27
        // Initialize manager on first use
28
        if ($ageVerificationManager === null) {
29
            $ageVerificationManager = new AgeVerificationManager;
30
        }
31
32
        // Check if this is an adult site that needs age verification
33
        $adultSites = [
34
            'adultdvdempire.com',
35
            'adultdvdmarketplace.com',
36
            'aebn.net',
37
            'hotmovies.com',
38
            'popporn.com',
39
        ];
40
41
        $isAdultSite = false;
42
        foreach ($adultSites as $site) {
43
            if (stripos($url, $site) !== false) {
44
                $isAdultSite = true;
45
                break;
46
            }
47
        }
48
49
        // For adult sites, use the age verification manager
50
        if ($isAdultSite) {
51
            try {
52
                $options = [];
53
54
                // Handle POST data if provided
55
                if ($postData !== null) {
56
                    $options['form_params'] = $postData;
57
                    $cookieJar = $ageVerificationManager->getCookieJar($url);
58
59
                    $client = new Client([
60
                        'cookies' => $cookieJar,
61
                        'headers' => [
62
                            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
63
                            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
64
                        ],
65
                    ]);
66
67
                    $response = $client->post($url, $options);
68
                    $html = $response->getBody()->getContents();
69
                } else {
70
                    $html = $ageVerificationManager->makeRequest($url, $options);
71
                }
72
73
                // Try to decode as JSON (some APIs return JSON)
74
                if ($html !== false) {
75
                    $jsonResponse = json_decode($html, true);
76
                    if (json_last_error() === JSON_ERROR_NONE) {
77
                        return $jsonResponse;
78
                    }
79
                }
80
81
                return $html;
82
83
            } catch (RequestException $e) {
84
                if (config('app.debug') === true) {
85
                    \Log::error('getRawHtmlWithAgeVerification: '.$e->getMessage());
86
                }
87
88
                return false;
89
            } catch (\Exception $e) {
90
                if (config('app.debug') === true) {
91
                    \Log::error('getRawHtmlWithAgeVerification: '.$e->getMessage());
92
                }
93
94
                return false;
95
            }
96
        }
97
98
        // For non-adult sites, use standard approach
99
        return getRawHtml($url, $cookie, $postData);
0 ignored issues
show
Bug Best Practice introduced by
The expression return getRawHtml($url, $cookie, $postData) also could return the type boolean which is incompatible with the documented return type array|false|string.
Loading history...
Bug introduced by
It seems like $cookie can also be of type string; however, parameter $cookie of getRawHtml() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return getRawHtml($url, /** @scrutinizer ignore-type */ $cookie, $postData);
Loading history...
100
    }
101
}
102
103
if (! function_exists('initializeAdultSiteCookies')) {
104
    /**
105
     * Initialize age verification cookies for all adult sites
106
     * Run this once during application setup
107
     *
108
     * @return array Statistics about initialized cookies
109
     */
110
    function initializeAdultSiteCookies(): array
111
    {
112
        $manager = new AgeVerificationManager;
113
114
        $sites = [
115
            'https://www.adultdvdempire.com',
116
            'https://www.adultdvdmarketplace.com',
117
            'https://straight.theater.aebn.net',
118
            'https://www.hotmovies.com',
119
            'https://www.popporn.com',
120
        ];
121
122
        $results = [
123
            'initialized' => 0,
124
            'failed' => 0,
125
            'sites' => [],
126
        ];
127
128
        foreach ($sites as $url) {
129
            try {
130
                // Making a request will automatically set up cookies
131
                $cookieJar = $manager->getCookieJar($url);
132
                $domain = parse_url($url, PHP_URL_HOST);
133
134
                $results['sites'][$domain] = [
135
                    'status' => 'initialized',
136
                    'cookies' => count($cookieJar),
137
                ];
138
                $results['initialized']++;
139
140
            } catch (\Exception $e) {
141
                $domain = parse_url($url, PHP_URL_HOST);
142
                $results['sites'][$domain] = [
143
                    'status' => 'failed',
144
                    'error' => $e->getMessage(),
145
                ];
146
                $results['failed']++;
147
            }
148
        }
149
150
        return $results;
151
    }
152
}
153
154
if (! function_exists('getAdultSiteCookieStats')) {
155
    /**
156
     * Get statistics about stored adult site cookies
157
     *
158
     * @return array Cookie statistics
159
     */
160
    function getAdultSiteCookieStats(): array
161
    {
162
        $manager = new AgeVerificationManager;
163
164
        return $manager->getCookieStats();
165
    }
166
}
167
168
if (! function_exists('clearAdultSiteCookies')) {
169
    /**
170
     * Clear all stored adult site cookies
171
     *
172
     * @param  string|null  $domain  Optional specific domain to clear
173
     * @return int|bool Number of cookies cleared (or bool for specific domain)
174
     */
175
    function clearAdultSiteCookies(?string $domain = null)
176
    {
177
        $manager = new AgeVerificationManager;
178
179
        if ($domain !== null) {
180
            return $manager->clearCookies($domain);
181
        }
182
183
        return $manager->clearAllCookies();
184
    }
185
}
186