Browse Source

Merge pull request #3 from Telaxus/master

Merge Telaxus/Epesi with jtylek/Epesi
master
Janusz Tylek 2 years ago
committed by GitHub
parent
commit
427a1325b0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 217 additions and 22 deletions
  1. +0
    -22
      composer.lock
  2. +129
    -0
      modules/Utils/Attachment/FileActionHandler.php
  3. +6
    -0
      modules/Utils/Attachment/file.php
  4. +43
    -0
      modules/Utils/Attachment/patches/20171024_use_generic_file_field.php
  5. +39
    -0
      modules/Utils/Attachment/patches/20171025_use_generic_attached_to_field.php

+ 0
- 22
composer.lock View File

@@ -2982,28 +2982,6 @@
"tput",
"window"
],
"time": "2017-05-02T12:26:19+00:00"
},
{
"name": "hoa/event",
"version": "1.17.01.13",
"source": {
"type": "git",
"url": "https://github.com/hoaproject/Event.git",
"reference": "6c0060dced212ffa3af0e34bb46624f990b29c54"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/hoaproject/Event/zipball/6c0060dced212ffa3af0e34bb46624f990b29c54",
"reference": "6c0060dced212ffa3af0e34bb46624f990b29c54",
"shasum": ""
},
"require": {
"hoa/consistency": "~1.0",
"hoa/exception": "~1.0"
},
"require-dev": {
"hoa/test": "~2.0"
},
"type": "library",
"extra": {


+ 129
- 0
modules/Utils/Attachment/FileActionHandler.php View File

@@ -0,0 +1,129 @@
<?php

require_once __DIR__ . '/../RecordBrowser/FileActionHandler.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Utils_Attachment_FileActionHandler
extends Utils_RecordBrowser_FileActionHandler
{
protected function getHandlingScript()
{
return 'modules/Utils/Attachment/file.php';
}

/**
* Get Action urls for RB file leightbox
*
* @param int $filestorageId Filestorage ID
* @param string $tab Recordset name. e.g. company
* @param int $recordId Record ID
* @param string $field Field identifier. e.g. company_name
* @param string $crypted If file is crypted or not
*
* @return array
*/
public function getActionUrlsAttachment($filestorageId, $tab, $recordId, $field, $crypted)
{
$params = ['tab' => $tab, 'record' => $recordId, 'field' => $field, 'crypted' => $crypted, 'cid' => CID];
return $this->getActionUrls($filestorageId, $params);
}
protected function hasAccess($action, $request)
{
$crypted = $request->get('crypted');
$recordId = $request->get('record');
if ($crypted && !isset($_SESSION['client']['cp'.$recordId]))
return false;
return parent::hasAccess($action, $request);
}
protected function getFile(Request $request, $disposition)
{
$filestorageId = $request->get('id');
$type = $request->get('action');
$crypted = $request->get('crypted');
$recordId = $request->get('record');
$filePack = is_array($filestorageId);
try {
$filestorageIds = is_array($filestorageId)? $filestorageId: array($filestorageId);
if ($filePack) {
$zipFilename = tempnam('tmp', 'zip');
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open($zipFilename, ZIPARCHIVE::OVERWRITE )!==true)
throw new Utils_FileStorage_Exception("cannot open $zipFilename for writing - contact with administrator");
}
$size = 0;
foreach ($filestorageIds as $filestorageId) {
$meta = Utils_FileStorageCommon::meta($filestorageId);
$buffer = Utils_FileStorageCommon::read_content($filestorageId);
if($crypted) {
$buffer = Utils_AttachmentCommon::decrypt($buffer, $_SESSION['client']['cp'.$recordId]);
if ($buffer===false) throw new Utils_FileStorage_Exception('File decryption error');
}
$size += filesize($meta['file']);
@ini_set('memory_limit',ceil($size*2/1024/1024+64).'M');
if ($filePack)
$zip->addFromString($meta['filename'], $buffer);
}
if ($filePack)
$zip->close();
} catch (Utils_FileStorage_Exception $ex) {
if (Base_AclCommon::i_am_admin()) {
return new Response($ex->getMessage(), 400);
}
return false;
}
$type = self::actions[$type];
$time = time();
foreach ($filestorageIds as $filestorageId) {
$remote_address = get_client_ip_address();
$remote_host = gethostbyaddr($remote_address);
DB::Execute('INSERT INTO utils_filestorage_access(file_id,date_accessed,accessed_by,type,ip_address,host_name) '.
'VALUES (%d,%T,%d,%d,%s,%s)',array($filestorageId,$time,Acl::get_user()?Acl::get_user():0,$type,$remote_address,$remote_host));
}
$response = new Response();
if (!$filePack) {
$mime = Utils_FileStorageCommon::get_mime_type($meta['file'], $meta['filename']);
$response->setContent($buffer);
$response->headers->set('Content-Type', $mime);
$response->headers->set('Content-Length', strlen($buffer));
$response->headers->set('Content-Disposition', "$disposition; filename=\"$meta[filename]\"");
}
else {
ob_start();
$fp = fopen($zipFilename, 'rb');
while (!feof($fp)) {
print fread($fp, 1024);
}
fclose($fp);
@unlink($zipFilename);
$buffer = ob_get_clean();
$response->setContent($buffer);
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Length', strlen($buffer));
$response->headers->set('Content-Disposition', "attachment; filename=note_".$recordId.'.zip');
$response->headers->set('Pragma', 'no-cache');
$response->headers->set('Expires', '0');
}
return $response;
}
}

+ 6
- 0
modules/Utils/Attachment/file.php View File

@@ -0,0 +1,6 @@
<?php

require_once __DIR__ . '/FileActionHandler.php';

$handler = new Utils_Attachment_FileActionHandler();
$handler->handle()->send();

+ 43
- 0
modules/Utils/Attachment/patches/20171024_use_generic_file_field.php View File

@@ -0,0 +1,43 @@
<?php

defined("_VALID_ACCESS") || die('Direct access forbidden');

$field = array('name' => _M('Files'),
'type' => 'file',
'position' => 'Note',
'required' => false,
'extra' => false,
'visible'=>false,
'QFfield_callback'=>array('Utils_AttachmentCommon','QFfield_files'),
);

Utils_RecordBrowserCommon::new_record_field('utils_attachment', $field);

$fields = Utils_RecordBrowserCommon::init('utils_attachment');
$field_key = $fields['Files']['pkey'];

$cp = Patch::checkpoint('add_file_values');
if (!$cp->is_done()) {
$last_id = $cp->get('last_id', 0);
$attachment_ids = DB::GetCol('SELECT id FROM utils_attachment_data_1 WHERE id > %d ORDER BY id ASC', [$last_id]);
foreach ($attachment_ids as $attachment_id) {
$files = DB::GetCol('SELECT id FROM utils_filestorage WHERE backref=%s', ['rb:utils_attachment/' . $attachment_id]);
if ($files) {
DB::Execute('UPDATE utils_filestorage SET backref=%s WHERE id IN (' . implode(',', array_fill(0, count($files), '%d')) . ')', array_merge(['rb:utils_attachment/' . $attachment_id . '/' . $field_key], array_values($files)));
$files = Utils_RecordBrowserCommon::encode_multi($files);
DB::Execute('UPDATE utils_attachment_data_1 SET f_files=%s WHERE id=%d', [$files, $attachment_id]);
}
$cp->set('last_id', $attachment_id);
}
$cp->done();
}

DB::DropTable('utils_attachment_download');
DB::DropTable('utils_attachment_file');
DB::DropTable('utils_attachment_clipboard');

+ 39
- 0
modules/Utils/Attachment/patches/20171025_use_generic_attached_to_field.php View File

@@ -0,0 +1,39 @@
<?php

defined("_VALID_ACCESS") || die('Direct access forbidden');

Utils_RecordBrowserCommon::delete_record_field('utils_attachment', 'Attached to');

$field = array('name' => _M('Attached to'),
'type' => 'multiselect',
'position' => 'Crypted',
'param' => '__RECORDSETS__::;',
'required' => false,
'extra' => false,
'visible'=>false,
);

Utils_RecordBrowserCommon::new_record_field('utils_attachment', $field);

$cp = Patch::checkpoint('add_attached_to_values');
if (!$cp->is_done()) {
$last_id = $cp->get('last_id', 0);
$attachment_ids = DB::GetCol('SELECT id FROM utils_attachment_data_1 WHERE id > %d ORDER BY id ASC', [$last_id]);
foreach ($attachment_ids as $attachment_id) {
$attached_to = DB::GetCol('SELECT local FROM utils_attachment_local WHERE attachment=%d', [$attachment_id]);
if ($attached_to) {
$attached_to = Utils_RecordBrowserCommon::encode_multi($attached_to);
DB::Execute('UPDATE utils_attachment_data_1 SET f_attached_to=%s WHERE id=%d', [$attached_to, $attachment_id]);
}
$cp->set('last_id', $attachment_id);
}
$cp->done();
}

DB::DropTable('utils_filestorage_local');


Loading…
Cancel
Save