Passed
Push — develop ( 1db85e...51f80f )
by Jens
03:14
created

cc.php ➔ utf8Convert()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 10
rs 9.4285
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
function humanFileSize($size,$unit="") {
69
	if( (!$unit && $size >= 1<<30) || $unit == "GB")
70
		return number_format($size/(1<<30),2)."GB";
71
	if( (!$unit && $size >= 1<<20) || $unit == "MB")
72
		return number_format($size/(1<<20),2)."MB";
73
	if( (!$unit && $size >= 1<<10) || $unit == "KB")
74
		return number_format($size/(1<<10),2)."KB";
75
	return number_format($size)." bytes";
76
}
77
78
function iconByFileType($fileType) {
79
	if (strpos($fileType, 'image') !== false) {
80
		return 'file-image-o';
81
	} elseif (strpos($fileType, 'pdf') !== false) {
82
		return 'file-pdf-o';
83
	} elseif (strpos($fileType, 'audio') !== false) {
84
		return 'file-audio-o';
85
	} elseif (strpos($fileType, 'text') !== false) {
86
		return 'file-text-o';
87
	} elseif (strpos($fileType, 'x-msdownload') !== false) {
88
		return 'windows';
89
	} elseif (in_array($fileType, array(
90
		'application/vnd.ms-excel',
91
		'application/msexcel',
92
		'application/xls',
93
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
94
		'application/vnd.google-apps.spreadsheet',
95
	))) {
96
		return 'file-excel-o';
97
	} elseif (in_array($fileType, array(
98
		'application/msword',
99
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
100
	))) {
101
		return 'file-word-o';
102
	} elseif (in_array($fileType, array(
103
		'application/x-rar-compressed',
104
		'application/x-zip-compressed',
105
		'application/zip',
106
	))) {
107
		return 'file-archive-o';
108
	}
109
	return 'file-o';
110
}
111
112
/**
113
 * Convert a string to url friendly slug
114
 *
115
 * @param string $str
116
 * @param array  $replace
117
 * @param string $delimiter
118
 *
119
 * @return mixed|string
120
 */
121
function slugify($str, $replace=array(), $delimiter='-') {
122
	if( !empty($replace) ) {
123
		$str = str_replace((array)$replace, ' ', $str);
124
	}
125
126
	$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
127
	$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
128
	$clean = strtolower(trim($clean, '-'));
129
	$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
130
131
	return $clean;
132
}
133
134
/** @noinspection PhpDocSignatureInspection */
135
136
/**
137
 * Dumps a var_dump of the passed arguments with <pre> tags surrounding it.
138
 * Dies afterwards
139
 *
140
 * @param mixed ...    The data to be displayed
141
 */
142
function dump()
143
{
144
	$debug_backtrace = current(debug_backtrace());
145
	if (PHP_SAPI == 'cli') {
146
		echo 'Dump: ' . $debug_backtrace['file'] . ':' . $debug_backtrace['line'] . "\n";
147
		foreach (func_get_args() as $data) {
148
			var_dump($data);
149
		}
150
	} else {
151
		ob_clean();
152
		echo '<div>Dump: ' . $debug_backtrace['file'] . ':<b>' . $debug_backtrace['line'] . "</b></div>";
153
		echo '<pre>';
154
		foreach (func_get_args() as $data) {
155
			echo "<code>";
156
			var_dump($data);
157
			echo "</code>";
158
		}
159
		echo '</pre>';
160
		echo <<<END
161
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/highlight.min.js"></script>
162
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.0.0/styles/default.min.css" />
163
<script>hljs.initHighlightingOnLoad();</script>
164
<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>
165
END;
166
	}
167
	die;
168
}
169
170
/**
171
 * Initializes the framework by creating the default
172
 * storage and base template
173
 *
174
 * @param string $storagePath
175
 */
176
function initFramework($storagePath)
177
{
178
	$storageDefaultPath = realpath('../_storage.json');
179
	$baseTemplateDefaultPath = realpath('../library/cc/_base.php');
180
	$baseTemplateTargetPath = '../templates/base.php';
181
	// Create the initial storage
182
	if (file_exists($storageDefaultPath) && realpath($storagePath) === false) {
183
		copy($storageDefaultPath, $storagePath);
184
	}
185
	// Create the initial base template
186
	if (file_exists($baseTemplateDefaultPath) && realpath($baseTemplateTargetPath) === false) {
187
		copy($baseTemplateDefaultPath, $baseTemplateTargetPath);
188
	}
189
}
190
191
function utf8Convert($array)
192
{
193
	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...
194
		if(!mb_detect_encoding($item, 'utf-8', true)){
195
			$item = utf8_encode($item);
196
		}
197
	});
198
199
	return $array;
200
}