1<?php defined('SYSPATH') OR die('No direct access allowed.');
2/**
3 * PDF controller.
4 *
5 * @package    PNP4Nagios
6 * @author     Joerg Linge
7 * @license    GPL
8 */
9class Pdf_Controller extends System_Controller  {
10
11    public function __construct(){
12        parent::__construct();
13
14        $this->use_bg           = 0;
15        $this->bg               = $this->config->conf['background_pdf'];
16        $this->pdf_page_size    = $this->config->conf['pdf_page_size'];
17        $this->pdf_margin_left  = $this->config->conf['pdf_margin_left'];
18        $this->pdf_margin_top   = $this->config->conf['pdf_margin_top'];
19        $this->pdf_margin_right = $this->config->conf['pdf_margin_right'];
20
21        // Define PDF background per url option
22        if(isset($this->bg) && $this->bg != ""){
23            if( is_readable( Kohana::config( 'core.pnp_etc_path')."/".$this->bg ) ){
24                $this->bg = Kohana::config('core.pnp_etc_path')."/".$this->bg;
25            }else{
26                $this->bg = $this->config->conf['background_pdf'];
27            }
28        }
29        // Use PDF background if readable
30        if(is_readable($this->bg)){
31            $this->use_bg = 1;
32        }
33
34    }
35
36    public function index(){
37
38        $this->tpl       = pnp::clean($this->input->get('tpl'));
39        $this->type      = "normal";
40
41        $this->data->getTimeRange($this->start,$this->end,$this->view);
42
43        // Service Details
44        if($this->host != "" && $this->service != ""){
45            $this->data->buildDataStruct($this->host,$this->service,$this->view);
46        // Host Overview
47        }elseif($this->host != ""){
48            if($this->view == ""){
49                $this->view = $this->config->conf['overview-range'];
50            }
51            $services = $this->data->getServices($this->host);
52            foreach($services as $service){
53                if($service['state'] == 'active')
54                       $this->data->buildDataStruct($this->host,$service['name'],$this->view);
55            }
56        // Special Templates
57        }elseif($this->tpl != ""){
58            $this->data->buildDataStruct('__special',$this->tpl,$this->view);
59            $this->type = 'special';
60        }else{
61            $this->host = $this->data->getFirstHost();
62            if(isset($this->host)){
63                url::redirect("/graph?host=$this->host");
64            }else{
65                throw new Kohana_User_Exception('Hostname not set ;-)', "RTFM my Friend, RTFM!");
66            }
67        }
68        #throw new Kohana_Exception(print_r($this->data->STRUCT,TRUE));
69        /*
70        * PDF Output
71        */
72        $pdf = new PDF("P", "mm", $this->pdf_page_size);
73        $pdf->AliasNbPages();
74        $pdf->SetAutoPageBreak('off');
75        $pdf->SetMargins($this->pdf_margin_left,$this->pdf_margin_top,$this->pdf_margin_right);
76        $pdf->AddPage();
77        if($this->use_bg){
78                $pdf->setSourceFile($this->bg);
79                $tplIdx = $pdf->importPage(1,'/MediaBox');
80                $pdf->useTemplate($tplIdx);
81        }
82        $pdf->SetCreator('Created with PNP');
83        $pdf->SetFont('Arial', '', 10);
84        // Title
85        $header = TRUE;
86        foreach($this->data->STRUCT as $key=>$data){
87            if($key != 0){
88                $header = FALSE;
89            }
90            if ($pdf->GetY() > 200) {
91                $pdf->AddPage();
92                if($this->use_bg){$pdf->useTemplate($tplIdx);}
93            }
94            if($this->type == 'normal'){
95                if($data['LEVEL'] == 0){
96                    $pdf->SetFont('Arial', '', 12);
97                    $pdf->CELL(120, 10, $data['MACRO']['DISP_HOSTNAME']." -- ".$data['MACRO']['DISP_SERVICEDESC'], 0, 1);
98                    $pdf->SetFont('Arial', '', 10);
99                    $pdf->CELL(120, 5, $data['TIMERANGE']['title']." (".$data['TIMERANGE']['f_start']." - ".$data['TIMERANGE']['f_end'].")", 0, 1);
100                    $pdf->SetFont('Arial', '', 8);
101                    $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
102                }else{
103                    $pdf->SetFont('Arial', '', 8);
104                    $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
105                }
106            }elseif($this->type == 'special'){
107                if($header){
108                    $pdf->SetFont('Arial', '', 12);
109                    $pdf->CELL(120, 10, $data['MACRO']['TITLE'], 0, 1);
110                    $pdf->SetFont('Arial', '', 10);
111                    $pdf->CELL(120, 5, $data['TIMERANGE']['title']." (".$data['TIMERANGE']['f_start']." - ".$data['TIMERANGE']['f_end'].")", 0, 1);
112                    $pdf->SetFont('Arial', '', 8);
113                    $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
114                }else{
115                    $pdf->SetFont('Arial', '', 10);
116                    $pdf->CELL(120, 5, $data['TIMERANGE']['title']." (".$data['TIMERANGE']['f_start']." - ".$data['TIMERANGE']['f_end'].")", 0, 1);
117                    $pdf->SetFont('Arial', '', 8);
118                    $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
119                }
120            }
121            $image = $this->rrdtool->doImage($data['RRD_CALL'],$out='PDF');
122            $img = $this->rrdtool->saveImage($image);
123            $Y = $pdf->GetY();
124            $cell_height = ($img['height'] * 0.23);
125            $cell_width = ($img['width'] * 0.23);
126            $pdf->Image($img['file'], $this->pdf_margin_left, $Y, $cell_width, $cell_height, 'PNG');
127            $pdf->CELL(120, $cell_height, '', 0, 1);
128            unlink($img['file']);
129        }
130        $pdf->Output("pnp4nagios.pdf","I");
131
132    }
133
134    public function page($page){
135        $this->start     = $this->input->get('start');
136        $this->end       = $this->input->get('end');
137        $this->view      = "";
138
139        if(isset($_GET['view']) && $_GET['view'] != "" ){
140            $this->view = pnp::clean($_GET['view']);
141        }
142
143        $this->data->getTimeRange($this->start,$this->end,$this->view);
144        $this->data->buildPageStruct($page,$this->view);
145        // Define PDF background per url option
146        if(isset($this->data->PAGE_DEF['background_pdf'])){
147            if( is_readable( Kohana::config( 'core.pnp_etc_path')."/".$this->data->PAGE_DEF['background_pdf'] ) ){
148                $this->bg = Kohana::config('core.pnp_etc_path')."/".$this->data->PAGE_DEF['background_pdf'];
149            }
150        }
151        /*
152        * PDF Output
153        */
154        $pdf = new PDF("P", "mm", $this->pdf_page_size);
155        $pdf->AliasNbPages();
156        $pdf->SetAutoPageBreak('off');
157        $pdf->SetMargins($this->pdf_margin_left,$this->pdf_margin_top,$this->pdf_margin_right);
158        $pdf->AddPage();
159        if($this->use_bg){
160                $pdf->setSourceFile($this->bg);
161                $tplIdx = $pdf->importPage(1,'/MediaBox');
162                $pdf->useTemplate($tplIdx);
163        }
164
165        $pdf->SetCreator('Created with PNP');
166        $pdf->SetFont('Arial', '', 10);
167        // Title
168        foreach($this->data->STRUCT as $data){
169            if ($pdf->GetY() > 200) {
170                $pdf->AddPage();
171                if($this->use_bg){$pdf->useTemplate($tplIdx);}
172            }
173            if($data['LEVEL'] == 0){
174                $pdf->SetFont('Arial', '', 12);
175                $pdf->CELL(120, 10, $data['MACRO']['DISP_HOSTNAME']." -- ".$data['MACRO']['DISP_SERVICEDESC'], 0, 1);
176                $pdf->SetFont('Arial', '', 10);
177                $pdf->CELL(120, 5, $data['TIMERANGE']['title']." (".$data['TIMERANGE']['f_start']." - ".$data['TIMERANGE']['f_end'].")", 0, 1);
178                $pdf->SetFont('Arial', '', 8);
179                $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
180            }else{
181                $pdf->SetFont('Arial', '', 8);
182                $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
183            }
184            $image = $this->rrdtool->doImage($data['RRD_CALL'],$out='PDF');
185            $img = $this->rrdtool->saveImage($image);
186            $Y = $pdf->GetY();
187            $cell_height = ($img['height'] * 0.23);
188            $cell_width = ($img['width'] * 0.23);
189            $pdf->Image($img['file'], $this->pdf_margin_left, $Y, $cell_width, $cell_height, 'PNG');
190            $pdf->CELL(120, $cell_height, '', 0, 1);
191            unlink($img['file']);
192        }
193        $pdf->Output("pnp4nagios.pdf","I");
194    }
195
196    public function basket(){
197        $this->start     = $this->input->get('start');
198        $this->end       = $this->input->get('end');
199        $this->view      = "";
200        if(isset($_GET['view']) && $_GET['view'] != "" ){
201            $this->view = pnp::clean($_GET['view']);
202        }
203        $this->data->getTimeRange($this->start,$this->end,$this->view);
204        $basket = $this->session->get("basket");
205        if(is_array($basket) && (!empty($basket))){
206             $this->data->buildBasketStruct($basket,$this->view);
207        }
208        //echo Kohana::debug($this->data->STRUCT);
209        /*
210        * PDF Output
211        */
212        $pdf = new PDF("P", "mm", $this->pdf_page_size);
213        $pdf->AliasNbPages();
214        $pdf->SetAutoPageBreak('off');
215        $pdf->SetMargins($this->pdf_margin_left,$this->pdf_margin_top,$this->pdf_margin_right);
216        $pdf->AddPage();
217        if($this->use_bg){
218                $pdf->setSourceFile($this->config->conf['background_pdf']);
219                $tplIdx = $pdf->importPage(1,'/MediaBox');
220                $pdf->useTemplate($tplIdx);
221        }
222
223        $pdf->SetCreator('Created with PNP');
224        $pdf->SetFont('Arial', '', 10);
225        // Title
226        foreach($this->data->STRUCT as $data){
227            if ($pdf->GetY() > 200) {
228                $pdf->AddPage();
229                if($this->use_bg){$pdf->useTemplate($tplIdx);}
230            }
231            if($data['LEVEL'] == 0){
232                $pdf->SetFont('Arial', '', 12);
233                $pdf->CELL(120, 10, $data['MACRO']['DISP_HOSTNAME']." -- ".$data['MACRO']['DISP_SERVICEDESC'], 0, 1);
234                $pdf->SetFont('Arial', '', 10);
235                $pdf->CELL(120, 5, $data['TIMERANGE']['title']." (".$data['TIMERANGE']['f_start']." - ".$data['TIMERANGE']['f_end'].")", 0, 1);
236                $pdf->SetFont('Arial', '', 8);
237                $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
238            }else{
239                $pdf->SetFont('Arial', '', 8);
240                $pdf->CELL(120, 5, "Datasource ".$data["ds_name"], 0, 1);
241            }
242            $image = $this->rrdtool->doImage($data['RRD_CALL'],$out='PDF');
243            $img = $this->rrdtool->saveImage($image);
244            $Y = $pdf->GetY();
245            $cell_height = ($img['height'] * 0.23);
246            $cell_width = ($img['width'] * 0.23);
247            $pdf->Image($img['file'], $this->pdf_margin_left, $Y, $cell_width, $cell_height, 'PNG');
248            $pdf->CELL(120, $cell_height, '', 0, 1);
249            unlink($img['file']);
250        }
251        $pdf->Output("pnp4nagios.pdf","I");
252
253    }
254}
255
256
257/*
258+
259*
260*/
261require Kohana::find_file('vendor/fpdf', 'fpdf');
262require Kohana::find_file('vendor/fpdf', 'fpdi');
263class PDF extends FPDI {
264        //Page header
265        function Header() {
266            //Arial bold 10
267            $this->SetFont('Arial', 'B', 10);
268        }
269
270        //Page footer
271        function Footer() {
272            //Position at 1.5 cm from bottom
273            $this->SetY(-20);
274            //Arial italic 8
275            $this->SetFont('Arial', 'I', 8);
276            //Page number
277            $this->Cell(0, 10, $this->PageNo() . '/{nb}', 0, 0, 'C');
278    }
279}
280
281