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