%PDF- %PDF-
| Direktori : /home1/lightco1/www/components/com_baforms/models/ |
| Current File : //home1/lightco1/www/components/com_baforms/models/form.php |
<?php
/**
* @package BaForms
* @author Balbooa http://www.balbooa.com/
* @copyright Copyright @ Balbooa
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
class baformsModelForm extends JModelAdmin
{
public function getForm($data = array(), $loadData = true)
{
}
public function saveUpload($fileName, $maxSize, $types, $id)
{
$types = explode(',', $types);
$maxSize = 1048576 * $maxSize;
$type = explode('.', $_FILES[$fileName]['name']);
$dir = JPATH_BASE . '/images/baforms';
if (!file_exists($dir) || !is_dir($dir)) {
mkdir($dir);
}
if (!in_array(trim($type[count($type)-1]), array('php', 'phps', 'php3', 'php4', 'phtml', 'pl',
'py', 'jsp', 'asp', 'htm', 'shtml', 'sh',
'cgi', 'htaccess', 'exe', 'dll'))) {
foreach ($types as $allow) {
if (trim($allow) == trim($type[count($type)-1])) {
if($_FILES[$fileName]['size'] < $maxSize) {
$newFile = rand(666666, 666666666666). '_' .$_FILES[$fileName]['name'];
if (!file_exists($dir.'/form_'.$id) || !is_dir($dir.'/form_'.$id)) {
mkdir($dir.'/form_'.$id);
}
$newFile = 'form_' .$id. '/' .$newFile;
$file = $dir ."/".$newFile;
if (move_uploaded_file($_FILES[$fileName]['tmp_name'], $file)) {
return $newFile;
}
}
}
}
} else {
return false;
}
}
public function sendEmail($title, $msg, $id, $email)
{
$options = $this->getEmailOptions($id);
$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
$sender = array($config->get('mailfrom'), $config->get('fromname') );
if ($options[0]->add_sender_email*1 === 1) {
$sender[0] = $email;
$sender[1] = '';
}
$recipient = $options[0]->email_recipient;
$recipient = explode(',', $recipient);
$message = '';
$msg = explode('_-_', $msg);
$files = array();
foreach ($msg as $mess) {
if ($mess != '') {
$mess = explode('|-_-|', $mess);
if ($mess[2] != 'upload') {
$message .= $mess[0]. ': ' . $mess[1]. '<br>';
} else if ($mes[1] != '') {
array_push($files, JUri::root(true) . '/images/baforms/' .$mess[1]);
}
}
}
if (!empty($recipient)) {
$subject = $options[0]->email_subject;
if (!empty($files)) {
$mailer->addAttachment($files);
}
$mailer->isHTML(true);
$body = $options[0]->email_body. ' ' .$title. '<br>' .$message. '';
$mailer->setSender($sender);
$mailer->setSubject($subject);
$mailer->addRecipient($recipient);
$mailer->setBody($body);
$mailer->Send();
}
if (!empty($options[0]->sender_email)) {
$mailer = JFactory::getMailer();
$mailer->isHTML(true);
$sender = array($options[0]->sender_email, $options[0]->sender_name);
$mailer->setSender($sender);
$subject = $options[0]->reply_subject;
$mailer->setSubject($subject);
$mailer->addRecipient($email);
$body = $options[0]->reply_body;
if ($options[0]->copy_submitted_data*1 === 1) {
$body .= '<br>' .$message. '';
if (!empty($files)) {
$mailer->addAttachment($files);
}
}
$mailer->setBody($body);
$mailer->Send();
}
}
public function getEmailOptions($id)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('email_recipient, email_subject, email_body, sender_name,
sender_email, reply_subject, reply_body, add_sender_email,
copy_submitted_data');
$query->from('#__baforms_forms');
$query->where('id='.$id);
$db->setQuery($query);
$items = $db->loadObjectList();
return $items;
}
public function checkItems($item, $type, $place)
{
if ($item != '') {
return $item;
} else {
if ($type == 'textarea') {
if ($place != '') {
return $place;
} else {
return 'Textarea';
}
}
if ($type == 'textInput') {
if ($place != '') {
return $place;
} else {
return 'TextInput';
}
}
if ($type == 'chekInline') {
return 'ChekInline';
}
if ($type == 'checkMultiple') {
return 'CheckMultiple';
}
if ($type == 'radioInline') {
return 'RadioInline';
}
if ($type == 'radioMultiple') {
return 'RadioMultiple';
}
if ($type == 'dropdown') {
return 'Dropdown';
}
if ($type == 'selectMultiple') {
return 'SelectMultiple';
}
if ($type == 'date') {
return 'Date';
}
if ($type == 'slider') {
return 'Slider';
}
if ($type == 'email') {
if ($place != '') {
return $place;
} else {
return 'Email';
}
}
}
}
public function save($data)
{
$id = $data['form_id'];
$flag = true;
$email = '';
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select("title, alow_captcha, sent_massage, error_massage");
$query->from("#__baforms_forms");
$query->where("id=" . $id);
$db->setQuery($query);
$form = $db->loadAssoc();
$title = $form['title'];
$capt = $form['alow_captcha'];
$succes = $form['sent_massage'];
$error = $form['error_massage'];
$submissionData = '';
$query = $db->getQuery(true);
$query->select('settings, id')
->from('#__baforms_items')
->where('form_id='. $id);
$db->setQuery($query);
$items = $db->loadObjectList();
$captName = array();
if ($capt != '0') {
$captcha = JCaptcha::getInstance($capt, array('namespace' => 'anything'));
if (isset($data[$capt])) {
$answer = $captcha->checkAnswer($data[$capt]);
if ($answer) {
$flag = true;
} else {
$flag = false;
}
} else {
foreach ($data as $key=> $dat) {
if ($key != 'task' && $key != 'form_id') {
array_push($captName, $key);
}
}
foreach ($items as $key=> $item) {
$item = $item->id;
for ($i = 0; $i < count($captName); $i++) {
if ($item == $captName[$i]) {
unset($captName[$i]);
sort($captName);
}
}
}
if (isset($captName[0])) {
$answer = $captcha->checkAnswer($data[$captName[0]]);
} else {
$answer = $captcha->checkAnswer('anything');
}
if ($answer) {
$flag = true;
} else {
$flag = false;
}
}
}
if ($flag) {
foreach ($items as $item) {
if ($flag) {
$itm = explode('_-_', $item->settings);
if ($itm[0] != 'button') {
$type = trim($itm[2]);
$itm = explode(';', $itm[3]);
if ($type == 'textarea' || $type == 'textInput' || $type == 'chekInline' ||
$type == 'checkMultiple' || $type == 'radioInline' || $type == 'radioMultiple' ||
$type == 'dropdown' || $type == 'selectMultiple') {
$required = $itm[3];
$itm = trim($this->checkItems($itm[0], $type, $itm[2]));
$name = $itm;
$itm = str_replace(' ', '_', $itm);
if ($required == 1) {
if (!empty($data[$item->id])) {
$flag = true;
} else {
$flag = false;
}
} else {
$flag = true;
}
} else if ($type == 'email') {
$itm = trim($this->checkItems($itm[0], $type, $itm[2]));
$name = $itm;
$itm = str_replace(' ', '_', $itm);
if(!empty($data[$item->id]) && preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/", $data[$item->id])) {
$email = $data[$item->id];
$flag = true;
} else {
$flag = false;
}
} else {
$itm = trim($this->checkItems($itm[0], $type, ''));
$name = $itm;
$itm = str_replace(' ', '_', $itm);
}
if ($flag) {
foreach ($data as $key => $elem) {
if ($key != "form_id" && $key != "task") {
if ($item->id == $key) {
if (is_array($elem)) {
$message = '';
foreach ($elem as $element) {
$message .= strip_tags($element). ';';
}
$submissionData .= $name. '|-_-|' .$message. '|-_-|'.$type. '_-_';
} else {
$submissionData .= $name. '|-_-|' .strip_tags($elem). '|-_-|'.$type. '_-_';
}
}
}
}
if (!array_key_exists($item->id, $data) &&
$type != 'map' && $type != 'htmltext' && $type != 'upload') {
$submissionData .= $name. '|-_-||-_-|'.$type. '_-_';
}
}
}
}
}
if ($flag) {
if (!empty($_FILES)) {
foreach ($_FILES as $key => $file) {
if ($file['error'] === 0 && $flag) {
foreach ($items as $item) {
if ($key == $item->id) {
$options = $item->settings;
$options = explode('_-_', $options);
$type = trim($options[2]);
$options = explode(';', $options[3]);
$link = $this->saveUpload($key, $options[2], $options[3], $id);
if ($link) {
$key = str_replace('_', ' ', $key);
$submissionData .= $options[0]. '|-_-|' .$link. '|-_-|' .$type. '_-_';
} else {
$flag = false;
}
break;
}
}
} else if ($file['error'] === 4) {
foreach ($items as $item) {
if ($key == $item->id) {
$options = $item->settings;
$options = explode('_-_', $options);
$type = trim($options[2]);
$options = explode(';', $options[3]);
$submissionData .= $options[0]. '|-_-||-_-|' .$type. '_-_';
break;
}
}
}
}
}
}
if ($flag) {
$columns = array('title, mesage, date_time');
$date = date('Y-m-d');
$values = array($db->quote($title), $db->quote($submissionData), $db->quote($date));
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->insert('#__baforms_submissions');
$query->columns($columns);
$query->values(implode(',', $values));
$db->setQuery($query);
$db->execute();
$this->sendEmail($title, $submissionData, $id, $email);
echo '<input id="form-sys-mesage" type="hidden" value="' .htmlspecialchars($succes, ENT_QUOTES). '">';
} else {
echo '<input id="form-sys-mesage" type="hidden" value="' .htmlspecialchars($error, ENT_QUOTES). '">';
}
} else {
echo '<input id="form-sys-mesage" type="hidden" value="' .htmlspecialchars($error, ENT_QUOTES). '">';
}
?>
<script language="JavaScript">
var intervalId = setInterval(sec,12);
function sec()
{
var msg = document.getElementById("form-sys-mesage").value;
if (msg) {
clearInterval(intervalId);
window.parent.postMessage(msg, "*");
}
}
</script>
<?php
exit;
}
}