Advertisement
Guest User

ajax chat

a guest
Jan 24th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.78 KB | None | 0 0
  1. $response = array(
  2.                     'FIELDS' => array(
  3.                         'NAME' => trim($request->get('NAME')),
  4.                         'EMAIL' => trim($request->get('EMAIL')),
  5.                         'PASSWORD' => trim($request->get('PASSWORD')),
  6.                         'MESSAGE' => trim($request->get('MESSAGE')),
  7.                     ),
  8.                     'HIDDENS' => array(),
  9.                     'ERRORS' => array(),
  10.                     'WARNINGS' => array(),
  11.                     'SUCCESS' => array()
  12.                 );
  13.  
  14.                 // CHECK HIDDENS
  15.  
  16.                 if(intval($request->get('EDIT_ID')))
  17.                 {
  18.                     $response['HIDDENS']['EDIT_ID'] = $request->get('EDIT_ID');
  19.                     $response['EDIT_ID'] = $request->get('EDIT_ID');
  20.                 }
  21.  
  22.                 // CHECK FIELDS
  23.  
  24.                 if(empty($response['FIELDS']['NAME']))
  25.                 {
  26.                     $response['ERRORS']['NAME'] = 'Введите имя';
  27.                 }
  28.  
  29.                 if(empty($response['FIELDS']['MESSAGE']))
  30.                 {
  31.                     $response['ERRORS']['MESSAGE'] = 'Введите сообщение';
  32.                 }
  33.  
  34.                 if(strlen($response['FIELDS']['MESSAGE']) > 1000)
  35.                 {
  36.                     $response['ERRORS']['MESSAGE'] = 'Введите сообщение меньше 1000 символов';
  37.                 }
  38.  
  39.                 if(!User::getInstance()->isAuthorized())
  40.                 {
  41.                     if(!filter_var($response['FIELDS']['EMAIL'], FILTER_VALIDATE_EMAIL))
  42.                     {
  43.                         $response['ERRORS']['EMAIL'] = 'Введите корректный email';
  44.                     }
  45.  
  46.                     if(empty($response['ERRORS']))
  47.                     {
  48.                         $user = new \CUser();
  49.                         $arUser = UserTable::GetList(array(
  50.                             'filter' => array('LOGIN' => $response['FIELDS']['EMAIL']),
  51.                             'select' => array('ID', 'UF_IP')
  52.                         ))->fetch();
  53.  
  54.                         if(intval($arUser['ID']))
  55.                         {
  56.                             if($arUser['UF_IP'] == User::getUserIP())
  57.                             {
  58.                                 User::getInstance()->Authorize($arUser['ID'], true);
  59.                                 $response['SUCCESS'][] = 'Вы успешно авторизовались';
  60.                             }
  61.                             else
  62.                             {
  63.                                 $response['DIFFERENCE_IP'] = true;
  64.  
  65.                                 if(empty($response['FIELDS']['PASSWORD']))
  66.                                 {
  67.                                     $response['WARNINGS']['EMAIL'] = 'Введите пароль из письма';
  68.                                 }
  69.                                 else
  70.                                 {
  71.                                     $result = User::getInstance()->Login($response['FIELDS']['EMAIL'], $response['FIELDS']['PASSWORD'], true);
  72.  
  73.                                     if($result['TYPE'] !== 'ERROR')
  74.                                     {
  75.                                         $response['DIFFERENCE_IP'] = false;
  76.                                         $response['SUCCESS'][] = 'Вы успешно авторизовались';
  77.                                         $user->update($arUser['ID'], array('UF_IP' => User::getUserIP()));
  78.                                     }
  79.                                     else
  80.                                     {
  81.                                         $response['WARNINGS']['PASSWORD'] = 'Неверный пароль';
  82.                                     }
  83.                                 }
  84.                             }
  85.                         }
  86.                         else
  87.                         {
  88.                             $user = new \CUser;
  89.  
  90.                             $password = rand(100000, 999999);
  91.                             $arFields = array(
  92.                                 'ACTIVE' => 'Y',
  93.                                 'NAME' => $response['FIELDS']['NAME'],
  94.                                 'EMAIL' => $response['FIELDS']['EMAIL'],
  95.                                 'LOGIN' => $response['FIELDS']['EMAIL'],
  96.                                 'PASSWORD' => $password,
  97.                                 'CONFIRM_PASSWORD' => $password,
  98.                                 'UF_PASSWORD' => $password,
  99.                                 'UF_IP' => User::getUserIP()
  100.                             );
  101.  
  102.                             if($UID = $user->Add($arFields))
  103.                             {
  104.                                 $response['SUCCESS'][] = 'Вы успешно зарегистрированы';
  105.                                 User::getInstance()->Authorize($UID, true);
  106.  
  107.                                 Event::send(array(
  108.                                     "EVENT_NAME" => 'FEEDBACK_FORM',
  109.                                     "C_FIELDS" => array(
  110.                                         'EMAIL_TO' => $response['FIELDS']['EMAIL'],
  111.                                         'NAME' => $response['FIELDS']['NAME'],
  112.                                         'PASSWORD' => $password
  113.                                     ),
  114.                                     "LID" => SITE_ID,
  115.                                     "DUPLICATE" => 'N',
  116.                                     "MESSAGE_ID" => 81
  117.                                 ));
  118.                             }
  119.                             else
  120.                             {
  121.                                 Log::add2log(array(
  122.                                     'Ошибка регистрации пользователя',
  123.                                     $user->LAST_ERROR
  124.                                 ));
  125.                             }
  126.                         }
  127.                     }
  128.                 }
  129.  
  130.                 if(empty($response['ERRORS']) && User::getInstance()->isAuthorized())
  131.                 {
  132.                     $ORMGChat = ORMFactory::compile('general_chat', 'communications');
  133.                     $arFields = array(
  134.                         'DATE' => new DateTime(),
  135.                         'NAME' => $response['FIELDS']['NAME'],
  136.                         'TEXT' => $response['FIELDS']['MESSAGE'],
  137.                         'USER_ID' => User::getInstance()->getID()
  138.                     );
  139.  
  140.                     if(intval($response['EDIT_ID']))
  141.                     {
  142.                         $arMessage = $ORMGChat::GetList(array(
  143.                             'filter' => array('ID' => $response['EDIT_ID']),
  144.                             'select' => array('ID', 'DATE')
  145.                         ))->fetch();
  146.  
  147.                         if(intval($arMessage['ID']))
  148.                         {
  149.                             $arFields['DATE'] = $arMessage['DATE'];
  150.  
  151.                             $result = $ORMGChat::update($response['EDIT_ID'], $arFields);
  152.                         }
  153.                         else
  154.                         {
  155.                             $response['WARNINGS'][] = 'Сообщение не доступно';
  156.                             $response['FIELDS']['MESSAGE'] = '';
  157.                             $response['HIDDENS'] = array();
  158.                             $response['EDIT_ID'] = false;
  159.                         }
  160.                     }
  161.                     else
  162.                     {
  163.                         $result = $ORMGChat::add($arFields);
  164.                     }
  165.  
  166.                     if($result->isSuccess())
  167.                     {
  168.                         if(intval($response['EDIT_ID']))
  169.                         {
  170.                             $response['SUCCESS'][] = 'Сообщение обновлено';
  171.  
  172.                             \CPullWatch::AddToStack('GENERAL_CHAT', Array(
  173.                                 'module_id' => 'general_chat',
  174.                                 'command' => 'update',
  175.                                 'params' => array(
  176.                                     'EDIT_ID' => $response['EDIT_ID'],
  177.                                     'MESSAGE' => self::getMesssage(
  178.                                         $result->getId(),
  179.                                         User::getInstance()->GetID(),
  180.                                         $arFields['NAME'],
  181.                                         $arFields['DATE']->format('G:i:s'),
  182.                                         htmlspecialchars($arFields['TEXT'])
  183.                                     )
  184.                                 )
  185.                             ));
  186.                         }
  187.                         else
  188.                         {
  189.                             \CPullWatch::AddToStack('GENERAL_CHAT', Array(
  190.                                 'module_id' => 'general_chat',
  191.                                 'command' => 'add',
  192.                                 'params' => array(
  193.                                     'MESSAGE' => self::getMesssage(
  194.                                         $result->getId(),
  195.                                         User::getInstance()->GetID(),
  196.                                         $arFields['NAME'],
  197.                                         $arFields['DATE']->format('G:i:s'),
  198.                                         htmlspecialchars($arFields['TEXT'])
  199.                                     )
  200.                                 )
  201.                             ));
  202.                         }
  203.  
  204.                         $response['FIELDS']['MESSAGE'] = '';
  205.                         $response['HIDDENS'] = array();
  206.                         $response['EDIT_ID'] = false;
  207.                     }
  208.                     else
  209.                     {
  210.                         $response['SUCCESS'] = false;
  211.                         $response['WARNINGS'][] = 'Ошибка редактирования';
  212.  
  213.                         Log::add2log(array_merge(
  214.                             array('Ошибка редактирования'),
  215.                             $result->getErrorMessages()
  216.                         ));
  217.                     }
  218.  
  219.                     $response['USER_ID'] = $arFields['USER_ID'];
  220.                 }
  221.  
  222.                 $GLOBALS['CHAT_USER_FORM'] = $response;
  223.  
  224.                 ob_start();
  225.                 App::GetInstance()->IncludeComponent(
  226.                     "bitrix:main.include",
  227.                     "",
  228.                     array(
  229.                         "AREA_FILE_SHOW" => "file",
  230.                         "PATH" => "/local/include/user_form.php"
  231.                     ),
  232.                     false,
  233.                     array('HIDE_ICONS'=>'Y')
  234.                 );
  235.                 $response['FORM'] = ob_get_contents();
  236.                 ob_end_clean();
  237.  
  238.                 echo json_encode($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement