<?php 
require("common_begin.php");

$lst = Listopia::getInstance();

$result = array();

if ($lst->isLoggedIn())
{
  $msgID = $_POST['id'];
  $authKey = $_POST['auth'];

  if (!isset($msgID))
  {
    $result['result'] = "No message ID sent";
  }
  else if (!isset($authKey) || $authKey != $_COOKIE[__LAUTH_KEY__])
  {
    $result['result'] = "Incorrect auth key";
  }
  else
  {
    // get the message data
    $query = $lst->Database()->select(array(
                  'fields' => 'mess_content',
                  'table' => 'messages',
                  'condition' => 'mess_id = '.((int)$msgID)
                  ));
                
    if (!empty($query[0]))
    {
      $ok = $lst->setOrUpdateUserAvatar(Session::getInstance()->gl_userid, $query[0]['messages']['mess_content']);
      if ($ok !== false)
      {
        $result['success'] = true;
      }
      $result['result'] = $insertResult;
    }
    else
    {
      $result['result'] = "Message doesn't exist";
    }    
  }
}
else
{
  $result['result'] = "Not logged in";
}

echo json_encode($result);

require("common_end.php");

