setTimezone(new DateTimeZone($timeZone));
$isTrial = $trailDateUtc->format('Y-m-d H:i:s');
$differTime = strtotime($isTrial) - time();
if ((int) $differTime >= 0) {
return (int) ($differTime / 86400);
}
return 0;
}
return false;
}
public static function scrapeUrl($url = '') {
if (!empty($url)) {
$param = array();
libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings
$url = $url . ((parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'no_cache=1'); //nocache
$html = self::b2sFileGetContents($url, true);
if (!empty($html) && $html !== false) {
//Search rist Parameter
$data = self::b2sGetAllTags($html, 'all', false);
if (is_array($data) && !empty($data)) {
return $data;
}
}
return false;
}
}
public static function getMetaTags($postId = 0, $postUrl = '', $network = 1) {
$type = ($network == 2) ? 'twitter' : 'og';
$search = ($network == 2) ? 'name' : 'property';
//GETSTOREEDDATA
if ((int) $postId != 0) {
$metaData = get_option('B2S_PLUGIN_POST_META_TAGES_' . strtoupper($type) . '_' . $postId);
if ($metaData !== false && is_array($metaData)) {
return $metaData;
}
}
//GETDATA
$getTags = array('title', 'description', 'image');
$param = array();
libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings
$postUrl = $postUrl . ((parse_url($postUrl, PHP_URL_QUERY) ? '&' : '?') . 'no_cache=1'); //nocache
$html = self::b2sFileGetContents($postUrl);
if (!empty($html) && $html !== false) {
//Search rist OG Parameter
$temp = self::b2sGetAllTags($html, $type, false, $search);
foreach ($getTags as $k => $v) {
if (isset($temp[$v]) && !empty($temp[$v])) {
$param[$v] = $temp[$v];
} else {
if ($v == 'title') {
if (function_exists('mb_convert_encoding')) {
$param[$v] = htmlspecialchars(self::b2sGetMetaTitle($html));
} else {
$param[$v] = self::b2sGetMetaTitle($html);
}
}
if ($v == 'description') {
if (function_exists('mb_convert_encoding')) {
$param[$v] = htmlspecialchars(self::b2sGetMetaDescription($html));
} else {
$param[$v] = self::b2sGetMetaDescription($html);
}
}
}
}
//STOREDATA
if ((int) $postId != 0) {
update_option('B2S_PLUGIN_POST_META_TAGES_' . strtoupper($type) . '_' . $postId, $param);
}
return $param;
}
return false;
}
private static function b2sFileGetContents($url, $extern = false) {
$args = array(
'timeout' => '20',
'redirection' => '5',
'user-agent' => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0"
);
$response = wp_remote_get($url, $args);
if (!is_wp_error($response)) {
return wp_remote_retrieve_body($response);
} else if ($extern) {
$res = json_decode(B2S_Api_Get::get(B2S_PLUGIN_API_ENDPOINT . 'get.php?action=scrapeUrl&url=' . urlencode($url)));
if (isset($res->data) && !empty($res->data)) {
return $res->data;
}
}
return false;
}
private static function b2sGetMetaDescription($html) {
//$res = get_meta_tags($url);
//return (isset($res['description']) ? self::cleanContent(strip_shortcodes($res['description'])) : '');
$res = preg_match('#]*content=[\"\']?(.*?)[\"\']? */?>#i', $html, $matches);
return (isset($matches[1]) && !empty($matches[1])) ? trim(preg_replace('/\s+/', ' ', $matches[1])) : '';
}
private static function b2sGetMetaTitle($html) {
$res = preg_match("/
(.*)<\/title>/siU", $html, $matches);
return (isset($matches[1]) && !empty($matches[1])) ? trim(preg_replace('/\s+/', ' ', $matches[1])) : '';
}
private static function b2sGetAllTags($html, $type = 'og', $ignoreEncoding = false, $search = 'property') {
$list = array();
$doc = new DOMDocument();
if (function_exists('mb_convert_encoding') && !$ignoreEncoding) {
@$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
} else {
@$doc->loadHTML($html);
}
$metas = $doc->getElementsByTagName('meta');
$title = $doc->getElementsByTagName("title");
if ($type == 'all') {
if ($title->length > 0) {
if ($title->item(0)->nodeValue != "") {
$list['default_title'] = $title->item(0)->nodeValue;
}
}
}
for ($i = 0; $i < $metas->length; $i++) {
$meta = $metas->item($i);
if ($type != 'all') {
if (($meta->getAttribute('property') == $type . ':title' || $meta->getAttribute('name') == $type . ':title') && !isset($list['title'])) {
$list['title'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
if (($meta->getAttribute('property') == $type . ':description' || $meta->getAttribute('name') == $type . ':description') && !isset($list['description'])) {
$desc = self::cleanContent(strip_shortcodes($meta->getAttribute('content')));
$list['description'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($desc) : $desc);
}
if (($meta->getAttribute('property') == $type . ':image' || $meta->getAttribute('name') == $type . ':image') && !isset($list['image'])) {
$list['image'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
} else {
if ($meta->getAttribute('name') == 'description' && !isset($list['default_description'])) {
$list['default_description'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
if ($meta->getAttribute($search) == 'og:title' && !isset($list['og_title'])) {
$list['og_title'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
if ($meta->getAttribute($search) == 'og:description' && !isset($list['og_description'])) {
$desc = self::cleanContent(strip_shortcodes($meta->getAttribute('content')));
$list['og_description'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($desc) : $desc);
}
if ($meta->getAttribute($search) == 'og:image' && !isset($list['og_image'])) {
$list['og_image'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
//Further
/* if ($meta->getAttribute($search) == 'twitter:title' && !isset($list['twitter_title'])) {
$list['twitter_title'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
}
if ($meta->getAttribute($search) == 'twitter:description' && !isset($list['twitter_description'])) {
$desc = self::cleanContent(strip_shortcodes($meta->getAttribute('content')));
$list['twitter_description'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($desc) : $desc);
}
if ($meta->getAttribute($search) == 'twitter:image' && !isset($list['twitter_image'])) {
$list['twitter_image'] = (function_exists('mb_convert_encoding') ? htmlspecialchars($meta->getAttribute('content')) : $meta->getAttribute('content'));
} */
}
}
return $list;
}
public static function getImagesByPostId($postId = 0, $postContent = '', $postUrl = '', $network = false, $postLang = 'en') {
$homeUrl = get_site_url();
$scheme = parse_url($homeUrl, PHP_URL_SCHEME);
$featuredImage = wp_get_attachment_url(get_post_thumbnail_id($postId));
$content = self::getFullContent($postId, $postContent, $postUrl, $postLang);
$matches = array();
if (!preg_match_all('%%', $content, $matches) && !$featuredImage) {
return false;
}
array_unshift($matches[1], $featuredImage);
$rtrnArray = array();
foreach ($matches[1] as $key => $imgUrl) {
if ($imgUrl == false) {
continue;
}
//AllowedExtensions?
if (!$network && !in_array(substr($imgUrl, strrpos($imgUrl, '.')), array('.jpg', '.png'))) {
continue;
}
//isRelativ?
if (!preg_match('/((http|https):\/\/|(www.))/', $imgUrl)) {
//StartWith //
if ((substr($imgUrl, 0, 2) == '//')) {
$imgUrl = (($scheme != NULL) ? $scheme : 'http') . ':' . $imgUrl;
} else {
//StartWith /
$imgUrl = (substr($imgUrl, 0, 1) != '/') ? '/' . $imgUrl : $imgUrl;
$imgUrl = str_replace('//', '/', $imgUrl);
$imgUrl = $homeUrl . $imgUrl;
if (strpos($imgUrl, 'http://') === false && strpos($imgUrl, 'https://') === false) {
$imgUrl = (($scheme != NULL) ? $scheme : 'http') . '://' . $imgUrl;
}
}
}
/* $file_headers = @get_headers($imgUrl);
if ((!is_array($file_headers)) || (is_array($file_headers) && !preg_match('/200/', $file_headers[0]))) {
continue;
} */
$rtrnArray[$key][0] = urldecode($imgUrl);
}
return $rtrnArray;
}
public static function prepareContent($postId = 0, $postContent = '', $postUrl = '', $allowHtml = '