2013-06-14 13:51:44 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-14 13:51:44 +02:00
|
|
|
|
2014-02-26 11:19:52 +01:00
|
|
|
use Icinga\Web\Controller\ActionController;
|
|
|
|
use Icinga\Application\Icinga;
|
2014-10-31 10:27:17 +01:00
|
|
|
use Icinga\Application\Logger;
|
2014-09-02 16:31:38 +02:00
|
|
|
use Icinga\Web\FileCache;
|
|
|
|
use Zend_Controller_Action_Exception as ActionException;
|
2013-07-12 13:45:11 +02:00
|
|
|
|
2013-09-24 12:48:30 +02:00
|
|
|
/**
|
|
|
|
* Delivery static content to clients
|
|
|
|
*/
|
2013-06-14 13:51:44 +02:00
|
|
|
class StaticController extends ActionController
|
|
|
|
{
|
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()
|
|
|
|
{
|
2014-09-02 16:31:38 +02:00
|
|
|
$cache = FileCache::instance();
|
|
|
|
$filename = md5(strtolower(trim($this->_request->getParam('email'))));
|
|
|
|
$cacheFile = 'gravatar-' . $filename;
|
|
|
|
header('Cache-Control: public');
|
|
|
|
header('Pragma: cache');
|
|
|
|
if ($etag = $cache->etagMatchesCachedFile($cacheFile)) {
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-21 18:42:01 +02:00
|
|
|
header('Content-Type: image/jpg');
|
2014-09-02 16:31:38 +02:00
|
|
|
if ($cache->has($cacheFile)) {
|
|
|
|
header('ETag: "' . $cache->etagForCachedFile($cacheFile) . '"');
|
|
|
|
$cache->send($cacheFile);
|
|
|
|
return;
|
|
|
|
}
|
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);
|
|
|
|
header('ETag: "' . $cache->etagForCachedFile($cacheFile) . '"');
|
2014-02-21 11:46:10 +01:00
|
|
|
echo $img;
|
|
|
|
}
|
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
/**
|
|
|
|
* Return an image from the application's or the module's public folder
|
|
|
|
*/
|
2013-07-12 13:45:11 +02:00
|
|
|
public function imgAction()
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$module = $this->_getParam('module_name');
|
2013-07-12 13:45:11 +02:00
|
|
|
$file = $this->_getParam('file');
|
2013-08-06 11:24:16 +02:00
|
|
|
$basedir = Icinga::app()->getModuleManager()->getModule($module)->getBaseDir();
|
2013-07-12 13:45:11 +02:00
|
|
|
|
2014-09-08 11:28:14 +02:00
|
|
|
$filePath = realpath($basedir . '/public/img/' . $file);
|
|
|
|
|
2014-09-08 12:59:22 +02:00
|
|
|
if (! $filePath || strpos($filePath, $basedir) !== 0) {
|
2013-07-12 13:45:11 +02:00
|
|
|
throw new ActionException(sprintf(
|
|
|
|
'%s does not exist',
|
|
|
|
$filePath
|
|
|
|
), 404);
|
|
|
|
}
|
|
|
|
if (preg_match('/\.([a-z]+)$/i', $file, $m)) {
|
|
|
|
$extension = $m[1];
|
|
|
|
} else {
|
|
|
|
$extension = 'fixme';
|
|
|
|
}
|
2013-09-03 18:43:17 +02:00
|
|
|
|
2014-06-20 14:53:38 +02:00
|
|
|
$s = stat($filePath);
|
2013-07-12 13:45:11 +02:00
|
|
|
header('Content-Type: image/' . $extension);
|
2014-06-20 14:53:38 +02:00
|
|
|
header(sprintf('ETag: "%x-%x-%x"', $s['ino'], $s['size'], (float) str_pad($s['mtime'], 16, '0')));
|
|
|
|
header('Cache-Control: public, max-age=3600');
|
|
|
|
header('Pragma: cache');
|
2013-07-12 13:45:11 +02:00
|
|
|
header('Last-Modified: ' . gmdate(
|
|
|
|
'D, d M Y H:i:s',
|
2014-06-20 14:53:38 +02:00
|
|
|
$s['mtime']
|
2013-07-12 13:45:11 +02:00
|
|
|
) . ' GMT');
|
|
|
|
|
|
|
|
readfile($filePath);
|
|
|
|
}
|
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
/**
|
|
|
|
* Return a javascript file from the application's or the module's public folder
|
|
|
|
*/
|
2013-06-14 13:51:44 +02:00
|
|
|
public function javascriptAction()
|
|
|
|
{
|
2013-08-06 11:24:16 +02:00
|
|
|
$module = $this->_getParam('module_name');
|
2013-07-12 13:45:11 +02:00
|
|
|
$file = $this->_getParam('file');
|
2013-08-06 11:24:16 +02:00
|
|
|
|
2013-08-20 12:00:41 +02:00
|
|
|
if ($module == 'app') {
|
|
|
|
$basedir = Icinga::app()->getApplicationDir('../public/js/icinga/components/');
|
|
|
|
$filePath = $basedir . $file;
|
|
|
|
} else {
|
|
|
|
if (!Icinga::app()->getModuleManager()->hasEnabled($module)) {
|
2013-08-20 19:47:04 +02:00
|
|
|
Logger::error(
|
|
|
|
'Non-existing frontend component "' . $module . '/' . $file
|
2014-02-26 11:19:52 +01:00
|
|
|
. '" was requested. The module "' . $module . '" does not exist or is not active.'
|
|
|
|
);
|
2013-08-20 12:00:41 +02:00
|
|
|
echo "/** Module not enabled **/";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$basedir = Icinga::app()->getModuleManager()->getModule($module)->getBaseDir();
|
|
|
|
$filePath = $basedir . '/public/js/' . $file;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2013-06-14 13:51:44 +02:00
|
|
|
|
|
|
|
if (!file_exists($filePath)) {
|
2013-08-20 19:31:29 +02:00
|
|
|
Logger::error(
|
|
|
|
'Non-existing frontend component "' . $module . '/' . $file
|
2014-02-26 11:19:52 +01:00
|
|
|
. '" was requested, which would resolve to the the path: ' . $filePath
|
|
|
|
);
|
2013-08-16 14:56:23 +02:00
|
|
|
echo '/** Module has no js files **/';
|
2013-08-06 11:53:42 +02:00
|
|
|
return;
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
|
|
|
$response = $this->getResponse();
|
2013-09-03 18:43:17 +02:00
|
|
|
$response->setHeader('Content-Type', 'text/javascript');
|
2013-09-11 17:07:33 +02:00
|
|
|
$this->setCacheHeader(3600);
|
2013-09-03 18:43:17 +02:00
|
|
|
|
2013-06-14 13:51:44 +02:00
|
|
|
$response->setHeader(
|
|
|
|
'Last-Modified',
|
|
|
|
gmdate(
|
|
|
|
'D, d M Y H:i:s',
|
|
|
|
filemtime($filePath)
|
|
|
|
) . ' GMT'
|
|
|
|
);
|
|
|
|
|
2013-09-03 18:43:17 +02:00
|
|
|
readfile($filePath);
|
2013-06-14 13:51:44 +02:00
|
|
|
}
|
2013-09-03 18:43:17 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-24 12:48:30 +02:00
|
|
|
* Set cache header for this response
|
2013-09-03 18:43:17 +02:00
|
|
|
*
|
2013-09-04 17:12:44 +02:00
|
|
|
* @param integer $maxAge The maximum age to set
|
2013-09-03 18:43:17 +02:00
|
|
|
*/
|
|
|
|
private function setCacheHeader($maxAge)
|
|
|
|
{
|
2013-10-16 12:00:09 +02:00
|
|
|
$this->_response->setHeader('Cache-Control', 'max-age=3600', true);
|
|
|
|
$this->_response->setHeader('Pragma', 'cache', true);
|
|
|
|
$this->_response->setHeader(
|
2013-09-03 18:43:17 +02:00
|
|
|
'Expires',
|
|
|
|
gmdate(
|
|
|
|
'D, d M Y H:i:s',
|
|
|
|
time()+3600
|
|
|
|
) . ' GMT',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2013-10-16 12:00:09 +02:00
|
|
|
|
|
|
|
public function stylesheetAction()
|
|
|
|
{
|
|
|
|
$lessCompiler = new \Icinga\Web\LessCompiler();
|
|
|
|
$moduleManager = Icinga::app()->getModuleManager();
|
|
|
|
|
|
|
|
$publicDir = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
|
|
|
|
|
|
|
|
$lessCompiler->addItem($publicDir . '/css/vendor');
|
|
|
|
$lessCompiler->addItem($publicDir . '/css/icinga');
|
|
|
|
|
|
|
|
foreach ($moduleManager->getLoadedModules() as $moduleName) {
|
|
|
|
$cssDir = $moduleName->getCssDir();
|
2013-10-16 14:48:22 +02:00
|
|
|
|
2013-10-16 12:00:09 +02:00
|
|
|
if (is_dir($cssDir)) {
|
|
|
|
$lessCompiler->addItem($cssDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_response->setHeader('Content-Type', 'text/css');
|
|
|
|
$this->setCacheHeader(3600);
|
|
|
|
|
|
|
|
$lessCompiler->printStack();
|
|
|
|
}
|
2013-07-12 13:45:11 +02:00
|
|
|
}
|