b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _functions_pingback.php

Documentation is available at _functions_pingback.php

  1. <?php
  2. /**
  3.  * Pingback support functions
  4.  * 
  5.  * b2evolution - {@link http://b2evolution.net/}
  6.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  7.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  8.  *
  9.  * @package evocore
  10.  * @author This file built upon code from original b2 - http://cafelog.com/
  11.  */
  12. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  13.  
  14.  
  15. /**
  16.  * Sending pingback
  17.  *
  18.  * original code by Mort (http://mort.mine.nu:8080)
  19.  *
  20.  * {@internal pingback(-)}}
  21.  */
  22. function pingback
  23.     $post_pingback
  24.     $content
  25.     $post_title
  26.     $post_url
  27.     $post_ID
  28.     $blogparams
  29.     $display true
  30. {
  31.     global $b2_version$debug$ItemCache;
  32.  
  33.     if$display )
  34.     {
  35.         echo "<div class=\"panelinfo\">\n";
  36.         echo '<h3>'T_('Sending pingbacks...')'</h3>'"\n";
  37.     }
  38.  
  39.     if$post_pingback )
  40.     {
  41.         if$display echo '<p>'T_('No pingback to be done.')'</p>'"\n";
  42.     }
  43.     else
  44.     {
  45.         $log debug_fopen('./pingback.log''a');
  46.         $post_links array();
  47.         debug_fwrite($logT_('BEGIN').' '.time()."\n");
  48.     
  49.         // Variables
  50.         $ltrs '\w';
  51.         $gunk '/#~:.?+=&%@!\-';
  52.         $punc '.:?\-';
  53.         $any $ltrs.$gunk.$punc;
  54.         $pingback_str_dquote 'rel="pingback"';
  55.         $pingback_str_squote 'rel=\'pingback\'';
  56.         $x_pingback_str 'x-pingback: ';
  57.         $pingback_href_original_pos 27;
  58.     
  59.         $Item $ItemCache->get_by_ID$post_ID );
  60.         $pagelinkedfrom $Item->gen_permalink();
  61.  
  62.         if!empty($post_url) )
  63.         {
  64.             $content '<a href="'.$post_url.'">'.$post_title.'</a>'.$post_url;
  65.         }
  66.  
  67.         // Step 1
  68.         // Parsing the post, external links (if any) are stored in the $post_links array
  69.         // This regexp comes straigth from phpfreaks.com
  70.         // http://www.phpfreaks.com/quickcode/Extract_All_URLs_on_a_Page/15.php
  71.         // preg_match_all("{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp);
  72.         // fplanque: \b is for word boundary
  73.         // trailing x is to ignore whitespace
  74.         // we need to simplify and allow ; in the URL
  75.          preg_match_all("{\b http:// [0-9A-Za-z:/_~+\-%.?&=;]+}x"$content$post_links_temp);
  76.          
  77.         // Debug
  78.         debug_fwrite($logT_('Post contents').':');
  79.         debug_fwrite($log$content."\n");
  80.         
  81.         // Step 2.
  82.         // Walking thru the links array
  83.         // first we get rid of links pointing to sites, not to specific files
  84.         // Example:
  85.         // http://dummy-weblog.org
  86.         // http://dummy-weblog.org/
  87.         // http://dummy-weblog.org/post.php
  88.         // We don't wanna ping first and second types, even if they have a valid <link/>
  89.     
  90.         foreach($post_links_temp[0as $link_test)
  91.         {
  92.             //echo "testing: $link_test <br />";
  93.             $test parse_url($link_test);
  94.             if$test['scheme'== 'http' )
  95.             {
  96.                 if (isset($test['query'])) 
  97.                 {
  98.                     $post_links[$link_test;
  99.                 }
  100.                 elseif(($test['path'!= '/'&& ($test['path'!= '')) 
  101.                 {
  102.                     $post_links[$link_test;
  103.                 }
  104.             }
  105.         }
  106.     
  107.         foreach ($post_links as $pagelinkedto)
  108.         {
  109.             if$display echo '<p>'T_('Processing:')' '$pagelinkedto"<br />\n";
  110.             debug_fwrite($logT_('Processing:').' '.$pagelinkedto."\n\n");
  111.     
  112.             $bits parse_url($pagelinkedto);
  113.             if (!isset($bits['host'])) {
  114.                 if$display echo T_('Couldn\'t find a hostname for:'),' ',$pagelinkedto"<br />\n";
  115.                 debug_fwrite($logT_('Couldn\'t find a hostname for:').' '.$pagelinkedto."\n\n");
  116.                 continue;
  117.             }
  118.             $host $bits['host'];
  119.             $path = isset($bits['path']$bits['path''';
  120.             if (isset($bits['query'])) {
  121.                 $path .= '?'.$bits['query'];
  122.             }
  123.             if (!$path{
  124.                 $path '/';
  125.             }
  126.             $port = isset($bits['port']$bits['port'80;
  127.     
  128.             // Try to connect to the server at $host
  129.             if$display echo T_('Connect to server at:')' ',$host;
  130.             $fp fsockopen($host$port$errno$errstr30);
  131.             if (!$fp
  132.             {
  133.                 if$display echo T_('Couldn\'t open a connection to:')' '$pagelinkedto"<br />\n";
  134.                 debug_fwrite($logT_('Couldn\'t open a connection to:').' '.$host."\n\n");
  135.                 continue;
  136.             }
  137.             echo "<br />\n";
  138.     
  139.             // Send the GET request
  140.             $request "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: b2evolution/$b2_version PHP/phpversion("\r\n\r\n";
  141.             ob_end_flush();
  142.             fputs($fp$request);
  143.     
  144.             // Start receiving headers and content
  145.             debug_fwrite($logT_('Start receiving headers and content')."\n");
  146.             $contents '';
  147.             $headers '';
  148.             $gettingHeaders true;
  149.             $found_pingback_server 0;
  150.             while (!feof($fp)) 
  151.             {
  152.                 $line fgets($fp4096);
  153.                 // echo "line (".strlen($line)."): [",htmlspecialchars($line),"] <br />\n";
  154.                 if (trim($line== '')  // ligne blanche = fin des headers
  155.                 {
  156.                     $gettingHeaders false;
  157.                 }
  158.                 $pingback_link_offset_dquote 0;
  159.                 $pingback_link_offset_squote 0;
  160.                 $x_pingback_header_offset 0;
  161.                 if (!$gettingHeaders
  162.                 {
  163.                     // echo 'CONTENT';
  164.                     $contents .= trim($line)."\n";
  165.                     // localise rel="'pingback"' :
  166.                     $pingback_link_offset_dquote strpos($contents$pingback_str_dquote)
  167.                     $pingback_link_offset_squote strpos($contents$pingback_str_squote);
  168.                 
  169.                 else 
  170.                 {
  171.                     // echo 'HEADER';
  172.                     $headers .= trim($line)."\n";
  173.                     $x_pingback_header_offset strpos(strtolower($headers)$x_pingback_str);
  174.                 }
  175.                 if ($x_pingback_header_offset
  176.                 {    // on a trouvé dans les headers
  177.                     preg_match('#x-pingback: (.+)#is'$headers$matches);
  178.                     $pingback_server_url trim($matches[1]);
  179.                     echo T_('Pingback server found from X-Pingback header:')' '$pingback_server_url"<br />\n";
  180.                     debug_fwrite($logT_('Pingback server found from X-Pingback header:').' '.$pingback_server_url."\n");
  181.                     $found_pingback_server 1;
  182.                     break;
  183.                 }    
  184.                 if$pingback_link_offset_dquote || $pingback_link_offset_squote 
  185.                 {    // on a trouvé dans les données
  186.                     $quote ($pingback_link_offset_dquote'"' '\'';
  187.                     $pingback_link_offset ($quote=='"'$pingback_link_offset_dquote $pingback_link_offset_squote;
  188.                     $pingback_href_pos strpos($contents'href='$pingback_link_offset);
  189.                     $pingback_href_start $pingback_href_pos+6;
  190.                     $pingback_href_end strpos($contents$quote$pingback_href_start);
  191.                     $pingback_server_url_len $pingback_href_end-$pingback_href_start;
  192.                     $pingback_server_url substr($contents$pingback_href_start$pingback_server_url_len);
  193.                     echo T_('Pingback server found from Pingback <link /> tag:')' '$pingback_server_url"<br />\n";
  194.                     debug_fwrite($log,  T_('Pingback server found from Pingback <link /> tag:').' '.$pingback_server_url."\n");
  195.                     $found_pingback_server 1;
  196.                     break;
  197.                 }
  198.             }
  199.     
  200.             if(!$found_pingback_server
  201.             {
  202.                 if$display )    echo T_('Pingback server not found in headers and content')"<br />\n";
  203.                 debug_fwrite($logT_('Pingback server not found in headers and content')"\n\n*************************\n\n");
  204.                 @fclose($fp);
  205.             
  206.             elseifempty($pingback_server_url) )
  207.             {
  208.                 if$display )    echo T_('Pingback server URL is empty (may be an internal PHP fgets error)')"<br />\n";
  209.                 debug_fwrite($logT_('Pingback server URL is empty (may be an internal PHP fgets error)')"\n\n*************************\n\n");
  210.                 @fclose($fp);
  211.             }
  212.             else 
  213.             {
  214.                 debug_fwrite($log,"\n\n"T_('Pingback server data')"\n");
  215.                 
  216.                 $parsed_url parse_url$pingback_server_url );
  217.                 debug_fwrite($log'host: '.$parsed_url['host']."\n");
  218.                 $port = isset($parsed_url['port']$parsed_url['port'80;
  219.                 debug_fwrite($log'port: '.$port."\n");
  220.                 debug_fwrite($log'path: '.$parsed_url['path']."\n\n");
  221.     
  222.                  // Now, the RPC call
  223.                 $method 'pingback.ping';
  224.                 if$display )    echo T_('Page Linked To:')" $pagelinkedto<br />\n";
  225.                 debug_fwrite($logT_('Page Linked To:').' '.$pagelinkedto."\n");
  226.                 if$display )    echo T_('Page Linked From:')" $pagelinkedfrom<br />\n";
  227.                 debug_fwrite($logT_('Page Linked From:').' '.$pagelinkedfrom."\n");
  228.  
  229.                 $client new xmlrpc_client$parsed_url['path']$parsed_url['host']$port);
  230.                 $client->setDebug$debug );
  231.                 $message new xmlrpcmsg($methodarray(new xmlrpcval($pagelinkedfrom)new xmlrpcval($pagelinkedto)));
  232.                 printfT_('Pinging %s...')."<br />\n"$host );
  233.                 $result $client->send($message);
  234.  
  235.                 // Display response
  236.                 $ret xmlrpc_displayresult$result$log );
  237.                 @fclose($fp);
  238.             }
  239.             if$display )    echo "</p>\n";
  240.         }
  241.  
  242.         debug_fwrite($log"\n"T_('END')": ".time()."\n****************************\n\r");
  243.         debug_fclose($log);
  244.         if$display )    echo "<p>"T_('Pingbacks done.')"<p>\n";
  245.     }
  246.     if$display )    echo "</div>\n";    
  247. }
  248.  
  249.  
  250.  
  251. /* 
  252.  * TEMPLATE FUNCTIONS:
  253.  */
  254.  
  255.  
  256.  
  257. /*****
  258.  * Pingback tags 
  259.  *****/
  260.  
  261. /**
  262.  * pingback_number(-)
  263.  * @deprecated deprecated by {@link Item::feedback_link()}
  264.  */
  265. function pingback_number($zero='#'$one='#'$more='#' 
  266. {
  267.     if$zero == '#' $zero T_('Pingback (0)');
  268.     if$one == '#' $one T_('Pingback (1)');
  269.     if$more == '#' $more T_('Pingbacks (%d)');
  270.  
  271.     global $id$tablecomments$tb$querycount$cache_pingbacknumber$use_cache;
  272.     $number generic_ctp_number($id'pingbacks');
  273.     if ($number == 0{
  274.         $blah $zero;
  275.     elseif ($number == 1{
  276.         $blah $one;
  277.     elseif ($number  1{
  278.         $n $number;
  279.         $more str_replace('%d'$n$more);
  280.         $blah $more;
  281.     }
  282.     echo $blah;
  283. }
  284.  
  285. /**
  286.  * Displays link to the pingback page
  287.  * @deprecated deprecated by {@link Item::feedback_link()}
  288.  */
  289. function pingback_link($file='',$c=0,$tb=0
  290. {
  291.     global $id;
  292.     if( ($file == ''|| ($file == '/')    )
  293.         $file get_bloginfo('blogurl');
  294.     echo url_add_param$file'p='.$id );
  295.     if$c == )
  296.     {    // include comments // fplanque: added
  297.         echo '&amp;c=1';
  298.     }
  299.     if$tb == )
  300.     {    // include trackback // fplanque: added
  301.         echo '&amp;tb=1';
  302.     }
  303.     echo '&amp;pb=1#pingbacks';
  304. }
  305.  
  306. /**
  307.  *
  308.  * @deprecated deprecated by {@link Item::feedback_link()}
  309.  */
  310. function pingback_popup_link($zero='#'$one='#'$more='#'$CSSclass='')
  311. {
  312.     global $blog$id$b2pingbackpopupfile$b2commentsjavascript;
  313.     echo '<a href="';
  314.     if ($b2commentsjavascript{
  315.         echo url_add_paramget_bloginfo('blogurl')'template=popup&amp;p='.$id.'&amp;pb=1' );
  316.         echo '" onclick="b2open(this.href); return false"';
  317.     else {
  318.         // if comments_popup_script() is not in the template, display simple comment link
  319.         pingback_link();
  320.         echo '"';
  321.     }
  322.     if (!empty($CSSclass)) {
  323.         echo ' class="'.$CSSclass.'"';
  324.     }
  325.     echo '>';
  326.     pingback_number($zero$one$more);
  327.     echo '</a>';
  328. }
  329.  
  330.  
  331.  
  332. /***** // Pingback tags *****/
  333.  
  334.  
  335.  
  336. ?>

Documentation generated on Tue, 20 May 2008 01:55:01 +0200 by phpDocumentor 1.4.2