This repository was archived by the owner on Jan 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
161 lines (149 loc) · 3.93 KB
/
Copy pathindex.php
File metadata and controls
161 lines (149 loc) · 3.93 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
<?php
require_once 'vendor/autoload.php';
use RedCat\ScrapFTS\Crawler;
use RedCat\DataMap\Bases;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Exception;
error_reporting(-1);
ini_set('display_startup_errors',true);
ini_set('display_errors','stdout');
$keys = [
'configdir',
'url',
'dbfile',
'substitution',
'subdomains',
'selectors',
];
foreach($keys as $key){
$$key = isset($_POST[$key])?$_POST[$key]:'';
}
if($configdir){
$json = file_get_contents($configdir.'/.scrapfts');
$json = json_decode($json,true);
if(!$json){
throw new Exception('invalid json format in '.$configdir.'/.scrapfts');
return;
}
foreach($keys as $key){
if($$key==''&&isset($json[$key])){
$$key = $json[$key];
}
}
}
$url2filename = trim(substr($url,6),'/');
if($url){
if(!$dbfile){
$dbfile = '.data/'.$url2filename;
}
if(substr($dbfile,-1)=='/'){
$dbfile .= $url2filename;
}
if(substr($dbfile,0,1)!='/'){
$dbfile = ($configdir?$configdir:getcwd()).'/'.$dbfile;
}
if(!in_array(strtolower(pathinfo($dbfile,PATHINFO_EXTENSION)),['db','sql','sqlite'])){
$dbfile .= '.sqlite';
}
if(!$selectors){
$selectors = 'body, title';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
label,input{
display:block;
float:left;
}
label{
width:200px;
}
input[type="text"]{
width:800px;
}
</style>
</head>
<body>
<form action="" method="POST">
<fieldset class="form-group">
<label for="configdir">Autoconf Dir .scrapfts</label>
<input id="configdir" name="configdir" type="text" value="<?php echo $configdir;?>">
</fieldset>
<fieldset class="form-group">
<label for="url">URL</label>
<input id="url" name="url" type="text" value="<?php echo $url;?>">
</fieldset>
<fieldset class="form-group">
<label for="subdomains">Enable Subdomains</label>
<input id="subdomains" name="subdomains" type="checkbox" value="1" <?php echo $subdomains?'checked="checked"':''?>>
</fieldset>
<fieldset class="form-group">
<label for="selectors">Selectors</label>
<input id="selectors" name="selectors" type="text" value="<?php echo $selectors;?>">
</fieldset>
<fieldset class="form-group">
<label for="substitution">Href Target Substitution</label>
<input id="substitution" name="substitution" type="text" value="<?php echo $substitution;?>">
</fieldset>
<fieldset class="form-group">
<label for="dbfile">DB File</label>
<input id="dbfile" name="dbfile" type="text" value="<?php echo $dbfile;?>">
</fieldset>
<fieldset class="form-group">
<input type="submit">
</fieldset>
</form>
</body>
</html>
<?php
if(!isset($_POST['url'])) return;
$crawler = new Crawler();
if($subdomains){
$crawler->enableSubdomains(true);
}
if($substitution){
$crawler->setDomainSubstitution($substitution);
}
$bases = new Bases();
$bases['scrapfts'] = ['dsn'=>'sqlite:'.$dbfile];
$db = $bases['scrapfts'];
//$db->debug();
$converter = new CssSelectorConverter();
ignore_user_abort(false);
set_time_limit(0);
if(php_sapi_name()!='cli'){
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate("D, d M Y H:i:s" ) . " GMT" );
header("Pragma: no-cache");
header("Cache-Control: no-cache");
header("Expires: -1");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: no-store, no-cache, must-revalidate");
ob_implicit_flush(true);
@ob_end_flush();
echo str_repeat(" ",1024);
}
echo "<pre>";
if($db->tableExists('page'))
$db->drop('page');
$crawler->setContentCallback(function($path,$content,$title)use($db){
echo $path."\n";
//echo $title."\n";
//echo str_replace("\n",'\n',$content)."\n\n";
//dd($content);
$db['page'][$path] = ['content_fulltext_'=>$content,'title'=>$title];
});
$crawler->enableSubdomains();
$selectorsArray = [];
foreach(explode(',',$selectors) as $selector){
$selectorsArray[] = $converter->toXPath(trim($selector));
}
touch($dbfile.'.working');
$crawler->scrap($url,$selectorsArray);
chmod($dbfile,0777);
unlink($dbfile.'.working');
echo "Done\n";
echo "</pre>";