-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.php
195 lines (167 loc) · 5.96 KB
/
crawl.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
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
<?php
include_once("config/connection.php");
set_time_limit(2000);
$crawledUrls = [];
$resultsArray = [];
$maxDepth;
$parentUrl;
function search($url, $maxDep = 0) {
global $maxDepth,$parentUrl,$crawledUrls,$resultsArray;
$maxDepth = $maxDep;
$parentUrl=$url;
$crawledUrls = []; // Reset visited URLs for each search
$resultsArray = []; // Reset search results for each search
crawlUrl($parentUrl, 0);
// Store the results to JSON after processing each URL
$id=storeResultsToDB($parentUrl);
return $id;
}
function crawlUrl($url, $depth) {
global $maxDepth,$crawledUrls,$parentUrl;
$url=resolveUrl($parentUrl,$url);
if ($depth <= $maxDepth && !in_array($url, $crawledUrls) && robotCheck($url)) {
$htmlContent = fetchHtmlContent($url);
if ($htmlContent !== false) {
parseHtmlContent($url, $htmlContent,$depth);
$crawledUrls[] = $url; // Mark the URL as visited
}
}else{
return;
}
}
function fetchHtmlContent($url) {
$curl = curl_init($url);
// Set cURL options
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// Execute cURL session
$htmlContent = curl_exec($curl);
// test for errors during the cURL request
if (curl_errno($curl)) {
echo 'Error fetching URL: ' . curl_error($curl) . PHP_EOL;
$htmlContent = false;
}
// Close cURL session
curl_close($curl);
return $htmlContent;
}
function generateUrl($urlParts) {
$scheme = isset($urlParts['scheme']) ? $urlParts['scheme'] . '://' : '';
$host = isset($urlParts['host']) ? $urlParts['host'] : '';
$port = isset($urlParts['port']) ? ':' . $urlParts['port'] : '';
$path = isset($urlParts['path']) ? $urlParts['path'] : '';
return $scheme . $host . $port . $path;
}
function robotCheck($url) {
$robotsUrl = parse_url($url);
$robotsUrl['path'] = '/robots.txt';
$robotsTxtUrl = generateUrl($robotsUrl);
$robotsTxtContent = fetchHtmlContent($robotsTxtUrl);
// Implement logic to test if $robotsTxtContent allows crawling of $url
if ($robotsTxtContent !== false) {
// Split the robots.txt content into lines
$lines = explode("\n", $robotsTxtContent);
// Loop through each line in the robots.txt file
foreach ($lines as $line) {
// Remove leading and trailing whitespaces
$line = trim($line);
// test for comments and ignore them
if (empty($line) || $line[0] === '#') {
continue;
}
// test for Disallow directive if the user agent is applicable
if (strpos($line, 'Disallow:') !== false) {
// Extract the disallowed path
$disallowedPath = trim(str_ireplace('Disallow:', '', $line));
// test if the URL matches the disallowed path
if (isset($robotsUrl['path']) && $robotsUrl['path'] == $disallowedPath) {
return false; // URL is disallowed by robots.txt
}
}
}
}
return true; // URL is allowed by robots.txt
}
function parseHtmlContent($baseUrl,$htmlContent,$depth) {
global $resultsArray;
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($htmlContent);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$query = "//a | //p | //h1 | //h2 | //h3 | //h4 | //h5 | //h6 | //li";
$results = $xpath->query($query);
if ($results->length > 0) {
$urlResults = ['url' => $baseUrl, 'data' => []];
foreach ($results as $result) {
if ($result->nodeName === 'a') {
$href=$result->getAttribute("href");
//crawl each url recursively till the given max depth
crawlUrl($href,$depth+1);
} else {
$content = $result->nodeValue;
$urlResults['data'][] = $content;
}
}
$resultsArray[] = $urlResults;
}
}
function storeResultsToDB($parentUrl) {
global $resultsArray, $conn;
if(!empty($resultsArray)){
$jsonData = json_encode($resultsArray, JSON_PRETTY_PRINT);
// Using a prepared statement to avoid SQL injection
$sql = "INSERT IGNORE INTO data (content, url) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $jsonData, $parentUrl);
// Execute the statement
if ($stmt->execute()) {
$lastInsertedID = $stmt->insert_id;
$stmt->close();
header("location: keywordSearch.php?id=$lastInsertedID");
} else {
$stmt->close();
return false;
}
}
}
function resolveUrl($baseUrl, $url) {
// test if the URL is already absolute
if (filter_var($url, FILTER_VALIDATE_URL)) {
return $url; // If it's absolute, return as it is
}
// Use PHP's built-in function to resolve relative URLs
$absoluteUrl = rtrim($baseUrl, '/') . '/' . ltrim($url, '/');
return $absoluteUrl;
}
if(isset($_POST['url']) && isset($_POST['depth']) && !empty($_POST['url'])){
global $conn;
$url=rtrim($_POST['url'],'/');
$depth=trim($_POST['depth']);
$sql = "SELECT id FROM data WHERE url = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $url);
// Execute the statement
if ($stmt->execute()) {
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$row =($result->fetch_assoc())['id'];
$stmt->close();
header("location: keywordSearch.php?id=$row");
}
else{
$searchResultId=search($url,$depth);
if($searchResultId){
header("location: keywordSearch.php?id=$res");
}else{
echo "Couldn't crawl the provided site, goto <a href='index.html'>homepage</a>";
}
}
}else{
header("location: index.html");
}
}
else{
header("location: index.html");
}
?>