2008-09-12 04:09:13 +02:00
< ? php
2010-06-22 03:13:10 +02:00
/**
2010-06-23 07:50:02 +02:00
* Add trackback ( receive and send ) feature blog entry
2010-06-22 03:13:10 +02:00
*/
2008-09-12 04:09:13 +02:00
class TrackBackDecorator extends DataObjectDecorator {
2010-06-22 03:13:10 +02:00
static $trackback_server_class = 'TrackbackHTTPServer' ;
2009-05-20 07:37:30 +02:00
function extraStatics () {
2008-09-12 04:09:13 +02:00
return array (
'has_many' => array (
2010-06-23 07:50:02 +02:00
'TrackBackURLs' => 'TrackBackURL' ,
2008-09-12 04:09:13 +02:00
'TrackBacks' => 'TrackBackPing'
)
);
}
2010-06-22 03:13:10 +02:00
function updateCMSFields ( $fields ) {
// Trackback URL field
if ( $this -> owner -> TrackBacksEnabled ()) {
2010-06-23 07:50:02 +02:00
$trackbackURLTable = new ComplexTableField (
$this ,
'TrackBackURLs' ,
'TrackBackURL' ,
array (
'URL' => 'URL' ,
'IsPung' => 'Pung?'
),
'getCMSFields_forPopup' ,
'' ,
'ID'
);
$fields -> addFieldToTab ( " Root.Content.Main " , $trackbackURLTable );
2010-06-22 03:13:10 +02:00
}
else {
2010-06-23 07:50:02 +02:00
$fields -> addFieldToTab ( " Root.Content.Main " , new ReadonlyField ( " TrackBackURLsReadOnly " , _t ( " BlogEntry.TrackbackURLs " , " Trackback URLs " ), _t ( " BlogEntry.TrackbackURLs_DISABLED " , " To use this feature, please check 'Enable TrackBacks' check box on the blog holder. " )));
2010-06-22 03:13:10 +02:00
}
}
function onBeforePublish () {
2010-06-23 07:50:02 +02:00
if ( ! $this -> owner -> TrackBacksEnabled () && ! $this -> owner -> TrackBackURLs ()) return ;
foreach ( $this -> owner -> TrackBackURLs () as $trackBackURL ) {
if ( ! $trackBackURL -> Pung && $this -> trackbackNotify ( $trackBackURL -> URL )) {
$trackBackURL -> Pung = true ;
$trackBackURL -> write ();
2010-06-22 03:13:10 +02:00
}
2010-06-23 07:50:02 +02:00
}
2010-06-22 03:13:10 +02:00
}
/**
* Trackback notify the specified trackback url
* @ param boolean | true on success , otherwise false
*/
2010-06-23 07:50:02 +02:00
function trackbackNotify ( $url ) {
2010-06-22 03:13:10 +02:00
$content = new HTMLText ( 'Content' );
2010-06-23 07:50:02 +02:00
$content -> setValue ( $this -> owner -> Content );
2010-06-22 03:13:10 +02:00
$excerpt = $content -> FirstParagraph ();
2010-06-23 07:50:02 +02:00
if ( $this -> owner -> Parent () && $this -> owner -> ParentID > 0 ) {
$blogName = $this -> owner -> Parent () -> Title ;
2010-06-22 03:13:10 +02:00
}
else {
$blogName = " " ;
}
2010-06-23 07:50:02 +02:00
2010-06-22 03:13:10 +02:00
$postData = array (
2010-06-23 07:50:02 +02:00
'url' => $this -> owner -> AbsoluteLink (),
'title' => $this -> owner -> Title ,
2010-06-22 03:13:10 +02:00
'excerpt' => $excerpt ,
'blog_name' => $blogName
);
2010-06-23 07:50:02 +02:00
2010-06-22 03:13:10 +02:00
$controller = Object :: create ( self :: $trackback_server_class );
2010-06-23 07:50:02 +02:00
$response = $controller -> request ( $url , $postData );
2010-06-22 03:13:10 +02:00
if ( $response -> getStatusCode () == '200' && stripos ( $response -> getBody (), " <error>0</error> " ) !== false ) {
return true ;
}
return false ;
}
2008-09-12 04:09:13 +02:00
function updateMetaTags ( & $tags ) {
$tags .= $this -> owner -> renderWith ( 'TrackBackRdf' );
}
function TrackBackPingLink () {
return $this -> owner -> AbsoluteLink () . 'trackbackping' ;
}
2009-07-28 07:33:22 +02:00
function decoratedTrackbackping () {
2008-09-12 04:09:13 +02:00
$error = 0 ;
$message = '' ;
if ( ! ( isset ( $_POST [ 'url' ]) && $_POST [ 'url' ])) {
$error = 1 ;
$message = 'Missing required POST parameter \'url\'.' ;
} else {
$trackbackping = new TrackBackPing ();
$trackbackping -> Url = $_POST [ 'url' ];
if ( isset ( $_POST [ 'title' ]) && $_POST [ 'title' ]) {
$trackbackping -> Title = $_POST [ 'title' ];
}
if ( isset ( $_POST [ 'excerpt' ]) && $_POST [ 'excerpt' ]) {
$trackbackping -> Excerpt = $_POST [ 'excerpt' ];
}
if ( isset ( $_POST [ 'blog_name' ]) && $_POST [ 'blog_name' ]) {
$trackbackping -> BlogName = $_POST [ 'blog_name' ];
}
$trackbackping -> PageID = $this -> owner -> ID ;
$trackbackping -> write ();
}
$returnData = new ArrayData ( array (
'Error' => $error ,
'Message' => $message
));
return $returnData -> renderWith ( 'TrackBackPingReturn' );
}
}
2010-06-22 03:13:10 +02:00
/**
* Example :
* $controller = Object :: create ( 'TrackbackHTTPClient' );
* $response = $controller -> request ( new SS_HTTPRequest ( 'POST' , $url , null , $postData ));
*/
class TrackbackHTTPServer {
function __construct () {}
/**
* @ param string
* @ param array
* @ return SS_HTTPResponse
*/
function request ( $url , $data ) {
$ch = curl_init ( $url );
2010-06-23 07:50:02 +02:00
curl_setopt ( $ch , CURLOPT_FRESH_CONNECT , true );
2010-06-22 03:13:10 +02:00
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt ( $ch , CURLOPT_POST , true );
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data );
$response = curl_exec ( $ch );
$statusCode = curl_getinfo ( $ch , CURLINFO_HTTP_CODE );
curl_close ( $ch );
return new SS_HTTPResponse ( $response , $statusCode );
}
}
2008-09-12 04:09:13 +02:00
?>