first simple code
[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\Pingback2\Server_Callback_IStorage,
26     \PEAR2\Services\Pingback2\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      * Verifies that a link from $source to $target exists.
56      *
57      * @param string $target     Target URI that should be linked in $source
58      * @param string $source     Pingback source URI that should link to target
59      * @param string $sourceBody Content of $source URI
60      * @param object $res        HTTP response from fetching $source
61      *
62      * @return boolean True if $source links to $target
63      *
64      * @throws Exception When something fatally fails
65      */
66     public function verifyLinkExists(
67         $target, $source, $sourceBody, \HTTP_Request2_Response $res
68     ) {
69         return false;
70     }
71 }
72
73 $s = new \PEAR2\Services\Pingback2\Server();
74 $s->addCallback(new PingbackStorage($db));
75 $s->run();
76 ?>