Rename Pdf/File to File/Pdf and adapt usage in the ActionController

This commit is contained in:
Eric Lippmann 2013-07-12 11:58:58 +02:00
parent 63a8854f40
commit 58ce815361
2 changed files with 97 additions and 100 deletions

View File

@ -1,14 +1,18 @@
<?php
namespace Icinga\Pdf;
namespace Icinga\File;
use TCPDF;
use Icinga\Web\Url;
use Icinga\Application\Icinga;
// $_SERVER['DOCUMENT_ROOT'] = '/';
$_SERVER['DOCUMENT_ROOT'] = Icinga::app()->getApplicationDir() . '/../public';
define('K_TCPDF_EXTERNAL_CONFIG', true);
//define('K_PATH_URL', 'http://net-test-icinga-vm1.adm.netways.de/develop/'); // ???
// define('K_PATH_URL', '/var/www/net-test-icinga-vm1.adm.netways.de/develop/public'); // ???
define('K_PATH_URL', '/develop'); // ???
define('K_PATH_URL', (string) Url::create('/') === '/' ? '' : (string) Url::create('/')); // ???'/'));
define('K_PATH_MAIN', dirname(ICINGA_LIBDIR) . '/public');
define('K_PATH_FONTS', ICINGA_LIBDIR . '/vendor/tcpdf/fonts/');
define('K_PATH_CACHE', ICINGA_LIBDIR . '/vendor/tcpdf/cache/');
@ -19,13 +23,13 @@ define('K_BLANK_IMAGE', K_PATH_IMAGES.'_blank.png'); // COULD be anything?
// define('K_CELL_HEIGHT_RATIO', 1.25);
define('K_SMALL_RATIO', 2/3);
define('K_TCPDF_CALLS_IN_HTML', true); // SECURITY: is false better?
define('K_TCPDF_CALLS_IN_HTML', false); // SECURITY: is false better?
define('K_TCPDF_THROW_EXCEPTION_ERROR', true);
define('K_THAI_TOPCHARS', false);
require_once 'vendor/tcpdf/tcpdf.php';
class File extends TCPDF
class Pdf extends TCPDF
{
protected $cell_height_ratio = 1.25;
public function __construct(

View File

@ -1,35 +1,13 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* Icinga 2 Web - Head for multiple monitoring frontends
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web;
use Icinga\Authentication\Manager;
use Icinga\Authentication\Manager as AuthManager;
use Icinga\Application\Benchmark;
use Icinga\Exception;
use Icinga\Application\Config;
use Icinga\Pdf\File;
use Icinga\Web\Notification;
use Zend_Layout as ZfLayout;
use Zend_Controller_Action as ZfController;
@ -37,6 +15,11 @@ use Zend_Controller_Request_Abstract as ZfRequest;
use Zend_Controller_Response_Abstract as ZfResponse;
use Zend_Controller_Action_HelperBroker as ZfActionHelper;
/*
* @TODO(el): There was a note from tg that the following line is temporary. Ask him why.
*/
use Icinga\File\Pdf;
/**
* Base class for all core action controllers
*
@ -45,21 +28,16 @@ use Zend_Controller_Action_HelperBroker as ZfActionHelper;
* @copyright Copyright (c) 2013 Icinga-Web Team <info@icinga.org>
* @author Icinga-Web Team <info@icinga.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @package Icinga\Web
*/
class ActionController extends ZfController
{
/**
* The Icinga Config object is available in all controllers. This is the
* modules config for module action controllers.
*
* @var Config
*/
protected $config;
/**
* @var bool
*/
protected $replaceLayout = false;
/**
@ -84,20 +62,12 @@ class ActionController extends ZfController
*/
protected $action_name;
/**
* @var bool
*/
// @TODO(el): Should be true, is/was disabled for testing purpose
protected $handlesAuthentication = false;
/**
* @var bool
*/
protected $modifiesSession = false;
/**
* @var bool
*/
protected $allowAccess = false;
private $allowAccess = false;
/**
* The constructor starts benchmarking, loads the configuration and sets
@ -113,6 +83,7 @@ class ActionController extends ZfController
array $invokeArgs = array()
) {
Benchmark::measure('Action::__construct()');
$this->module_name = $request->getModuleName();
$this->controller_name = $request->getControllerName();
$this->action_name = $request->getActionName();
@ -123,20 +94,11 @@ class ActionController extends ZfController
->_setInvokeArgs($invokeArgs);
$this->_helper = new ZfActionHelper($this);
/*
* --------------------------------------------
* Authentication is disabled to test bootstrap
* --------------------------------------------
*
* @todo remove this!
*/
if ($this->handlesAuthentication() ||
Manager::getInstance(
AuthManager::getInstance(
null,
array(
"writeSession" => $this->modifiesSession
'writeSession' => $this->modifiesSession
)
)->isAuthenticated()
) {
@ -171,7 +133,7 @@ class ActionController extends ZfController
* Helper function creating a new widget
*
* @param string $name The widget name
* @param array|string $properties Optional widget properties
* @param string $properties Optional widget properties
*
* @return Widget\AbstractWidget
*/
@ -185,9 +147,8 @@ class ActionController extends ZfController
*
* TODO: This has not been implemented yet
*
* @param $uri
* @param string $permission Permission name
* @internal param string $object No idea what this should have been :-)
* @param string $object No idea what this should have been :-)
*
* @return bool
*/
@ -204,18 +165,31 @@ class ActionController extends ZfController
* @param string $permission Permission name
* @param string $object No idea what this should have been :-)
*
* @throws \Exception
* @return self
*/
final protected function assertPermission($permission, $object = null)
{
if (!$this->hasPermission($permission, $object)) {
if (! $this->hasPermission($permission, $object)) {
// TODO: Log violation, create dedicated Exception class
throw new \Exception('Permission denied');
}
return $this;
}
protected function preserve($key, $value = null)
{
if ($value === null) {
$value = $this->_getParam($key);
}
if ($value !== null) {
if (! isset($this->view->preserve)) {
$this->view->preserve = array();
}
$this->view->preserve[$key] = $value;
}
return $this;
}
/**
* Our benchmark wants to know when we started our dispatch loop
*
@ -224,8 +198,7 @@ class ActionController extends ZfController
public function preDispatch()
{
Benchmark::measure('Action::preDispatch()');
if (!$this->allowAccess) {
if (! $this->allowAccess) {
$this->_request->setModuleName('default')
->setControllerName('authentication')
->setActionName('login')
@ -237,21 +210,27 @@ class ActionController extends ZfController
$this->view->controller_name = $this->controller_name;
$this->view->module_name = $this->module_name;
// TODO(el): What is this, why do we need that here?
$this->view->compact = $this->_request->getParam('view') === 'compact';
Benchmark::measure(sprintf(
'Action::preDispatched(): %s / %s / %s',
$this->module_name,
$this->controller_name,
$this->action_name
));
//$this->quickRedirect('/authentication/login?a=e');
}
/**
* @param $url
* @param array $params
*/
public function redirectNow($url, array $params = array())
{
if ($url instanceof Url) {
$url = $url->getRelative();
}
$this->_helper->Redirector->gotoUrlAndExit($url);
}
/**
* @return bool
*/
public function handlesAuthentication()
{
return $this->handlesAuthentication;
@ -284,7 +263,6 @@ class ActionController extends ZfController
{
Benchmark::measure('Action::postDispatch()');
// TODO: Move this elsewhere, this is just an ugly test:
if ($this->_request->getParam('filetype') === 'pdf') {
@ -296,8 +274,8 @@ class ActionController extends ZfController
// TODO: We need a way to retrieve public dir, even if located elsewhere
$css = $less->compileFile($cssdir . '/pdfprint.less');
/*
foreach ($app->moduleManager()->getLoadedModules() as $name => $module) {
/*
foreach (\Icinga\Application\Icinga::app()->moduleManager()->getLoadedModules() as $name => $module) {
if ($module->hasCss()) {
$css .= $less->compile(
'.icinga-module.module-'
@ -308,8 +286,8 @@ class ActionController extends ZfController
);
}
}
*/
*/
// END of CSS test
$this->render(
@ -317,16 +295,32 @@ class ActionController extends ZfController
$this->_helper->viewRenderer->getResponseSegment(),
$this->_helper->viewRenderer->getNoController()
);
$html = '<style>' . $css . '</style>' . (string)$this->getResponse();
$html = (string) $this->getResponse();
if ($this->module_name !== null) {
$html = '<div class="icinga-module module-'
. $this->module_name
. '">'
. "\n"
. $html
. '</div>';
}
$pdf = new File();
$html = '<style>' . $css . '</style>' . $html;
//$html .= $this->view->action('services', 'list', 'monitoring', array('limit' => 10));
// $html = preg_replace('~icinga-module.module-bpapp~', 'csstest', $html);
// echo $html; exit;
$pdf = new Pdf();
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
$pdf->setFontSubsetting(false);
$pdf->writeHTML($html); //0, 0, '', '', $html, 0, 1, 0, true, '', true);
$pdf->Output('docs.pdf', 'I');
exit;
}
// END of PDF test
if ($this->_request->isXmlHttpRequest()) {
if ($this->replaceLayout || $this->_getParam('_render') === 'body') {
$this->_helper->layout()->setLayout('just-the-body');
@ -335,7 +329,6 @@ class ActionController extends ZfController
$this->_helper->layout()->setLayout('inline');
}
}
$notification = Notification::getInstance();
if ($notification->hasMessages()) {
$nhtml = '<ul class="notification">';
@ -349,10 +342,10 @@ class ActionController extends ZfController
$this->getResponse()->append('notification', $nhtml);
}
/*if (Session::getInstance()->show_benchmark) {
if (AuthManager::getInstance()->getSession()->get('show_benchmark')) {
Benchmark::measure('Response ready');
$this->getResponse()->append('benchmark', $this->renderBenchmark());
}*/
$this->_helper->layout()->benchmark = $this->renderBenchmark();
}
}
}