2008-09-12 04:09:13 +02:00
< ? php
2010-06-22 03:13:10 +02:00
/**
* TODO : Multiple trackback urls for each blog entry
*/
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 (
2010-06-22 03:13:10 +02:00
'db' => array (
'TrackbackURL' => 'Varchar(2048)' ,
'PungTrackbackURL' => 'Varchar(2048)'
),
2008-09-12 04:09:13 +02:00
'has_many' => array (
'TrackBacks' => 'TrackBackPing'
)
);
}
2010-06-22 03:13:10 +02:00
function updateCMSFields ( $fields ) {
// Trackback URL field
if ( $this -> owner -> TrackBacksEnabled ()) {
$fields -> addFieldToTab ( " Root.Content.Main " , new TextField ( " TrackbackURL " , _t ( " BlogEntry.TRACKBACKURL " , " Trackback URL " )), " Content " );
}
else {
$fields -> addFieldToTab ( " Root.Content.Main " , new ReadonlyField ( " TrackbackURLReadOnly " , _t ( " BlogEntry.TRACKBACKURL " , " Trackback URL " ), _t ( " BlogEntry.TRACKBACKURL_DISABLED " , " To use this feature, please check 'Enable TrackBacks' check box on the blog holder. " )), " Content " );
}
}
function onBeforePublish () {
$owner = $this -> owner ;
if ( ! empty ( $owner -> TrackbackURL ) && $owner -> TrackBacksEnabled () && $owner -> ShouldTrackbackNotify ()) {
if ( $this -> trackbackNotify ()) {
$owner -> PungTrackbackURL = $owner -> TrackbackURL ;
$owner -> write ();
}
}
}
/**
* Trackback notify the specified trackback url
* @ param boolean | true on success , otherwise false
*/
function trackbackNotify () {
$owner = $this -> owner ;
$content = new HTMLText ( 'Content' );
$content -> setValue ( $owner -> Content );
$excerpt = $content -> FirstParagraph ();
if ( $owner -> Parent () && $owner -> ParentID > 0 ) {
$blogName = $owner -> Parent () -> Title ;
}
else {
$blogName = " " ;
}
$postData = array (
'url' => $owner -> AbsoluteLink (),
'title' => $owner -> Title ,
'excerpt' => $excerpt ,
'blog_name' => $blogName
);
$controller = Object :: create ( self :: $trackback_server_class );
$response = $controller -> request ( $owner -> TrackbackURL , $postData );
if ( $response -> getStatusCode () == '200' && stripos ( $response -> getBody (), " <error>0</error> " ) !== false ) {
return true ;
}
return false ;
}
function ShouldTrackbackNotify () {
return ( trim ( $this -> owner -> TrackbackURL ) != '' && $this -> owner -> TrackbackURL != $this -> owner -> PungTrackbackURL );
}
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 );
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
?>