<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 4/24/2015
 * Time: 12:56 PM
 */

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

$params                 =   getParams();
$output                 =   array('status' => 1);

if(!isset($_FILES['img']) || $_FILES['img']['error'] !== 0){
    dj($output);
}

$img                    =   $_FILES['img'];
$name                   =   get_random_string(16);
$ext                    =   get_extension($img['name']);
$max                    =   $settings['max_upload_size'] * 1000;
$getUserQuery           =   $db->prepare("SELECT `upl_dir` FROM users WHERE id = :uid");
$getUserQuery->execute(array(":uid"  =>  $_SESSION['uid']));
$getUserRow             =   $getUserQuery->fetch(PDO::FETCH_ASSOC);

if($img['size'] > $max || !in_array($ext, array("png", "jpg", "jpeg", "gif"))){
    dj($output);
}

$upl                    =   $getUserRow['upl_dir'] . "/" . $name . "." . $ext;

if(!move_uploaded_file( $img['tmp_name'], "../../uploads/" . $upl)){
    dj($output);
}

$updateImgQuery =   $db->prepare("UPDATE users SET header_img = :img WHERE id = :uid");
$updateImgQuery->execute(array(
    ":img"  =>  $upl,
    ":uid"  =>  $_SESSION['uid']
));

$output['status']       =   2;
$output['img']          =   $upl;
dj($output);