-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
166 lines (162 loc) · 4.89 KB
/
index.php
File metadata and controls
166 lines (162 loc) · 4.89 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
<?php
session_start();
include_once('simple_html_dom.php');
$db = new PDO("sqlite:crawl.db");
$db->exec("CREATE TABLE Links (Id INTEGER PRIMARY KEY, url VARCHAR, original VARCHAR)");
function crawl($target_url, $mhave, $mnot, $rurl, $index=0){
if (strpos($target_url,'www.amazon.com') == 0 AND strpos($target_url,'http://') == 0) { // Add URL prefix if not exist
$target_url = 'http://' . $rurl . $target_url;
}else{
$target_url = $target_url;
}
$html = new simple_html_dom();
$html->load_file($target_url);
$array = array();
foreach($html->find('a') as $link)
{
if (empty($link->href) || empty($link)){
continue;
}
if (strpos($link->href,'www.amazon.com') == 0 AND strpos($link->href,'http://') == 0) { // Add Link Prefix if not exist
$url = 'http://' . $rurl . urldecode($link->href);
}else{
$url = $link->href;
}
$pattern2 = '/' . implode('|', array_map('preg_quote', $mhave)) . '/i';
if(preg_match($pattern2, $url) <> 1) {
continue;
}
$pattern = '/' . implode('|', array_map('preg_quote', $mnot)) . '/i';
if(preg_match($pattern, $link) > 0) {
continue;
}
$link_href = $link->href;
$array[$index]['url'] = $url;
$array[$index]['parent'] = $target_url;
$array[$index]['original'] = $link_href;
$index++;
echo '</font>';
}
return $array;
}
// Variables
$exclude = ['.mx', '.br', '.au', 'https', 'Media-Player', 'redirect', 'product-reviews', 'services.amazon', 'aws.amazon', '#', 'fresh.amazon', 'nav_a', 'onload=', 'void(0)', 'adobe.com', 'javascript', 'footer_logo', 'pd_pyml_rhf', 'gno_joinprmlogo', 'ref=gno_logo', 'Thread=', 'customer-media', 'ref=nav_logo', 'nav_joinprmlogo', 'access', 'ntpoffrw'];
$include = ['www.amazon.com'];
$root_url = 'www.amazon.com';
$count1 = 0;
if (!isset($index)){
$index = 0;
}
if(isset($_SESSION['data'])){
$data = $_SESSION['data'];
}
if(isset($_POST['plus'])){
next_index('links');
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['minus'])){
prev_index('links');
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['clear'])){
session_destroy();
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_SESSION['data'])){
$index = count($_SESSION['data']);
}
if (!isset($url)){
$url = 'http://www.amazon.com';
}
?>
<html lang = "en">
<head>
<meta charset = "utf-8">
</head>
<body><center>
<form method="post">
<table border="1" width="50%" bordercolor="blue">
<tr><td colspan=4>URL: <input type="text" size="75" name="url" value=
<?PHP
echo '"' . trim($url) . '"';
?>
></tr></td>
<tr>
<td align=center><?PHP if (isset($index)){echo 'Links in Session: ' . $index; } ?><td colspan=1 align=center></td><td align=center>Database</td>
</tr>
<tr>
<td align=center><input type="submit" name="run" value="Crawl URL" /><input type="submit" name="runrecursive" value="Crawl URLS in Array (Below)" /><input type="submit" name="clear" value="Clear Session" /><input type="submit" name="show" value="Show All in Session" /></td>
<td><input type="submit" name="save" value="Save" /></td>
</tr>
</table>
</form>
<?php
echo 'URL Crawled: ' . $url;
?>
</center>
<?PHP
$skipped = 0;
echo '<font size=1>';
if (isset($_POST['save'])){
$data = $_SESSION['data'];
foreach($data as $d){
$url = $d['url'];
$parent = $d['parent'];
$original = $d['original'];
$firstpart = substr($url,22, 1);
ctype_upper($firstpart);
if(ctype_upper($firstpart)){ // If the first letter is capitalized, it's mostly likely a product page.
$db->exec("INSERT INTO Links (url, original) VALUES ('$url', '$original')");
echo 'Row Inserted: ' . $original . '<br/>';
$count1 = $count1 + 1;
}else{
$skipped = $skipped + 1;
}
}
echo '<font color="green"><b>Rows Inserted: ' . $count1 . '</b></font><br/>';
echo '<font color="red"<<b>Skipped: ' . $skipped . '</b></font><br/>';
}
if (isset($_POST['run'])){
if(isset($_POST['url'])){
$url = trim($_POST['url']);
}
$array1 = crawl($url, $include, $exclude, $root_url, $index);
echo '<pre>';
print_r($array1);
echo '</pre>';
if (isset($data)){
$_SESSION['data'] = array_merge($data, $array1);
$data = $_SESSION['data'];
}else{
$_SESSION['data'] = $array1;
$data = $_SESSION['data'];
}
}
if (isset($_POST['runrecursive'])){
$data = $_SESSION['data'];
foreach($data as $d){ // Loop through each URL in browser session;
$d = $d['original'];
$array1 = crawl($d, $include, $exclude, $root_url, $index); // Crawl URL using function crawl() above;
echo '<pre>';
print_r($array1);
echo '</pre>';
if (isset($data)){
$_SESSION['data'] = array_merge($data, $array1);
$data = $_SESSION['data'];
}else{
$_SESSION['data'] = $array1;
$data = $_SESSION['data'];
}}}
echo '<br/>';
if(isset($_POST['show'])){
echo '<pre>';
echo '$_SESSION[data]';
$data = $_SESSION['data'];
var_export($data);
echo '</pre>';
}
echo "<br/>";
?>
</font>
</body>
</html>