Skip to content

Commit

Permalink
Add Raw HTML widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Nov 14, 2024
1 parent 8520cd1 commit 61a300f
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 115 deletions.
232 changes: 118 additions & 114 deletions framework/elements/formbuilder/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,142 +22,146 @@
$mainframe = Factory::getApplication();
$asformbuilder = $mainframe->input->get('as-form-builder-', array(), 'RAW');
$unqid = $mainframe->input->get('form_id', '', 'ALNUM');
$element = Helper::getElement($unqid, $template);
if (!empty($asformbuilder) && !empty($element)) {
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$return = array();
try {
$params = $element['params'];
$mail = Factory::getContainer()->get(MailerFactoryInterface::class)->createMailer();;
$message = $params->get('email_body', '');
$email_headers = $params->get('email_headers', '');
$gcaptcha = '';

foreach ($asformbuilder as $field => $value) {
$message = str_replace('{{'.$field.'}}', $value, $message);
$email_headers = str_replace('{{'.$field.'}}', $value, $email_headers);
if ($field == 'g-recaptcha-response') {
$gcaptcha = $value;
}
$element = Helper::getElement($unqid);
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$return = array();
try {
if (!empty($asformbuilder)) {
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_FORM_DATA'));
}
if (!empty($element)) {
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_FORM_ID'));
}
$params = $element['params'];
$mail = Factory::getContainer()->get(MailerFactoryInterface::class)->createMailer();
$message = $params->get('email_body', '');
$email_headers = $params->get('email_headers', '');
$gcaptcha = '';

foreach ($asformbuilder as $field => $value) {
$message = str_replace('{{'.$field.'}}', $value, $message);
$email_headers = str_replace('{{'.$field.'}}', $value, $email_headers);
if ($field == 'g-recaptcha-response') {
$gcaptcha = $value;
}
}

$replyToMail = $replyToName = '';
$replyToMail = $replyToName = '';

if (intval($params->get('enable_captcha', 0))) {
$captcha_type = $params->get('captcha_type', 'default');
if ($captcha_type == 'recaptcha' || $captcha_type == 'recaptcha_invisible') {
if($gcaptcha == ''){
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_CAPTCHA'));
if (intval($params->get('enable_captcha', 0))) {
$captcha_type = $params->get('captcha_type', 'default');
if ($captcha_type == 'recaptcha' || $captcha_type == 'recaptcha_invisible') {
if($gcaptcha == ''){
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_CAPTCHA'));
} else {
if($captcha_type == 'recaptcha_invisible') {
PluginHelper::importPlugin('captcha', 'recaptcha_invisible');
} else {
if($captcha_type == 'recaptcha_invisible') {
PluginHelper::importPlugin('captcha', 'recaptcha_invisible');
} else {
PluginHelper::importPlugin('captcha', 'recaptcha');
}
$dispatcher = Factory::getApplication()->getDispatcher();
$event = new Joomla\Event\Event('onCheckAnswer', [$gcaptcha]);
$res = $dispatcher->dispatch('onCheckAnswer', $event);

if (!$res[0]) {
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_CAPTCHA'));
}
PluginHelper::importPlugin('captcha', 'recaptcha');
}
} else {
if (!Helper::getCaptcha('as-formbuilder-captcha')) {
$dispatcher = Factory::getApplication()->getDispatcher();
$event = new Joomla\Event\Event('onCheckAnswer', [$gcaptcha]);
$res = $dispatcher->dispatch('onCheckAnswer', $event);

if (!$res[0]) {
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_CAPTCHA'));
}
}
} else {
if (!Helper::getCaptcha('as-formbuilder-captcha')) {
throw new \Exception(Text::_('ASTROID_AJAX_ERROR_INVALID_CAPTCHA'));
}
}
}

//get sender UP
$senderip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
// Subject Structure
$site_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$mail_subject = $params->get('email_subject', '') . ' - ' . $site_name;
$mail_subject = preg_replace_callback('/\{\{(\S+?)\}\}/siU', function ($matches) use (&$asformbuilder, &$site_name) {
if (isset($asformbuilder[$matches[1]])) {
return $asformbuilder[$matches[1]];
} elseif ($matches[1] == 'site-name') {
return $site_name;
}
return $matches[0];
}, $mail_subject);
// Message structure
$mail_body = $message;
$mail_body .= '<p><strong>' . Text::_('ASTROID_FORMBUILDER_SENDER_IP'). '</strong>: ' . $senderip .'</p>';
//get sender UP
$senderip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
// Subject Structure
$site_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$mail_subject = $params->get('email_subject', '') . ' - ' . $site_name;
$mail_subject = preg_replace_callback('/\{\{(\S+?)\}\}/siU', function ($matches) use (&$asformbuilder, &$site_name) {
if (isset($asformbuilder[$matches[1]])) {
return $asformbuilder[$matches[1]];
} elseif ($matches[1] == 'site-name') {
return $site_name;
}
return $matches[0];
}, $mail_subject);
// Message structure
$mail_body = $message;
$mail_body .= '<p><strong>' . Text::_('ASTROID_FORMBUILDER_SENDER_IP'). '</strong>: ' . $senderip .'</p>';

$config = Factory::getConfig();
$config = Factory::getConfig();

$sender = array( $config->get( 'mailfrom' ), $config->get( 'fromname' ) );
$recipient = $config->get( 'mailfrom' );
$sender = array( $config->get( 'mailfrom' ), $config->get( 'fromname' ) );
$recipient = $config->get( 'mailfrom' );

// $sender = array( $email, $name );
// $sender = array( $email, $name );

$from_email = $params->get('from_email', '');
if (!empty($from_email)) {
$sender = array($from_email, $from_email);
$mail->addReplyTo($from_email, $from_email);
}
$from_email = $params->get('from_email', '');
if (!empty($from_email)) {
$sender = array($from_email, $from_email);
$mail->addReplyTo($from_email, $from_email);
}

$recipient = $params->get('recipient', '');
$recipient = $params->get('recipient', '');


if (!empty($email_headers)) {
$additional_header_ajax = explode("\n", $email_headers);
foreach ($additional_header_ajax as $_header)
if (!empty($email_headers)) {
$additional_header_ajax = explode("\n", $email_headers);
foreach ($additional_header_ajax as $_header)
{
$_header = explode(':', $_header);
if (count($_header) > 0)
{
$_header = explode(':', $_header);
if (count($_header) > 0)
if (strtolower($_header[0]) == 'reply-to')
{
if (strtolower($_header[0]) == 'reply-to')
{
$replyToMail = isset($_header[1]) ? trim($_header[1]) : '';
}
if (strtolower($_header[0]) == 'reply-name')
{
$replyToName = isset($_header[1]) ? trim($_header[1]) : '';
}
if (strtolower($_header[0]) == 'cc' && isset($_header[1]))
{
$mail->addCc(trim($_header[1]));
}
if (strtolower($_header[0]) == 'bcc' && isset($_header[1]))
{
$mail->addCc(trim($_header[1]));
}
$replyToMail = isset($_header[1]) ? trim($_header[1]) : '';
}
}
if (!empty($replyToMail)) {
if (!empty($replyToName)) {
$mail->addReplyTo($replyToMail, $replyToName);
} else {
$mail->addReplyTo($replyToMail);
if (strtolower($_header[0]) == 'reply-name')
{
$replyToName = isset($_header[1]) ? trim($_header[1]) : '';
}
if (strtolower($_header[0]) == 'cc' && isset($_header[1]))
{
$mail->addCc(trim($_header[1]));
}
if (strtolower($_header[0]) == 'bcc' && isset($_header[1]))
{
$mail->addCc(trim($_header[1]));
}
}
}

$mail->setSender($sender);
$mail->addRecipient($recipient);
$mail->setSubject($mail_subject);
$mail->isHTML(true);
$mail->Encoding = 'base64';
$mail->setBody($mail_body);

$message_success = $params->get('success_message', Text::_('ASTROID_FORMBUILDER_SENT_SUCCESSFULLY'));
$message_failed = $params->get('failed_message', Text::_('ASTROID_FORMBUILDER_SENT_MAIL_FAILED'));

if ($mail->Send()) {
$return["status"] = 'success';
$return["message"] = $message_success;
$return["code"] = 200;
} else {
throw new \Exception($message_failed);
if (!empty($replyToMail)) {
if (!empty($replyToName)) {
$mail->addReplyTo($replyToMail, $replyToName);
} else {
$mail->addReplyTo($replyToMail);
}
}
} catch (\Exception $e) {
$return["status"] = "error";
$return["code"] = $e->getCode();
$return["message"] = $e->getMessage();
}
echo \json_encode($return);
}

$mail->setSender($sender);
$mail->addRecipient($recipient);
$mail->setSubject($mail_subject);
$mail->isHTML(true);
$mail->Encoding = 'base64';
$mail->setBody($mail_body);

$message_success = $params->get('success_message', Text::_('ASTROID_FORMBUILDER_SENT_SUCCESSFULLY'));
$message_failed = $params->get('failed_message', Text::_('ASTROID_FORMBUILDER_SENT_MAIL_FAILED'));

if ($mail->Send()) {
$return["status"] = 'success';
$return["message"] = $message_success;
$return["code"] = 200;
} else {
throw new \Exception($message_failed);
}
} catch (\Exception $e) {
$return["status"] = "error";
$return["code"] = $e->getCode();
$return["message"] = $e->getMessage();
}
echo \json_encode($return);
17 changes: 17 additions & 0 deletions framework/elements/rawhtml/rawhtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @package Astroid Framework
* @author Astroid Framework Team https://astroidframe.work
* @copyright Copyright (C) 2023 AstroidFrame.work.
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
* DO NOT MODIFY THIS FILE DIRECTLY AS IT WILL BE OVERWRITTEN IN THE NEXT UPDATE
* You can easily override all files under /astroid/ folder.
* Just copy the file to JROOT/templates/YOUR_ASTROID_TEMPLATE/astroid/elements/module_position/module_position.php folder to create and override
*/

// No direct access.
defined('_JEXEC') or die;

extract($displayData);
echo $params->get('custom_html', '');
23 changes: 23 additions & 0 deletions framework/elements/rawhtml/rawhtml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<element>
<title>ASTROID_WIDGET_RAWHTML_LABEL</title>
<description>ASTROID_WIDGET_RAWHTML_LABEL</description>
<icon>fa-brands fa-html5</icon>
<category>ASTROID_ELEMENT_CATEGORY_ADVANCED,ASTROID_ELEMENT_CATEGORY_UTILITY</category>
<element_type>widget</element_type>
<form>
<fields>
<fieldset name="general-settings" label="TPL_ASTROID_GENERAL_SETTINGS_LABEL" addfieldpath="/libraries/astroid/framework/fields">
<field astroidgroup="general" code="html" name="custom_html" type="astroidtextarea" label="ASTROID_WIDGET_CODEPEN_CUSTOM_HTML_LABEL" description="ASTROID_WIDGET_CODEPEN_CUSTOM_HTML_LABEL"/>
</fieldset>
<fieldset name="assignment-settings" label="TPL_ASTROID_ASSIGNMENT_SETTINGS_LABEL" addfieldpath="/libraries/astroid/framework/fields" articleData="false">
<field type="astroidlist" name="assignment_type" label="ASTROID_WIDGET_ASSIGN" default="1">
<option value="1">ASTROID_WIDGET_ON_ALL_PAGES</option>
<option value="0">ASTROID_WIDGET_NO_PAGES</option>
<option value="2">ASTROID_WIDGET_SELECTED_PAGES</option>
</field>
<field ngShow="[assignment_type]==2" type="astroidassignment" label="ASTROID_ASSIGN_TO_MENU_ITEM" name="assignment"/>
</fieldset>
</fields>
</form>
</element>
7 changes: 6 additions & 1 deletion language/en-GB/en-GB.astroid.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,8 @@ ASTROID_FORMBUILDER_SENDER_IP="Sender IP"
ASTROID_FORMBUILDER_SENT_SUCCESSFULLY="Message sent successfully!"
ASTROID_FORMBUILDER_SENT_MAIL_FAILED="Message sent failed!"
ASTROID_WIDGET_FORM_SHOW_LABEL_LABEL="Show Label"
ASTROID_AJAX_ERROR_INVALID_FORM_DATA="Invalid Form Data"
ASTROID_AJAX_ERROR_INVALID_FORM_ID="Invalid Form ID"

; Accordion
ASTROID_WIDGET_ACCORDION_LABEL="Accordion"
Expand Down Expand Up @@ -1721,4 +1723,7 @@ ASTROID_WIDGET_CODEPEN_EXTERNAL_CSS_LABEL="External CSS"
ASTROID_WIDGET_CODEPEN_EXTERNAL_CSS_URL_LABEL="External CSS URL"
ASTROID_LIBRARIES="Astroid Libraries"
ASTROID_LIBRARIES_DESC="The system will automatically load Astroid libraries to support your scripts."
ASTROID_LIBRARIES_HINT="Select an Astroid Library"
ASTROID_LIBRARIES_HINT="Select an Astroid Library"

; RawHTML
ASTROID_WIDGET_RAWHTML_LABEL="HTML"

0 comments on commit 61a300f

Please sign in to comment.