-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cleaner.php
164 lines (142 loc) · 4.4 KB
/
Cleaner.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
<?php
class Cleaner
{
public $contentCleaned = false;
public $contentLinkReport = array();
public $internalDomains = array('example.com');
public $excludeSubdomains = array('media');
/* Set up objects for validation and modification */
public function __construct(){
$this->getObjects('Validate');
$this->getObjects('Modify');
}
/*
* Clean content
* @param $content -- the content to be cleaned
*/
public function clean($content){
$htmlParser = new App_Util_SimpleHtmlDomParser();
$htmlParser->load($content);
$links = $htmlParser->find('a');
$this->contentLinkReport = array();
$this->contentCleaned = false;
foreach($links as $link){
$url = $link->href;
if($this->isInternalUrl($url)){
$urlReport = $this->validate($link);
$urlReport = array_merge($urlReport, $this->modify($link));
$urlReport['is_internal'] = true;
$this->contentLinkReport[] = $urlReport;
if(!$urlReport['is_valid']){
$this->contentCleaned = true;
//Fix URL
$link->href = $urlReport['correct_url'];
}
if(count($urlReport['params']) > 0){
foreach($urlReport['params'] as $k => $v){
$link->{$k} = $v;
}
}
}else{
$this->contentLinkReport[] = array('url'=>$url,'is_internal'=>false);
}
}
$correctedContent = $htmlParser->save();
$htmlParser->clear();
return $correctedContent;
}
/*
* Determine if the link is an internal link or an outside link
* @param $href -- the link to check
*
*/
public function isInternalUrl($href){
$urlParts = parse_url($href);
if(isset($urlParts['host'])){
return in_array($urlParts['host'], $this->internalDomains);
} elseif(isset($urlParts['path']) && strpos($urlParts['path'],'.com') === false){
return true;
} else {
return false;
}
}
/**
* Validate a given URL
* @param $url -- The URL to validate
* @return $report --- A report of the URL - array(is_valid => true/false, errors=> a string of errors, correct_url => the correct url)
*/
public function validate($link){
$report['is_valid'] = true;
$report['errors'] = array();
$report['correct_url'] = '';
$report['url'] = $link->href;
foreach($this->validateObjs as $validationObj){
$validator = new $validationObj($link);
if(!$validator->isValid()){
$report['is_valid'] = false;
$report['errors'][] = $validator->getError();
$url = $link->href = $validator->fix();
echo "{$validationObj}: {$url}\n";
} else {
$url = $link->href;
}
}
$report['correct_url'] = $url;
return $report;
}
/**
* Modify a given link
* @param $link - simplehtmldom object
* @return $report - a report on the link with new params
*/
public function modify($link){
$report = array('params' => array());
foreach($this->modifyObjs as $modificationObj){
$modifier = new $modificationObj($link);
if($modifier->requiresModification()){
$report['params'] = $report['params'] + $modifier->modify();
}
}
return $report;
}
/**
* Set up objects to manipulate a url
* @return void
*/
public function getObjects($type){
$files = array();
$iterator = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.$type);
$property = strtolower($type).'Objs';
foreach($iterator as $file){
if ($file->isDot() || $file->isDir()) {
continue;
}
$filename = $file->getFilename();
if ('Abstract.php' == $filename) {
continue;
}
if (!preg_match('/^([a-z_]+)\\.php$/i', $filename, $matches)) {
continue;
}
$files[] = 'App_Util_Url_' . $type . '_' . $matches[1];
}
$this->{$property} = $files;
}
public function printLinkReport(){
foreach($this->contentLinkReport as $link){
if($link['is_internal']){
if($link['is_valid']){
echo "<span style='color:green'>".$link['url']."</span>";
}else{
echo "<span style='color:red'>".$link['url']."</span> <span>INVALID URL!".implode(',',$link['errors'])." FIXED: ".$link['correct_url']."</span>";
}
if(count($link['params']) > 0){
echo 'Modified: '.var_export($link['params'], true);
}
}else{
echo "<span style='color:blue'>".$link['url']."</span>";
}
echo "<br/>";
}
}
}