<?php
defined('COT_CODE') or die('Wrong URL');

$return = array(
  'success' => false,
  'error' => 'Вы должны быть авторизованы',
  'redirect' => '',
);

if($usr['id'] > 0) {
  if($cfg['users']['user_email_noprotection'] || $cfg['users']['regnoactivation']) {
    $return['error'] = 'Метод не доступен';
  } else {
    $ruseremail = cot_import('user_email', 'P', 'TXT');
    if(!empty($return)) {
      $return['success'] = true;

      $email_exists = (bool)cot::$db->query("SELECT user_id FROM ".cot::$db->users." WHERE user_email = ? LIMIT 1",
            array($ruseremail))->fetch();

      if (!cot_check_email($ruseremail)) {
        $return['success'] = false;
        $return['error'] = $L['aut_emailtooshort'];
      }
      if ($email_exists && !$cfg['useremailduplicate']) {
        $return['success'] = false;
        $return['error'] = $L['aut_emailalreadyindb'];
      }

      if($return['success']) {
        $validationkey = md5(microtime());

        $db->update($db_users, array('user_newemail' => $ruseremail, 'user_lostpass' => $validationkey), "user_id='".$usr['id']."'");

        $rsubject = $L['aut_mailnoticetitle'];
        $ractivate = $cfg['mainurl'].'/'.cot_url('users', 'm=confirmemail&a=validate&v='.$validationkey, '', true);
        $rbody = sprintf($L['aut_emailchange'], $usr['name'], $ractivate);
        $rbody .= "\n\n".$L['aut_contactadmin'];
        cot_mail($ruseremail, $rsubject, $rbody);

        $return['redirect'] = cot_url('users', 'm=profile', '', true);
      }
    } else {
      $return['error'] = $L['aut_emailtooshort'];
    }
  }
}

?>
