My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
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
<?php
class url_unshortener{
/**Unshorten a url
*
* @param string
* @return string
*/
public static function GetRealUrl($sUrl){
if($sRealUrl = self::GetRealUrlFromDB($sUrl))
return $sRealUrl;

$sRealUrl = $sUrl;
//Let's catch the domain, and try to find a method to expand it...
$sDomain = self::catch_domain($sUrl);
if($sDomain){
$sDomain = str_replace('.','_',$sDomain);
$sDomain = str_replace('-','_',$sDomain);
$sMethod = 'expand_'.$sDomain;
if(method_exists('url_unshortener',$sMethod))
$sRealUrl = self::$sMethod($sUrl);
else
$sRealUrl = self::expand_default($sUrl);//Use the default expanding method

if($sRealUrl != $sUrl)
$sRealUrl = self::GetRealUrl($sRealUrl);//Let's retry (maybe someone shortened twice ...)
}

//Put it in the cache :
if($sUrl && $sRealUrl){
self::SaveRealUrl($sUrl,$sRealUrl);
}

return $sRealUrl;
}



/** Retrieves an already resolved url
*
* @param $sUrl
* @return string|boolean the real url or false
*/
protected static function GetRealUrlFromDB($sUrl){
if($rs = mysql_query('SELECT real_url FROM url_resolving WHERE url="'.$sUrl.'"')){
if(mysql_num_rows($rs)==1)
return mysql_result($rs,0);
}
return false;
}

/** Saves a resolved url for caching (mysql is waaaay faster than resolving)
*
* @param string $sUrl
* @param string $sRealUrl
*/
protected static function SaveRealUrl($sUrl,$sRealUrl){
mysql_query('INSERT INTO url_resolving VALUES ("'.$sUrl.'","'.$sRealUrl.'")');
}


public static function catch_domain($sUrl){
if(preg_match('/https?:\/\/([^\/\?]+)/i', $sUrl, $aMatches))
return $aMatches[1];
return null;
}

public static function catch_short_code($sUrl){
if(preg_match('/https?:\/\/[^\/\?]+[\/|\?](.*)/', $sUrl, $aMatches))
return $aMatches[1];
return null;
}

/**Default method to expand urls
*
* @param string $sUrl
* @return string the original url
*/
protected static function expand_default($sUrl){
$ch = curl_init($sUrl);
// set cURL to return the contents in a variable instead of giving it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'TwitterFavs_bot +http://lab.loicg.net/twitter_favs/');//Shhhhhh, it's our secret ;)

$contents = curl_exec($ch);

// Check if any error occured
if(!curl_errno($ch)){
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
$header = substr($contents, 0, $header_size);

//If the URL is redirected, we try to catch the real URL
if($http_code==301){
if(preg_match('/Location:[\h]+(https?:\/\/[^\n\h]+)/',$header,$aMatches)){
$sUrl = $aMatches[1];
return $sUrl;
}
}
}
curl_close($ch);
return $sUrl;
}

/** Bit.ly and j.mp expanding method
*
* @param string $sURL
* @return string the original URL
*/
protected static function expand_bit_ly($sUrl){
$sAPIurl = 'http://api.bit.ly/v3/expand?shortUrl='.urlencode($sUrl).'&login='.BITLY_API_LOGIN.'&apiKey='.BITLY_API_KEY;
$sAnswer = file_get_contents($sAPIurl);
$oAnswer = json_decode($sAnswer);

if($oAnswer->status_code!="200") return $sUrl;
return $oAnswer->data->expand[0]->long_url;
}
protected static function expand_j_mp($sUrl){
return self::expand_bit_ly($sUrl);
}

/**Minu.me and minurl.fr expanding method
*
* @param string $sURL
* @return string the original URL
*/
protected static function expand_minurl_fr($sUrl){
$sShortCode = self::catch_short_code($sUrl);
if(!$sShortCode) return $sUrl;
$sAPIurl = 'http://www.minu.me/api.php?lien='.$sShortCode;
$sRealUrl = file_get_contents($sAPIurl);
return $sRealUrl;
}
protected static function expand_minu_me($sUrl){
return self::expand_minurl_fr($sUrl);
}

/**Murl.fr expanding method
*
* @param string $sURL
* @return string the original URL
*/
protected static function expand_murl_fr($sUrl){
$sAPIurl = 'http://api.murl.fr/?action=gl&url='.urlencode($sUrl);
$sRealUrl = file_get_contents($sAPIurl);
return $sRealUrl;
}

/**mi-ni.me expanding method
*
* @param string $sURL
* @return string the original URL
*/
protected static function expand_mi_ni_me($sUrl){
$sAPIurl = 'http://minime.elseif.eu/api/unveil.php?url='.urlencode($sUrl);
$sRealUrl = file_get_contents($sAPIurl);
return $sRealUrl;
}

/**Ping.fm expanding method
*
* @param string $sURL
* @return string the original URL
*/
protected static function expand_ping_fm($sUrl){
$sAPIurl = $sUrl.'?r';
$sRealUrl = file_get_contents($sAPIurl);
return $sRealUrl;
}


}

Change log

r27 by gerbaudloic on Dec 5, 2010   Diff
Fix : bug in the shortcode extractor
Go to: 
Project members, sign in to write a code review

Older revisions

r26 by gerbaudloic on Dec 4, 2010   Diff
Added an expand method for mi-ni.me
url shortener
r25 by gerbaudloic on Dec 4, 2010   Diff
Improve URL_Unshortener (now uses some
shorteners' API)
r18 by gerbaudloic on Nov 28, 2010   Diff
Change : now coded using objects
(cleaner code)
Add : templating system
Add : hashtag capture system
All revisions of this file

File info

Size: 4743 bytes, 173 lines
Powered by Google Project Hosting