<?php

/**
 * Dialog System
 * @version 2.2
 * @package DS
 * @copyright (c) Alexeev Vlad
 */

defined('COT_CODE') or die('Wrong URL');

require_once cot_incfile('forms');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('ds', 'a');
cot_block($usr['auth_read']);

$return = 0;
$msgid = cot_import('id','G','INT');

if($msgid > 0 && $usr['id'] > 0) {
  $msgsql = $db->query("SELECT * FROM $db_ds_msg WHERE id = $msgid LIMIT 1")->fetch();
  if($msgsql['id'] > 0 && $msgsql['dialog'] > 0)
  {
    $dialogsql = $db->query("SELECT * FROM $db_ds_dialog WHERE id = ".$msgsql['dialog']." LIMIT 1")->fetch();
    if ((($dialogsql['fromid'] == $usr['id']) || ($dialogsql['toid'] == $usr['id'])) || (($dialogsql['fromid'] == 0 || $dialogsql['toid'] == 0) && in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups'])))
    {
      $act_f = ($dialogsql['fromid'] == $usr['id'] || ($dialogsql['fromid'] == 0 && in_array($usr['maingrp'], $cfg['ds']['supportinfo']['modergroups']))) ? 'act_fid' : 'act_tid';

      /* === Hook === */
      foreach (cot_getextplugins('ds.impotrant.first') as $pl)
      {
      	include $pl;
      }
      /* ===== */

      $return = ($msgsql[$act_f] == 1 ? 2 : 1);
      $db->update($db_ds_msg, array($act_f => $return), 'id='.$msgsql['id']);

      /* === Hook === */
      foreach (cot_getextplugins('ds.impotrant.done') as $pl)
      {
      	include $pl;
      }
      /* ===== */
    }
  }
}

echo $return;


