aboutsummaryrefslogtreecommitdiffstats
path: root/addon/twitter/slinky.php
blob: 51432c3f5372392d6404fd05ec379dc2db8f85f6 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
/*
Copyright (c) 2009, Beau Lebens
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

 - Redistributions of source code must retain the above copyright notice, this 
   list of conditions and the following disclaimer.
 - 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.
 - Neither the name of Dented Reality nor the names of the authors may be used 
   to endorse or promote products derived from this software without specific 
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.
	
*/

// Return options for Slinky_Service->url_get() and ->url_post()
define( 'SLINKY_BODY', 1 ); // Default
define( 'SLINKY_HEADERS', 2 ); // Not implemented yet
define( 'SLINKY_FINAL_URL', 3 ); // Default for lengthening URLs

// So that services may decide what to do with us
define( 'SLINKY_USER_AGENT', 'Slinky v1.0 +http://dentedreality.com.au/projects/slinky/' );

// How many seconds until remote requests should be cut?
define( 'SLINKY_TIMEOUT', 10 );

/**
 * Slinky allows you to go back and forth between "long" and shortened URLs 
 * using popular URL shortening services.
 * 
 * Slinky assumes you have cURL installed and working, and requires the JSON
 * extension installed if you're working with a service that uses JSON.
 * 
 * Slinky will ONLY work with PHP5+
 * 
 * It supports some of the more popular services, with easy extensibility for
 * adding your own relatively easily. It defaults to using TinyURL
 * for shortening URLs. If you want to use some of the other services, you need
 * to set some configuration options before actually shortening/lengthening
 * URLs. I'd strongly suggest that you cache results using memcached, a local
 * DB or whatever to avoid having to hit APIs etc every time you encounter a
 * URL.
 * 
 * Slinky supports shortening, and auto-detection (for lengthening URLs) 
 * using these services:
 * - Bit.ly
 * - Tr.im
 * - TinyURL
 * - Is.Gd
 * - Fon.gs
 * - Micurl.com
 * - ur1.ca
 * - Ptiturl
 * - Tighturl
 * - 2tu.us
 * - Snipr / Snipurl / Snurl.com / Sn.im
 * 
 * 
 * To use Slinky:
 * 
 * $slinky = new Slinky( 'http://dentedreality.com.au/' );
 * - Creates a new Slinky instance, will default to using TinyURL for ->short();
 * 
 * $slinky = new Slinky( 'http://dentedreality.com.au', new Slinky_Bitly() );
 * - Creates new Slinky, forces use of Bit.ly for ->short();
 * 
 * $slinky = new Slinky( 'http://dentedreality.com.au/' );
 * echo $slinky->short();
 * - echos the short version of http://dentedreality.com.au/ (default to TinyURL)
 * 
 * $slinky = new Slinky( 'http://tinyurl.com/jw5sh' );
 * echo $slinky->long();
 * - echos out the long version of http://tinyurl.com/jw5sh (which should be http://dentedreality.com.au/)
 * 
 * $slinky = new Slinky( 'http://dentedreality.com.au/' );
 * echo $slinky->long();
 * - Attempts to lengthen the URL, but will not auto-detect which service it is
 *   so it will just output the original URL. Useful for always attempting to
 *   lengthen any URL you come across (fails gracefully)
 * 
 * $slinky = new Slinky( 'http://dentedreality.com.au/' );
 * $slinky->set_cascade( array( new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
 * echo $slinky->short();
 * - Uses the powerful cascade mode to make sure that we get a short URL from 
 *   Tr.im, Is.Gd or TinyURL (in that order).
 * 
 * See specific service class definitions below for examples of how to use them,
 * as some services allow (or require) additional properties before you can use
 * them (for authentication etc).
 * 
 * To use a different service with Slinky, just create your own class and
 * extend Slinky_Service(). Make sure you implement url_is_short(), url_is_long(),
 * make_short() and make_long(). If you need to GET or POST a URL, you can use
 * ->url_get() and ->url_post(), which your class will have inherited.
**/
class Slinky {
	var $url     = false;
	var $service = false;
	var $cascade = false;
	
	function __construct( $url = false, $service = false ) {
		$this->url     = $url;
		$this->service = $service;
	}
	
	/**
	 * Specify which URL Service to use
	 *
	 * @param Slinky_Service $service Packaged or custom Service object
	 * @return void
	 */
	public function set_service( $service = false ) {
		if ( is_object( $service ) ) {
			$this->service = $service;
		}
	}
	
