cmc-sales/php/app/controllers/email_attachments_controller.php

94 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2009-02-09 14:25:49 -08:00
<?php
class EmailAttachmentsController extends AppController {
2012-11-07 03:55:24 -08:00
var $name = 'EmailAttachments';
var $helpers = array('Html', 'Form');
2009-02-09 14:25:49 -08:00
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
function download($id) {
// maybe make this GET a go webservice that's something like
// GET vault.cmctechnologies.com.au/filename
// HTTP basic auth, or some sort of preshared key
// service hits S3 if required. Cached on disk for $sometime
2025-11-24 02:59:54 -08:00
2012-11-07 03:55:24 -08:00
$file = $this->EmailAttachment->findById($id);
2011-03-09 23:18:26 -08:00
2025-11-24 02:59:54 -08:00
// Try legacy emails directory first (where vault saves files)
$file_path = '/var/www/emails';
$full_path = $file_path."/".$file['EmailAttachment']['name'];
// Fallback to attachments directory if not found in emails
if(!file_exists($full_path)) {
$file_path = Configure::read('attachments_directory');
$full_path = $file_path."/".$file['EmailAttachment']['name'];
}
if(file_exists($full_path)) {
2011-03-09 23:18:26 -08:00
2019-07-03 23:25:10 -07:00
2012-11-07 03:55:24 -08:00
Configure::write('debug', 0);
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
if(!$file['EmailAttachment']['filename']) {
$filename = 'vault_'.time();
}
else {
$filename = $file['EmailAttachment']['filename'];
}
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
header('Content-type: ' . $file['EmailAttachment']['type']);
2019-07-03 23:53:39 -07:00
header('Content-length: ' . $file['EmailAttachment']['size']);
2012-11-07 03:55:24 -08:00
header('Content-Disposition: attachment; filename='.$filename);
2011-03-09 23:18:26 -08:00
2025-11-24 02:59:54 -08:00
readfile($full_path);
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
exit();
2010-04-26 23:49:20 -07:00
}
2012-11-07 03:55:24 -08:00
else {
2025-11-24 02:59:54 -08:00
echo "ERROR: File Not Found";
echo '\n';
2012-11-07 03:55:24 -08:00
echo $file['EmailAttachment']['filename'];
die();
}
}
2009-02-09 14:25:49 -08:00
2012-11-07 03:55:24 -08:00
function view($id = null) {
Configure::write('debug', 0);
$this->layout = 'minimal';
if(!$id) {
return;
}
else {
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
$file = $this->EmailAttachment->find('first', array('conditions'=>array('EmailAttachment.id'=>$id)));
//$this->set('attachment', $file);
2011-03-09 23:18:26 -08:00
2025-11-24 02:59:54 -08:00
// Try legacy emails directory first (where vault saves files)
$file_path = '/var/www/emails';
$full_path = $file_path."/".$file['EmailAttachment']['name'];
// Fallback to attachments directory if not found in emails
if(!file_exists($full_path)) {
$file_path = Configure::read('attachments_directory');
$full_path = $file_path."/".$file['EmailAttachment']['name'];
}
2011-03-09 23:18:26 -08:00
2025-11-24 02:59:54 -08:00
$contents = file_get_contents($full_path);
2012-11-07 03:55:24 -08:00
if($file['EmailAttachment']['type'] == 'text/plain') {
$contents = nl2br($contents, true);
}
2011-03-09 23:18:26 -08:00
2012-11-07 03:55:24 -08:00
$this->set('contents', $contents);
2011-03-09 23:18:26 -08:00
}
2012-11-07 03:55:24 -08:00
}
2011-03-09 23:18:26 -08:00
2009-02-09 14:25:49 -08:00
}