Passed
Push — develop ( 41b2f9...677ca4 )
by Jens
02:17
created

cc.php ➔ sanitize_output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
// Error settings
3
ini_set('display_errors', true);
4
ini_set('error_reporting', E_ALL);
5
6
// Allow Short Open Tags
7
ini_set('short_open_tag', true);
8
9
// Set internal encoding
10
mb_internal_encoding("UTF-8");
11
12
// Time settings
13
setlocale(LC_ALL, 'nl_NL');
14
date_default_timezone_set('Europe/Amsterdam');
15
16
function sanitize_output($buffer) {
17
18
    $search = array(
19
        '/\>[^\S ]+/s',     // strip whitespaces after tags, except space
20
        '/[^\S ]+\</s',     // strip whitespaces before tags, except space
21
        '/(\s)+/s',         // shorten multiple whitespace sequences
22
        '/<!--(.|\s)*?-->/' // Remove HTML comments
23
    );
24
25
    $replace = array(
26
        '>',
27
        '<',
28
        '\\1',
29
        ''
30
    );
31
32
    $buffer = preg_replace($search, $replace, $buffer);
33
34
    return $buffer;
35
}
36
37
ob_start("sanitize_output");
38
session_start();
39
40
include('errorhandler.php');
41
include('autoloader.php');
42
43
new \library\cc\Application();
44
45
if (php_sapi_name() != "cli") {
46
	ob_end_flush();
47
}
48
49
/** @noinspection PhpDocSignatureInspection */
50
51
/**
52
 * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
53
 * Dies afterwards
54
 *
55
 * @param mixed ...    The data to be displayed
56
 */
57
function dump()
58
{
59
	$debug_backtrace = current(debug_backtrace());
60
	if (PHP_SAPI == 'cli') {
61
		echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
62
		foreach (func_get_args() as $data) {
63
			var_dump($data);
64
		}
65
	} else {
66
		ob_clean();
67
		echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
68
		echo '<pre>';
69
		foreach (func_get_args() as $data) {
70
			echo "<code>";
71
			var_dump($data);
72
			echo "</code>";
73
		}
74
		echo '</pre>';
75
		echo <<<END
76
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
77
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
78
<script>hljs.initHighlightingOnLoad();</script>
79
<style>code.php.hljs{margin: -0.4em 0;}code.active{background-color:#e0e0e0;}code:before{content:attr(data-line);}code.active:before{color:#c00;font-weight:bold;}</style>
80
END;
81
	}
82
	die;
83
}
84
85
/**
86
 * Initializes the framework by creating the default
87
 * storage and base template
88
 */
89
function initFramework()
90
{
91
	$baseTemplateDefaultPath = realpath('../library/cc/install/_base.php');
92
	$baseTemplateTargetPath = '../templates/base.php';
93
	$baseConfigDefaultPath = realpath('../library/cc/install/_config.json');
94
	$baseConfigTargetPath = '../config.json';
95
96
	// Create the initial config
97
	if (file_exists($baseConfigDefaultPath) && realpath($baseConfigTargetPath) === false) {
98
		copy($baseConfigDefaultPath, $baseConfigTargetPath);
99
	}
100
101
	// Create the initial base template
102
	if (file_exists($baseTemplateDefaultPath) && realpath($baseTemplateTargetPath) === false) {
103
		copy($baseTemplateDefaultPath, $baseTemplateTargetPath);
104
	}
105
}
106
107
/**
108
 * Convert all values of an array to utf8
109
 *
110
 * @param $array
111
 * @return array
112
 */
113
function utf8Convert($array)
114
{
115
	array_walk_recursive($array, function(&$item){
116
		if(!mb_detect_encoding($item, 'utf-8', true)){
117
			$item = utf8_encode($item);
118
		}
119
	});
120
121
	return $array;
122
}