	/**
	 * If you pass an array of Slinky_Service objects to this method, they will
	 * be used in order to try to get a short URL, so if one fails, it will
	 * try the next and so on, until it gets a valid short URL, or it runs
	 * out of options.
	 * 
	 * @param array $services List of Slinky_Service objects as an array
	**/
	public function set_cascade( $services = false ) {
		if ( !$services || !is_array( $services ) )
			return false;
		
		$this->cascade = $services;
	}
	
	/**
	 * Guess the URL service to use from known domains of short URLs
	 *
	 * @param string $url 
	 */
	public function set_service_from_url( $url = false ) {
		if ( !$url )
			$url = $this->url;
		
		$host = parse_url( $url, PHP_URL_HOST );
		switch ( str_replace( 'www.', '', $host ) ) {
			case 'bit.ly':
				if ( class_exists( 'Slinky_Bitly' ) ) {
					$this->service = new Slinky_Bitly();
					break;
				}
			case 'tr.im':
				if ( class_exists( 'Slinky_Trim' ) ) {
					$this->service = new Slinky_Trim();
					break;
				}
			case 'tinyurl.com':
				if ( class_exists( 'Slinky_TinyURL' ) ) {
					$this->service = new Slinky_TinyURL();
					break;
				}
			case 'is.gd':
				if ( class_exists( 'Slinky_IsGd' ) ) {
					$this->service = new Slinky_IsGd();
					break;
				}
			case 'fon.gs':
				if ( class_exists( 'Slinky_Fongs' ) ) {
					$this->service = new Slinky_Fongs();
					break;
				}
			case 'micurl.com':
				if ( class_exists( 'Slinky_Micurl' ) ) {
					$this->service = new Slinky_Micurl();
					break;
				}
			case 'ur1.ca':
				if ( class_exists( 'Slinky_Ur1ca' ) ) {
					$this->service = new Slinky_Ur1ca();
					break;
				}
			case 'ptiturl.com':
				if ( class_exists( 'Slinky_PtitURL' ) ) {
					$this->service = new Slinky_PtitURL();
					break;
				}
			case 'tighturl.com':
			case '2tu.us':
				if ( class_exists( 'Slinky_TightURL' ) ) {
					$this->service = new Slinky_TightURL();
					break;
				}
			case 'snipr.com':
			case 'snipurl.com':
			case 'snurl.com':
			case 'sn.im':
				if ( class_exists( 'Slinky_Snipr' ) ) {
					$this->service = new Slinky_Snipr();
					break;
				}
			default:
				$this->service = new Slinky_Default();
				break;
		}
	}
	
	/**
	 * Take a long URL and make it short. Will avoid "re-shortening" a URL if it
	 * already seems to be short.
	 *
	 * @param string $url Optional URL to shorten, otherwise use $this->url
	 * @return The short version of the URL
	 */
	public function short( $url = false ) {
		if ( $url )
			$this->url = $url;
			
		if ( !$this->service )
			$this->set_service( new Slinky_TinyURL() ); // Defaults to tinyurl because it doesn't require any configuration
		
		if ( !$this->cascade )
			$this->cascade = array( $this->service ); // Use a single service in cascade mode
		
		foreach ( $this->cascade as $service ) {
			if ( $service->url_is_short( $this->url ) )
				return trim( $this->url ); // Identified as already short, using this service

			$response = trim( $service->make_short( $this->url ) );
			if ( $response && $this->url != $response )
				return trim( $response );
		}
		
		return $this->url; // If all else fails, just send back the URL we already know about
	}
	
	/**
	 * Take a short URL and make it long ("resolve" it).
	 *
	 * @param string $url The short URL
	 * @return A long URL
	 */
	public function long( $url = false ) {
		if ( $url )
			$this->url = $url;
			
		if ( !$this->service )
			$this->set_service_from_url();
		
		if ( $this->service->url_is_long( $this->url ) )
			return trim( $this->url );
		
		return trim( $this->service->make_long( $this->url ) );
	}
}

/**
 * Use this class to create a Service implementation for your own URL 
 * shortening service. Extend the class and customize methods to suit your 
 * service. Note that it is an "abstract" class, so there are certain methods 
 * which you *must* define.
**/
abstract class Slinky_Service {
	
	/**
	 * Determine, based on the input URL, if it's already a short URL, from
	 * this particular service. e.g. a Bit.ly URL contains "bit.ly/"
	**/
	abstract function url_is_short( $url );
	
