Example #1
0
 public function ajaxUpload($autosave = false, $size = false)
 {
     if (!$this->image || !$this->field) {
         return true;
     }
     //только для случаев, когда модель имеет одну картинку
     $file = CUploadedFile::getInstance($this->owner, $this->image);
     if ($file == null) {
         //для случаев если uploader был не в cactiveform
         $file = CUploadedFile::getInstanceByName(get_class($this->owner) . '_' . $this->image);
     }
     if (is_object($file) && get_class($file) === 'CUploadedFile') {
         $filename = time();
         //аплоад, во временную папку
         $tmp = param('upload_tmp_folder') . $filename . '.' . $file->getExtensionName();
         File::checkPermissions(param('upload_tmp_folder'));
         $file->saveAs($tmp);
         $photo = new Photo();
         $photo->filename = $filename;
         $imgId = $photo->uploadImage($tmp, $this->params);
         if ($imgId) {
             if ($this->owner->{$this->relation}) {
                 //если фотография раньше была
                 $photo = $this->owner->{$this->relation};
                 $photo->delete();
             }
             //связка модели с фотографией
             $this->owner->{$this->field} = $imgId;
             if (!$this->owner->isNewRecord && $autosave) {
                 $this->owner->saveAttributes(array($this->field));
             }
             //удаление из временой папки
             @unlink($tmp);
             if ($size) {
                 return Yii::app()->baseUrl . DS . $this->params['path'] . $size . DS . $filename . '.' . $file->getExtensionName() . '?' . time();
             }
         }
     }
 }
