Skip to content

Commit 4f806f5

Browse files
committed
Added automatic redirections for google url shortener and google translate
1 parent c4c9383 commit 4f806f5

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

Embed/Embed.php

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public static function create($url, array $options = null)
1919
throw new \InvalidArgumentException("Embed::create only accepts instances of Embed\\Request or strings");
2020
}
2121

22+
//Resolve special redirections
23+
UrlRedirect::resolve($url);
24+
2225
if (!$url->isValid()) {
2326
return false;
2427
}

Embed/RequestResolvers/UrlJsRedirect.php renamed to Embed/UrlRedirect.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22
/**
3-
* Class to resolve javascript based redirections
3+
* Class to resolve some specific redirections
44
*/
5-
namespace Embed\RequestResolvers;
5+
namespace Embed;
66

7-
class UrlJsRedirect
7+
class UrlRedirect
88
{
99
protected static $urls = array(
10-
'google' => 'https?://www.google.com/url*'
10+
'google' => 'https?://www.google.com/url*',
11+
'googleTranslator' => 'https?://translate.google.com/translate*'
1112
);
1213

14+
1315
/**
1416
* Resolve the url redirection
1517
*
@@ -42,4 +44,17 @@ protected static function google(Url $url)
4244
$url->setUrl($urlString);
4345
}
4446
}
47+
48+
49+
/**
50+
* Resolve a google translation url
51+
*
52+
* @param Url $url
53+
*/
54+
protected static function googleTranslator(Url $url)
55+
{
56+
if (($urlString = $url->getParameter('u'))) {
57+
$url->setUrl($urlString);
58+
}
59+
}
4560
}

tests/EmbedTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,15 @@ public function testUrlDirectory()
110110
$this->assertEquals('third', $url->getDirectory(2));
111111
$this->assertNull($url->getDirectory(3));
112112
}
113+
114+
public function testRedirections()
115+
{
116+
$this->checkUrl(
117+
'https://translate.google.com/translate?sl=de&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=http%3A%2F%2Fwww.heise.de%2Fnewsticker%2Fmeldung%2FXKeyscore-Quellcode-Tor-Nutzer-werden-von-der-NSA-als-Extremisten-markiert-und-ueberwacht-2248328.html&edit-text=',
118+
array(
119+
'title' => 'XKeyscore-Quellcode: Tor-Nutzer werden von der NSA als Extremisten markiert und überwacht',
120+
'image' => 'http://www.heise.de/imgs/18/1/2/6/2/0/9/3/urn-newsml-dpa-com-20090101-140212-99-03856_large_4_3-2f16ffb85099e6ad.jpeg'
121+
)
122+
);
123+
}
113124
}

0 commit comments

Comments
 (0)