| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/app/classes/ |
Upload File : |
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace app\classes;
/**
* Description of Page
*
* @author Maroš
*/
class Page extends PDOElement {
const ELEMENTS_SORT = "elements_sort";
protected $mainElement;
protected $elements;
protected $elementsObjects = array();
protected $title;
protected $isBlog = false;
protected $fullUrl;
protected $tempVars = array();
protected $description;
protected $keywords;
protected $metaPicture;
protected $metaPictureHeight;
protected $metaPictureWidth;
protected $message = false;
protected $originalTitle;
protected $breadcrumbs = array();
protected $printTitle = true;
protected $metaIndex = true;
protected $metaFollow = true;
public function __construct($id, $hidden = false, $disabled = false, $blog = false) {
$this->archiveMode = false;
$this->isBlog = $blog;
$bindArray = array("state_deleted" => \app\constants\State::DELETED, is_numeric($id) ? "id" : "address" => $id);
$conditions = " state!=:state_deleted ";
$conditions .= $hidden === true ? "" : " AND state!=:state_hidden";
$conditions .= $disabled === true ? "" : " AND state!=:state_disabled";
$hidden ? "" : $bindArray["state_hidden"] = \app\constants\PageState::HIDDEN;
$disabled ? "" : $bindArray["state_disabled"] = \app\constants\PageState::DISABLED;
parent::__construct($id, $blog ? "posts" : "pages", $bindArray, is_numeric($id) ? "id" : "address", $conditions);
$this->id = $this->element["id"];
$this->idColumnName = "id";
$this->uniqueColumn = "address";
try {
$this->mainElement = new PageElement($this->db->getValue("SELECT id FROM " . ($blog ? "posts_elements" : "pages_elements") . " WHERE " . ($blog ? "posts_id" : "pages_id") . "=:pages_id AND type=:type", array("pages_id" => $this->getId(), "type" => \app\constants\PageElementType::MAIN_CONTENT)), $this, true, $blog);
} catch (\Exception $ex) {
//probably not found (first instance of page)
}
$this->elements = new PagesElements($this, $disabled, $blog);
$this->title = $this->_get("name");
$this->description = $this->_get("description");
$this->keywords = $this->_get("keywords");
$this->metaPicture = \app\constants\Config::DEFAULT_IMAGE;
$this->fullUrl = $this->_get("address") . "/";
$this->metaPictureHeight = \app\constants\Config::DEFAULT_IMAGE_HEIGHT;
$this->metaPictureWidth = \app\constants\Config::DEFAULT_IMAGE_WIDTH;
if (strlen(trim($this->description)) === 0 || strlen(trim($this->keywords)) === 0) {
try {
$app = \App::getInstance();
$homePage = new PDOElement($app->getConfig(\app\constants\Config::HOME_PAGE), "pages", array("id" => $app->getConfig(\app\constants\Config::HOME_PAGE)), "id");
if (strlen(trim($this->description)) === 0) {
$this->description = $homePage->_get("description");
}
if (strlen(trim($this->keywords)) === 0) {
$this->keywords = $homePage->_get("keywords");
}
} catch (\Exception $ex) {
echo $ex->getMessage();
}
}
}
public static function newElement($data, $requiredColumns, $columns = array(), $blog = false) {
$db = PDODatabase::getInstance();
$vars = array(
"description",
"keywords",
"logo",
);
if ($blog) {
$vars[] = "category";
}
$inserts = parent::newElement($data, $requiredColumns, $vars);
$attempts = 0;
$pm = PagesManagement::getInstance();
if (!$pm->pageTemplateExists($inserts["template"])) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_INPUT, \app\constants\ResponseCodes::INVALID_INPUT);
}
addPage:
try {
$db->addRecord($blog ? "posts" : "pages", $inserts);
} catch (\Exception $ex) {
if ($ex->getCode() === 1062 || intval($ex->getCode()) === 23000) {
$attempts++;
$inserts["address"] = $data["address"] . "-" . $attempts;
goto addPage;
}
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
$pageId = $db->getLastInsertId();
$page = new Page($pageId, true, true, $blog);
$page->addMetadataVariable(self::ELEMENTS_SORT, array());
$page->addElement("Hlavný obsah stránky", $data["content"], \app\constants\PageElementType::MAIN_CONTENT, array(), \app\constants\State::ACTIVE, $blog);
$pagesManagement = PagesManagement::getInstance();
if (!$blog) {
$pagesManagement->addPageToSort($data["type"], $pageId);
}
return $page;
}
public function delete() {
$pm = PagesManagement::getInstance();
if (($this->table === "pages")) {
foreach ($pm->findSubPages($this->_get("type"), $this->_get("id")) as $pageSort) {
try {
$page = new Page($pageSort["id"], true, true);
$page->delete();
} catch (\Exception $ex) {
// page probably doesnt exist
}
}
}
$NULL = NULL;
foreach ($this->elements->toArray() as $element) {
$element = new PageElement($element, $NULL, true, $this->table === "posts");
$element->delete();
}
parent::delete();
}
public function addElementToSort($id) {
$sort = $this->getMetadata(self::ELEMENTS_SORT);
$sort[] = array("id" => $id);
$this->setMetadata(self::ELEMENTS_SORT, $sort);
}
public function setElementsSort(array $sort) {
$this->setMetadata(self::ELEMENTS_SORT, $sort);
}
public function removeElementFromSort($id) {
$sort = $this->getMetadata(self::ELEMENTS_SORT);
foreach ($sort as $item) {
if (intval($id) === intval($item["id"])) {
unset($sort[$id]);
$this->setMetadata(self::ELEMENTS_SORT, $sort);
break;
}
}
}
/**
* @return PageElement
*/
public function getMainElement() {
return $this->mainElement;
}
public function update(array $dataArray, array $exclude = array()) {
if (key_exists("template", $dataArray)) {
$pm = PagesManagement::getInstance();
if (!$pm->pageTemplateExists($dataArray["template"])) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_INPUT, \app\constants\ResponseCodes::INVALID_INPUT);
}
}
if (key_exists("address", $dataArray)) {
$address = $dataArray["address"];
$attempts = 0;
updatePage:
try {
parent::update($dataArray, $exclude);
} catch (\Exception $ex) {
if ($ex->getCode() === 1062 || intval($ex->getCode()) === 23000) {
$attempts++;
$dataArray["address"] = $address . "-" . $attempts;
goto updatePage;
}
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
if (key_exists("content", $dataArray)) {
$this->getMainElement()->update(array("content" => $dataArray["content"]));
}
if (key_exists("type", $dataArray) && $this->_get("type") !== $dataArray["type"]) {
$pm = PagesManagement::getInstance();
if (($this->table === "pages")) {
$pm->addPageToSort($dataArray["type"], $this->getId());
}
}
parent::update($dataArray, $exclude);
}
public function toArray($exclude = array()) {
return $this->mainElement === null ? parent::toArray($exclude) : array_merge(parent::toArray($exclude), array("content" => $this->mainElement->getContent()));
}
/**
* @return PagesElements
*/
public function getElements() {
return $this->elements;
}
public function addElement($name, $content, $type, array $metadata, $state = \app\constants\State::ACTIVE, $blog = false) {
PageElement::newElement(array(
"name" => $name,
"content" => $content,
$blog ? "posts_id" : "pages_id" => $this->getId(),
"state" => $state,
"type" => $type,
"metadata" => serialize($metadata)
), array(), array(), $blog);
$element = new PageElement($this->db->getLastInsertId(), $this, true, $blog);
$this->addElementToSort($element->getId());
}
public function prepareElements() {
foreach ($this->elements->toArray() as $element) {
$elementObj = new PageElement($element, $this);
$this->elementsObjects[$element["id"]] = $elementObj;
$elementObj->prepareWidget();
}
}
public function renderElements() {
foreach ($this->getMetadata(self::ELEMENTS_SORT) as $sort) {
if (isset($this->elementsObjects[$sort["id"]])) {
$this->elementsObjects[$sort["id"]]->render();
}
}
}
public function renderHead() {
foreach ($this->getMetadata(self::ELEMENTS_SORT) as $sort) {
if (isset($this->elementsObjects[$sort["id"]])) {
$this->elementsObjects[$sort["id"]]->renderHead();
}
}
}
public function renderFooter() {
foreach ($this->getMetadata(self::ELEMENTS_SORT) as $sort) {
if (isset($this->elementsObjects[$sort["id"]])) {
$this->elementsObjects[$sort["id"]]->renderFooter();
}
}
}
public function prepare() {
$this->notFoundIfIsNode();
$this->prepareElements();
if (class_exists("\modules\seo\code\Seo") && isset($_SERVER) && isset($_SERVER["REQUEST_URI"])) {
try {
$seo = new \modules\seo\code\Seo($_SERVER["REQUEST_URI"], FILTER_VALIDATE_URL, true);
$seo->apply($this);
} catch (\Exception $ex) {
}
}
}
public function verifyLogin() {
if (intval($this->_get("requires_login")) === 1) {
BasicUser::getLoggedUser();
}
}
public function verifyPasswordChange() {
if (intval($this->_get("requires_login")) === 1) {
$user = BasicUser::getLoggedUser();
if ($user->_get("state") === \app\constants\State::CREATED) {
throw new \Exception("Password change required!", \app\constants\ResponseCodes::INVALID_PASSWORD);
}
}
}
public function render() {
require DOCUMENT_ROOT . "templates/templates/" . $this->_get("template") . ".php";
}
public function getTitle() {
return $this->title;
}
public function setTitle($title) {
$this->title = $title;
}
public function getFullUrl() {
return $this->fullUrl;
}
public function setFullUrl($url) {
$this->fullUrl = $url;
}
protected function notFoundIfIsNode() {
$pm = PagesManagement::getInstance();
$subpages = $pm->findSubPages($this->_get("type"), $this->_get("id"));
if (count($subpages) > 0) {
throw new \Exception(\app\constants\ResponseCodes::ITEM_NOT_FOUND, \app\constants\ResponseCodes::ITEM_NOT_FOUND);
}
}
public function publicatePost() {
if ($this->isBlog && $this->_get("state") === \app\constants\State::DISABLED && strtotime($this->_get("date_of_publication")) < time()) {
$this->update(array("state" => \app\constants\State::ACTIVE));
$app = \App::getInstance();
$translator = \app\classes\Translator::getInstance();
$mailer = $app->getPHPMailer("info");
$vars = array(
"link" => array(
"translate" => false,
"value" => $app->getBaseUrl() . strtolower($this->_get("category")) . "/" . $this->_get("address") . "/"
)
);
$message = toHtml($this->_get("email_content")) . "<br><br>" . $translator->convertVarsText(convertToHtmlTags($translator->_get("blog_email_postfix")), $vars);
$mailer->Subject = $this->_get("name");
$mailer->Body = $message;
$users = new PDOCollection("users", "id", false, array("state" => \app\constants\State::ACTIVE), " WHERE state=:state");
foreach ($users->toArray() as $user) {
$mailer->clearAllRecipients();
$mailer->addAddress($user["officer_email"]);
if ($mailer->send()) {
Logger::log(\app\classes\Logger::INFO, "app\\classes\\Page->publicatePost()", "Post ID " . $this->_get("id") . ": e-mail notification successfully sent: " . $user["email"]);
} else {
Logger::log(\app\classes\Logger::ERROR, "app\\classes\\Page->publicatePost()", "Post ID " . $this->_get("id") . ": e-mail notification failed: " . $user["email"]);
}
usleep(mt_rand(4000000, 5000000));
$string = $user["newsletter_emails"];
$explode = explode(",", $string);
if (is_array($explode)) {
foreach ($explode as $email) {
if (filter_var(trim($email), FILTER_VALIDATE_EMAIL) && trim($email) !== trim($user["officer_email"])) {
$mailer->clearAllRecipients();
$mailer->addAddress(trim($email));
if ($mailer->send()) {
Logger::log(\app\classes\Logger::INFO, "app\\classes\\Page->publicatePost()", "Post ID " . $this->_get("id") . ": additional e-mail notification successfully sent: " . trim($email));
} else {
Logger::log(\app\classes\Logger::ERROR, "app\\classes\\Page->publicatePost()", "Post ID " . $this->_get("id") . ": additional e-mail notification failed: " . trim($email));
}
usleep(mt_rand(4000000, 5000000));
}
}
}
}
}
}
public function getTempVar($key) {
if (isset($this->tempVars[$key])) {
return $this->tempVars[$key];
}
return null;
}
public function setTempVar($key, $value) {
$this->tempVars[$key] = $value;
}
function getDescription() {
return $this->description;
}
function getKeywords() {
return $this->keywords;
}
function getMetaPicture() {
return $this->metaPicture;
}
function getMetaPictureHeight() {
return $this->metaPictureHeight;
}
function getMetaPictureWidth() {
return $this->metaPictureWidth;
}
function getMessage() {
return $this->message;
}
function getOriginalTitle() {
return $this->originalTitle;
}
function setDescription($description) {
$this->description = $description;
}
function setKeywords($keywords) {
$this->keywords = $keywords;
}
function setMetaPicture($metaPicture) {
$this->metaPicture = $metaPicture;
}
function setMetaPictureHeight($metaPictureHeight) {
$this->metaPictureHeight = $metaPictureHeight;
}
function setMetaPictureWidth($metaPictureWidth) {
$this->metaPictureWidth = $metaPictureWidth;
}
function setMessage($message) {
$this->message = $message;
}
function setOriginalTitle($title) {
$this->originalTitle = $title;
}
function setPrintTitle($bool) {
$this->printTitle = $bool;
}
function printTitle() {
return $this->printTitle;
}
function setIndexFollow(bool $index, bool $follow) {
$this->metaIndex = $index;
$this->metaFollow = $follow;
}
function getIndexFollow() {
return ($this->metaIndex ? "INDEX" : "NOINDEX") . ", " . ($this->metaFollow ? "FOLLOW" : "NOFOLLOW");
}
protected function initBreadcrumbs($sort, $pages, $isLogged = false, $depth = 0) {
$app = App::getInstance();
$activeNode = false;
$loggedSkipPages = array(
$app->getConfig(app\constants\Config::LOGIN_PAGE),
$app->getConfig(app\constants\Config::REGISTRATION_PAGE)
);
$skipPages = array(
$app->getConfig(app\constants\Config::LOGGED_MENU_NODE_PAGE),
);
foreach ($sort as $item) {
if (isset($pages[$item["id"]])) {
$page = $pages[$item["id"]];
if ((!$isLogged && intval($page["requires_login"]) === 1) || ($isLogged && in_array($page["id"], $loggedSkipPages)) || (in_array($page["id"], $skipPages))) {
continue;
}
if (isset($item["children"])) {
if (getBreadcrumbs($item["children"], $pages, $this->_get("address"), $isLogged, $depth + 1) || $page["address"] === $this->_get("address")) {
$this->breadcrumbs[$page["id"]] = $page;
$activeNode = true;
}
} else {
if ($page["address"] === $this->_get("address")) {
$this->breadcrumbs[$page["id"]] = $page;
$activeNode = true;
}
}
}
}
return $activeNode;
}
}