init MVC framework refactory #14

This commit is contained in:
ghost 2023-09-23 21:37:52 +03:00
parent c4f5409ffa
commit a600a08a28
28 changed files with 1235 additions and 925 deletions

View file

@ -0,0 +1,116 @@
<?php
class AppControllerIndex
{
private $_db;
private $_sphinx;
private $_memory;
public function __construct()
{
require_once __DIR__ . '/../../library/database.php';
require_once __DIR__ . '/../../library/sphinx.php';
require_once __DIR__ . '/../../library/scrapeer.php';
require_once __DIR__ . '/../../library/time.php';
require_once __DIR__ . '/../../library/curl.php';
require_once __DIR__ . '/../../library/valid.php';
require_once __DIR__ . '/../../library/filter.php';
require_once __DIR__ . '/../../../vendor/autoload.php';
try
{
$this->_db = new Database(
DB_HOST,
DB_PORT,
DB_NAME,
DB_USERNAME,
DB_PASSWORD
);
$this->_sphinx = new Sphinx(
SPHINX_HOST,
SPHINX_PORT
);
$this->_memory = new \Yggverse\Cache\Memory(
MEMCACHED_HOST,
MEMCACHED_PORT,
MEMCACHED_NAMESPACE,
MEMCACHED_TIMEOUT + time()
);
}
catch (Exception $error)
{
require_once __DIR__ . '/error/500.php';
$controller = new AppControllerError500(
print_r($error, true)
);
$controller->render();
exit;
}
}
public function render()
{
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$pages = [];
require_once __DIR__ . '/module/pagination.php';
$appControllerModulePagination = new appControllerModulePagination();
require_once __DIR__ . '/module/head.php';
$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
$page > 1 ?
sprintf(
_('Page %s - BitTorrent Registry for Yggdrasil - %s'),
$page,
WEBSITE_NAME
) :
sprintf(
_('%s - BitTorrent Registry for Yggdrasil'),
WEBSITE_NAME
),
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);
require_once __DIR__ . '/module/profile.php';
$appControllerModuleProfile = new AppControllerModuleProfile($user->userId);
require_once __DIR__ . '/module/header.php';
$appControllerModuleHeader = new AppControllerModuleHeader();
require_once __DIR__ . '/module/footer.php';
$appControllerModuleFooter = new AppControllerModuleFooter();
include __DIR__ . '/../view/theme/default/index.phtml';
}
}

View file

@ -0,0 +1,25 @@
<?php
class AppControllerModuleFooter
{
public function render()
{
$response['trackers'] = [];
if ($trackers = json_decode(file_get_contents(__DIR__ . '/../../../config/trackers.json')))
{
foreach ($trackers as $tracker)
{
if (!empty($tracker->announce) && !empty($tracker->stats))
{
$response['trackers'][] = [
'announce' => $tracker->announce,
'stats' => $tracker->stats,
];
}
}
}
include __DIR__ . '../../../view/theme/default/module/footer.phtml';
}
}

View file

@ -0,0 +1,54 @@
<?php
class AppControllerModuleHead
{
private $_title;
private $_base;
private $_links = [];
public function __construct(string $base, string $title, array $links = [])
{
$this->setBase($base);
$this->setTitle($title);
foreach ($links as $link)
{
$this->addLink(
$link['rel'],
$link['type'],
$link['href'],
);
}
}
public function setBase(string $base) : void
{
$this->_base = $base;
}
public function setTitle(string $title) : void
{
$this->_title = $title;
}
public function addLink(string $rel, string $type, string $href) : void
{
$this->_links[] = (object)
[
'rel' => $rel,
'type' => $type,
'href' => $href,
];
}
public function render()
{
$base = $this->_base;
$links = $this->_links;
$title = htmlentities($this->_title);
include __DIR__ . '../../../view/theme/default/module/head.phtml';
}
}

View file

@ -0,0 +1,19 @@
<?php
class AppControllerModuleHeader
{
public function render()
{
$name = str_replace(
'YGG',
'<span>YGG</span>',
WEBSITE_NAME
);
require_once __DIR__ . '/search.php';
$appControllerModuleSearch = new AppControllerModuleSearch();
include __DIR__ . '../../../view/theme/default/module/header.phtml';
}
}

