make modify_url work better
This commit is contained in:
		
							parent
							
								
									70acc6015b
								
							
						
					
					
						commit
						b60e8ac5b4
					
				| @ -406,6 +406,22 @@ function get_base_href(): string | |||||||
|     return $dir; |     return $dir; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * The opposite of the standard library's parse_url | ||||||
|  |  */ | ||||||
|  | function unparse_url($parsed_url) { | ||||||
|  |     $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; | ||||||
|  |     $host     = isset($parsed_url['host']) ? $parsed_url['host'] : ''; | ||||||
|  |     $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; | ||||||
|  |     $user     = isset($parsed_url['user']) ? $parsed_url['user'] : ''; | ||||||
|  |     $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass']  : ''; | ||||||
|  |     $pass     = ($user || $pass) ? "$pass@" : ''; | ||||||
|  |     $path     = isset($parsed_url['path']) ? $parsed_url['path'] : ''; | ||||||
|  |     $query    = !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; | ||||||
|  |     $fragment = !empty($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; | ||||||
|  |     return "$scheme$user$pass$host$port$path$query$fragment"; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function startsWith(string $haystack, string $needle): bool | function startsWith(string $haystack, string $needle): bool | ||||||
| { | { | ||||||
|     $length = strlen($needle); |     $length = strlen($needle); | ||||||
|  | |||||||
| @ -39,4 +39,22 @@ class UrlsTest extends TestCase | |||||||
|             make_http("https://foo.com") |             make_http("https://foo.com") | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     public function test_modify_url() | ||||||
|  |     { | ||||||
|  |         $this->assertEquals( | ||||||
|  |             "/foo/bar?a=3&b=2", | ||||||
|  |             modify_url("/foo/bar?a=1&b=2", ["a"=>"3"]) | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         $this->assertEquals( | ||||||
|  |             "https://blah.com/foo/bar?b=2", | ||||||
|  |             modify_url("https://blah.com/foo/bar?a=1&b=2", ["a"=>null]) | ||||||
|  |         ); | ||||||
|  | 
 | ||||||
|  |         $this->assertEquals( | ||||||
|  |             "/foo/bar", | ||||||
|  |             modify_url("/foo/bar?a=1&b=2", ["a"=>null, "b"=>null]) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,7 +1,4 @@ | |||||||
| <?php declare(strict_types=1); | <?php declare(strict_types=1); | ||||||
| /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ |  | ||||||
| * HTML Generation                                                           * |  | ||||||
| \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |  | ||||||
| 
 | 
 | ||||||
| class Link | class Link | ||||||
| { | { | ||||||
| @ -60,40 +57,24 @@ function make_link(?string $page=null, ?string $query=null): string | |||||||
|  */ |  */ | ||||||
| function modify_current_url(array $changes): string | function modify_current_url(array $changes): string | ||||||
| { | { | ||||||
|     return modify_url($_SERVER['QUERY_STRING'], $changes); |     return modify_url($_SERVER['REQUEST_URI'], $changes); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function modify_url(string $url, array $changes): string | function modify_url(string $url, array $changes): string | ||||||
| { | { | ||||||
|     // SHIT: PHP is officially the worst web API ever because it does not
 |     $parts = parse_url($url); | ||||||
|     // have a built-in function to do this.
 |  | ||||||
| 
 | 
 | ||||||
|     // SHIT: parse_str is magically retarded; not only is it a useless name, it also
 |  | ||||||
|     // didn't return the parsed array, preferring to overwrite global variables with
 |  | ||||||
|     // whatever data the user supplied. Thankfully, 4.0.3 added an extra option to
 |  | ||||||
|     // give it an array to use...
 |  | ||||||
|     $params = []; |     $params = []; | ||||||
|     parse_str($url, $params); |     if(isset($parts['query'])) parse_str($parts['query'], $params); | ||||||
| 
 |  | ||||||
|     if (isset($changes['q'])) { |  | ||||||
|         $base = $changes['q']; |  | ||||||
|         unset($changes['q']); |  | ||||||
|     } else { |  | ||||||
|         $base = _get_query(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     if (isset($params['q'])) { |  | ||||||
|         unset($params['q']); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     foreach ($changes as $k => $v) { |     foreach ($changes as $k => $v) { | ||||||
|         if (is_null($v) and isset($params[$k])) { |         if (is_null($v) and isset($params[$k])) { | ||||||
|             unset($params[$k]); |             unset($params[$k]); | ||||||
|         } |         } | ||||||
|         $params[$k] = $v; |         $params[$k] = $v; | ||||||
|     } |     } | ||||||
|  |     $parts['query'] = http_build_query($params); | ||||||
| 
 | 
 | ||||||
|     return make_link($base, http_build_query($params)); |     return unparse_url($parts); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -117,6 +98,10 @@ function make_http(string $link): string | |||||||
|     return $link; |     return $link; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * If HTTP_REFERER is set, and not blacklisted, then return it | ||||||
|  |  * Else return a default $dest | ||||||
|  |  */ | ||||||
| function referer_or(string $dest, ?array $blacklist=null): string | function referer_or(string $dest, ?array $blacklist=null): string | ||||||
| { | { | ||||||
|     if(empty($_SERVER['HTTP_REFERER'])) return $dest; |     if(empty($_SERVER['HTTP_REFERER'])) return $dest; | ||||||
|  | |||||||
| @ -112,6 +112,7 @@ abstract class ShimmiePHPUnitTestCase extends TestCase | |||||||
|         if (!$args) { |         if (!$args) { | ||||||
|             $args = []; |             $args = []; | ||||||
|         } |         } | ||||||
|  |         $_SERVER['REQUEST_URI'] = make_link($page_name, http_build_query($args)); | ||||||
|         $_GET = $args; |         $_GET = $args; | ||||||
|         $_POST = []; |         $_POST = []; | ||||||
|         $page = new Page(); |         $page = new Page(); | ||||||
| @ -129,6 +130,7 @@ abstract class ShimmiePHPUnitTestCase extends TestCase | |||||||
|         if (!$args) { |         if (!$args) { | ||||||
|             $args = []; |             $args = []; | ||||||
|         } |         } | ||||||
|  |         $_SERVER['REQUEST_URI'] = make_link($page_name); | ||||||
|         foreach ($args as $k=>$v) { |         foreach ($args as $k=>$v) { | ||||||
|             if(is_array($v)) { |             if(is_array($v)) { | ||||||
|                 $args[$k] = $v; |                 $args[$k] = $v; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user