61c374ec5c217d2fa7618901f9ab4fb2a97f538e
[stapibas.git] / src / stapibas / Pingback / DbStorage.php
1 <?php
2 namespace stapibas;
3
4 class Pingback_DbStorage
5     implements \PEAR2\Services\Pingback\Server\Callback\IStorage,
6     \PEAR2\Services\Pingback\Server\Callback\ILink
7 {
8     public function __construct(PDO $db)
9     {
10         $this->db = $db;
11     }
12
13     public function storePingback(
14         $target, $source, $sourceBody, \HTTP_Request2_Response $res
15     ) {
16         if ($this->alreadyExists($target, $source)) {
17             throw new \Exception(
18                 'Pingback from ' . $source . ' to ' . $target
19                 . ' has already been registered.',
20                 48
21             );
22         }
23         $stmt = $this->db->prepare(
24             'INSERT INTO pingbacks SET'
25             . '  p_source = :source'
26             . ', p_target = :target'
27             . ', p_time = NOW()'
28             . ', p_client_ip = :ip'
29             . ', p_client_agent = :agent'
30             . ', p_client_referer = :referer'
31             . ', p_needs_review = 1'
32             . ', p_use = 1'
33             . ', p_needs_update = 1'
34         );
35         $stmt->execute(
36             array(
37                 ':source'  => $source,
38                 ':target'  => $target,
39                 ':ip'      => isset($_SERVER['REMOTE_ADDR'])
40                     ? $_SERVER['REMOTE_ADDR'] : '',
41                 ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
42                     ? $_SERVER['HTTP_USER_AGENT'] : '',
43                 ':referer' => isset($_SERVER['HTTP_REFERER'])
44                     ? $_SERVER['HTTP_REFERER'] : '',
45             )
46         );
47     }
48
49     protected function alreadyExists($target, $source)
50     {
51         $res = $this->db->query(
52             'SELECT COUNT(*) as count FROM pingbacks'
53             . ' WHERE p_source = ' . $this->db->quote($source)
54             . ' AND p_target = ' . $this->db->quote($target)
55         );
56         $answer = $res->fetch(\PDO::FETCH_OBJ);
57         return $answer->count > 0;
58     }
59
60     /**
61      * Verifies that a link from $source to $target exists.
62      *
63      * @param string $target     Target URI that should be linked in $source
64      * @param string $source     Pingback source URI that should link to target
65      * @param string $sourceBody Content of $source URI
66      * @param object $res        HTTP response from fetching $source
67      *
68      * @return boolean True if $source links to $target
69      *
70      * @throws Exception When something fatally fails
71      */
72     public function verifyLinkExists(
73         $target, $source, $sourceBody, \HTTP_Request2_Response $res
74     ) {
75         return false;
76     }
77 }
78 ?>