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
wpsso / lib / ext / tinyurl.php

tinyurl.php in WPSSO Core – Complete Schema Markup and Meta Tags 22.7.0, at lib/ext/tinyurl.php

272 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TinyUrl class
4 *
5 * This source file can be used to communicate with TinyURL.com (http://tinyurl.com)
6 *
7 * The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-tinyurl-bugs[at]verkoyen[dot]eu.
8 * If you report a bug, make sure you give me enough information (include your code).
9 *
10 * Changelog since 1.0.0
11 * - corrected some documentation
12 * - wrote some explanation for the method-parameters
13 *
14 * License
15 * Copyright (c) 2009, Tijs Verkoyen. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
18 *
19 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
22 *
23 * This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
24 *
25 * @author Tijs Verkoyen <php-tinyurl@verkoyen.eu>
26 * @version 1.0.1
27 *
28 * @copyright Copyright (c) 2008, Tijs Verkoyen. All rights reserved.
29 * @license BSD License
30 */
31
32 if ( ! defined( 'ABSPATH' ) ) {
33
34 die( 'These aren\'t the droids you\'re looking for.' );
35 }
36
37 if ( ! class_exists( 'SuextTinyUrl' ) ) {
38
39 class SuextTinyUrl {
40
41 // internal constant to enable/disable debugging
42 const DEBUG = false;
43
44 // url for the twitter-api
45 const API_URL = 'http://tinyurl.com';
46
47 // port for the tinyUrl-API
48 const API_PORT = 80;
49
50 // current version
51 const VERSION = '1.0.1';
52
53
54 /**
55 * The timeout
56 *
57 * @var int
58 */
59 private $timeOut = 60;
60
61
62 /**
63 * The user agent
64 *
65 * @var string
66 */
67 private $userAgent;
68
69
70 /**
71 * Default constructor
72 *
73 * @return void
74 */
75 public function __construct()
76 {
77 // nothing to do
78 }
79
80
81 /**
82 * Make the call
83 *
84 * @return string
85 * @param string $url
86 * @param array[optional] $aParameters
87 */
88 private function doCall( $url, $aParameters = array() ) {
89
90 // redefine
91 $url = (string) $url;
92 $aParameters = (array) $aParameters;
93
94 // rebuild url if we don't use post
95 if( ! empty( $aParameters ) ) {
96
97 // init var
98 $queryString = '';
99
100 // loop parameters and add them to the queryString
101 foreach( $aParameters as $key => $value) {
102
103 $queryString .= '&'. $key .'='. urlencode(utf8_encode($value));
104 }
105
106 // cleanup querystring
107 $queryString = trim($queryString, '&');
108
109 // append to url
110 $url .= (strpos($url, '?') === false ? '?' : '&').$queryString;
111 }
112
113 // set options
114 $options[ CURLOPT_URL ] = $url;
115 $options[ CURLOPT_PORT ] = self::API_PORT;
116 $options[ CURLOPT_USERAGENT ] = $this->getUserAgent();
117 $options[ CURLOPT_FOLLOWLOCATION ] = 1;
118 $options[ CURLOPT_RETURNTRANSFER ] = 1;
119 $options[ CURLOPT_TIMEOUT ] = (int) $this->getTimeOut();
120
121 $ch = curl_init();
122
123 curl_setopt_array( $ch, $options );
124
125 $response = curl_exec( $ch );
126 $curl_errnum = curl_errno( $ch );
127 $curl_errmsg = curl_error( $ch ); // See https://curl.se/libcurl/c/libcurl-errors.html.
128 $ssl_verify = (int) curl_getinfo( $ch, CURLINFO_SSL_VERIFYRESULT ); // See https://www.php.net/manual/en/function.curl-getinfo.php.
129 $http_code = (int) curl_getinfo( $ch, CURLINFO_HTTP_CODE );
130
131 curl_close( $ch );
132
133 $http_success_codes = array( 200, 201 );
134
135 try {
136
137 if ( ! empty( $curl_errnum ) ) {
138
139 $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'TinyURL', $curl_errnum . ' ' . $curl_errmsg );
140
141 throw new WpssoErrorException( $except_msg );
142
143 } elseif ( ! in_array( $http_code, $http_success_codes ) ) {
144
145 $except_msg = sprintf( __( '%1$s shortener API error: %2$s', 'wpsso' ), 'TinyURL', '' );
146
147 throw new WpssoErrorException( $except_msg, $http_code );
148
149 } else {
150
151 return $response;
152 }
153
154 } catch ( WpssoErrorException $e ) {
155
156 $e->errorMessage();
157 }
158
159 return false;
160 }
161
162
163 /**
164 * Get the timeout that will be used
165 *
166 * @return int
167 */
168 public function getTimeOut()
169 {
170 return (int) $this->timeOut;
171 }
172
173
174 /**
175 * Get the useragent that will be used. Our version will be prepended to yours.
176 * It will look like: "PHP Tinyurl/<version> <your-user-agent>"
177 *
178 * @return string
179 */
180 public function getUserAgent()
181 {
182 return (string) 'PHP TinyUrl/' . self::VERSION . ' ' . $this->userAgent;
183 }
184
185
186 /**
187 * Set the timeout
188 * After this time the request will stop. You should handle any errors triggered by this.
189 *
190 * @return void
191 * @param int $seconds The timeout in seconds
192 */
193 public function setTimeOut($seconds)
194 {
195 $this->timeOut = (int) $seconds;
196 }
197
198
199 /**
200 * Set the user-agent for you application
201 * It will be appended to ours, the result will look like: "PHP TinyUrl/<version> <your-user-agent>"
202 *
203 * @return void
204 * @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>
205 */
206 public function setUserAgent($userAgent)
207 {
208 $this->userAgent = (string) $userAgent;
209 }
210
211
212 /**
213 * Create a TinyUrl
214 *
215 * @return string
216 * @param string $url The orginal url that should be shortened
217 */
218 public function create($url) {
219
220 // redefine
221 $url = (string) $url;
222
223 // build parameters
224 $aParameters[ 'url' ] = $url;
225
226 // make the call
227 return $this->doCall( self::API_URL .'/api-create.php', $aParameters );
228 }
229
230
231 /**
232 * Reverse a TinyUrl into a real url
233 *
234 * @return mixed If something fails it will return false, otherwise the orginal url will be returned as a string
235 * @param string $url The short tinyUrl that should be reversed
236 */
237 public function reverse($url) {
238
239 // redefine
240 $url = (string) $url;
241
242 // explode on .com
243 $aChunks = explode( 'tinyurl.com/', $url);
244
245 if( isset( $aChunks[ 1 ] ) ) {
246
247 // rebuild url
248 $requestUrl = 'http://preview.tinyurl.com/' . $aChunks[1];
249
250 // make the call
251 if ( $response = $this->doCall( $requestUrl ) ) {
252
253 // init var
254 $aMatches = array();
255
256 // match
257 preg_match( '/redirecturl" href="(.*)">/', $response, $aMatches);
258
259 // return if something was found
260 if ( isset( $aMatches[ 1 ] ) ) {
261
262 return (string) $aMatches[ 1 ];
263 }
264 }
265 }
266
267 // fallback
268 return false;
269 }
270 }
271 }
272