<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 2/21/2015
 * Time: 1:19 PM
 */

include( '../../inc/db.php' );
include( '../../inc/functions.php' );

if(!isset($_SESSION['loggedin']) || $user['mem_level'] != 2){
    die("AYYYYYY");
}

if( !isReady($_POST, array( "name", "cmd" ) ) ){
    die( "EMPTY" );
}

$name   =   secure($_POST['name']);
$cmd    =   $_POST['cmd'];
$emote  =   $_FILES['img'];
$randN  =   get_random_string(16);
$ext    =   get_extension($emote['name']);


if( !move_uploaded_file( $emote['tmp_name'], "../../assets/emotes/" . $randN . "." . $ext ) ){
    die("FAILED_UPLOAD");
}

$insertEmoteQuery    =   $db->prepare("INSERT INTO emotes(img,e_cmd,img_name)
                                      VALUES( :i, :e, :n )");
$insertEmoteQuery->execute(array(
    ":i"        =>  $randN . "." . $ext,
    ":e"       =>  $cmd,
    ":n"       =>  $name
));

die("SUCCESS");