	/**
	 * Determine if this is a "long" URL (just means it hasn't been shortened)
	 * already. e.g. a no-Bit.ly URL would NOT contain "bit.ly/"
	**/
	abstract function url_is_long( $url );
	
	/**
	 * Handle taking the $url and converting it to a short URL via whatever
	 * means is provided at the remote service.
	**/
	abstract function make_short( $url );
	
	/**
	 * Return the long/expanded version of a URL via any API means available
	 * from this service. As a fallback, you might
	 * consider just following the URL and using SLINKY_FINAL_URL as the 
	 * return method from a $this->url_get() call to find out.
	 * 
	 * This one is optional for Services extending this class, if they don't
	 * then the following implementation will work on most services anyway.
	**/
	public function make_long( $url ) {
		return $this->url_get( $url, SLINKY_FINAL_URL );
	}
	
	/**
	 * Method for getting properties that you might need during the process
	 * of shortening/lengthening a URL (e.g. auth credentials)
	**/
	public function get( $prop ) {
		if ( empty( $this->$prop ) )
			return null;
			
		return $this->$prop;
	}
	
	/**
	 * Method for setting properties that you might need during the process
	 * of shortening/lengthening a URL (e.g. auth credentials)
	**/
	public function set( $prop, $val ) {
		$this->$prop = $val;
	}
	
	/**
	 * Internal helper for performing a GET request on a remote URL.
	 * 
	 * @param string $url The URL to GET
	 * @param const $return The return method [ SLINKY_BODY | SLINKY_FINAL_URL | SLINKY_HEADERS ]
	 * @return Mixed, based on the $return var passed in.
	**/
	protected function url_get( $url, $return = SLINKY_BODY ) {
		$ch = curl_init( $url );
		curl_setopt( $ch, CURLOPT_USERAGENT, SLINKY_USER_AGENT );
		curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );			// Don't stress about SSL validity
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );			// Return the response, don't output it
		curl_setopt( $ch, CURLOPT_TIMEOUT, SLINKY_TIMEOUT );	// Limit how long we'll wait for a response
		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );			// Allow following of redirections
		$r  = curl_exec( $ch );
		if ( curl_errno( $ch ) ) {
			return false;
		}
		
		// Return whatever we were asked for
		if ( SLINKY_FINAL_URL == $return )
			return curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL );
		else if ( SLINKY_BODY == $return )
			return $r;
		
		return false;
	}
	
	/**
	 * Internal helper for performing a POST request on a remote URL.
	 * 
	 * @param string $url The URL to POST to
	 * @param array $payload Array containing name/value pairs of the parameters to POST
	 * @param const $return The return method [ SLINKY_BODY | SLINKY_FINAL_URL | SLINKY_HEADERS ]
	 * @return Mixed, based on the $return var passed in.
	**/
	protected function url_post( $url, $payload = array(), $return = SLINKY_BODY ) {
		$ch = curl_init( $url );
		curl_setopt( $ch, CURLOPT_POST, true );
		curl_setopt( $ch, CURLOPT_POSTFIELDS, (array) $payload );
		curl_setopt( $ch, CURLOPT_USERAGENT, SLINKY_USER_AGENT );
		curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );			// Don't stress about SSL validity
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );			// Return the response, don't output it
		curl_setopt( $ch, CURLOPT_TIMEOUT, SLINKY_TIMEOUT );	// Limit how long we'll wait for a response
		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );			// Allow following of redirections
		$r  = curl_exec( $ch );
		if ( curl_errno( $ch ) ) {
			return false;
		}
		
		// Return whatever we were asked for
		if ( SLINKY_FINAL_URL == $return )
			return curl_getinfo( $ch, CURLINFO_EFFECTIVE_URL );
		else if ( SLINKY_BODY == $return )
			return $r;
		
		return false;
	}
}

// This default service is used in cases when you try to do something based
// on auto-detection, but we can't detect anything. It will also resolve URLs
// to their "long" version by following all redirects.
class Slinky_Default extends Slinky_Service {
	function url_is_short( $url ) {
		return false;
	}
	
	function url_is_long( $url ) {
		return false;
	}
	
	function make_short( $url ) {
		return $url;
	}
}

