wishthis/index.php

181 lines
3.5 KiB
PHP
Raw Normal View History

2021-11-12 15:23:48 +00:00
<?php
/**
* index.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
2022-03-04 09:09:49 +00:00
define('VERSION', '0.4.0');
2022-01-27 11:53:39 +00:00
define('ROOT', __DIR__);
/**
2021-11-12 15:23:48 +00:00
* Include
*/
2021-11-15 13:21:18 +00:00
require 'vendor/autoload.php';
2021-11-12 15:23:48 +00:00
2022-01-26 12:06:36 +00:00
$include = new Grandel\IncludeDirectory(__DIR__ . '/src/classes');
$include = new Grandel\IncludeDirectory(__DIR__ . '/src/functions');
2021-11-12 15:23:48 +00:00
/**
2021-11-15 08:45:36 +00:00
* Config
2021-11-12 15:23:48 +00:00
*/
2022-01-26 12:06:36 +00:00
$configPath = __DIR__ . '/' . 'src/config/config.php';
2021-11-12 15:23:48 +00:00
2021-11-14 08:13:01 +00:00
if (file_exists($configPath)) {
require $configPath;
}
/**
* Database
*/
2021-11-15 11:20:55 +00:00
$database = false;
2022-01-14 13:13:07 +00:00
$options = false;
2021-11-15 11:20:55 +00:00
2021-11-14 08:13:01 +00:00
if (
defined('DATABASE_HOST')
&& defined('DATABASE_NAME')
&& defined('DATABASE_USER')
&& defined('DATABASE_PASSWORD')
) {
$database = new wishthis\Database(
DATABASE_HOST,
DATABASE_NAME,
DATABASE_USER,
DATABASE_PASSWORD
);
2021-11-15 11:20:55 +00:00
2022-01-14 13:13:07 +00:00
/**
* Options
*/
$options = new wishthis\Options($database);
}
2022-01-14 09:50:48 +00:00
/**
* Session
*/
2022-03-10 08:28:01 +00:00
$sessionLifetime = 2592000; // 1 Month
session_set_cookie_params($sessionLifetime, '/');
session_start();
2022-03-10 08:28:01 +00:00
/** Refresh lifetime */
$session = session_get_cookie_params();
setcookie(
session_name(),
session_id(),
time() + $sessionLifetime,
$session['path'],
$session['domain'],
$session['secure'],
$session['httponly']
);
2022-03-22 12:43:54 +00:00
/**
* Language
*/
/** Determine Locale */
$userLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locales = array_filter(
array_map(
function ($value) {
if ('po' === pathinfo($value, PATHINFO_EXTENSION)) {
return pathinfo($value, PATHINFO_FILENAME);
}
},
scandir(ROOT . '/translations')
)
);
$locale = \Locale::lookup($locales, $userLocale, false, 'en');
/** Load Translation */
$translationFilepath = ROOT . '/translations/' . $locale . '.po';
$translations = null;
if (file_exists($translationFilepath)) {
$loader = new \Gettext\Loader\PoLoader();
$translations = $loader->loadFile($translationFilepath);
}
2022-03-22 13:28:26 +00:00
/**
* Wish
*/
wishthis\Wish::initialize();
/**
* API
*/
if (isset($api)) {
return;
}
2021-11-15 11:20:55 +00:00
/**
* Install
*/
2022-03-10 07:57:48 +00:00
if (!$options || !$options->getOption('isInstalled')) {
2022-01-14 09:53:53 +00:00
$page = 'install';
2021-11-12 15:23:48 +00:00
}
2022-01-18 13:37:35 +00:00
/**
* User
*/
if ($options) {
$user = new wishthis\User();
}
2022-01-14 13:13:07 +00:00
/**
* Update
2022-02-22 13:40:25 +00:00
*
* Check for update every 24 hours.
2022-01-14 13:13:07 +00:00
*/
2022-01-21 13:12:15 +00:00
use Github\Client;
2022-03-10 07:57:48 +00:00
if ($options && $options->getOption('isInstalled')) {
2022-02-22 13:40:25 +00:00
$updateLastChecked = $options->getOption('updateLastChecked');
if (!$updateLastChecked || time() - $updateLastChecked >= 86400) {
try {
$client = new Client();
$release = $client->api('repo')->releases()->latest('grandeljay', 'wishthis');
$tag = $release['tag_name'];
$version = str_replace('v', '', $tag);
if (-1 === version_compare($options->version, $version)) {
2022-03-05 09:11:30 +00:00
$options->setOption('updateAvailable', true);
2022-02-22 13:40:25 +00:00
}
} catch (\Github\Exception\RuntimeException $th) {
echo wishthis\Page::warning($th->getMessage());
2022-01-27 11:53:54 +00:00
}
2022-02-22 13:40:25 +00:00
$options->setOption('updateLastChecked', time());
2022-01-14 13:13:07 +00:00
}
}
2022-01-18 11:43:28 +00:00
/**
* Wishlist
*/
if (!isset($_GET['page']) && isset($_GET['wishlist'])) {
$page = 'wishlist';
}
2021-11-12 15:23:48 +00:00
/**
* Page
*/
if (!isset($page)) {
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
}
2022-01-26 12:06:36 +00:00
$pagePath = 'src/pages/' . $page . '.php';
2021-11-12 15:23:48 +00:00
2022-01-14 13:13:07 +00:00
if (file_exists($pagePath)) {
require $pagePath;
} else {
http_response_code(404);
?>
<h1>Not found</h1>
<p>The requested URL was not found on this server.</p>
<?php
2022-01-18 09:51:40 +00:00
echo $pagePath;
2022-01-14 13:13:07 +00:00
die();
}