| 1 |
<?php |
| 2 |
|
| 3 |
namespace simplehtmldom; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 6 |
/** |
| 7 |
* Website: http://sourceforge.net/projects/simplehtmldom/ |
| 8 |
* Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/) |
| 9 |
* |
| 10 |
* Licensed under The MIT License |
| 11 |
* See the LICENSE file in the project root for more information. |
| 12 |
* |
| 13 |
* Authors: |
| 14 |
* S.C. Chen |
| 15 |
* John Schlick |
| 16 |
* Rus Carroll |
| 17 |
* logmanoriginal |
| 18 |
* |
| 19 |
* Contributors: |
| 20 |
* Yousuke Kumakura |
| 21 |
* Vadim Voituk |
| 22 |
* Antcs |
| 23 |
* |
| 24 |
* Version Rev. 2.0-RC2 (415) |
| 25 |
*/ |
| 26 |
|
| 27 |
include_once 'HtmlDocument.php'; |
| 28 |
|
| 29 |
class HtmlWeb |
| 30 |
{ |
| 31 |
|
| 32 |
/** |
| 33 |
* @return HtmlDocument Returns the DOM for a webpage |
| 34 |
* @return null Returns null if the cURL extension is not loaded and allow_url_fopen=Off |
| 35 |
* @return null Returns null if the provided URL is invalid (not PHP_URL_SCHEME) |
| 36 |
* @return null Returns null if the provided URL does not specify the HTTP or HTTPS protocol |
| 37 |
*/ |
| 38 |
function load($url) |
| 39 |
{ |
| 40 |
if (!filter_var($url, FILTER_VALIDATE_URL)) { |
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
if ($scheme = parse_url($url, PHP_URL_SCHEME)) { |
| 45 |
switch (strtolower($scheme)) { |
| 46 |
case 'http': |
| 47 |
case 'https': |
| 48 |
break; |
| 49 |
default: |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
if (extension_loaded('curl')) { |
| 54 |
return $this->load_curl($url); |
| 55 |
} elseif (ini_get('allow_url_fopen')) { |
| 56 |
return $this->load_fopen($url); |
| 57 |
} else { |
| 58 |
//error_log(__FUNCTION__ . ' requires either the cURL extension or allow_url_fopen=On in php.ini'); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* cURL implementation of load |
| 67 |
*/ |
| 68 |
private function load_curl($url) |
| 69 |
{ |
| 70 |
$ch = curl_init(); |
| 71 |
|
| 72 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 73 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 74 |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 75 |
|
| 76 |
// There is no guarantee this request will be fulfilled |
| 77 |
// -- https://www.php.net/manual/en/function.curl-setopt.php |
| 78 |
curl_setopt($ch, CURLOPT_BUFFERSIZE, MAX_FILE_SIZE); |
| 79 |
|
| 80 |
// There is no guarantee this request will be fulfilled |
| 81 |
$header = array( |
| 82 |
'Accept: text/html', // Prefer HTML format |
| 83 |
'Accept-Charset: utf-8', // Prefer UTF-8 encoding |
| 84 |
); |
| 85 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
| 86 |
|
| 87 |
$doc = curl_exec($ch); |
| 88 |
|
| 89 |
if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) !== 200) { |
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
curl_close($ch); |
| 94 |
|
| 95 |
if (strlen($doc) > MAX_FILE_SIZE) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
|
| 99 |
return new HtmlDocument($doc); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* fopen implementation of load |
| 104 |
*/ |
| 105 |
private function load_fopen($url) |
| 106 |
{ |
| 107 |
// There is no guarantee this request will be fulfilled |
| 108 |
$context = stream_context_create(array('http' => array( |
| 109 |
'header' => array( |
| 110 |
'Accept: text/html', // Prefer HTML format |
| 111 |
'Accept-Charset: utf-8', // Prefer UTF-8 encoding |
| 112 |
), |
| 113 |
'ignore_errors' => true // Always fetch content |
| 114 |
))); |
| 115 |
|
| 116 |
$doc = file_get_contents($url, false, $context, 0, MAX_FILE_SIZE + 1); |
| 117 |
|
| 118 |
if (isset($http_response_header)) { |
| 119 |
foreach ($http_response_header as $rh) { |
| 120 |
// https://stackoverflow.com/a/1442526 |
| 121 |
$parts = explode(' ', $rh, 3); |
| 122 |
|
| 123 |
if (preg_match('/HTTP\/\d\.\d/', $parts[0])) { |
| 124 |
$code = $parts[1]; |
| 125 |
} |
| 126 |
} // Last code is final status |
| 127 |
|
| 128 |
if (!isset($code) || $code !== '200') { |
| 129 |
return null; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (strlen($doc) > MAX_FILE_SIZE) { |
| 134 |
return null; |
| 135 |
} |
| 136 |
|
| 137 |
return new HtmlDocument($doc); |
| 138 |
} |
| 139 |
} |
| 140 |
|