function import($feedObj, $maxItems = 0) { jimport('simplepie.simplepie'); $config = EasyBlogHelper::getConfig(); $itemMigrated = 0; $isDomSupported = false; $defaultAllowedHTML = '<img>,<a>,<br>,<table>,<tbody>,<th>,<tr>,<td>,<div>,<span>,<p>,<h1>,<h2>,<h3>,<h4>,<h5>,<h6>'; if (class_exists('DomDocument')) { $isDomSupported = true; require_once EBLOG_CLASSES . DIRECTORY_SEPARATOR . 'readability' . DIRECTORY_SEPARATOR . 'Readability.php'; } $params = EasyBlogHelper::getRegistry($feedObj->params); $maxItems = $maxItems ? $maxItems : $params->get('feedamount', 0); $feedURL = $feedObj->url; require_once EBLOG_HELPERS . DIRECTORY_SEPARATOR . 'connectors.php'; $connector = new EasyBlogConnectorsHelper(); $connector->addUrl($feedURL); $connector->execute(); $content = $connector->getResult($feedURL); // to ensure the leading no text before the <?xml> tag //$pattern = '/(.*?)(?=<\?xml)/ims'; $pattern = '/(.*?)<\\?xml version/is'; $replacement = '<?xml version'; $content = preg_replace($pattern, $replacement, $content, 1); if (strpos($content, '<?xml version') === false) { // look like the content missing the xml header. lets manually add in. $content = '<?xml version="1.0" encoding="utf-8"?>' . $content; } $parser = new SimplePie(); $parser->strip_htmltags(false); $parser->set_raw_data($content); $parser->init(); $items = ''; $items = $parser->get_items(); if (count($items) > 0) { //lets process the data insert $myCnt = 0; foreach ($items as $item) { @ini_set('max_execution_time', 180); if (!empty($maxItems) && $myCnt == $maxItems) { break; } $timezoneSec = $item->get_date('Z'); $itemdate = $item->get_date('U'); $itemdate = $itemdate - $timezoneSec; $mydate = date('Y-m-d H:i:s', $itemdate); $feedUid = $item->get_id(); $feedPath = $item->get_link(); $feedHistory = EasyBlogHelper::getTable('FeedHistory'); $newHistoryId = ''; if ($feedHistory->isExists($feedObj->id, $feedUid)) { continue; } else { //log the feed item so that in future it will not process again. $date = EasyBlogHelper::getDate(); $newHistory = EasyBlogHelper::getTable('FeedHistory'); $newHistory->feed_id = $feedObj->id; $newHistory->uid = $feedUid; $newHistory->created = $date->toMySQL(); $newHistory->store(); $newHistoryId = $newHistory->id; } $blogObj = new stdClass(); // set the default setting from the feed configuration via backend. $blogObj->category_id = $feedObj->item_category; $blogObj->published = $feedObj->item_published; $blogObj->frontpage = $feedObj->item_frontpage; $blogObj->created_by = $feedObj->item_creator; $blogObj->allowcomment = $config->get('main_comment', 1); $blogObj->subscription = $config->get('main_subscription', 1); $blogObj->issitewide = '1'; $text = $item->get_content(); // @rule: Append copyright text $blogObj->copyrights = $params->get('copyrights', ''); if ($feedObj->item_get_fulltext && $isDomSupported) { $feedItemUrl = urldecode($item->get_link()); $fiConnector = new EasyBlogConnectorsHelper(); $fiConnector->addUrl($feedItemUrl); $fiConnector->execute(); $fiContent = $fiConnector->getResult($feedItemUrl); // to ensure the leading no text before the <?xml> tag $pattern = '/(.*?)<html/is'; $replacement = '<html'; $fiContent = preg_replace($pattern, $replacement, $fiContent, 1); if (!empty($fiContent)) { $fiContent = EasyBlogHelper::getHelper('string')->forceUTF8($fiContent); $readability = new Readability($fiContent); $readability->debug = false; $readability->convertLinksToFootnotes = false; $result = $readability->init(); if ($result) { $content = $readability->getContent()->innerHTML; //$content = EasyBlogHelper::getHelper( 'string' )->fixUTF8( $content ); $content = EasyBlogFeedsHelper::tidyContent($content); if (stristr(html_entity_decode($content), '<!DOCTYPE html') === false) { $text = $content; $text = $this->_processRelLinktoAbs($text, $feedPath); } } } } // strip un-allowed html tag. $text = strip_tags($text, $params->get('allowed', $defaultAllowedHTML)); // Append original source link into article if necessary if ($params->get('sourceLinks')) { JFactory::getLanguage()->load('com_easyblog', JPATH_ROOT); $text .= '<div><a href="' . $item->get_link() . '" target="_blank">' . JText::_('COM_EASYBLOG_FEEDS_ORIGINAL_LINK') . '</a></div>'; } if ($feedObj->author) { $feedAuthor = $item->get_author(); if (!empty($feedAuthor)) { $authorName = $feedAuthor->get_name(); $authorEmail = $feedAuthor->get_email(); if (!empty($authorName)) { // Store it as copyright column instead $text .= '<div>' . JText::sprintf('COM_EASYBLOG_FEEDS_ORIGINAL_AUTHOR', $authorName) . '</div>'; } else { if (!empty($authorEmail)) { $authorArr = explode(' ', $authorEmail); if (isset($authorArr[1])) { $authorName = $authorArr[1]; $authorName = str_replace(array('(', ')'), '', $authorName); $text .= '<div>' . JText::sprintf('COM_EASYBLOG_FEEDS_ORIGINAL_AUTHOR', $authorName) . '</div>'; } } } } } if ($feedObj->item_content == 'intro') { $blogObj->intro = $text; } else { $blogObj->content = $text; } $creationDate = $mydate; $blogObj->created = $mydate; $blogObj->modified = $mydate; $blogObj->title = $item->get_title(); if (empty($blogObj->title)) { $blogObj->title = $this->_getTitleFromLink($item->get_link()); } $blogObj->title = EasyBlogStringHelper::unhtmlentities($blogObj->title); $blogObj->permalink = EasyBlogHelper::getPermalink($blogObj->title); $blogObj->publish_up = $mydate; $blogObj->isnew = !$feedObj->item_published ? true : false; $blog = EasyBlogHelper::getTable('blog'); $blog->bind($blogObj); if ($feedObj->item_published) { $blog->notify(); } if ($blog->store()) { $myCnt++; //update the history with blog id if (!empty($newHistoryId)) { $tmpHistory = EasyBlogHelper::getTable('FeedHistory'); $tmpHistory->load($newHistoryId); $tmpHistory->post_id = $blog->id; $tmpHistory->store(); } $itemMigrated++; if ($feedObj->item_published) { //insert activity here. EasyBlogHelper::addJomSocialActivityBlog($blog, true, true); // Determines if admin wants to auto post this item to the social sites. if ($params->get('autopost')) { $allowed = array(EBLOG_OAUTH_LINKEDIN, EBLOG_OAUTH_FACEBOOK, EBLOG_OAUTH_TWITTER); // @rule: Process centralized options first // See if there are any global postings enabled. $blog->autopost($allowed, $allowed); } } } //end if } } return $itemMigrated; }
function _processWPXMLAttachment($wpPostId, $content, $attachments, $authorId) { require_once EBLOG_HELPERS . DIRECTORY_SEPARATOR . 'connectors.php'; foreach ($attachments as $attachment) { $link = $attachment['link']; $attachementURL = $attachment['attachment_url']; if (EasyImageHelper::isImage($attachementURL)) { $filname = EasyImageHelper::getFileName($attachementURL); $extension = EasyImageHelper::getFileExtension($attachementURL); $folder = JPATH_ROOT . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'blogs' . DIRECTORY_SEPARATOR . $wpPostId; if (!JFolder::exists($folder)) { JFolder::create($folder); } // new image location $newFile = $folder . DIRECTORY_SEPARATOR . $filname; $connector = new EasyBlogConnectorsHelper(); $connector->addUrl($attachementURL); $connector->execute(); $imageraw = $connector->getResult($attachementURL); if ($imageraw) { if (JFile::write($newFile, $imageraw)) { //replace the string in the content. $absImagePath = rtrim(JURI::root(), '/') . '/images/blogs/' . $wpPostId . '/' . $filname; $content = str_ireplace('href="' . $link . '"', 'href="' . $absImagePath . '"', $content); $pattern = '/src=[\\"\']?([^\\"\']?.*(png|jpg|jpeg|gif))[\\"\']?/i'; $content = preg_replace($pattern, 'src="' . $absImagePath . '"', $content); } } // if( file_put_contents( $newFile, file_get_contents($attachementURL) ) !== false ) // { // //replace the string in the content. // $absImagePath = rtrim( JURI::root(), '/' ) . '/images/blogs/' . $wpPostId . '/' . $filname; // $content = JString::str_ireplace( 'href="' . $link . '"' , 'href="' . $absImagePath . '"' , $content ); // // $pattern = '/src=[\"\']?([^\"\']?.*(png|jpg|jpeg|gif))[\"\']?/i'; // $content = preg_replace( $pattern , 'src="'.$absImagePath.'"' , $content ); // } } } return $content; }