ACC SHELL
<?php
class FilesController extends AppController
{
var $uses = 'File';
var $name = 'Files';
var $components = array('Pagination', 'Filter');
var $helpers = array('Pagination', 'Filter');
/**
*
*/
function beforeFilter()
{
if (! empty($this->params['admin'])) {
if (! $this->Session->read('logged_user')) {
$this->Session->write('redir_to', '/admin/files/index');
$this->redirect('/admin/login');
exit();
}
}
}
/**
*
*/
function file()
{
if (isset($_SERVER['HTTP_RANGE'])) {
@ob_end_clean();
header('HTTP/1.0 304 Not Modified');
header('Date: ' . date('r'));
header('Connection: close');
exit();
}
if (empty($this->params['id'])) {
$this->redirect('/');
exit();
}
if (! ($file = $this->File->findById((int) $this->params['id']))) {
$this->redirect('/');
exit();
}
// The file is public, send it
if ($file['File']['public']) {
$this->sendFile($file);
}
// Client is logged as admin so send the file
if ($this->Session->read('logged_user')) {
$this->sendFile($file);
}
// Client isn't logged in any way, redirect him to login page
if (empty($_SESSION['logged_customer'])) {
$_SESSION['redir_to'] = '/' . $this->params['url']['url'];
$this->redirect('/login');
exit();
}
$this->sendFile($file);
}
/**
*
*/
function sendFile($file)
{
@session_write_close();
@ob_end_clean();
header('Content-Type: ' . $file['File']['content_type']);
header('Content-Disposition: '
. (! strncasecmp('image/', $file['File']['content_type'], 6)
? 'inline' : 'attachment')
. '; filename="' . $file['File']['file_name'] . '"');
if (! empty($this->params['thumb'])) {
$mt = filemtime(sprintf('../data/files/thumbs/%04d', $file['File']['id']));
header('Last-Modified: ' . date('r', $mt));
header('Content-Length: ' .
filesize(sprintf('../data/files/thumbs/%04d', $file['File']['id'])));
$fname = sprintf('../data/files/thumbs/%04d', $file['File']['id']);
}
else {
$mt = filemtime(sprintf('../data/files/%04d', $file['File']['id']));
header('Last-Modified: ' . date('r', $mt));
header('Content-Length: ' . $file['File']['file_size']);
$fname = sprintf('../data/files/%04d', $file['File']['id']);
}
$f = fopen($fname, 'rb');
while ($buf = fread($f, 65536)) {
echo $buf;
@flush();
}
fclose($f);
exit;
}
/* Admin methods */
/**
*
*/
function admin_gallery()
{
$this->Pagination->controller = &$this;
$this->Pagination->show = 30;
$this->Pagination->ident = 'gallery';
$this->Pagination->init(
'content_type like \'image/%\'',
'File',
NULL,
array(),
0
);
$this->set('files', $this->File->findAll('content_type like \'image/%\'', NULL,
$this->Pagination->order,
$this->Pagination->show,
$this->Pagination->page
));
$this->render('admin_gallery', 'simple');
}
/**
*
*/
function admin_index()
{
$this->File->recursive = 0;
$this->Filter->init($this);
$this->Filter->setFilter(aa('file_name', 'Soubor'), NULL, a('~', '!~'));
$this->Filter->setFilter(aa('public', 'Veřejný'), array('Ne', 'Ano'), a('='));
$this->Filter->filter($f, $cond);
$this->set('filters', $f);
$this->Pagination->controller = &$this;
$this->Pagination->show = 30;
$this->Pagination->init(
$cond, 'File', NULL, array('file_name','id'), 0
);
$this->set('files', $this->File->findAll($cond, NULL,
$this->Pagination->order,
$this->Pagination->show,
$this->Pagination->page
));
}
function admin_add()
{
$up = 0;
foreach ($_FILES as $file) {
if ($file['error'] || ! $file['size']) {
continue;
}
$up++;
if (! preg_match('/^image.*jpe?g|png/i', $file['type'])) {
$this->File->id = false;
$this->File->save(array(
'file_name' => $file['name'],
'file_type' => $file['type'],
'file_size' => $file['size'],
'content_type' => $file['type'],
'public' => 0,
));
move_uploaded_file($file['tmp_name'], sprintf('../data/files/%04d',
$this->File->id));
chmod(sprintf('../data/files/%04d', $this->File->id), 0644);
continue;
}
$nfo = getimagesize($file['tmp_name']);
$this->File->id = false;
$this->File->save(array(
'file_name' => $file['name'],
'file_type' => $file['type'],
'file_size' => $file['size'],
'content_type' => $file['type'],
'dims' => "{$nfo[0]}x{$nfo[1]}",
'public' => 1,
));
move_uploaded_file($file['tmp_name'], sprintf('../data/files/%04d',
$this->File->id));
chmod(sprintf('../data/files/%04d', $this->File->id), 0644);
if (preg_match('/jpe?g/', $file['type'])) {
$im = imagecreatefromjpeg(sprintf('../data/files/%04d',
$this->File->id));
}
else {
$im = imagecreatefrompng(sprintf('../data/files/%04d',
$this->File->id));
}
$x = imagesx($im);
$y = imagesy($im);
if ($x > 100) {
$nx = 100;
$ny = $y / ($x / $nx);
$im2 = imagecreatetruecolor($nx, $ny);
imagecopyresampled($im2, $im, 0, 0, 0, 0, $nx, $ny, $x, $y);
if (preg_match('/jpe?g/', $file['type'])) {
imagejpeg($im2, sprintf('../data/files/thumbs/%04d',
$this->File->id));
}
else {
imagepng($im2, sprintf('../data/files/thumbs/%04d',
$this->File->id));
}
imagedestroy($im2);
}
else {
if (preg_match('/jpe?g/', $file['type'])) {
imagejpeg($im, sprintf('../data/files/thumbs/%04d',
$this->File->id));
}
else {
imagepng($im, sprintf('../data/files/thumbs/%04d',
$this->File->id));
}
}
imagedestroy($im);
chmod(sprintf('../data/files/thumbs/%04d', $this->File->id), 0644);
}
if ($up) {
$this->redirect('/admin/files');
}
}
function admin_view($id = NULL)
{
if ((int) $id) {
if ($file = $this->File->findById((int) $id)) {
$this->sendFile($file);
}
}
$this->redirect('/admin/files/index');
}
function admin_delete($id) {
$this->File->del($id);
@unlink(sprintf('../data/files/thumbs/%04d', $id));
@unlink(sprintf('../data/files/%04d', $id));
$this->redirect('/admin/files/index');
}
/**
*
*/
function admin_update()
{
$ids = join(',', array_map('intval', explode(',',
$this->params['form']['public_ids'])));
$this->File->query("
UPDATE {$this->File->table} SET public = 0 WHERE id IN ($ids)
");
if (! empty($this->params['form']['public'])) {
$ids = join(',', array_map('intval', array_keys($this->params['form']['public'])));
$this->File->query("
UPDATE {$this->File->table} SET public = 1 WHERE id IN ($ids)
");
}
$this->redirect('/admin/files/index');
}
}
?>
ACC SHELL 2018