use new pingback2 class structure/naming
[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
7 function __autoload($className)
8 {
9     $className = ltrim($className, '\\');
10     $fileName  = '';
11     $namespace = '';
12     if ($lastNsPos = strripos($className, '\\')) {
13         $namespace = substr($className, 0, $lastNsPos);
14         $className = substr($className, $lastNsPos + 1);
15         $fileName  = str_replace('\\', '/', $namespace) . '/';
16     }
17     $fileName .= str_replace('_', '/', $className) . '.php';
18
19     require $fileName;
20 }
21
22 $db = new PDO($dbdsn, $dbuser, $dbpass);
23
24 class PingbackStorage
25     implements \PEAR2\Services\Pingback\Server\Callback\IStorage,
26     \PEAR2\Services\Pingback\Server\Callback\ILink
27 {
28     public function __construct(PDO $db)
29     {
30         $this->db = $db;
31     }
32
33     public function storePingback(
34         $target, $source, $sourceBody, \HTTP_Request2_Response $res
35     ) {
36         $stmt = $this->db->prepare(
37             'INSERT INTO pingbacks'
38             . ' (p_source, p_target, p_time, p_client_ip, p_client_agent, p_client_referer)'
39             . ' VALUES(:source, :target, NOW(), :ip, :agent, :referer)'
40         );
41         $stmt->execute(
42             array(
43                 ':source'  => $source,
44                 ':target'  => $target,
45                 ':ip'      => isset($_SERVER['REMOTE_ADDR'])
46                     ? $_SERVER['REMOTE_ADDR'] : '',
47                 ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
48                     ? $_SERVER['HTTP_USER_AGENT'] : '',
49                 ':referer' => isset($_SERVER['HTTP_REFERER'])
50                     ? $_SERVER['HTTP_REFERER'] : '',
51             )
52         );
53     }
54
55     /**
56      * Verifies that a link from $source to $target exists.
57      *
58      * @param string $target     Target URI that should be linked in $source
59      * @param string $source     Pingback source URI that should link to target
60      * @param string $sourceBody Content of $source URI
61      * @param object $res        HTTP response from fetching $source
62      *
63      * @return boolean True if $source links to $target
64      *
65      * @throws Exception When something fatally fails
66      */
67     public function verifyLinkExists(
68         $target, $source, $sourceBody, \HTTP_Request2_Response $res
69     ) {
70         return false;
71     }
72 }
73
74 class PingbackMailer
75     implements \PEAR2\Services\Pingback\Server\Callback\IStorage
76 {
77     public function storePingback(
78         $target, $source, $sourceBody, \HTTP_Request2_Response $res
79     ) {
80         mail(
81             'cweiske@cweiske.de',
82             'New pingback',
83             "A pingback just came in, for\n"
84             . '> '  . $target . "\n"
85             . "from\n"
86             . '> ' . $source . "\n"
87             . "\n\nLove, stapibas",
88             "From: stapibas <server@cweiske.de>"
89         );
90     }
91 }
92
93 $s = new \PEAR2\Services\Pingback\Server();
94 $s->addCallback(new PingbackStorage($db));
95 $s->addCallback(new PingbackMailer());
96 $s->run();
97 ?>