Index: trunk/phase3/trackback.php |
— | — | @@ -7,55 +7,79 @@ |
8 | 8 | |
9 | 9 | require_once( './includes/WebStart.php' ); |
10 | 10 | |
11 | | -function XMLsuccess() { |
12 | | - header( "Content-Type: application/xml; charset=utf-8" ); |
13 | | - echo "<?xml version=\"1.0\" encoding=\"utf-8\"?> |
| 11 | +class TrackBack { |
| 12 | + |
| 13 | + private $r, $url, $title = null; |
| 14 | + |
| 15 | + private function XMLsuccess() { |
| 16 | + header( "Content-Type: application/xml; charset=utf-8" ); |
| 17 | + echo <<<XML |
| 18 | +<?xml version="1.0" encoding="utf-8"?> |
14 | 19 | <response> |
15 | | -<error>0</error> |
| 20 | + <error>0</error> |
16 | 21 | </response> |
17 | | - "; |
18 | | - exit; |
19 | | -} |
| 22 | +XML; |
| 23 | + exit; |
| 24 | + } |
20 | 25 | |
21 | | -function XMLerror( $err = "Invalid request." ) { |
22 | | - header( "HTTP/1.0 400 Bad Request" ); |
23 | | - header( "Content-Type: application/xml; charset=utf-8" ); |
24 | | - echo "<?xml version=\"1.0\" encoding=\"utf-8\"?> |
| 26 | + private function XMLerror( $err = "Invalid request." ) { |
| 27 | + header( "HTTP/1.0 400 Bad Request" ); |
| 28 | + header( "Content-Type: application/xml; charset=utf-8" ); |
| 29 | + echo <<<XML |
| 30 | +<?xml version="1.0" encoding="utf-8"?> |
25 | 31 | <response> |
26 | | -<error>1</error> |
27 | | -<message>Invalid request: $err</message> |
| 32 | + <error>1</error> |
| 33 | + <message>Invalid request: $err</message> |
28 | 34 | </response> |
29 | | -"; |
30 | | - exit; |
31 | | -} |
| 35 | +XML; |
| 36 | + exit; |
| 37 | + } |
32 | 38 | |
33 | | -if( !$wgUseTrackbacks ) |
34 | | - XMLerror("Trackbacks are disabled."); |
| 39 | + public function __construct() { |
| 40 | + global $wgUseTrackbacks, $wgRequest; |
35 | 41 | |
36 | | -if( !isset( $_POST['url'] ) |
37 | | - || !isset( $_REQUEST['article'] ) ) |
38 | | - XMLerror("Required field not specified"); |
| 42 | + if( !$wgUseTrackbacks && false ) |
| 43 | + $this->XMLerror( "Trackbacks are disabled" ); |
39 | 44 | |
40 | | -$dbw = wfGetDB( DB_MASTER ); |
| 45 | + $this->r = $wgRequest; |
41 | 46 | |
42 | | -$tbtitle = strval( @$_POST['title'] ); |
43 | | -$tbex = strval( @$_POST['excerpt'] ); |
44 | | -$tburl = strval( $_POST['url'] ); |
45 | | -$tbname = strval( @$_POST['blog_name'] ); |
46 | | -$tbarticle = strval( $_REQUEST['article'] ); |
| 47 | + if( !$this->r->wasPosted() ) { |
| 48 | + $this->XMLerror( "Must be posted" ); |
| 49 | + } |
47 | 50 | |
48 | | -$title = Title::newFromText($tbarticle); |
49 | | -if( !$title || !$title->exists() ) |
50 | | - XMLerror( "Specified article does not exist." ); |
| 51 | + $this->url = $wgRequest->getText( 'url' ); |
| 52 | + $article = $wgRequest->getText( 'article' ); |
51 | 53 | |
52 | | -$dbw->insert('trackbacks', array( |
53 | | - 'tb_page' => $title->getArticleID(), |
54 | | - 'tb_title' => $tbtitle, |
55 | | - 'tb_url' => $tburl, |
56 | | - 'tb_ex' => $tbex, |
57 | | - 'tb_name' => $tbname |
58 | | -)); |
| 54 | + if( !$this->url || !$article ) { |
| 55 | + $this->XMLerror( "Required field not specified" ); |
| 56 | + } |
59 | 57 | |
60 | | -$dbw->commit(); |
| 58 | + $this->title = Title::newFromText( $article ); |
| 59 | + if( !$this->title || !$this->title->exists() ) { |
| 60 | + $this->XMLerror( "Specified article does not exist." ); |
| 61 | + } |
| 62 | + } |
61 | 63 | |
62 | | -XMLsuccess(); |
| 64 | + public function write() { |
| 65 | + $dbw = wfGetDB( DB_MASTER ); |
| 66 | + |
| 67 | + $tbtitle = $this->r->getText( 'title' ); |
| 68 | + $tbex = $this->r->getText( 'excerpt' ); |
| 69 | + $tbname = $this->r->getText( 'blog_name' ); |
| 70 | + |
| 71 | + $dbw->insert('trackbacks', array( |
| 72 | + 'tb_page' => $this->title->getArticleID(), |
| 73 | + 'tb_title' => $tbtitle, |
| 74 | + 'tb_url' => $this->url, |
| 75 | + 'tb_ex' => $tbex, |
| 76 | + 'tb_name' => $tbname |
| 77 | + )); |
| 78 | + |
| 79 | + $dbw->commit(); |
| 80 | + |
| 81 | + $this->XMLsuccess(); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +$tb = new TrackBack(); |
| 86 | +$tb->write(); |