Refactor
This commit is contained in:
parent
2b6339e2c6
commit
4ff2112247
6 changed files with 218 additions and 13 deletions
33
index.php
33
index.php
|
@ -6,6 +6,8 @@
|
|||
* @author Jay Trees <github.jay@grandel.anonaddy.me>
|
||||
*/
|
||||
|
||||
namespace wishthis;
|
||||
|
||||
define('VERSION', '0.6.0');
|
||||
define('ROOT', __DIR__);
|
||||
define('DEFAULT_LOCALE', 'en_GB');
|
||||
|
@ -15,8 +17,25 @@ define('DEFAULT_LOCALE', 'en_GB');
|
|||
*/
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
$include = new Grandel\IncludeDirectory(__DIR__ . '/src/classes');
|
||||
$include = new Grandel\IncludeDirectory(__DIR__ . '/src/functions');
|
||||
$include = new \Grandel\IncludeDirectory(__DIR__ . '/src/functions');
|
||||
|
||||
spl_autoload_register(
|
||||
function (string $fullClass) {
|
||||
/** Only include classes from this namespace */
|
||||
if (__NAMESPACE__ === substr($fullClass, 0, strlen(__NAMESPACE__))) {
|
||||
$fullClass = substr($fullClass, strlen(__NAMESPACE__));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parts = explode('\\', $fullClass);
|
||||
$class = implode('/', $parts);
|
||||
|
||||
$filepath = ROOT . '/src/classes/' . strtolower($class) . '.php';
|
||||
|
||||
require $filepath;
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Config
|
||||
|
@ -39,7 +58,7 @@ if (
|
|||
&& defined('DATABASE_USER')
|
||||
&& defined('DATABASE_PASSWORD')
|
||||
) {
|
||||
$database = new wishthis\Database(
|
||||
$database = new Database(
|
||||
DATABASE_HOST,
|
||||
DATABASE_NAME,
|
||||
DATABASE_USER,
|
||||
|
@ -49,7 +68,7 @@ if (
|
|||
/**
|
||||
* Options
|
||||
*/
|
||||
$options = new wishthis\Options($database);
|
||||
$options = new Options($database);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,7 +96,7 @@ setcookie(
|
|||
* User
|
||||
*/
|
||||
if ($options) {
|
||||
$user = new wishthis\User();
|
||||
$user = new User();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,7 +132,7 @@ if (file_exists($translationFilepath)) {
|
|||
/**
|
||||
* Wish
|
||||
*/
|
||||
wishthis\Wish::initialize();
|
||||
Wish::initialize();
|
||||
|
||||
/**
|
||||
* API
|
||||
|
@ -125,7 +144,7 @@ if (isset($api)) {
|
|||
/**
|
||||
* Pretty URLs
|
||||
*/
|
||||
$url = new \wishthis\URL($_SERVER['REQUEST_URI']);
|
||||
$url = new URL($_SERVER['REQUEST_URI']);
|
||||
|
||||
if ($url->isPretty()) {
|
||||
$_SESSION['_GET'] = query_to_key_value_pair($url->getPermalink());
|
||||
|
|
|
@ -24,14 +24,14 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
WHERE `id` = ' . $_GET['wish_id'] . ';')
|
||||
->fetch();
|
||||
|
||||
$wish = new wish($columns, true);
|
||||
$wish = new Wish($columns, true);
|
||||
|
||||
$response = array(
|
||||
'info' => $wish,
|
||||
'html' => $wish->getCard($_GET['wishlist_user'])
|
||||
);
|
||||
} elseif (isset($_GET['wish_url'])) {
|
||||
$cache = new EmbedCache($_GET['wish_url']);
|
||||
$cache = new Cache\Embed($_GET['wish_url']);
|
||||
$info = $cache->get(true);
|
||||
$exists = $cache->exists() ? 'true' : 'false';
|
||||
|
||||
|
|
15
src/classes/cache/blog.php
vendored
Normal file
15
src/classes/cache/blog.php
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* WordPress blog cache.
|
||||
*/
|
||||
|
||||
namespace wishthis;
|
||||
|
||||
class BlogCache extends Cache
|
||||
{
|
||||
public function __construct(private string $url)
|
||||
{
|
||||
$this->$directory = parent::$directory . '/blog';
|
||||
}
|
||||
}
|
171
src/classes/cache/cache.php
vendored
Normal file
171
src/classes/cache/cache.php
vendored
Normal file
|
@ -0,0 +1,171 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Generic cache class
|
||||
*/
|
||||
|
||||
namespace wishthis;
|
||||
|
||||
class Cache
|
||||
{
|
||||
/**
|
||||
* Private
|
||||
*/
|
||||
private string $directory = ROOT . '/src/cache';
|
||||
private string $filepath;
|
||||
|
||||
private function getIdentifier(): string
|
||||
{
|
||||
return md5($this->url);
|
||||
}
|
||||
|
||||
private function getFilepath(): string
|
||||
{
|
||||
return $this->directory . '/' . $this->getIdentifier();
|
||||
}
|
||||
|
||||
/**
|
||||
* Public
|
||||
*/
|
||||
public function __construct(private string $url)
|
||||
{
|
||||
}
|
||||
|
||||
public function get(bool $generateCache = false): \stdClass
|
||||
{
|
||||
$filepath = $this->getFilepath();
|
||||
|
||||
$info = file_exists($filepath) ? json_decode(file_get_contents($filepath)) : new \stdClass();
|
||||
|
||||
if (true === $this->generateCache() || true === $generateCache) {
|
||||
/**
|
||||
* @link https://github.com/oscarotero/Embed
|
||||
*/
|
||||
$embed = new \Embed\Embed();
|
||||
|
||||
$info_simplified = new \stdClass();
|
||||
$info_simplified->authorName = '';
|
||||
$info_simplified->authorUrl = '';
|
||||
$info_simplified->cms = '';
|
||||
$info_simplified->code = '';
|
||||
$info_simplified->description = '';
|
||||
$info_simplified->favicon = '';
|
||||
$info_simplified->feeds = array();
|
||||
$info_simplified->icon = '';
|
||||
$info_simplified->image = '';
|
||||
$info_simplified->keywords = array();
|
||||
$info_simplified->language = '';
|
||||
$info_simplified->languages = array();
|
||||
$info_simplified->license = '';
|
||||
$info_simplified->providerName = '';
|
||||
$info_simplified->providerUrl = '';
|
||||
$info_simplified->publishedTime = '';
|
||||
$info_simplified->redirect = '';
|
||||
$info_simplified->title = $this->url;
|
||||
$info_simplified->url = $this->url;
|
||||
|
||||
if ($generateCache) {
|
||||
try {
|
||||
$info = $embed->get($this->url);
|
||||
$info_simplified->authorName = (string) $info->authorName;
|
||||
$info_simplified->authorUrl = (string) $info->authorUrl;
|
||||
$info_simplified->cms = (string) $info->cms;
|
||||
$info_simplified->code = (string) $info->code;
|
||||
$info_simplified->description = (string) $info->description;
|
||||
$info_simplified->favicon = (string) $info->favicon;
|
||||
$info_simplified->feeds = (array) $info->feeds;
|
||||
$info_simplified->icon = (string) $info->icon;
|
||||
$info_simplified->image = isset($info->image) ? (string) $info->image : null;
|
||||
$info_simplified->keywords = (array) $info->keywords;
|
||||
$info_simplified->language = (string) $info->language;
|
||||
$info_simplified->languages = (array) $info->languages;
|
||||
$info_simplified->license = (string) $info->license;
|
||||
$info_simplified->providerName = (string) $info->providerName;
|
||||
$info_simplified->providerUrl = (string) $info->providerUrl;
|
||||
$info_simplified->publishedTime = $info->publishedTime ? $info->publishedTime->format('d.m.Y') : '';
|
||||
$info_simplified->redirect = (string) $info->redirect;
|
||||
$info_simplified->title = (string) $info->title;
|
||||
$info_simplified->url = (string) $info->url;
|
||||
} catch (\Throwable $ex) {
|
||||
$generateCache = false;
|
||||
|
||||
echo $ex->getMessage();
|
||||
}
|
||||
|
||||
$ch_options = array(
|
||||
CURLOPT_AUTOREFERER => true,
|
||||
CURLOPT_CONNECTTIMEOUT => 30,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0',
|
||||
);
|
||||
|
||||
/** Favicon */
|
||||
if (str_contains(pathinfo($info_simplified->favicon, PATHINFO_EXTENSION), 'ico')) {
|
||||
$ch = curl_init($info_simplified->favicon);
|
||||
curl_setopt_array($ch, $ch_options);
|
||||
|
||||
$favicon = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$info_simplified->favicon = $favicon && 200 === $code ? 'data:image/x-icon;base64,' . base64_encode($favicon) : '';
|
||||
}
|
||||
|
||||
/** Repsonse code */
|
||||
$ch = curl_init($info_simplified->url);
|
||||
curl_setopt_array($ch, $ch_options);
|
||||
|
||||
$favicon = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
if (0 === $code || 404 === $code) {
|
||||
$generateCache = false;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
/** Update info */
|
||||
$info = $info_simplified;
|
||||
}
|
||||
|
||||
if ($generateCache) {
|
||||
$directory = dirname($filepath);
|
||||
|
||||
if (false === file_exists($directory)) {
|
||||
mkdir($directory);
|
||||
}
|
||||
|
||||
file_put_contents($filepath, json_encode($info));
|
||||
}
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function exists(): bool
|
||||
{
|
||||
return file_exists($this->getFilepath());
|
||||
}
|
||||
|
||||
public function age(): int
|
||||
{
|
||||
return time() - filemtime($this->getFilepath());
|
||||
}
|
||||
|
||||
public function maxAge(): int
|
||||
{
|
||||
return 2592000; // 30 days
|
||||
}
|
||||
|
||||
public function generateCache(): bool
|
||||
{
|
||||
return !$this->exists()
|
||||
|| ($this->exists() && $this->age() > $this->maxAge());
|
||||
}
|
||||
}
|
|
@ -8,9 +8,9 @@
|
|||
* @author Jay Trees <github.jay@grandel.anonaddy.me>
|
||||
*/
|
||||
|
||||
namespace wishthis;
|
||||
namespace wishthis\Cache;
|
||||
|
||||
class EmbedCache
|
||||
class Embed
|
||||
{
|
||||
/**
|
||||
* Private
|
|
@ -41,7 +41,7 @@ class Wish
|
|||
/**
|
||||
* Non-Static
|
||||
*/
|
||||
private EmbedCache $cache;
|
||||
private Cache\Embed $cache;
|
||||
|
||||
public int $id;
|
||||
public int $wishlist;
|
||||
|
@ -84,7 +84,7 @@ class Wish
|
|||
$this->info = new \stdClass();
|
||||
|
||||
if ($this->url) {
|
||||
$this->cache = new EmbedCache($this->url);
|
||||
$this->cache = new Cache\Embed($this->url);
|
||||
$this->info = $this->cache->get($generateCache);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue