Files
monpas/application/controllers/shared/Upload.php
T
2021-07-10 16:25:16 +02:00

153 lines
4.5 KiB
PHP

<?php
use Pheanstalk\Pheanstalk;
class Upload extends MY_Controller {
public function __construct()
{
parent::__construct();
$models = array( "shared/Files_model");
$this->load->model($models);
if (!$this->check_online_status()) {
echo json_encode(array('offline' => TRUE));
die;
};// if
$this->load->helper("file");
}
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[rand(0, $max)];
}
return $str;
}
private function CheckFileExists($naam, $modulenr){
$exists = $this->Files_model->file_exists($naam, $modulenr, $this->globorgid);
return $exists;
}
public function doUpload()
{
$folder = $this->input->post('folderID');
$module = $this->input->post('module');
$oldDocID = $this->input->post('oldDocID');
$fn = '';
if (isset ($_FILES['upload_file'])){
$fn = $_FILES['upload_file'];
}
$this->sharedUpload($fn, $folder, $module,$oldDocID);
}
private function sharedUpload($fn, $folder, $module,$docID = false){
$newName = $this->random_str(12);
$fileExists = $this->CheckFileExists($fn['name'], $module);
// nog afvangen : file exists maar een doc id meegegeven: dan moet de file overschreven worden
$error ='';
if ($fn) {
$ext =end(explode('.', $fn['name']) );
if ($ext) $ext = '.'. $ext;
if ($fileExists) { $error=$fn['name'].": bestand is al geupload"; };
if ($error <=''){
if (move_uploaded_file( $fn['tmp_name'] , '../dossier/'.$newName ) ) {
$data = ['organisatie_id' =>$this->globorgid,
'folder_id' =>$folder,
'original_name' =>$fn['name'],
'new_name' =>$newName,
'modified' =>date('Y-m-d h:i:s'),
'owner' =>$this->userid ,
'extension' =>$ext ];
$newID = $this->Files_model->insert_record($data);
if ($newID){ $this->queueConversion($newID); }
}
};
} else{
$error .= $fn.' fout bij uploaden';
}
echo $error;
}
public function doDropUpload(){
$folder = $this->uri->segment(4);
$module = $this->uri->segment(5);
$fn = '';
$fn = $_FILES['file'];
$this->sharedUpload($fn, $folder, $module);
}
private function queueConversion( $fileID ) {
$pheanstalk = new Pheanstalk( '127.0.0.1' );
$job = new stdClass();
$job->function = 'convertDocument';
$job->user_data = $fileID;
$job_data = json_encode( $job );
$pheanstalk
->useTube( $this->beanstalkPipe )
->put( $job_data );
}
public function show_pdf(){
$fileID= $this->uri->segment( 4 );
// TODO check file toegangsrecht
$file = $this->Files_model->get(['id'=>$fileID],false,true);
if (! $file) {
$this->json('file niet gevonden');
return;
}
if (! ($file->extension == ".pdf" || $file->conversion_status == 'WORD-PDF-CONVERSION-SUCCEEDED')){
$this->json('geen pdf beschikbaar');
return;
}
if ($file->extension == ".pdf") $fullpath = $this->dossierpath;
else $fullpath = $this->pdfpath;
$fullpath = $fullpath . $file->new_name;
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/pdf' );
header( 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $fullpath) );
readfile( $fullpath );
}
public function download_file(){
//TODO check toegangsrecht
$fileID = $this->uri->segment(4);
$document = $this->Files_model->get(['id'=>$fileID],false,true);
if (! $document) {
$this->json('Document niet gevonden');
return;
}
$this->load->helper( 'download' );
$data = file_get_contents( $this->dossierpath . $document->new_name );
force_download( $document->original_name, $data );
}
public function download_bijlage(){
$orgID = $this->uri->segment(4);
$randomname = $this->Organisaties_model->get(['id'=>$orgID],'randomname', true);
$originalname = $this->Organisaties_model->get(['id'=>$orgID],'bijlagefile', true);
$this->load->helper( 'download' );
$data = file_get_contents( './../docs/' . $randomname );
force_download( $originalname, $data );
}
}