-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvToRedirect.php
More file actions
82 lines (69 loc) · 2.04 KB
/
csvToRedirect.php
File metadata and controls
82 lines (69 loc) · 2.04 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
<?php
class csvToRedirect
{
private $_data;
private $_sorted;
private $_output;
private $_target;
public function __construct($target)
{
$this->_target = $target;
}
protected function parseCsv()
{
if ($file = fopen($this->_target, 'r')) {
while (!feof($file)) {
$row = fgetcsv($file);
$this->_data[] = [$row[0], $row[1]];
}
}
}
protected function cleanRedirects()
{
if (is_null($this->_data)) {
throw new Exception('Nothing to clean!');
}
$find = ['%\?prev=[0-9]+?&%', '%\?next=[0-9]+?&%', '%&next[0-9]+=(.*)+%', '%&prev[0-9]+=(.*)+%'];
$replace = ['?', '?', '', ''];
foreach ($this->_data as $i => $v) {
$this->_data[$i] = [preg_replace($find, $replace, $v[0]), $v[1]];
}
return true;
}
protected function sortRedirects()
{
if (is_null($this->_data)) {
throw new Exception('Nothing to sort!');
}
foreach ($this->_data as $v) {
if (preg_match('%^/product.cfm(.*)?%', $v[0])) {
$this->_sorted['product'][] = [$v[0], $v[1]];
}
}
return true;
}
protected function buildOutput()
{
foreach($this->_sorted['imc-product'] as $v){
$vars = preg_replace(['%^/product.cfm\?%'], '', $v[0]);
$this->_output .= 'RewriteCond %{REQUEST_URI} ^/product.cfm$'."\r\n";
$this->_output .= 'RewriteCond %{QUERY_STRING} ^'.$vars."$\r\n";
$this->_output .= 'RewriteRule ^.*$ ' . $v[1] . '? [L,R=301]' . "\r\n";
}
}
protected function saveOutput()
{
$handle = fopen('output.txt', 'w');
fwrite($handle, $this->_output);
}
public function run()
{
$this->parseCsv();
$this->cleanRedirects();
$this->sortRedirects();
$this->buildOutput();
$this->saveOutput();
}
}
$run = new csvToRedirect('redirectstoparse.csv');
$run->run();