Browse Source

get rid of global functions

moves XRay classes to `p3k\XRay` namespace
pull/38/head
Aaron Parecki 7 years ago
parent
commit
5221cf79e9
No known key found for this signature in database GPG Key ID: 276C2817346D6056
14 changed files with 36 additions and 46 deletions
  1. +1
    -1
      composer.json
  2. +5
    -5
      composer.lock
  3. +2
    -2
      controllers/Certbot.php
  4. +1
    -1
      controllers/Main.php
  5. +8
    -4
      controllers/Parse.php
  6. +1
    -1
      controllers/Rels.php
  7. +1
    -1
      lib/XRay/Formats/GitHub.php
  8. +1
    -1
      lib/XRay/Formats/HTMLPurifier_AttrDef_HTML_Microformats2.php
  9. +1
    -1
      lib/XRay/Formats/Instagram.php
  10. +4
    -25
      lib/XRay/Formats/Mf2.php
  11. +1
    -1
      lib/XRay/Formats/Twitter.php
  12. +1
    -1
      lib/XRay/Formats/XKCD.php
  13. +1
    -0
      lib/helpers.php
  14. +8
    -2
      tests/HelpersTest.php

+ 1
- 1
composer.json View File

@ -17,7 +17,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"XRay\\": "lib/XRay"
"p3k\\XRay\\": "lib/XRay"
}, },
"files": [ "files": [
"lib/helpers.php" "lib/helpers.php"

+ 5
- 5
composer.lock View File

@ -256,16 +256,16 @@
}, },
{ {
"name": "p3k/http", "name": "p3k/http",
"version": "0.1.1",
"version": "0.1.4",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/aaronpk/p3k-http.git", "url": "https://github.com/aaronpk/p3k-http.git",
"reference": "7409b0a44f190b053d694304e716de7ce5b3568b"
"reference": "136aac6f7ecd6d6e16e8ff9286b43110680c49ab"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/7409b0a44f190b053d694304e716de7ce5b3568b",
"reference": "7409b0a44f190b053d694304e716de7ce5b3568b",
"url": "https://api.github.com/repos/aaronpk/p3k-http/zipball/136aac6f7ecd6d6e16e8ff9286b43110680c49ab",
"reference": "136aac6f7ecd6d6e16e8ff9286b43110680c49ab",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -290,7 +290,7 @@
], ],
"description": "A simple wrapper API around the PHP curl functions", "description": "A simple wrapper API around the PHP curl functions",
"homepage": "https://github.com/aaronpk/p3k-http", "homepage": "https://github.com/aaronpk/p3k-http",
"time": "2017-04-28T18:51:28+00:00"
"time": "2017-04-28T19:46:12+00:00"
}, },
{ {
"name": "p3k/timezone", "name": "p3k/timezone",

+ 2
- 2
controllers/Certbot.php View File

@ -13,7 +13,7 @@ class Certbot {
$state = mt_rand(10000,99999); $state = mt_rand(10000,99999);
$_SESSION['state'] = $state; $_SESSION['state'] = $state;
$response->setContent(view('certbot', [
$response->setContent(p3k\XRay\view('certbot', [
'title' => 'X-Ray', 'title' => 'X-Ray',
'state' => $state 'state' => $state
])); ]));
@ -109,7 +109,7 @@ class Certbot {
'challenge' => $challenge 'challenge' => $challenge
]), 0, 600); ]), 0, 600);
$response->setContent(view('certbot', [
$response->setContent(p3k\XRay\view('certbot', [
'title' => 'X-Ray', 'title' => 'X-Ray',
'challenge' => $challenge, 'challenge' => $challenge,
'token' => $token, 'token' => $token,

+ 1
- 1
controllers/Main.php View File

@ -5,7 +5,7 @@ use Symfony\Component\HttpFoundation\Response;
class Main { class Main {
public function index(Request $request, Response $response) { public function index(Request $request, Response $response) {
$response->setContent(view('index', [
$response->setContent(p3k\XRay\view('index', [
'title' => 'X-Ray' 'title' => 'X-Ray'
])); ]));
return $response; return $response;

+ 8
- 4
controllers/Parse.php View File

@ -2,7 +2,7 @@
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use XRay\Formats;
use p3k\XRay\Formats;
class Parse { class Parse {
@ -92,7 +92,7 @@ class Parse {
]); ]);
} }
$url = \normalize_url($url);
$url = p3k\XRay\normalize_url($url);
// Check if this is a Twitter URL and if they've provided API credentials, use the API // Check if this is a Twitter URL and if they've provided API credentials, use the API
if(preg_match('/https?:\/\/(?:mobile\.twitter\.com|twitter\.com|twtr\.io)\/(?:[a-z0-9_\/!#]+statuse?s?\/([0-9]+)|([a-zA-Z0-9_]+))/i', $url, $match)) { if(preg_match('/https?:\/\/(?:mobile\.twitter\.com|twitter\.com|twtr\.io)\/(?:[a-z0-9_\/!#]+statuse?s?\/([0-9]+)|([a-zA-Z0-9_]+))/i', $url, $match)) {
@ -103,10 +103,14 @@ class Parse {
return $this->parseGitHubURL($request, $response, $url); return $this->parseGitHubURL($request, $response, $url);
} }
if(!should_follow_redirects($url))
// Special-case appspot.com URLs to not follow redirects.
// https://cloud.google.com/appengine/docs/php/urlfetch/
if(!p3k\XRay\should_follow_redirects($url)) {
$this->http->set_max_redirects(0);
$this->http->set_transport(new p3k\HTTP\Stream()); $this->http->set_transport(new p3k\HTTP\Stream());
else
} else {
$this->http->set_transport(new p3k\HTTP\Curl()); $this->http->set_transport(new p3k\HTTP\Curl());
}
// Now fetch the URL and check for any curl errors // Now fetch the URL and check for any curl errors
// Don't cache the response if a token is used to fetch it // Don't cache the response if a token is used to fetch it

+ 1
- 1
controllers/Rels.php View File

@ -63,7 +63,7 @@ class Rels {
]); ]);
} }
$url = \normalize_url($url);
$url = p3k\XRay\normalize_url($url);
$result = $this->http->get($url); $result = $this->http->get($url);

+ 1
- 1
lib/XRay/Formats/GitHub.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
use DateTime, DateTimeZone; use DateTime, DateTimeZone;
use Parse, Config; use Parse, Config;

+ 1
- 1
lib/XRay/Formats/HTMLPurifier_AttrDef_HTML_Microformats2.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
/** /**
* Allows Microformats2 classes but rejects any others * Allows Microformats2 classes but rejects any others

+ 1
- 1
lib/XRay/Formats/Instagram.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
use DOMDocument, DOMXPath; use DOMDocument, DOMXPath;
use DateTime, DateTimeZone; use DateTime, DateTimeZone;

+ 4
- 25
lib/XRay/Formats/Mf2.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
use HTMLPurifier, HTMLPurifier_Config; use HTMLPurifier, HTMLPurifier_Config;
use Parse; use Parse;
@ -48,7 +48,7 @@ class Mf2 {
foreach($mf2['items'] as $item) { foreach($mf2['items'] as $item) {
if(array_key_exists('url', $item['properties'])) { if(array_key_exists('url', $item['properties'])) {
$urls = $item['properties']['url']; $urls = $item['properties']['url'];
$urls = array_map('self::normalize_url', $urls);
$urls = array_map('\p3k\XRay\normalize_url', $urls);
if(in_array($url, $urls)) { if(in_array($url, $urls)) {
Parse::debug("mf2:1: Recognized $url as a permalink because an object on the page matched the URL of the request"); Parse::debug("mf2:1: Recognized $url as a permalink because an object on the page matched the URL of the request");
if(in_array('h-card', $item['type'])) { if(in_array('h-card', $item['type'])) {
@ -77,7 +77,7 @@ class Mf2 {
foreach($mf2['items'] as $card) { foreach($mf2['items'] as $card) {
if(in_array('h-card', $card['type']) && array_key_exists('url', $card['properties'])) { if(in_array('h-card', $card['type']) && array_key_exists('url', $card['properties'])) {
$urls = $card['properties']['url']; $urls = $card['properties']['url'];
$urls = array_map('self::normalize_url', $urls);
$urls = array_map('\p3k\XRay\normalize_url', $urls);
if(count(array_intersect($urls, $mf2['rels']['author'])) > 0) { if(count(array_intersect($urls, $mf2['rels']['author'])) > 0) {
// There is an author h-card on this page // There is an author h-card on this page
// Now look for the first h-* object other than an h-card and use that as the object // Now look for the first h-* object other than an h-card and use that as the object
@ -496,7 +496,7 @@ class Mf2 {
$found = false; $found = false;
foreach($item['properties']['url'] as $url) { foreach($item['properties']['url'] as $url) {
if(self::isURL($url)) { if(self::isURL($url)) {
$url = self::normalize_url($url);
$url = \p3k\XRay\normalize_url($url);
if($url == $authorURL) { if($url == $authorURL) {
$data['url'] = $url; $data['url'] = $url;
$found = true; $found = true;
@ -723,25 +723,4 @@ class Mf2 {
} }
return \mf2\Parse($result['body'], $url); return \mf2\Parse($result['body'], $url);
} }
private static function normalize_url($url) {
$parts = parse_url($url);
if(empty($parts['path']))
$parts['path'] = '/';
$parts['host'] = strtolower($parts['host']);
return self::build_url($parts);
}
private static function build_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 = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
} }

+ 1
- 1
lib/XRay/Formats/Twitter.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
use DateTime, DateTimeZone; use DateTime, DateTimeZone;
use Parse; use Parse;

+ 1
- 1
lib/XRay/Formats/XKCD.php View File

@ -1,5 +1,5 @@
<?php <?php
namespace XRay\Formats;
namespace p3k\XRay\Formats;
use DOMDocument, DOMXPath; use DOMDocument, DOMXPath;
use DateTime, DateTimeZone; use DateTime, DateTimeZone;

+ 1
- 0
lib/helpers.php View File

@ -1,4 +1,5 @@
<?php <?php
namespace p3k\XRay;
function view($template, $data=[]) { function view($template, $data=[]) {
global $templates; global $templates;

+ 8
- 2
tests/HelpersTest.php View File

@ -3,14 +3,20 @@ class HelpersTest extends PHPUnit_Framework_TestCase {
public function testLowercaseHostname() { public function testLowercaseHostname() {
$url = 'http://Example.com/'; $url = 'http://Example.com/';
$result = normalize_url($url);
$result = p3k\XRay\normalize_url($url);
$this->assertEquals('http://example.com/', $result); $this->assertEquals('http://example.com/', $result);
} }
public function testAddsSlashToBareDomain() { public function testAddsSlashToBareDomain() {
$url = 'http://example.com'; $url = 'http://example.com';
$result = normalize_url($url);
$result = p3k\XRay\normalize_url($url);
$this->assertEquals('http://example.com/', $result); $this->assertEquals('http://example.com/', $result);
} }
public function testDoesNotModify() {
$url = 'https://example.com/';
$result = p3k\XRay\normalize_url($url);
$this->assertEquals('https://example.com/', $result);
}
} }

Loading…
Cancel
Save