· 4 years ago · Aug 19, 2021, 07:48 PM
1<?php
2
3if (!defined('RAPIDLEECH')) {
4 require_once ('index.html');
5 exit();
6}
7
8class rapidgator_net extends DownloadClass {
9 private $baseUrl, $link, $cJar, $page, $cookie, $referer, $DLregexp;
10 public function Download($link) {
11 $this->baseUrl = 'https://rapidgator.net/';
12
13 $link = parse_url($link);
14 $link['scheme'] = 'https';
15 $link['host'] = 'rapidgator.net';
16 $this->link = $GLOBALS['Referer'] = $link = rebuild_url($link);
17 $this->cookie = array('lang' => 'en');
18 $this->DLregexp = '@https?://(?:pr|(s))\d+\.rapidgator\.net/(?(1)download/)[^\s\"\'<>]+@i';
19 if ((empty($_POST['step']) || !in_array($_POST['step'], array('1', '2', 'L'))) && (empty($_GET['rgredir']) || (stripos($_GET['rgredir'], '/auth/login') === false && stripos($_GET['rgredir'], '/site/ChangeLocation/key/') === false))) {
20 // Weird RG redirects.
21 $rdc = 0;
22 $this->page = false; // False value for starting the loop.
23 $redir = $this->link;
24 $this->referer = !empty($GLOBALS['Referer']) ? $GLOBALS['Referer'] : $this->link;
25 while (($redir = $this->ChkRGRedirs($this->page, $redir)) && $rdc < 15) {
26 $this->page = cURL($redir, $this->cookie, 0, $this->referer);
27 $this->cookie = GetCookiesArr($this->page, $this->cookie);
28 $this->referer = $redir;
29 $rdc++;
30 }
31
32 // I haven't tested those redirects fine so i will check this too.
33 if (stripos($redir, 'rapidgator.net/file/') === false) {
34 $this->page = cURL($this->link, $this->cookie, 0, $this->referer);
35 $this->cookie = GetCookiesArr($this->page, $this->cookie);
36 }
37
38 is_present($this->page, '>File not found<', 'File not found.');
39 }
40
41 if ($_REQUEST['premium_acc'] == 'on' && ((!empty($_REQUEST['premium_user']) && !empty($_REQUEST['premium_pass'])) || (!empty($GLOBALS['premium_acc']['rapidgator_net']['user']) && !empty($GLOBALS['premium_acc']['rapidgator_net']['pass'])))) {
42 $this->Login();
43 } else html_error('Login Failed: User or Password is empty.');
44 }
45
46 private function PremiumDL() {
47 $page = cURL($this->link, $this->cookie);
48
49 if (preg_match('@You have reached quota of downloaded information for premium accounts. At the moment, the quota is \d+ [GT]B(?: per \d+ day\(s\))?@i', $page, $err)) html_error($err[0]);
50
51 if (!preg_match($this->DLregexp, $page, $dlink)) html_error('Error: Download-link not found.');
52
53 $this->RedirectDownload($dlink[0], 'rapidgatorpr');
54 }
55
56 private function Login() {
57 if (!empty($_REQUEST['pA_encrypted']) && !empty($_REQUEST['premium_user']) && !empty($_REQUEST['premium_pass'])) {
58 $_REQUEST['premium_user'] = decrypt(urldecode($_REQUEST['premium_user']));
59 $_REQUEST['premium_pass'] = decrypt(urldecode($_REQUEST['premium_pass']));
60 unset($_REQUEST['pA_encrypted']);
61 }
62 $pA = (empty($_REQUEST['premium_user']) || empty($_REQUEST['premium_pass']) ? false : true);
63 $user = ($pA ? $_REQUEST['premium_user'] : $GLOBALS['premium_acc']['rapidgator_net']['user']);
64 $pass = ($pA ? $_REQUEST['premium_pass'] : $GLOBALS['premium_acc']['rapidgator_net']['pass']);
65
66 if (empty($user) || empty($pass)) html_error('Login Failed: User or Password is empty. Please check login data.');
67 $this->cookie = array('lang' => 'en'); // Account is always showed as free if it comes from a file, as i don't send file's link as referer, lets reset the cookies.
68
69 if (($page = $this->cJar_load($user, $pass))) {
70 // Account loaded from Cookie Storage
71 is_present($page, '>Free</a>', 'Account isn\'t premium');
72 return $this->PremiumDL();
73 }
74
75 $post = array();
76 $post['LoginForm%5Bemail%5D'] = urlencode($user);
77 $post['LoginForm%5Bpassword%5D'] = urlencode($pass);
78 $post['LoginForm%5BrememberMe%5D'] = '1';
79 if (!empty($_POST['step']) && $_POST['step'] == 'L') {
80 $_POST['step'] = false;
81 if (empty($_POST['captcha'])) html_error('You didn\'t enter the image verification code.');
82 $this->cookie = StrToCookies(decrypt(urldecode($_POST['cookie'])));
83 $post['LoginForm%5BverifyCode%5D'] = urlencode($_POST['captcha']);
84 }
85
86 // There are more of those redirects at login
87 $rdc = 0;
88 $page = false; // False value for starting the loop.
89 $redir = $this->baseUrl . 'auth/login';
90 $this->referer = !empty($GLOBALS['Referer']) && $GLOBALS['Referer'] != $this->link ? $GLOBALS['Referer'] : $this->baseUrl;
91 while (($redir = $this->ChkRGRedirs($page, $redir, '(?:/auth/login|/site/ChangeLocation/key/)')) && $rdc < 15) {
92 $page = cURL($redir, $this->cookie, $post, $this->referer);
93 $this->cookie = GetCookiesArr($page, $this->cookie);
94 $this->referer = $redir;
95 $rdc++;
96 }
97
98 is_present($page, 'Wrong e-mail or password.', 'Login Failed: Email/Password incorrect.');
99 is_present($page, 'E-mail is not a valid email address.', 'Login Failed: Login isn\'t an email address.');
100 is_present($page, 'Frequent logins.', 'Too frequent logins, please wait and try again.');
101 is_present($page, 'We discovered that you try to access your account from unusual location.', 'Login Failed: Login Blocked By IP, Check Account Email And Follow The Steps To Add IP to Whitelist.');
102 if (stripos($page, 'The code from a picture does not coincide') !== false) {
103 if (!empty($post['LoginForm%5BverifyCode%5D'])) html_error('Login Failed: Incorrect CAPTCHA response.');
104 if (!preg_match('@(https?://(?:[^\./\r\n\'\"\t\:]+\.)?rapidgator\.net(?:\:\d+)?)?/auth/captcha/\w+/\w+@i', $page, $imgurl)) html_error('Error: CAPTCHA not found.');
105 $imgurl = (empty($imgurl[1])) ? $this->baseUrl . substr($imgurl[0], 1) : $imgurl[0];
106 //Download captcha img.
107 $captcha = explode("\r\n\r\n", cURL($imgurl, $this->cookie), 2);
108 if (substr($captcha[0], 9, 3) != '200') html_error('Error downloading captcha img.');
109 $mimetype = (preg_match('@image/[\w+]+@', $captcha[0], $mimetype) ? $mimetype[0] : 'image/png');
110
111 $data = $this->DefaultParamArr($this->link, CookiesToStr($this->cookie), 1, 1);
112 $data['step'] = 'L';
113 $data['premium_acc'] = 'on'; // I should add 'premium_acc' to DefaultParamArr()
114 if ($pA) {
115 $data['pA_encrypted'] = 'true';
116 $data['premium_user'] = urlencode(encrypt($user)); // encrypt() will keep this safe.
117 $data['premium_pass'] = urlencode(encrypt($pass)); // And this too.
118 }
119 $this->EnterCaptcha("data:$mimetype;base64,".base64_encode($captcha[1]), $data, 5, 'Login');
120 exit;
121 }
122 //is_present($page, 'The code from a picture does not coincide', 'Login Failed: Captcha... (T8: I will add it later)');
123
124 if (empty($this->cookie['user__'])) html_error("Login Error: Cannot find 'user__' cookie.");
125 $this->cookie['lang'] = 'en';
126 $this->cJar_save();
127
128 $page = cURL($this->baseUrl, $this->cookie, 0, $this->baseUrl . 'auth/login');
129 is_present($page, '>Free</a>', 'Account isn\'t premium.');
130
131 return $this->PremiumDL();
132 }
133
134 private function ChkRGRedirs($page, $lasturl, $rgpath = '/') {
135 if (!is_array($lasturl)) $lasturl = parse_url($lasturl);
136 if ($page === false) return rebuild_url($lasturl);
137 $hpos = strpos($page, "\r\n\r\n");
138 $headers = empty($hpos) ? $page : substr($page, 0, $hpos);
139
140 if (stripos($headers, "\nLocation: ") === false && stripos($headers, "\nSet-Cookie: ") === false && stripos($headers, '<script') !== false && !(cut_str($page, '<title>', '</title>'))) {
141 if (empty($_GET['rgredir'])) {
142 if (!($body = cut_str($page, '<body>', '</body>'))) $body = $page;
143 if (stripos($body, '<script') !== strripos($body, '<script')) html_error('Unknown error while getting redirect code.');
144 $login = ($_REQUEST['premium_acc'] == 'on' && (!empty($_REQUEST['premium_user']) && !empty($_REQUEST['premium_pass'])));
145 $data = $this->DefaultParamArr($this->link, 0, rebuild_url($lasturl));
146 $data['rgredir'] = '';
147 $data['premium_acc'] = $_REQUEST['premium_acc']; // I should add 'premium_acc' to DefaultParamArr()
148 if ($login) {
149 $data['pA_encrypted'] = 'true';
150 $data['premium_user'] = urlencode(encrypt($_REQUEST['premium_user'])); // encrypt() will keep this safe.
151 $data['premium_pass'] = urlencode(encrypt($_REQUEST['premium_pass'])); // And this too.
152 }
153 if (!($js = cut_str($body, '<script language="JavaScript">', '</script>')) && !($js = cut_str($body, '<script type="text/javascript">', '</script>'))) html_error('Cannot get the redirect code.');
154 $js = str_ireplace(array('window.location.href','document.location.href'), 'document.getElementById("rgredir").value', $js);
155 if (strpos($js, 'document.body.onmousemove') !== false) { // New redirect code
156 $js = preg_replace('@^[\s\t]*\w+\([^\;]+;@i', '', $js);
157 $js = preg_replace('@document\.body\.onmousemove[\s\t]*=[\s\t]*function[\s\t]*\(\)[\s\t]*\{@i', '', $js);
158 $js = preg_replace('@document\.body\.onmousemove[\s\t]*=[\s\t]*\'\';?\};[\s\t]*window\.setTimeout\([\s\t]*((\"[^\"]+\")|(\'[^\']+\'))[^\;]+;[\s\t\r\n]*$@i', '', $js);
159 } elseif (($funcPos = stripos($js, 'function WriteA(')) !== false) { // JS + aaaaaaaaaaaaaaaaaaaaaaaaa
160 $links = array();
161 if (preg_match_all('@<a\s*[^>]*\shref="((?:https?://(?:www\.)?rapidgator\.net)?/[^\"]+)"[^>]*\sid="([A-Za-z][\w\.\-]*)"@i', $body, $a)) $links = array_merge($links, array_combine($a[2], $a[1]));
162 if (preg_match_all('@<a\s*[^>]*\sid="([A-Za-z][\w\.\-]*)"[^>]*\shref="((?:https?://(?:www\.)?rapidgator\.net)?/[^\"]+)"@i', $body, $a)) $links = array_merge($links, array_combine($a[1], $a[2]));
163 if (empty($links)) html_error('Cannot get the redirect fields');
164 unset($a);
165
166 $jsLinks = '';
167 foreach ($links as $key => $link) {
168 if (strpos($link, '://') === false) $link = (!empty($lasturl['scheme']) && strtolower($lasturl['scheme']) == 'https' ? 'https' : 'http').'://rapidgator.net' . $link;
169 $jsLinks .= "$key: '".addslashes($link)."', ";
170 }
171 unset($links, $key, $link);
172 $jsLinks = '{' . substr($jsLinks, 0, -2) . '}';
173 $func = substr($js, $funcPos);
174 if (!preg_match('@\.getElementById\(([\$_A-Za-z][\$\w]*)\)@i', $func, $linkVar)) html_error('Cannot edit redirect JS');
175 $linkVar = $linkVar[1];
176 unset($func);
177 $js = substr($js, 0, $funcPos)."\nvar T8RGLinks = $jsLinks;\nif ($linkVar in T8RGLinks) document.getElementById('rgredir').value = T8RGLinks[$linkVar];";
178 unset($jsLinks, $funcPos, $linkVar);
179 }
180 echo "\n<form name='rg_redir' action='{$_SERVER['SCRIPT_NAME']}' method='POST'><br />\n";
181 foreach ($data as $name => $input) echo "<input type='hidden' name='$name' id='$name' value='" . htmlspecialchars($input, ENT_QUOTES) . "' />\n";
182 echo "</form>\n<span id='T8_emsg' class='htmlerror' style='text-align:center;display:none;'></span>\n<noscript><span class='htmlerror'><b>Sorry, this code needs JavaScript enabled to work.</b></span></noscript>\n<script type='text/javascript'>/* <![CDATA[ Th3-822 */\n\tvar T8 = true;\n\ttry {{$js}\n\t} catch(e) {\n\t\t$('#T8_emsg').html('<b>Cannot decode challenge: ['+e.name+'] '+e.message+'</b>').show();\n\t\tT8 = false;\n\t}\n\tif (T8) window.setTimeout(\"$('form[name=rg_redir]').submit();\", 300); // 300 µs to make sure that the value was decoded and added.\n/* ]]> */</script>\n\n</body>\n</html>";
183 exit;
184 } else {
185 $_GET['rgredir'] = rawurldecode($_GET['rgredir']);
186 if (strpos($_GET['rgredir'], '://')) $_GET['rgredir'] = parse_url($_GET['rgredir'], PHP_URL_PATH);
187 if (empty($_GET['rgredir']) || substr($_GET['rgredir'], 0, 1) != '/') html_error('Invalid redirect value.');
188 $redir = (!empty($lasturl['scheme']) && strtolower($lasturl['scheme']) == 'https' ? 'https' : 'http') . '://rapidgator.net' . $_GET['rgredir'];
189 unset($_GET['rgredir']);
190 }
191 } elseif (preg_match('@Location: ((https?://(?:[^/\r\n]+\.)?rapidgator\.net)?'.$rgpath.'[^\r\n]*)@i', $headers, $redir)) $redir = (empty($redir[2])) ? (!empty($lasturl['scheme']) && strtolower($lasturl['scheme']) == 'https' ? 'https' : 'http') . '://rapidgator.net' . $redir[1] : $redir[1];
192
193 return (empty($redir) ? false : $redir);
194 }
195
196 private function cJar_encrypt($data, $key) {
197 if (empty($data)) return false;
198 global $secretkey;
199 $_secretkey = $secretkey;
200 $secretkey = $key;
201 $data = base64_encode(encrypt(json_encode($data)));
202 $secretkey = $_secretkey;
203 return $data;
204 }
205
206 private function cJar_decrypt($data, $key) {
207 if (empty($data)) return false;
208 global $secretkey;
209 $_secretkey = $secretkey;
210 $secretkey = $key;
211 $data = json_decode(decrypt(base64_decode($data)), true);
212 $secretkey = $_secretkey;
213 return (!empty($data) ? $data : false);
214 }
215
216 private function cJar_load($user, $pass) {
217 if (empty($user) || empty($pass)) return html_error('Login Failed: User or Password is empty.');
218
219 $user = strtolower($user);
220 $this->cJar['file'] = DOWNLOAD_DIR . get_class($this) . '_dl.php';
221 $this->cJar['hash'] = base64_encode(sha1("$user$pass", true));
222 $this->cJar['key'] = substr(base64_encode(hash('sha512', "$user$pass", true)), 0, 56);
223
224 if (file_exists($this->cJar['file']) && ($cFile = file($this->cJar['file'])) && is_array($cFile = unserialize($cFile[1])) && array_key_exists($this->cJar['hash'], $cFile) && ($testCookie = $this->cJar_decrypt($cFile[$this->cJar['hash']]['cookie'], $this->cJar['key']))) {
225 return $this->cJar_test($testCookie);
226 } else return false;
227 }
228
229 private function cJar_test($cookie) {
230 if (empty($this->cJar) || empty($cookie['user__'])) return false;
231 $oldCookie = $this->cookie;
232 $this->cookie = array_merge($cookie, array('lang' => 'en'));
233
234 // Weird RG redirects.
235 $rdc = 0;
236 $page = false; // False value for starting the loop.
237 $redir = $this->baseUrl;
238 $referer = $this->baseUrl; // Account is always shown as free if referer is from a file. (I'm not sure if this still happens)
239 while (($redir = $this->ChkRGRedirs($page, $redir)) && $rdc < 15) {
240 $page = cURL($redir, $this->cookie, 0, $referer);
241 $this->cookie = GetCookiesArr($page, $this->cookie);
242 $referer = $redir;
243 $rdc++;
244 }
245
246 if (!empty($this->cookie['user__']) && stripos($page, '/auth/logout') !== false)
247 {
248 $this->cookie['lang'] = 'en';
249 $this->cJar_save(); // Update last used time.
250 return $page;
251 }
252
253 $this->cookie = $oldCookie;
254 return false;
255 }
256
257 private function cJar_save() {
258 if (empty($this->cJar)) return;
259 $maxTime = 31 * 86400; // Max time to keep unused cookies saved (31 days)
260 if (file_exists($this->cJar['file']) && ($savedcookies = file($this->cJar['file'])) && is_array($savedcookies = unserialize($savedcookies[1]))) {
261 // Remove old cookies
262 foreach ($savedcookies as $k => $v) if (time() - $v['time'] >= $maxTime) unset($savedcookies[$k]);
263 } else $savedcookies = array();
264 $savedcookies[$this->cJar['hash']] = array('time' => time(), 'cookie' => $this->cJar_encrypt($this->cookie, $this->cJar['key']));
265
266 file_put_contents($this->cJar['file'], "<?php exit(); ?>\r\n" . serialize($savedcookies), LOCK_EX);
267 }
268}
269
270// [14-8-2012] Written by Th3-822.
271// [26-8-2012] Fixed regexp on redirect code. -Th3-822
272// [04-9-2012] Added error msg in free dl. -Th3-822
273// [09-9-2012] Fixed redirect issues, more code added & small edits. -Th3-822
274// [02-10-2012] Fixed for new weird redirect code. - Th3-822
275// [28-1-2013] Added Login captcha support. - Th3-822
276// [10-8-2013] Fixed redirects (again). - Th3-822
277// [25-11-2013] Fixed redirects function (aagain :D ). - Th3-822
278// [03-1-2014] Added support for rg.to domain. - Th3-822
279// [24-3-2014] Fixed FreeDL. - Th3-822
280// [20-5-2014] Fixed Login, bw error msg. - Th3-822
281// [16-12-2015][WIP] Fixing Blocks, Redirect Handling & Forcing Plugin To Use cURL. - Th3-822
282// [01-8-2016] Fixed FreeDL Captchas. - Th3-822
283// [28-8-2017] Switched to HTTPs (Untested) - Th3-822
284// [13-10-2019] Added Login Cookie Storage (login is rate-limited) & Removed FreeDL (as it now uses reCAPTCHA 2) - Th3-822
285// [17-2-2020] Fixed download regexp. - Th3-822