<?php

/*!
 * ifsoft.co.uk engine v1.0
 *
 * http://ifsoft.com.ua, http://ifsoft.co.uk
 * qascript@ifsoft.co.uk
 *
 * Copyright 2012-2015 Demyanchuk Dmitry (https://vk.com/dmitry.demyanchuk)
 */

$result = array("error" => true);

if (!empty($_POST)) {

    $accountId = isset($_POST['accountId']) ? $_POST['accountId'] : '';
    $accessToken = isset($_POST['accessToken']) ? $_POST['accessToken'] : '';

    $auth = new auth($dbo);

    if (!$auth->authorize($accountId, $accessToken)) {

        api::printError(ERROR_ACCESS_TOKEN, "Error authorization.");
    }

    if (isset($_FILES['uploaded_file']['name'])) {

        $currentTime = time();
        $uploaded_file_ext = @pathinfo($_FILES['uploaded_file']['name'], PATHINFO_EXTENSION);

//        $img = new imglib($dbo);
//        $response = $imgLib->createPostImg($_FILES['uploaded_file']['tmp_name'], $_FILES['uploaded_file']['name']);
//
//        if ($response['error'] === false) {
//
//            $result = array("error" => false,
//                            "imgUrl" => $response['imgUrl'],
//                            "imgUrl2" => $response['imgUrl'],
//                            "imgUrl3" => $response['imgUrl']);
//        }
//
//        unset($imglib);

        if (@move_uploaded_file($_FILES['uploaded_file']['tmp_name'], TEMP_PATH."{$currentTime}.".$uploaded_file_ext)) {

            $response = array();

            $imgLib = new imglib($dbo);
            $response = $imgLib->createPostImg(TEMP_PATH."{$currentTime}.".$uploaded_file_ext, TEMP_PATH."{$currentTime}.".$uploaded_file_ext);

            if ($response['error'] === false) {

                $result = array("error" => false,
                                "imgUrl" => $response['imgUrl'],
                                "imgUrl2" => $response['imgUrl'],
                                "imgUrl3" => $response['imgUrl']);
            }

            unset($imgLib);
        }
    }

    echo json_encode($result);
    exit;
}
