Initial commit of 0.2.0
This commit is contained in:
154
inc/global.php
Normal file
154
inc/global.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* @package ogre-obfuscation
|
||||
* @author cleverogre
|
||||
* @version 0.2.0
|
||||
* @since 0.1.0
|
||||
*/
|
||||
|
||||
namespace Ogre\Obfuscation;
|
||||
|
||||
use simplehtmldom\HtmlDocument;
|
||||
use function is_user_logged_in;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
function email_enabled() {
|
||||
if (is_user_logged_in()) return false;
|
||||
|
||||
if (Settings::instance()->email_enabled != '1') return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function obfuscate($str) {
|
||||
static $obfuscate_id = 0;
|
||||
|
||||
$out = '';
|
||||
for ($i = 0; $i < strlen($str); $i++) {
|
||||
$out .= '&#' . ord(substr($str, $i, 1)) . ';';
|
||||
}
|
||||
|
||||
$out2 = '';
|
||||
for ($i = 0; $i < strlen($out); $i++) {
|
||||
$out2 = substr($out, $i, 1) . $out2;
|
||||
}
|
||||
|
||||
$output = "<span class=\"obfuscate\" id=\"obfuscate-{$obfuscate_id}\" data-content=\"{$out2}\"></span>";
|
||||
$obfuscate_id++;
|
||||
return $output;
|
||||
}
|
||||
|
||||
function _obfuscate_element($element) {
|
||||
return obfuscate($element->outertext);
|
||||
}
|
||||
|
||||
function find_emails($str) {
|
||||
preg_match_all('/[a-zA-Z0-9_\-\+\.]+@[a-zA-Z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i', $str, $matches, PREG_OFFSET_CAPTURE);
|
||||
if (!is_array($matches) || empty($matches)) return false;
|
||||
|
||||
$return = array();
|
||||
foreach ($matches[0] as $match) {
|
||||
$return[] = array(
|
||||
'text' => $match[0],
|
||||
'pos' => $match[1],
|
||||
'len' => strlen($match[0]),
|
||||
);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
function filter($content) {
|
||||
// Parse content
|
||||
$html = new HtmlDocument();
|
||||
$html->load('<div id="dom_root">' . $content . '</div>');
|
||||
|
||||
//_filter_html($html->find('div[id=dom_root]', 0));
|
||||
|
||||
$html->find('div[id=dom_root]', 0)->outertext = _filter_html($html->find('div[id=dom_root]', 0));
|
||||
$html->find('div[id=dom_root]', 0)->outertext = $html->find('div[id=dom_root]', 0)->innertext; // remove root div wrapper
|
||||
|
||||
$content = $html->save();
|
||||
|
||||
// Clean up memory
|
||||
$html->clear();
|
||||
unset($html);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
function _filter_html($element) {
|
||||
if ($element == null) return false;
|
||||
|
||||
switch ($element->tag) {
|
||||
case 'a':
|
||||
if (_test_anchor($element)) {
|
||||
return _obfuscate_element($element);
|
||||
} else {
|
||||
return $element->outertext;
|
||||
}
|
||||
default:
|
||||
$node = _get_text_node($element);
|
||||
$node['text'] = _filter_text($node['text']);
|
||||
|
||||
foreach ($node['tags'] as &$tag) {
|
||||
$tag['content'] = _filter_html($tag['element']);
|
||||
}
|
||||
|
||||
$element->outertext = _set_text_node($node);
|
||||
return $element->outertext;
|
||||
}
|
||||
}
|
||||
|
||||
function _filter_text($text) {
|
||||
$emails = find_emails($text);
|
||||
if (!$emails) return $text;
|
||||
|
||||
for ($i = count($emails) - 1; $i >= 0; $i--) {
|
||||
$text = substr($text, 0, $emails[$i]['pos']) . obfuscate($emails[$i]['text']) . substr($text, $emails[$i]['pos'] + $emails[$i]['len']);
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
function _get_text_node($element) {
|
||||
//$tag_pattern = '/<\s*[a-zA-Z]+[^>]*>.*?<\s*\/\s*[a-zA-Z]+>/is'
|
||||
|
||||
$text = $element->outertext;
|
||||
$return = array(
|
||||
'raw' => $text,
|
||||
'text' => '',
|
||||
'tags' => array(),
|
||||
);
|
||||
|
||||
$i = 0;
|
||||
foreach ($element->children() as $child) {
|
||||
$return['tags'][$i] = array(
|
||||
'element' => $child,
|
||||
'index' => $i,
|
||||
'key' => "[[{$i}]]",
|
||||
'content' => $child->outertext,
|
||||
'pos' => strpos($text, $child->outertext),
|
||||
'len' => strlen($child->outertext),
|
||||
);
|
||||
$text = str_replace($child->outertext, "[[{$i}]]", $text);
|
||||
$i++;
|
||||
}
|
||||
|
||||
$return['text'] = $text;
|
||||
return $return;
|
||||
}
|
||||
|
||||
function _set_text_node($node) {
|
||||
$text = $node['text'];
|
||||
foreach ($node['tags'] as $tag) {
|
||||
$text = str_replace($tag['key'], $tag['content'], $text);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
|
||||
function _test_anchor($element) {
|
||||
if ($element->tag != 'a') return false;
|
||||
return preg_match('/<a.*>[a-zA-Z0-9_\-\+\.]+@[a-zA-Z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?<\/a>/i', $element->outertext) == 1;
|
||||
}
|
||||
Reference in New Issue
Block a user