<?php

/*
////////////////////////////////////////////////////////////////////////////////
// JohnCMS                             Content Management System              //
// Официальный сайт сайт проекта:      http://johncms.com                     //
// Дополнительный сайт поддержки:      http://gazenwagen.com                  //
////////////////////////////////////////////////////////////////////////////////
// JohnCMS core team:                                                         //
// Евгений Рябинин aka john77          john77@gazenwagen.com                  //
// Олег Касьянов aka AlkatraZ          alkatraz@gazenwagen.com                //
//                                                                            //
// Информацию о версиях смотрите в прилагаемом файле version.txt              //
////////////////////////////////////////////////////////////////////////////////
*/

defined('_IN_JOHNCMS') or die('Error: restricted access');

if (!$user_id) {
  header("Location: index.php");
 exit;
}

/*
-----------------------------------------------------------------
Личные настройки чата
-----------------------------------------------------------------
*/ 
$cv = array(
  'Стандарт' => '',
  'Красный' => '#FF0000',
  'Зелёный' => '#32CD32',
  'Синий' => '#0000FF',
  'Оранжевый' => '#FFA500',
  'Серый' => '#808080',
  'Розовый' => '#FF00FF'
);

switch ($mod) {
  case 'nik':
    if ($chat_settings['color_nik'] && $datauser['balans'] >= $chat_settings['color_nik']) {
      echo '<div class="phdr">Цвет ника</div>';
      if (isset($_GET['yes'])) {
        $cvet_n = (isset($_POST['cvet_n']) && in_array(trim($_POST['cvet_n']), $cv)) ? trim($_POST['cvet_n']) : ''; 
        if ($cvet_n != $set_chat['cvet_n']) {
          $set_chat['cvet_n'] = $cvet_n;
          $balans = $datauser['balans'] - $chat_settings['color_nik'];
          mysql_query("UPDATE `users` SET `balans` = '".$balans."', `set_chat` = '" . mysql_real_escape_string(serialize($set_chat)) . "' WHERE `id` = '".$user_id."'");
        }
        header("Location: index.php?act=my_set");
      } else {
        echo '<form action="index.php?act=my_set&amp;mod=nik&amp;yes" method="post"><div class="menu">';
        echo '&#160;Ник:<br />&#160;<select name="cvet_n">';
        foreach ($cv as $val => $key) {
          echo '<option value="'.$key.'"' . ($set_chat['cvet_n'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
        }
        echo '</select><br />';
        echo '<p><input type="submit" name="submit" value="Сохранить"/></p></div></form>';
      }
      echo '<div class="phdr"><a href="index.php?act=my_set">Назад</a></div>';
    } else {
      header("Location: index.php?act=my_set");
     exit;
    }
  break;

  case 'nik_text':
    if ($chat_settings['color_nik_text'] && $datauser['balans'] >= $chat_settings['color_nik_text']) {
      echo '<div class="phdr">Цвет ника в тексте</div>';
      if (isset($_GET['yes'])) {
        $cvet_ns = (isset($_POST['cvet_ns']) && in_array(trim($_POST['cvet_ns']), $cv)) ? trim($_POST['cvet_ns']) : ''; 
        if ($cvet_ns != $set_chat['cvet_ns']) {
          $set_chat['cvet_ns'] = $cvet_ns;
          $balans = $datauser['balans'] - $chat_settings['color_nik_text'];
          mysql_query("UPDATE `users` SET `balans` = '".$balans."', `set_chat` = '" . mysql_real_escape_string(serialize($set_chat)) . "' WHERE `id` = '".$user_id."'");
        }
        header("Location: index.php?act=my_set");
      } else {
        echo '<form action="index.php?act=my_set&amp;mod=nik_text&amp;yes" method="post"><div class="menu">';
        echo '&#160;Ник:<br />&#160;<select name="cvet_ns">';
        foreach ($cv as $val => $key) {
          echo '<option value="'.$key.'"' . ($set_chat['cvet_ns'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
        }
        echo '</select><br />';
        echo '<p><input type="submit" name="submit" value="Сохранить"/></p></div></form>';
      }
      echo '<div class="phdr"><a href="index.php?act=my_set">Назад</a></div>';
    } else {
      header("Location: index.php?act=my_set");
     exit;
    }
  break;
  
  case 'text':
    if ($chat_settings['color_text'] && $datauser['balans'] >= $chat_settings['color_text']) {
      echo '<div class="phdr">Цвет текста</div>';
      if (isset($_GET['yes'])) {
        $cvet = (isset($_POST['cvet']) && in_array(trim($_POST['cvet']), $cv)) ? trim($_POST['cvet']) : ''; 
        if ($cvet != $set_chat['cvet']) {
          $set_chat['cvet'] = $cvet;
          $balans = $datauser['balans'] - $chat_settings['color_text'];
          mysql_query("UPDATE `users` SET `balans` = '".$balans."', `set_chat` = '" . mysql_real_escape_string(serialize($set_chat)) . "' WHERE `id` = '".$user_id."'");
        }
        header("Location: index.php?act=my_set");
      } else {
        echo '<form action="index.php?act=my_set&amp;mod=text&amp;yes" method="post"><div class="menu">';
        echo '&#160;Ник:<br />&#160;<select name="cvet">';
        foreach ($cv as $val => $key) {
          echo '<option value="'.$key.'"' . ($set_chat['cvet'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
        }
        echo '</select><br />';
        echo '<p><input type="submit" name="submit" value="Сохранить"/></p></div></form>';
      }
      echo '<div class="phdr"><a href="index.php?act=my_set">Назад</a></div>';
    } else {
      header("Location: index.php?act=my_set");
     exit;
    }
  break;

  default :
    echo '<div class="phdr"><a href="index.php"><b>Чат</b></a> | Настройки</div>';
    if (isset($_POST['submit'])) {
      
      /*
      -----------------------------------------------------------------
      Установка новых настроек
      -----------------------------------------------------------------
      */ 
      $set_chat['refresh'] = isset($_POST['refresh']) ? intval($_POST['refresh']) : 20;
      $set_chat['chmes'] = isset($_POST['chmes']) ? intval($_POST['chmes']) : 10;
      $set_chat['panel_bb'] = isset($_POST['panel_bb']) ? 1 : 0;
      $set_chat['carea'] = isset($_POST['carea']) ? 1 : 0;
      $set_chat['avatar'] = isset($_POST['avatar']) ? 1 : 0;
      $set_chat['carea_w'] = isset($_POST['carea_w']) ? intval($_POST['carea_w']) : 20;
      $set_chat['carea_h'] = isset($_POST['carea_h']) ? intval($_POST['carea_h']) : 2;
      if (!$chat_settings['color_text'])
        $set_chat['cvet'] = (isset($_POST['cvet']) && in_array(trim($_POST['cvet']), $cv)) ? trim($_POST['cvet']) : '';
      if (!$chat_settings['color_nik'])
        $set_chat['cvet_n'] = (isset($_POST['cvet_n']) && in_array(trim($_POST['cvet_n']), $cv)) ? trim($_POST['cvet_n']) : '';
      if (!$chat_settings['color_nik_text'])
        $set_chat['cvet_ns'] = (isset($_POST['cvet_ns']) && in_array(trim($_POST['cvet_ns']), $cv)) ? trim($_POST['cvet_ns']) : '';
      if ($set_chat['refresh'] < 10)
        $set_chat['refresh'] = 10;
      elseif ($set_chat['refresh'] > 99)
        $set_chat['refresh'] = 99;
      if ($set_chat['chmes'] < 5)
        $set_chat['chmes'] = 5;
      elseif ($set_chat['chmes'] > 40)
        $set_chat['chmes'] = 40;
      if ($set_chat['carea_w'] < 10)
        $set_chat['carea_w'] = 10;
      elseif ($set_chat['carea_w'] > 80)
        $set_chat['carea_w'] = 80;
      if ($set_chat['carea_h'] < 1)
        $set_chat['carea_h'] = 1;
      elseif ($set_chat['carea_h'] > 9)
        $set_chat['carea_h'] = 9;
      mysql_query("UPDATE `users` SET `set_chat` = '" . mysql_real_escape_string(serialize($set_chat)) . "' WHERE `id` = '".$user_id."'");
      echo '<div class="rmenu">Настройки сохранены</div>';
    }
    if (isset($_GET['reset']) || empty($set_chat)) {
      
      /*
      -----------------------------------------------------------------
      Сброс настроек
      -----------------------------------------------------------------
      */ 
      $set_chat = array();
      $set_chat = array (
        'refresh' => 20,
        'chmes' => 10,
        'carea' => 0,
        'carea_w' => 20,
        'carea_h' => 2,
        'avatar' => 1,
        'cvet' => '',
        'cvet_n' => '',
        'cvet_ns' => '',
        'panel_bb' => 0
      );
      mysql_query("UPDATE `users` SET `set_chat` = '" . mysql_real_escape_string(serialize($set_chat)) . "' WHERE `id` = '$user_id' LIMIT 1");
      echo '<div class="rmenu">Установлены настройки по умолчанию</div>';
    }

    /*
    -----------------------------------------------------------------
    Форма ввода личных настроек чата
    -----------------------------------------------------------------
    */ 
    echo '<form action="index.php?act=my_set" method="post"><div class="menu"><p><h3><img src="img/edit.png" width="16" height="16" class="left" />&#160;Основные настройки</h3>';
    if ($set_user['avatar'])
      echo '&#160;<input name="avatar" type="checkbox" value="1" ' . ($set_chat['avatar'] ? 'checked="checked"' : '') . ' />&#160;Аватары в чате<br />';
    echo '&#160;<input name="carea" type="checkbox" value="1" ' . ($set_chat['carea'] ? 'checked="checked"' : '') . ' />&#160;Вкл. поле ввода<br />';
    echo '&#160;<input name="panel_bb" type="checkbox" value="1" ' . ($set_chat['panel_bb'] ? 'checked="checked"' : '') . ' />&#160;Панель ВВ тегов<br />';
    echo '&#160;<input type="text" name="refresh" size="2" maxlength="2" value="' . $set_chat['refresh'] . '"/>&#160;Обновление <span class="gray"><small>(10-99 сек.)</small></span><br />';
    echo '&#160;<input type="text" name="carea_w" size="2" maxlength="2" value="' . $set_chat['carea_w'] . '"/> Ширина формы <span class="gray"><small>(10-80)</small></span><br />';
    echo '&#160;<input type="text" name="carea_h" size="2" maxlength="1" value="' . $set_chat['carea_h'] . '"/> Высота формы <span class="gray"><small>(1-9)</small></span><br />';
    echo '</p><p><h3><img src="img/color_edit.png" width="16" height="16" class="left" />&#160;Цвета</h3>';
    $cv2 = array_flip($cv);
    if (!$chat_settings['color_nik']) {
      echo '&#160;Ник:<br />&#160;<select name="cvet_n">';
      foreach ($cv as $val => $key) {
        echo '<option value="'.$key.'"' . ($set_chat['cvet_n'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
      }
      echo '</select><br />';
    } else {
      echo '<p>&#160;Ник:&#160;';
      if ($set_chat['cvet_n'])
        echo '<span style="color: '.$set_chat['cvet_n'].'">'.$cv2[$set_chat['cvet_n']].'</span>';
      else
        echo $cv2[$set_chat['cvet_n']].'';
      if ($datauser['balans'] >= $chat_settings['color_nik']) {
        echo '<br />&#160;<small><a href="index.php?act=my_set&amp;mod=nik">Сменить цвет</a> <span class="gray">(Баллов: '.$chat_settings['color_nik'].')</span></small>';
      } else {
        echo '<br />&#160;<small><span class="gray">(Для изменения недостаточно баллов)</span></small>';
      }
      echo '</p>';
    }
    if (!$chat_settings['color_nik_text']) {
      echo '&#160;Ник в тексте:<br />&#160;<select name="cvet_ns">';
      foreach ($cv as $val => $key) {
        echo '<option value="'.$key.'"' . ($set_chat['cvet_ns'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
      }
      echo '</select><br />';
    } else {
      echo '<p>&#160;Ник в тексте:&#160;';
      if ($set_chat['cvet_ns'])
        echo '<span style="color: '.$set_chat['cvet_ns'].'">'.$cv2[$set_chat['cvet_ns']].'</span>';
      else
        echo $cv2[$set_chat['cvet_ns']].'';
      if ($datauser['balans'] >= $chat_settings['color_nik_text']) {
        echo '<br />&#160;<small><a href="index.php?act=my_set&amp;mod=nik_text">Сменить цвет</a> <span class="gray">(Баллов: '.$chat_settings['color_nik_text'].')</span></small>';
      } else {
        echo '<br />&#160;<small><span class="gray">(Для изменения недостаточно баллов)</span></small>';
      }
      echo '</p>';
    }
    if (!$chat_settings['color_text']) {
      echo '&#160;Сообщения:<br />&#160;<select name="cvet">';
      foreach ($cv as $val => $key) {
        echo '<option value="'.$key.'"' . ($set_chat['cvet'] == $key ? ' selected="selected">' : '>') . $val . '</option>';
      }
      echo '</select><br />';
    } else {
      echo '<p>&#160;Сообщения:&#160;';
      if ($set_chat['cvet'])
        echo '<span style="color: '.$set_chat['cvet'].'">'.$cv2[$set_chat['cvet']].'</span>';
      else
        echo $cv2[$set_chat['cvet']].'';
      if ($datauser['balans'] >= $chat_settings['color_text']) {
        echo '<br />&#160;<small><a href="index.php?act=my_set&amp;mod=text">Сменить цвет</a> <span class="gray">(Баллов: '.$chat_settings['color_text'].')</span></small>';
      } else {
        echo '<br />&#160;<small><span class="gray">(Для изменения недостаточно баллов)</span></small>';
      }
      echo '</p>';
    }
    echo '</p><p><input type="submit" name="submit" value="Сохранить"/></p></div></form>';
    echo '<div class="phdr"><a href="index.php?act=my_set&amp;reset">Сброс настроек</a></div>';
}
echo '<p><a href="../chat">В чат</a></p>';

?>