Passed
Push — develop ( 51f80f...cde9d6 )
by Jens
03:13
created

cc.php ➔ initFramework()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 7
eloc 12
c 3
b 0
f 1
nc 8
nop 1
dl 0
loc 22
rs 6.9811
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
ob_start();
17
session_start();
18
19
include('errorhandler.php');
20
include('autoloader.php');
21
22
new \library\cc\Application();
23
24
if (php_sapi_name() != "cli") {
25
	ob_end_flush();
26
}
27
28
/**
29
 * @param $ptime
30
 * @return string|void
31
 */
32
function timeElapsedString($ptime)
33
{
34
	$etime = time() - $ptime;
35
36
	if ($etime < 1)
37
	{
38
		return '0 seconds';
39
	}
40
41
	$a = array( 365 * 24 * 60 * 60  =>  'year',
42
				30 * 24 * 60 * 60  =>  'month',
43
				24 * 60 * 60  =>  'day',
44
				60 * 60  =>  'hour',
45
				60  =>  'minute',
46
				1  =>  'second'
47
	);
48
	$a_plural = array( 'year'   => 'years',
49
					   'month'  => 'months',
50
					   'day'    => 'days',
51
					   'hour'   => 'hours',
52
					   'minute' => 'minutes',
53
					   'second' => 'seconds'
54
	);
55
56
	foreach ($a as $secs => $str)
57
	{
58
		$d = $etime / $secs;
59
		if ($d >= 1)
60
		{
61
			$r = round($d);
62
			return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
63
		}
64
	}
65
	return 0;
66
}
67
68
/**
69
 * Converts an amount of bytes to a human readable
70
 * format
71
 *
72
 * @param $size
73
 * @param string $unit
74
 * @return string
75
 */
76
function humanFileSize($size,$unit="") {
77
	if( (!$unit && $size >= 1<<30) || $unit == "GB")
78
		return number_format($size/(1<<30),2)."GB";
79
	if( (!$unit && $size >= 1<<20) || $unit == "MB")
80
		return number_format($size/(1<<20),2)."MB";
81
	if( (!$unit && $size >= 1<<10) || $unit == "KB")
82
		return number_format($size/(1<<10),2)."KB";
83
	return number_format($size)." bytes";
84
}
85
86
/**
87
 * Selects the right font-awesome icon for each filetype
88
 *
89
 * @param $fileType
90
 * @return string
91
 */
92
93
function iconByFileType($fileType) {
94
	if (strpos($fileType, 'image') !== false) {
95
		return 'file-image-o';
96
	} elseif (strpos($fileType, 'pdf') !== false) {
97
		return 'file-pdf-o';
98
	} elseif (strpos($fileType, 'audio') !== false) {
99
		return 'file-audio-o';
100
	} elseif (strpos($fileType, 'text') !== false) {
101
		return 'file-text-o';
102
	} elseif (strpos($fileType, 'x-msdownload') !== false) {
103
		return 'windows';
104
	} elseif (in_array($fileType, array(
105
		'application/vnd.ms-excel',
106
		'application/msexcel',
107
		'application/xls',
108
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
109
		'application/vnd.google-apps.spreadsheet',
110
	))) {
111
		return 'file-excel-o';
112
	} elseif (in_array($fileType, array(
113
		'application/msword',
114
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
115
	))) {
116
		return 'file-word-o';
117
	} elseif (in_array($fileType, array(
118
		'application/x-rar-compressed',
119
		'application/x-zip-compressed',
120
		'application/zip',
121
	))) {
122
		return 'file-archive-o';
123
	}
124
	return 'file-o';
125
}
126
127
/**
128
 * Convert a string to url friendly slug
129
 *
130
 * @param string $str
131
 * @param array  $replace
132
 * @param string $delimiter
133
 *
134
 * @return mixed|string
135
 */
136
function slugify($str, $replace=array(), $delimiter='-') {
137
	if( !empty($replace) ) {
138
		$str = str_replace((array)$replace, ' ', $str);
139
	}
140
141
	$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
142
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
143
	$clean = strtolower(trim($clean, '-'));
144
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
145
146
	return $clean;
147
}
148
149
/** @noinspection PhpDocSignatureInspection */
150
151
/**
152
 * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
153
 * Dies afterwards
154
 *
155
 * @param mixed ...    The data to be displayed
156
 */
157
function dump()
158
{
159
	$debug_backtrace = current(debug_backtrace());
160
	if (PHP_SAPI == 'cli') {
161
		echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
162
		foreach (func_get_args() as $data) {
163
			var_dump($data);
164
		}
165
	} else {
166
		ob_clean();
167
		echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
168
		echo '<pre>';
169
		foreach (func_get_args() as $data) {
170
			echo "<code>";
171
			var_dump($data);
172
			echo "</code>";
173
		}
174
		echo '</pre>';
175
		echo <<<END
176
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
177
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
178
<script>hljs.initHighlightingOnLoad();</script>
179
<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>
180
END;
181
	}
182
	die;
183
}
184
185
/**
186
 * Initializes the framework by creating the default
187
 * storage and base template
188
 *
189
 * @param string $storagePath
190
 */
191
function initFramework($storagePath)
192
{
193
	$storageDefaultPath = realpath('../_storage.json');
194
	$baseTemplateDefaultPath = realpath('../library/cc/_base.php');
195
	$baseTemplateTargetPath = '../templates/base.php';
196
	$baseConfigDefaultPath = realpath('../library/cc/_config.json');
197
	$baseConfigTargetPath = '../config.json';
198
199
	// Create the initial base template
200
	if (file_exists($baseConfigDefaultPath) && realpath($baseConfigTargetPath) === false) {
201
		copy($baseConfigDefaultPath, $baseConfigTargetPath);
202
	}
203
204
	// Create the initial storage
205
	if (file_exists($storageDefaultPath) && realpath($storagePath) === false) {
206
		copy($storageDefaultPath, $storagePath);
207
	}
208
	// Create the initial base template
209
	if (file_exists($baseTemplateDefaultPath) && realpath($baseTemplateTargetPath) === false) {
210
		copy($baseTemplateDefaultPath, $baseTemplateTargetPath);
211
	}
212
}
213
214
/**
215
 * Convert all values of an array to utf8
216
 *
217
 * @param $array
218
 * @return array
219
 */
220
function utf8Convert($array)
221
{
222
	array_walk_recursive($array, function(&$item, $key){
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
223
		if(!mb_detect_encoding($item, 'utf-8', true)){
224
			$item = utf8_encode($item);
225
		}
226
	});
227
228
	return $array;
229
}