MylinksPagepeeker::getProviderPublicKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 * This program is distributed in the hope that it will be useful,
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
 *
10
 * to use the provider:
11
 * $shot = new MylinksThumbshots();
12
 * $shot->setProviderPrivateKey(my_key);
13
 * $shot->setShotSize(array('width'=>120));
14
 * $shot->setSiteUrl("http://site_to_capture");
15
 * $mylinks_shotprovider = $shot->getProviderUrl();
16
 *
17
 * Then in the template use something like:
18
 *  <img src='<{$mylinks_shotprovider}>' target='_blank' alt='' style='margin: 3px 7px;'>
19
 *  and at the bottom of the page show the attribution
20
 *  echo $shot->getAttribution();
21
 */
22
23
/**
24
 * MyLinks category.php
25
 *
26
 * Xoops mylinks - a multicategory links module
27
 *
28
 * @copyright ::  {@link http://xoops.org/ XOOPS Project}
29
 * @copyright ::  {@link http://www.zyspec.com ZySpec Incorporated}
30
 * @license   ::    {@link http://www.gnu.org/licenses/gpl-2.0.html GNU Public License}
31
 * @package   ::    mylinks
32
 * @subpackage:: class
33
 * @author    ::     zyspec <[email protected]>
34
 */
35
require_once XOOPS_ROOT_PATH . '/modules/mylinks/class/thumbplugin.interface.php';
36
37
/**
38
 * Class MylinksPagepeeker
39
 */
40
class MylinksPagepeeker implements MylinksThumbPlugin
41
{
42
    private   $image_width   = 0;
43
    private   $image_height  = 0;
44
    private   $image_size    = 'm';
45
    private   $site_url      = null;
46
    private   $key           = null;
47
    private   $attribution   = "<a href=\"http://www.pagepeeker.com\" target=\"_blank\" title=\"Thumbnail Screenshots by PagePeeker\">Thumbnail Screenshots by PagePeeker</a>";
48
    private   $provider_url  = 'http://free.pagepeeker.com/v2/thumbs.php';
49
    private   $provider_name = 'Pagepeeker';
50
    protected $_dirname      = null;
51
52
    /**
53
     * MylinksPagepeeker constructor.
54
     */
55
    public function __construct()
56
    {
57
        global $xoopsModule;
58
        $this->_dirname = basename(dirname(dirname(__DIR__)));
59
    }
60
61
    /**
62
     * @return mixed|string
63
     */
64
    public function getProviderUrl()
65
    {
66
        $query_string = array(
67
            'size' => $this->image_size,
68
            'url'  => $this->site_url
69
        );
70
        if (!empty($key)) {
0 ignored issues
show
Bug introduced by
The variable $key seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
71
            $query_string['code'] = $this->key;
72
            $query_string['wait'] = 5;  // generate screenshot if it doesn't exist (waits xx sec)
73
            ksort($query_string);
74
        }
75
        $query = http_build_query($query_string);
76
        $query = empty($query) ? '' : '?' . $query;
77
78
        // now fix provider URL
79
        $_mHandler = xoops_getHandler('module');
80
        $_mlModule = $_mHandler->getByDirname($this->_dirname);
81
        $myKey     = $_mlModule->getInfo('shotpubkey');
82
        /* change the provider URL if the key is set */
83
        if (!empty($myKey)) {
84
            $providerUrl = str_ireplace('http://free', 'http://api', $this->provider_url);
85
        } else {
86
            $providerUrl = $this->provider_url;
87
        }
88
89
        $providerUrl = $providerUrl . $query;
90
91
        return $providerUrl;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getProviderName()
98
    {
99
        return $this->provider_name;
100
    }
101
102
    /**
103
     * @param $sz
104
     * @return mixed|void
105
     */
106
    public function setShotSize($sz)
107
    {
108
        $validX  = array(90, 120, 200, 400, 480);
109
        $validY  = array(68, 90, 150, 300, 360);
110
        $sizeMap = array(0 => 't', 1 => 's', 2 => 'm', 3 => 'l', 4 => 'x');
111
112
        if (is_array($sz)) { /* size is an array (width, height) */
113
            $x = (int)$sz['width'];
114
            if (in_array($x, $validX)) {
115
                $Xdilav           = array_flip($validX);
116
                $this->image_size = $sizeMap[$Xdilav[$x]];
117 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
                $max_i = count($validX);
119
                for ($i = 0; $i < $max_i; $i++) {
120
                    if ($validX[$i] > $x) {
121
                        break;
122
                    }
123
                }
124
                $this->image_size = $sizeMap[$i];
125
            }
126 View Code Duplication
        } elseif (is_numeric($sz)) { /* size is a number */
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
            $max_i = count($validX);
128
            for ($i = 0; $i < $max_i; $i++) {
129
                if ($validX[$i] > $sz) {
130
                    break;
131
                }
132
            }
133
            $this->image_size = $sizeMap[$i];
134
        } else { /* size is relative - t|s|m|l|x */
135
            $sz = mb_strtolower($sz);
136
            if (array_key_exists($sz, $sizeMap)) {
137
                $this->image_size = $sizeMap[$sz];
138
            } else {
139
                $this->image_size = 'm';
140
            }
141
        }
142
        $paMezis            = array_flip($sizeMap);
143
        $aKey               = $paMezis[$this->image_size];
144
        $this->image_width  = $validX[$aKey];
145
        $this->image_height = $validY[$aKey];
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function getShotSize()
152
    {
153
        return array('width' => $this->image_width, 'height' => $this->image_height);
154
    }
155
156
    /**
157
     * @param $url
158
     * @return mixed|void
159
     */
160
    public function setSiteUrl($url)
161
    {
162
        //@todo: sanitize url;
163
        $this->site_url = formatURL($url);
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getSiteUrl()
170
    {
171
        return urlencode($this->site_url);
172
    }
173
174
    /**
175
     * @param null $attr
176
     */
177
    public function setAttribution($attr = null)
178
    {
179
        $this->attribution = $attr;
180
    }
181
182
    /**
183
     * @param int $allowhtml
184
     * @return string
185
     */
186
    public function getAttribution($allowhtml = 0)
187
    {
188
        if ($allowhtml) {
189
            return $this->attribution;
190
        } else {
191
            $myts = MyTextSanitizer::getInstance();
192
193
            return $myts->htmlSpecialChars($this->attribution);
194
        }
195
    }
196
197
    /**
198
     * @param $key
199
     * @return mixed|void
200
     */
201
    public function setProviderPublicKey($key)
202
    {
203
        $this->key = $key;
204
    }
205
206
    /**
207
     * @return null
208
     */
209
    public function getProviderPublicKey()
210
    {
211
        return $this->key;
212
    }
213
214
    /**
215
     * @param $key
216
     * @return bool
217
     */
218
    public function setProviderPrivateKey($key)
219
    {
220
        return false;
221
    }
222
223
    /**
224
     * @return bool
225
     */
226
    public function getProviderPrivateKey()
227
    {
228
        return false;
229
    }
230
}
231