View file

@ -0,0 +1,9 @@
<?php
class AppControllerCommonPage
{
public function render(int $pageId)
{
include __DIR__ . '../../../view/theme/default/common/page.phtml';
}
}

View file

@ -0,0 +1,43 @@
<?php
class AppControllerModulePagination
{
public function render(string $url, int $total, int $limit)
{
if ($total > $limit)
{
parse_str($url, $query);
$pagination->page = isset($query['total']) ? (int) $query['total'] : 1;
$pagination->pages = ceil($total / $limit);
// Previous
if ($page > 1)
{
$query['page'] = $page - 1;
$pagination->back = sprintf('%s', WEBSITE_URL, http_build_query($query));
}
else
{
$pagination->back = false;
}
// Next
if ($page < ceil($total / $limit))
{
$query['page'] = $page + 1;
$pagination->next = sprintf('%s', WEBSITE_URL, http_build_query($query));
}
else
{
$pagination->next = false;
}
// Render
}
}
}

View file

@ -0,0 +1,11 @@
<?php
class AppControllerModuleSearch
{
public function render()
{
$query = empty($_GET['query']) ? false : urldecode($_GET['query']);
include __DIR__ . '../../../view/theme/default/module/search.phtml';
}
}

View file

@ -0,0 +1,65 @@
<?php
class AppControllerResponse
{
private $_title;
private $_h1;
private $_text;
private $_code;
public function __construct(string $title, string $h1, string $text, int $code = 200)
{
$this->_title = $title;
$this->_h1 = $h1;
$this->_text = $text;
$this->_code = $code;
}
public function render()
{
header(
sprintf(
'HTTP/1.0 %s Not Found',
$this->_code
)
);
$h1 = $this->_h1;
$text = $this->_text;
require_once __DIR__ . '/module/head.php';
$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
$this->_title,
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);
require_once __DIR__ . '/module/header.php';
$appControllerModuleHeader = new AppControllerModuleHeader();
require_once __DIR__ . '/module/footer.php';
$appControllerModuleFooter = new AppControllerModuleFooter();
include __DIR__ . '../../view/theme/default/response.phtml';
}
}

View file

@ -0,0 +1,96 @@
<?php
class AppControllerSubmit
{
private function _response(string $title, string $h1, string $text, int $code = 200)
{
require_once __DIR__ . '/response.php';
$appControllerResponse = new AppControllerResponse(
$title,
$h1,
$text,
$code
);
$appControllerResponse->render();
exit;
}
public function render()
{
require_once __DIR__ . '/user.php';
$appControllerUser = new AppControllerUser(
$_SERVER['REMOTE_ADDR']
);
// Get user info
if (!$user = $appControllerUser->getUser())
{
$this->_response(
sprintf(
_('Error - %s'),
WEBSITE_NAME
),
_('500'),
_('Could not init user'),
500
);
}
// Require account type selection
if (is_null($user->public))
{
header(
sprintf('Location: %s/welcome', trim(WEBSITE_URL, '/'))
);
}
// Render
require_once __DIR__ . '/module/head.php';
$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
sprintf(
_('Submit - %s'),
WEBSITE_NAME
),
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);
require_once __DIR__ . '/module/profile.php';
$appControllerModuleProfile = new AppControllerModuleProfile(
$appControllerUser
);
require_once __DIR__ . '/module/header.php';
$appControllerModuleHeader = new AppControllerModuleHeader();
require_once __DIR__ . '/module/footer.php';
$appControllerModuleFooter = new AppControllerModuleFooter();
include __DIR__ . '../../view/theme/default/submit.phtml';
}
}

152
src/app/controller/user.php Normal file
View file

