first work on rendering
[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     \PEAR2\Services\Pingback\Server\Callback\ITarget
8 {
9     public function __construct(PDO $db)
10     {
11         $this->db = $db;
12     }
13
14     /**
15      * Verifies that the given target URI exists in our system.
16      *
17      * @param string $target Target URI that got linked to
18      *
19      * @return boolean True if the target URI exists, false if not
20      *
21      * @throws Exception When something fatally fails
22      */
23     public function verifyTargetExists($target)
24     {
25         $res = $this->db->query(
26             'SELECT COUNT(*) as count FROM pingbacktargets'
27             . ' WHERE ' . $this->db->quote($target) . ' LIKE pt_url'
28         );
29         $answer = $res->fetch(\PDO::FETCH_OBJ);
30         if ($answer->count == 0) {
31             throw new \Exception(
32                 'The specified target URI cannot be used as a target.',
33                 33
34             );
35         }
36
37         return true;
38     }
39
40     public function storePingback(
41         $target, $source, $sourceBody, \HTTP_Request2_Response $res
42     ) {
43         if ($this->alreadyExists($target, $source)) {
44             throw new \Exception(
45                 'Pingback from ' . $source . ' to ' . $target
46                 . ' has already been registered.',
47                 48
48             );
49         }
50         $stmt = $this->db->prepare(
51             'INSERT INTO pingbacks SET'
52             . '  p_source = :source'
53             . ', p_target = :target'
54             . ', p_time = NOW()'
55             . ', p_client_ip = :ip'
56             . ', p_client_agent = :agent'
57             . ', p_client_referer = :referer'
58             . ', p_needs_review = 1'
59             . ', p_use = 1'
60             . ', p_needs_update = 1'
61         );
62         $stmt->execute(
63             array(
64                 ':source'  => $source,
65                 ':target'  => $target,
66                 ':ip'      => isset($_SERVER['REMOTE_ADDR'])
67                     ? $_SERVER['REMOTE_ADDR'] : '',
68                 ':agent'   => isset($_SERVER['HTTP_USER_AGENT'])
69                     ? $_SERVER['HTTP_USER_AGENT'] : '',
70                 ':referer' => isset($_SERVER['HTTP_REFERER'])
71                     ? $_SERVER['HTTP_REFERER'] : '',
72             )
73         );
74     }
75
76     protected function alreadyExists($target, $source)
77     {
78         $res = $this->db->query(
79             'SELECT COUNT(*) as count FROM pingbacks'
80             . ' WHERE p_source = ' . $this->db->quote($source)
81             . ' AND p_target = ' . $this->db->quote($target)
82         );
83         $answer = $res->fetch(\PDO::FETCH_OBJ);
84         return $answer->count > 0;
85     }
86
87     /**
88      * Verifies that a link from $source to $target exists.
89      *
90      * @param string $target     Target URI that should be linked in $source
91      * @param string $source     Pingback source URI that should link to target
92      * @param string $sourceBody Content of $source URI
93      * @param object $res        HTTP response from fetching $source
94      *
95      * @return boolean True if $source links to $target
96      *
97      * @throws Exception When something fatally fails
98      */
99     public function verifyLinkExists(
100         $target, $source, $sourceBody, \HTTP_Request2_Response $res
101     ) {
102         return false;
103     }
104 }
105 ?>