forked from allu77/TVScraper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DDUBrowser.php
132 lines (103 loc) · 3.54 KB
/
DDUBrowser.php
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
<?php
#define('DDU_LOGINURL', 'http://dduniverse.net/ita/ucp.php?mode=login');
define('DDU_LOGINURL', 'http://ddunlimited.net/ucp.php?mode=login');
define('COOKIES_DDUBASENAME', 'DDUBrowser_');
require_once('SimpleBrowser.php');
class DDUBrowser extends SimpleBrowser {
protected $username;
protected $password;
protected $lastLoginLink;
public function setLogin($user, $pass) {
$this->username = $user;
$this->password = $pass;
$this->setCookiesFile(COOKIES_DDUBASENAME . md5($user) .'.txt');
$this->log("Cookies file set to " . $this->cookiesFile);
}
private function getXpath($page) {
$dom = new DOMDocument();
if (!@$dom->loadHTML($page)) {
trigger_error('Cannot load HTML page', E_USER_ERROR);
}
$xpath = new DOMXPath($dom);
if (! $xpath) {
trigger_error('Cannot create XPATH handler', E_USER_ERROR);
}
return $xpath;
}
private function needsLogin($xpath) {
/* $loginForm = $xpath->evaluate('/html/body//form[@id="login"]');
if ($loginForm->length > 0) {
return true;
} else {
$registerLink = $xpath->evaluate('/html/body//a[contains(text(), "link visibili solo agli utenti registrati")]');
if ($registerLink->length > 0) {
return true;
}
}*/
$loginLink = $xpath->evaluate('/html/body//div[@id="page-header"]//a[@title="Login"]');
if ($loginLink->length > 0) {
$this->lastLoginLink = $loginLink->item(0)->getAttribute('href');
return true;
}
return false;
}
private function getLoginParams($xpath) {
//TODO: Handle no xpath, handle dynamic form target
$inputElements = $xpath->evaluate('/html/body//form[@id="login"]//input');
$params = Array();
$params['username'] = $this->username;
$params['password'] = $this->password;
for ($i = 0; $i < $inputElements->length; $i++) {
$input = $inputElements->item($i);
$this->log('Found INPUT name=' . $input->getAttribute('name') . ' type=' . $input->getAttribute('type'));
if ($input->getAttribute('type') == 'hidden' || $input->getAttribute('type') == 'submit') {
$this->log("INPUT is added");
$params[$input->getAttribute('name')] = $input->getAttribute('value');
}
}
return $params;
}
private function login($xpath) {
$loginPage = parent::get(DDU_LOGINURL, false);
$loginXpath = $this->getXpath($loginPage);
$params = $this->getLoginParams($loginXpath);
$page = $this->post(DDU_LOGINURL, $params);
if (! file_exists($this->cookiesFile)) {
$this->log("Cookies file " . $this->cookiesFile . " does not exists. Can't log in.", LOGGER_ERROR);
return false;
}
$this->log("User " . md5($this->username) . " login", LOGGER_INFO);
$newXpath = $this->getXpath($page);
return !$this->needsLogin($newXpath);
}
protected function getCacheFileNameOnly ($url) {
// Per User Cache
return md5($this->username) . '_' . parent::getCacheFileNameOnly($url);
}
public function get($url, $perGetUseCache = true) {
$page = parent::get($url, $perGetUseCache);
$xpath = $this->getXpath($page);
if (! $this->needsLogin($xpath)) {
$this->log("Page doesn't need re-authentication");
return $page;
} else {
$this->log("Page needs re-authentication");
$this->resetCacheForURL($url);
if (!$this->login($xpath)) {
$this->log("Couldn't log in", LOGGER_ERROR);
return false;
} else {
$page = parent::get($url);
$xpath = $this->getXpath($page);
if (! $this->needsLogin($xpath)) {
return $page;
} else {
$this->resetCacheForURL($url);
$this->log("Unauthenticated page received after login. Severe error", LOGGER_ERROR);
return false;
}
}
}
}
}
?>