00001 <?php
00002
00003 lt_include( PLOG_CLASS_PATH."class/net/http/httpclient.class.php" );
00004 lt_include( PLOG_CLASS_PATH."class/dao/article.class.php" );
00005 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
00006 lt_include( PLOG_CLASS_PATH."class/net/requestgenerator.class.php" );
00007 lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
00008
00009 define( "TRACKBACK_SUCCESS", 0 );
00010 define( "TRACKBACK_FAILED", 1 );
00011 define( "TRACKBACK_UNAVAILABLE", 2 );
00012
00021 class TrackbackClient
00022 {
00023
00027 function TrackbackClient()
00028 {
00029
00030 }
00031
00045 function getTrackbackLinks( $page, $pageUrl = "" ) {
00046 $regexp ="#<rdf:RDF.*?</rdf:RDF>#s";
00047 preg_match_all( $regexp, $page, $out, PREG_SET_ORDER );
00048 $links = Array();
00049 foreach( $out as $result ) {
00050
00051
00052
00053
00054 if(preg_match( "/dc:identifier=\"([^\"]+)\"/", $result[0], $dc_ident))
00055 $dcIdentifier = $dc_ident[1];
00056
00057
00058 if(preg_match( "/trackback:ping=\"([^\"]+)\"/", $result[0], $tb_ping)) {
00059 $tbPing = $tb_ping[1];
00060
00061
00062
00063 if( $dcIdentifier && $pageUrl ) {
00064 $matched = 0;
00065
00066 $len1 = strlen($pageUrl);
00067 $len2 = strlen($dcIdentifier);
00068 $length = ($len1 > $len2) ? $len2 : $len1;
00069 if( strncasecmp($dcIdentifier,$pageUrl,$length) == 0)
00070 $matched = 1;
00071
00072 $len1 = strlen(htmlentities($pageUrl));
00073 $len2 = strlen($dcIdentifier);
00074 $length = ($len1 > $len2) ? $len2 : $len1;
00075 if( strncasecmp($dcIdentifier,htmlentities($pageUrl),$length) == 0)
00076 $matched = 1;
00077
00078 $len1 = strlen($pageUrl);
00079 $len2 = strlen(urldecode($dcIdentifier));
00080 $length = ($len1 > $len2) ? $len2 : $len1;
00081 if( strncasecmp(urldecode($dcIdentifier),$pageUrl,$length) == 0)
00082 $matched = 1;
00083
00084 if($matched == 1){
00085
00086 array_push( $links, $tbPing );
00087 }
00088 }
00089 else {
00090 array_push( $links, $tbPing );
00091 }
00092 }
00093 }
00094
00095 return $links;
00096 }
00097
00105 function fetchPage( $url )
00106 {
00107 $s = new HttpClient();
00108 $result = $s->fetch( $url );
00109
00110 if( !$result ) {
00111 if($s->timed_out)
00112
00113 return "";
00114
00115 }
00116 else
00117 return $s->results;
00118 }
00119
00128 function sendTrackback( $trackbackUrl, $article, $blogInfo )
00129 {
00130 $s = new HttpClient();
00131 $t =& RequestGenerator::getRequestGenerator( $blogInfo );
00132 $formvars["title"] = $article->getTopic();
00133
00134 $formvars["excerpt"] = substr( Textfilter::filterAllHTML( $article->getText() ), 0, 252 )."...";
00135 $formvars["url"] = $t->postPermalink( $article );
00136 $formvars["blog_name"] = $blogInfo->getBlog();
00137
00138
00139 if( !$s->submit( $trackbackUrl, $formvars ))
00140 return false;
00141
00142 $matched = preg_match( "/.*<error>(.*)<\/error>.*/", $s->results, $res );
00143
00144 if( !$matched || $res[1] == 1 )
00145 return false;
00146 else
00147 return true;
00148 }
00149
00159 function _buildResult( $url, $code )
00160 {
00161 $result = Array();
00162 $result["url"] = $url;
00163 $result["status"] = $code;
00164
00165 return $result;
00166 }
00167
00168
00187 function sendTrackbacks( $links, $article, $blogName )
00188 {
00189
00190 $results = Array();
00191 foreach( $links as $link )
00192 {
00193
00194
00195 $page = $this->fetchPage( $link );
00196 if( $page != "" ) {
00197
00198 $tbLinks = $this->getTrackbackLinks( $page, $link );
00199 if( empty( $tbLinks)) {
00200
00201 array_push( $results, $this->_buildResult( $link, TRACKBACK_UNAVAILABLE ));
00202
00203 }
00204 else {
00205 foreach( $tbLinks as $tbLink ) {
00206
00207
00208 $result = $this->sendTrackback( $tbLink, $article, $blogName );
00209 if( !$result ) {
00210
00211 array_push( $results, $this->_buildResult( $link, TRACKBACK_FAILED ));
00212
00213 }
00214 else {
00215
00216 array_push( $results, $this->_buildResult( $link, TRACKBACK_SUCCESS ));
00217
00218 }
00219 }
00220 }
00221 }
00222 else {
00223
00224 array_push( $results, $this->_buildResult( $link, TRACKBACK_UNAVAILABLE ));
00225
00226 }
00227 }
00228
00229 return $results;
00230 }
00231
00246 function sendDirectTrackbacks( $trackbacks, $article, $blogName )
00247 {
00248
00249 $results = Array();
00250
00251
00252
00253 if( !is_array( $trackbacks ))
00254 $trackbacks = Array();
00255
00256 foreach( $trackbacks as $trackback )
00257 {
00258
00259
00260 $result = $this->sendTrackback( $trackback, $article, $blogName );
00261 if( !$result ) {
00262
00263 array_push( $results, $this->_buildResult( $trackback, TRACKBACK_FAILED ));
00264
00265 }
00266 else {
00267
00268 array_push( $results, $this->_buildResult( $trackback, TRACKBACK_SUCCESS ));
00269
00270 }
00271 }
00272
00273 return $results;
00274 }
00275 }
00276 ?>