<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=ds.send.send.done
 * [END_COT_EXT]
 */

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

require_once cot_incfile('mavatars', 'plug');

$conversations_files = cot_import('conversations-files', 'P', 'ARR');

if(is_array($conversations_files['new'])) foreach($conversations_files['new'] as $mfile) {
   if($mfile > 0 && (!is_array($conversations_files['delete']) || !in_array($mfile, $conversations_files['delete']))) {
      $db->update($db_mavatars, array('mav_category' => $chatid, 'mav_code' => $pm['id']), 'mav_id='.$mfile.' AND mav_userid='.$usr['id'].' AND mav_extension="ds" AND mav_category="" AND (mav_code="" OR mav_code IS NULL)');
   }
}

$dfiles = $db->query("SELECT * FROM $db_mavatars WHERE mav_userid=".$usr['id']." AND mav_extension='ds' AND mav_category='' AND (mav_code='' OR mav_code IS NULL)")->fetchAll();
foreach($dfiles as $dfile) {
  if(file_exists($dfile['mav_filepath'].$dfile['mav_filename'].'.'.$dfile['mav_fileext'])) {
    unlink($dfile['mav_filepath'].$dfile['mav_filename'].'.'.$dfile['mav_fileext']);
  }
  $db->delete($db_mavatars, 'mav_id='.$dfile['mav_id']);
}