Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
YourlsProxy
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
8
 getError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isError
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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;
13
14use Exception;
15
16/**
17 * YourlsProxy
18 *
19 * Forwards a URL for shortening to YOURLS (your own URL shortener) and stores
20 * the result.
21 */
22class YourlsProxy
23{
24    /**
25     * error message
26     *
27     * @access private
28     * @var    string
29     */
30    private $_error = '';
31
32    /**
33     * shortened URL
34     *
35     * @access private
36     * @var    string
37     */
38    private $_url = '';
39
40    /**
41     * constructor
42     *
43     * initializes and runs PrivateBin
44     *
45     * @access public
46     * @param string $link
47     */
48    public function __construct(Configuration $conf, $link)
49    {
50        if (!str_starts_with($link, $conf->getKey('basepath') . '?')) {
51            $this->_error = 'Trying to shorten a URL that isn\'t pointing at our instance.';
52            return;
53        }
54
55        $yourls_api_url = $conf->getKey('apiurl', 'yourls');
56        if (empty($yourls_api_url)) {
57            $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
58            return;
59        }
60
61        $data = file_get_contents(
62            $yourls_api_url, false, stream_context_create(
63                array(
64                    'http' => array(
65                        'method'  => 'POST',
66                        'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
67                        'content' => http_build_query(
68                            array(
69                                'signature' => $conf->getKey('signature', 'yourls'),
70                                'format'    => 'json',
71                                'action'    => 'shorturl',
72                                'url'       => $link,
73                            )
74                        ),
75                    ),
76                )
77            )
78        );
79        try {
80            $data = Json::decode($data);
81        } catch (Exception $e) {
82            $this->_error = 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".';
83            error_log('Error calling YOURLS: ' . $e->getMessage());
84            return;
85        }
86
87        if (
88            !is_null($data) &&
89            array_key_exists('statusCode', $data) &&
90            $data['statusCode'] == 200 &&
91            array_key_exists('shorturl', $data)
92        ) {
93            $this->_url = $data['shorturl'];
94        } else {
95            $this->_error = 'Error parsing YOURLS response.';
96        }
97    }
98
99    /**
100     * Returns the (untranslated) error message
101     *
102     * @access public
103     * @return string
104     */
105    public function getError()
106    {
107        return $this->_error;
108    }
109
110    /**
111     * Returns the shortened URL
112     *
113     * @access public
114     * @return string
115     */
116    public function getUrl()
117    {
118        return $this->_url;
119    }
120
121    /**
122     * Returns true if any error has occurred
123     *
124     * @access public
125     * @return bool
126     */
127    public function isError()
128    {
129        return !empty($this->_error);
130    }
131}