| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -24,13 +24,15 @@ class Rels { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  public function fetch(Request $request, Response $response) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $opts = []; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if($request->get('timeout')) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      // We might make 2 HTTP requests, so each request gets half the desired timeout
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $this->http->timeout = $request->get('timeout') / 2; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $opts['timeout'] = $request->get('timeout') / 2; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if($request->get('max_redirects')) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $this->http->max_redirects = (int)$request->get('max_redirects'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $opts['max_redirects'] = (int)$request->get('max_redirects'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if($request->get('pretty')) { | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -46,51 +48,11 @@ class Rels { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Attempt some basic URL validation
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $scheme = parse_url($url, PHP_URL_SCHEME); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if(!in_array($scheme, ['http','https'])) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      return $this->respond($response, 400, [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        'error' => 'invalid_url', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        'error_description' => 'Only http and https URLs are supported' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $host = parse_url($url, PHP_URL_HOST); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if(!$host) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      return $this->respond($response, 400, [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        'error' => 'invalid_url', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        'error_description' => 'The URL provided was not valid' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $url = p3k\XRay\normalize_url($url); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $result = $this->http->get($url); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $html = $result['body']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $mf2 = mf2\Parse($html, $result['url']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $rels = $result['rels']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if(isset($mf2['rels'])) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $rels = array_merge($rels, $mf2['rels']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    // Resolve all relative URLs
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    foreach($rels as $rel=>$values) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      foreach($values as $i=>$value) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        $value = \mf2\resolveUrl($result['url'], $value); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					        $rels[$rel][$i] = $value; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    if(count($rels) == 0) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      $rels = new StdClass; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $xray = new p3k\XRay(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $xray->http = $this->http; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    $res = $xray->rels($url, $opts); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return $this->respond($response, 200, [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      'url' => $result['url'], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      'code' => $result['code'], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					      'rels' => $rels | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    ]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					    return $this->respond($response, !empty($res['error']) ? 400 : 200, $res); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					  } | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} |