<?php
$loader = $this->registry->get('load');
$loader->controller('common/upload');

class ControllerCommonImageManager extends ControllerCommonUpload
{

  public function __construct($registry)
  {
    parent::__construct($registry);

    $this->setPathFile(DIR_IMAGE);
    $this->setPathValidate('common/filemanager');

    $this->setAllowed($this->allowedImg);

    $this->setAllowedExt($this->allowedImgExt);

    $this->setFolder('image');

    $this->setLinkFile($this->urlloja->link('', '', 'SSL', false) . 'image/data/');

  }

  public function list()
  {
    $this->load->dmodel('tool/image');

    if (isset($this->request->get['file'])) {
      $this->response->setOutput($this->dmodel_tool_image->resize(html_entity_decode($this->request->get['file'], ENT_QUOTES, 'UTF-8'), 100, 100));
    }
  }

  public function upload()
  {
    $this->language->load('common/filemanager');

    $json = array();
    $size = 300000;
    $error_size = $this->language->get('error_file_size');
    if (isset($this->request->post['size'])) {
      $size = $this->request->post['size'];
      $error_size = $this->language->get('error_file_size_bigger');
    }

    if (isset($this->request->post['directory'])) {
      if (isset($this->request->files['file']) && $this->request->files['file']['tmp_name']) {
        $filename = basename(html_entity_decode(preg_replace(['/[^A-Za-z0-9 _ .-]/', '/ /i'], ['', '_'], $this->request->files['file']['name']), ENT_QUOTES, 'UTF-8'));

        if ((strlen($filename) < 3) || (strlen($filename) > 255)) {
          $json['error'] = $this->language->get('error_filename');
        }

        $directory = rtrim($this->getPathFile() . 'data/' . str_replace('../', '', $this->request->post['directory']), '/');

        if (!is_dir($directory)) {
          $json['error'] = $this->language->get('error_directory');
        }

        if ($this->request->files['file']['size'] > $size) {
          $json['error'] = $error_size;
        }

        $dimensoes = getimagesize($this->request->files['file']['tmp_name']);

        if($dimensoes[0] > 1920){
          $json['error'] = 'Lagura da imagem não suportada, a largura maxima é de 1920px';
        }

        if($dimensoes[1] > 1920){
          $json['error'] = 'Altura da imagem não suportada, a altura maxima é de 1920px';
        }

        if (!in_array($this->request->files['file']['type'], $this->getAllowed())) {
          $json['error'] = $this->language->get('error_file_type');
        }

        if (!in_array(strtolower(strrchr($filename, '.')), $this->getAllowedExt())) {
          $json['error'] = $this->language->get('error_file_type');
        }

        if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
          $json['error'] = 'error_upload_' . $this->request->files['file']['error'];
        }
      } else {
        $json['error'] = $this->language->get('error_file');
      }
    } else {
      $json['error'] = $this->language->get('error_directory');
    }

    if (!$this->user->hasPermission('modify', $this->getPathValidate())) {
      $json['error'] = $this->language->get('error_permission');
    }

    if (!isset($json['error'])) {
      if (@move_uploaded_file($this->request->files['file']['tmp_name'], $directory . '/' . $filename)) {
        $json['success'] = $this->language->get('text_uploaded');
      } else {
        $json['error'] = $this->language->get('error_uploaded');
      }
    }

    $this->response->setOutput(json_encode($json));
  }

}