// Implementation of TinyURL as a Slinky Service
class Slinky_TinyURL extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'tinyurl.com/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'tinyurl.com/' );
	}
	
	function make_short( $url ) {
		return $this->url_get( 'http://tinyurl.com/api-create.php?url=' . urlencode( $url ) );
	}
	
	function make_long( $url ) {
		$bits = parse_url( $url );
		$result = $this->url_get( 'http://tinyurl.com/preview.php?num=' . substr( $bits['path'], 1 ) );
		if ( preg_match('/<a id="redirecturl" href="([^"]+)">/is', $result, $matches ) )
			return $matches[1];
		else
			return $url;
	}
}

// Implementation of Bit.ly as a Slinky Service
/*
To use Bit.ly, you MUST set your login and apiKey for the service first, e.g.

$bitly = new Slinky_Bitly();
$bitly->set( 'login', 'bitly_login' );
$bitly->set( 'apiKey', 'bitly_apiKey' );

$slinky = new Slinky( $url, $bitly );
echo $slinky->short();

You could also do this if the URL was already a bit.ly URL and you 
were going to make it longer, since Bitly is supported for auto-detection:

$slinky = new Slinky( $url );
$slinky->set_service_from_url();
$slinky->service->set( 'login', 'bitly_login' );
$slinky->service->set( 'apiKey', 'bitly_apiKey' );
echo $slinky->long();

*/
class Slinky_Bitly extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'bit.ly/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'bit.ly/' );
	}
	
	function make_short( $url ) {
		// Can't do anything unless these 2 properties are set first
		if ( !$this->get( 'login' ) || !$this->get( 'apiKey' ) )
			return $url;
		
		$result = $this->url_post( 'http://api.bit.ly/shorten?version=2.0.1&format=json&login=' . $this->get( 'login' ) . '&apiKey=' . $this->get( 'apiKey' ) . '&longUrl=' . urlencode( $url ) );
		$result = json_decode( $result );
		if ( !$result->errorCode ) {
			foreach ( $result->results as $detail ) {
				return $detail->shortUrl;
			}
		} else {
			return false;
		}
	}
	
	function make_long( $url ) {
		// Can't do anything unless these 2 properties are set first
		if ( !$this->get( 'login' ) || !$this->get( 'apiKey' ) )
			return $url;
		
		$result = $this->url_post( 'http://api.bit.ly/expand?version=2.0.1&format=json&login=' . $this->get( 'login' ) . '&apiKey=' . $this->get( 'apiKey' ) . '&shortUrl=' . urlencode( $url ) );
		$result = json_decode( $result );
		if ( !$result->errorCode ) {
			foreach ( $result->results as $detail ) {
				return $detail->longUrl;
			}
		} else {
			return false;
		}
	}
}

// Implementation of Tr.im as a Slinky Service
/*
When using Tr.im, you MAY optionally set your username and password to tie 
URLs to your account, e.g.

$trim = new Slinky_Trim();
$trim->set( 'username', 'trim_username' );
$trim->set( 'password', 'trim_password' );

$slinky = new Slinky( $url, $trim );
echo $slinky->short();

You could also do this if the URL was already a tr.im URL and you 
were going to make it longer, since Tr.im is supported for auto-detection:

$slinky = new Slinky( $url );
$slinky->set_service_from_url();
echo $slinky->long();

*/
class Slinky_Trim extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'tr.im/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'tr.im/' );
	}
	
	function make_short( $url ) {
		$url = 'http://api.tr.im/api/trim_simple?url=' . urlencode( $url );

		if ( $this->get( 'username' ) && $this->get( 'password' ) )
			$url .= '&username=' . urlencode( $this->get( 'username' ) ) . '&password=' . urlencode( $this->get( 'password' ) );
		
		return $this->url_get( $url );
	}
	
	function make_long( $url ) {
		$bits = parse_url( $url );
		$result = $this->url_get( 'http://api.tr.im/api/trim_destination.json?trimpath=' . substr( $bits['path'], 1 ) );
		$result = json_decode($result);
		if ( 'OK' == $result->status->result )
			return $result->destination;
		else
			return $url;
	}
}

// Implementation of Is.Gd as a Slinky Service
class Slinky_IsGd extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'is.gd/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'is.gd/' );
	}
	
	function make_short( $url ) {
		$response = $this->url_get( 'http://is.gd/api.php?longurl=' . urlencode( $url ) );
		if ( 'error' == substr( strtolower( $response ), 0, 5 ) )
			return false;
		else
			return $response;
	}
}

