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 <?php
namespace Icinga\Pdf; namespace Icinga\File;
use TCPDF; 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_TCPDF_EXTERNAL_CONFIG', true);
//define('K_PATH_URL', 'http://net-test-icinga-vm1.adm.netways.de/develop/'); // ??? //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', '/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_MAIN', dirname(ICINGA_LIBDIR) . '/public');
define('K_PATH_FONTS', ICINGA_LIBDIR . '/vendor/tcpdf/fonts/'); define('K_PATH_FONTS', ICINGA_LIBDIR . '/vendor/tcpdf/fonts/');
define('K_PATH_CACHE', ICINGA_LIBDIR . '/vendor/tcpdf/cache/'); 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_CELL_HEIGHT_RATIO', 1.25);
define('K_SMALL_RATIO', 2/3); 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_TCPDF_THROW_EXCEPTION_ERROR', true);
define('K_THAI_TOPCHARS', false);
require_once 'vendor/tcpdf/tcpdf.php'; require_once 'vendor/tcpdf/tcpdf.php';
class File extends TCPDF class Pdf extends TCPDF
{ {
protected $cell_height_ratio = 1.25; protected $cell_height_ratio = 1.25;
public function __construct( public function __construct(

View File

@ -1,35 +1,13 @@
<?php <?php
// {{{ICINGA_LICENSE_HEADER}}} // {{{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}}} // {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web; namespace Icinga\Web;
use Icinga\Authentication\Manager; use Icinga\Authentication\Manager as AuthManager;
use Icinga\Application\Benchmark; use Icinga\Application\Benchmark;
use Icinga\Exception; use Icinga\Exception;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Pdf\File;
use Icinga\Web\Notification; use Icinga\Web\Notification;
use Zend_Layout as ZfLayout; use Zend_Layout as ZfLayout;
use Zend_Controller_Action as ZfController; 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_Response_Abstract as ZfResponse;
use Zend_Controller_Action_HelperBroker as ZfActionHelper; 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 * 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> * @copyright Copyright (c) 2013 Icinga-Web Team <info@icinga.org>
* @author 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 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @package Icinga\Web
*/ */
class ActionController extends ZfController class ActionController extends ZfController
{ {
/** /**
* The Icinga Config object is available in all controllers. This is the * The Icinga Config object is available in all controllers. This is the
* modules config for module action controllers. * modules config for module action controllers.
*
* @var Config * @var Config
*/ */
protected $config; protected $config;
/**
* @var bool
*/
protected $replaceLayout = false; protected $replaceLayout = false;
/** /**
@ -84,20 +62,12 @@ class ActionController extends ZfController
*/ */
protected $action_name; protected $action_name;
/** // @TODO(el): Should be true, is/was disabled for testing purpose
* @var bool
*/
protected $handlesAuthentication = false; protected $handlesAuthentication = false;
/**
* @var bool
*/
protected $modifiesSession = false; protected $modifiesSession = false;
/** private $allowAccess = false;
* @var bool
*/
protected $allowAccess = false;
/** /**
* The constructor starts benchmarking, loads the configuration and sets * The constructor starts benchmarking, loads the configuration and sets
@ -113,30 +83,22 @@ class ActionController extends ZfController
array $invokeArgs = array() array $invokeArgs = array()
) { ) {
Benchmark::measure('Action::__construct()'); Benchmark::measure('Action::__construct()');
$this->module_name = $request->getModuleName();
$this->module_name = $request->getModuleName();
$this->controller_name = $request->getControllerName(); $this->controller_name = $request->getControllerName();
$this->action_name = $request->getActionName(); $this->action_name = $request->getActionName();
$this->loadConfig(); $this->loadConfig();
$this->setRequest($request) $this->setRequest($request)
->setResponse($response) ->setResponse($response)
->_setInvokeArgs($invokeArgs); ->_setInvokeArgs($invokeArgs);
$this->_helper = new ZfActionHelper($this); $this->_helper = new ZfActionHelper($this);
/*
* --------------------------------------------
* Authentication is disabled to test bootstrap
* --------------------------------------------
*
* @todo remove this!
*/
if ($this->handlesAuthentication() || if ($this->handlesAuthentication() ||
Manager::getInstance( AuthManager::getInstance(
null, null,
array( array(
"writeSession" => $this->modifiesSession 'writeSession' => $this->modifiesSession
) )
)->isAuthenticated() )->isAuthenticated()
) { ) {
@ -171,7 +133,7 @@ class ActionController extends ZfController
* Helper function creating a new widget * Helper function creating a new widget
* *
* @param string $name The widget name * @param string $name The widget name
* @param array|string $properties Optional widget properties * @param string $properties Optional widget properties
* *
* @return Widget\AbstractWidget * @return Widget\AbstractWidget
*/ */
@ -185,9 +147,8 @@ class ActionController extends ZfController
* *
* TODO: This has not been implemented yet * TODO: This has not been implemented yet
* *
* @param $uri
* @param string $permission Permission name * @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 * @return bool
*/ */
@ -204,18 +165,31 @@ class ActionController extends ZfController
* @param string $permission Permission name * @param string $permission Permission name
* @param string $object No idea what this should have been :-) * @param string $object No idea what this should have been :-)
* *
* @throws \Exception
* @return self * @return self
*/ */
final protected function assertPermission($permission, $object = null) final protected function assertPermission($permission, $object = null)
{ {
if (!$this->hasPermission($permission, $object)) { if (! $this->hasPermission($permission, $object)) {
// TODO: Log violation, create dedicated Exception class // TODO: Log violation, create dedicated Exception class
throw new \Exception('Permission denied'); throw new \Exception('Permission denied');
} }
return $this; 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 * Our benchmark wants to know when we started our dispatch loop
* *
@ -224,8 +198,7 @@ class ActionController extends ZfController
public function preDispatch() public function preDispatch()
{ {
Benchmark::measure('Action::preDispatch()'); Benchmark::measure('Action::preDispatch()');
if (! $this->allowAccess) {
if (!$this->allowAccess) {
$this->_request->setModuleName('default') $this->_request->setModuleName('default')
->setControllerName('authentication') ->setControllerName('authentication')
->setActionName('login') ->setActionName('login')
@ -237,21 +210,27 @@ class ActionController extends ZfController
$this->view->controller_name = $this->controller_name; $this->view->controller_name = $this->controller_name;
$this->view->module_name = $this->module_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'); //$this->quickRedirect('/authentication/login?a=e');
} }
/**
* @param $url
* @param array $params
*/
public function redirectNow($url, array $params = array()) public function redirectNow($url, array $params = array())
{ {
if ($url instanceof Url) {
$url = $url->getRelative();
}
$this->_helper->Redirector->gotoUrlAndExit($url); $this->_helper->Redirector->gotoUrlAndExit($url);
} }
/**
* @return bool
*/
public function handlesAuthentication() public function handlesAuthentication()
{ {
return $this->handlesAuthentication; return $this->handlesAuthentication;
@ -265,8 +244,8 @@ class ActionController extends ZfController
protected function renderBenchmark() protected function renderBenchmark()
{ {
return '<pre class="benchmark">' return '<pre class="benchmark">'
. Benchmark::renderToHtml() . Benchmark::renderToHtml()
. '</pre>'; . '</pre>';
} }
/** /**
@ -284,7 +263,6 @@ class ActionController extends ZfController
{ {
Benchmark::measure('Action::postDispatch()'); Benchmark::measure('Action::postDispatch()');
// TODO: Move this elsewhere, this is just an ugly test: // TODO: Move this elsewhere, this is just an ugly test:
if ($this->_request->getParam('filetype') === 'pdf') { if ($this->_request->getParam('filetype') === 'pdf') {
@ -293,40 +271,56 @@ class ActionController extends ZfController
require_once 'vendor/lessphp/lessc.inc.php'; require_once 'vendor/lessphp/lessc.inc.php';
$less = new \lessc; $less = new \lessc;
$cssdir = dirname(ICINGA_LIBDIR) . '/public/css'; $cssdir = dirname(ICINGA_LIBDIR) . '/public/css';
// TODO: We need a way to retrieve public dir, even if located elsewhere // TODO: We need a way to retrieve public dir, even if located elsewhere
$css = $less->compileFile($cssdir . '/pdfprint.less'); $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()) { if ($module->hasCss()) {
$css .= $less->compile( $css .= $less->compile(
'.icinga-module.module-' '.icinga-module.module-'
. $name . $name
. " {\n" . " {\n"
. file_get_contents($module->getCssFilename()) . file_get_contents($module->getCssFilename())
. "}\n\n" . "}\n\n"
); );
} }
} }
*/
*/
// END of CSS test // END of CSS test
$this->render( $this->render(
null, null,
$this->_helper->viewRenderer->getResponseSegment(), $this->_helper->viewRenderer->getResponseSegment(),
$this->_helper->viewRenderer->getNoController() $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->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'); $pdf->Output('docs.pdf', 'I');
exit; exit;
} }
// END of PDF test // END of PDF test
if ($this->_request->isXmlHttpRequest()) { if ($this->_request->isXmlHttpRequest()) {
if ($this->replaceLayout || $this->_getParam('_render') === 'body') { if ($this->replaceLayout || $this->_getParam('_render') === 'body') {
$this->_helper->layout()->setLayout('just-the-body'); $this->_helper->layout()->setLayout('just-the-body');
@ -335,24 +329,23 @@ class ActionController extends ZfController
$this->_helper->layout()->setLayout('inline'); $this->_helper->layout()->setLayout('inline');
} }
} }
$notification = Notification::getInstance(); $notification = Notification::getInstance();
if ($notification->hasMessages()) { if ($notification->hasMessages()) {
$nhtml = '<ul class="notification">'; $nhtml = '<ul class="notification">';
foreach ($notification->getMessages() as $msg) { foreach ($notification->getMessages() as $msg) {
$nhtml .= '<li>[' $nhtml .= '<li>['
. $msg->type . $msg->type
. '] ' . '] '
. htmlspecialchars($msg->message); . htmlspecialchars($msg->message);
} }
$nhtml .= '</ul>'; $nhtml .= '</ul>';
$this->getResponse()->append('notification', $nhtml); $this->getResponse()->append('notification', $nhtml);
} }
/*if (Session::getInstance()->show_benchmark) { if (AuthManager::getInstance()->getSession()->get('show_benchmark')) {
Benchmark::measure('Response ready'); Benchmark::measure('Response ready');
$this->getResponse()->append('benchmark', $this->renderBenchmark()); $this->_helper->layout()->benchmark = $this->renderBenchmark();
}*/ }
} }
} }