2013-06-14 13:51:44 +02:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2015-08-27 14:08:12 +02:00
|
|
|
namespace Icinga\Controllers;
|
|
|
|
|
2014-02-26 11:19:52 +01:00
|
|
|
use Icinga\Application\Icinga;
|
2017-03-09 09:20:03 +01:00
|
|
|
use Icinga\Web\Controller;
|
2014-09-02 16:31:38 +02:00
|
|
|
use Icinga\Web\FileCache;
|
2013-07-12 13:45:11 +02:00
|
|
|
|
2013-09-24 12:48:30 +02:00
|
|
|
/**
|
2017-03-09 09:20:03 +01:00
|
|
|
* Deliver static content to clients
|
2013-09-24 12:48:30 +02:00
|
|
|
*/
|
2015-08-27 14:07:00 +02:00
|
|
|
class StaticController extends Controller
|
2013-06-14 13:51:44 +02:00
|
|
|
{
|
2013-08-16 14:56:23 +02:00
|
|
|
/**
|
2013-08-30 15:50:49 +02:00
|
|
|
* Static routes don't require authentication
|
|
|
|
*
|
|
|
|
* @var bool
|
2013-08-16 14:56:23 +02:00
|
|
|
*/
|
2013-08-30 15:50:49 +02:00
|
|
|
protected $requiresAuthentication = false;
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
/**
|
|
|
|
* Disable layout rendering as this controller doesn't provide any html layouts
|
|
|
|
*/
|
2013-06-14 13:51:44 +02:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->_helper->viewRenderer->setNoRender(true);
|
|
|
|
$this->_helper->layout()->disableLayout();
|
|
|
|
}
|
|
|
|
|
2014-02-21 11:46:10 +01:00
|
|
|
public function gravatarAction()
|
|
|
|
{
|
2017-03-09 09:20:03 +01:00
|
|
|
$response = $this->getResponse();
|
2017-08-10 13:44:35 +02:00
|
|
|
$response->setHeader('Cache-Control', 'public, max-age=1814400, stale-while-revalidate=604800', true);
|
|
|
|
|
|
|
|
$noCache = $this->getRequest()->getHeader('Cache-Control') === 'no-cache'
|
|
|
|
|| $this->getRequest()->getHeader('Pragma') === 'no-cache';
|
2017-03-09 09:20:03 +01:00
|
|
|
|
2014-09-02 16:31:38 +02:00
|
|
|
$cache = FileCache::instance();
|
2017-03-09 09:20:03 +01:00
|
|
|
$filename = md5(strtolower(trim($this->getParam('email'))));
|
2014-09-02 16:31:38 +02:00
|
|
|
$cacheFile = 'gravatar-' . $filename;
|
|
|
|
|
2017-08-10 13:44:35 +02:00
|
|
|
if (! $noCache && $cache->has($cacheFile, time() - 1814400)) {
|
|
|
|
if ($cache->etagMatchesCachedFile($cacheFile)) {
|
|
|
|
$response->setHttpResponseCode(304);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setHeader('Content-Type', 'image/jpg', true);
|
2017-03-09 09:20:03 +01:00
|
|
|
$response->setHeader('ETag', sprintf('"%s"', $cache->etagForCachedFile($cacheFile)));
|
2014-09-02 16:31:38 +02:00
|
|
|
$cache->send($cacheFile);
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 09:20:03 +01:00
|
|
|
|
2014-09-05 12:00:29 +02:00
|
|
|
$img = file_get_contents('http://www.gravatar.com/avatar/' . $filename . '?s=120&d=mm');
|
2014-09-02 16:31:38 +02:00
|
|
|
$cache->store($cacheFile, $img);
|
2017-03-09 09:20:03 +01:00
|
|
|
$response->setHeader('ETag', sprintf('"%s"', $cache->etagForCachedFile($cacheFile)));
|
|
|
|
|
2014-02-21 11:46:10 +01:00
|
|
|
echo $img;
|
|
|
|
}
|
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
/**
|
2017-03-09 09:20:03 +01:00
|
|
|
* Return an image from a module's public folder
|
2013-08-30 15:50:49 +02:00
|
|
|
*/
|
2013-07-12 13:45:11 +02:00
|
|
|
public function imgAction()
|
|
|
|
{
|
2017-03-09 09:20:03 +01:00
|
|
|
$moduleRoot = Icinga::app()
|
|
|
|
->getModuleManager()
|
|
|
|
->getModule($this->getParam('module_name'))
|
|
|
|
->getBaseDir();
|
2013-07-12 13:45:11 +02:00
|
|
|
|
2017-03-09 09:20:03 +01:00
|
|
|
$file = $this->getParam('file');
|
|
|
|
$filePath = realpath($moduleRoot . '/public/img/' . $file);
|
2014-09-08 11:28:14 +02:00
|
|
|
|
2015-08-28 13:14:33 +02:00
|
|
|
if ($filePath === false) {
|
2015-08-27 14:07:46 +02:00
|
|
|
$this->httpNotFound('%s does not exist', $filePath);
|
2013-07-12 13:45:11 +02:00
|
|
|
}
|
2017-03-09 09:20:03 +01:00
|
|
|
|
2013-07-12 13:45:11 +02:00
|
|
|
if (preg_match('/\.([a-z]+)$/i', $file, $m)) {
|
|
|
|
$extension = $m[1];
|
2015-12-07 14:18:57 +01:00
|
|
|
if ($extension === 'svg') {
|
|
|
|
$extension = 'svg+xml';
|
|
|
|
}
|
2013-07-12 13:45:11 +02:00
|
|
|
} else {
|
|
|
|
$extension = 'fixme';
|
|
|
|
}
|
2013-09-03 18:43:17 +02:00
|
|
|
|
2014-06-20 14:53:38 +02:00
|
|
|
$s = stat($filePath);
|
2017-03-09 09:20:03 +01:00
|
|
|
$this->getResponse()
|
|
|
|
->setHeader('Pragma', 'cache')
|
|
|
|
->setHeader('Content-Type', 'image/' . $extension)
|
|
|
|
->setHeader('Cache-Control', 'public, max-age=3600')
|
|
|
|
->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', $s['mtime']) . ' GMT')
|
|
|
|
->setHeader('ETag', sprintf('%x-%x-%x', $s['ino'], $s['size'], (float) str_pad($s['mtime'], 16, '0')));
|
2013-07-12 13:45:11 +02:00
|
|
|
|
|
|
|
readfile($filePath);
|
|
|
|
}
|
|
|
|
}
|