| 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/ttjs.cz/dev/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.
*/
/**
* Description of App
*
* @author Maroš
*/
class App {
private static $instance;
private static $secret = "7aVaexUnzbn57TuuK6CdT4Oq4Dn8PRi2xU5lub6Tv8aGv6Ke6oOcj2TbLN8VZJdX1dl6sJ8BjZVunnKnIWGWelReeU2DAmBvpnrn";
private $config;
private $baseUrl;
private $db;
private $lang;
private $translator;
protected function __construct() {
$document_root = $_SERVER["DOCUMENT_ROOT"];
if (substr($document_root, -1) !== "/") {
$document_root .= "/";
}
define("DOCUMENT_ROOT", $document_root);
foreach (scandir($document_root . "app" . DIRECTORY_SEPARATOR . "functions") as $filename) {
$path = __DIR__ . "/../functions" . DIRECTORY_SEPARATOR . $filename;
if (is_file($path)) {
require_once $path;
}
}
spl_autoload_register('App::loadSources');
$config = new \app\classes\PDOCollection("config", "variable");
$this->config = $config->toArray();
if (defined("APP_VERSION_REWRITE")) {
if (floatval(APP_VERSION_REWRITE) !== floatval($this->config["version"]["value"])) {
$this->config["version"]["value"] = APP_VERSION_REWRITE;
}
}
define("APP_VERSION_TAG", "?v=" . $this->getVersion());
$this->baseUrl = $this->config["protocol"]["value"] . "://" . $this->config["domain"]["value"] . "/";
$this->db = \app\classes\PDODatabase::getInstance();
$this->lang = $this->getConfig(app\constants\Config::LANG);
$this->escapeInputs();
}
/**
* @return App
*/
public static function getInstance() {
if (App::$instance == NULL) {
App::$instance = new App();
}
return App::$instance;
}
/**
* @return App
*/
public static function run() {
return App::getInstance();
}
/**
* Loads main sources from app folder.
*/
private static function loadSources() {
spl_autoload_register(function ($class_name) {
$parts = explode("\\", $class_name);
$file = implode("/", $parts) . '.php';
$file_absolute = __DIR__ . "/../../" . implode("/", $parts) . ".php";
if (file_exists($file)) {
require_once $file;
return;
}
if (file_exists($file_absolute)) {
require_once $file_absolute;
return;
}
if (file_exists($class_name)) {
require_once $class_name;
return;
}
});
}
static function getSecret() {
return self::$secret;
}
/**
* Convert xss vulnerable tags back to chars.
* @param string $value <p>string to convert</p>
* @return string <p>Returns converted string.</p>
*/
public function convertToHtmlTags($value) {
return str_replace(array("<", ">", """, "'"), array("<", ">", '"', "'"), $value);
}
/**
* Convert xss vulnerable tags.
* @param string $value <p>string to convert</p>
* @return string <p>Returns converted string.</p>
*/
public function convertToNonHtmlTags($value) {
return str_replace(array("<", ">", '"', "'"), array("<", ">", """, "'"), $value);
}
/**
* Get config value (from config table in database).
* @param string $key <p>key</p>
* @return mixed <p>Returns value of config variable (string or array). If key doesn't exist, return FALSE.</p>
*/
public function getConfig($key) {
if (!key_exists($key, $this->config)) {
return false;
}
$unserialized = @unserialize($this->config[$key]["value"]);
return $unserialized === false ? $this->config[$key]["value"] : $unserialized;
}
/**
* Set config value - this change will be affected in database.
* @param string $key <p>key</p>
* @param mixed $val <p>value</p>
* @return mixed <p>Returns TRUE on success otherwise FALSE(if key doesn't exist).</p>
*/
public function setConfig($key, $val) {
if (!key_exists($key, $this->config)) {
return false;
}
$this->config[$key]["value"] = is_array($val) ? serialize($val) : $val;
$this->db->updateRecord("config", array("value" => $this->config[$key]["value"]), $key, "variable");
return true;
}
/**
* Add new config variable.
* @param string $key <p>key</p>
* @param mixed $val <p>default value</p>
* @return mixed <p>Returns TRUE on success otherwise FALSE(if key already exists).</p>
*/
public function addConfigVariable($key, $val = "") {
if (key_exists($key, $this->config)) {
return false;
}
$this->config[$key]["value"] = is_array($val) ? serialize($val) : $val;
$this->db->addRecord("config", array("variable" => $key, "value" => $this->config[$key]["value"]));
return true;
}
/**
* Base url of website.
* @return string <p>Returns base url (eg. https://www.backend.sk).</p>
*/
public function getBaseUrl($includeWwwIfPossible = false) {
if ($includeWwwIfPossible && substr_count($this->baseUrl, ".") === 1) {
return str_replace("://", "://www.", $this->baseUrl);
}
return $this->baseUrl;
}
public function setLang($lang) {
$this->lang = $lang;
$this->checkTranslatorInstance();
$this->translator->setLang($lang);
}
public function getLang() {
return $this->lang;
}
public function setTranslator(\app\classes\Translator $translator) {
$this->translator = $translator;
}
private function checkTranslatorInstance() {
if ($this->translator === NULL) {
$this->translator = \app\classes\Translator::getInstance();
}
}
private function escapeInputs() {
array_walk_recursive($_GET, function (&$val) {
$val = str_replace(array("<", ">", '"', "'"), array("<", ">", """, "'"), $val);
});
array_walk_recursive($_POST, function (&$val) {
$val = str_replace(array("<", ">", '"', "'"), array("<", ">", """, "'"), $val);
});
}
/**
* Version of CMS.
* @return string <p>Returns version of CMS.</p>
*/
public function getVersion() {
if (\app\constants\Config::DEV_MODE) {
return time();
}
return $this->getConfig("version");
}
/**
* Version tag of CMS.
* @return string <p>Returns version tag of CMS.</p>
*/
public function tagVersion() {
if (app\constants\Config::DEV_MODE) {
echo "?v=" . time();
} else {
echo "?v=" . $this->getVersion();
}
}
public function getLoggedEmail() {
return isset($_SESSION["email"]) ? $_SESSION["email"] : false;
}
public function getPageAddress($pageConfigConstant) {
$pageId = is_numeric($pageConfigConstant) ? $pageConfigConstant : $this->getConfig($pageConfigConstant);
if ($pageId === false) {
return false;
}
$address = $this->db->getValue("SELECT address FROM pages WHERE id=:id", array("id" => $pageId));
return $address === NULL ? false : $address;
}
public function redirect($pageConfigConstant) {
$address = $this->getPageAddress($pageConfigConstant);
if ($address === false) {
header('Location: ' . $this->getBaseUrl() . $pageConfigConstant . "/");
exit;
}
header('Location: ' . $this->getBaseUrl() . $address . "/");
exit;
}
private function getNativeEmailHeader() {
$domain = $this->getConfig(\app\constants\Config::DOMAIN);
$header = array(
'From: "' . $domain . '" <' . substr($domain, 0, strpos($domain, ".")) . '@' . $domain . '>',
'Reply-To: "No Reply" <noreply@' . $domain . '>',
"MIME-Version: 1.0",
"Content-type:text/html;charset=UTF-8"
);
return implode("\r\n", $header);
}
public function sendAlertEmailToSysAdmin($subject, $message) {
return mail(app\constants\Config::SYSADMIN_MAIL, "backend.sk | " . $subject . " (" . DOCUMENT_ROOT . ") " . (date("d.m.Y H:i:s")), $message, $this->getNativeEmailHeader(), null);
}
public function sendMultipleEmails($emails, $subject, $message) {
\app\classes\Logger::log(\app\classes\Logger::INFO, "App", "Exec command: php " . DOCUMENT_ROOT . "threads/send-email.php '" . $emails . " ' '" . $subject . " ' '" . $message . " ' > /dev/null &");
exec("php " . DOCUMENT_ROOT . "threads/send-email.php '" . $emails . " ' '" . $subject . " ' '" . $message . " ' > /dev/null &");
}
public function sendEmailNative($to, $subject, $message, $additional_headers = null, $additional_parameters = null, $attachments = array()) {
$mailer = $this->getPHPMailer();
$mailer->Subject = $subject;
$mailer->Body = $this->getEmailHeader() . $message . $this->getEmailFooter();
$mailer->addAddress($to);
foreach ($attachments as $key => $attachment) {
$mailer->addAttachment($attachment, is_numeric($key) ? "" : $key);
}
$sent = $mailer->send();
if ($sent) {
\app\classes\Logger::log(\app\classes\Logger::INFO, "App", "E-mail successfully sent to: " . $to . ", SUBJECT: " . $subject . ", MESSAGE: " . $message);
} else {
if (isset($mailer)) {
\app\classes\Logger::log(\app\classes\Logger::ERROR, "App", "E-mail sent failed: " . $to . ", ERROR:" . $mailer->ErrorInfo . ", SUBJECT: " . $subject . ", MESSAGE: " . $message);
} else {
\app\classes\Logger::log(\app\classes\Logger::ERROR, "App", "E-mail sent failed: " . $to . ", SUBJECT: " . $subject . ", MESSAGE: " . $message);
}
$this->sendAlertEmailToSysAdmin("E-mail sending failed", "See db_logs ID: " . $this->db->getLastInsertId());
}
return $sent;
// return mail($to, $subject, $message, $additional_headers, $additional_parameters);
}
protected function getEmailHeader() {
$path = DOCUMENT_ROOT . "templates/email/header.php";
if (file_exists($path)) {
ob_start();
require $path;
$output = ob_get_clean();
return $output;
}
return "";
}
protected function getEmailFooter() {
$path = DOCUMENT_ROOT . "templates/email/footer.php";
if (file_exists($path)) {
ob_start();
require $path;
$output = ob_get_clean();
return $output;
}
return "";
}
/**
* @return PHPMailer\PHPMailer\PHPMailer
*/
public function getPHPMailer($from = false, $replyTo = false) {
$mailer = new PHPMailer\PHPMailer\PHPMailer();
$domain = $this->getConfig(\app\constants\Config::DOMAIN);
$mailer->setFrom($this->getConfig(\app\constants\Config::SMTP_SENDER), $this->getConfig(\app\constants\Config::SMTP_SENDER_NAME));
if ($replyTo === false) {
$mailer->addReplyTo("noreply@" . $domain, "No Reply");
} else {
$mailer->addReplyTo($replyTo);
}
$mailer->isHTML();
$mailer->CharSet = 'UTF-8';
$mailer->IsSMTP();
// $mailer->SMTPDebug = 2;
$mailer->Host = $this->getConfig(\app\constants\Config::SMTP_SERVER);
$mailer->Port = $this->getConfig(\app\constants\Config::SMTP_PORT);
$mailer->SMTPAuth = true;
$mailer->SMTPAutoTLS = true;
$mailer->Username = $this->getConfig(\app\constants\Config::SMTP_LOGIN);
$mailer->Password = $this->getConfig(\app\constants\Config::SMTP_PASSWORD);
$mailer->SMTPSecure = $this->getConfig(\app\constants\Config::SMTP_SECURE); //tls for sendinblue
return $mailer;
}
public function getModuleConfig($class) {
$rc = new \ReflectionClass(get_class($class));
$fullPath = dirname($rc->getFileName());
$modulePath = substr($fullPath, strpos($fullPath, "modules/") + 8);
$moduleName = substr($modulePath, 0, strpos($modulePath, "/")) . "_module";
$config = $this->getConfig($moduleName);
if ($config === false) {
$this->addConfigVariable($moduleName, array());
return array();
}
return $config;
}
public function setModuleConfig($class, array $config) {
$rc = new \ReflectionClass(get_class($class));
$fullPath = dirname($rc->getFileName());
$modulePath = substr($fullPath, strpos($fullPath, "modules/") + 8);
$moduleName = substr($modulePath, 0, strpos($modulePath, "/")) . "_module";
$oldConfig = $this->getConfig($moduleName);
if ($oldConfig === false) {
$this->addConfigVariable($moduleName, $config);
return true;
}
return $this->setConfig($moduleName, $config);
}
public function getNotificationEmails() {
$string = $this->getConfig(\app\constants\Config::NOTIFICATION_EMAILS);
$explode = explode(",", $string);
$emails = array();
foreach ($explode as $email) {
if (filter_var(trim($email), FILTER_VALIDATE_EMAIL)) {
array_push($emails, trim($email));
}
}
return $emails;
}
public function sendNotifcationGCM($registrationId, $title, $message, $action, array $data = array(), $ttl = 86400) {
if (strlen($registrationId) > 20) {
$gcmClient = new Coreproc\Gcm\GcmClient(GOOGLE_SERVER_KEY);
$gcmMessage = new Coreproc\Gcm\Classes\Message($gcmClient);
$gcmMessage->addRegistrationId($registrationId);
$gcmMessage->setTimeToLive($ttl);
$messageData = array(
"title" => $title,
"body" => $message,
"sound" => "default",
"action" => $action,
"data" => $data
);
$gcmMessage->setData($messageData);
try {
$response = $gcmMessage->send();
\app\classes\Logger::log(\app\classes\Logger::INFO, "App Notification", "Response: " . print_r($response, true));
} catch (\Exception $exception) {
\app\classes\Logger::log(\app\classes\Logger::ERROR, "App Notification", "Response: " . print_r($exception->getMessage(), true));
}
}
}
}