Passed
Push — develop ( 1bfb65...a01f89 )
by Jens
02:58
created

cc.php ➔ iconByFileType()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nc 3
nop 1
dl 0
loc 27
rs 8.8571
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
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
	$fileTypeIcons = array(
95
		'image' => 'file-image-o',
96
		'pdf' => 'file-pdf-o',
97
		'audio' => 'file-audio-o',
98
		'x-msdownload' => 'windows',
99
		'application/vnd.ms-excel' => 'file-excel-o',
100
		'application/msexcel' => 'file-excel-o',
101
		'application/xls' => 'file-excel-o',
102
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'file-excel-o',
103
		'application/vnd.google-apps.spreadsheet' => 'file-excel-o',
104
		'application/msword' => 'file-word-o',
105
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'file-word-o',
106
		'application/x-rar-compressed' => 'file-archive-o',
107
		'application/x-zip-compressed' => 'file-archive-o',
108
		'application/zip' => 'file-archive-o',
109
		'text' => 'file-text-o',
110
	);
111
112
	foreach ($fileTypeIcons as $needle => $icon) {
113
		if (strpos($fileType, $needle) !== false) {
114
			return $icon;
115
		}
116
	}
117
118
	return 'file-o';
119
}
120
121
/**
122
 * Convert a string to url friendly slug
123
 *
124
 * @param string $str
125
 * @param array  $replace
126
 * @param string $delimiter
127
 *
128
 * @return mixed|string
129
 */
130
function slugify($str, $replace=array(), $delimiter='-') {
131
	if( !empty($replace) ) {
132
		$str = str_replace((array)$replace, ' ', $str);
133
	}
134
135
	$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
136
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
137
	$clean = strtolower(trim($clean, '-'));
138
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
139
140
	return $clean;
141
}
142
143
/** @noinspection PhpDocSignatureInspection */
144
145
/**
146
 * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
147
 * Dies afterwards
148
 *
149
 * @param mixed ...    The data to be displayed
150
 */
151
function dump()
152
{
153
	$debug_backtrace = current(debug_backtrace());
154
	if (PHP_SAPI == 'cli') {
155
		echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
156
		foreach (func_get_args() as $data) {
157
			var_dump($data);
158
		}
159
	} else {
160
		ob_clean();
161
		echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
162
		echo '<pre>';
163
		foreach (func_get_args() as $data) {
164
			echo "<code>";
165
			var_dump($data);
166
			echo "</code>";
167
		}
168
		echo '</pre>';
169
		echo <<<END
170
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
171
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
172
<script>hljs.initHighlightingOnLoad();</script>
173
<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>
174
END;
175
	}
176
	die;
177
}
178
179
/**
180
 * Initializes the framework by creating the default
181
 * storage and base template
182
 */
183
function initFramework()
184
{
185
	$baseTemplateDefaultPath = realpath('../library/cc/install/_base.php');
186
	$baseTemplateTargetPath = '../templates/base.php';
187
	$baseConfigDefaultPath = realpath('../library/cc/install/_config.json');
188
	$baseConfigTargetPath = '../config.json';
189
190
	// Create the initial config
191
	if (file_exists($baseConfigDefaultPath) && realpath($baseConfigTargetPath) === false) {
192
		copy($baseConfigDefaultPath, $baseConfigTargetPath);
193
	}
194
195
	// Create the initial base template
196
	if (file_exists($baseTemplateDefaultPath) && realpath($baseTemplateTargetPath) === false) {
197
		copy($baseTemplateDefaultPath, $baseTemplateTargetPath);
198
	}
199
}
200
201
/**
202
 * Convert all values of an array to utf8
203
 *
204
 * @param $array
205
 * @return array
206
 */
207
function utf8Convert($array)
208
{
209
	array_walk_recursive($array, function(&$item){
210
		if(!mb_detect_encoding($item, 'utf-8', true)){
211
			$item = utf8_encode($item);
212
		}
213
	});
214
215
	return $array;
216
}