1<?php
2/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
3
4namespace Icinga\Application;
5
6use Icinga\Chart\Inline\PieChart;
7use Icinga\Web\JavaScript;
8use Icinga\Web\StyleSheet;
9
10error_reporting(E_ALL | E_STRICT);
11
12if (isset($_SERVER['REQUEST_URI'])) {
13    $ruri = $_SERVER['REQUEST_URI'];
14} else {
15    return false;
16}
17
18// Workaround, PHPs internal Webserver seems to mess up SCRIPT_FILENAME
19// as it prefixes it's absolute path with DOCUMENT_ROOT
20if (preg_match('/^PHP .* Development Server/', $_SERVER['SERVER_SOFTWARE'])) {
21    $script = basename($_SERVER['SCRIPT_FILENAME']);
22    $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'] = '/' . $script;
23    $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT']
24      . DIRECTORY_SEPARATOR
25      . $script;
26}
27
28$baseDir = $_SERVER['DOCUMENT_ROOT'];
29$baseDir = dirname($_SERVER['SCRIPT_FILENAME']);
30
31// Fix aliases
32$remove = str_replace('\\', '/', dirname($_SERVER['PHP_SELF']));
33if (substr($ruri, 0, strlen($remove)) !== $remove) {
34    return false;
35}
36$ruri = ltrim(substr($ruri, strlen($remove)), '/');
37
38if (strpos($ruri, '?') === false) {
39    $params = '';
40    $path = $ruri;
41} else {
42    list($path, $params) = preg_split('/\?/', $ruri, 2);
43}
44
45$special = array(
46    'css/icinga.css',
47    'css/icinga.min.css',
48    'js/icinga.dev.js',
49    'js/icinga.min.js'
50);
51
52if (in_array($path, $special)) {
53    include_once __DIR__ . '/EmbeddedWeb.php';
54    EmbeddedWeb::start();
55
56    switch ($path) {
57        case 'css/icinga.css':
58            Stylesheet::send();
59            exit;
60        case 'css/icinga.min.css':
61            Stylesheet::send(true);
62            exit;
63
64        case 'js/icinga.dev.js':
65            JavaScript::send();
66            exit;
67
68        case 'js/icinga.min.js':
69            JavaScript::sendMinified();
70            break;
71
72        default:
73            return false;
74    }
75} elseif ($path === 'svg/chart.php') {
76    if (!array_key_exists('data', $_GET)) {
77        return false;
78    }
79    include __DIR__ . '/EmbeddedWeb.php';
80    EmbeddedWeb::start();
81    header('Content-Type: image/svg+xml');
82    $pie = new PieChart();
83    $pie->initFromRequest();
84    $pie->toSvg();
85} elseif ($path === 'png/chart.php') {
86    if (!array_key_exists('data', $_GET)) {
87        return false;
88    }
89    include __DIR__ . '/EmbeddedWeb.php';
90    EmbeddedWeb::start();
91    header('Content-Type: image/png');
92    $pie = new PieChart();
93    $pie->initFromRequest();
94    $pie->toPng();
95} elseif (file_exists($baseDir . '/' . $path) && is_file($baseDir . '/' . $path)) {
96    return false;
97} else {
98    include __DIR__ . '/Web.php';
99    Web::start()->dispatch();
100}
101