Example #2
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 #3
0
 /**
  *
  * get and post actions (router)
  *
  */
 public function actions()
 {
     // no read permissions?
     if (!File::checkPermissions('r')) {
         File::writeLog('auth bad - no read access');
         File::error("Access Forbidden");
         die;
     }
     // POST actions
     if (isset($_POST['action'])) {
         $action = $_POST['action'];
         unset($_POST['action']);
         // actions with read & write permissions
         if (File::checkPermissions('rw')) {
             switch ($action) {
                 case 'delete':
                     foreach ($_POST as $post_file) {
                         if (in_array($post_file, Config::get('restricted_files'))) {
                             continue;
                         }
                         $files[] = $this->filterInput($this->decrypt($post_file));
                     }
                     $this->deleteFiles($files, $_SESSION['cwd']);
                     break;
                 case 'rename':
                     if (!isset($_POST['oldname']) || !isset($_POST['newname'])) {
                         break;
                     }
                     $oldname = $this->filterInput($this->decrypt($_POST['oldname']));
                     $newname = $this->filterInput($_POST['newname']);
                     if (in_array($oldname, Config::get('restricted_files')) || in_array($newname, Config::get('restricted_files'))) {
                         break;
                     }
                     $this->renameFile($oldname, $newname);
                     break;
                 case 'edit-save':
                     if (!isset($_POST['filename'])) {
                         break;
                     }
                     $filename = $this->filterInput($this->decrypt($_POST['filename']));
                     $content = $_POST['content'];
                     if (in_array($filename, Config::get('restricted_files'))) {
                         break;
                     }
                     file_put_contents($_SESSION['cwd'] . DS . $filename, $content);
                     File::writeLog('edit file / save - ' . $filename);
                     break;
                 case 'zip':
                     if (!isset($_POST['archivename'])) {
                         break;
                     }
                     $archive_name = $this->filterInput($_POST['archivename']);
                     unset($_POST['archivename']);
                     foreach ($_POST as $post_file) {
                         $files[] = $this->filterInput($this->decrypt($post_file));
                     }
                     $this->zipFiles($files, $archive_name);
                     break;
                 case 'unzip':
                     if (!isset($_POST['filename'])) {
                         break;
                     }
                     $filename = $this->filterInput($this->decrypt($_POST['filename']));
                     $this->unzipFile($filename);
                     break;
                 case 'simple-copy':
                 case 'simple-move':
                     // link to home dir is blank
                     if (!isset($_POST['destination'])) {
                         $_POST['destination'] = '';
                     }
                     $destination = $this->filterInput($this->decrypt($_POST['destination']), false);
                     $destination = rawurldecode($destination);
                     unset($_POST['destination']);
                     foreach ($_POST as $post_file) {
                         $files[] = $this->filterInput($this->decrypt($post_file));
                     }
                     if ($action == 'simple-copy') {
                         $this->copyFiles($files, $_SESSION['cwd'], $this->_repository . DS . $destination);
                     }
                     if ($action == 'simple-move') {
                         $this->moveFiles($files, $_SESSION['cwd'], $this->_repository . DS . $destination);
                     }
                     break;
                 default:
                     break;
             }
         }
         // flush url
         header('Location: /TCS/course');
         die;
     }
     //
     // GET actions
     //
     // download file
     if (isset($_GET['download']) && !empty($_GET['download'])) {
         $filename = $this->filterInput($this->decrypt($_GET['download']));
         if (in_array($filename, Config::get('restricted_files'))) {
             die;
         }
         if (!file_exists($_SESSION['cwd'] . DS . $filename)) {
             die;
         }
         // Set headers
         header("Cache-Control: public");
         header("Content-Description: File Transfer");
         header("Content-Disposition: attachment; filename=\"{$filename}\"");
         header("Content-Type: application/octet-stream");
         header("Content-Transfer-Encoding: binary");
         // output file
         set_time_limit(0);
         $file = @fopen($_SESSION['cwd'] . DS . $filename, "rb");
         while (!feof($file)) {
             print @fread($file, 1024 * 8);
             ob_flush();
             flush();
         }
         File::writeLog('download - ' . $filename);
         die;
     }
     // edit action - load file content via this ajax
     if (isset($_GET['edit-load']) && File::checkPermissions('rw')) {
         $filename = $this->filterInput($this->decrypt($_GET['edit-load']));
         if (in_array($filename, Config::get('restricted_files'))) {
             die;
         }
         if (!file_exists($_SESSION['cwd'] . DS . $filename)) {
             die;
         }
         echo file_get_contents($_SESSION['cwd'] . DS . $filename);
         File::writeLog('edit file / load - ' . $filename);
         die;
     }
     // new folder / new file
     if ((isset($_GET['newdir']) || isset($_GET['newfile'])) && self::$_role != 'Student') {
         $newdir = $newfile = '';
         if (isset($_GET['newdir']) && $_GET['newdir'] != '') {
             $newdir = $this->filterInput($_GET['newdir']);
             if (!in_array($newdir, Config::get('restricted_files'))) {
                 $this->newFolder($_SESSION['cwd'] . DS . $newdir);
             }
         } elseif (isset($_GET['newfile']) && $_GET['newfile'] != '') {
             $newfile = $this->filterInput($_GET['newfile']);
             if (!in_array($newfile, Config::get('restricted_files'))) {
                 touch($_SESSION['cwd'] . DS . $newfile);
             }
         }
         File::writeLog('create new - ' . $newdir . $newfile);
         // flush url
         header('Location: ' . Config::get('base_url') . '/' . $_GET['page']);
         die;
     }
     // directory tree - ajax load
     if (isset($_GET['tree']) || !empty($_GET['tree'])) {
         $tree_action = $this->filterInput($_GET['tree']);
         $dirs = '';
         if ($tree_action == 'cd') {
             $dirs = $this->getDirectoryTree($this->_repository, false, '/cd/');
         }
         if ($tree_action == 'copy' || $tree_action == 'move') {
             $dirs = $this->getDirectoryTree($this->_repository, true, '');
         }
         echo $dirs;
         File::writeLog('tree load');
         die;
     }
     return;
 }
Example #4
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]];
     //тут именно так и нужно, что с другой фунцией они возвращали одинаковый результат
 }