PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
← All changes | lib/ext/yourls.php +236 -0 22.1.322.7.0 View file →
@@ -1,0 +1,236 @@
1 +<?php
2 +/**
3 + * API client for yourls.org
4 + *
5 + * @author Šarūnas Dubinskas <s.dubinskas@evp.lt>
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 +
10 + die( 'These aren\'t the droids you\'re looking for.' );
11 +}
12 +
13 +if ( ! class_exists( 'SuextYourls' ) ) {
14 +
15 + class SuextYourls {
16 +
17 + /**
18 + * Available actions
19 + */
20 + const ACTION_SHORTURL = 'shorturl';
21 + const ACTION_URL_STATS = 'url-stats';
22 + const ACTION_EXPAND = 'expand';
23 +
24 + /**
25 + * @var string
26 + */
27 + protected $apiUrl;
28 +
29 + /**
30 + * @var null|string
31 + */
32 + protected $username;
33 +
34 + /**
35 + * @var null|string
36 + */
37 + protected $password;
38 +
39 + /**
40 + * @var null|string
41 + */
42 + protected $token;
43 +
44 + /**
45 + * @var string
46 + */
47 + private $lastResponse;
48 +
49 + /**
50 + * Class constructor
51 + *
52 + * @param string $apiUrl
53 + * @param null|string $username
54 + * @param null|string $password
55 + * @param null|string $token
56 + */
57 + public function __construct( $apiUrl, $username = null, $password = null, $token = null ) {
58 +
59 + $this->apiUrl = $apiUrl;
60 + $this->username = $username;
61 + $this->password = $password;
62 + $this->token = $token;
63 + }
64 +
65 + /**
66 + * Get short URL for a URL
67 + *
68 + * @param string $url
69 + * @param null|string $keyword
70 + *
71 + * @return string
72 + */
73 + public function shorten( $url, $keyword = null ) {
74 +
75 + $resp_data = $this->call( self::ACTION_SHORTURL, array( 'url' => $url, 'keyword' => $keyword ) );
76 +
77 + return isset( $resp_data[ 'shorturl' ] ) ? $resp_data[ 'shorturl' ] : false;
78 + }
79 +
80 + /**
81 + * Get stats about short URL
82 + *
83 + * @param string $shortUrl
84 + *
85 + * @return array
86 + */
87 + public function getUrlStats( $shortUrl ) {
88 +
89 + return $this->call( self::ACTION_URL_STATS, array( 'shorturl' => $shortUrl ) );
90 + }
91 +
92 + /**
93 + * Get long URL of a short URL
94 + *
95 + * @param string $shortUrl
96 + *
97 + * @return string
98 + */
99 + public function expand( $shortUrl ) {
100 +
101 + $resp_data = $this->call( self::ACTION_EXPAND, array( 'shorturl' => $shortUrl ) );
102 +
103 + return isset( $resp_data[ 'longurl' ] ) ? $resp_data[ 'longurl' ] : false;
104 + }
105 +
106 + /**
107 + * Returns last raw response from API
108 + *
109 + * @return string
110 + */
111 + public function getLastResponse() {
112 +
113 + return $this->lastResponse;
114 + }
115 +
116 + /**
117 + * Calls API action with specified params
118 + *
119 + * @param string $action
120 + * @param array $params
121 + *
122 + * @return array
123 + */
124 + protected function call( $action, $params = array() ) {
125 +
126 + $params['action'] = $action;
127 +
128 + if ( $this->username ) {
129 +
130 + $params[ 'username' ] = $this->username;
131 + $params[ 'password' ] = $this->password;
132 +
133 + } else {
134 +
135 + $params[ 'timestamp' ] = time();
136 + $params[ 'signature' ] = md5( $this->token.$params['timestamp'] );
137 + }
138 +
139 + $params[ 'format' ] = 'json';
140 +
141 + $url = $this->apiUrl . ( strpos( $this->apiUrl, '?' ) === false ? '?' : '&' ) . http_build_query( $params );
142 +
143 + $ch = curl_init();
144 +
145 + curl_setopt( $ch, CURLOPT_URL, $url );
146 + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
147 + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
148 + curl_setopt( $ch, CURLOPT_TIMEOUT, 15 );
149 +
150 + /**
151 + * When negotiating a TLS or SSL connection, the server sends a
152 + * certificate indicating its identity. Curl verifies whether the
153 + * certificate is authentic, i.e. that you can trust that the server is
154 + * who the certificate says it is. This trust is based on a chain of
155 + * digital signatures, rooted in certification authority (CA)
156 + * certificates you supply. curl uses a default bundle of CA
157 + * certificates (the path for that is determined at build time) and you
158 + * can specify alternate certificates with the CURLOPT_CAINFO option or
159 + * the CURLOPT_CAPATH option.
160 + *
161 + * See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html
162 + */
163 + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 1 );
164 +
165 + /**
166 + * When CURLOPT_SSL_VERIFYHOST is 2, that certificate must indicate
167 + * that the server is the server to which you meant to connect, or the
168 + * connection fails. Simply put, it means it has to have the same name
169 + * in the certificate as is in the URL you operate against.
170 + *
171 + * See https://curl.haxx.se/libcurl/c/CURLOPT_SSL_VERIFYHOST.html
172 + */
173 + curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
174 +
175 + /**
176 + * Define and disable the "Expect: 100-continue" header.
177 + */
178 + curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Expect:' ) );
179 +
180 + if ( ! ini_get('safe_mode') && ! ini_get('open_basedir') ) {
181 +
182 + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
183 + curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
184 + }
185 +
186 + $response = $this->lastResponse = curl_exec( $ch );
187 + $curl_errnum = curl_errno( $ch );
188 + $curl_errmsg = curl_error( $ch ); // See https://curl.se/libcurl/c/libcurl-errors.html.
189 + $ssl_verify = (int) curl_getinfo( $ch, CURLINFO_SSL_VERIFYRESULT ); // See https://www.php.net/manual/en/function.curl-getinfo.php.
190 + $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
191 +
192 + curl_close( $ch );
193 +
194 + $http_success_codes = array( 200, 201 );
195 +
196 + try {
197 +
198 + if ( ! empty( $curl_errnum ) ) {
199 +
200 + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'YOURLS', $curl_errnum . ' ' . $curl_errmsg );
201 +
202 + throw new WpssoErrorException( $except_msg );
203 +
204 + } elseif ( ! in_array( $http_code, $http_success_codes ) ) {
205 +
206 + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'YOURLS', '' );
207 +
208 + throw new WpssoErrorException( $except_msg, $http_code );
209 +
210 + } else {
211 +
212 + $resp_data = json_decode( $response, $assoc = true );
213 +
214 + if ( null === $resp_data ) {
215 +
216 + $json_msg = sprintf( __( 'JSON response decode error code %d.', 'wpsso' ), json_last_error() );
217 +
218 + $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'YOURLS', $json_msg );
219 +
220 + throw new WpssoErrorException( $except_msg );
221 +
222 + } else {
223 +
224 + return $resp_data;
225 + }
226 + }
227 +
228 + } catch ( WpssoErrorException $e ) {
229 +
230 + return $e->errorMessage();
231 + }
232 +
233 + return false;
234 + }
235 + }
236 +}