PEAR2_Services_LinkbackPEAR2_Services_Linkback-0.2.1/src/PEAR2/Services/Linkback/Server/Responder/Webmention.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * This file is part of the PEAR2\Services\Linkback package.
 *
 * PHP version 5
 *
 * @category Services
 * @package  PEAR2\Services\Linkback
 * @author   Christian Weiske <cweiske@php.net>
 * @license  http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link     http://pear2.php.net/package/Services_Linkback
 */
namespace PEAR2\Services\Linkback\Server\Responder;
use PEAR2\Services\Linkback\States;

/**
 * Sends HTTP headers and a webmention result back to the client.
 *
 * @category Services
 * @package  PEAR2\Services\Linkback
 * @author   Christian Weiske <cweiske@php.net>
 * @license  http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link     http://pear2.php.net/package/Services_Linkback
 */
class Webmention extends Base
{
    protected static $htmlTemplate = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>Webmention: %TITLE%</title>
 </head>
 <body>
  <h1>Webmention: %TITLE%</h1>
  <p>%MESSAGE%</p>
 </body>
</html>

XML;

    protected static $arCodeNames = array(
        States::ACCESS_DENIED        => 'access_denied',
        States::ALREADY_REGISTERED   => 'already_registered',
        States::INVALID_URI          => 'invalid_uri',
        States::NO_LINK_IN_SOURCE    => 'no_link_found',
        States::PINGBACK_UNSUPPORTED => 'target_not_supported',
        States::SOURCE_NOT_LOADED    => 'source_not_loaded',
        States::SOURCE_URI_NOT_FOUND => 'source_not_found',
        States::SPAM                 => 'spam',
        States::TARGET_URI_NOT_FOUND => 'target_not_found',
    );

    /**
     * Send the given response back to the client.
     * Sends the correct headers.
     *
     * @param mixed $res Array of return values ('faultCode' and 'faultString'),
     *                   or single string if all is fine
     *
     * @return void
     */
    public function send($res)
    {
        if (is_array($res)) {
            $this->sendHeader('HTTP/1.0 400 Bad Request');
        } else {
            $this->sendHeader('HTTP/1.0 202 Accepted');
        }

        $http = new \HTTP2();
        $supportedTypes = array(
            'application/xhtml+xml', 'text/html',
            'application/json',
            'text/plain'
        );

        $type = $http->negotiateMimeType($supportedTypes, 'text/plain');
        $this->sendHeader('Content-type: ' . $type . '; charset=utf-8');

        $outputType = $type;
        if ($outputType == 'application/xhtml+xml') {
            $outputType = 'text/html';
        }
        if (is_array($res)) {
            $this->sendError($outputType, $res['faultCode'], $res['faultString']);
        } else {
            $this->sendOk($outputType, $res);
        }
    }

    protected function sendError($type, $nCode, $message)
    {
        if ($type == 'application/json') {
            $this->sendOutput(
                json_encode(
                    (object) array(
                        'error' => $this->getCodeName($nCode),
                        'error_description' => $message
                    )
                )
            );
        } else if ($type == 'text/html') {
            $this->sendOutput(
                str_replace(
                    array('%TITLE%', '%MESSAGE%'),
                    array(
                        str_replace('_', ' ', $this->getCodeName($nCode)),
                        $message
                    ),
                    self::$htmlTemplate
                )
            );
        } else {
            $this->sendOutput(
                'Webmention error #' . $nCode . ': '
                . str_replace('_', ' ', $this->getCodeName($nCode)) . "\n"
                . $message . "\n"
            );
        }
    }

    protected function sendOk($type, $message)
    {
        if ($type == 'application/json') {
            $this->sendOutput(
                json_encode(
                    (object) array(
                        'result' => $message
                    )
                )
            );
        } else if ($type == 'text/html') {
            $this->sendOutput(
                str_replace(
                    array('%TITLE%', '%MESSAGE%'),
                    array('All fine', $message),
                    self::$htmlTemplate
                )
            );
        } else {
            $this->sendOutput(
                'OK. ' . $message . "\n"
            );
        }
    }

    protected function getCodeName($nCode)
    {
        if (isset(self::$arCodeNames[$nCode])) {
            return self::$arCodeNames[$nCode];
        }
        return 'unknown_error (' . $nCode . ')';
    }
}
?>
EOF