summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Thumbnailer/Phancap.php
blob: 1b76849e66f2700b6526609adc02ee999bd48f52 (plain)
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
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * Show website thumbnails/screenshots using phancap
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 * @see      http://cweiske.de/phancap.htm
 */
class SemanticScuttle_Thumbnailer_Phancap
{
    /**
     * Configuration array.
     * Required keys:
     * - url
     * - token
     * - secret
     */
    protected $config = array();

    /**
     * Set phancap configuration
     *
     * @param array $config Phancap configuration
     *
     * @return void
     */
    public function setConfig($config)
    {
        $this->config = $config;
    }

    /**
     * Get the URL for a website thumbnail
     *
     * @param string  $bookmarkUrl URL of website to create thumbnail for
     * @param integer $width       Screenshot width
     * @param integer $height      Screenshot height
     *
     * @return mixed FALSE when no screenshot could be obtained,
     *               string with the URL otherwise
     */
    public function getThumbnailUrl($bookmarkUrl, $width, $height)
    {
        //default parameters for the phancap service
        $parameters = array(
            'url'     => $bookmarkUrl,
            'swidth'  => $width,
            'sheight' => $height,
            'sformat' => 'jpg',
        );

        if (isset($this->config['token']) && $this->config['token'] != '') {
            $parameters['atoken']     = $this->config['token'];
            $parameters['atimestamp'] = time();

            //create signature
            ksort($parameters);
            foreach ($parameters as $key => $value) {
                $encparams[] = $key . '=' . rawurlencode($value);
            }
            $encstring = implode('&', $encparams);
            $signature = hash_hmac('sha1', $encstring, $this->config['secret']);
            //append signature to parameters
            $parameters['asignature'] = $signature;
        }

        //url-encode the parameters
        $urlParams = array();
        foreach ($parameters as $key => $value) {
            $urlParams[] = $key . '=' . urlencode($value);
        }

        //final URL
        return $this->config['url'] . '?' . implode('&', $urlParams);
    }
}
?>