-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.sitemap.php
executable file
·242 lines (167 loc) · 5.76 KB
/
class.sitemap.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* @package linea21.plugins
* @subpackage sitemap
* @author Simon Georget <simon@linea21.com>
* @version $id SVN
* @access public
* @license http://opensource.org/licenses/MIT
*
* Inspired from
* http://stackoverflow.com/questions/3948947/can-i-use-wget-to-generate-a-sitemap-of-a-website-given-its-url
*/
class sitemap {
private $config = array();
private $locations = array();
private $log = '';
private $logtag = 'p';
public function __construct($url, $sitemap_path, $working_path = './', $sitemap_name = 'sitemap.xml', $priority = '0.500') {
try{
$this->config['url'] = $url;
$this->config['working_path'] = $working_path;
$this->config['sitemap_name'] = $sitemap_name;
$this->config['sitemap_url'] = $this->config['url'].$this->config['sitemap_name'];
$this->config['sitemap_path'] = $sitemap_path;
$this->config['wgetlog_file'] = $this->config['working_path'].'wgetlog.txt';
$this->config['sedlog_file'] = $this->config['working_path'].'sedlog.txt';
$this->config['priority'] = $priority;
// changing working path for exec() calls
chdir($this->config['working_path']);
$this->checkPermissions($this->config['sitemap_path']);
$this->checkPermissions($this->config['working_path']);
}
catch(Exception $e){
return $e->getMessage();
}
}
public function deleteSitemap() {
$r = unlink($this->config['sitemap_path'].$this->config['sitemap_name']);
if($r && !$quiet) $this->log("Deleting current sitemap : ".$url.$sitemap_name .".");
}
public function checkPermissions($folder) {
if(!is_writable($folder)) die('Error: '.$folder.' is not writable. Please check permissions on given folder');
}
public function exists() {
return file_exists($this->config['sitemap_path'].$this->config['sitemap_name']);
}
public function getVar($varname) {
return $this->config[$varname];
}
public function getConfig() {
return $this->config;
}
public function getCounter() {
return count($this->locations);
}
public function getLocations() {
return $this->locations;
}
public function log($str) {
$this->log .= "<".$this->logtag.">".$str."</".$this->logtag.">\n";
}
public function setLogTag($tag) {
$this->logtag = $tag;
}
public function getLog() {
return $this->log;
}
public function lastModified($format = '"m.d.y H:i') {
if($this->exists($this->config['sitemap_path'].$this->config['sitemap_name'])) {
return date($format, filemtime($this->config['sitemap_path'].$this->config['sitemap_name']));
} else {
return false;
}
}
/**
* generate()
* Run the full script to generate sitemap
*/
public function generate() {
$cmd = "wget --spider --recursive --no-verbose -P sitemapCrawler --output-file=". $this->config['wgetlog_file'] ." ". $this->config['url'];
exec($cmd);
$this->log("Wget() - Crawling website : ". $this->config['url'] .".");
$cmd = 'sed -n "s@.\+ URL:\([^ ]\+\) .\+@\1@p" wgetlog.txt | sed "s@&@\&@" > ' . $this->config['sedlog_file'];
exec($cmd);
$this->log("Sed() - Retrieving urls from : ". $this->config['url'] .".");
$this->setLocations();
$this->saveFile();
$this->clean();
return true;
}
/**
* setLocations()
* Convert sed log file to array
* and populate $this->locations attributes
*/
public function setLocations() {
// convert file to PHP array
$lines = file($this->config['sedlog_file'], FILE_IGNORE_NEW_LINES);
// we remove duplicates
foreach($lines as $line)
{
if(!in_array($line, $this->locations)) {
array_push($this->locations, $this->formatUrl($line));
$this->log("Adding entry : '".$this->formatUrl($line) ."'.");
} else {
$this->log("Removing duplicate entry : '".$this->formatUrl($line)."'.");
}
}
}
/**
* saveFile()
* generate xml sitemap file
*/
public function saveFile() {
// finally we create the sitemap file
if($fp = fopen($this->config['sitemap_path'].$this->config['sitemap_name'], "w+")) {
$out = '<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n";
foreach ($this->locations as $loc) {
$out .= "<url>\n\t<loc>".$loc."</loc>\n\t<priority>".$this->config['priority']."</priority>\n</url>\n";
}
$out .= '</urlset>';
fputs($fp, $out);
fclose($fp);
$this->log("Sitemap has been written to '".$this->config['sitemap_path'].$this->config['sitemap_name']."'.");
} else {
$this->log("Error: cannot save '".$this->config['sitemap_path'].$this->config['sitemap_name']."' file.");
}
}
/**
* clean()
* delete all temp files
*/
public function clean() {
unlink($this->config['wgetlog_file']);
$this->log("Deleting file : ".$this->config['wgetlog_file'].".");
unlink($this->config['sedlog_file']);
$this->log("Deleting file : ".$this->config['sedlog_file'].".");
$this->sureRemoveDir($this->config['working_path'].'sitemapCrawler', true);
$this->log("Deleting folder : ".$this->config['working_path'] .'sitemapCrawler'.".");
}
private function formatUrl($url) {
$url = str_replace('&', '&', $url); // necessary if some '&' are already '&' encoded
$url = str_replace('&', '&', $url);
return $url;
}
/**
* SureRemoveDir()
* Recursive delete directories
*/
private function sureRemoveDir($dir, $DeleteMe) {
if(!$dh = @opendir($dir)) return;
while (($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) $this->sureRemoveDir($dir.'/'.$obj, true);
}
if ($DeleteMe){
closedir($dh);
@rmdir($dir);
}
}
}
?>