Example #1
0
 public static function getFileType($filepath)
 {
     /*
      * This extracts the information from the file contents.
      * Unfortunately it doesn't properly detect the difference between text-based file types.
      *
     $mime_type = File::getMimeType($filepath);
     $mime_type_chunks = explode("/", $mime_type, 2);
     $type = $mime_type_chunks[1];
     */
     return File::getFileExtension($filepath);
 }
Example #2
0
 function uploadFile($location, $userfile)
 {
     global $encodeExplorer;
     $name = basename($userfile['name']);
     if (get_magic_quotes_gpc()) {
         $name = stripslashes($name);
     }
     $upload_dir = $location->getFullPath();
     $upload_file = $upload_dir . $name;
     if (function_exists("finfo_open") && function_exists("finfo_file")) {
         $mime_type = File::getFileMime($userfile['tmp_name']);
     } else {
         $mime_type = $userfile['type'];
     }
     $extension = File::getFileExtension($userfile['name']);
     if (!$location->uploadAllowed()) {
         $encodeExplorer->setErrorString("upload_not_allowed");
     } else {
         if (!$location->isWritable()) {
             $encodeExplorer->setErrorString("upload_dir_not_writable");
         } else {
             if (!is_uploaded_file($userfile['tmp_name'])) {
                 $encodeExplorer->setErrorString("failed_upload");
             } else {
                 if (is_array(EncodeExplorer::getConfig("upload_allow_type")) && count(EncodeExplorer::getConfig("upload_allow_type")) > 0 && !in_array($mime_type, EncodeExplorer::getConfig("upload_allow_type"))) {
                     $encodeExplorer->setErrorString("upload_type_not_allowed");
                 } else {
                     if (is_array(EncodeExplorer::getConfig("upload_reject_extension")) && count(EncodeExplorer::getConfig("upload_reject_extension")) > 0 && in_array($extension, EncodeExplorer::getConfig("upload_reject_extension"))) {
                         $encodeExplorer->setErrorString("upload_type_not_allowed");
                     } else {
                         if (!@move_uploaded_file($userfile['tmp_name'], $upload_file)) {
                             $encodeExplorer->setErrorString("failed_move");
                         } else {
                             chmod($upload_file, EncodeExplorer::getConfig("upload_file_mode"));
                             Logger::logCreation($location->getDir(true, false, false, 0) . $name, false);
                             Logger::emailNotification($location->getDir(true, false, false, 0) . $name, true);
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
 public function getThumbnail($size = 'thumb', $width = false, $height = false, $alt = '', $params = array(), $absoluteUrl = false, $refresh = false)
 {
     $photo = $this->owner->{$this->relation};
     if (!$photo) {
         $placeholder = $this->placeholder($size);
         $params = array_merge($params, array('width' => $width ? $width : 'auto', 'height' => $height ? $height : 'auto'));
         $prefix = $absoluteUrl ? request()->hostInfo : '';
         return CHtml::image($prefix . $placeholder, $alt, $params);
     }
     //вывод флеш банера
     if (File::getFileExtension($photo->filename) == 'swf') {
         $options = array();
         $options['src'] = $photo->getImageUrl($size, $absoluteUrl);
         $aInfo = getimagesize(substr($options['src'], 1));
         //надо убрать спереди /
         list($iWidth, $iHeight) = $aInfo;
         $options['width'] = $iWidth;
         $options['height'] = $iHeight;
         $options['src'] .= $this->owner->getUrl() ? '?clickTAG=' . $this->owner->getUrl() : '';
         return app()->controller->widget('ext.flash.EJqueryFlash', array('name' => 'flash' . $this->owner->id, 'htmlOptions' => $options), true);
     }
     //вывод фотки
     return $photo->getThumbnail($size, $width, $height, $alt, $params, $absoluteUrl, $refresh);
 }
Example #4
0
 /**
  * Filters extensions from file name and store it.
  *
  * @param string	File name
  *
  * @return Upload
  */
 protected function setExtension($filename)
 {
     $this->extension = File::getFileExtension($filename);
     return $this;
 }
Example #5
0
 public static function resizeImage($source_file, $dest_file, $width, $height, $crop = false)
 {
     if (!is_readable($source_file)) {
         $source_file = File::getFilePath($source_file);
     }
     if (!is_readable($dest_file)) {
         $dest_file = File::getFilePath($dest_file);
     }
     if (!file_exists($source_file)) {
         throw new Exception('Original image file not found!');
     }
     $source_extension = strtolower(File::getFileExtension($source_file));
     $dest_extension = strtolower(File::getFileExtension($dest_file));
     switch ($source_extension) {
         case 'jpg':
         case 'jpeg':
             $img = imagecreatefromjpeg($source_file);
             break;
         case 'gif':
             $img = imagecreatefromgif($source_file);
             break;
         case 'png':
             $img = imagecreatefrompng($source_file);
             break;
         default:
             throw new Exception('Image format not supported.');
             break;
     }
     list($w, $h) = getimagesize($source_file);
     if ($w === $width && $h === $height) {
         $new = $img;
         // Preserve transparency
         if ($source_extension == "gif" || $source_extension == "png") {
             imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
             imagealphablending($new, false);
             imagesavealpha($new, true);
         }
     } else {
         if ($crop) {
             $width_new = $h * $width / $height;
             $height_new = $w * $height / $width;
             //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
             if ($width_new > $width) {
                 $x = 0;
                 $y = ($h - $height_new) / 2;
                 $h = $height_new;
             } else {
                 $x = ($w - $width_new) / 2;
                 $y = 0;
                 $w = $width_new;
             }
         } else {
             $ratio = min($width / $w, $height / $h);
             $width = $w * $ratio;
             $height = $h * $ratio;
             $x = 0;
             $y = 0;
         }
         $new = imagecreatetruecolor($width, $height);
         // Preserve transparency
         if ($source_extension == "gif" || $source_extension == "png") {
             imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
             imagealphablending($new, false);
             imagesavealpha($new, true);
         }
         imagecopyresampled($new, $img, 0, 0, $x, $y, $width, $height, $w, $h);
     }
     switch ($dest_extension) {
         case 'jpg':
         case 'jpeg':
             imagejpeg($new, $dest_file, 90);
             break;
         case 'gif':
             imagegif($new, $dest_file);
             break;
         case 'png':
             imagepng($new, $dest_file, 5);
             break;
     }
 }
Example #6
0
 private function shareProcess($body, $filename)
 {
     $ext = File::getFileExtension($filename);
     if (in_array($ext, $this->allowedExtensions)) {
         File::checkPermissions(app()->getRuntimePath() . "/tmp");
         $temporary = app()->getRuntimePath() . "/tmp/temp_" . time() . ".{$ext}";
         file_put_contents($temporary, $body);
         $this->saveFile($temporary, $this->_destFileDir . DS . $filename);
     }
 }
Example #7
0
 /**
  * Check if all the parts exist, and 
  * gather all the parts of the file together
  *
  * @param string $location  - the final location
  * @param string $temp_dir  - the temporary directory holding all the parts of the file
  * @param string $fileName  - the original file name
  * @param string $chunkSize - each chunk size (in bytes)
  * @param string $totalSize - original file size (in bytes)
  * @param string $logloc    - relative location for log file
  *
  * @return uploaded file
  */
 public function createFileFromChunks($location, $temp_dir, $fileName, $chunkSize, $totalSize, $logloc)
 {
     global $chunk;
     $upload_dir = str_replace('\\', '', $location);
     $extension = File::getFileExtension($fileName);
     // count all the parts of this file
     $total_files = 0;
     foreach (scandir($temp_dir) as $file) {
         if (stripos($file, $fileName) !== false) {
             $total_files++;
         }
     }
     $finalfile = FileManager::safeExtension($fileName, $extension);
     // check that all the parts are present
     // the size of the last part is between chunkSize and 2*$chunkSize
     if ($total_files * $chunkSize >= $totalSize - $chunkSize + 1) {
         // create the final file
         if (($openfile = fopen($upload_dir . $finalfile, 'w')) !== false) {
             for ($i = 1; $i <= $total_files; $i++) {
                 fwrite($openfile, file_get_contents($temp_dir . '/' . $fileName . '.part' . $i));
             }
             fclose($openfile);
             // rename the temporary directory (to avoid access from other
             // concurrent chunks uploads) and than delete it
             if (rename($temp_dir, $temp_dir . '_UNUSED')) {
                 Actions::deleteDir($temp_dir . '_UNUSED');
             } else {
                 Actions::deleteDir($temp_dir);
             }
             $chunk->setSuccess(" <span><i class=\"fa fa-check-circle\"></i> " . $finalfile . " </span> ", "yep");
             $chunk->setUserUp($totalSize);
             $message = array('user' => GateKeeper::getUserInfo('name'), 'action' => 'ADD', 'type' => 'file', 'item' => $logloc . $finalfile);
             Logger::log($message, "");
             if (SetUp::getConfig("notify_upload")) {
                 Logger::emailNotification($logloc . $finalfile, 'upload');
             }
         } else {
             setError(" <span><i class=\"fa fa-exclamation-triangle\"></i> cannot create the destination file", "nope");
             return false;
         }
     }
 }
Example #8
0
 /**
  * Функция конвертации 
  * xml, tsv, html, json файлов в csv
  * 
  * @param  [string] $infile  [входной файл]
  * @param  [string] $outfile [выходной файл]
  * @return [void]
  */
 public static function convertOtherFormats($infile, $pathToConvert)
 {
     //проверить существование входящего файла
     if (!file_exists($infile)) {
         throw new Exception("Входящий файл " . basename($infile) . " не существует");
     }
     $ext = File::getFileExtension($infile);
     if (!in_array($ext, ['xml', 'tsv', 'html', 'json', 'xlt', 'xls', 'xlsx', 'doc', 'docx'])) {
         throw new Exception("Файл данного формата ({$ext}) не поддерживается");
     }
     if (in_array($ext, ['doc', 'docx'])) {
         shell_exec("HOME=/var/www/new.marketrf.ru/www_system soffice --headless --convert-to html --outdir " . dirname($infile) . " {$infile}");
         $infile = substr($infile, 0, strpos($infile, "." . $ext)) . ".html";
         $ext = 'html';
     }
     $path = Yii::getPathOfAlias('core.extensions.SimpleExcel');
     require_once $path . DS . "SimpleExcel.php";
     $inFileType = SimpleExcel::identity($infile);
     $file = new SimpleExcel($inFileType);
     $file->parser->loadFile($infile);
     $slugger = new SlugBehavior();
     $infileName = File::getFileName($infile);
     $outFileName = $slugger->makeSlug(basename($infileName)) . ".csv";
     $outfile = $pathToConvert . DS . $outFileName;
     $file->convertTo('CSV');
     File::checkPermissions($pathToConvert);
     $file->writer->saveFile($outFileName, $outfile);
     unset($file);
     //удаляем временный html файл если входной был doc, docx
     if (in_array($ext, ['doc', 'docx'])) {
         unlink($infile);
     }
     return [['filename' => $outfile]];
     //тут именно так и нужно, что с другой фунцией они возвращали одинаковый результат
 }