Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @see Blog_AbstractController
- */
- require_once 'AbstractController.php';
- class Blog_RpcController extends Blog_AbstractController
- {
- public function init()
- {
- $this->_helper->ModelLoader('Blog_Linkbacks');
- }
- public function pingbackAction()
- {
- $this->_helper->ViewRenderer->setNoRender();
- $this->_helper->LayoutManager->useLayoutName(false);
- $server = new Zend_XmlRpc_Server();
- $server->setClass('Blog_PingbackRpc', 'pingback');
- echo $server->handle();
- }
- public function trackbackAction()
- {
- $this->_helper->LayoutManager->setLayoutName(false);
- $this->_helper->ViewRenderer->setViewSuffix('pxml');
- if (!isset($_POST['url'])) {
- $this->view->errorMessage = 'Invalid trackback request';
- return;
- }
- $blogPostId = $this->_request->getParam('post-id');
- $blogPostsTable = new Blog_Posts();
- $blogPosts = $blogPostsTable->find($blogPostId);
- if ($blogPosts->count() != 1) {
- $this->view->errorMessage = 'Blog post not found';
- return;
- }
- $blogPost = $blogPosts->current();
- $blogLinkbackTable = new Blog_Linkbacks();
- $blogLinkback = $blogLinkbackTable->createRow();
- $filter_notags = new Zend_Filter_StripTags();
- $blogLinkback->blog_post_id = $blogPost->id;
- $blogLinkback->direction = 'INCOMING';
- $blogLinkback->status = 'IN_MODERATION';
- $blogLinkback->title = $filter_notags->filter($_POST['title']);
- $blogLinkback->excerpt = $filter_notags->filter($_POST['excerpt']);
- $blogLinkback->url = $filter_notags->filter($_POST['url']);
- $blogLinkback->blog_name = $filter_notags->filter($_POST['blog_name']);
- $blogLinkback->from_ip = $_SERVER['REMOTE_ADDR'];
- $blogLinkback->linked_on = date('Y-m-d H:i:s');
- $blogLinkback->save();
- }
- }
- class Blog_PingbackRpc
- {
- /**
- * This is a sample function
- *
- * @param string $remoteUri Url1
- * @param string $sourceUri Url2
- * @return struct
- */
- public function ping($remoteUri, $sourceUri)
- {
- // build a request out of the Url
- $sourceUriRquest = new Zend_Controller_Request_Http($sourceUri);
- Zend_Controller_Front::getInstance()->getRouter()->route($sourceUriRquest);
- // find our blog post from url
- $blogPost = Zend_Controller_Action_HelperBroker::getStaticHelper('BlogPostRetriever')->getFromRequest($sourceUriRquest);
- // RETURN: no valid source blog post
- if ($blogPost->id == null) {
- return 0x0010;
- }
- Zend_Controller_Action_HelperBroker::getExistingHelper('ModelLoader')->load(
- 'Blog_Linkbacks',
- ':moduleDir/models/Data'
- );
- $blogLinkbacksTable = new Blog_Linkbacks();
- $blogLinkbacks = $blogLinkbacksTable->fetchAll(array(
- 'direction = "INCOMING"',
- $blogLinkbacksTable->getAdapter()->quoteInto('url_source = ?', $remoteUri),
- 'blog_post_id = "' . $blogPost->id . '"')
- );
- // RETURN: already have a linkback to this post by said source uri
- if ($blogLinkbacks->count() != 0) {
- return 0x0030;
- }
- // make sure the doc exists on the remote server
- $i = new Zend_Http_Client($remoteUri);
- $j = $i->request(Zend_Http_Client::GET);
- // RETURN: our source blog post does not exist on remote server
- if (($pos = strpos(($sourceUriBody = $j->getBody()), $sourceUri)) === false) {
- return 0x0011;
- }
- // get the title of the remote blog post
- preg_match('/<title>([^<]*?)<\/title>/is', $sourceUriBody, $titles);
- $title = $titles[1]; unset($titles);
- // RETURN: no title on page
- if ($title == null) {
- return false;
- }
- $contents = $j->getBody();
- $contents = preg_replace('/[\s|\r|\n]+/im', ' ', $contents);
- $contents = preg_replace('/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body|borrowed|from|wordpress)[^>]*>/is', ' |||blog-paragraph||| ', $contents);
- $stripper = new Zend_Filter_StripTags('a', 'href');
- $contents = $stripper->filter($contents);
- $excerpt = null;
- foreach (explode('|||blog-paragraph|||', $contents) as $content) {
- if (($linkPos = strpos($content, $sourceUri)) !== false) {
- $contentWords = explode(' ', $content);
- if (count($contentWords) > 100) {
- foreach ($contentWords as $contentWordIndex => $contentWord) {
- if (strpos($contentWord, $sourceUri) !== false) {
- break;
- } else {
- $contentWordIndex = null;
- }
- }
- $startIndex = (($contentWordIndex-49) > 0) ? $contentWordIndex - 48 : 0;
- $endIndex = (($contentWordIndex+49) < count($contentWords)) ? $contentWordIndex + 49 : count($contentWords);
- $excerpt = implode(' ', array_slice($contentWords, $startIndex, $startIndex + $endIndex));
- if ($startIndex != 0) {
- $excerpt = ' ... ' . $excerpt;
- }
- if ($endIndex != count($contentWords)) {
- $excerpt .= ' ... ';
- }
- } else {
- $excerpt = $content;
- }
- break;
- }
- }
- // RETURN: no content / excerpt found
- if ($content == null) {
- return false;
- }
- $blogLinkback = $blogLinkbacksTable->createRow();
- $blogLinkbackValues = array(
- 'blog_post_id' => $blogPost->id,
- 'direction' => 'INCOMING',
- 'url_source' => $remoteUri,
- 'url_destination' => $sourceUri,
- 'status' => 'MODERATED',
- 'title' => $title,
- 'excerpt' => $excerpt,
- 'from_ip' => $_SERVER['REMOTE_ADDR']
- );
- $blogLinkback->setFromArray($blogLinkbackValues);
- $blogLinkback->save();
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement