<?php
require_once('../wu_init.php');
if (!wu_token()) { exit('wu-error'); }
if (!USER_LOGGED) { exit('wu-error'); }
include('wu_checkadmin.php');
require('../wu-functions/wu_functions.php');
require('../wu-functions/bbdecoder/Parser.php');

if(isset($_POST['id']) && isset($_POST['msg_text_edit'])){
if(!empty($_POST['id']) && !empty($_POST['msg_text_edit'])){
$id = intval($_POST['id']);
$text = mysqli_real_escape_string($connect_db, wu_strip(rtrim($_POST['msg_text_edit'])));

$msg = mysqli_fetch_assoc(mysqli_query($connect_db, "SELECT id,msg,files,frm,dt FROM `".DB_PREFIX."_tickets_msg` WHERE id='$id' LIMIT 1"));
if (empty($msg['id'])) { exit('res-wu-error'); }

mysqli_query($connect_db, "UPDATE `".DB_PREFIX."_tickets_msg` SET `msg` = '$text' WHERE id = '$id'");

//Запуск парсера
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());

//Вывод прикреплений
if (!empty($msg['files'])) {
$images = array('gif','jpg', 'png', 'jpeg');
$files_e = '';
$files = explode('|', $msg['files']);
foreach ($files as $file) {
$expl = explode(':', $file);
$ifile = $expl[0];
$iname = $expl[1];
$exp = explode('.', $ifile);
$exp_e = strtolower($exp[1]);
if (!empty($file)){
if (in_array($exp_e, $images)) {
$files_e .= '<div class="border-thumbnail-preview tip lightgallerys" data-placement="bottom" title="'.$iname.'"><a href="/wu-uploads/attach/full/'.$ifile.'"><img class="img-thumbnail-preview" src="/wu-uploads/attach/thumbs/'.$ifile.'" alt="'.$iname.'" /></a></div>';
} else {
if ($msg['frm'] == 1) { $txti = ''; } else { $txti = ' text-info'; }
$files_e .= '<a href="/wu-engine/wu-actions/attach_download/'.strrev(base64_encode($ifile.'|'.$iname)).'" class="icon-thumbnail-preview tip" data-placement="bottom" title="'.$iname.'"><i class="icon-file6'.$txti.'"></i></a>';
}
}
}
} else { $files_e = ''; }

$parser->parse(htmlentities($_POST['msg_text_edit']));
$parsered = $parser->getAsHtml();
echo wu_smile(nl2br($parsered));
if (!empty($msg['files'])) {
if (!empty($msg['msg'])) { echo '<br />'; }
echo '<div>Прикреплённые файлы:<br />'.$files_e.'</div>';
}
?>
<span class="attribution"><?php echo wudatenorm($msg['dt']); echo '<i class="icon-pencil msg_control" onclick="msg_edit(\''.$msg['id'].'\');"></i><i class="icon-remove msg_control" onclick="msg_del(\''.$msg['id'].'\');"></i>'; ?></span>

<?php
} else { exit('res-wu-err-msg'); }
} else { exit('res-wu-error'); }
?>