-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHispaShare.php
More file actions
238 lines (201 loc) · 8.47 KB
/
Copy pathHispaShare.php
File metadata and controls
238 lines (201 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
class HispaShare
{
private $username;
private $password;
private $cookieFile;
private $baseUrl = 'https://www.hispashare.org';
private $debug;
// Modified constructor to remove hardcoded credentials and allow debug toggle
public function __construct($username, $password, $debug = false)
{
if (empty($username) || empty($password)) {
throw new Exception("Username and Password are required.");
}
$this->username = $username;
$this->password = $password;
$this->debug = $debug;
// Use system temp dir for cookies
$this->cookieFile = sys_get_temp_dir() . '/cookies_' . md5($username) . '.txt';
}
private function log($message)
{
// Only write logs if debug is enabled
if (!$this->debug) {
return;
}
// Write to a log file in the temp directory, not the project root
$logFile = sys_get_temp_dir() . '/debug_hispashare.log';
$timestamp = date('Y-m-d H:i:s');
file_put_contents($logFile, "[$timestamp] $message" . PHP_EOL, FILE_APPEND);
}
public function login()
{
$postData = [
'username' => $this->username,
'password' => $this->password,
'login' => 'Iniciar'
];
$response = $this->_request($this->baseUrl . '/', 'POST', $postData);
if (strpos($response, 'logout') !== false || strpos($response, 'Cerrar') !== false) {
return true;
}
if (strpos($response, 'name="login"') !== false || strpos($response, 'Nombre de usuario') !== false) {
$this->log("Login check failed.");
return false;
}
return true;
}
public function search($query, $params = [])
{
$queryParams = [
'view' => 'advsearch',
'advsrch' => '1',
'advtit' => $query
];
if (isset($params['imdb_min'])) $queryParams['advscomin'] = $params['imdb_min'];
if (isset($params['imdb_max'])) $queryParams['advscomax'] = $params['imdb_max'];
if (isset($params['duration_min'])) $queryParams['advdurmin'] = $params['duration_min'];
if (isset($params['duration_max'])) $queryParams['advdurmax'] = $params['duration_max'];
if (isset($params['year_min'])) $queryParams['advdatmin'] = $params['year_min'];
if (isset($params['year_max'])) $queryParams['advdatmax'] = $params['year_max'];
$url = $this->baseUrl . '/?' . http_build_query($queryParams);
$this->log("Searching URL: $url");
$html = $this->_request($url);
if (strpos($html, 'name="login"') !== false) {
$this->log("Login required during search. Attempting re-login.");
if ($this->login()) {
$html = $this->_request($url);
} else {
return [];
}
}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = [];
$nodes = $xpath->query('//a[starts-with(@href, "?view=title&id=")]');
foreach ($nodes as $node) {
if ($node->getElementsByTagName('small')->length > 0) {
$fullTitle = trim($node->textContent);
$href = $this->baseUrl . '/' . $node->getAttribute('href');
$year = null;
if (preg_match('/\(.*?(\d{4})\)$/', $fullTitle, $matches)) {
$year = (int) $matches[1];
}
$results[] = [
'title' => $fullTitle,
'year' => $year,
'url' => $href
];
}
}
return $results;
}
public function getLinks($url)
{
$this->log("Getting links from: $url");
$html = $this->_request($url);
if (strpos($html, 'name="login"') !== false) {
if ($this->login()) {
$html = $this->_request($url);
} else {
return [];
}
}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
// Extract IMDb ID
$imdbId = null;
$imdbNodes = $xpath->query('//a[contains(@href, "imdb.com/title/")]');
if ($imdbNodes->length > 0) {
$imdbHref = $imdbNodes->item(0)->getAttribute('href');
if (preg_match('/tt\d+/', $imdbHref, $matches)) {
$imdbId = $matches[0];
}
}
$links = [];
$rows = $xpath->query('//table[@class="T2"]//tr[td[@class="ICON"]]');
foreach ($rows as $row) {
$size = '';
$downloads = '';
$sizeNodes = $xpath->query('.//td[@class="SIZE"]', $row);
if ($sizeNodes->length >= 1) $size = trim($sizeNodes->item(0)->textContent);
if ($sizeNodes->length >= 2) $downloads = trim($sizeNodes->item(1)->textContent);
$anchor = $xpath->query('.//a[starts-with(@href, "javascript:Download")]', $row)->item(0);
if (!$anchor) continue;
$href = $anchor->getAttribute('href');
if (preg_match("/javascript:Download\((\d+)\s*,\s*['\"](\d+)['\"]\)/i", $href, $matches)) {
$id = $matches[1];
$code = $matches[2];
// Languages
$audio = [];
$subtitles = [];
$t2Div = $xpath->query('ancestor::div[table[@class="T2"]]', $row)->item(0);
if ($t2Div) {
$nextDiv = $t2Div->nextSibling;
while ($nextDiv && ($nextDiv->nodeType !== XML_ELEMENT_NODE || $nextDiv->tagName !== 'div')) {
$nextDiv = $nextDiv->nextSibling;
}
if ($nextDiv) {
$flags = $xpath->query('.//table[@class="T3"]//img[@class="FLAG"]', $nextDiv);
foreach ($flags as $flag) {
$alt = strtolower($flag->getAttribute('alt'));
if (strpos($alt, 'sub') !== false || strpos($alt, 'for') !== false) {
$subtitles[] = $alt;
} else {
$audio[] = $alt;
}
}
}
}
// AJAX Request
$ajaxUrl = $this->baseUrl . "/ajax/download.php?id=$id&code=$code&nocache=" . mt_rand();
$ajaxContent = $this->_request($ajaxUrl);
preg_match_all('/ed2k:\/\/\|file\|[^|]+\|\d+\|\w+\|(?:\/|h=\w+\|(?:\/)?)/i', $ajaxContent, $ed2kMatches);
foreach ($ed2kMatches[0] as $ed2k) {
$parts = explode('|', $ed2k);
$title = isset($parts[2]) ? urldecode($parts[2]) : 'Unknown';
// Simple dupe check
$isDupe = false;
foreach ($links as $l) { if ($l['url'] === $ed2k) $isDupe = true; }
if (!$isDupe) {
$links[] = [
'type' => 'ed2k',
'title' => $title,
'url' => $ed2k,
'size' => $size,
'downloads' => $downloads,
'audio' => $audio,
'subtitles' => $subtitles,
'imdb_id' => $imdbId
];
}
}
}
}
return $links;
}
private function _request($url, $method = 'GET', $data = [])
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFile);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
?>