@ -0,0 +1,152 @@
<?php
class AppControllerUser
{
private $_database;
private $_user;
public function __construct(string $address)
{
// Connect DB
require_once __DIR__ . '/../model/database.php';
try
{
$this->_database = new AppModelDatabase(
DB_HOST,
DB_PORT,
DB_NAME,
DB_USERNAME,
DB_PASSWORD
);
}
catch (Exception $error)
{
$this->_response(
sprintf(
_('Error - %s'),
WEBSITE_NAME
),
_('500'),
print_r($error, true),
500
);
}
// Validate user address
require_once __DIR__ . '/../../library/valid.php';
$error = [];
if (!Valid::host($address, $error))
{
$this->_response(
sprintf(
_('Error - %s'),
WEBSITE_NAME
),
_('406'),
print_r($error, true),
406
);
}
// Init user session
try
{
$this->_database->beginTransaction();
$this->_user = $this->_database->getUser(
$this->_database->initUserId(
$address,
USER_DEFAULT_APPROVED,
time()
)
);
$this->_database->commit();
}
catch (Exception $error)
{
$this->_database->rollback();
$this->_response(
sprintf(
_('Error - %s'),
WEBSITE_NAME
),
_('500'),
print_r($error, true),
500
);
}
}
private function _response(string $title, string $h1, string $text, int $code = 200)
{
require_once __DIR__ . '/response.php';
$appControllerResponse = new AppControllerResponse(
$title,
$h1,
$text,
$code
);
$appControllerResponse->render();
exit;
}
public function getUser()
{
return $this->_user;
}
public function findUserPageStarsDistinctTotalByValue(bool $value) : int
{
return $this->_database->findUserPageStarsDistinctTotal(
$this->_user->userId,
$value
);
}
public function findUserPageViewsDistinctTotal() : int
{
return $this->_database->findUserPageViewsDistinctTotal(
$this->_user->userId
);
}
public function findUserPageDownloadsDistinctTotal() : int
{
return $this->_database->findUserPageDownloadsDistinctTotal(
$this->_user->userId
);
}
public function findUserPageCommentsDistinctTotal() : int
{
return $this->_database->findUserPageCommentsDistinctTotal(
$this->_user->userId
);
}
public function findUserPageEditionsDistinctTotal() : int
{
return $this->_database->findUserPageEditionsDistinctTotal(
$this->_user->userId
);
}
public function updateUserPublic(bool $public, int $time) : int
{
return $this->_database->updateUserPublic(
$this->_user->userId,
$public,
$time
);
}
}

View file

@ -0,0 +1,119 @@
<?php
class AppControllerWelcome
{
private $_user;
public function __construct()
{
require_once __DIR__ . '/../model/user.php';
$this->_user = new AppModelUser(
$_SERVER['REMOTE_ADDR']
);
}
private function _response(string $title, string $h1, string $text, int $code = 200)
{
require_once __DIR__ . '/response.php';
$appControllerResponse = new AppControllerResponse(
$title,
$h1,
$text,
$code
);
$appControllerResponse->render();
exit;
}
public function render()
{
if (!$user = $this->_user->get())
{
$this->_response(
sprintf(
_('Error - %s'),
WEBSITE_NAME
),
_('500'),
_('Could not init user'),
500
);
}
if (!is_null($user->public))
{
$this->_response(
sprintf(
_('Welcome back - %s'),
WEBSITE_NAME
),
_('Welcome back!'),
sprintf(
_('You already have selected account type to %s'),
$user->public ? _('Distributed') : _('Local')
),
405
);
}
if (isset($_POST['public']))
{
if ($this->_user->updateUserPublic((bool) $_POST['public'], time()))
{
$this->_response(
sprintf(
_('Success - %s'),
WEBSITE_NAME
),
_('Success!'),
sprintf(
_('Account type successfully changed to %s'),
$_POST['public'] ? _('Distributed') : _('Local')
),
);
}
}
require_once __DIR__ . '/module/head.php';
$appControllerModuleHead = new AppControllerModuleHead(
WEBSITE_URL,
sprintf(
_('Welcome to %s'),
WEBSITE_NAME
),
[
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/common.css?%s',
WEBSITE_CSS_VERSION
),
],
[
'rel' => 'stylesheet',
'type' => 'text/css',
'href' => sprintf(
'assets/theme/default/css/framework.css?%s',
WEBSITE_CSS_VERSION
),
],
]
);
require_once __DIR__ . '/module/header.php';
$appControllerModuleHeader = new AppControllerModuleHeader();
require_once __DIR__ . '/module/footer.php';
$appControllerModuleFooter = new AppControllerModuleFooter();
include __DIR__ . '../../view/theme/default/welcome.phtml';
}
}