diff --git a/src/app/controller/index.php b/src/app/controller/index.php new file mode 100644 index 0000000..6be1c5b --- /dev/null +++ b/src/app/controller/index.php @@ -0,0 +1,116 @@ +_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'; + } +} \ No newline at end of file diff --git a/src/app/controller/module/footer.php b/src/app/controller/module/footer.php new file mode 100644 index 0000000..d1a1f81 --- /dev/null +++ b/src/app/controller/module/footer.php @@ -0,0 +1,25 @@ +announce) && !empty($tracker->stats)) + { + $response['trackers'][] = [ + 'announce' => $tracker->announce, + 'stats' => $tracker->stats, + ]; + } + } + } + + include __DIR__ . '../../../view/theme/default/module/footer.phtml'; + } +} \ No newline at end of file diff --git a/src/app/controller/module/head.php b/src/app/controller/module/head.php new file mode 100644 index 0000000..781873b --- /dev/null +++ b/src/app/controller/module/head.php @@ -0,0 +1,54 @@ +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'; + } +} \ No newline at end of file diff --git a/src/app/controller/module/header.php b/src/app/controller/module/header.php new file mode 100644 index 0000000..df162b1 --- /dev/null +++ b/src/app/controller/module/header.php @@ -0,0 +1,19 @@ +YGG', + WEBSITE_NAME + ); + + require_once __DIR__ . '/search.php'; + + $appControllerModuleSearch = new AppControllerModuleSearch(); + + include __DIR__ . '../../../view/theme/default/module/header.phtml'; + } +} \ No newline at end of file diff --git a/src/app/controller/module/page.php b/src/app/controller/module/page.php new file mode 100644 index 0000000..036a6e7 --- /dev/null +++ b/src/app/controller/module/page.php @@ -0,0 +1,9 @@ + $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 + } + } +} \ No newline at end of file diff --git a/src/app/controller/module/search.php b/src/app/controller/module/search.php new file mode 100644 index 0000000..cf5ea4a --- /dev/null +++ b/src/app/controller/module/search.php @@ -0,0 +1,11 @@ +_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'; + } +} \ No newline at end of file diff --git a/src/app/controller/submit.php b/src/app/controller/submit.php new file mode 100644 index 0000000..dddf4fc --- /dev/null +++ b/src/app/controller/submit.php @@ -0,0 +1,96 @@ +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'; + } +} \ No newline at end of file diff --git a/src/app/controller/user.php b/src/app/controller/user.php new file mode 100644 index 0000000..ba58cdb --- /dev/null +++ b/src/app/controller/user.php @@ -0,0 +1,152 @@ +_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 + ); + } +} \ No newline at end of file diff --git a/src/app/controller/welcome.php b/src/app/controller/welcome.php new file mode 100644 index 0000000..611be0d --- /dev/null +++ b/src/app/controller/welcome.php @@ -0,0 +1,119 @@ +_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'; + } +} \ No newline at end of file diff --git a/src/library/database.php b/src/app/model/database.php similarity index 96% rename from src/library/database.php rename to src/app/model/database.php index ccb6c0e..ec435cb 100644 --- a/src/library/database.php +++ b/src/app/model/database.php @@ -1,6 +1,6 @@ rowCount(); } + public function findUserPageStarsDistinctTotal(int $userId, bool $value) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageStar` WHERE `userId` = ? AND `value` = ?'); + + $query->execute([$userId, (int) $value]); + + return $query->fetch()->result; + } + + public function findUserPageViewsDistinctTotal(int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageView` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + } + + public function findUserPageDownloadsDistinctTotal(int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageDownload` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + } + + public function findUserPageCommentsDistinctTotal(int $userId) : int { + + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageComment` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + } + + public function findUserPageEditionsDistinctTotal(int $userId) : int { + + return 0; + + /* @TODO + $this->_debug->query->select->total++; + + $query = $this->_db->prepare('SELECT COUNT(DISTINCT `pageId`) AS `result` FROM `userPageEdition` WHERE `userId` = ?'); + + $query->execute([$userId]); + + return $query->fetch()->result; + */ + } + // Magnet public function addMagnet(int $userId, int $xl, diff --git a/src/library/sphinx.php b/src/app/model/sphinx.php similarity index 99% rename from src/library/sphinx.php rename to src/app/model/sphinx.php index 6cb15ae..db2f8be 100644 --- a/src/library/sphinx.php +++ b/src/app/model/sphinx.php @@ -1,6 +1,6 @@ + + render() ?> +
+ render() ?> +