Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $response = array(
- 'FIELDS' => array(
- 'NAME' => trim($request->get('NAME')),
- 'EMAIL' => trim($request->get('EMAIL')),
- 'PASSWORD' => trim($request->get('PASSWORD')),
- 'MESSAGE' => trim($request->get('MESSAGE')),
- ),
- 'HIDDENS' => array(),
- 'ERRORS' => array(),
- 'WARNINGS' => array(),
- 'SUCCESS' => array()
- );
- // CHECK HIDDENS
- if(intval($request->get('EDIT_ID')))
- {
- $response['HIDDENS']['EDIT_ID'] = $request->get('EDIT_ID');
- $response['EDIT_ID'] = $request->get('EDIT_ID');
- }
- // CHECK FIELDS
- if(empty($response['FIELDS']['NAME']))
- {
- $response['ERRORS']['NAME'] = 'Введите имя';
- }
- if(empty($response['FIELDS']['MESSAGE']))
- {
- $response['ERRORS']['MESSAGE'] = 'Введите сообщение';
- }
- if(strlen($response['FIELDS']['MESSAGE']) > 1000)
- {
- $response['ERRORS']['MESSAGE'] = 'Введите сообщение меньше 1000 символов';
- }
- if(!User::getInstance()->isAuthorized())
- {
- if(!filter_var($response['FIELDS']['EMAIL'], FILTER_VALIDATE_EMAIL))
- {
- $response['ERRORS']['EMAIL'] = 'Введите корректный email';
- }
- if(empty($response['ERRORS']))
- {
- $user = new \CUser();
- $arUser = UserTable::GetList(array(
- 'filter' => array('LOGIN' => $response['FIELDS']['EMAIL']),
- 'select' => array('ID', 'UF_IP')
- ))->fetch();
- if(intval($arUser['ID']))
- {
- if($arUser['UF_IP'] == User::getUserIP())
- {
- User::getInstance()->Authorize($arUser['ID'], true);
- $response['SUCCESS'][] = 'Вы успешно авторизовались';
- }
- else
- {
- $response['DIFFERENCE_IP'] = true;
- if(empty($response['FIELDS']['PASSWORD']))
- {
- $response['WARNINGS']['EMAIL'] = 'Введите пароль из письма';
- }
- else
- {
- $result = User::getInstance()->Login($response['FIELDS']['EMAIL'], $response['FIELDS']['PASSWORD'], true);
- if($result['TYPE'] !== 'ERROR')
- {
- $response['DIFFERENCE_IP'] = false;
- $response['SUCCESS'][] = 'Вы успешно авторизовались';
- $user->update($arUser['ID'], array('UF_IP' => User::getUserIP()));
- }
- else
- {
- $response['WARNINGS']['PASSWORD'] = 'Неверный пароль';
- }
- }
- }
- }
- else
- {
- $user = new \CUser;
- $password = rand(100000, 999999);
- $arFields = array(
- 'ACTIVE' => 'Y',
- 'NAME' => $response['FIELDS']['NAME'],
- 'EMAIL' => $response['FIELDS']['EMAIL'],
- 'LOGIN' => $response['FIELDS']['EMAIL'],
- 'PASSWORD' => $password,
- 'CONFIRM_PASSWORD' => $password,
- 'UF_PASSWORD' => $password,
- 'UF_IP' => User::getUserIP()
- );
- if($UID = $user->Add($arFields))
- {
- $response['SUCCESS'][] = 'Вы успешно зарегистрированы';
- User::getInstance()->Authorize($UID, true);
- Event::send(array(
- "EVENT_NAME" => 'FEEDBACK_FORM',
- "C_FIELDS" => array(
- 'EMAIL_TO' => $response['FIELDS']['EMAIL'],
- 'NAME' => $response['FIELDS']['NAME'],
- 'PASSWORD' => $password
- ),
- "LID" => SITE_ID,
- "DUPLICATE" => 'N',
- "MESSAGE_ID" => 81
- ));
- }
- else
- {
- Log::add2log(array(
- 'Ошибка регистрации пользователя',
- $user->LAST_ERROR
- ));
- }
- }
- }
- }
- if(empty($response['ERRORS']) && User::getInstance()->isAuthorized())
- {
- $ORMGChat = ORMFactory::compile('general_chat', 'communications');
- $arFields = array(
- 'DATE' => new DateTime(),
- 'NAME' => $response['FIELDS']['NAME'],
- 'TEXT' => $response['FIELDS']['MESSAGE'],
- 'USER_ID' => User::getInstance()->getID()
- );
- if(intval($response['EDIT_ID']))
- {
- $arMessage = $ORMGChat::GetList(array(
- 'filter' => array('ID' => $response['EDIT_ID']),
- 'select' => array('ID', 'DATE')
- ))->fetch();
- if(intval($arMessage['ID']))
- {
- $arFields['DATE'] = $arMessage['DATE'];
- $result = $ORMGChat::update($response['EDIT_ID'], $arFields);
- }
- else
- {
- $response['WARNINGS'][] = 'Сообщение не доступно';
- $response['FIELDS']['MESSAGE'] = '';
- $response['HIDDENS'] = array();
- $response['EDIT_ID'] = false;
- }
- }
- else
- {
- $result = $ORMGChat::add($arFields);
- }
- if($result->isSuccess())
- {
- if(intval($response['EDIT_ID']))
- {
- $response['SUCCESS'][] = 'Сообщение обновлено';
- \CPullWatch::AddToStack('GENERAL_CHAT', Array(
- 'module_id' => 'general_chat',
- 'command' => 'update',
- 'params' => array(
- 'EDIT_ID' => $response['EDIT_ID'],
- 'MESSAGE' => self::getMesssage(
- $result->getId(),
- User::getInstance()->GetID(),
- $arFields['NAME'],
- $arFields['DATE']->format('G:i:s'),
- htmlspecialchars($arFields['TEXT'])
- )
- )
- ));
- }
- else
- {
- \CPullWatch::AddToStack('GENERAL_CHAT', Array(
- 'module_id' => 'general_chat',
- 'command' => 'add',
- 'params' => array(
- 'MESSAGE' => self::getMesssage(
- $result->getId(),
- User::getInstance()->GetID(),
- $arFields['NAME'],
- $arFields['DATE']->format('G:i:s'),
- htmlspecialchars($arFields['TEXT'])
- )
- )
- ));
- }
- $response['FIELDS']['MESSAGE'] = '';
- $response['HIDDENS'] = array();
- $response['EDIT_ID'] = false;
- }
- else
- {
- $response['SUCCESS'] = false;
- $response['WARNINGS'][] = 'Ошибка редактирования';
- Log::add2log(array_merge(
- array('Ошибка редактирования'),
- $result->getErrorMessages()
- ));
- }
- $response['USER_ID'] = $arFields['USER_ID'];
- }
- $GLOBALS['CHAT_USER_FORM'] = $response;
- ob_start();
- App::GetInstance()->IncludeComponent(
- "bitrix:main.include",
- "",
- array(
- "AREA_FILE_SHOW" => "file",
- "PATH" => "/local/include/user_form.php"
- ),
- false,
- array('HIDE_ICONS'=>'Y')
- );
- $response['FORM'] = ob_get_contents();
- ob_end_clean();
- echo json_encode($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement