Improve cache handling

This commit is contained in:
grandeljay 2022-06-16 10:28:44 +02:00
parent 962bbcd89c
commit ea5bae3f74
4 changed files with 59 additions and 49 deletions

View file

@ -50,56 +50,37 @@ switch ($_SERVER['REQUEST_METHOD']) {
break;
}
$wish_title = trim($_POST['wish_title']);
$wish_description = trim($_POST['wish_description']);
$wish_title = empty(trim($_POST['wish_title'])) ? 'NULL' : '"' . trim($_POST['wish_title']) . '"';
$wish_description = empty(trim($_POST['wish_description'])) ? 'NULL' : '"' . trim($_POST['wish_description']) . '"';
$wish_image = 'NULL';
$wish_url = trim($_POST['wish_url']);
$wish_url = empty(trim($_POST['wish_url'])) ? 'NULL' : '"' . trim($_POST['wish_url']) . '"';
$wish_priority = isset($_POST['wish_priority']) && $_POST['wish_priority'] ? $_POST['wish_priority'] : 'NULL';
$wish_is_purchasable = isset($_POST['wish_is_purchasable']) ? 'true' : 'false';
if (isset($_POST['wish_id'], $_POST['wishlist_id'])) {
/** Update wish */
$wish_id = $_POST['wish_id'];
$wishlist_id = $_POST['wishlist_id'];
$wish = new Wish($_POST['wish_id']);
/** Update wish information */
if (!empty($wish_url)) {
$cache = new Cache\Embed($wish_url);
$cache = new Cache\Embed(trim($_POST['wish_url']));
$info = $cache->get(true);
if (empty($wish_title)) {
if (empty($wish_title) && empty($wish->title)) {
$wish_title = $info->title;
}
if (empty($wish_description)) {
if (empty($wish_description) && empty($wish->description)) {
$wish_description = $info->description;
}
if (null !== $info->image) {
/** URL */
$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',
);
/** Image */
if (!empty($info->image)) {
$codeImage = URL::getResponseCode($info->image);
$ch = curl_init($info->image);
curl_setopt_array($ch, $ch_options);
$favicon = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 === $code) {
if (200 === $codeImage) {
$wish_image = '"' . $info->image . '"';
}
curl_close($ch);
}
$response = array(
@ -110,14 +91,14 @@ switch ($_SERVER['REQUEST_METHOD']) {
$database
->query(
'UPDATE `wishes`
SET `wishlist` = ' . $wishlist_id . ',
`title` = "' . $wish_title . '",
`description` = "' . $wish_description . '",
`image` = ' . $wish_image . ',
`url` = "' . $wish_url . '",
`priority` = ' . $wish_priority . ',
`is_purchasable` = ' . $wish_is_purchasable . '
WHERE `id` = ' . $wish_id . ';'
SET `wishlist` = ' . $wish->wishlist . ',
`title` = ' . $wish_title . ',
`description` = ' . $wish_description . ',
`image` = ' . $wish_image . ',
`url` = ' . $wish_url . ',
`priority` = ' . $wish_priority . ',
`is_purchasable` = ' . $wish_is_purchasable . '
WHERE `id` = ' . $wish->id . ';'
);
/**
@ -134,12 +115,12 @@ switch ($_SERVER['REQUEST_METHOD']) {
`wish`,
`price`
) VALUES (
' . $wish_id . ',
' . $wish->id . ',
' . $wish_price . '
);'
);
$response['lastInsertId'] = $wish_id;
$response['lastInsertId'] = $wish->id;
} elseif (isset($_POST['wishlist_id'])) {
/** Insert wish */
$wishlist_id = $_POST['wishlist_id'];

View file

@ -105,17 +105,11 @@ class Embed extends Cache
}
/** URL */
$ch = curl_init($info->url);
curl_setopt_array($ch, $ch_options);
$codeURL = \wishthis\URL::getResponseCode($info->url);
$favicon = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $code) {
if (200 !== $codeURL) {
$generateCache = false;
}
curl_close($ch);
}
if ($generateCache) {

View file

@ -10,6 +10,41 @@ namespace wishthis;
class URL
{
/**
* Static
*/
public static function getResponseCode(string $url): int
{
$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',
);
$ch = curl_init($url);
curl_setopt_array($ch, $ch_options);
curl_exec($ch);
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (0 === $responseCode) {
echo curl_error($ch);
}
curl_close($ch);
return $responseCode;
}
/**
* Non-Static
*/
public string $url;
public function __construct(string $url)

View file

@ -81,7 +81,7 @@ class Wish
'SELECT ' . self::SELECT . '
FROM ' . self::FROM . '
LEFT JOIN ' . self::LEFT_JOIN . '
WHERE ' . sprintf(self::WHERE, $_GET['wish_id'])
WHERE ' . sprintf(self::WHERE, $wish)
)
->fetch();