1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Doc\Controllers;
use finfo;
use SplFileInfo;
use Icinga\Application\Icinga;
use Icinga\Module\Doc\DocController;
use Icinga\Module\Doc\Exception\DocException;
use Icinga\Web\Url;
class ModuleController extends DocController
{
/**
* Get the path to a module documentation
*
* @param string $module The name of the module
* @param string $default The default path
* @param bool $suppressErrors Whether to not throw an exception if the module documentation is not available
*
* @return string|null Path to the documentation or null if the module documentation is not available
* and errors are suppressed
*
* @throws \Icinga\Exception\Http\HttpNotFoundException If the module documentation is not available and errors
* are not suppressed
*/
protected function getPath($module, $default, $suppressErrors = false)
{
if (is_dir($default)) {
return $default;
}
if (($path = $this->Config()->get('documentation', 'modules')) !== null) {
$path = str_replace('{module}', $module, $path);
if (is_dir($path)) {
return $path;
}
}
if ($suppressErrors) {
return null;
}
$this->httpNotFound($this->translate('Documentation for module \'%s\' is not available'), $module);
}
/**
* List modules which are enabled and having the 'doc' directory
*/
public function indexAction()
{
$moduleManager = Icinga::app()->getModuleManager();
$modules = array();
foreach ($moduleManager->listInstalledModules() as $module) {
$path = $this->getPath($module, $moduleManager->getModuleDir($module, '/doc'), true);
if ($path !== null) {
$modules[] = $moduleManager->getModule($module, false);
}
}
$this->view->modules = $modules;
$this->getTabs()->add('module-documentation', array(
'active' => true,
'title' => $this->translate('Module Documentation', 'Tab title'),
'url' => Url::fromRequest()
));
}
/**
* Assert that the given module is installed
*
* @param string $moduleName
*
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
*/
protected function assertModuleInstalled($moduleName)
{
$moduleManager = Icinga::app()->getModuleManager();
if (! $moduleManager->hasInstalled($moduleName)) {
$this->httpNotFound($this->translate('Module \'%s\' is not installed'), $moduleName);
}
}
/**
* View the toc of a module's documentation
*
* @throws \Icinga\Exception\MissingParameterException If the required parameter 'moduleName' is empty
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
* @see assertModuleInstalled()
*/
public function tocAction()
{
$module = $this->params->getRequired('moduleName');
$this->assertModuleInstalled($module);
$moduleManager = Icinga::app()->getModuleManager();
$name = $moduleManager->getModule($module, false)->getTitle();
try {
$this->renderToc(
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$name,
'doc/module/chapter',
array('moduleName' => $module)
);
} catch (DocException $e) {
$this->httpNotFound($e->getMessage());
}
}
/**
* View a chapter of a module's documentation
*
* @throws \Icinga\Exception\MissingParameterException If one of the required parameters 'moduleName' and
* 'chapter' is empty
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
* @see assertModuleInstalled()
*/
public function chapterAction()
{
$module = $this->params->getRequired('moduleName');
$this->assertModuleInstalled($module);
$chapter = $this->params->getRequired('chapter');
$this->view->moduleName = $module;
try {
$this->renderChapter(
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$chapter,
'doc/module/chapter',
'doc/module/img',
array('moduleName' => $module)
);
} catch (DocException $e) {
$this->httpNotFound($e->getMessage());
}
}
/**
* Deliver images
*/
public function imageAction()
{
$module = $this->params->getRequired('moduleName');
$image = $this->params->getRequired('image');
$docPath = $this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc'));
$imagePath = realpath($docPath . '/' . $image);
if ($imagePath === false || substr($imagePath, 0, strlen($docPath)) !== $docPath) {
$this->httpNotFound('%s does not exist', $image);
}
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout()->disableLayout();
$imageInfo = new SplFileInfo($imagePath);
$etag = md5($imageInfo->getMTime() . $imagePath);
$lastModified = gmdate('D, d M Y H:i:s T', $imageInfo->getMTime());
$match = false;
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
$ifNoneMatch = explode(', ', stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
foreach ($ifNoneMatch as $tag) {
if ($tag === $etag) {
$match = true;
break;
}
}
} elseif (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$lastModifiedSince = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
if ($lastModifiedSince === $lastModified) {
$match = true;
}
}
$this->getResponse()
->setHeader('ETag', $etag)
->setHeader('Cache-Control', 'no-transform,public,max-age=3600,must-revalidate', true);
if ($match) {
$this->getResponse()->setHttpResponseCode(304);
} else {
$finfo = new finfo();
$this->getResponse()
->setHeader('Content-Type', $finfo->file($imagePath, FILEINFO_MIME_TYPE))
->setHeader('Content-Length', $imageInfo->getSize())
->sendHeaders();
ob_end_clean();
readfile($imagePath);
}
}
/**
* View a module's documentation as PDF
*
* @throws \Icinga\Exception\MissingParameterException If the required parameter 'moduleName' is empty
* @throws \Icinga\Exception\Http\HttpNotFoundException If the given module is not installed
* @see assertModuleInstalled()
*/
public function pdfAction()
{
$module = $this->params->getRequired('moduleName');
$this->assertModuleInstalled($module);
$this->renderPdf(
$this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$module,
'doc/module/chapter',
array('moduleName' => $module)
);
}
}
|