| Current Path : /home/j/u/v/juvelize/martine/components/com_easybookreloaded/controllers/ |
| Current File : /home/j/u/v/juvelize/martine/components/com_easybookreloaded/controllers/entry.php |
<?php
/**
* @copyright
* @package Easybook Reloaded - EBR for Joomla! 3.x
* @author Viktor Vogel <admin@kubik-rubik.de>
* @version 3.4.1.1-FREE - 2021-08-29
* @link https://kubik-rubik.de/ebr-easybook-reloaded
*
* @license GNU/GPL
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
defined('_JEXEC') || die('Restricted access');
use EasybookReloaded\{Helper, Content, Route as EasybookReloadedRoute};
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\{Factory, Language\Text, Router\Route, Uri\Uri, Session\Session, Plugin\PluginHelper, Cache\Cache};
use Joomla\Input\Input;
/**
* Class EasybookReloadedControllerEntry
*
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
class EasybookReloadedControllerEntry extends BaseController
{
/**
* @var object $input
* @since 3.4.0-FREE
*/
protected $input;
/**
* EasybookReloadedControllerEntry constructor.
*
* @since 3.4.0-FREE
*/
public function __construct()
{
parent::__construct();
$this->input = new Input();
}
/**
* Processes adding requests
*
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function add(): void
{
$this->addEdit();
}
/**
* Processes adding and editing requests
*
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
private function addEdit(): void
{
$id = $this->input->getInt('cid', 0);
if ((($id === 0 && EASYBOOK_CANADD) || ($id !== 0 && EASYBOOK_CANEDIT)) && !Helper::getParams('offline')) {
$this->input->set('view', 'entry');
$this->input->set('layout', 'form');
$this->display();
return;
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . EasybookReloadedRoute::getGbId(), false);
$this->setRedirect($link, Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS'), 'message');
}
/**
* Processes editing requests
*
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function edit(): void
{
$this->addEdit();
}
/**
* Saves the entry and inform the administrator(s) or show an error message to entry creator
*
* @throws Exception
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function save(): void
{
Session::checkToken() || jexit('Invalid Token');
$this->cleanCache();
$id = $this->input->getInt('id', 0);
$gbId = EasybookReloadedRoute::getGbId();
if ((($id === 0 && EASYBOOK_CANADD) || ($id !== 0 && EASYBOOK_CANEDIT)) && !Helper::getParams('offline')) {
/** @var EasybookReloadedModelEntry $model */
$model = $this->getModel('entry');
// Store the entered data, create an output message and send the notification mail
if ($row = $model->store()) {
$message = Text::_('COM_EASYBOOKRELOADED_ENTRY_SAVED_BUT_HAS_TO_BE_APPROVED');
$type = 'notice';
if (Helper::getParams('defaultPublished', true)) {
$message = Text::_('COM_EASYBOOKRELOADED_ENTRY_SAVED');
$type = 'success';
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false);
// Send mail if it is a new entry and the send mail option is activated
if ($id === 0 && Helper::getParams('sendMail', true)) {
$this->sendMailNotification($row, $gbId);
}
$this->setRedirect($link, $message, $type);
return;
}
$message = Text::sprintf('COM_EASYBOOKRELOADED_PLEASE_VALIDATE_YOUR_INPUTS', $this->getInputErrors());
$link = Route::_('index.php?option=com_easybookreloaded&controller=entry&task=add&retry=true', false);
$type = 'error';
Factory::getSession()->clear('errors', 'easybookreloaded');
$this->setRedirect($link, $message, $type);
return;
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false);
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS');
$type = 'message';
$this->setRedirect($link, $message, $type);
}
/**
* Cleans the cached pages of the component by the system cache plugin
*
* @throws Exception
* @deprecated Used due to B/C reasons for older Joomla! versions
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
private function cleanCache(): void
{
// Clean page cache if System Cache plugin is enabled
if (PluginHelper::isEnabled('system', 'cache')) {
$gbId = EasybookReloadedRoute::getGbId();
$cacheUrls = [
'index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId,
'index.php?option=com_easybookreloaded&controller=entry&task=add',
'index.php?option=com_easybookreloaded&controller=entry&task=add&retry=true',
];
foreach ($cacheUrls as $cacheUrl) {
$this->cleanCacheProcess($cacheUrl);
}
}
}
/**
* Executes the cache cleaning process
*
* @param string $cacheUrl
*
* @return void
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
private function cleanCacheProcess(string $cacheUrl): void
{
$uri = Uri::getInstance();
$uri->setPath(Route::_($cacheUrl, false));
$cacheId = $uri->toString(['host', 'port', 'scheme', 'path', 'query', 'fragment']);
$cacheId = md5(serialize([$cacheId]));
Cache::getInstance('page')->remove($cacheId, 'page');
}
/**
* Sends the notification mail
*
* @param object $row
* @param int $gbId
*
* @throws Exception
* @since 3.4.1.0-FREE
*/
private function sendMailNotification(object $row, int $gbId): void
{
// Reference needed objects and prepare the requested variables for the mail
$mail = Factory::getMailer();
$uri = Uri::getInstance();
$db = Factory::getDbo();
// Load all request variables
$dataTemp = $this->input->request->getArray();
array_walk(
$dataTemp,
static function (&$dataTemp) {
$dataTemp = htmlspecialchars(strip_tags(trim($dataTemp)));
}
);
// Get unfiltered request variable for gbtext
$dataTemp['gbtext'] = htmlspecialchars($this->input->getRaw('gbtext'), ENT_QUOTES);
$name = $dataTemp['gbname'];
$text = $dataTemp['gbtext'];
$ip = '0.0.0.0';
if (Helper::getParams('enableLog', true)) {
$ip = Content::getIpAddress();
}
$title = '';
if (!empty($dataTemp['gbtitle'])) {
$title = $dataTemp['gbtitle'];
}
// Get config object for the secret word, sitename and email settings
$config = Factory::getConfig();
$hashId = $this->createHashId($row, $config->get('secret'));
$href = $uri::base() . EasybookReloadedRoute::getRoute($row->get('id'), $gbId);
$hashmailPublish = $uri::base() . EasybookReloadedRoute::getRouteHash('publishMail', $gbId) . $hashId;
$hashmailComment = $uri::base() . EasybookReloadedRoute::getRouteHash('commentMail', $gbId) . $hashId;
$hashmailEdit = $uri::base() . EasybookReloadedRoute::getRouteHash('editMail', $gbId) . $hashId;
$hashmailDelete = $uri::base() . EasybookReloadedRoute::getRouteHash('removeMail', $gbId) . $hashId;
// Mail subject - get the name of the website and add it to the subject
$siteName = $config->get('sitename');
$mail->setSubject(Text::sprintf('COM_EASYBOOKRELOADED_NEW_GUESTBOOKENTRY', $siteName));
$mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_A_NEW_GUESTBOOKENTRY_HAS_BEEN_WRITTEN', $uri::base(), $name, $title, $text, $href, $hashmailPublish, $hashmailComment, $hashmailEdit, $hashmailDelete, $ip));
if (Helper::getParams('sendMailHtml')) {
$mail->isHtml(true);
$mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_A_NEW_GUESTBOOKENTRY_HAS_BEEN_WRITTEN_HTML', $uri::base(), $name, $title, $text, $href, $hashmailPublish, $hashmailComment, $hashmailEdit, $hashmailDelete, $ip));
}
// Set recipient and reply to addresses
$replyTo = $row->get('gbmail');
if (empty($replyTo)) {
$replyTo = $config->get('mailfrom');
}
$mail->addRecipient($this->getRecipients());
$mail->addReplyTo($replyTo, $row->get('gbname'));
$mail->setSender([$config->get('mailfrom'), $config->get('fromname')]);
// Which mail type should be used? Default is PHP mail
if ($config->get('mailer') === 'sendmail') {
$mail->useSendmail($config->get('sendmail'));
} elseif ($config->get('mailer') === 'smtp') {
$mail->useSmtp($config->get('smtpauth'), $config->get('smtphost'), $config->get('smtpuser'), $config->get('smtppass'), $config->get('smtpsecure'), $config->get('smtpport'));
}
// Send the mail
$mail->Send();
}
/**
* @param object $row
* @param string $secret
*
* @return string
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
private function createHashId(object $row, string $secret): string
{
$hash = [];
$hash['id'] = (int)$row->get('id');
$hash['gbmail'] = md5($row->get('gbmail'));
$hash['username'] = $row->get('gbname');
// Get the custom secret word. If no word was set, take the Joomla! secret word from the configuration
$hash['customSecret'] = $secret;
$secretWord = Helper::getParams('secretWord');
if (!empty($secretWord)) {
$hash['customSecret'] = Helper::getParams('secretWord');
}
$hash = substr(base64_encode(md5(serialize($hash))), 0, 16);
return $row->get('id') . '-' . $hash;
}
/**
* Gets all email notification recipients
*
* @return array
* @since 3.4.0-FREE
*/
private function getRecipients(): array
{
$admins = [];
$db = Factory::getDbo();
$emailfornotificationUsergroupArray = Helper::getParams('emailForNotificationUserGroup', [8]);
foreach ($emailfornotificationUsergroupArray as $emailfornotificationUsergroup) {
$query = "SELECT " . $db->quoteName('email') . " FROM " . $db->quoteName('#__users') . " AS A, " . $db->quoteName('#__user_usergroup_map') . " AS B WHERE " . $db->quoteName('B.group_id') . " = " . $db->quote($emailfornotificationUsergroup) . " AND " . $db->quoteName('B.user_id') . " = " . $db->quoteName('A.id') . " AND " . $db->quoteName('A.sendEmail') . " = 1";
$db->setQuery($query);
$result = $db->loadRowList();
if (!empty($result)) {
foreach ($result as $value) {
$admins[] = $value[0];
}
}
}
if (Helper::getParams('emailForNotification')) {
$emailfornotification = array_map('trim', explode(',', Helper::getParams('emailForNotification')));
foreach ($emailfornotification as $email) {
$admins[] = $email;
}
}
return $admins;
}
/**
* Gets input errors
*
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
private function getInputErrors(): string
{
$errorsOutput = [];
$errorsArray = array_keys(Factory::getSession()->get('errors', null, 'easybookreloaded'));
if ((in_array('easycalccheck', $errorsArray, true)) || (in_array('spamCheckResult', $errorsArray, true))) {
if (in_array('spamCheckResult', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_EASYCALCCHECK_TIME');
} else {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_EASYCALCCHECK');
}
} elseif (in_array('akismet', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_AKISMET');
} elseif (in_array('gbid', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_GBID');
} elseif (in_array('easycalccheckQuestion', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_SPAMCHECKQUESTION');
} else {
if (in_array('name', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_NAME');
}
if (in_array('mail', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_MAIL');
}
if (in_array('title', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_TITLE');
}
if (in_array('text', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_TEXT');
}
if (in_array('icq', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_ICQ');
}
if (in_array('skype', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_SKYPE');
}
if (in_array('tooManyLinks', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_TOOMANYLINKS');
}
if (in_array('iptimelock', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_TIMELOCK');
}
if (in_array('gbimage', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_IMAGE');
}
if (in_array('eugdpr', $errorsArray, true)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_ERROR_EUGDPR');
}
if (empty($errorsOutput)) {
$errorsOutput[] = Text::_('COM_EASYBOOKRELOADED_UNKNOWNERROR');
}
}
return implode(', ', $errorsOutput);
}
/**
* Calls the comment form if user has the correct permission rights
*
* @throws Exception
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function comment(): void
{
if (EASYBOOK_CANEDIT) {
$this->input->set('view', 'entry');
$this->input->set('layout', 'commentform');
$this->input->set('hidemainmenu', 1);
$this->display();
return;
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . EasybookReloadedRoute::getGbId(), false);
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS');
$type = 'message';
$this->setRedirect($link, $message, $type);
}
/**
* Removes an entry from the database
*
* @throws Exception
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function remove(): void
{
$this->cleanCache();
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS');
$type = 'message';
if (EASYBOOK_CANEDIT) {
/** @var EasybookReloadedModelEntry $model */
$model = $this->getModel('entry');
$message = Text::_('COM_EASYBOOKRELOADED_ENTRY_DELETED');
$type = 'success';
if (!$model->delete()) {
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_ENTRY_COULD_NOT_BE_DELETED');
$type = 'error';
}
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . EasybookReloadedRoute::getGbId(), false);
$this->setRedirect($link, $message, $type);
}
/**
* Changes the status of the entry - online / offline
*
* @throws Exception
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function publish(): void
{
$this->cleanCache();
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS');
$type = 'message';
if (EASYBOOK_CANEDIT) {
/** @var EasybookReloadedModelEntry $model */
$model = $this->getModel('entry');
switch ($model->publish()) {
case -1:
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_CHANGE_PUBLISH_STATUS');
$type = 'error';
break;
case 0:
$message = Text::_('COM_EASYBOOKRELOADED_ENTRY_UNPUBLISHED');
$type = 'success';
break;
case 1:
$message = Text::_('COM_EASYBOOKRELOADED_ENTRY_PUBLISHED');
$type = 'success';
break;
}
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . EasybookReloadedRoute::getGbId(), false);
$this->setRedirect($link, $message, $type);
}
/**
* Saves the comment of the administrator and inform the entry creator
*
* @throws Exception
* @since 3.4.0-FREE
* @version 3.4.1.0-FREE
*/
public function saveComment(): void
{
$this->cleanCache();
$gbId = EasybookReloadedRoute::getGbId();
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_RIGHTS');
$type = 'message';
if (EASYBOOK_CANEDIT) {
Session::checkToken() || jexit('Invalid Token');
/** @var EasybookReloadedModelEntry $model */
$model = $this->getModel('entry');
$id = $model->saveComment();
if ($id === 0) {
$message = Text::_('COM_EASYBOOKRELOADED_ERROR_COULD_NOT_SAVE_COMMENT');
$type = 'error';
} else {
$message = Text::_('COM_EASYBOOKRELOADED_COMMENT_SAVED');
$informCreator = Factory::getApplication()->input->getBool('inform', false);
if ($informCreator) {
$data = $model->getRow($id);
$this->sendMailComment($data, $gbId);
$message = Text::_('COM_EASYBOOKRELOADED_COMMENT_SAVED_INFORM');
}
$type = 'success';
}
}
$link = Route::_('index.php?option=com_easybookreloaded&view=easybookreloaded&gbid=' . $gbId, false);
$this->setRedirect($link, $message, $type);
}
/**
* Sends the comment mail
*
* @param object $data
* @param int $gbId
*
* @throws Exception
* @since 3.4.1.0-FREE
*/
private function sendMailComment(object $data, int $gbId): void
{
$uri = Uri::getInstance();
$mail = Factory::getMailer();
$href = $uri::base() . EasybookReloadedRoute::getRoute($data->get('id'), $gbId);
$mail->setSubject(Text::_('COM_EASYBOOKRELOADED_ADMIN_COMMENT_SUBJECT'));
$mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_ADMIN_COMMENT_BODY', $data->get('gbname'), $uri::base(), $href));
if (Helper::getParams('sendMailHtml')) {
$mail->isHtml(true);
$mail->setBody(Text::sprintf('COM_EASYBOOKRELOADED_ADMIN_COMMENT_BODY_HTML', $data->get('gbname'), $uri::base(), $href));
}
$config = Factory::getConfig();
$mail->addRecipient($data->get('gbmail'));
$mail->setSender([$config->get('mailfrom'), $config->get('fromname')]);
// Which mail type should be used? Default is PHP mail
if ($config->get('mailer') === 'sendmail') {
$mail->useSendmail($config->get('sendmail'));
} elseif ($config->get('mailer') === 'smtp') {
$mail->useSmtp($config->get('smtpauth'), $config->get('smtphost'), $config->get('smtpuser'), $config->get('smtppass'), $config->get('smtpsecure'), $config->get('smtpport'));
}
$mail->Send();
}
}