Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (success)
86.96%
20 / 23
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChhotoProxy
86.96% covered (success)
86.96%
20 / 23
66.67% covered (warning)
66.67%
2 / 3
6.08
0.00% covered (danger)
0.00%
0 / 1
 _getProxyUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 _getProxyPayload
82.35% covered (success)
82.35%
14 / 17
0.00% covered (danger)
0.00%
0 / 1
2.02
 _extractShortUrl
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php declare(strict_types=1);
2/**
3 * PrivateBin
4 *
5 * a zero-knowledge paste bin
6 *
7 * @link      https://github.com/PrivateBin/PrivateBin
8 * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
9 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10 */
11
12namespace PrivateBin\Proxy;
13
14use JsonException;
15use PrivateBin\Configuration;
16use PrivateBin\Json;
17
18/**
19 * ChhotoProxy
20 *
21 * Forwards a URL for shortening to Chhoto URL and stores the result.
22 */
23class ChhotoProxy extends AbstractProxy
24{
25    /**
26     * Overrides the abstract parent function to get the proxy URL.
27     *
28     * @param Configuration $conf
29     * @return string
30     */
31    protected function _getProxyUrl(Configuration $conf): string
32    {
33        return $conf->getKey('apiurl', 'chhoto');
34    }
35
36    /**
37     * Overrides the abstract parent function to get contents from Chhoto API.
38     *
39     * @access protected
40     * @param Configuration $conf
41     * @param string $link
42     * @return array
43     */
44    protected function _getProxyPayload(Configuration $conf, string $link): array
45    {
46        $apiKey = $conf->getKey('apikey', 'chhoto');
47
48        $body = [
49            'shortlink'    => '',          // empty = auto-generate
50            'longlink'     => $link,
51            'expiry_delay' => 0,           // 0 = never expire
52            'notes'        => 'PrivateBin paste',
53        ];
54
55        try {
56            return [
57                'method'  => 'POST',
58                'header'  => "Content-Type: application/json\r\n" .
59                             'X-API-Key: ' . $apiKey . "\r\n" .
60                             "Accept: application/json\r\n",
61                'content' => Json::encode($body),
62            ];
63        } catch (JsonException $e) {
64            error_log('[' . get_class($this) . '] Error encoding body: ' . $e->getMessage());
65            return [];
66        }
67    }
68
69    /**
70     * Extracts the short URL from the Chhoto API response.
71     *
72     * @access protected
73     * @param array $data
74     * @return ?string
75     */
76    protected function _extractShortUrl(array $data): ?string
77    {
78        // Chhoto usually returns "shorturl"
79        if (!empty($data['shorturl'])) {
80            return $data['shorturl'];
81        }
82
83        // Fallback for older versions that return only the slug
84        if (!empty($data['shortlink'])) {
85            return $this->_proxyUrl . ltrim($data['shortlink'], '/');
86        }
87
88        return null;
89    }
90}