b2evolution PHP Cross Reference Blogging Systems

Source: /inc/comments/_trackback.funcs.php - 224 lines - 6430 bytes - Summary - Text - Print

Description: This file implements trackback functions. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements trackback functions.
   4   *
   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.
   9   * Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10   *
  11   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
  12   *
  13   * {@internal Open Source relicensing agreement:
  14   * Daniel HAHLER grants Francois PLANQUE the right to license
  15   * Daniel HAHLER's contributions to this file and the b2evolution project
  16   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  17   * }}
  18   *
  19   * @package evocore
  20   *
  21   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  22   * @author cafelog (team)
  23   * @author blueyed: Daniel HAHLER.
  24   * @author fplanque: Francois PLANQUE.
  25   * @author jmuto: Jun MUTO
  26   * @author sakichan: Nobuo SAKIYAMA.
  27   * @author vegarg: Vegar BERG GULDAL.
  28   *
  29   * @version $Id: _trackback.funcs.php 6136 2014-03-08 07:59:48Z manuel $
  30   */
  31  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  32  
  33  
  34  /**
  35   * trackbacks(-)
  36   *
  37   * Do multiple trackbacks
  38   *
  39   * fplanque: added
  40   */
  41  function trackbacks( $post_trackbacks, $Item )
  42  {
  43      global $Messages;
  44  
  45      $excerpt = $Item->get_excerpt();
  46  
  47      $Messages->add( T_('Excerpt sent in trackbacks:').' '.$excerpt, 'note' );
  48      $trackback_urls = split('( )+', $post_trackbacks,10);        // fplanque: ;
  49      foreach($trackback_urls as $tb_url)
  50      { // trackback each url:
  51          $tb_url = trim($tb_url);
  52          if( empty( $tb_url ) ) continue;
  53          trackback( $tb_url, $Item->title, $excerpt, $Item->ID, $Item->get_permanent_url('', '', '&') );
  54      }
  55  }
  56  
  57  
  58  /**
  59   * Send Trackback to single URL
  60   *
  61   * trackback(-)
  62   *
  63   * @todo add autodiscovery
  64   */
  65  function trackback(
  66      $trackback_url,
  67      $title,
  68      $excerpt,
  69      $ID,
  70      $url ) // post ID
  71  {
  72      global $app_name, $app_version, $Blog, $Messages;
  73  
  74      $trackback_message = T_('Sending trackback to:').' '.htmlspecialchars($trackback_url).' ';
  75  
  76      $title = rawurlencode($title);
  77      $excerpt = rawurlencode($excerpt);
  78      $blog_name = rawurlencode($Blog->get( 'name' ));
  79      $url = rawurlencode($url);
  80      // dis is the trackback stuff to be sent:
  81      $query_string = 'title='.$title.'&url='.$url.'&blog_name='.$blog_name.'&excerpt='.$excerpt;
  82      // echo "url:$trackback_url<br>$sending:$query_string<br />";
  83  
  84      $result = '';
  85      if (strstr($trackback_url, '?'))
  86      {    // use a HTTP GET request
  87          $Messages->add( $trackback_message.'[get]', 'note' );
  88          $trackback_url = parse_url($trackback_url.'&'.$query_string);
  89  
  90          if (!empty($trackback_url['host']) && !empty($trackback_url['path'])
  91              && ($fp = @fsockopen($trackback_url['host'], 80, $foo, $foo, 20)) !== false)
  92          {
  93              $header  = 'GET '.$trackback_url['path'].'?'.$trackback_url['query']." HTTP/1.0\r\n";
  94              $header .= 'Host: '.$trackback_url['host']."\r\n";
  95              $header .= 'User-Agent: '.$app_name.'/'.$app_version."\r\n\r\n";
  96              fwrite($fp, $header, strlen($header));
  97  
  98              if(function_exists('stream_set_timeout'))
  99              {
 100                  stream_set_timeout($fp, 20); // PHP 4.3.0
 101              }
 102              else
 103              {
 104                  socket_set_timeout($fp, 20); // PHP 4
 105              }
 106  
 107              while (!feof($fp))
 108              {
 109                  $result .= fgets($fp);
 110              }
 111              fclose($fp);
 112  
 113              /* debug code
 114              $df = fopen('trackback.log', 'a');
 115              fwrite($df, "---------\nRequest:\n\n$header\n\nResponse:\n\n$result\n");
 116              fclose($df);
 117              */
 118          }
 119      }
 120      else
 121      {
 122          $Messages->add( $trackback_message.'[post]', 'note' );
 123          $trackback_url = parse_url($trackback_url);
 124          if( ! empty($trackback_url['host']) && ! empty($trackback_url['path']) )
 125          { // Only try trackback if we have host and path:
 126              $port = isset($trackback_url['port']) ? $trackback_url['port'] : 80;
 127              $http_request  = 'POST '.$trackback_url['path']." HTTP/1.0\r\n";
 128              $http_request .= 'Host: '.$trackback_url['host']."\r\n";
 129              $http_request .= 'Content-Type: application/x-www-form-urlencoded'."\r\n";
 130              $http_request .= 'Content-Length: '.strlen($query_string)."\r\n";
 131              $http_request .= "User-Agent: $app_name/$app_version\r\n";
 132              $http_request .= "\r\n";
 133              $http_request .= $query_string;
 134              evo_flush();
 135              if( $fs = @fsockopen($trackback_url['host'], $port, $errno, $errst, 20) ) // this timeout is just for setting up the socket
 136              {
 137                  // Set timeout for data:
 138                  if( function_exists('stream_set_timeout') )
 139                  {
 140                      stream_set_timeout( $fs, 20 ); // PHP 4.3.0
 141                  }
 142                  else
 143                  {
 144                      socket_set_timeout( $fs, 20 ); // PHP 4
 145                  }
 146                  fputs($fs, $http_request);
 147                  $result = '';
 148                  while(!feof($fs))
 149                  {
 150                      $result .= fgets($fs, 4096);
 151                  }
 152  
 153                  /* debug code
 154                  $debug_file = 'trackback.log';
 155                  $fp = fopen($debug_file, 'a');
 156                  fwrite($fp, "\n*****\nRequest:\n\n$http_request\n\nResponse:\n\n$result");
 157                  while(!@feof($fs)) {
 158                      fwrite($fp, @fgets($fs, 4096));
 159                  }
 160                  fwrite($fp, "\n\n");
 161                  fclose($fp);
 162                  */
 163  
 164                  fclose($fs);
 165              }
 166          }
 167      }
 168      // extract the error code and message, then make the error code readable
 169      if ( preg_match("/<error>[\r\n\t ]*(\d+)[\r\n\t ]*<\/error>/", $result, $error) )
 170      {
 171          preg_match("/<message>(.*?)<\/message>/", $result, $error_message);
 172  
 173          $message = isset($error_message[1]) ? $error_message[1] : '';
 174  
 175          switch ($error[1]) {
 176              case '0':
 177                  $result_message = '[' . T_('Succeeded') . '] ' . $message;
 178                  break;
 179              case '1':
 180                  $result_message = '[' . T_('Failed') . '] ' . $message;
 181                  break;
 182              default:
 183                  $result_message = '[' . T_('Unknown error') . ' (' . $error[1] . ')] ' . $message;
 184                  break;
 185          }
 186      }
 187      else
 188      {
 189          $result_message = T_('No valid trackback response. Maybe the given url is not a Trackback url.') . ' &quot;' . $result . '&quot;';
 190      }
 191      $Messages->add( T_('Response:').' '.strip_tags($result_message), 'note' );
 192      return $result;
 193  }
 194  
 195  
 196  
 197  /**
 198   * @deprecated deprecated by {@link Item::feedback_link()}
 199   */
 200  function trackback_number( $zero='#', $one='#', $more='#', $post_ID = NULL )
 201  {
 202      if( $zero == '#' ) $zero = T_('Trackback (0)');
 203      if( $one == '#' ) $one = T_('Trackback (1)');
 204      if( $more == '#' ) $more = T_('Trackbacks (%d)');
 205  
 206      if( empty( $post_ID ) )
 207      {
 208          global $id;
 209          $post_ID = $id;
 210      }
 211      $number = generic_ctp_number($post_ID, 'trackbacks');
 212      if ($number == 0) {
 213          $blah = $zero;
 214      } elseif ($number == 1) {
 215          $blah = $one;
 216      } elseif ($number  > 1) {
 217          $n = $number;
 218          $more = str_replace('%d', $n, $more);
 219          $blah = $more;
 220      }
 221      echo $blah;
 222  }
 223  
 224  ?>

title

Description

title

Description

title

Description

title

title

Body