PluginProbe
YeeMail — Email Template Builder & Customizer / 2.2.1
YeeMail — Email Template Builder & Customizer v2.2.1
3.0.1 2.2.1 3.0.0 2.2.0 trunk 2.0.0 2.0.3 2.0.5 2.0.8 2.1.6 2.1.7 2.1.8
yeemail / libs / HtmlWeb.php

HtmlWeb.php in YeeMail — Email Template Builder & Customizer 2.2.1, at libs/HtmlWeb.php

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