Last active
December 7, 2020 02:17
-
-
Save Acen/edc6ab5863200743c0300773709e0a53 to your computer and use it in GitHub Desktop.
Facebook PHP Manual Unshim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function manualUnshim(string $url): string | |
{ | |
if(!str_contains($url, 'l.facebook.com')){ | |
return $url; | |
} | |
$urlArray = explode('u=', $url); | |
$decodedUrl = urldecode($urlArray[1]); | |
$parsed_url = parse_url($decodedUrl); | |
unset($parsed_url['query']); | |
$urlClass = new UnparsedURL($parsed_url); | |
return $urlClass->url(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sourced from https://gist.github.com/Ellrion/f51ba0d40ae1d62eeae44fd1adf7b704 | |
*/ | |
class UnparsedURL | |
{ | |
private $parts; | |
public function __construct(array $parts){ | |
$this->parts = $parts; | |
} | |
/** | |
* @return string | |
*/ | |
public function url(): string | |
{ | |
$scheme = isset($this->parts['scheme']) ? $this->parts['scheme'] . '://' : ''; | |
$host = $this->parts['host'] ?? ''; | |
$port = isset($this->parts['port']) ? ':' . $this->parts['port'] : ''; | |
$user = $this->parts['user'] ?? ''; | |
$pass = isset($this->parts['pass']) ? ':' . $this->parts['pass'] : ''; | |
$pass = $user || $pass ? "$pass@" : ''; | |
$path = $this->parts['path'] ?? ''; | |
$query = isset($this->parts['query']) ? '?' . $this->parts['query'] : ''; | |
$fragment = isset($this->parts['fragment']) ? '#' . $this->parts['fragment'] : ''; | |
return implode('', [ | |
$scheme, | |
$user, | |
$pass, | |
$host, | |
$port, | |
$path, | |
$query, | |
$fragment, | |
]); | |
} | |
/** | |
* @return string | |
*/ | |
public function __toString(): string | |
{ | |
return $this->url(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
class UnparsedURLTest extends TestCase | |
{ | |
public function UrlDataProvider(): array | |
{ | |
return [ | |
[parse_url('http://www.google.com/'), 'http://www.google.com/'], | |
[parse_url('http://@foo:1/path/path?#'), 'http://foo:1/path/path'], | |
[ | |
parse_url('http://u:p@foo:1/path/path?q#frag'), | |
'http://u:p@foo:1/path/path?q#frag', | |
], | |
[ | |
[ | |
'scheme' => 'https', | |
'host' => 'domain.co.nz', | |
'path' => | |
'/blahblah-blah', | |
], | |
'https://domain.co.nz/blahblah-blah' | |
], | |
]; | |
} | |
/** | |
* @dataProvider UrlDataProvider | |
* @param array $input | |
* @param $expected | |
*/ | |
public function testUrl(array $input, $expected): void | |
{ | |
$unparsedUrl = new UnparsedURL($input); | |
self::assertSame($expected, $unparsedUrl->url()); | |
} | |
/** | |
* @dataProvider UrlDataProvider | |
* @param array $input | |
* @param $expected | |
*/ | |
public function test__toString(array $input, $expected): void | |
{ | |
$this->expectOutputString($expected); | |
$unparsedUrl = new UnparsedURL($input); | |
echo $unparsedUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment