2018-02-26 06:05:25 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file webpagetool.php
|
2018-04-13 06:25:33 +00:00
|
|
|
* @date 2018-04-13
|
2018-02-26 06:05:25 +00:00
|
|
|
* @author Go Namhyeon <gnh1201@gmail.com>
|
|
|
|
* @brief WebPageTool helper
|
|
|
|
*/
|
|
|
|
|
2018-04-10 11:26:54 +00:00
|
|
|
if(!function_exists("get_web_legacy")) {
|
|
|
|
function get_web_legacy($url) {
|
|
|
|
return (ini_get("allow_url_fopen") ? file_get_contents($url) : false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 12:59:00 +00:00
|
|
|
if(!function_exists("get_web_build_qs")) {
|
|
|
|
function get_web_build_qs($url, $data) {
|
|
|
|
$pos = strpos($url, '?');
|
|
|
|
if ($pos === false) {
|
|
|
|
$url = $url . '?' . http_build_query($data);
|
|
|
|
} else {
|
|
|
|
$url = $url . '&' . http_build_query($data);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 11:26:54 +00:00
|
|
|
if(!function_exists("get_web_cmd")) {
|
|
|
|
function get_web_cmd($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
|
|
|
$output = "";
|
|
|
|
$cmd_fin = "";
|
|
|
|
$cmd = "";
|
|
|
|
|
|
|
|
if($method == "get") {
|
2018-05-12 13:52:00 +00:00
|
|
|
$cmd = "curl -A '%s' -k '%s'";
|
|
|
|
$cmd_fin = sprintf($cmd, addslashes($ua), addslashes(get_web_build_qs($url, $data)));
|
2018-04-10 11:26:54 +00:00
|
|
|
$output = shell_exec($cmd_fin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($method == "post") {
|
2018-05-12 13:52:00 +00:00
|
|
|
$cmd = "curl -X POST -A '%s' -k '%s' %s";
|
2018-04-10 11:26:54 +00:00
|
|
|
$params_cmd = "";
|
|
|
|
foreach($data as $k=>$v) {
|
2018-05-12 13:52:00 +00:00
|
|
|
if(substr($v, 0, 1) == "@") { // if file
|
|
|
|
$params_cmd .= sprintf("-F '%s=%s' ", addslashes($k), addslashes($v));
|
|
|
|
} else {
|
|
|
|
$params_cmd .= sprintf("-d '%s=%s' ", addslashes($k), addslashes($v));
|
|
|
|
}
|
2018-04-10 11:26:54 +00:00
|
|
|
}
|
2018-04-10 11:29:32 +00:00
|
|
|
$cmd_fin = sprintf($cmd, addslashes($ua), addslashes($url), $params_cmd);
|
2018-04-10 11:26:54 +00:00
|
|
|
$output = shell_exec($cmd_fin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 06:10:06 +00:00
|
|
|
if(!function_exists("get_web_page")) {
|
2018-02-26 06:05:25 +00:00
|
|
|
function get_web_page($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
2018-04-10 11:26:54 +00:00
|
|
|
$status = "-1";
|
|
|
|
$resno = "-1";
|
|
|
|
$errno = "-1";
|
|
|
|
|
2018-05-12 13:52:00 +00:00
|
|
|
if($method = "post.cmd" || $method == "get.cmd") {
|
2018-04-10 11:26:54 +00:00
|
|
|
$content = get_web_cmd($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
|
2018-05-12 13:52:00 +00:00
|
|
|
echo $content;
|
2018-04-10 11:26:54 +00:00
|
|
|
} else {
|
2018-05-12 13:52:00 +00:00
|
|
|
if(!in_array("curl", get_loaded_extensions())) {
|
|
|
|
return "cURL extension needs to be installed.";
|
|
|
|
}
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
CURLOPT_URL => $url, // set url
|
|
|
|
CURLOPT_PROXY => $proxy, // set proxy server
|
|
|
|
CURLOPT_RETURNTRANSFER => true, // return web page
|
|
|
|
CURLOPT_HEADER => false, // don't return headers
|
|
|
|
CURLOPT_FOLLOWLOCATION => true, // follow redirects
|
|
|
|
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
|
|
|
|
CURLOPT_ENCODING => "", // handle compressed
|
|
|
|
CURLOPT_USERAGENT => $ua, // name of client
|
|
|
|
CURLOPT_AUTOREFERER => true, // set referrer on redirect
|
|
|
|
CURLOPT_CONNECTTIMEOUT => $ct_out, // time-out on connect
|
|
|
|
CURLOPT_TIMEOUT => $t_out, // time-out on response
|
|
|
|
CURLOPT_FAILONERROR => true, // get error code
|
|
|
|
CURLOPT_SSL_VERIFYHOST => false, // ignore ssl host verification
|
|
|
|
CURLOPT_SSL_VERIFYPEER => false, // ignore ssl peer verification
|
|
|
|
);
|
|
|
|
|
|
|
|
if(empty($options[CURLOPT_USERAGENT])) {
|
|
|
|
$ua = "2018 ReasonableFramework;https://github.com/gnh1201/reasonableframework";
|
|
|
|
$options[CURLOPT_USERAGENT] = $ua;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($method == "post" && count($data) > 0) {
|
|
|
|
$options[CURLOPT_POST] = 1;
|
|
|
|
$options[CURLOPT_POSTFIELDS] = $data;
|
|
|
|
}
|
2018-04-10 11:26:54 +00:00
|
|
|
|
2018-05-12 13:52:00 +00:00
|
|
|
if($method == "get" && count($data) > 0) {
|
|
|
|
$options[CURLOPT_URL] = get_web_build_qs($url, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt_array($ch, $options);
|
|
|
|
|
|
|
|
$content = curl_exec($ch);
|
|
|
|
if(!is_string($content)) {
|
|
|
|
$res_method = $method . ".cmd";
|
|
|
|
$res = get_web_page($url, $res_method, $data, $proxy, $ua, $ct_out, $t_out);
|
|
|
|
$content = $res['content'];
|
|
|
|
} else {
|
|
|
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
$resno = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
|
|
|
$errno = curl_errno($ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_close($ch);
|
|
|
|
}
|
2018-02-26 06:05:25 +00:00
|
|
|
|
|
|
|
$content_size = strlen($content);
|
|
|
|
|
|
|
|
$response = array(
|
|
|
|
"content" => $content,
|
2018-04-10 11:26:54 +00:00
|
|
|
"size" => $content_size,
|
|
|
|
"status" => $status,
|
|
|
|
"resno" => $resno,
|
|
|
|
"errno" => $errno,
|
2018-02-26 06:05:25 +00:00
|
|
|
);
|
2018-03-10 17:07:27 +00:00
|
|
|
|
2018-02-26 06:05:25 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 06:10:06 +00:00
|
|
|
if(!function_exists("get_web_json")) {
|
2018-02-26 06:05:25 +00:00
|
|
|
function get_web_json($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = array();
|
2018-03-10 17:06:56 +00:00
|
|
|
|
|
|
|
$response = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
|
|
|
|
if($response['size'] > 0) {
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = json_decode($response['content']);
|
2018-02-26 06:06:47 +00:00
|
|
|
}
|
2018-02-26 06:05:25 +00:00
|
|
|
|
2018-04-13 06:25:33 +00:00
|
|
|
return $result;
|
2018-02-26 06:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-10 16:59:53 +00:00
|
|
|
|
|
|
|
if(!function_exists("get_web_dom")) {
|
2018-03-10 17:03:10 +00:00
|
|
|
function get_web_dom($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = new stdClass();
|
2018-03-10 17:06:56 +00:00
|
|
|
$response = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
|
2018-03-10 16:59:53 +00:00
|
|
|
|
|
|
|
// load simple_html_dom
|
2018-03-10 17:06:56 +00:00
|
|
|
if($response['size'] > 0) {
|
2018-03-10 17:06:05 +00:00
|
|
|
loadHelper("simple_html_dom");
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = function_exists("str_get_html") ? str_get_html($response['content']) : $result;
|
2018-03-10 16:59:53 +00:00
|
|
|
}
|
2018-03-10 17:00:12 +00:00
|
|
|
|
2018-04-13 06:25:33 +00:00
|
|
|
return $result;
|
2018-03-10 16:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 02:05:48 +00:00
|
|
|
|
|
|
|
if(!function_exists("get_web_meta")) {
|
|
|
|
function get_web_meta($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = array();
|
2018-03-21 02:05:48 +00:00
|
|
|
$response = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
|
|
|
|
|
|
|
|
// load PHP-Metaparser
|
|
|
|
if($response['size'] > 0) {
|
|
|
|
loadHelper("metaparser.lnk");
|
|
|
|
$parser = new MetaParser($response['content'], $url);
|
2018-04-13 06:25:33 +00:00
|
|
|
$result = $parser->getDetails();
|
2018-03-21 02:05:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 06:25:33 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!function_exists("get_web_xml")) {
|
|
|
|
function get_web_xml($url, $method="get", $data=array(), $proxy="", $ua="", $ct_out=45, $t_out=45) {
|
|
|
|
$result = new stdClass();
|
|
|
|
|
|
|
|
if(function_exists("simplexml_load_string")) {
|
|
|
|
$response = get_web_page($url, $method, $data, $proxy, $ua, $ct_out, $t_out);
|
|
|
|
|
|
|
|
if($response['size'] > 0) {
|
2018-04-13 07:06:35 +00:00
|
|
|
$result = simplexml_load_string($response['content'], null, LIBXML_NOCDATA);
|
2018-04-13 06:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2018-03-21 02:05:48 +00:00
|
|
|
}
|
|
|
|
}
|