Browse Source

Небольшие правки

master
M@d D3n 7 years ago
parent
commit
670c8fb970
  1. 264
      contactsnew/class.contactsnew.php
  2. 10
      contactsnew/module.php

264
contactsnew/class.contactsnew.php

@ -26,11 +26,11 @@ class ContactsNew
*/
// папка с шаблонами
var $tpl_dir;
public $tpl_dir;
// основные поля
var $fields_main = array();
var $fields_main_in_string = array();
var $fields_main_data = array(
public $fields_main = array();
public $fields_main_in_string = array();
public $fields_main_data = array(
'email' => array(
'new' => true,
'title' => 'email',
@ -67,7 +67,7 @@ class ContactsNew
)
);
// переменная для хранения формы
var $form = array();
public $form = array();
/**
* Внутренние методы класса
@ -82,24 +82,58 @@ class ContactsNew
$this->fields_main_in_string = "'" . implode("','",$this->fields_main) . "'";
}
/**
* Возвращаем JSON
*/
function _json($data, $exit = false)
{
header("Content-Type: application/json;charset=utf-8");
$json = json_encode($data);
if ($json === false)
{
$json = json_encode(array("jsonError", json_last_error_msg()));
if ($json === false)
{
$json = '{"jsonError": "unknown"}';
}
http_response_code(500);
}
echo $json;
if ($exit)
exit;
}
/**
* Метод забирает форму из бд по алиасу/id
*/
function _form ($alias_id,$_fields=true)
function _form ($alias_id, $_fields = true)
{
global $AVE_DB, $AVE_Template;
// иначе забираем из бд форму
$form = array();
// основные параметры
$form = $AVE_DB->Query("
SELECT * FROM " . PREFIX . "_module_contactsnew_forms
WHERE
" . (is_numeric($alias_id) ? 'id' : 'alias') . " = '" . $alias_id . "'
")->FetchAssocArray();
// если форма не обнаружена, выходим
if (empty($form)) return array();
if (empty($form))
return array();
$form['alias_id'] = $alias_id;
// получатели
$form['mail_set'] = unserialize($form['mail_set']);
@ -111,7 +145,9 @@ class ContactsNew
WHERE form_id = '" . $form['id'] . "'
ORDER BY id ASC
");
$form['fields'] = array();
while ($field = $sql->FetchAssocArray())
{
// раскрываем массив настроек для селектов
@ -144,6 +180,7 @@ class ContactsNew
$form['fields'][$field['id']] = $field;
}
}
// убираем слеши
$form = $this->_stripslashes($form);
@ -158,8 +195,10 @@ class ContactsNew
*/
function _stripslashes($var)
{
if (!is_array($var)) return stripslashes($var);
else return array_map(array($this,'_stripslashes'),$var);
if (! is_array($var))
return stripslashes($var);
else
return array_map(array($this, '_stripslashes'), $var);
}
/**
@ -167,8 +206,10 @@ class ContactsNew
*/
function _trim($var)
{
if (!is_array($var)) return trim($var);
else return array_map(array($this,'_trim'),$var);
if (! is_array($var))
return trim($var);
else
return array_map(array($this, '_trim'), $var);
}
/**
@ -176,28 +217,38 @@ class ContactsNew
*/
function _cleanvar($var)
{
if (!is_array($var)) return trim($var) > '' ? trim($var) : null;
if (! is_array($var))
return trim($var) > ''
? trim($var)
: null;
$narr = array();
while(list($key, $val) = each($var))
{
if (is_array($val))
{
$val = _cleanvar($val);
if (count($val) > 0) $narr[$key] = $val;
if (count($val) > 0)
$narr[$key] = $val;
}
else
{
if (trim($val) > '') $narr[$key] = $val;
if (trim($val) > '')
$narr[$key] = $val;
}
}
unset ($var);
return $narr;
}
/**
* Валидация Email-а
*/
function _email_validate ($email='')
function _email_validate ($email = '')
{
return (filter_var($email, FILTER_VALIDATE_EMAIL) === false ? false : true);
}
@ -205,15 +256,17 @@ class ContactsNew
/**
* Проверка алиаса тега на валидность и уникальность
*/
function _alias_validate ($alias='',$fid=0)
function _alias_validate ($alias = '', $fid = 0)
{
global $AVE_DB;
// соответствие требованиям
if (
empty ($alias) ||
preg_match('/^[A-Za-z0-9-_]{1,20}$/i', $alias) !== 1 ||
is_numeric($alias)
) return 'syn';
// уникальность
return !(bool)$AVE_DB->Query("
SELECT 1 FROM " . PREFIX . "_module_contactsnew_forms
@ -223,48 +276,71 @@ class ContactsNew
")->GetCell();
}
/**
* Получение списка рубрик
*/
function _rubrics ()
{
global $AVE_DB;
$rubs = array();
$sql = $AVE_DB->Query("
SELECT Id, rubric_title FROM " . PREFIX . "_rubrics
");
while ($rub = $sql->FetchAssocArray()) $rubs[$rub['Id']] = $rub['rubric_title'];
while ($rub = $sql->FetchAssocArray())
$rubs[$rub['Id']] = $rub['rubric_title'];
return $rubs;
}
/**
* Получение списка документов
*/
function _docs ($rubs_id = array())
{
global $AVE_DB;
if (empty($rubs_id)) return array();
if (empty($rubs_id))
return array();
// @fix для v1.0 beta <= 2: поддержка одной рубрики, не массива
if (!is_array($rubs_id)) $rubs_id = array(0 => $rubs_id);
if (! is_array($rubs_id))
$rubs_id = array(0 => $rubs_id);
$docs = array();
$sql = $AVE_DB->Query("
SELECT Id, document_title FROM " . PREFIX . "_documents
WHERE rubric_id IN (" . implode(',',$rubs_id) . ")
");
while ($doc = $sql->FetchAssocArray()) $docs[$doc['Id']] = $doc['document_title'];
while ($doc = $sql->FetchAssocArray())
$docs[$doc['Id']] = $doc['document_title'];
return $docs;
}
/**
* Парсинг главных тегов
*/
function _parse_tags ($str)
{
global $AVE_Core, $AVE_DB;
if (empty($_SESSION['user_login'])) $_SESSION['user_login'] = (UGROUP != 2) ? $AVE_DB->Query("SELECT user_name FROM " . PREFIX . "_users WHERE Id = '" . UID . "'")->GetCell() : '';
if (empty($_SESSION['user_login']))
$_SESSION['user_login'] = (UGROUP != 2)
? $AVE_DB->Query("SELECT user_name FROM " . PREFIX . "_users WHERE Id = '" . UID . "'")->GetCell()
: '';
$str = preg_replace_callback('/\[tag:(css|js):([^ :\/]+):?(\S+)*\]/', array($AVE_Core, '_parse_combine'), $str);
$str = parse_hide($str);
return str_replace(array(
'[tag:docid]',
'[tag:formtitle]',
@ -298,7 +374,7 @@ class ContactsNew
$_SESSION['user_email'],
htmlspecialchars(get_settings('site_name'), ENT_QUOTES),
$_SERVER['HTTP_HOST'],
),$str);
), $str);
}
/**
@ -307,8 +383,11 @@ class ContactsNew
function _eval2var($code)
{
global $AVE_DB, $AVE_Core, $AVE_Template;
ob_start();
eval($code);
return ob_get_clean();
}
@ -320,6 +399,7 @@ class ContactsNew
foreach ($this->fields_main as $field_title)
{
$count = 0;
$tpl = str_replace(array(
'[tag:fld:' . $field_title . ']',
'[tag:if_fld:' . $field_title,
@ -329,8 +409,11 @@ class ContactsNew
'[tag:if_fld:' . $this->form['fields_main'][$field_title],
'[tag:elseif_fld:' . $this->form['fields_main'][$field_title],
),$tpl,$count);
if ($save_is) $this->form['is_' . $field_title] = $count > 0 ? true : false;
if ($save_is)
$this->form['is_' . $field_title] = $count > 0 ? true : false;
}
return $tpl;
}
@ -351,7 +434,10 @@ class ContactsNew
function _parse_tag_title ($matches)
{
$field_id = $matches[1];
return !$this->form['fields'][$field_id]['main'] ? $this->form['fields'][$field_id]['title'] : $this->form['fields'][$field_id]['title_lang'];
return !$this->form['fields'][$field_id]['main']
? $this->form['fields'][$field_id]['title']
: $this->form['fields'][$field_id]['title_lang'];
}
/**
@ -368,17 +454,30 @@ class ContactsNew
function _parse_tag_fld_form ($matches)
{
$field_id = (int)$matches[1];
// забираем массив поля
$field = $this->form['fields'][$field_id];
// если поля нет, возвращаем тег обратно
if (empty($field)) return $matches[0];
if (empty($field))
return $matches[0];
// если поле выключено, возвращаем пустую строку
if (empty($field['active'])) return '';
if (empty($field['active']))
return '';
$alias_id = $this->form['alias_id'];
$fld_val = $this->form['is_submited'] ? $this->_stripslashes($_POST['form-' . $alias_id][$field_id]) :
(in_array($field['type'],array('input','textarea')) ? $this->_eval2var('?>' . $field['defaultval'] . '<?') : $field['defaultval']);
$fld_val = $this->form['is_submited']
? $this->_stripslashes($_POST['form-' . $alias_id][$field_id])
: (in_array($field['type'],array('input','textarea'))
? $this->_eval2var('?>' . $field['defaultval'] . '<?')
: $field['defaultval']);
$attributes = trim($field['attributes']);
$this->form['fields'][$field_id]['is_used'] = true;
$input = '';
$return = '';
@ -442,16 +541,21 @@ class ContactsNew
$input .= '</select>';
break;
}
// вставляем поле в шаблон поля
$return = trim($field['tpl']) > '' ? str_replace('[tag:fld]',$input,$field['tpl']) : $input;
$return = trim($field['tpl']) > ''
? str_replace('[tag:fld]',$input,$field['tpl'])
: $input;
// парсим теги названия и id
$return = str_replace(array(
'[tag:id]',
'[tag:title]',
),array(
), array(
$field['id'],
'[tag:title:' . $field_id . ']',
),$return);
), $return);
// если попытка отправить форму, обрабатываем валидацию и пустоту
if ($this->form['is_submited'])
{
@ -490,8 +594,9 @@ class ContactsNew
$this->form['ajax']['fields'][$field_id]['validate'] = false;
$this->form['ajax']['fields'][$field_id]['is_valid'] = null;
}
// пустота (для любых обязательных полей)
if (!empty($field['required']) && $field['required'])
if (! empty($field['required']) && $field['required'])
{
if ($field['type'] == 'file')
$empty = (
@ -929,31 +1034,39 @@ class ContactsNew
return $fid_new;
}
/**
* Вывод формы
*/
function form_display ($alias_id)
{
global $AVE_Template;
$form = $this->_form($alias_id);
if (empty($form)) return "[mod_contactsnew:$alias_id] - " . $AVE_Template->get_config_vars('form_notfound');
if (empty($form))
return "[mod_contactsnew:$alias_id] - " . $AVE_Template->get_config_vars('form_notfound');
// по дефолту форма валидна, но не отправлена - потом перезаписываем, если что
$this->form['is_valid'] = true;
$this->form['ajax']['form']['is_valid'] = true;
$this->form['ajax']['form']['is_sent'] = false;
$this->form['ajax']['form']['finish_tpl'] = null;
// rubheader
$GLOBALS['user_header']['module_contactsnew_' . $alias_id] = $this->_parse_tags($this->form['rubheader']);
// вывод финишной страницы, если включена проверка от повторной отправки
if (!empty($_GET['mcnfinish']) && $form['protection'])
if (! empty($_GET['mcnfinish']) && $form['protection'])
{
if ($_SESSION['mcnfinish'][$form['id'] . $_GET['mcnfinish']] === true)
{
unset($_SESSION['mcnfinish'][$form['id'] . $_GET['mcnfinish']]);
// формируем финишную страницу
$tpl = $this->_parse_tags($form['finish_tpl']);
$tpl = $this->_eval2var('?>' . $tpl . '<?');
return $tpl;
}
else
@ -962,10 +1075,11 @@ class ContactsNew
exit;
}
}
// определяем (bool) отправка формы
else $this->form['is_submited'] = false;
if (!empty($_POST['form-' . $alias_id]))
else
$this->form['is_submited'] = false;
if (! empty($_POST['form-' . $alias_id]))
{
$this->form['is_submited'] = true;
// выполняем код после отправки формы
@ -1009,14 +1123,18 @@ class ContactsNew
}
}
/**
* Отправка формы
*/
function form_submit ($alias_id)
{
global $AVE_DB, $AVE_Template;
$form = $this->form;
$fields = $form['fields'];
$fid = $form['id'];
// формируем список получателей
@ -1029,9 +1147,14 @@ class ContactsNew
)
{
$email = '';
if ($form['is_email'] === true) $email = $_POST['form-' . $alias_id][$form['fields_main']['email']];
if (empty($email)) $email = $_SESSION['user_email'];
if (!empty($email))
if ($form['is_email'] === true)
$email = $_POST['form-' . $alias_id][$form['fields_main']['email']];
if (empty($email))
$email = $_SESSION['user_email'];
if (! empty($email))
{
$recs[] = array(
'email' => $email,
@ -1039,17 +1162,24 @@ class ContactsNew
'agent' => 'user',
);
}
$history['email'] = $email;
} else // если чекбоксы - отправить копию неактивные
}
else // если чекбоксы - отправить копию неактивные
{
$email = '';
if ($form['is_email'] === true) $email = $_POST['form-' . $alias_id][$form['fields_main']['email']];
if (empty($email)) $email = $_SESSION['user_email'];
if ($form['is_email'] === true)
$email = $_POST['form-' . $alias_id][$form['fields_main']['email']];
if (empty($email))
$email = $_SESSION['user_email'];
$history['email'] = $email;
}
// главные получатели
$recs = array_merge($recs,$form['mail_set']['receivers']);
$recs = array_merge($recs, $form['mail_set']['receivers']);
// выбранные в форме получатели
if ($this->form['is_receivers'] === true)
@ -1066,10 +1196,13 @@ class ContactsNew
// перезаписываем список уникальных получателей в переменную письма
$this->form['receivers'] = array();
foreach ($recs as $rec)
{
if (!isset($this->form['receivers'][$rec['email']]) && trim($rec['email']) > '') $this->form['receivers'][trim($rec['email'])] = $rec;
if (!isset($this->form['receivers'][$rec['email']]) && trim($rec['email']) > '')
$this->form['receivers'][trim($rec['email'])] = $rec;
}
$recs = $this->form['receivers'];
$recs[] = array('agent' => 'history');
@ -1081,37 +1214,48 @@ class ContactsNew
// обрабатываем шаблон письма
$tpl = $form['mail_tpl'];
// меняем теги основных полей на стандартные
$tpl = $this->_parse_tag_fld_main($tpl);
// парсим [tag:easymail]
if (strpos($tpl,'[tag:easymail]') !== false)
{
$easy = '';
foreach ($fields as $field_id => $field)
{
if ($field['is_used'] !== true || $field['title'] == 'captcha' || empty($field['active'])) continue;
if ($field['is_used'] !== true || $field['title'] == 'captcha' || empty($field['active']))
continue;
$easy .= "[tag:title:$field_id]" . ": [tag:fld:$field_id];" . ($form['mail_set']['format'] === 'text' ? "\r\n" : '<br>');
}
// убираем последний перевод строки
$easy = ($form['mail_set']['format'] === 'text') ? rtrim($easy) : substr($easy,0,-4);
$tpl = str_replace('[tag:easymail]',$easy,$tpl);
}
// парсим теги полей и названий
$tpl = preg_replace_callback('/\[tag:fld:(\d+)]/', array($this,'_parse_tag_fld_mail'),$tpl);
$tpl = preg_replace_callback('/\[tag:title:([A-Za-z0-9-_]+)]/', array($this,'_parse_tag_title'),$tpl);
// парсим основные теги
$tpl = $this->_parse_tags($tpl);
// заменяем теги условий
$tpl = preg_replace('/\[tag:(if|elseif)_fld:(\d*)(.*?)]/si','<? $1 (\$_POST["form-' . $alias_id . '"][$2]$3): ?>',$tpl);
$tpl = str_replace(array('[tag:else_fld]','[/tag:if_fld]'),array('<? else: ?>','<? endif ?>'),$tpl);
// файлы-вложения
$attach = array();
if (!empty($_FILES['form-' . $alias_id]['tmp_name']))
if (! empty($_FILES['form-' . $alias_id]['tmp_name']))
{
foreach ($_FILES['form-' . $alias_id]['name'] as $field_id => $fname)
{
$ext = (end(explode('.', $fname)));
if (
!empty($_FILES['form-' . $alias_id]['tmp_name'][$field_id]) &&
!empty($form['fields'][$field_id]) &&
@ -1120,8 +1264,12 @@ class ContactsNew
)
{
$fname = BASE_DIR . '/' . ATTACH_DIR . '/' . str_replace(' ', '_', mb_strtolower(trim($fname)));
if (file_exists($fname)) $fname = rtrim($fname,'.' . $ext) . '_' . mt_rand(0,10000) . '.' . $ext;
if (file_exists($fname))
$fname = rtrim($fname,'.' . $ext) . '_' . mt_rand(0,10000) . '.' . $ext;
@move_uploaded_file($_FILES['form-' . $alias_id]['tmp_name'][$field_id], $fname);
$attach[] = $fname;
}
}
@ -1132,11 +1280,13 @@ class ContactsNew
$from_name_tpl = $this->_parse_tags($from_name_tpl);
$from_name_tpl = $this->_parse_tag_fld_main($from_name_tpl);
$from_name_tpl = preg_replace_callback('/\[tag:fld:(\d+)]/', array($this,'_parse_tag_fld_post'),$from_name_tpl);
// Email отправителя
$from_email_tpl = $form['mail_set']['from_email'];
$from_email_tpl = $this->_parse_tags($from_email_tpl);
$from_email_tpl = $this->_parse_tag_fld_main($from_email_tpl);
$from_email_tpl = preg_replace_callback('/\[tag:fld:(\d+)]/', array($this,'_parse_tag_fld_post'),$from_email_tpl);
// Тема
$subject_tpl = $form['mail_set']['subject_tpl'];
$subject_tpl = $this->_parse_tags($subject_tpl);
@ -1147,8 +1297,11 @@ class ContactsNew
foreach ($recs as $rec)
{
$mail = $tpl;
$from_name = $from_name_tpl;
$from_email = $from_email_tpl;
$subject = $subject_tpl;
$if_user_open = ($rec['agent'] === 'user');
@ -1171,6 +1324,7 @@ class ContactsNew
{
$history['dialog']['request']['body'] = $mail;
$history['dialog']['request']['format'] = $form['mail_set']['format'];
$AVE_DB->Query("
INSERT INTO " . PREFIX . "_module_contactsnew_history
SET
@ -1181,6 +1335,7 @@ class ContactsNew
dialog = '" . addslashes(serialize($history['dialog'])) . "',
postdata = '" . addslashes(serialize($_POST)) . "'
");
unset($history);
}
// иначе, отправляем письмо
@ -1190,11 +1345,14 @@ class ContactsNew
$this->_parse_tag_if($from_name,'if_admin',$if_admin_open);
$this->_parse_tag_if($from_email,'if_user',$if_user_open);
$this->_parse_tag_if($from_email,'if_admin',$if_admin_open);
$from_name = $this->_eval2var('?>' . $from_name . '<?');
$from_name = trim(preg_replace('/\s+/',' ',$from_name));
$from_email = $this->_eval2var('?>' . $from_email . '<?');
$from_email = trim(preg_replace('/\s+/','',$from_email));
if (empty($from_email)) $from_email = get_settings('mail_from');
if (empty($from_email))
$from_email = get_settings('mail_from');
send_mail(
$rec['email'],
@ -1204,7 +1362,8 @@ class ContactsNew
$from_name,
$form['mail_set']['format'],
$attach,
false,false
false,
false
);
// @fix для AVE.cms.v1.5.beta <= 28: в send_mail() не выключен кэш в конце
//ob_end_clean();
@ -1215,11 +1374,11 @@ class ContactsNew
eval('?>' . $this->form['code_onsend'] . '<?');
// если включена защита от повторной отправки и не ajax
if ($form['protection'] && !isAjax())
if ($form['protection'] && ! isAjax())
{
$rand = mt_rand();
$_SESSION['mcnfinish'][$form['id'] . $rand] = true;
header('Location: ' . $_SERVER['REQUEST_URI'] . ((strpos($_SERVER['REQUEST_URI'],'?') !== false) ? '&' : '?') . 'mcnfinish=' . $rand);
header('Location: ' . $_SERVER['REQUEST_URI'] . ((strpos($_SERVER['REQUEST_URI'], '?') !== false) ? '&' : '?') . 'mcnfinish=' . $rand);
exit;
}
// иначе
@ -1231,6 +1390,7 @@ class ContactsNew
// сохраняем информацию для аякса
$this->form['ajax']['form']['is_sent'] = true;
$this->form['ajax']['form']['finish_tpl'] = $tpl;
return $tpl;
}
}

10
contactsnew/module.php

@ -37,6 +37,7 @@ function mod_contactsnew($alias_id)
global $AVE_Template;
require_once(BASE_DIR . '/modules/contactsnew/class.contactsnew.php');
$contactsnew = new ContactsNew;
$contactsnew->tpl_dir = BASE_DIR . '/modules/contactsnew/templates/';
@ -49,12 +50,14 @@ function mod_contactsnew($alias_id)
/**
* AJAX-методы
*/
if (!defined('ACP') && isset($_REQUEST['module']) && $_REQUEST['module'] == 'contactsnew')
if (! defined('ACP') && isset($_REQUEST['module']) && $_REQUEST['module'] == 'contactsnew')
{
global $AVE_Template;
$alias_id = $_REQUEST['alias_id'];
require_once(BASE_DIR . '/modules/contactsnew/class.contactsnew.php');
$contactsnew = new ContactsNew;
$contactsnew->tpl_dir = BASE_DIR . '/modules/contactsnew/templates/';
@ -69,7 +72,7 @@ if (!defined('ACP') && isset($_REQUEST['module']) && $_REQUEST['module'] == 'con
case 'validate':
$contactsnew->form_display($alias_id);
exit (json_encode($contactsnew->form['ajax']));
$contactsnew->_json($contactsnew->form['ajax'], true);
}
}
@ -81,6 +84,7 @@ if (defined('ACP') && isset($_REQUEST['mod']) && $_REQUEST['mod'] == 'contactsne
$fid = !empty($_REQUEST['fid']) ? (int)$_REQUEST['fid'] : 0;
require_once(BASE_DIR . '/modules/contactsnew/class.contactsnew.php');
$contactsnew = new ContactsNew;
$contactsnew->tpl_dir = BASE_DIR . '/modules/contactsnew/templates/';
@ -88,7 +92,7 @@ if (defined('ACP') && isset($_REQUEST['mod']) && $_REQUEST['mod'] == 'contactsne
$AVE_Template->config_load($lang_file);
// создаём переменные с версией движка
$ave14 = ((float)str_replace(',','.',APP_VERSION) < 1.5);
$ave14 = ((float)str_replace(',', '.', APP_VERSION) < 1.5);
$AVE_Template->assign('ave14', $ave14);
$AVE_Template->assign('ave15', !$ave14);

Loading…
Cancel
Save