Esempio n. 1
0
/**
 * Download attached files
 *
 * @param string $module
 * @param int $entity_id
 */
function downloadAtomAttaches($module, $entity_id = '')
{
    $Register = Register::getInstance();
    $user = !empty($_SESSION['user']) ? $_SESSION['user'] : array('id' => 0, 'name' => __('Guest'));
    $attaches = array();
    $files_dir = ROOT . '/sys/files/' . $module . '/';
    //$files_dir = ROOT . '/tmp/';
    $max_attach = Config::read('max_attaches', $module);
    if (empty($max_attach) || !is_numeric($max_attach)) {
        $max_attach = 5;
    }
    for ($i = 1; $i <= $max_attach; $i++) {
        $attach_name = 'attach' . $i;
        if (!empty($_FILES[$attach_name]['name'])) {
            // Извлекаем из имени файла расширение
            $filename = getSecureFilename($_FILES[$attach_name]['name'], $files_dir);
            $ext = strrchr($_FILES[$attach_name]['name'], ".");
            $is_image = isImageFile($_FILES[$attach_name]['type'], $ext);
            // Перемещаем файл из временной директории сервера в директорию files
            if (move_uploaded_file($_FILES[$attach_name]['tmp_name'], $files_dir . $filename)) {
                if ($is_image == '1') {
                    $watermark_path = ROOT . '/sys/img/' . (Config::read('watermark_type') == '1' ? 'watermark_text.png' : Config::read('watermark_img'));
                    if (Config::read('use_watermarks') && !empty($watermark_path) && file_exists($watermark_path)) {
                        $waterObj = new FpsImg();
                        $save_path = $files_dir . $filename;
                        $waterObj->createWaterMark($save_path, $watermark_path);
                    }
                }
                chmod($files_dir . $filename, 0644);
                $attach_file_data = array('entity_id' => $entity_id, 'user_id' => $user['id'], 'attach_number' => $i, 'filename' => $filename, 'size' => $_FILES[$attach_name]['size'], 'date' => new Expr('NOW()'), 'is_image' => $is_image);
                $className = ucfirst($module) . 'AttachesEntity';
                $entity = new $className($attach_file_data);
                $id = $entity->save();
                $entity->setId($id);
                $attaches[] = array_merge($entity->asArray(), array('user' => $user));
            }
        }
    }
    return $attaches;
}
Esempio n. 2
0
 /**
  * Try to save file
  *
  * @param $file array (From POST request)
  */
 private function __saveFile($file, $id)
 {
     /**
      * We doesn't check an file extension here
      * because it was doing above in the Validator.
      * That's why we could be sure that $file is image.
      */
     $ext = strtolower(strchr($file['name'], '.'));
     $file_name = $id . $ext;
     $files_dir = ROOT . '/sys/files/' . $this->module . '/';
     $path = getSecureFilename($file_name, $files_dir);
     // Перемещаем файл из временной директории сервера в директорию files
     if (move_uploaded_file($file['tmp_name'], $files_dir . $path)) {
         chmod($files_dir . $path, 0644);
         // Create watermark and resample image
         $watermark_path = ROOT . '/sys/img/' . (Config::read('watermark_type') == '1' ? 'watermark_text.png' : Config::read('watermark_img'));
         if (Config::read('use_watermarks') && !empty($watermark_path) && file_exists($watermark_path)) {
             $waterObj = new FpsImg();
             $waterObj->createWaterMark($files_dir . $path, $watermark_path);
         }
         return $path;
     }
     return false;
 }
Esempio n. 3
0
 /**
  * Try to save file
  * 
  * @param array $file (From POST request)
  */
 private function __saveFile($file)
 {
     $path = getSecureFilename($file['name'], $this->attached_files_path);
     // Перемещаем файл из временной директории сервера в директорию files
     if (move_uploaded_file($file['tmp_name'], $this->attached_files_path . $path)) {
         chmod($this->attached_files_path . $path, 0644);
         return $path;
     }
     return false;
 }