-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.php
165 lines (145 loc) · 3.93 KB
/
jQuery.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
<?php
/**
* Converts an html document into a css searchable object
*
* @property DOMDocument $document DOMDocument object
* @property DOMXpath $xpath DOMXpath object
* @property array $nodes Array of jQueryNodes
* @property DOMNodeList $results DOMNodeList object
* @property string $last_xpath last generate xpath
* @property object $CI Codeigniter
*/
class jQuery {
public $document;
public $xpath;
public $nodes = array();
public $results;
public $last_xpath;
public $CI;
/**
* Build jQuery object
*
* @param mixed $source Can be a url, file or string of html
* @return jQuery
*/
public function __construct($source=false){
if($source){
$this->load($source);
}
return $this;
}
/**
* Simple GET request to target page
*
* @param string $url Target page to request
* @return jQuery
*/
public function get($url){
return $this->load($url);
}
/**
* Simple POST request to target page
*
* @param string $url Target page to request
* @param string $params Query string for post vars
* @return jQuery
*/
public function post($url, $params){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$results = curl_exec($ch);
curl_close($ch);
return $this->load($results);
}
public function load($source){
$this->document = new DOMDocument();
libxml_use_internal_errors(true);
# Is source is a url
if(filter_var($source, FILTER_VALIDATE_URL) !== FALSE){
$source = file_get_contents($source);
$this->document->recover = true;
$this->document->strictErrorChecking = false;
$this->document->loadHTML($source);
# Is source a file
}elseif(file_exists($source)){
$this->document->loadHTMLFile($source);
# Source must be pure html
}else{
$this->document->loadHTML($source);
}
$this->xpath = new DOMXpath($this->document);
return $this;
}
/**
* Converts a css string to xpath string
*
* @param string $css String of css selectors
* @return string
*/
public function css2xpath($css){
$xpath = $css;
# Attributes
$xpath = str_replace('[', '[@', $xpath);
# Nodes
$xpath = preg_replace('/\s{1}+(?![\.#\s])([^\.#\s]*)/', "/$1", $xpath);
# IDs
$xpath = preg_replace('/(?:^|\s)+#([^\s\/]*)/', '*[@id="$1"]', $xpath);
$xpath = preg_replace('/(?<!\s)#([^\s\/]*)/', '[@id="$1"]', $xpath);
# Classes
$xpath = preg_replace('/(?:^|\s+)\.([^\s\/]*)/', "*[contains(concat(' ', normalize-space(@class), ' '), ' $1 ')]", $xpath);
$xpath = preg_replace('/(?<!\s)\.([^\s\/]*)/', "[contains(concat(' ', normalize-space(@class), ' '), ' $1 ')]", $xpath);
# Final regexes
$xpath = 'descendant-or-self::'.$xpath;
$xpath = preg_replace('/(\/)/', '$1descendant::', $xpath);
$xpath = preg_replace('/(?<!:)(\*)/', '/$1', $xpath);
$this->last_xpath = $xpath;
return $this->last_xpath;
}
/**
* Searches the document for nodes matching css selector
*
* @param string $css String of css selectors
* @return jQuery
*/
public function find($css='html'){
$xpath = $this->css2xpath(trim($css));
$this->results = $this->xpath->query($xpath);
$this->node_array();
return $this;
}
/**
* Get the results of a find
*
* @return array
*/
public function result(){
return $this->nodes;
}
/**
* Get the first result of the set
*
* @return array
*/
public function one(){
return $this->nodes[0];
}
/**
* Builds jQueryNodes from result of a find
*
* @return jQuery
*/
private function node_array(){
$this->nodes = array();
if($this->results){
foreach($this->results as $node){
$this->nodes[] = new jQueryNode($node, $this->document);
}
}
return $this;
}
}
?>