first work on feed updater + pinger
[stapibas.git] / www / xmlrpc.php
1 <?php
2 /**
3  * Simply stores all pingbacks in the database.
4  */
5 require_once __DIR__ . '/../data/config.php';
6 require_once 'stapibas/autoloader.php';
7
8 $db = new PDO($dbdsn, $dbuser, $dbpass);
9
10 class PingbackStorage
11     implements \PEAR2\Services\Pingback\Server\Callback\IStorage,
12     \PEAR2\Services\Pingback\Server\Callback\ILink
13 {
14     public function __construct(PDO $db)
15     {
16         $this->db = $db;
17     }
18
19     public function storePingback(
20         $target, $source, $sourceBody, \HTTP_Request2_Response $res
21     ) {
22         $stmt = $this->db->prepare(
23             'INSERT INTO pingbacks'
24             . ' (p_source, p_target, p_time, p_client_ip, p_client_agent, p_client_referer)'
25             . ' VALUES(:source, :target, NOW(), :ip, :agent, :referer)'
26         );
27         $stmt->execute(
28             array(
29                 ':source'  => $source,
30                 ':target'  => $target,
31                 ':ip'      => isset($_SERVER['REMOTE_ADDR'])
32                     ? $_SERVER['REMOTE_ADDR'] : '',
33                 ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
34                     ? $_SERVER['HTTP_USER_AGENT'] : '',
35                 ':referer' => isset($_SERVER['HTTP_REFERER'])
36                     ? $_SERVER['HTTP_REFERER'] : '',
37             )
38         );
39     }
40
41     /**
42      * Verifies that a link from $source to $target exists.
43      *
44      * @param string $target     Target URI that should be linked in $source
45      * @param string $source     Pingback source URI that should link to target
46      * @param string $sourceBody Content of $source URI
47      * @param object $res        HTTP response from fetching $source
48      *
49      * @return boolean True if $source links to $target
50      *
51      * @throws Exception When something fatally fails
52      */
53     public function verifyLinkExists(
54         $target, $source, $sourceBody, \HTTP_Request2_Response $res
55     ) {
56         return false;
57     }
58 }
59
60 class PingbackMailer
61     implements \PEAR2\Services\Pingback\Server\Callback\IStorage
62 {
63     public function storePingback(
64         $target, $source, $sourceBody, \HTTP_Request2_Response $res
65     ) {
66         mail(
67             'cweiske@cweiske.de',
68             'New pingback',
69             "A pingback just came in, for\n"
70             . '> '  . $target . "\n"
71             . "from\n"
72             . '> ' . $source . "\n"
73             . "\n\nLove, stapibas",
74             "From: stapibas <server@cweiske.de>"
75         );
76     }
77 }
78
79 $s = new \PEAR2\Services\Pingback\Server();
80 $s->addCallback(new PingbackStorage($db));
81 $s->addCallback(new PingbackMailer());
82 $s->run();
83 ?>