[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/dao/ -> trackbackclient.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH."class/net/http/httpclient.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/dao/article.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
   6      lt_include( PLOG_CLASS_PATH."class/net/requestgenerator.class.php" );
   7      lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );    
   8  
   9      define( "TRACKBACK_SUCCESS", 0 );
  10      define( "TRACKBACK_FAILED", 1 );
  11      define( "TRACKBACK_UNAVAILABLE", 2 );
  12  
  13      /**
  14       * Implementation of a very simple Trackback client. It is capable of autodiscovering
  15       * the trackback urls in a page, given the right url.
  16       * It also offers mechanisms to send trackback pings to pages and a function that will
  17       * send trackback pings to the list of urls specified in an array as a parameter.
  18       *
  19       * \ingroup DAO
  20       */
  21      class TrackbackClient  
  22      {
  23  
  24          /**
  25           * Constructor.
  26           */
  27      	function TrackbackClient()
  28          {
  29              
  30          }
  31  
  32          /**
  33           * Uses a very simple regexp to find all the trackback:ping attributes in a page, but
  34           * it works without having to use an xml parser which would complicate things a little...
  35           *
  36           * @param page The html code which contains the RDF references embedded.
  37           * @param pageUrl Additionally, we can have the url of the page so that we can
  38           * make sure that we are fetching the right trackback url because we can then compare
  39           * the dc:identifier with the url. If they are the same, then we are getting the right
  40           * one. But if this is parameter is not provided, we will get the trackback url
  41           * without making sure it is the right one.
  42           * @return Returns an array of urls which are the trackbackurls that have been
  43           * found in the page, or an empty array if there was none found.
  44           */
  45           function getTrackbackLinks( $page, $pageUrl = "" ) {
  46               //$regexp = "/trackback:ping\ *=\ *\"(.+)\"/";
  47              $regexp ="/<rdf:RDF.*?<\/rdf:RDF>/s";
  48              $links = preg_match_all( $regexp, $page, $out, PREG_SET_ORDER );
  49  
  50              $links = Array();
  51              foreach( $out as $result ) {
  52                  // we have to get now from within the rdf code the following identifiers:
  53                  // dc:identifier, trackback:ping and dc:about
  54  
  55                  // get the dc identifier
  56                  if(preg_match( "/dc:identifier=\"([^\"]+)\"/", $result[0], $dc_ident))
  57                      $dcIdentifier = $dc_ident[1];
  58  
  59                  // get the trackback:ping
  60                  if(preg_match( "/trackback:ping=\"([^\"]+)\"/", $result[0], $tb_ping)) {
  61                      $tbPing = $tb_ping[1];
  62  
  63                      // if we provided the url of the page, we have a mechanism to make sure
  64                      // that we are fetching the right trackback ping
  65                      if( $pageUrl != "" ) {
  66                          if( $dcIdentifier == $pageUrl || $dcIdentifier == htmlentities($pageUrl) || urldecode($dcIdentifier) == $pageUrl ) {
  67                              //print("identifer matches page!!");
  68                              array_push( $links, $tbPing );
  69                          }
  70                      }
  71                      else {
  72                          array_push( $links, $tbPing );
  73                      }
  74                  }
  75              }
  76  
  77              return $links;
  78          }
  79  
  80          /**
  81           * Fetches a page from the given url.
  82           *
  83           * @param url The url with the page we would like to fetch.
  84           * @return Returns a string with the contents of the page or an empty string if the page
  85           * could not be fetched.
  86           */
  87  	    function fetchPage( $url )
  88          {
  89              $s = new HttpClient();
  90              $result = $s->fetch( $url );
  91  
  92              if( !$result ) {
  93                              if($s->timed_out)
  94                      //print("timed out!!!");
  95                  return "";
  96  
  97              }
  98              else
  99                  return $s->results;
 100          }
 101  
 102          /**
 103            * Sends a trackback ping to the given url.
 104            *
 105            * @param trackbackUrl the url where to send the trackback ping.
 106           * @param article The article object that is pinging the remote host.
 107            * @param blogName The name of the blog sending the trackback ping
 108            * @return Returns true if successful or false otherwise.
 109            */
 110      	function sendTrackback( $trackbackUrl, $article, $blogInfo )
 111          {
 112              $s = new HttpClient();
 113              $t =& RequestGenerator::getRequestGenerator( $blogInfo );
 114              $formvars["title"]     = $article->getTopic();
 115              // according to MT's implementation, the excerpt should not be longer than 255 characters.
 116              $formvars["excerpt"]   = substr( Textfilter::filterAllHTML( $article->getText() ), 0, 252 )."...";
 117              $formvars["url"]       = $t->postPermalink( $article );
 118              $formvars["blog_name"] = $blogInfo->getBlog();
 119  
 120              // submit the form
 121              if( !$s->submit( $trackbackUrl, $formvars ))
 122                  return false;
 123  
 124              preg_match( "/.*<error>(.*)<\/error>.*/", $s->results, $res );
 125              if( $res[1] == 1 )
 126                  return false;
 127              else
 128                  return true;
 129          }
 130  
 131          /**
 132           * Private function that builds an array with the results for the
 133            * sendTrackbacks function.
 134           *
 135           * @param url The original url where we tried to send a trackback ping.
 136           * @param code Our own internal error code.
 137           * @return Returns an array with two positions, the first one with the url
 138           * and the second one with the error code.
 139           */
 140  	    function _buildResult( $url, $code )
 141          {
 142              $result = Array();
 143              $result["url"] = $url;
 144              $result["status"] = $code;
 145  
 146              return $result;
 147          }
 148  
 149  
 150          /**
 151           * Tries to autodiscover the trackback links in the pages given as parameters.
 152           * The first parameter is an array of urls to blog permalinks which are the urls where
 153           * we would like to try to send trackback pings. The trackback urls will be autodiscovered and
 154           * then we will attempt to send a trackback link if available.
 155           * The result of this function is another array of arrays, where for every position we will
 156           * have another array with two positions: <b>"url"</b> will be the original url and <b>"status"</b> will be a value
 157           * identifying what happened:<ul>
 158           * <li>TRACKBACK_SUCCESS: Trackback was successfully sent</li>
 159           * <li>TRACKBACK_FAILED: There was some kind of problem sending the trackback</li>
 160           * <li>TRACKBACK_UNAVAILABLE: the page did not specify any trackback link.</li>
 161           * </ul>
 162           *
 163           * @param links An array with the urls where we would like to try and send trackback pings.
 164           * @param article The Article object with the information we need to send the ping.
 165           * @param blogName The name of the blog that is sending the information.
 166           * @retun An array with the information explained above.
 167           */
 168      	function sendTrackbacks( $links, $article, $blogName )
 169          {
 170  
 171              $results = Array();
 172              foreach( $links as $link )
 173              {
 174                  // fetch the page
 175                  //print("processing link ".$link."<br/>");
 176                  $page = $this->fetchPage( $link );
 177                  if( $page != "" ) {
 178                      // if the page could be fetched...
 179                      $tbLinks = $this->getTrackbackLinks( $page, $link );
 180                      if( empty( $tbLinks)) {
 181                          // there were no trackback links in the page
 182                          array_push( $results, $this->_buildResult( $link, TRACKBACK_UNAVAILABLE ));
 183                          //print("Error: trackback unavailable<br/>");
 184                      }
 185                      else {
 186                          foreach( $tbLinks as $tbLink ) {
 187                              // try and send a trackback
 188                              //print("sending to tblink: ".$tbLink."<br/>");
 189                              $result = $this->sendTrackback( $tbLink, $article, $blogName );
 190                              if( !$result ) {
 191                                  // if it didn't work, mark it
 192                                  array_push( $results, $this->_buildResult( $link, TRACKBACK_FAILED ));
 193                                  //print("Error: trackback failed<br/>");
 194                              }
 195                              else {
 196                                  // it actually worked, so we say so ;)
 197                                  array_push( $results, $this->_buildResult( $link, TRACKBACK_SUCCESS ));
 198                                  //print("It worked!<br/>");
 199                              }
 200                          }
 201                      }
 202                  }
 203                  else {
 204                      // page could not be fetched, so the trackback was not available
 205                      array_push( $results, $this->_buildResult( $link, TRACKBACK_UNAVAILABLE ));
 206                      //print("Error: page was empty");
 207                  }
 208              }
 209  
 210              return $results;
 211          }
 212  
 213          /**
 214           * Send trackbacks directly.
 215           * The result of this function is another array of arrays, where for every position we will
 216           * have another array with two positions: <b>"url"</b> will be the original url and <b>"status"</b> will be a value
 217           * identifying what happened:<ul>
 218           * <li>TRACKBACK_SUCCESS: Trackback was successfully sent</li>
 219           * <li>TRACKBACK_FAILED: There was some kind of problem sending the trackback</li>
 220           * </ul>
 221           *
 222           * @param trackbacks An array with the urls where we would like to try and send trackback pings.
 223           * @param article The Article object with the information we need to send the ping.
 224           * @param blogName The name of the blog that is sending the information.
 225           * @retun An array with the information explained above.
 226           */
 227      	function sendDirectTrackbacks( $trackbacks, $article, $blogName )
 228          {
 229  
 230              $results = Array();
 231              
 232              // to avoid annoying errors with the foreach() loop in case somebody's passing
 233              // something which is not an array
 234              if( !is_array( $trackbacks ))
 235                $trackbacks = Array();
 236              
 237              foreach( $trackbacks as $trackback )
 238              {
 239                     // try and send a trackback
 240                  //print("sending to tblink: ".$tbLink."<br/>");
 241                     $result = $this->sendTrackback( $trackback, $article, $blogName );
 242                     if( !$result ) {
 243                         // if it didn't work, mark it
 244                         array_push( $results, $this->_buildResult( $trackback, TRACKBACK_FAILED ));
 245                      //print("Error: trackback failed<br/>");
 246                     }
 247                     else {
 248                         // it actually worked, so we say so ;)
 249                         array_push( $results, $this->_buildResult( $trackback, TRACKBACK_SUCCESS ));
 250                      //print("It worked!<br/>");
 251                     }
 252              }
 253  
 254              return $results;
 255          }
 256      }
 257  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics