<?php

class PortfolioController
    extends baseController
{
    function index()
    {
        global $website_options;

        $portfolioModel = new portfolioModel();
        $entries = $portfolioModel->getList();

        require 'views/portfolio/index.tpl.php';
    }

    function upload()
    {
        $response = array('status' => 'error');
        if (!empty($_FILES['ufile'])) {
            do {
                $file = $_FILES['ufile'];
                if ($file['error']) {
                    $response['code'] = 1;
                    break;
                }

                if ($file['size'] > 1024*1024*10) {
                    $response['code'] = 2;
                    break;
                }

                $type = null;
                if (!empty($_POST['type']) && $_POST['type'] == 'image') {
                    $type = 'image';
                }

                $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
                if (in_array($ext, ['php', 'htm', 'html', 'php4', 'php5', 'htaccess'])) {
                    $response['code'] = 3;
                    break;
                }

                if ($type == 'image' && ($ext != 'jpg' && $ext != 'png' && $ext != 'jpeg')) {
                    $response['code'] = 4;
                    break;
                }

                $filename = sha1($file['tmp_name']);

                $destination = "static/uploads/portfolio/$filename.$ext";
                //if (!file_exists($destination)) {
                    //if ($type == 'image') {
                    //    autoRotateImage($file['tmp_name']);
                    //}

                    move_uploaded_file($file['tmp_name'], $destination);
                //}

                $uploadsModel = new uploadsModel();
                $id = $uploadsModel->insert($destination, $type, $file['name']);

                $response['status'] = 'ok';
                $response['id'] = $id;
            } while (0);
        }

        echo json_encode($response);
        exit;
    }
}
?>