<?php
/*
=================================================================
Bullet Energy - by White-Master 
-----------------------------------------------------------------
http://white-master.ru/
-----------------------------------------------------------------
Copyright (c) 2011 White-Master
=================================================================
Данный код защищен авторскими правами
=================================================================
Файл: upload.class.php
=================================================================
*/
class BulletEnergyUpload
{
  private $modules_config = array();
  private $group_config = array();
  private $acces_file_upload = array();
  private $acces_ext_images = array();
  private $group_param = array();
  private $memberID = array();
  private $fileDeniExt = array();
  private $folder_dir = '';
  private $files_dir = '';
  private $cashe_system = '';
  private $added_time = '';
  private $thistime = '';
  private $db = null;
  public function __construct()
  {
    global $member_id, $config;
    $this->memberID = $member_id;
    require ('../data/forum_config.php');
    $this->modules_config = $bullet_energy_config;
    $this->folder_dir = ROOT_DIR . '/uploads/forum/images/' . date("Y-m") . '/';
    $this->files_dir = ROOT_DIR . '/uploads/forum/files/';
    $this->cashe_system = ENGINE_DIR . '/bullet_energy/cache/system/';
    $this->group_config = unserialize(@file_get_contents($this->cashe_system . 'groups_config.tmp'));
    if (!class_exists('db')) {
      require_once ENGINE_DIR . '/classes/mysql.class.php';
    }
    $this->db = new db;
    $this->acces_file_upload = explode(',', $this->modules_config['upload_file_type']);
    $this->acces_ext_images = array("gif", "jpg", "png", "jpe", "jpeg");
    $this->fileDeniExt = array('phtml', 'php', 'php3', 'php4', 'php5', 'php6', 'phps', 'cgi', 'pl', 'asp', 'aspx', 'fpl', 'jsp');
    $this->added_time = time() + ($config['date_adjust'] * 60);
    $this->thistime = date("Y-m", $this->added_time);
    if (!$this->group_config or !$this->modules_config or !is_array($member_id)) {
      die();
    }
  }
  public function save_file()
  {
    $this->check_dirs($this->folder_dir);
    $this->check_dirs($this->folder_dir . "thumbs/");
    $this->check_dirs($this->files_dir);
    if (!is_dir($this->folder_dir . "thumbs/") or !is_dir($this->folder_dir) or !is_dir($this->files_dir)) {
      die();
    }
    if (!is_writable($this->folder_dir . "thumbs/") or !is_writable($this->folder_dir) or !is_writable($this->files_dir)) {
      die();
    }
    $user_name = $this->db->safesql($this->memberID['name']);
    $user_id = $this->db->safesql($this->memberID['user_id']);
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $file_name = time() . $this->nameTranslit($_FILES['Filedata']['name']);
    $file_size = $_FILES['Filedata']['size'];
    $file_type = $_FILES['Filedata']['type'];
    $targetPath = $this->folder_dir;
    $targetPath_ExtF = $this->files_dir;
    $targetFile = str_replace('//', '/', $targetPath) . $file_name;
    $targetFile_Ext_file = str_replace('//', '/', $targetPath_ExtF) . $file_name;
    $arr_ = explode('.', end(explode('/', $targetFile)));
    //:TODO: stropos ext file
    if (in_array(end($arr_), $this->fileDeniExt)) {
      die();
    }
    if (in_array(end($arr_), $this->acces_file_upload) or in_array(end($arr_), $this->acces_ext_images)) {
      if ($_FILES['Filedata']['size'] <= $this->modules_config['upload_max_size'] * 1024) {
        if (in_array(end($arr_), $this->acces_ext_images)) {
          move_uploaded_file($tempFile, $targetFile);
          if (intval($this->modules_config['max_images_upload']) != 0) {
            include_once ENGINE_DIR . '/classes/thumb.class.php';
            $thumb = new thumbnail($targetFile);
            if ($thumb->size_auto($this->modules_config['size_image_th'], $this->modules_config['wh_images'])) {
              $thumb->jpeg_quality(100);
              $thumb->save($this->folder_dir . "thumbs/" . $file_name);
            }
            unset($thumb);
            if (file_exists($this->folder_dir . "thumbs/" . $file_name)) {
              @chmod($this->folder_dir . "thumbs/" . $file_name, 0666);
            }
          }
          $this->db->query("INSERT INTO " . PREFIX . "_be_attachment (attachment_id,attachment_date,attachment_type,attachment_name,attachment_user_name,attachment_user_id,attachment_content_id,attachment_count) VALUES (NULL,'{$this->added_time}','0','{$file_name}','{$user_name}','{$user_id}','0','0')");
        } else {
          move_uploaded_file($tempFile, $targetFile_Ext_file);
          if (file_exists($targetFile_Ext_file)) {
            $this->db->query("INSERT INTO " . PREFIX . "_be_attachment (attachment_id,attachment_date,attachment_type,attachment_name,attachment_user_name,attachment_user_id,attachment_content_id,attachment_count,size) VALUES (NULL,'{$this->added_time}','1','{$file_name}','{$user_name}','{$user_id}','0','0','{$file_size}')");
          }
        }
      } else {
        die();
      }
    } else {
      die();
    }
  }
  private function nameTranslit($rec = '')
  {
    $pattern = '/[а-яА-Я]+/';
    preg_match($pattern, $rec, $sss);
    if ($sss) {
      return totranslit(iconv("UTF-8", 'windows-1251', $rec));
    } else {
      return totranslit($rec);
    }
  }
  private function check_dirs($dir)
  {
    if (file_exists($dir) == false) {
      @mkdir($dir, 0777);
      @chmod($dir, 0777);
    }
  }
}

?>