// Fon.gs
class Slinky_Fongs extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'fon.gs/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'fon.gs/' );
	}
	
	function make_short( $url ) {
		$response = $this->url_get( 'http://fon.gs/create.php?url=' . urlencode( $url ) );
		if ( 'OK:' == substr( $response, 0, 3 ) )
			return str_replace( 'OK: ', '', $response );
		else
			return $url;
	}
}

// Micu.rl
class Slinky_Micurl extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'micurl.com/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'micurl.com/' );
	}
	
	function make_short( $url ) {
		$result = $this->url_get( 'http://micurl.com/api.php?url=' . urlencode( $url ) );
		if ( 1 != $result && 2 != $result )
			return 'http://micurl.com/' . $result;
		else
			return $url;
	}
}

// ur1.ca
class Slinky_Ur1ca extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'ur1.ca/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'ur1.ca/' );
	}
	
	function make_short( $url ) {
		$result = $this->url_post( 'http://ur1.ca/', array( 'longurl' => $url ) );
		if ( preg_match( '/<p class="success">Your ur1 is: <a href="([^"]+)">/', $result, $matches ) )
			return $matches[1];
		else
			return $url;
	}
}

// PtitURL.com
class Slinky_PtitURL extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'ptiturl.com/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'ptiturl.com/' );
	}
	
	function make_short( $url ) {
		$result = $this->url_get( 'http://ptiturl.com/index.php?creer=oui&url=' . urlencode( $url ) );
		if ( preg_match( '/<pre><a href=\'?([^\'>]+)\'?>/', $result, $matches ) )
			return $matches[1];
		else
			return $url;
	}
}

// Tighturl.com
class Slinky_TightURL extends Slinky_Service {
	function url_is_short( $url ) {
		return stristr( $url, 'tighturl.com/' )
				|| stristr( $url, '2tu.us/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'tighturl.com/' )
				&& !stristr( $url, '2tu.us/' );
	}
	
	function make_short( $url ) {
		$response = $this->url_get( 'http://tighturl.com/?save=y&url=' . urlencode( $url ) );
		if ( preg_match( '/Your tight URL is: <code><a href=\'([^\']+)\' target=\'_blank\'>/', $response, $matches ) ) {
			return $matches[1];
		} else {
			return $url;
		}
	}
}

// Snipr for Slinky
/*
To use Snipr, you MUST set your user_id and API (key) for the service first, e.g.

$snipr = new Slinky_Snipr();
$snipr->set( 'user_id', 'Snipr User ID' );
$snipr->set( 'API', 'Snipr API Key' );

$slinky = new Slinky( $url, $snipr );
echo $slinky->short();

NOTE: Snipr requires the SimpleXML extension to be installed for lengthening URLs
*/
class Slinky_Snipr extends Slinky_Service {
	// Snipurl, Snurl, Snipr, Sn.im
	function url_is_short( $url ) {
		return stristr( $url, 'snipr.com/' ) || stristr( $url, 'snipurl.com/' ) || stristr( $url, 'snurl.com/' ) || stristr( $url, 'sn.im/' );
	}
	
	function url_is_long( $url ) {
		return !stristr( $url, 'snipr.com/' ) || !stristr( $url, 'snipurl.com/' ) || !stristr( $url, 'snurl.com/' ) || !stristr( $url, 'sn.im/' );
	}
	
	function make_short( $url ) {
		if ( !$this->get( 'user_id' ) || !$this->get( 'API' ) )
			return $url;
		
		$response = $this->url_post( 'http://snipr.com/site/getsnip', array( 'sniplink' => urlencode( $url ), 'snipuser' => $this->get( 'user_id'), 'snipapi' => $this->get( 'API' ), 'snipformat' => 'simple' ) );
		if ( 'ERROR' != substr( $response, 0, 5 ) )
			return $response;
		else
			return $url;
	}
}

// If you're testing things out, http://dentedreality.com.au/ should convert to:
// - http://tinyurl.com/jw5sh
// - http://bit.ly/hEkAD
// - http://tr.im/sk1H
// - http://is.gd/1yJ81
// - http://fon.gs/tc1p8c
// - http://micurl.com/qen3uub
// - http://ur1.ca/7dcd
// - http://ptiturl.com/?id=bac8fb
// - http://tighturl.com/kgd
// - http://snipr.com/nbbw3
// 
// $slinky = new Slinky( 'http://dentedreality.com.au/' );
// echo $slinky->short();