Beispiel #1
0
 public function prepare()
 {
     $filename = $this->getPath();
     if ($this->rawParams['error'] !== UPLOAD_ERR_OK || $this->isDubious($filename) === true) {
         $this->isValid = false;
         return;
     }
     if (class_exists('\\FInfo')) {
         $info = new \FInfo(FILEINFO_MIME_TYPE);
         $this->type = $info->file($filename);
     }
 }
 /**
  * loader
  * file loader
  *
  * @param string $model
  * @param string $model_id
  * @param string $fieldName
  * @param string $hash
  * @return
  */
 function loader($model = null, $model_id = null, $fieldName = null, $hash = null, $fileName = null)
 {
     $this->layout = false;
     $this->autoRender = false;
     Configure::write('debug', 0);
     if (!$model || $model_id == null || !$fieldName || !$hash) {
         $this->cakeError('error404');
         return;
     }
     if (Security::hash($model . $model_id . $fieldName . $this->Session->read('Filebinder.hash')) !== $hash) {
         $this->cakeError('error404');
         return;
     }
     $this->loadModel($model);
     if ($model_id == 0) {
         // tmp file
         $tmpPath = CACHE;
         if (!empty($this->{$model}->bindFields)) {
             foreach ($this->{$model}->bindFields as $value) {
                 if ($value['field'] === $fieldName && !empty($value['tmpPath'])) {
                     $tmpPath = $value['tmpPath'];
                 }
             }
         }
         $filePath = $tmpPath . $fileName;
     } else {
         $query = array();
         $query['recursive'] = -1;
         $query['fields'] = array('id', $fieldName);
         $query['conditions'] = array('id' => $model_id);
         $file = $this->{$model}->find('first', $query);
         if (empty($fileName)) {
             $fileName = $file[$model][$fieldName]['file_name'];
         }
         $fileContentType = $file[$model][$fieldName]['file_content_type'];
         $filePath = $file[$model][$fieldName]['file_path'];
     }
     if (!file_exists($filePath)) {
         $this->cakeError('error404');
         return;
     }
     if (strstr(env('HTTP_USER_AGENT'), 'MSIE')) {
         $fileName = mb_convert_encoding($fileName, "SJIS", "UTF-8");
         header('Content-Disposition: inline; filename="' . $fileName . '"');
     } else {
         header('Content-Disposition: attachment; filename="' . $fileName . '"');
     }
     header('Content-Length: ' . filesize($filePath));
     if (!empty($fileContentType)) {
         header('Content-Type: ' . $fileContentType);
     } else {
         if (class_exists('FInfo')) {
             $info = new FInfo(FILEINFO_MIME_TYPE);
             $fileContentType = $info->file($filePath);
             header('Content-Type: ' . $fileContentType);
         } else {
             if (function_exists('mime_content_type')) {
                 $fileContentType = mime_content_type($filePath);
                 header('Content-Type: ' . $fileContentType);
             }
         }
     }
     ob_end_clean();
     // clean
     readfile($filePath);
 }
 private function mime_type($filename)
 {
     static $info;
     if (!isset($info)) {
         $info = new FInfo(FILEINFO_MIME_TYPE);
     }
     $mime_type = file_exists($filename) ? $info->file($filename) : false;
     if ($mime_type == 'text/plain') {
         if (preg_match('/\\.css$/i', $filename)) {
             $mime_type = 'text/css';
         } else {
             if (preg_match('/\\.js$/i', $filename)) {
                 $mime_type = 'application/x-javascript';
             } else {
                 if (preg_match('/\\.html?$/i', $filename)) {
                     $mime_type = 'text/html';
                 } else {
                     if (preg_match('/\\.xml$/i', $filename)) {
                         $mime_type = 'application/xml';
                     }
                 }
             }
         }
     }
     return $mime_type;
 }