Last active
December 15, 2017 22:43
-
-
Save andronex/58e4a785fa0418d61df2d18444605c73 to your computer and use it in GitHub Desktop.
Отправка сообщения Ajax с помощью API MODX через внешний файл.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//шлём заголовки | |
header('Access-Control-Allow-Origin: *'); | |
header('Content-type: application/json; charset=utf-8'); | |
// инициализация MODX API | |
define('MODX_API_MODE', true); | |
require_once dirname(dirname(__FILE__)) . '/core/config/config.inc.php'; | |
require_once MODX_BASE_PATH . 'index.php'; | |
// Включаем обработку ошибок | |
$modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'FILE'); | |
$modx->setLogLevel(modX::LOG_LEVEL_ERROR); | |
$modx->getService('error','error.modError'); | |
$modx->error->message = null; | |
//проверем заполнение полей | |
if(!$_POST['con_email']){ | |
$out = array('success' => false, 'message' => 'Не указан e-mail!'); | |
die(json_encode($out)); | |
} | |
if(!$_POST['con_phone']){ | |
$out = array('success' => false, 'message' => 'Не указан телефон!'); | |
die(json_encode($out)); | |
} | |
if(!$_POST['con_message']){ | |
$out = array('success' => false, 'message' => 'Нечего отправлять!'); | |
die(json_encode($out)); | |
} | |
//принимаем e-mail и фильтруем его | |
if(!$email = filter_var($_POST['con_email'], FILTER_SANITIZE_EMAIL)){ | |
$out = array('success' => false, 'message' => 'Неверный e-mail!'); | |
die(json_encode($out)); | |
} | |
$input = $modx->sanitize($_REQUEST, $modx->sanitizePatterns); | |
$_POST = array(); | |
$_REQUEST = array(); | |
$_REQUEST = $input; | |
$name = $modx->sanitizeString($input['con_name']); | |
$message = $input['con_message']; | |
$phone = $modx->sanitizeString($input['con_phone']); | |
$ispartner = false; | |
if(!$profile = $modx->getObject('modUserProfile', array('email' => $email))){ | |
$pass = md5($email . time()); | |
$user = $modx->newObject('modUser', array('username' => $email)); | |
$user->set('password', $pass); | |
$profile = $modx->newObject('modUserProfile'); | |
$user->addOne($profile); | |
// Назначаем дополнительные поля | |
$profile->set('fullname', ($name) ? 'Партнёр: ' . $name : 'Партнёр ' . $email); | |
$profile->set('email', $email); | |
$profile->set('mobilephone', $phone); | |
if(!$user->save()){ | |
$modx->log(modX::LOG_LEVEL_ERROR, print_r('Не удалось создать партнёра ' . $email, true)); | |
} | |
else{ | |
//добавляем в группу с ID = 3 | |
$user->joinGroup(3, null, null); | |
$ispartner = true; | |
} | |
} | |
else{ | |
$ispartner = true; | |
} | |
$message .= nl2br("\r\n\r\n\r\n | |
" . ($ispartner) ? "\r\n\r\n\r\n | |
Метка системы: пользователь добавлен в группу Партнёров на сайте" : "\r\n\r\n\r\n | |
Метка системы: не удалось добавить пользователя в группу Партнёров на сайте", false); | |
$modx->getService('mail', 'mail.modPHPMailer'); | |
$modx->mail->set(modMail::MAIL_BODY, $message); | |
$modx->mail->set(modMail::MAIL_FROM, $modx->getOption('mail_smtp_user')); | |
$modx->mail->set(modMail::MAIL_SENDER, $modx->getOption('mail_smtp_user')); | |
$modx->mail->set(modMail::MAIL_SUBJECT, 'У вас новое сообщение с сайта ' . MODX_HTTP_HOST); | |
$modx->mail->address('to', $modx->getChunk('email')); | |
$modx->mail->setHTML(true); | |
if (!$modx->mail->send()) { | |
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo); | |
$out = array('success' => false, 'message' => 'Возникла ошибка при отправке сообщения!'); | |
die(json_encode($out)); | |
} | |
$modx->mail->reset(); | |
$out = array('success' => true, 'message' => 'Сообщение успешно отправлено!'); | |
die(json_encode($out)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment