Version Description
- G+ platform bug, typo: iub_cs_activate_inline vs _iubcs_activate-inline
Download this release
Release Info
Developer | Facens |
Plugin | iubenda Cookie Solution for GDPR |
Version | 1.9.3 |
Comparing to | |
See all releases |
Version 1.9.3
- iubenda_cookie_solution.php +269 -0
- readme.txt +139 -0
- simple_html_dom.php +1738 -0
iubenda_cookie_solution.php
ADDED
@@ -0,0 +1,269 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/*
|
3 |
+
Plugin Name: Iubenda Cookie Solution
|
4 |
+
Plugin URI: https://www.iubenda.com
|
5 |
+
Description: Iubenda Cookie Solution permette di gestire tutti gli aspetti della cookie law su WP.
|
6 |
+
Author: Duccio Catalioto
|
7 |
+
Version: 1.9.3
|
8 |
+
Author URI: https://www.iubenda.com
|
9 |
+
*/
|
10 |
+
|
11 |
+
if(!function_exists('file_get_html')){
|
12 |
+
include_once 'simple_html_dom.php';
|
13 |
+
}
|
14 |
+
|
15 |
+
DEFINE('VOICE_MENU', 'Iubenda Cookie Solution');
|
16 |
+
DEFINE('URL_MENU', str_replace(' ', '_', VOICE_MENU));
|
17 |
+
DEFINE('IUB_REGEX_PATTERN', '/<!--IUB_COOKIE_POLICY_START-->(.*)<!--IUB_COOKIE_POLICY_END-->/sU');
|
18 |
+
|
19 |
+
|
20 |
+
/***************************************
|
21 |
+
*
|
22 |
+
* Add Iubenda JS script to the header
|
23 |
+
*
|
24 |
+
****************************************/
|
25 |
+
|
26 |
+
function strpos_array($haystack, $needle){
|
27 |
+
if(is_array($needle)){
|
28 |
+
foreach($needle as $need){
|
29 |
+
if(strpos($haystack, $need) !== false){
|
30 |
+
return true;
|
31 |
+
}
|
32 |
+
}
|
33 |
+
}else{
|
34 |
+
if(strpos($haystack, $need) !== false) {
|
35 |
+
return true;
|
36 |
+
}
|
37 |
+
}
|
38 |
+
return false;
|
39 |
+
}
|
40 |
+
|
41 |
+
|
42 |
+
function consentGiven(){
|
43 |
+
foreach($_COOKIE as $key => $value){
|
44 |
+
if(strpos_array($key, array('_iub_cs-s', '_iub_cs'))){
|
45 |
+
return true;
|
46 |
+
}
|
47 |
+
}
|
48 |
+
}
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
function iub_header(){
|
53 |
+
ob_start();
|
54 |
+
$iub_code = get_option('iub_code');
|
55 |
+
echo html_entity_decode(stripslashes($iub_code));
|
56 |
+
}
|
57 |
+
|
58 |
+
add_action('wp_head', 'iub_header', 99);
|
59 |
+
|
60 |
+
/***********************************
|
61 |
+
*
|
62 |
+
* Encode all the javascript/html content fetched from this comments:
|
63 |
+
*
|
64 |
+
* <!--IUB_COOKIE_POLICY_START-->
|
65 |
+
* <script>..</script>
|
66 |
+
* <!--IUB_COOKIE_POLICY_END-->
|
67 |
+
*
|
68 |
+
* AND
|
69 |
+
*
|
70 |
+
* [iub-cookie-solution]
|
71 |
+
*
|
72 |
+
* code
|
73 |
+
*
|
74 |
+
* [/iub-cookie-solution]
|
75 |
+
*
|
76 |
+
************************************/
|
77 |
+
|
78 |
+
function create_tags($html){
|
79 |
+
|
80 |
+
$elements = $html->find("*");
|
81 |
+
$js = '';
|
82 |
+
|
83 |
+
foreach($elements as $e){
|
84 |
+
|
85 |
+
switch($e->tag){
|
86 |
+
case 'script':
|
87 |
+
$s = $e->innertext;
|
88 |
+
$js.= '<script type="text/plain" class="_iub_cs_activate">'.$s.'</script>';
|
89 |
+
break;
|
90 |
+
|
91 |
+
default:
|
92 |
+
$js.= '<noscript class="_no_script_iub">';
|
93 |
+
$js.= $e->outertext;
|
94 |
+
$js.= '</noscript>';
|
95 |
+
break;
|
96 |
+
}
|
97 |
+
|
98 |
+
}
|
99 |
+
|
100 |
+
return $js;
|
101 |
+
}
|
102 |
+
|
103 |
+
|
104 |
+
function __shutdown(){
|
105 |
+
$final = '';
|
106 |
+
|
107 |
+
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting
|
108 |
+
// that buffer's output into the final output.
|
109 |
+
$levels = count(ob_get_level());
|
110 |
+
|
111 |
+
for ($i = 0; $i < $levels; $i++){
|
112 |
+
$final .= ob_get_clean();
|
113 |
+
}
|
114 |
+
|
115 |
+
// Apply any filters to the final output
|
116 |
+
echo apply_filters('final_output', $final);
|
117 |
+
}
|
118 |
+
|
119 |
+
add_action('shutdown', '__shutdown', 0);
|
120 |
+
|
121 |
+
|
122 |
+
function __final_output($output){
|
123 |
+
|
124 |
+
$auto_script_tags = array(
|
125 |
+
'platform.twitter.com/widgets.js',
|
126 |
+
'apis.google.com/js/plusone.js',
|
127 |
+
'apis.google.com/js/platform.js',
|
128 |
+
'connect.facebook.net',
|
129 |
+
'www.youtube.com/iframe_api'
|
130 |
+
);
|
131 |
+
|
132 |
+
$auto_iframe_tags = array(
|
133 |
+
'youtube.com',
|
134 |
+
'platform.twitter.com',
|
135 |
+
'www.facebook.com/plugins/like.php',
|
136 |
+
'apis.google.com'
|
137 |
+
);
|
138 |
+
|
139 |
+
|
140 |
+
if(consentGiven()){
|
141 |
+
return $output;
|
142 |
+
}
|
143 |
+
|
144 |
+
/* Replace all the comments with js/html encoded code */
|
145 |
+
preg_match_all(IUB_REGEX_PATTERN, $output, $scripts);
|
146 |
+
if(is_array($scripts[1])){
|
147 |
+
$count = count($scripts[1]);
|
148 |
+
$js_scripts = array();
|
149 |
+
for($j=0; $j<$count; $j++){
|
150 |
+
$html = str_get_html($scripts[1][$j], $lowercase=true, $forceTagsClosed=true, $stripRN=false);
|
151 |
+
$js_scripts[] = create_tags($html);
|
152 |
+
}
|
153 |
+
|
154 |
+
if(is_array($scripts[1]) && is_array($js_scripts)){
|
155 |
+
if(count($scripts[1]) >= 1 && count($js_scripts) >= 1){
|
156 |
+
$output = strtr($output, array_combine($scripts[1], $js_scripts));
|
157 |
+
}
|
158 |
+
}
|
159 |
+
}
|
160 |
+
|
161 |
+
$html = str_get_html($output, $lowercase=true, $forceTagsClosed=true, $stripRN=false);
|
162 |
+
|
163 |
+
/* Auto match script and replace */
|
164 |
+
$scripts = $html->find("script");
|
165 |
+
if(is_array($scripts)){
|
166 |
+
$count = count($scripts);
|
167 |
+
for($j=0; $j<$count; $j++){
|
168 |
+
$s = $scripts[$j];
|
169 |
+
if (strpos_array($s->innertext, $auto_script_tags) !== false) {
|
170 |
+
$class = $s->class;
|
171 |
+
$s->class = $class . ' _iub_cs_activate';
|
172 |
+
$s->type = 'text/plain';
|
173 |
+
}else{
|
174 |
+
$src = $s->src;
|
175 |
+
if (strpos_array($src, $auto_script_tags) !== false) {
|
176 |
+
$class = $s->class;
|
177 |
+
$s->class = $class . ' _iub_cs_activate-inline';
|
178 |
+
$s->type = 'text/plain';
|
179 |
+
}
|
180 |
+
}
|
181 |
+
}
|
182 |
+
}
|
183 |
+
|
184 |
+
/* Auto match iframe and replace */
|
185 |
+
$iframes = $html->find("iframe");
|
186 |
+
if(is_array($iframes)){
|
187 |
+
$count = count($iframes);
|
188 |
+
for($j=0; $j<$count; $j++){
|
189 |
+
$i = $iframes[$j];
|
190 |
+
$src = $i->src;
|
191 |
+
if (strpos_array($src, $auto_iframe_tags) !== false){
|
192 |
+
$new_src = "data:text/html;base64,PGh0bWw+PGJvZHk+U3VwcHJlc3NlZDwvYm9keT48L2h0bWw+";
|
193 |
+
$class = $i->class;
|
194 |
+
$i->suppressedsrc = $src;
|
195 |
+
$i->src = $new_src;
|
196 |
+
$i->class = $class . ' _iub_cs_activate';
|
197 |
+
}
|
198 |
+
}
|
199 |
+
}
|
200 |
+
|
201 |
+
return $html;
|
202 |
+
|
203 |
+
}
|
204 |
+
|
205 |
+
add_filter('final_output', '__final_output', $output);
|
206 |
+
|
207 |
+
|
208 |
+
function iub_func($atts, $content = "") {
|
209 |
+
/* Shortcode function */
|
210 |
+
$html = str_get_html($content, $lowercase=true, $forceTagsClosed=true, $stripRN=false);
|
211 |
+
return create_tags($html);
|
212 |
+
}
|
213 |
+
|
214 |
+
|
215 |
+
add_shortcode('iub-cookie-policy', 'iub_func');
|
216 |
+
|
217 |
+
|
218 |
+
/***********************************
|
219 |
+
*
|
220 |
+
* Add menù item on the admin
|
221 |
+
*
|
222 |
+
************************************/
|
223 |
+
|
224 |
+
|
225 |
+
function iub_admin_actions() {
|
226 |
+
add_options_page(VOICE_MENU, VOICE_MENU, 1, URL_MENU, 'iub_admin');
|
227 |
+
}
|
228 |
+
|
229 |
+
|
230 |
+
function iub_admin(){
|
231 |
+
|
232 |
+
/* Handling POST DATA and FETCHING from DB */
|
233 |
+
if($_POST['iub_update_form'] == 1) {
|
234 |
+
|
235 |
+
$iub_code = htmlentities($_POST['iub_code']);
|
236 |
+
update_option('iub_code', $iub_code);
|
237 |
+
echo '<div class="updated"><p><strong>Opzioni salvate</strong></p></div>';
|
238 |
+
} else {
|
239 |
+
$iub_code = get_option('iub_code');
|
240 |
+
}
|
241 |
+
|
242 |
+
echo '
|
243 |
+
<div class="wrap">
|
244 |
+
<h2> iubenda Cookie Policy Solution</h2>
|
245 |
+
<form name="iub_form" method="post" action="'.str_replace( '%7E', '~', $_SERVER['REQUEST_URI']).'">
|
246 |
+
<h4>Iubenda settings</h4>
|
247 |
+
<p>
|
248 |
+
Codice iubenda<br>
|
249 |
+
<textarea name="iub_code" cols="44" rows="13">'.stripslashes($iub_code).'</textarea>
|
250 |
+
</p>
|
251 |
+
|
252 |
+
<p class="submit">
|
253 |
+
<input type="hidden" name="iub_update_form" value="1">
|
254 |
+
<input type="submit" name="Submit" value="Update">
|
255 |
+
</p>
|
256 |
+
</form>
|
257 |
+
<p>
|
258 |
+
Per informazioni ed istruzioni su questo plugin, visita questa guida:<br>
|
259 |
+
<a href="https://www.iubenda.com/it/help/posts/810">https://www.iubenda.com/it/help/posts/810</a>
|
260 |
+
|
261 |
+
</p>
|
262 |
+
|
263 |
+
</div>';
|
264 |
+
}
|
265 |
+
|
266 |
+
|
267 |
+
add_action('admin_menu', 'iub_admin_actions');
|
268 |
+
|
269 |
+
?>
|
readme.txt
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
=== Plugin Name ===
|
2 |
+
Contributors: iubenda
|
3 |
+
Donate link:
|
4 |
+
Tags: cookies, cookie law, cookie policy, cookie banner, cookie block, privacy policy, cookie consent
|
5 |
+
Requires at least: 3.0.1
|
6 |
+
Tested up to: 4.4.2
|
7 |
+
Stable tag: 1.0
|
8 |
+
License: GPLv2 or later
|
9 |
+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
+
|
11 |
+
Iubenda cookie solution plugin for WordPress for the creation of cookie banners and consent based cookie blocks. The plugin works with the Iubenda Cookie Law Solution and allows WordPress users to block the most common widgets to comply with the Cookie Law, particularly designed for Italy.
|
12 |
+
|
13 |
+
== Description ==
|
14 |
+
|
15 |
+
This plugin is described in English, browse down to the end of the readme to find an Italian version.
|
16 |
+
|
17 |
+
# Functionality
|
18 |
+
|
19 |
+
- The plugin automatically inserts the Iubenda code in the head of every page of the site
|
20 |
+
- It automatically blocks scripts by YouTube, Facebook, G + and Twitter (other automatic blocks on the way!)
|
21 |
+
- It allows to manually block all the remaining resources, without the need of direct intervention on the code
|
22 |
+
- It handles the display of cookie banners and cookie policy, saving user preferences about the use of cookies
|
23 |
+
|
24 |
+
== Installation ==
|
25 |
+
|
26 |
+
- **Manual installation**: download the plugin, go to the WordPress admin panel of your site and click on Plugins > Add New. At the top, then click the "upload plugin" button and then choose the ZIP file from your computer you just downloaded. Finally, click on "Install Now" and activate the plugin;
|
27 |
+
- **Installation via WP.org**: search in your WordPress plugin admin panel for "Iubenda Cookie Law Solution for Wordpress", install it;
|
28 |
+
- Once the plugin is installed and activated, go to the Admin Panel (Settings > Iubenda Cookie Solution) where you will be asked to paste the code into tht field that gets generated from your Iubenda account dashboard when you activate the cookie law kit for your privacy policy. For more information on how to activate the cookie law kit, see this article: https://www.iubenda.com/it/help/posts/680
|
29 |
+
- At this point the plugin will begin to show the banner on which displays your cookie policy (link) to users who visit the site for the first time. No need for other configurations.
|
30 |
+
- Furthermore, the plugin automatically recognizes and blocks cookies that get installed via the YouTube video player and social widgets - such as the Facebook Like Box - on your site.
|
31 |
+
- The other scripts that install cookies for which the automatic block isn't yet available can and should be "wrapped" using these comments:
|
32 |
+
|
33 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
34 |
+
<!--IUB_COOKIE_POLICY_END-->`
|
35 |
+
|
36 |
+
== Changelog ==
|
37 |
+
|
38 |
+
= 1.9.3 =
|
39 |
+
* G+ platform bug, typo: _iub_cs_activate_inline vs _iub_cs_activate-inline
|
40 |
+
|
41 |
+
= 1.9.2 =
|
42 |
+
* G+ platform bug
|
43 |
+
|
44 |
+
= 1.9.1 =
|
45 |
+
* Minor improvements
|
46 |
+
|
47 |
+
= 1.9 =
|
48 |
+
* Improved parsing without regex
|
49 |
+
* No parsing if the user has already given the consent
|
50 |
+
|
51 |
+
= 1.0 =
|
52 |
+
* First plugin version.
|
53 |
+
|
54 |
+
== Usage ==
|
55 |
+
|
56 |
+
How does this plugin work with a Facebook button for example?
|
57 |
+
|
58 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
59 |
+
<script>
|
60 |
+
(function(d, s, id) {
|
61 |
+
var js, fjs = d.getElementsByTagName(s)[0];
|
62 |
+
if (d.getElementById(id)) return;
|
63 |
+
js = d.createElement(s); js.id = id;
|
64 |
+
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3&appId=808061959224601";
|
65 |
+
fjs.parentNode.insertBefore(js, fjs);
|
66 |
+
}(document, 'script', 'facebook-jssdk'));
|
67 |
+
</script>
|
68 |
+
<!--IUB_COOKIE_POLICY_END-->`
|
69 |
+
|
70 |
+
If there are HTML / IMG / IFRAME elements, you need to proceed in this way:
|
71 |
+
|
72 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
73 |
+
<iframe src="...
|
74 |
+
<img src="...
|
75 |
+
<!--IUB_COOKIE_POLICY_END-->`
|
76 |
+
|
77 |
+
For articles, however, there's a shortcode available:
|
78 |
+
|
79 |
+
`[iub-cookie-policy]
|
80 |
+
[/iub-cookie-policy]`
|
81 |
+
|
82 |
+
In case of continued browsing, the preferences of your users for the use of cookies will be set on "OK" to clear the banner and unlock the cookies. Moreover, banners and the blocking codes will not be delivered to subsequent visits by users who have already given their consent (and such preference will be updated at each subsequent visit for the future).
|
83 |
+
|
84 |
+
== Further notes ==
|
85 |
+
|
86 |
+
At the moment the automatic blocking of the YouTube video player and social widgets are done only if these scripts are located after the function wp_head(). The solution is the first version. Test it and contact us to report any errors.
|
87 |
+
|
88 |
+
== Italian explanations ==
|
89 |
+
|
90 |
+
- Il plugin inserisce in modo automatico il codice di iubenda nell’head di tutte le pagine del sito
|
91 |
+
- Blocca in automatico i codici di YouTube, Facebook, G+ e Twitter (altri blocchi automatici in arrivo!)
|
92 |
+
- Permette di bloccare manualmente tutte le risorse restanti, senza la necessità di interventi diretti sul codice
|
93 |
+
- Gestisce la visualizzazione del banner e della cookie policy, ed il salvataggio delle preferenze degli utenti circa l’installazione dei cookie
|
94 |
+
|
95 |
+
# Istruzioni
|
96 |
+
|
97 |
+
- **Installazione manuale**: Scarica il plugin, accedi al pannello di amministrazione WordPress del tuo sito e clicca su Plugin > Aggiungi nuovo. In alto, clicca quindi sul pulsante Carica plugin e poi scegli dal tuo computer il file ZIP appena scaricato. Clicca infine su Installa adesso e attiva il plugin.
|
98 |
+
- **Installazione automatica**: installa dal repository WordPres.org cercando "Iubenda Cookie Law Solution for Wordpress"
|
99 |
+
- Una volta installato ed attivato il plugin, accedi al pannello Admin (Impostazioni > Iubenda Cookie Solution) dove ti verrà chiesto di incollare in un campo il codice che iubenda genera quando attivi il kit cookie law sulla tua privacy policy. Per ulteriori informazioni su come attivare il kit cookie law, consulta questo articolo: https://www.iubenda.com/it/help/posts/680
|
100 |
+
- A questo punto il plugin inizierà a mostrare il banner in cui è richiamata la tua cookie policy agli utenti che visitano il sito per la prima volta senza la necessità di altre configurazioni.
|
101 |
+
- In più, il plugin riconoscerà e bloccherà in automatico i cookie installati da tutti i video player di YouTube e dai widget sociali – come il Facebook Like Box – presenti sul tuo sito.
|
102 |
+
- Gli altri script per i quali non è ancora disponibile il blocco automatico – e che installano cookie che richiedono il blocco prima del consenso – vanno “avvolti” utilizzando questi commenti:
|
103 |
+
|
104 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
105 |
+
<!--IUB_COOKIE_POLICY_END-->`
|
106 |
+
|
107 |
+
Per esempio in un plugin Facebook dovrai fare come segue:
|
108 |
+
|
109 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
110 |
+
<script>
|
111 |
+
(function(d, s, id) {
|
112 |
+
var js, fjs = d.getElementsByTagName(s)[0];
|
113 |
+
if (d.getElementById(id)) return;
|
114 |
+
js = d.createElement(s); js.id = id;
|
115 |
+
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3&appId=808061959224601";
|
116 |
+
fjs.parentNode.insertBefore(js, fjs);
|
117 |
+
}(document, 'script', 'facebook-jssdk'));
|
118 |
+
</script>
|
119 |
+
<!--IUB_COOKIE_POLICY_END-—>`
|
120 |
+
|
121 |
+
Se invece ci sono parti di HTML / IMG / IFRAME, bisogna procedere in questo modo:
|
122 |
+
|
123 |
+
`<!--IUB_COOKIE_POLICY_START-->
|
124 |
+
<iframe src="...
|
125 |
+
<img src="...
|
126 |
+
<!--IUB_COOKIE_POLICY_END-->`
|
127 |
+
|
128 |
+
|
129 |
+
Per gli articoli, invece, è disponibile uno shortcode:
|
130 |
+
|
131 |
+
`[iub-cookie-policy]
|
132 |
+
[/iub-cookie-policy]`
|
133 |
+
|
134 |
+
|
135 |
+
In caso di proseguimento della navigazione, le preferenze dei tuoi utenti circa l’installazione dei cookie verranno settate sul si in modo da far scomparire il banner e da sbloccare i cookie. Inoltre, banner e blocco codici non verranno erogati alle successive visite da parte degli utenti che hanno già prestato il proprio consenso (e tale preferenza verrà aggiornata ad ogni successiva visita).
|
136 |
+
|
137 |
+
**Note ulteriori:**
|
138 |
+
|
139 |
+
Al momento il blocco automatico dei video player di YouTube e dei widget sociali avviene solo se questi script si trovano dopo la funzione wp_head(). La soluzione è alla prima versione. Testatela e contattateci per segnalare eventuali errori.
|
simple_html_dom.php
ADDED
@@ -0,0 +1,1738 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Website: http://sourceforge.net/projects/simplehtmldom/
|
4 |
+
* Additional projects that may be used: http://sourceforge.net/projects/debugobject/
|
5 |
+
* Acknowledge: Jose Solorzano (https://sourceforge.net/projects/php-html/)
|
6 |
+
* Contributions by:
|
7 |
+
* Yousuke Kumakura (Attribute filters)
|
8 |
+
* Vadim Voituk (Negative indexes supports of "find" method)
|
9 |
+
* Antcs (Constructor with automatically load contents either text or file/url)
|
10 |
+
*
|
11 |
+
* all affected sections have comments starting with "PaperG"
|
12 |
+
*
|
13 |
+
* Paperg - Added case insensitive testing of the value of the selector.
|
14 |
+
* Paperg - Added tag_start for the starting index of tags - NOTE: This works but not accurately.
|
15 |
+
* This tag_start gets counted AFTER \r\n have been crushed out, and after the remove_noice calls so it will not reflect the REAL position of the tag in the source,
|
16 |
+
* it will almost always be smaller by some amount.
|
17 |
+
* We use this to determine how far into the file the tag in question is. This "percentage will never be accurate as the $dom->size is the "real" number of bytes the dom was created from.
|
18 |
+
* but for most purposes, it's a really good estimation.
|
19 |
+
* Paperg - Added the forceTagsClosed to the dom constructor. Forcing tags closed is great for malformed html, but it CAN lead to parsing errors.
|
20 |
+
* Allow the user to tell us how much they trust the html.
|
21 |
+
* Paperg add the text and plaintext to the selectors for the find syntax. plaintext implies text in the innertext of a node. text implies that the tag is a text node.
|
22 |
+
* This allows for us to find tags based on the text they contain.
|
23 |
+
* Create find_ancestor_tag to see if a tag is - at any level - inside of another specific tag.
|
24 |
+
* Paperg: added parse_charset so that we know about the character set of the source document.
|
25 |
+
* NOTE: If the user's system has a routine called get_last_retrieve_url_contents_content_type availalbe, we will assume it's returning the content-type header from the
|
26 |
+
* last transfer or curl_exec, and we will parse that and use it in preference to any other method of charset detection.
|
27 |
+
*
|
28 |
+
* Found infinite loop in the case of broken html in restore_noise. Rewrote to protect from that.
|
29 |
+
* PaperG (John Schlick) Added get_display_size for "IMG" tags.
|
30 |
+
*
|
31 |
+
* Licensed under The MIT License
|
32 |
+
* Redistributions of files must retain the above copyright notice.
|
33 |
+
*
|
34 |
+
* @author S.C. Chen <me578022@gmail.com>
|
35 |
+
* @author John Schlick
|
36 |
+
* @author Rus Carroll
|
37 |
+
* @version 1.5 ($Rev: 210 $)
|
38 |
+
* @package PlaceLocalInclude
|
39 |
+
* @subpackage simple_html_dom
|
40 |
+
*/
|
41 |
+
|
42 |
+
/**
|
43 |
+
* All of the Defines for the classes below.
|
44 |
+
* @author S.C. Chen <me578022@gmail.com>
|
45 |
+
*/
|
46 |
+
define('HDOM_TYPE_ELEMENT', 1);
|
47 |
+
define('HDOM_TYPE_COMMENT', 2);
|
48 |
+
define('HDOM_TYPE_TEXT', 3);
|
49 |
+
define('HDOM_TYPE_ENDTAG', 4);
|
50 |
+
define('HDOM_TYPE_ROOT', 5);
|
51 |
+
define('HDOM_TYPE_UNKNOWN', 6);
|
52 |
+
define('HDOM_QUOTE_DOUBLE', 0);
|
53 |
+
define('HDOM_QUOTE_SINGLE', 1);
|
54 |
+
define('HDOM_QUOTE_NO', 3);
|
55 |
+
define('HDOM_INFO_BEGIN', 0);
|
56 |
+
define('HDOM_INFO_END', 1);
|
57 |
+
define('HDOM_INFO_QUOTE', 2);
|
58 |
+
define('HDOM_INFO_SPACE', 3);
|
59 |
+
define('HDOM_INFO_TEXT', 4);
|
60 |
+
define('HDOM_INFO_INNER', 5);
|
61 |
+
define('HDOM_INFO_OUTER', 6);
|
62 |
+
define('HDOM_INFO_ENDSPACE',7);
|
63 |
+
// helper functions
|
64 |
+
// -----------------------------------------------------------------------------
|
65 |
+
// get html dom from file
|
66 |
+
// $maxlen is defined in the code as PHP_STREAM_COPY_ALL which is defined as -1.
|
67 |
+
function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $stripRN=true)
|
68 |
+
{
|
69 |
+
// We DO force the tags to be terminated.
|
70 |
+
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $stripRN, $defaultBRText, $defaultSpanText);
|
71 |
+
// For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
|
72 |
+
$contents = file_get_contents($url, $use_include_path, $context, $offset);
|
73 |
+
// Paperg - use our own mechanism for getting the contents as we want to control the timeout.
|
74 |
+
//$contents = retrieve_url_contents($url);
|
75 |
+
if (empty($contents) || strlen($contents) > 60000)
|
76 |
+
{
|
77 |
+
return false;
|
78 |
+
}
|
79 |
+
// The second parameter can force the selectors to all be lowercase.
|
80 |
+
$dom->load($contents, $lowercase, $stripRN);
|
81 |
+
return $dom;
|
82 |
+
}
|
83 |
+
|
84 |
+
// get html dom from string
|
85 |
+
function str_get_html($str, $lowercase=true, $forceTagsClosed=true, $stripRN=true)
|
86 |
+
{
|
87 |
+
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $stripRN);
|
88 |
+
if (empty($str) || strlen($str) > 60000)
|
89 |
+
{
|
90 |
+
$dom->clear();
|
91 |
+
return false;
|
92 |
+
}
|
93 |
+
$dom->load($str, $lowercase, $stripRN);
|
94 |
+
return $dom;
|
95 |
+
}
|
96 |
+
|
97 |
+
// dump html dom tree
|
98 |
+
function dump_html_tree($node, $show_attr=true, $deep=0)
|
99 |
+
{
|
100 |
+
$node->dump($node);
|
101 |
+
}
|
102 |
+
|
103 |
+
|
104 |
+
/**
|
105 |
+
* simple html dom node
|
106 |
+
* PaperG - added ability for "find" routine to lowercase the value of the selector.
|
107 |
+
* PaperG - added $tag_start to track the start position of the tag in the total byte index
|
108 |
+
*
|
109 |
+
* @package PlaceLocalInclude
|
110 |
+
*/
|
111 |
+
class simple_html_dom_node
|
112 |
+
{
|
113 |
+
public $nodetype = HDOM_TYPE_TEXT;
|
114 |
+
public $tag = 'text';
|
115 |
+
public $attr = array();
|
116 |
+
public $children = array();
|
117 |
+
public $nodes = array();
|
118 |
+
public $parent = null;
|
119 |
+
// The "info" array - see HDOM_INFO_... for what each element contains.
|
120 |
+
public $_ = array();
|
121 |
+
public $tag_start = 0;
|
122 |
+
private $dom = null;
|
123 |
+
|
124 |
+
function __construct($dom)
|
125 |
+
{
|
126 |
+
$this->dom = $dom;
|
127 |
+
$dom->nodes[] = $this;
|
128 |
+
}
|
129 |
+
|
130 |
+
function __destruct()
|
131 |
+
{
|
132 |
+
$this->clear();
|
133 |
+
}
|
134 |
+
|
135 |
+
function __toString()
|
136 |
+
{
|
137 |
+
return $this->outertext();
|
138 |
+
}
|
139 |
+
|
140 |
+
// clean up memory due to php5 circular references memory leak...
|
141 |
+
function clear()
|
142 |
+
{
|
143 |
+
$this->dom = null;
|
144 |
+
$this->nodes = null;
|
145 |
+
$this->parent = null;
|
146 |
+
$this->children = null;
|
147 |
+
}
|
148 |
+
|
149 |
+
// dump node's tree
|
150 |
+
function dump($show_attr=true, $deep=0)
|
151 |
+
{
|
152 |
+
$lead = str_repeat(' ', $deep);
|
153 |
+
|
154 |
+
echo $lead.$this->tag;
|
155 |
+
if ($show_attr && count($this->attr)>0)
|
156 |
+
{
|
157 |
+
echo '(';
|
158 |
+
foreach ($this->attr as $k=>$v)
|
159 |
+
echo "[$k]=>\"".$this->$k.'", ';
|
160 |
+
echo ')';
|
161 |
+
}
|
162 |
+
echo "\n";
|
163 |
+
|
164 |
+
if ($this->nodes)
|
165 |
+
{
|
166 |
+
foreach ($this->nodes as $c)
|
167 |
+
{
|
168 |
+
$c->dump($show_attr, $deep+1);
|
169 |
+
}
|
170 |
+
}
|
171 |
+
}
|
172 |
+
|
173 |
+
|
174 |
+
// Debugging function to dump a single dom node with a bunch of information about it.
|
175 |
+
function dump_node($echo=true)
|
176 |
+
{
|
177 |
+
|
178 |
+
$string = $this->tag;
|
179 |
+
if (count($this->attr)>0)
|
180 |
+
{
|
181 |
+
$string .= '(';
|
182 |
+
foreach ($this->attr as $k=>$v)
|
183 |
+
{
|
184 |
+
$string .= "[$k]=>\"".$this->$k.'", ';
|
185 |
+
}
|
186 |
+
$string .= ')';
|
187 |
+
}
|
188 |
+
if (count($this->_)>0)
|
189 |
+
{
|
190 |
+
$string .= ' $_ (';
|
191 |
+
foreach ($this->_ as $k=>$v)
|
192 |
+
{
|
193 |
+
if (is_array($v))
|
194 |
+
{
|
195 |
+
$string .= "[$k]=>(";
|
196 |
+
foreach ($v as $k2=>$v2)
|
197 |
+
{
|
198 |
+
$string .= "[$k2]=>\"".$v2.'", ';
|
199 |
+
}
|
200 |
+
$string .= ")";
|
201 |
+
} else {
|
202 |
+
$string .= "[$k]=>\"".$v.'", ';
|
203 |
+
}
|
204 |
+
}
|
205 |
+
$string .= ")";
|
206 |
+
}
|
207 |
+
|
208 |
+
if (isset($this->text))
|
209 |
+
{
|
210 |
+
$string .= " text: (" . $this->text . ")";
|
211 |
+
}
|
212 |
+
|
213 |
+
$string .= " HDOM_INNER_INFO: '";
|
214 |
+
if (isset($node->_[HDOM_INFO_INNER]))
|
215 |
+
{
|
216 |
+
$string .= $node->_[HDOM_INFO_INNER] . "'";
|
217 |
+
}
|
218 |
+
else
|
219 |
+
{
|
220 |
+
$string .= ' NULL ';
|
221 |
+
}
|
222 |
+
|
223 |
+
$string .= " children: " . count($this->children);
|
224 |
+
$string .= " nodes: " . count($this->nodes);
|
225 |
+
$string .= " tag_start: " . $this->tag_start;
|
226 |
+
$string .= "\n";
|
227 |
+
|
228 |
+
if ($echo)
|
229 |
+
{
|
230 |
+
echo $string;
|
231 |
+
return;
|
232 |
+
}
|
233 |
+
else
|
234 |
+
{
|
235 |
+
return $string;
|
236 |
+
}
|
237 |
+
}
|
238 |
+
|
239 |
+
// returns the parent of node
|
240 |
+
// If a node is passed in, it will reset the parent of the current node to that one.
|
241 |
+
function parent($parent=null)
|
242 |
+
{
|
243 |
+
// I am SURE that this doesn't work properly.
|
244 |
+
// It fails to unset the current node from it's current parents nodes or children list first.
|
245 |
+
if ($parent !== null)
|
246 |
+
{
|
247 |
+
$this->parent = $parent;
|
248 |
+
$this->parent->nodes[] = $this;
|
249 |
+
$this->parent->children[] = $this;
|
250 |
+
}
|
251 |
+
|
252 |
+
return $this->parent;
|
253 |
+
}
|
254 |
+
|
255 |
+
// verify that node has children
|
256 |
+
function has_child()
|
257 |
+
{
|
258 |
+
return !empty($this->children);
|
259 |
+
}
|
260 |
+
|
261 |
+
// returns children of node
|
262 |
+
function children($idx=-1)
|
263 |
+
{
|
264 |
+
if ($idx===-1)
|
265 |
+
{
|
266 |
+
return $this->children;
|
267 |
+
}
|
268 |
+
if (isset($this->children[$idx]))
|
269 |
+
{
|
270 |
+
return $this->children[$idx];
|
271 |
+
}
|
272 |
+
return null;
|
273 |
+
}
|
274 |
+
|
275 |
+
// returns the first child of node
|
276 |
+
function first_child()
|
277 |
+
{
|
278 |
+
if (count($this->children)>0)
|
279 |
+
{
|
280 |
+
return $this->children[0];
|
281 |
+
}
|
282 |
+
return null;
|
283 |
+
}
|
284 |
+
|
285 |
+
// returns the last child of node
|
286 |
+
function last_child()
|
287 |
+
{
|
288 |
+
if (($count=count($this->children))>0)
|
289 |
+
{
|
290 |
+
return $this->children[$count-1];
|
291 |
+
}
|
292 |
+
return null;
|
293 |
+
}
|
294 |
+
|
295 |
+
// returns the next sibling of node
|
296 |
+
function next_sibling()
|
297 |
+
{
|
298 |
+
if ($this->parent===null)
|
299 |
+
{
|
300 |
+
return null;
|
301 |
+
}
|
302 |
+
|
303 |
+
$idx = 0;
|
304 |
+
$count = count($this->parent->children);
|
305 |
+
while ($idx<$count && $this!==$this->parent->children[$idx])
|
306 |
+
{
|
307 |
+
++$idx;
|
308 |
+
}
|
309 |
+
if (++$idx>=$count)
|
310 |
+
{
|
311 |
+
return null;
|
312 |
+
}
|
313 |
+
return $this->parent->children[$idx];
|
314 |
+
}
|
315 |
+
|
316 |
+
// returns the previous sibling of node
|
317 |
+
function prev_sibling()
|
318 |
+
{
|
319 |
+
if ($this->parent===null) return null;
|
320 |
+
$idx = 0;
|
321 |
+
$count = count($this->parent->children);
|
322 |
+
while ($idx<$count && $this!==$this->parent->children[$idx])
|
323 |
+
++$idx;
|
324 |
+
if (--$idx<0) return null;
|
325 |
+
return $this->parent->children[$idx];
|
326 |
+
}
|
327 |
+
|
328 |
+
// function to locate a specific ancestor tag in the path to the root.
|
329 |
+
function find_ancestor_tag($tag)
|
330 |
+
{
|
331 |
+
global $debug_object;
|
332 |
+
if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }
|
333 |
+
|
334 |
+
// Start by including ourselves in the comparison.
|
335 |
+
$returnDom = $this;
|
336 |
+
|
337 |
+
while (!is_null($returnDom))
|
338 |
+
{
|
339 |
+
if (is_object($debug_object)) { $debug_object->debug_log(2, "Current tag is: " . $returnDom->tag); }
|
340 |
+
|
341 |
+
if ($returnDom->tag == $tag)
|
342 |
+
{
|
343 |
+
break;
|
344 |
+
}
|
345 |
+
$returnDom = $returnDom->parent;
|
346 |
+
}
|
347 |
+
return $returnDom;
|
348 |
+
}
|
349 |
+
|
350 |
+
// get dom node's inner html
|
351 |
+
function innertext()
|
352 |
+
{
|
353 |
+
if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
|
354 |
+
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
355 |
+
|
356 |
+
$ret = '';
|
357 |
+
foreach ($this->nodes as $n)
|
358 |
+
$ret .= $n->outertext();
|
359 |
+
return $ret;
|
360 |
+
}
|
361 |
+
|
362 |
+
// get dom node's outer text (with tag)
|
363 |
+
function outertext()
|
364 |
+
{
|
365 |
+
global $debug_object;
|
366 |
+
if (is_object($debug_object))
|
367 |
+
{
|
368 |
+
$text = '';
|
369 |
+
if ($this->tag == 'text')
|
370 |
+
{
|
371 |
+
if (!empty($this->text))
|
372 |
+
{
|
373 |
+
$text = " with text: " . $this->text;
|
374 |
+
}
|
375 |
+
}
|
376 |
+
$debug_object->debug_log(1, 'Innertext of tag: ' . $this->tag . $text);
|
377 |
+
}
|
378 |
+
|
379 |
+
if ($this->tag==='root') return $this->innertext();
|
380 |
+
|
381 |
+
// trigger callback
|
382 |
+
if ($this->dom && $this->dom->callback!==null)
|
383 |
+
{
|
384 |
+
call_user_func_array($this->dom->callback, array($this));
|
385 |
+
}
|
386 |
+
|
387 |
+
if (isset($this->_[HDOM_INFO_OUTER])) return $this->_[HDOM_INFO_OUTER];
|
388 |
+
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
389 |
+
|
390 |
+
// render begin tag
|
391 |
+
if ($this->dom && $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]])
|
392 |
+
{
|
393 |
+
$ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup();
|
394 |
+
} else {
|
395 |
+
$ret = "";
|
396 |
+
}
|
397 |
+
|
398 |
+
// render inner text
|
399 |
+
if (isset($this->_[HDOM_INFO_INNER]))
|
400 |
+
{
|
401 |
+
// If it's a br tag... don't return the HDOM_INNER_INFO that we may or may not have added.
|
402 |
+
if ($this->tag != "br")
|
403 |
+
{
|
404 |
+
$ret .= $this->_[HDOM_INFO_INNER];
|
405 |
+
}
|
406 |
+
} else {
|
407 |
+
if ($this->nodes)
|
408 |
+
{
|
409 |
+
foreach ($this->nodes as $n)
|
410 |
+
{
|
411 |
+
$ret .= $this->convert_text($n->outertext());
|
412 |
+
}
|
413 |
+
}
|
414 |
+
}
|
415 |
+
|
416 |
+
// render end tag
|
417 |
+
if (isset($this->_[HDOM_INFO_END]) && $this->_[HDOM_INFO_END]!=0)
|
418 |
+
$ret .= '</'.$this->tag.'>';
|
419 |
+
return $ret;
|
420 |
+
}
|
421 |
+
|
422 |
+
// get dom node's plain text
|
423 |
+
function text()
|
424 |
+
{
|
425 |
+
if (isset($this->_[HDOM_INFO_INNER])) return $this->_[HDOM_INFO_INNER];
|
426 |
+
switch ($this->nodetype)
|
427 |
+
{
|
428 |
+
case HDOM_TYPE_TEXT: return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
429 |
+
case HDOM_TYPE_COMMENT: return '';
|
430 |
+
case HDOM_TYPE_UNKNOWN: return '';
|
431 |
+
}
|
432 |
+
if (strcasecmp($this->tag, 'script')===0) return '';
|
433 |
+
if (strcasecmp($this->tag, 'style')===0) return '';
|
434 |
+
|
435 |
+
$ret = '';
|
436 |
+
// In rare cases, (always node type 1 or HDOM_TYPE_ELEMENT - observed for some span tags, and some p tags) $this->nodes is set to NULL.
|
437 |
+
// NOTE: This indicates that there is a problem where it's set to NULL without a clear happening.
|
438 |
+
// WHY is this happening?
|
439 |
+
if (!is_null($this->nodes))
|
440 |
+
{
|
441 |
+
foreach ($this->nodes as $n)
|
442 |
+
{
|
443 |
+
$ret .= $this->convert_text($n->text());
|
444 |
+
}
|
445 |
+
|
446 |
+
// If this node is a span... add a space at the end of it so multiple spans don't run into each other. This is plaintext after all.
|
447 |
+
if ($this->tag == "span")
|
448 |
+
{
|
449 |
+
$ret .= $this->dom->default_span_text;
|
450 |
+
}
|
451 |
+
|
452 |
+
|
453 |
+
}
|
454 |
+
return $ret;
|
455 |
+
}
|
456 |
+
|
457 |
+
function xmltext()
|
458 |
+
{
|
459 |
+
$ret = $this->innertext();
|
460 |
+
$ret = str_ireplace('<![CDATA[', '', $ret);
|
461 |
+
$ret = str_replace(']]>', '', $ret);
|
462 |
+
return $ret;
|
463 |
+
}
|
464 |
+
|
465 |
+
// build node's text with tag
|
466 |
+
function makeup()
|
467 |
+
{
|
468 |
+
// text, comment, unknown
|
469 |
+
if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]);
|
470 |
+
|
471 |
+
$ret = '<'.$this->tag;
|
472 |
+
$i = -1;
|
473 |
+
|
474 |
+
foreach ($this->attr as $key=>$val)
|
475 |
+
{
|
476 |
+
++$i;
|
477 |
+
|
478 |
+
// skip removed attribute
|
479 |
+
if ($val===null || $val===false)
|
480 |
+
continue;
|
481 |
+
|
482 |
+
$ret .= $this->_[HDOM_INFO_SPACE][$i][0];
|
483 |
+
//no value attr: nowrap, checked selected...
|
484 |
+
if ($val===true)
|
485 |
+
$ret .= $key;
|
486 |
+
else {
|
487 |
+
switch ($this->_[HDOM_INFO_QUOTE][$i])
|
488 |
+
{
|
489 |
+
case HDOM_QUOTE_DOUBLE: $quote = '"'; break;
|
490 |
+
case HDOM_QUOTE_SINGLE: $quote = '\''; break;
|
491 |
+
default: $quote = '';
|
492 |
+
}
|
493 |
+
$ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1].'='.$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;
|
494 |
+
}
|
495 |
+
}
|
496 |
+
$ret = $this->dom->restore_noise($ret);
|
497 |
+
return $ret . $this->_[HDOM_INFO_ENDSPACE] . '>';
|
498 |
+
}
|
499 |
+
|
500 |
+
// find elements by css selector
|
501 |
+
//PaperG - added ability for find to lowercase the value of the selector.
|
502 |
+
function find($selector, $idx=null, $lowercase=false)
|
503 |
+
{
|
504 |
+
$selectors = $this->parse_selector($selector);
|
505 |
+
if (($count=count($selectors))===0) return array();
|
506 |
+
$found_keys = array();
|
507 |
+
|
508 |
+
// find each selector
|
509 |
+
for ($c=0; $c<$count; ++$c)
|
510 |
+
{
|
511 |
+
// The change on the below line was documented on the sourceforge code tracker id 2788009
|
512 |
+
// used to be: if (($levle=count($selectors[0]))===0) return array();
|
513 |
+
if (($levle=count($selectors[$c]))===0) return array();
|
514 |
+
if (!isset($this->_[HDOM_INFO_BEGIN])) return array();
|
515 |
+
|
516 |
+
$head = array($this->_[HDOM_INFO_BEGIN]=>1);
|
517 |
+
|
518 |
+
// handle descendant selectors, no recursive!
|
519 |
+
for ($l=0; $l<$levle; ++$l)
|
520 |
+
{
|
521 |
+
$ret = array();
|
522 |
+
foreach ($head as $k=>$v)
|
523 |
+
{
|
524 |
+
$n = ($k===-1) ? $this->dom->root : $this->dom->nodes[$k];
|
525 |
+
//PaperG - Pass this optional parameter on to the seek function.
|
526 |
+
$n->seek($selectors[$c][$l], $ret, $lowercase);
|
527 |
+
}
|
528 |
+
$head = $ret;
|
529 |
+
}
|
530 |
+
|
531 |
+
foreach ($head as $k=>$v)
|
532 |
+
{
|
533 |
+
if (!isset($found_keys[$k]))
|
534 |
+
{
|
535 |
+
$found_keys[$k] = 1;
|
536 |
+
}
|
537 |
+
}
|
538 |
+
}
|
539 |
+
|
540 |
+
// sort keys
|
541 |
+
ksort($found_keys);
|
542 |
+
|
543 |
+
$found = array();
|
544 |
+
foreach ($found_keys as $k=>$v)
|
545 |
+
$found[] = $this->dom->nodes[$k];
|
546 |
+
|
547 |
+
// return nth-element or array
|
548 |
+
if (is_null($idx)) return $found;
|
549 |
+
else if ($idx<0) $idx = count($found) + $idx;
|
550 |
+
return (isset($found[$idx])) ? $found[$idx] : null;
|
551 |
+
}
|
552 |
+
|
553 |
+
// seek for given conditions
|
554 |
+
// PaperG - added parameter to allow for case insensitive testing of the value of a selector.
|
555 |
+
protected function seek($selector, &$ret, $lowercase=false)
|
556 |
+
{
|
557 |
+
global $debug_object;
|
558 |
+
if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }
|
559 |
+
|
560 |
+
list($tag, $key, $val, $exp, $no_key) = $selector;
|
561 |
+
|
562 |
+
// xpath index
|
563 |
+
if ($tag && $key && is_numeric($key))
|
564 |
+
{
|
565 |
+
$count = 0;
|
566 |
+
foreach ($this->children as $c)
|
567 |
+
{
|
568 |
+
if ($tag==='*' || $tag===$c->tag) {
|
569 |
+
if (++$count==$key) {
|
570 |
+
$ret[$c->_[HDOM_INFO_BEGIN]] = 1;
|
571 |
+
return;
|
572 |
+
}
|
573 |
+
}
|
574 |
+
}
|
575 |
+
return;
|
576 |
+
}
|
577 |
+
|
578 |
+
$end = (!empty($this->_[HDOM_INFO_END])) ? $this->_[HDOM_INFO_END] : 0;
|
579 |
+
if ($end==0) {
|
580 |
+
$parent = $this->parent;
|
581 |
+
while (!isset($parent->_[HDOM_INFO_END]) && $parent!==null) {
|
582 |
+
$end -= 1;
|
583 |
+
$parent = $parent->parent;
|
584 |
+
}
|
585 |
+
$end += $parent->_[HDOM_INFO_END];
|
586 |
+
}
|
587 |
+
|
588 |
+
for ($i=$this->_[HDOM_INFO_BEGIN]+1; $i<$end; ++$i) {
|
589 |
+
$node = $this->dom->nodes[$i];
|
590 |
+
|
591 |
+
$pass = true;
|
592 |
+
|
593 |
+
if ($tag==='*' && !$key) {
|
594 |
+
if (in_array($node, $this->children, true))
|
595 |
+
$ret[$i] = 1;
|
596 |
+
continue;
|
597 |
+
}
|
598 |
+
|
599 |
+
// compare tag
|
600 |
+
if ($tag && $tag!=$node->tag && $tag!=='*') {$pass=false;}
|
601 |
+
// compare key
|
602 |
+
if ($pass && $key) {
|
603 |
+
if ($no_key) {
|
604 |
+
if (isset($node->attr[$key])) $pass=false;
|
605 |
+
} else {
|
606 |
+
if (($key != "plaintext") && !isset($node->attr[$key])) $pass=false;
|
607 |
+
}
|
608 |
+
}
|
609 |
+
// compare value
|
610 |
+
if ($pass && $key && $val && $val!=='*') {
|
611 |
+
// If they have told us that this is a "plaintext" search then we want the plaintext of the node - right?
|
612 |
+
if ($key == "plaintext") {
|
613 |
+
// $node->plaintext actually returns $node->text();
|
614 |
+
$nodeKeyValue = $node->text();
|
615 |
+
} else {
|
616 |
+
// this is a normal search, we want the value of that attribute of the tag.
|
617 |
+
$nodeKeyValue = $node->attr[$key];
|
618 |
+
}
|
619 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, "testing node: " . $node->tag . " for attribute: " . $key . $exp . $val . " where nodes value is: " . $nodeKeyValue);}
|
620 |
+
|
621 |
+
//PaperG - If lowercase is set, do a case insensitive test of the value of the selector.
|
622 |
+
if ($lowercase) {
|
623 |
+
$check = $this->match($exp, strtolower($val), strtolower($nodeKeyValue));
|
624 |
+
} else {
|
625 |
+
$check = $this->match($exp, $val, $nodeKeyValue);
|
626 |
+
}
|
627 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, "after match: " . ($check ? "true" : "false"));}
|
628 |
+
|
629 |
+
// handle multiple class
|
630 |
+
if (!$check && strcasecmp($key, 'class')===0) {
|
631 |
+
foreach (explode(' ',$node->attr[$key]) as $k) {
|
632 |
+
// Without this, there were cases where leading, trailing, or double spaces lead to our comparing blanks - bad form.
|
633 |
+
if (!empty($k)) {
|
634 |
+
if ($lowercase) {
|
635 |
+
$check = $this->match($exp, strtolower($val), strtolower($k));
|
636 |
+
} else {
|
637 |
+
$check = $this->match($exp, $val, $k);
|
638 |
+
}
|
639 |
+
if ($check) break;
|
640 |
+
}
|
641 |
+
}
|
642 |
+
}
|
643 |
+
if (!$check) $pass = false;
|
644 |
+
}
|
645 |
+
if ($pass) $ret[$i] = 1;
|
646 |
+
unset($node);
|
647 |
+
}
|
648 |
+
// It's passed by reference so this is actually what this function returns.
|
649 |
+
if (is_object($debug_object)) {$debug_object->debug_log(1, "EXIT - ret: ", $ret);}
|
650 |
+
}
|
651 |
+
|
652 |
+
protected function match($exp, $pattern, $value) {
|
653 |
+
global $debug_object;
|
654 |
+
if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
|
655 |
+
|
656 |
+
switch ($exp) {
|
657 |
+
case '=':
|
658 |
+
return ($value===$pattern);
|
659 |
+
case '!=':
|
660 |
+
return ($value!==$pattern);
|
661 |
+
case '^=':
|
662 |
+
return preg_match("/^".preg_quote($pattern,'/')."/", $value);
|
663 |
+
case '$=':
|
664 |
+
return preg_match("/".preg_quote($pattern,'/')."$/", $value);
|
665 |
+
case '*=':
|
666 |
+
if ($pattern[0]=='/') {
|
667 |
+
return preg_match($pattern, $value);
|
668 |
+
}
|
669 |
+
return preg_match("/".$pattern."/i", $value);
|
670 |
+
}
|
671 |
+
return false;
|
672 |
+
}
|
673 |
+
|
674 |
+
protected function parse_selector($selector_string) {
|
675 |
+
global $debug_object;
|
676 |
+
if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
|
677 |
+
|
678 |
+
// pattern of CSS selectors, modified from mootools
|
679 |
+
// Paperg: Add the colon to the attrbute, so that it properly finds <tag attr:ibute="something" > like google does.
|
680 |
+
// Note: if you try to look at this attribute, yo MUST use getAttribute since $dom->x:y will fail the php syntax check.
|
681 |
+
// Notice the \[ starting the attbute? and the @? following? This implies that an attribute can begin with an @ sign that is not captured.
|
682 |
+
// This implies that an html attribute specifier may start with an @ sign that is NOT captured by the expression.
|
683 |
+
// farther study is required to determine of this should be documented or removed.
|
684 |
+
// $pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";
|
685 |
+
$pattern = "/([\w-:\*]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is";
|
686 |
+
preg_match_all($pattern, trim($selector_string).' ', $matches, PREG_SET_ORDER);
|
687 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, "Matches Array: ", $matches);}
|
688 |
+
|
689 |
+
$selectors = array();
|
690 |
+
$result = array();
|
691 |
+
//print_r($matches);
|
692 |
+
|
693 |
+
foreach ($matches as $m) {
|
694 |
+
$m[0] = trim($m[0]);
|
695 |
+
if ($m[0]==='' || $m[0]==='/' || $m[0]==='//') continue;
|
696 |
+
// for browser generated xpath
|
697 |
+
if ($m[1]==='tbody') continue;
|
698 |
+
|
699 |
+
list($tag, $key, $val, $exp, $no_key) = array($m[1], null, null, '=', false);
|
700 |
+
if (!empty($m[2])) {$key='id'; $val=$m[2];}
|
701 |
+
if (!empty($m[3])) {$key='class'; $val=$m[3];}
|
702 |
+
if (!empty($m[4])) {$key=$m[4];}
|
703 |
+
if (!empty($m[5])) {$exp=$m[5];}
|
704 |
+
if (!empty($m[6])) {$val=$m[6];}
|
705 |
+
|
706 |
+
// convert to lowercase
|
707 |
+
if ($this->dom->lowercase) {$tag=strtolower($tag); $key=strtolower($key);}
|
708 |
+
//elements that do NOT have the specified attribute
|
709 |
+
if (isset($key[0]) && $key[0]==='!') {$key=substr($key, 1); $no_key=true;}
|
710 |
+
|
711 |
+
$result[] = array($tag, $key, $val, $exp, $no_key);
|
712 |
+
if (trim($m[7])===',') {
|
713 |
+
$selectors[] = $result;
|
714 |
+
$result = array();
|
715 |
+
}
|
716 |
+
}
|
717 |
+
if (count($result)>0)
|
718 |
+
$selectors[] = $result;
|
719 |
+
return $selectors;
|
720 |
+
}
|
721 |
+
|
722 |
+
function __get($name)
|
723 |
+
{
|
724 |
+
if (isset($this->attr[$name]))
|
725 |
+
{
|
726 |
+
return $this->convert_text($this->attr[$name]);
|
727 |
+
}
|
728 |
+
switch ($name)
|
729 |
+
{
|
730 |
+
case 'outertext': return $this->outertext();
|
731 |
+
case 'innertext': return $this->innertext();
|
732 |
+
case 'plaintext': return $this->text();
|
733 |
+
case 'xmltext': return $this->xmltext();
|
734 |
+
default: return array_key_exists($name, $this->attr);
|
735 |
+
}
|
736 |
+
}
|
737 |
+
|
738 |
+
function __set($name, $value)
|
739 |
+
{
|
740 |
+
global $debug_object;
|
741 |
+
if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
|
742 |
+
|
743 |
+
switch ($name)
|
744 |
+
{
|
745 |
+
case 'outertext': return $this->_[HDOM_INFO_OUTER] = $value;
|
746 |
+
case 'innertext':
|
747 |
+
if (isset($this->_[HDOM_INFO_TEXT])) return $this->_[HDOM_INFO_TEXT] = $value;
|
748 |
+
return $this->_[HDOM_INFO_INNER] = $value;
|
749 |
+
}
|
750 |
+
if (!isset($this->attr[$name]))
|
751 |
+
{
|
752 |
+
$this->_[HDOM_INFO_SPACE][] = array(' ', '', '');
|
753 |
+
$this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
|
754 |
+
}
|
755 |
+
$this->attr[$name] = $value;
|
756 |
+
}
|
757 |
+
|
758 |
+
function __isset($name)
|
759 |
+
{
|
760 |
+
switch ($name)
|
761 |
+
{
|
762 |
+
case 'outertext': return true;
|
763 |
+
case 'innertext': return true;
|
764 |
+
case 'plaintext': return true;
|
765 |
+
}
|
766 |
+
//no value attr: nowrap, checked selected...
|
767 |
+
return (array_key_exists($name, $this->attr)) ? true : isset($this->attr[$name]);
|
768 |
+
}
|
769 |
+
|
770 |
+
function __unset($name) {
|
771 |
+
if (isset($this->attr[$name]))
|
772 |
+
unset($this->attr[$name]);
|
773 |
+
}
|
774 |
+
|
775 |
+
// PaperG - Function to convert the text from one character set to another if the two sets are not the same.
|
776 |
+
function convert_text($text)
|
777 |
+
{
|
778 |
+
global $debug_object;
|
779 |
+
if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
|
780 |
+
|
781 |
+
$converted_text = $text;
|
782 |
+
|
783 |
+
$sourceCharset = "";
|
784 |
+
$targetCharset = "";
|
785 |
+
|
786 |
+
if ($this->dom)
|
787 |
+
{
|
788 |
+
$sourceCharset = strtoupper($this->dom->_charset);
|
789 |
+
$targetCharset = strtoupper($this->dom->_target_charset);
|
790 |
+
}
|
791 |
+
if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);}
|
792 |
+
|
793 |
+
if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0))
|
794 |
+
{
|
795 |
+
// Check if the reported encoding could have been incorrect and the text is actually already UTF-8
|
796 |
+
if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text)))
|
797 |
+
{
|
798 |
+
$converted_text = $text;
|
799 |
+
}
|
800 |
+
else
|
801 |
+
{
|
802 |
+
$converted_text = iconv($sourceCharset, $targetCharset, $text);
|
803 |
+
}
|
804 |
+
}
|
805 |
+
|
806 |
+
// Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output.
|
807 |
+
if ($targetCharset == 'UTF-8')
|
808 |
+
{
|
809 |
+
if (substr($converted_text, 0, 3) == "\xef\xbb\xbf")
|
810 |
+
{
|
811 |
+
$converted_text = substr($converted_text, 3);
|
812 |
+
}
|
813 |
+
if (substr($converted_text, -3) == "\xef\xbb\xbf")
|
814 |
+
{
|
815 |
+
$converted_text = substr($converted_text, 0, -3);
|
816 |
+
}
|
817 |
+
}
|
818 |
+
|
819 |
+
return $converted_text;
|
820 |
+
}
|
821 |
+
|
822 |
+
/**
|
823 |
+
* Returns true if $string is valid UTF-8 and false otherwise.
|
824 |
+
*
|
825 |
+
* @param mixed $str String to be tested
|
826 |
+
* @return boolean
|
827 |
+
*/
|
828 |
+
static function is_utf8($str)
|
829 |
+
{
|
830 |
+
$c=0; $b=0;
|
831 |
+
$bits=0;
|
832 |
+
$len=strlen($str);
|
833 |
+
for($i=0; $i<$len; $i++)
|
834 |
+
{
|
835 |
+
$c=ord($str[$i]);
|
836 |
+
if($c > 128)
|
837 |
+
{
|
838 |
+
if(($c >= 254)) return false;
|
839 |
+
elseif($c >= 252) $bits=6;
|
840 |
+
elseif($c >= 248) $bits=5;
|
841 |
+
elseif($c >= 240) $bits=4;
|
842 |
+
elseif($c >= 224) $bits=3;
|
843 |
+
elseif($c >= 192) $bits=2;
|
844 |
+
else return false;
|
845 |
+
if(($i+$bits) > $len) return false;
|
846 |
+
while($bits > 1)
|
847 |
+
{
|
848 |
+
$i++;
|
849 |
+
$b=ord($str[$i]);
|
850 |
+
if($b < 128 || $b > 191) return false;
|
851 |
+
$bits--;
|
852 |
+
}
|
853 |
+
}
|
854 |
+
}
|
855 |
+
return true;
|
856 |
+
}
|
857 |
+
/*
|
858 |
+
function is_utf8($string)
|
859 |
+
{
|
860 |
+
//this is buggy
|
861 |
+
return (utf8_encode(utf8_decode($string)) == $string);
|
862 |
+
}
|
863 |
+
*/
|
864 |
+
|
865 |
+
/**
|
866 |
+
* Function to try a few tricks to determine the displayed size of an img on the page.
|
867 |
+
* NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types.
|
868 |
+
*
|
869 |
+
* @author John Schlick
|
870 |
+
* @version April 19 2012
|
871 |
+
* @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out.
|
872 |
+
*/
|
873 |
+
function get_display_size()
|
874 |
+
{
|
875 |
+
global $debug_object;
|
876 |
+
|
877 |
+
$width = -1;
|
878 |
+
$height = -1;
|
879 |
+
|
880 |
+
if ($this->tag !== 'img')
|
881 |
+
{
|
882 |
+
return false;
|
883 |
+
}
|
884 |
+
|
885 |
+
// See if there is aheight or width attribute in the tag itself.
|
886 |
+
if (isset($this->attr['width']))
|
887 |
+
{
|
888 |
+
$width = $this->attr['width'];
|
889 |
+
}
|
890 |
+
|
891 |
+
if (isset($this->attr['height']))
|
892 |
+
{
|
893 |
+
$height = $this->attr['height'];
|
894 |
+
}
|
895 |
+
|
896 |
+
// Now look for an inline style.
|
897 |
+
if (isset($this->attr['style']))
|
898 |
+
{
|
899 |
+
// Thanks to user gnarf from stackoverflow for this regular expression.
|
900 |
+
$attributes = array();
|
901 |
+
preg_match_all("/([\w-]+)\s*:\s*([^;]+)\s*;?/", $this->attr['style'], $matches, PREG_SET_ORDER);
|
902 |
+
foreach ($matches as $match) {
|
903 |
+
$attributes[$match[1]] = $match[2];
|
904 |
+
}
|
905 |
+
|
906 |
+
// If there is a width in the style attributes:
|
907 |
+
if (isset($attributes['width']) && $width == -1)
|
908 |
+
{
|
909 |
+
// check that the last two characters are px (pixels)
|
910 |
+
if (strtolower(substr($attributes['width'], -2)) == 'px')
|
911 |
+
{
|
912 |
+
$proposed_width = substr($attributes['width'], 0, -2);
|
913 |
+
// Now make sure that it's an integer and not something stupid.
|
914 |
+
if (filter_var($proposed_width, FILTER_VALIDATE_INT))
|
915 |
+
{
|
916 |
+
$width = $proposed_width;
|
917 |
+
}
|
918 |
+
}
|
919 |
+
}
|
920 |
+
|
921 |
+
// If there is a width in the style attributes:
|
922 |
+
if (isset($attributes['height']) && $height == -1)
|
923 |
+
{
|
924 |
+
// check that the last two characters are px (pixels)
|
925 |
+
if (strtolower(substr($attributes['height'], -2)) == 'px')
|
926 |
+
{
|
927 |
+
$proposed_height = substr($attributes['height'], 0, -2);
|
928 |
+
// Now make sure that it's an integer and not something stupid.
|
929 |
+
if (filter_var($proposed_height, FILTER_VALIDATE_INT))
|
930 |
+
{
|
931 |
+
$height = $proposed_height;
|
932 |
+
}
|
933 |
+
}
|
934 |
+
}
|
935 |
+
|
936 |
+
}
|
937 |
+
|
938 |
+
// Future enhancement:
|
939 |
+
// Look in the tag to see if there is a class or id specified that has a height or width attribute to it.
|
940 |
+
|
941 |
+
// Far future enhancement
|
942 |
+
// Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width
|
943 |
+
// Note that in this case, the class or id will have the img subselector for it to apply to the image.
|
944 |
+
|
945 |
+
// ridiculously far future development
|
946 |
+
// If the class or id is specified in a SEPARATE css file thats not on the page, go get it and do what we were just doing for the ones on the page.
|
947 |
+
|
948 |
+
$result = array('height' => $height,
|
949 |
+
'width' => $width);
|
950 |
+
return $result;
|
951 |
+
}
|
952 |
+
|
953 |
+
// camel naming conventions
|
954 |
+
function getAllAttributes() {return $this->attr;}
|
955 |
+
function getAttribute($name) {return $this->__get($name);}
|
956 |
+
function setAttribute($name, $value) {$this->__set($name, $value);}
|
957 |
+
function hasAttribute($name) {return $this->__isset($name);}
|
958 |
+
function removeAttribute($name) {$this->__set($name, null);}
|
959 |
+
function getElementById($id) {return $this->find("#$id", 0);}
|
960 |
+
function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
|
961 |
+
function getElementByTagName($name) {return $this->find($name, 0);}
|
962 |
+
function getElementsByTagName($name, $idx=null) {return $this->find($name, $idx);}
|
963 |
+
function parentNode() {return $this->parent();}
|
964 |
+
function childNodes($idx=-1) {return $this->children($idx);}
|
965 |
+
function firstChild() {return $this->first_child();}
|
966 |
+
function lastChild() {return $this->last_child();}
|
967 |
+
function nextSibling() {return $this->next_sibling();}
|
968 |
+
function previousSibling() {return $this->prev_sibling();}
|
969 |
+
function hasChildNodes() {return $this->has_child();}
|
970 |
+
function nodeName() {return $this->tag;}
|
971 |
+
function appendChild($node) {$node->parent($this); return $node;}
|
972 |
+
|
973 |
+
}
|
974 |
+
|
975 |
+
/**
|
976 |
+
* simple html dom parser
|
977 |
+
* Paperg - in the find routine: allow us to specify that we want case insensitive testing of the value of the selector.
|
978 |
+
* Paperg - change $size from protected to public so we can easily access it
|
979 |
+
* Paperg - added ForceTagsClosed in the constructor which tells us whether we trust the html or not. Default is to NOT trust it.
|
980 |
+
*
|
981 |
+
* @package PlaceLocalInclude
|
982 |
+
*/
|
983 |
+
class simple_html_dom
|
984 |
+
{
|
985 |
+
public $root = null;
|
986 |
+
public $nodes = array();
|
987 |
+
public $callback = null;
|
988 |
+
public $lowercase = false;
|
989 |
+
// Used to keep track of how large the text was when we started.
|
990 |
+
public $original_size;
|
991 |
+
public $size;
|
992 |
+
protected $pos;
|
993 |
+
protected $doc;
|
994 |
+
protected $char;
|
995 |
+
protected $cursor;
|
996 |
+
protected $parent;
|
997 |
+
protected $noise = array();
|
998 |
+
protected $token_blank = " \t\r\n";
|
999 |
+
protected $token_equal = ' =/>';
|
1000 |
+
protected $token_slash = " />\r\n\t";
|
1001 |
+
protected $token_attr = ' >';
|
1002 |
+
// Note that this is referenced by a child node, and so it needs to be public for that node to see this information.
|
1003 |
+
public $_charset = '';
|
1004 |
+
public $_target_charset = '';
|
1005 |
+
protected $default_br_text = "";
|
1006 |
+
public $default_span_text = "";
|
1007 |
+
|
1008 |
+
// use isset instead of in_array, performance boost about 30%...
|
1009 |
+
protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);
|
1010 |
+
protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);
|
1011 |
+
// Known sourceforge issue #2977341
|
1012 |
+
// B tags that are not closed cause us to return everything to the end of the document.
|
1013 |
+
protected $optional_closing_tags = array(
|
1014 |
+
'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),
|
1015 |
+
'th'=>array('th'=>1),
|
1016 |
+
'td'=>array('td'=>1),
|
1017 |
+
'li'=>array('li'=>1),
|
1018 |
+
'dt'=>array('dt'=>1, 'dd'=>1),
|
1019 |
+
'dd'=>array('dd'=>1, 'dt'=>1),
|
1020 |
+
'dl'=>array('dd'=>1, 'dt'=>1),
|
1021 |
+
'p'=>array('p'=>1),
|
1022 |
+
'nobr'=>array('nobr'=>1),
|
1023 |
+
'b'=>array('b'=>1),
|
1024 |
+
'option'=>array('option'=>1),
|
1025 |
+
);
|
1026 |
+
|
1027 |
+
function __construct($str=null, $lowercase=true, $forceTagsClosed=true, $stripRN=true)
|
1028 |
+
{
|
1029 |
+
if ($str)
|
1030 |
+
{
|
1031 |
+
if (preg_match("/^http:\/\//i",$str) || is_file($str))
|
1032 |
+
{
|
1033 |
+
$this->load_file($str);
|
1034 |
+
}
|
1035 |
+
else
|
1036 |
+
{
|
1037 |
+
$this->load($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);
|
1038 |
+
}
|
1039 |
+
}
|
1040 |
+
// Forcing tags to be closed implies that we don't trust the html, but it can lead to parsing errors if we SHOULD trust the html.
|
1041 |
+
if (!$forceTagsClosed) {
|
1042 |
+
$this->optional_closing_array=array();
|
1043 |
+
}
|
1044 |
+
$this->_target_charset = 'UTF-8';
|
1045 |
+
}
|
1046 |
+
|
1047 |
+
function __destruct()
|
1048 |
+
{
|
1049 |
+
$this->clear();
|
1050 |
+
}
|
1051 |
+
|
1052 |
+
// load html from string
|
1053 |
+
function load($str, $lowercase=true, $stripRN=true)
|
1054 |
+
{
|
1055 |
+
global $debug_object;
|
1056 |
+
|
1057 |
+
// prepare
|
1058 |
+
$this->prepare($str, $lowercase, $stripRN, $defaultBRText, $defaultSpanText);
|
1059 |
+
// strip out cdata
|
1060 |
+
$this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);
|
1061 |
+
// strip out comments
|
1062 |
+
$this->remove_noise("'<!--(.*?)-->'is");
|
1063 |
+
// Per sourceforge http://sourceforge.net/tracker/?func=detail&aid=2949097&group_id=218559&atid=1044037
|
1064 |
+
// Script tags removal now preceeds style tag removal.
|
1065 |
+
// strip out <script> tags
|
1066 |
+
$this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");
|
1067 |
+
$this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");
|
1068 |
+
// strip out <style> tags
|
1069 |
+
$this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");
|
1070 |
+
$this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");
|
1071 |
+
// strip out preformatted tags
|
1072 |
+
$this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");
|
1073 |
+
// strip out server side scripts
|
1074 |
+
$this->remove_noise("'(<\?)(.*?)(\?>)'s", true);
|
1075 |
+
// strip smarty scripts
|
1076 |
+
$this->remove_noise("'(\{\w)(.*?)(\})'s", true);
|
1077 |
+
|
1078 |
+
// parsing
|
1079 |
+
while ($this->parse());
|
1080 |
+
// end
|
1081 |
+
$this->root->_[HDOM_INFO_END] = $this->cursor;
|
1082 |
+
$this->parse_charset();
|
1083 |
+
|
1084 |
+
// make load function chainable
|
1085 |
+
return $this;
|
1086 |
+
|
1087 |
+
}
|
1088 |
+
|
1089 |
+
// load html from file
|
1090 |
+
function load_file()
|
1091 |
+
{
|
1092 |
+
$args = func_get_args();
|
1093 |
+
$this->load(call_user_func_array('file_get_contents', $args), true);
|
1094 |
+
// Throw an error if we can't properly load the dom.
|
1095 |
+
if (($error=error_get_last())!==null) {
|
1096 |
+
$this->clear();
|
1097 |
+
return false;
|
1098 |
+
}
|
1099 |
+
}
|
1100 |
+
|
1101 |
+
// set callback function
|
1102 |
+
function set_callback($function_name)
|
1103 |
+
{
|
1104 |
+
$this->callback = $function_name;
|
1105 |
+
}
|
1106 |
+
|
1107 |
+
// remove callback function
|
1108 |
+
function remove_callback()
|
1109 |
+
{
|
1110 |
+
$this->callback = null;
|
1111 |
+
}
|
1112 |
+
|
1113 |
+
// save dom as string
|
1114 |
+
function save($filepath='')
|
1115 |
+
{
|
1116 |
+
$ret = $this->root->innertext();
|
1117 |
+
if ($filepath!=='') file_put_contents($filepath, $ret, LOCK_EX);
|
1118 |
+
return $ret;
|
1119 |
+
}
|
1120 |
+
|
1121 |
+
// find dom node by css selector
|
1122 |
+
// Paperg - allow us to specify that we want case insensitive testing of the value of the selector.
|
1123 |
+
function find($selector, $idx=null, $lowercase=false)
|
1124 |
+
{
|
1125 |
+
return $this->root->find($selector, $idx, $lowercase);
|
1126 |
+
}
|
1127 |
+
|
1128 |
+
// clean up memory due to php5 circular references memory leak...
|
1129 |
+
function clear()
|
1130 |
+
{
|
1131 |
+
foreach ($this->nodes as $n) {$n->clear(); $n = null;}
|
1132 |
+
// This add next line is documented in the sourceforge repository. 2977248 as a fix for ongoing memory leaks that occur even with the use of clear.
|
1133 |
+
if (isset($this->children)) foreach ($this->children as $n) {$n->clear(); $n = null;}
|
1134 |
+
if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
|
1135 |
+
if (isset($this->root)) {$this->root->clear(); unset($this->root);}
|
1136 |
+
unset($this->doc);
|
1137 |
+
unset($this->noise);
|
1138 |
+
}
|
1139 |
+
|
1140 |
+
function dump($show_attr=true)
|
1141 |
+
{
|
1142 |
+
$this->root->dump($show_attr);
|
1143 |
+
}
|
1144 |
+
|
1145 |
+
// prepare HTML data and init everything
|
1146 |
+
protected function prepare($str, $lowercase=true, $stripRN=true)
|
1147 |
+
{
|
1148 |
+
$this->clear();
|
1149 |
+
|
1150 |
+
// set the length of content before we do anything to it.
|
1151 |
+
$this->size = strlen($str);
|
1152 |
+
// Save the original size of the html that we got in. It might be useful to someone.
|
1153 |
+
$this->original_size = $this->size;
|
1154 |
+
|
1155 |
+
//before we save the string as the doc... strip out the \r \n's if we are told to.
|
1156 |
+
if ($stripRN) {
|
1157 |
+
$str = str_replace("\r", " ", $str);
|
1158 |
+
$str = str_replace("\n", " ", $str);
|
1159 |
+
|
1160 |
+
// set the length of content since we have changed it.
|
1161 |
+
$this->size = strlen($str);
|
1162 |
+
}
|
1163 |
+
|
1164 |
+
$this->doc = $str;
|
1165 |
+
$this->pos = 0;
|
1166 |
+
$this->cursor = 1;
|
1167 |
+
$this->noise = array();
|
1168 |
+
$this->nodes = array();
|
1169 |
+
$this->lowercase = $lowercase;
|
1170 |
+
$this->default_br_text = "\r\n";
|
1171 |
+
$this->default_span_text = " ";
|
1172 |
+
$this->root = new simple_html_dom_node($this);
|
1173 |
+
$this->root->tag = 'root';
|
1174 |
+
$this->root->_[HDOM_INFO_BEGIN] = -1;
|
1175 |
+
$this->root->nodetype = HDOM_TYPE_ROOT;
|
1176 |
+
$this->parent = $this->root;
|
1177 |
+
if ($this->size>0) $this->char = $this->doc[0];
|
1178 |
+
}
|
1179 |
+
|
1180 |
+
// parse html content
|
1181 |
+
protected function parse()
|
1182 |
+
{
|
1183 |
+
if (($s = $this->copy_until_char('<'))==='')
|
1184 |
+
{
|
1185 |
+
return $this->read_tag();
|
1186 |
+
}
|
1187 |
+
|
1188 |
+
// text
|
1189 |
+
$node = new simple_html_dom_node($this);
|
1190 |
+
++$this->cursor;
|
1191 |
+
$node->_[HDOM_INFO_TEXT] = $s;
|
1192 |
+
$this->link_nodes($node, false);
|
1193 |
+
return true;
|
1194 |
+
}
|
1195 |
+
|
1196 |
+
// PAPERG - dkchou - added this to try to identify the character set of the page we have just parsed so we know better how to spit it out later.
|
1197 |
+
// NOTE: IF you provide a routine called get_last_retrieve_url_contents_content_type which returns the CURLINFO_CONTENT_TYPE from the last curl_exec
|
1198 |
+
// (or the content_type header from the last transfer), we will parse THAT, and if a charset is specified, we will use it over any other mechanism.
|
1199 |
+
protected function parse_charset()
|
1200 |
+
{
|
1201 |
+
global $debug_object;
|
1202 |
+
|
1203 |
+
$charset = null;
|
1204 |
+
|
1205 |
+
if (function_exists('get_last_retrieve_url_contents_content_type'))
|
1206 |
+
{
|
1207 |
+
$contentTypeHeader = get_last_retrieve_url_contents_content_type();
|
1208 |
+
$success = preg_match('/charset=(.+)/', $contentTypeHeader, $matches);
|
1209 |
+
if ($success)
|
1210 |
+
{
|
1211 |
+
$charset = $matches[1];
|
1212 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'header content-type found charset of: ' . $charset);}
|
1213 |
+
}
|
1214 |
+
|
1215 |
+
}
|
1216 |
+
|
1217 |
+
if (empty($charset))
|
1218 |
+
{
|
1219 |
+
$el = $this->root->find('meta[http-equiv=Content-Type]',0, true);
|
1220 |
+
if (!empty($el))
|
1221 |
+
{
|
1222 |
+
$fullvalue = $el->content;
|
1223 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag found' . $fullvalue);}
|
1224 |
+
|
1225 |
+
if (!empty($fullvalue))
|
1226 |
+
{
|
1227 |
+
$success = preg_match('/charset=(.+)/i', $fullvalue, $matches);
|
1228 |
+
if ($success)
|
1229 |
+
{
|
1230 |
+
$charset = $matches[1];
|
1231 |
+
}
|
1232 |
+
else
|
1233 |
+
{
|
1234 |
+
// If there is a meta tag, and they don't specify the character set, research says that it's typically ISO-8859-1
|
1235 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'meta content-type tag couldn\'t be parsed. using iso-8859 default.');}
|
1236 |
+
$charset = 'ISO-8859-1';
|
1237 |
+
}
|
1238 |
+
}
|
1239 |
+
}
|
1240 |
+
}
|
1241 |
+
|
1242 |
+
// If we couldn't find a charset above, then lets try to detect one based on the text we got...
|
1243 |
+
if (empty($charset))
|
1244 |
+
{
|
1245 |
+
// Use this in case mb_detect_charset isn't installed/loaded on this machine.
|
1246 |
+
$charset = false;
|
1247 |
+
if (function_exists('mb_detect_encoding'))
|
1248 |
+
{
|
1249 |
+
// Have php try to detect the encoding from the text given to us.
|
1250 |
+
$charset = mb_detect_encoding($this->root->plaintext . "ascii", $encoding_list = array( "UTF-8", "CP1252" ) );
|
1251 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'mb_detect found: ' . $charset);}
|
1252 |
+
}
|
1253 |
+
|
1254 |
+
// and if this doesn't work... then we need to just wrongheadedly assume it's UTF-8 so that we can move on - cause this will usually give us most of what we need...
|
1255 |
+
if ($charset === false)
|
1256 |
+
{
|
1257 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'since mb_detect failed - using default of utf-8');}
|
1258 |
+
$charset = 'UTF-8';
|
1259 |
+
}
|
1260 |
+
}
|
1261 |
+
|
1262 |
+
// Since CP1252 is a superset, if we get one of it's subsets, we want it instead.
|
1263 |
+
if ((strtolower($charset) == strtolower('ISO-8859-1')) || (strtolower($charset) == strtolower('Latin1')) || (strtolower($charset) == strtolower('Latin-1')))
|
1264 |
+
{
|
1265 |
+
if (is_object($debug_object)) {$debug_object->debug_log(2, 'replacing ' . $charset . ' with CP1252 as its a superset');}
|
1266 |
+
$charset = 'CP1252';
|
1267 |
+
}
|
1268 |
+
|
1269 |
+
if (is_object($debug_object)) {$debug_object->debug_log(1, 'EXIT - ' . $charset);}
|
1270 |
+
|
1271 |
+
return $this->_charset = $charset;
|
1272 |
+
}
|
1273 |
+
|
1274 |
+
// read tag info
|
1275 |
+
protected function read_tag()
|
1276 |
+
{
|
1277 |
+
if ($this->char!=='<')
|
1278 |
+
{
|
1279 |
+
$this->root->_[HDOM_INFO_END] = $this->cursor;
|
1280 |
+
return false;
|
1281 |
+
}
|
1282 |
+
$begin_tag_pos = $this->pos;
|
1283 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1284 |
+
|
1285 |
+
// end tag
|
1286 |
+
if ($this->char==='/')
|
1287 |
+
{
|
1288 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1289 |
+
// This represents the change in the simple_html_dom trunk from revision 180 to 181.
|
1290 |
+
// $this->skip($this->token_blank_t);
|
1291 |
+
$this->skip($this->token_blank);
|
1292 |
+
$tag = $this->copy_until_char('>');
|
1293 |
+
|
1294 |
+
// skip attributes in end tag
|
1295 |
+
if (($pos = strpos($tag, ' '))!==false)
|
1296 |
+
$tag = substr($tag, 0, $pos);
|
1297 |
+
|
1298 |
+
$parent_lower = strtolower($this->parent->tag);
|
1299 |
+
$tag_lower = strtolower($tag);
|
1300 |
+
|
1301 |
+
if ($parent_lower!==$tag_lower)
|
1302 |
+
{
|
1303 |
+
if (isset($this->optional_closing_tags[$parent_lower]) && isset($this->block_tags[$tag_lower]))
|
1304 |
+
{
|
1305 |
+
$this->parent->_[HDOM_INFO_END] = 0;
|
1306 |
+
$org_parent = $this->parent;
|
1307 |
+
|
1308 |
+
while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)
|
1309 |
+
$this->parent = $this->parent->parent;
|
1310 |
+
|
1311 |
+
if (strtolower($this->parent->tag)!==$tag_lower) {
|
1312 |
+
$this->parent = $org_parent; // restore origonal parent
|
1313 |
+
if ($this->parent->parent) $this->parent = $this->parent->parent;
|
1314 |
+
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
1315 |
+
return $this->as_text_node($tag);
|
1316 |
+
}
|
1317 |
+
}
|
1318 |
+
else if (($this->parent->parent) && isset($this->block_tags[$tag_lower]))
|
1319 |
+
{
|
1320 |
+
$this->parent->_[HDOM_INFO_END] = 0;
|
1321 |
+
$org_parent = $this->parent;
|
1322 |
+
|
1323 |
+
while (($this->parent->parent) && strtolower($this->parent->tag)!==$tag_lower)
|
1324 |
+
$this->parent = $this->parent->parent;
|
1325 |
+
|
1326 |
+
if (strtolower($this->parent->tag)!==$tag_lower)
|
1327 |
+
{
|
1328 |
+
$this->parent = $org_parent; // restore origonal parent
|
1329 |
+
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
1330 |
+
return $this->as_text_node($tag);
|
1331 |
+
}
|
1332 |
+
}
|
1333 |
+
else if (($this->parent->parent) && strtolower($this->parent->parent->tag)===$tag_lower)
|
1334 |
+
{
|
1335 |
+
$this->parent->_[HDOM_INFO_END] = 0;
|
1336 |
+
$this->parent = $this->parent->parent;
|
1337 |
+
}
|
1338 |
+
else
|
1339 |
+
return $this->as_text_node($tag);
|
1340 |
+
}
|
1341 |
+
|
1342 |
+
$this->parent->_[HDOM_INFO_END] = $this->cursor;
|
1343 |
+
if ($this->parent->parent) $this->parent = $this->parent->parent;
|
1344 |
+
|
1345 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1346 |
+
return true;
|
1347 |
+
}
|
1348 |
+
|
1349 |
+
$node = new simple_html_dom_node($this);
|
1350 |
+
$node->_[HDOM_INFO_BEGIN] = $this->cursor;
|
1351 |
+
++$this->cursor;
|
1352 |
+
$tag = $this->copy_until($this->token_slash);
|
1353 |
+
$node->tag_start = $begin_tag_pos;
|
1354 |
+
|
1355 |
+
// doctype, cdata & comments...
|
1356 |
+
if (isset($tag[0]) && $tag[0]==='!') {
|
1357 |
+
$node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until_char('>');
|
1358 |
+
|
1359 |
+
if (isset($tag[2]) && $tag[1]==='-' && $tag[2]==='-') {
|
1360 |
+
$node->nodetype = HDOM_TYPE_COMMENT;
|
1361 |
+
$node->tag = 'comment';
|
1362 |
+
} else {
|
1363 |
+
$node->nodetype = HDOM_TYPE_UNKNOWN;
|
1364 |
+
$node->tag = 'unknown';
|
1365 |
+
}
|
1366 |
+
if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';
|
1367 |
+
$this->link_nodes($node, true);
|
1368 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1369 |
+
return true;
|
1370 |
+
}
|
1371 |
+
|
1372 |
+
// text
|
1373 |
+
if ($pos=strpos($tag, '<')!==false) {
|
1374 |
+
$tag = '<' . substr($tag, 0, -1);
|
1375 |
+
$node->_[HDOM_INFO_TEXT] = $tag;
|
1376 |
+
$this->link_nodes($node, false);
|
1377 |
+
$this->char = $this->doc[--$this->pos]; // prev
|
1378 |
+
return true;
|
1379 |
+
}
|
1380 |
+
|
1381 |
+
if (!preg_match("/^[\w-:]+$/", $tag)) {
|
1382 |
+
$node->_[HDOM_INFO_TEXT] = '<' . $tag . $this->copy_until('<>');
|
1383 |
+
if ($this->char==='<') {
|
1384 |
+
$this->link_nodes($node, false);
|
1385 |
+
return true;
|
1386 |
+
}
|
1387 |
+
|
1388 |
+
if ($this->char==='>') $node->_[HDOM_INFO_TEXT].='>';
|
1389 |
+
$this->link_nodes($node, false);
|
1390 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1391 |
+
return true;
|
1392 |
+
}
|
1393 |
+
|
1394 |
+
// begin tag
|
1395 |
+
$node->nodetype = HDOM_TYPE_ELEMENT;
|
1396 |
+
$tag_lower = strtolower($tag);
|
1397 |
+
$node->tag = ($this->lowercase) ? $tag_lower : $tag;
|
1398 |
+
|
1399 |
+
// handle optional closing tags
|
1400 |
+
if (isset($this->optional_closing_tags[$tag_lower]) )
|
1401 |
+
{
|
1402 |
+
while (isset($this->optional_closing_tags[$tag_lower][strtolower($this->parent->tag)]))
|
1403 |
+
{
|
1404 |
+
$this->parent->_[HDOM_INFO_END] = 0;
|
1405 |
+
$this->parent = $this->parent->parent;
|
1406 |
+
}
|
1407 |
+
$node->parent = $this->parent;
|
1408 |
+
}
|
1409 |
+
|
1410 |
+
$guard = 0; // prevent infinity loop
|
1411 |
+
$space = array($this->copy_skip($this->token_blank), '', '');
|
1412 |
+
|
1413 |
+
// attributes
|
1414 |
+
do
|
1415 |
+
{
|
1416 |
+
if ($this->char!==null && $space[0]==='')
|
1417 |
+
{
|
1418 |
+
break;
|
1419 |
+
}
|
1420 |
+
$name = $this->copy_until($this->token_equal);
|
1421 |
+
if ($guard===$this->pos)
|
1422 |
+
{
|
1423 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1424 |
+
continue;
|
1425 |
+
}
|
1426 |
+
$guard = $this->pos;
|
1427 |
+
|
1428 |
+
// handle endless '<'
|
1429 |
+
if ($this->pos>=$this->size-1 && $this->char!=='>') {
|
1430 |
+
$node->nodetype = HDOM_TYPE_TEXT;
|
1431 |
+
$node->_[HDOM_INFO_END] = 0;
|
1432 |
+
$node->_[HDOM_INFO_TEXT] = '<'.$tag . $space[0] . $name;
|
1433 |
+
$node->tag = 'text';
|
1434 |
+
$this->link_nodes($node, false);
|
1435 |
+
return true;
|
1436 |
+
}
|
1437 |
+
|
1438 |
+
// handle mismatch '<'
|
1439 |
+
if ($this->doc[$this->pos-1]=='<') {
|
1440 |
+
$node->nodetype = HDOM_TYPE_TEXT;
|
1441 |
+
$node->tag = 'text';
|
1442 |
+
$node->attr = array();
|
1443 |
+
$node->_[HDOM_INFO_END] = 0;
|
1444 |
+
$node->_[HDOM_INFO_TEXT] = substr($this->doc, $begin_tag_pos, $this->pos-$begin_tag_pos-1);
|
1445 |
+
$this->pos -= 2;
|
1446 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1447 |
+
$this->link_nodes($node, false);
|
1448 |
+
return true;
|
1449 |
+
}
|
1450 |
+
|
1451 |
+
if ($name!=='/' && $name!=='') {
|
1452 |
+
$space[1] = $this->copy_skip($this->token_blank);
|
1453 |
+
$name = $this->restore_noise($name);
|
1454 |
+
if ($this->lowercase) $name = strtolower($name);
|
1455 |
+
if ($this->char==='=') {
|
1456 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1457 |
+
$this->parse_attr($node, $name, $space);
|
1458 |
+
}
|
1459 |
+
else {
|
1460 |
+
//no value attr: nowrap, checked selected...
|
1461 |
+
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
|
1462 |
+
$node->attr[$name] = true;
|
1463 |
+
if ($this->char!='>') $this->char = $this->doc[--$this->pos]; // prev
|
1464 |
+
}
|
1465 |
+
$node->_[HDOM_INFO_SPACE][] = $space;
|
1466 |
+
$space = array($this->copy_skip($this->token_blank), '', '');
|
1467 |
+
}
|
1468 |
+
else
|
1469 |
+
break;
|
1470 |
+
} while ($this->char!=='>' && $this->char!=='/');
|
1471 |
+
|
1472 |
+
$this->link_nodes($node, true);
|
1473 |
+
$node->_[HDOM_INFO_ENDSPACE] = $space[0];
|
1474 |
+
|
1475 |
+
// check self closing
|
1476 |
+
if ($this->copy_until_char_escape('>')==='/')
|
1477 |
+
{
|
1478 |
+
$node->_[HDOM_INFO_ENDSPACE] .= '/';
|
1479 |
+
$node->_[HDOM_INFO_END] = 0;
|
1480 |
+
}
|
1481 |
+
else
|
1482 |
+
{
|
1483 |
+
// reset parent
|
1484 |
+
if (!isset($this->self_closing_tags[strtolower($node->tag)])) $this->parent = $node;
|
1485 |
+
}
|
1486 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1487 |
+
|
1488 |
+
// If it's a BR tag, we need to set it's text to the default text.
|
1489 |
+
// This way when we see it in plaintext, we can generate formatting that the user wants.
|
1490 |
+
// since a br tag never has sub nodes, this works well.
|
1491 |
+
if ($node->tag == "br")
|
1492 |
+
{
|
1493 |
+
$node->_[HDOM_INFO_INNER] = $this->default_br_text;
|
1494 |
+
}
|
1495 |
+
|
1496 |
+
return true;
|
1497 |
+
}
|
1498 |
+
|
1499 |
+
// parse attributes
|
1500 |
+
protected function parse_attr($node, $name, &$space)
|
1501 |
+
{
|
1502 |
+
// Per sourceforge: http://sourceforge.net/tracker/?func=detail&aid=3061408&group_id=218559&atid=1044037
|
1503 |
+
// If the attribute is already defined inside a tag, only pay atetntion to the first one as opposed to the last one.
|
1504 |
+
if (isset($node->attr[$name]))
|
1505 |
+
{
|
1506 |
+
return;
|
1507 |
+
}
|
1508 |
+
|
1509 |
+
$space[2] = $this->copy_skip($this->token_blank);
|
1510 |
+
switch ($this->char) {
|
1511 |
+
case '"':
|
1512 |
+
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
|
1513 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1514 |
+
$node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('"'));
|
1515 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1516 |
+
break;
|
1517 |
+
case '\'':
|
1518 |
+
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;
|
1519 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1520 |
+
$node->attr[$name] = $this->restore_noise($this->copy_until_char_escape('\''));
|
1521 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1522 |
+
break;
|
1523 |
+
default:
|
1524 |
+
$node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
|
1525 |
+
$node->attr[$name] = $this->restore_noise($this->copy_until($this->token_attr));
|
1526 |
+
}
|
1527 |
+
// PaperG: Attributes should not have \r or \n in them, that counts as html whitespace.
|
1528 |
+
$node->attr[$name] = str_replace("\r", "", $node->attr[$name]);
|
1529 |
+
$node->attr[$name] = str_replace("\n", "", $node->attr[$name]);
|
1530 |
+
// PaperG: If this is a "class" selector, lets get rid of the preceeding and trailing space since some people leave it in the multi class case.
|
1531 |
+
if ($name == "class") {
|
1532 |
+
$node->attr[$name] = trim($node->attr[$name]);
|
1533 |
+
}
|
1534 |
+
}
|
1535 |
+
|
1536 |
+
// link node's parent
|
1537 |
+
protected function link_nodes(&$node, $is_child)
|
1538 |
+
{
|
1539 |
+
$node->parent = $this->parent;
|
1540 |
+
$this->parent->nodes[] = $node;
|
1541 |
+
if ($is_child)
|
1542 |
+
{
|
1543 |
+
$this->parent->children[] = $node;
|
1544 |
+
}
|
1545 |
+
}
|
1546 |
+
|
1547 |
+
// as a text node
|
1548 |
+
protected function as_text_node($tag)
|
1549 |
+
{
|
1550 |
+
$node = new simple_html_dom_node($this);
|
1551 |
+
++$this->cursor;
|
1552 |
+
$node->_[HDOM_INFO_TEXT] = '</' . $tag . '>';
|
1553 |
+
$this->link_nodes($node, false);
|
1554 |
+
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1555 |
+
return true;
|
1556 |
+
}
|
1557 |
+
|
1558 |
+
protected function skip($chars)
|
1559 |
+
{
|
1560 |
+
$this->pos += strspn($this->doc, $chars, $this->pos);
|
1561 |
+
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1562 |
+
}
|
1563 |
+
|
1564 |
+
protected function copy_skip($chars)
|
1565 |
+
{
|
1566 |
+
$pos = $this->pos;
|
1567 |
+
$len = strspn($this->doc, $chars, $pos);
|
1568 |
+
$this->pos += $len;
|
1569 |
+
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1570 |
+
if ($len===0) return '';
|
1571 |
+
return substr($this->doc, $pos, $len);
|
1572 |
+
}
|
1573 |
+
|
1574 |
+
protected function copy_until($chars)
|
1575 |
+
{
|
1576 |
+
$pos = $this->pos;
|
1577 |
+
$len = strcspn($this->doc, $chars, $pos);
|
1578 |
+
$this->pos += $len;
|
1579 |
+
$this->char = ($this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
|
1580 |
+
return substr($this->doc, $pos, $len);
|
1581 |
+
}
|
1582 |
+
|
1583 |
+
protected function copy_until_char($char)
|
1584 |
+
{
|
1585 |
+
if ($this->char===null) return '';
|
1586 |
+
|
1587 |
+
if (($pos = strpos($this->doc, $char, $this->pos))===false) {
|
1588 |
+
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
|
1589 |
+
$this->char = null;
|
1590 |
+
$this->pos = $this->size;
|
1591 |
+
return $ret;
|
1592 |
+
}
|
1593 |
+
|
1594 |
+
if ($pos===$this->pos) return '';
|
1595 |
+
$pos_old = $this->pos;
|
1596 |
+
$this->char = $this->doc[$pos];
|
1597 |
+
$this->pos = $pos;
|
1598 |
+
return substr($this->doc, $pos_old, $pos-$pos_old);
|
1599 |
+
}
|
1600 |
+
|
1601 |
+
protected function copy_until_char_escape($char)
|
1602 |
+
{
|
1603 |
+
if ($this->char===null) return '';
|
1604 |
+
|
1605 |
+
$start = $this->pos;
|
1606 |
+
while (1)
|
1607 |
+
{
|
1608 |
+
if (($pos = strpos($this->doc, $char, $start))===false)
|
1609 |
+
{
|
1610 |
+
$ret = substr($this->doc, $this->pos, $this->size-$this->pos);
|
1611 |
+
$this->char = null;
|
1612 |
+
$this->pos = $this->size;
|
1613 |
+
return $ret;
|
1614 |
+
}
|
1615 |
+
|
1616 |
+
if ($pos===$this->pos) return '';
|
1617 |
+
|
1618 |
+
if ($this->doc[$pos-1]==='\\') {
|
1619 |
+
$start = $pos+1;
|
1620 |
+
continue;
|
1621 |
+
}
|
1622 |
+
|
1623 |
+
$pos_old = $this->pos;
|
1624 |
+
$this->char = $this->doc[$pos];
|
1625 |
+
$this->pos = $pos;
|
1626 |
+
return substr($this->doc, $pos_old, $pos-$pos_old);
|
1627 |
+
}
|
1628 |
+
}
|
1629 |
+
|
1630 |
+
// remove noise from html content
|
1631 |
+
// save the noise in the $this->noise array.
|
1632 |
+
protected function remove_noise($pattern, $remove_tag=false)
|
1633 |
+
{
|
1634 |
+
global $debug_object;
|
1635 |
+
if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }
|
1636 |
+
|
1637 |
+
$count = preg_match_all($pattern, $this->doc, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
|
1638 |
+
|
1639 |
+
for ($i=$count-1; $i>-1; --$i)
|
1640 |
+
{
|
1641 |
+
$key = '___noise___'.sprintf('% 5d', count($this->noise)+1000);
|
1642 |
+
if (is_object($debug_object)) { $debug_object->debug_log(2, 'key is: ' . $key); }
|
1643 |
+
$idx = ($remove_tag) ? 0 : 1;
|
1644 |
+
$this->noise[$key] = $matches[$i][$idx][0];
|
1645 |
+
$this->doc = substr_replace($this->doc, $key, $matches[$i][$idx][1], strlen($matches[$i][$idx][0]));
|
1646 |
+
}
|
1647 |
+
|
1648 |
+
// reset the length of content
|
1649 |
+
$this->size = strlen($this->doc);
|
1650 |
+
if ($this->size>0)
|
1651 |
+
{
|
1652 |
+
$this->char = $this->doc[0];
|
1653 |
+
}
|
1654 |
+
}
|
1655 |
+
|
1656 |
+
// restore noise to html content
|
1657 |
+
function restore_noise($text)
|
1658 |
+
{
|
1659 |
+
global $debug_object;
|
1660 |
+
if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }
|
1661 |
+
|
1662 |
+
while (($pos=strpos($text, '___noise___'))!==false)
|
1663 |
+
{
|
1664 |
+
// Sometimes there is a broken piece of markup, and we don't GET the pos+11 etc... token which indicates a problem outside of us...
|
1665 |
+
if (strlen($text) > $pos+15)
|
1666 |
+
{
|
1667 |
+
$key = '___noise___'.$text[$pos+11].$text[$pos+12].$text[$pos+13].$text[$pos+14].$text[$pos+15];
|
1668 |
+
if (is_object($debug_object)) { $debug_object->debug_log(2, 'located key of: ' . $key); }
|
1669 |
+
|
1670 |
+
if (isset($this->noise[$key]))
|
1671 |
+
{
|
1672 |
+
$text = substr($text, 0, $pos).$this->noise[$key].substr($text, $pos+16);
|
1673 |
+
}
|
1674 |
+
else
|
1675 |
+
{
|
1676 |
+
// do this to prevent an infinite loop.
|
1677 |
+
$text = substr($text, 0, $pos).'UNDEFINED NOISE FOR KEY: '.$key . substr($text, $pos+16);
|
1678 |
+
}
|
1679 |
+
}
|
1680 |
+
else
|
1681 |
+
{
|
1682 |
+
// There is no valid key being given back to us... We must get rid of the ___noise___ or we will have a problem.
|
1683 |
+
$text = substr($text, 0, $pos).'NO NUMERIC NOISE KEY' . substr($text, $pos+11);
|
1684 |
+
}
|
1685 |
+
}
|
1686 |
+
return $text;
|
1687 |
+
}
|
1688 |
+
|
1689 |
+
// Sometimes we NEED one of the noise elements.
|
1690 |
+
function search_noise($text)
|
1691 |
+
{
|
1692 |
+
global $debug_object;
|
1693 |
+
if (is_object($debug_object)) { $debug_object->debug_log_entry(1); }
|
1694 |
+
|
1695 |
+
foreach($this->noise as $noiseElement)
|
1696 |
+
{
|
1697 |
+
if (strpos($noiseElement, $text)!==false)
|
1698 |
+
{
|
1699 |
+
return $noiseElement;
|
1700 |
+
}
|
1701 |
+
}
|
1702 |
+
}
|
1703 |
+
function __toString()
|
1704 |
+
{
|
1705 |
+
return $this->root->innertext();
|
1706 |
+
}
|
1707 |
+
|
1708 |
+
function __get($name)
|
1709 |
+
{
|
1710 |
+
switch ($name)
|
1711 |
+
{
|
1712 |
+
case 'outer text':
|
1713 |
+
return $this->root->innertext();
|
1714 |
+
case 'inner text':
|
1715 |
+
return $this->root->innertext();
|
1716 |
+
case 'plaintext':
|
1717 |
+
return $this->root->text();
|
1718 |
+
case 'charset':
|
1719 |
+
return $this->_charset;
|
1720 |
+
case 'target_charset':
|
1721 |
+
return $this->_target_charset;
|
1722 |
+
}
|
1723 |
+
}
|
1724 |
+
|
1725 |
+
// camel naming conventions
|
1726 |
+
function childNodes($idx=-1) {return $this->root->childNodes($idx);}
|
1727 |
+
function firstChild() {return $this->root->first_child();}
|
1728 |
+
function lastChild() {return $this->root->last_child();}
|
1729 |
+
function createElement($name, $value=null) {return @str_get_html("<$name>$value</$name>")->first_child();}
|
1730 |
+
function createTextNode($value) {return @end(str_get_html($value)->nodes);}
|
1731 |
+
function getElementById($id) {return $this->find("#$id", 0);}
|
1732 |
+
function getElementsById($id, $idx=null) {return $this->find("#$id", $idx);}
|
1733 |
+
function getElementByTagName($name) {return $this->find($name, 0);}
|
1734 |
+
function getElementsByTagName($name, $idx=-1) {return $this->find($name, $idx);}
|
1735 |
+
function loadFile() {$args = func_get_args();$this->load_file($args);}
|
1736 |
+
}
|
1737 |
+
|
1738 |
+
?>
|