-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgoogle_search.class.php
More file actions
316 lines (302 loc) · 10.3 KB
/
google_search.class.php
File metadata and controls
316 lines (302 loc) · 10.3 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* class for parsing google search results
* @license GNU LGPL Ver 3.0
* @package google-alias
* @author celend
* @date 14-10-27
*/
if(!defined('QUOTE'))
exit('Access Denied!');
class search{
private $content = '';
private $paras = array();
private $k; //content encrypt is enable
private $e; //content encrypt key
private $headers = array();
public $paras_m = array();
public $status = array();
public $results = array();
const url = 'https://www.google.com/search?';
/**
* construct function.
* @param string $keywork
*/
function __construct($keywork){
global $headers;
$this->k = opt('CON_ENC_K');
$this->e = opt('CON_ENC');
$this->headers =$headers;
$this->paras['q'] = $keywork;
$this->paras_m[opt('GET_Q')] = $keywork;
if(!isset($this->paras['num']))
$this->paras['num'] = opt('NUM');
if(opt('SAFE_SEARCH'))
$this->paras['safe'] = 'strict';
$this->paras['start'] = 0;
$this->paras['hl'] = opt('LANG');
}
/**
* prepare parameters to load html data.
* @return $this
*/
public function load(){
$paras = $this->arr2url($this->paras);
$ch = curl_init(search::url.$paras);
curl_setopt_array($ch, $this->headers);
$this->content = curl_exec($ch);
$this->status['errno'] = curl_errno($ch);
if($this->status['errno'])
return FALSE;
if(HAVE_GZIP && opt('ENABLE_GZIP'))
$this->content = zlib_decode($this->content);
$this->remove_css_and_js();
preg_match('`<div id="resultStats"[^>]*>[^\d]*([\d,]*)[^<]*<nobr>[^\d]*([\d\.]*)[^<]*</nobr></div>`m', $this->content, $r);
if(!isset($r[1]) || !isset($r[2])){
$this->status['errno'] = 404;
return FALSE;
}
else{
$this->status['res_num'] = $r[1];
$this->status['time'] = $r[2];
}
return $this;
}
/**
* remove the style and javascript tag from content.
* @return $this
*/
private function remove_css_and_js(){
$this->content = str_replace("\n", '', $this->content);
$this->content = preg_replace('`<script[^>]*>.*?</script>`', '', $this->content);
$this->content = preg_replace('`<style[^>]*>.*?</style>`', '', $this->content);
return $this;
}
/**
* arr convert into url.
* @param $arr
* @return string
*/
public static function arr2url(array $arr){
if(!is_array($arr))
return FALSE;
$s = '';
foreach($arr as $k => $v){
$s .= urlencode($k).'='.urlencode($v).'&';
}
$s = substr($s, 0, count($s) - 2);
return $s;
}
/**
* url convert into url.
* @param $str
* @return array
*/
public static function url2arr($str){
if(!is_string($str))
return FALSE;
parse_str($str, $f);
return $f;
}
/**
* set the results per page.
* @param int $num
* @return $this
*/
public function set_num($num){
$this->paras['num'] = $num;
$this->paras_m[opt('GET_NUM')] = $num;
return $this;
}
/**
* set the page number.
* @param int $p
* @return $this
*/
public function set_page($p){
$this->paras['start'] = ($p - 1) * $this->paras['num'];
$this->paras_m[opt('GET_PAGE')] = $p;
return $this;
}
/**
* set the search language use standard language code. see https://sites.google.com/site/tomihasa/google-language-codes
* @param string $lang
* @return $this
*/
public function set_lang($lang){
$this->paras_m[opt('GET_LANG')] = $lang;
$this->paras['hl'] = $lang;
$this->paras['lr'] = 'lang_'.$lang;
return $this;
}
/**
* set time before, e.g. h12 = before 12 hours, m30 = before 30 minutes.
* @param $str
* @return $this
*/
public function set_time($str){
if(!is_string($str))
return FALSE;
$str = strtolower($str);
if(!isset($this->paras['tbs'])){
$this->paras['tbs'] = '';
}
else
$this->paras['tbs'] .= ',';
switch($str){
//just now
case 's':
$this->paras['tbs'] .= 'qdr:s';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//few minutes ago
case 'n':
$this->paras['tbs'] .= 'qdr:n';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//half of hour ago
case 't':
$this->paras['tbs'] .= 'qdr:n30';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//half of day ago
case 'j':
$this->paras['tbs'] .= 'qdr:h12';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//a hour ago
case 'h':
$this->paras['tbs'] .= 'qdr:h';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//a day ago
case 'd':
$this->paras['tbs'] .= 'qdr:d';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//a weekend ago
case 'w':
$this->paras['tbs'] .= 'qdr:w';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//a month ago
case 'm':
$this->paras['tbs'] .= 'qdr:m';
$this->paras_m[opt('GET_TIME')] = $str;
break;
//a year ago
case 'y':
$this->paras['tbs'] .= 'qdr:y';
$this->paras_m[opt('GET_TIME')] = $str;
break;
default:
return False;
}
return $this;
}
/**
* parsing the html data to array.
* @return $this
*/
function get_results(){
if($this->status['errno'])
return FALSE;
$c = 0;
$s = array();
while(TRUE){
$s1 = stripos($this->content, '<div class="g"', $c);
$s2 = stripos($this->content, '<div class="g"', $s1 + 15);
if(!$s2){
$e = substr($this->content, $s1);
$s3 = strripos($e, '</div>') + 5;
$s[] = substr($this->content, $s1, $s3);
break;
}
$e = substr($this->content, $s1, $s2);
$s3 = strripos($e, '</div>') + 5;
$s[] = substr($this->content, $s1, $s3);
$c = $s2;
}
for($i = 0; $i < count($s); $i++){
$id_reg = '@<div[^>]+class="g"[^>]?(?:id="([^"]*)")?[^>]*>@s';
preg_match($id_reg, $s[$i], $r);
$id = isset($r[1]) ? $this->e ? encrypt($r[1], $this->k) : $r[1] : '';
$href_reg = '@<h3[^>]+class="r">.*?<a[^>]+href="([^"]*)"[^>]*>(.*?)</a>@s';
preg_match($href_reg, $s[$i], $r);
$href = isset($r[1]) ? $this->e ? encrypt($r[1], $this->k) : $r[1] : '';
$tle = isset($r[2]) ? $this->e ? encrypt($r[2], $this->k) : $r[2] : '';
$disc_reg = '@<span[^>]+class="st"[^>]*>((?:<span[^>]+class="f">.*?</span>)?.*?)</span>@s';
preg_match($disc_reg, $s[$i], $r);
$disc = isset($r[1]) ? $this->e ? encrypt($r[1], $this->k) : $r[1] : '';
$site_reg = '@<cite[^>]+class="_Rm[^"]*"[^>]*>(.*?)</cite>@s';
preg_match($site_reg, $s[$i], $r);
$site = isset($r[1]) ? $this->e ? encrypt($r[1], $this->k) : $r[1] : '';
$this->results[] = array('id' => $id, 'url' => $href, 'title' => $tle, 'info' => $disc, 'site' => $site);
}
//related searches
preg_match_all('@<p class="_e4b"><a[^>]+>(.*?)</a>@s', $this->content, $r, PREG_SET_ORDER);
if(count($r) > 0){
$this->results['related'] = array();
foreach($r as $v){
$this->results['related'][] = $v[1];
}
}
else{
$this->results['related'] = FALSE;
}
return $this;
}
/**
* return current page number;
* @return int
*/
public function get_page(){
return floor($this->paras['start'] / $this->paras['num']) + 1;
}
/**
* use by view class, this is url builder, not original url;
* @param array $paras
* @param string key
* @param string value
* @return string url
*/
public function get_url($paras = null, $key = null, $val = null){
$tmp = $this->paras_m;
if(is_array($paras)){
foreach($paras as $k => $v){
$tmp[$k] = $v;
}
}
elseif($key !== null && $val !== null){
$tmp[$key] = $val;
}
if(opt('ENCRYPT')){
$tmp[opt('GET_Q')] = encrypt($tmp[opt('GET_Q')], opt('ENCRYPT_K'));
}
return './?'.$this->arr2url($tmp);
}
public function get_key(){
return $this->paras['q'];
}
public function get_key_url($key){
$paras = array();
if(isset($this->paras_m[opt('GET_NUM')]))
$paras[opt('GET_NUM')] = $this->paras_m[opt('GET_NUM')];
if(isset($this->paras_m[opt('GET_LANG')]))
$paras[opt('GET_LANG')] = $this->paras_m[opt('GET_LANG')];
if(isset($this->paras_m[opt('GET_TIME')]))
$paras[opt('GET_TIME')] = $this->paras_m[opt('GET_TIME')];
$paras[opt('GET_Q')] = $this->e ? encrypt($key, opt('ENCRYPT_K')) : $key;
return './?'.$this->arr2url($paras);
}
public function get_commit_paras(){
$paras = array();
if(isset($this->paras_m[opt('GET_NUM')]))
$paras[opt('GET_NUM')] = $this->paras_m[opt('GET_NUM')];
if(isset($this->paras_m[opt('GET_LANG')]))
$paras[opt('GET_LANG')] = $this->paras_m[opt('GET_LANG')];
if(isset($this->paras_m[opt('GET_TIME')]))
$paras[opt('GET_TIME')] = $this->paras_m[opt('GET_TIME')];
return $paras;
}
};