Advertisement
Guest User

ralphschindler

a guest
Aug 4th, 2007
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.49 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @see Blog_AbstractController
  5.  */
  6. require_once 'AbstractController.php';
  7.  
  8. class Blog_RpcController extends Blog_AbstractController
  9. {
  10.    
  11.     public function init()
  12.     {
  13.         $this->_helper->ModelLoader('Blog_Linkbacks');
  14.     }
  15.    
  16.     public function pingbackAction()
  17.     {
  18.         $this->_helper->ViewRenderer->setNoRender();
  19.         $this->_helper->LayoutManager->useLayoutName(false);
  20.  
  21.         $server = new Zend_XmlRpc_Server();
  22.         $server->setClass('Blog_PingbackRpc', 'pingback');
  23.         echo $server->handle();
  24.     }
  25.    
  26.     public function trackbackAction()
  27.     {
  28.         $this->_helper->LayoutManager->setLayoutName(false);
  29.         $this->_helper->ViewRenderer->setViewSuffix('pxml');
  30.  
  31.         if (!isset($_POST['url'])) {
  32.             $this->view->errorMessage = 'Invalid trackback request';
  33.             return;
  34.         }
  35.        
  36.         $blogPostId = $this->_request->getParam('post-id');
  37.  
  38.         $blogPostsTable = new Blog_Posts();
  39.         $blogPosts = $blogPostsTable->find($blogPostId);
  40.        
  41.         if ($blogPosts->count() != 1) {
  42.            $this->view->errorMessage = 'Blog post not found';
  43.            return;
  44.         }
  45.        
  46.         $blogPost = $blogPosts->current();
  47.  
  48.         $blogLinkbackTable = new Blog_Linkbacks();
  49.         $blogLinkback = $blogLinkbackTable->createRow();
  50.        
  51.         $filter_notags = new Zend_Filter_StripTags();
  52.        
  53.         $blogLinkback->blog_post_id = $blogPost->id;
  54.         $blogLinkback->direction    = 'INCOMING';
  55.         $blogLinkback->status       = 'IN_MODERATION';
  56.         $blogLinkback->title        = $filter_notags->filter($_POST['title']);
  57.         $blogLinkback->excerpt      = $filter_notags->filter($_POST['excerpt']);
  58.         $blogLinkback->url          = $filter_notags->filter($_POST['url']);
  59.         $blogLinkback->blog_name    = $filter_notags->filter($_POST['blog_name']);
  60.         $blogLinkback->from_ip      = $_SERVER['REMOTE_ADDR'];
  61.         $blogLinkback->linked_on    = date('Y-m-d H:i:s');
  62.        
  63.         $blogLinkback->save();
  64.  
  65.     }
  66.    
  67. }
  68.  
  69. class Blog_PingbackRpc
  70. {
  71.    
  72.     /**
  73.     * This is a sample function
  74.     *
  75.     * @param string $remoteUri Url1
  76.     * @param string $sourceUri Url2
  77.     * @return struct
  78.     */
  79.     public function ping($remoteUri, $sourceUri)
  80.     {
  81.        
  82.         // build a request out of the Url
  83.         $sourceUriRquest = new Zend_Controller_Request_Http($sourceUri);
  84.         Zend_Controller_Front::getInstance()->getRouter()->route($sourceUriRquest);
  85.        
  86.         // find our blog post from url
  87.         $blogPost = Zend_Controller_Action_HelperBroker::getStaticHelper('BlogPostRetriever')->getFromRequest($sourceUriRquest);
  88.        
  89.         // RETURN: no valid source blog post
  90.         if ($blogPost->id == null) {
  91.             return 0x0010;
  92.         }
  93.        
  94.        Zend_Controller_Action_HelperBroker::getExistingHelper('ModelLoader')->load(
  95.             'Blog_Linkbacks',
  96.             ':moduleDir/models/Data'
  97.             );
  98.        
  99.         $blogLinkbacksTable = new Blog_Linkbacks();
  100.         $blogLinkbacks = $blogLinkbacksTable->fetchAll(array(
  101.             'direction = "INCOMING"',
  102.             $blogLinkbacksTable->getAdapter()->quoteInto('url_source = ?', $remoteUri),
  103.             'blog_post_id = "' . $blogPost->id . '"')
  104.             );
  105.            
  106.         // RETURN: already have a linkback to this post by said source uri
  107.         if ($blogLinkbacks->count() != 0) {
  108.             return 0x0030;
  109.         }
  110.            
  111.         // make sure the doc exists on the remote server
  112.         $i = new Zend_Http_Client($remoteUri);
  113.         $j = $i->request(Zend_Http_Client::GET);
  114.        
  115.         // RETURN: our source blog post does not exist on remote server
  116.         if (($pos = strpos(($sourceUriBody = $j->getBody()), $sourceUri)) === false) {
  117.             return 0x0011;
  118.         }
  119.        
  120.         // get the title of the remote blog post
  121.         preg_match('/<title>([^<]*?)<\/title>/is', $sourceUriBody, $titles);
  122.         $title = $titles[1]; unset($titles);
  123.  
  124.         // RETURN: no title on page
  125.         if ($title == null) {
  126.             return false;
  127.         }
  128.        
  129.         $contents = $j->getBody();
  130.         $contents = preg_replace('/[\s|\r|\n]+/im', ' ', $contents);
  131.         $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);
  132.         $stripper = new Zend_Filter_StripTags('a', 'href');
  133.         $contents = $stripper->filter($contents);
  134.                
  135.         $excerpt = null;
  136.        
  137.         foreach (explode('|||blog-paragraph|||', $contents) as $content) {
  138.             if (($linkPos = strpos($content, $sourceUri)) !== false) {
  139.                
  140.                 $contentWords = explode(' ', $content);
  141.                
  142.                 if (count($contentWords) > 100) {
  143.                     foreach ($contentWords as $contentWordIndex => $contentWord) {
  144.                         if (strpos($contentWord, $sourceUri) !== false) {
  145.                             break;
  146.                         } else {
  147.                             $contentWordIndex = null;
  148.                         }
  149.                     }
  150.  
  151.                     $startIndex = (($contentWordIndex-49) > 0) ? $contentWordIndex - 48 : 0;
  152.                     $endIndex   = (($contentWordIndex+49) < count($contentWords)) ? $contentWordIndex + 49 : count($contentWords);
  153.                    
  154.                     $excerpt = implode(' ', array_slice($contentWords, $startIndex, $startIndex + $endIndex));
  155.                    
  156.                     if ($startIndex != 0) {
  157.                         $excerpt = ' ... ' . $excerpt;
  158.                     }
  159.                    
  160.                     if ($endIndex != count($contentWords)) {
  161.                         $excerpt .= ' ... ';
  162.                     }
  163.                    
  164.                 } else {
  165.                     $excerpt = $content;
  166.                 }
  167.                        
  168.                 break;
  169.             }
  170.         }
  171.        
  172.         // RETURN: no content / excerpt found
  173.         if ($content == null) {
  174.             return false;
  175.         }
  176.        
  177.         $blogLinkback = $blogLinkbacksTable->createRow();
  178.         $blogLinkbackValues = array(
  179.             'blog_post_id'      => $blogPost->id,
  180.             'direction'         => 'INCOMING',
  181.             'url_source'        => $remoteUri,
  182.             'url_destination'   => $sourceUri,
  183.             'status'            => 'MODERATED',
  184.             'title'             => $title,
  185.             'excerpt'           => $excerpt,
  186.             'from_ip'           => $_SERVER['REMOTE_ADDR']
  187.             );
  188.         $blogLinkback->setFromArray($blogLinkbackValues);
  189.         $blogLinkback->save();
  190.            
  191.         return true;
  192.     }
  193. }
  194.  
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement