This repository has been archived by the owner on May 19, 2020. It is now read-only.
/
helpers.php
executable file
·158 lines (136 loc) · 3.59 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
use System\config;
use System\uri;
/**
* translates a line of text
*
* @param string $line line to translate
* @param string[] ..., variables to replace
*
* @return mixed|string
*/
function __($line)
{
$args = array_slice(func_get_args(), 1);
return Language::line($line, null, $args);
}
/**
* Checks whether the current request is on the admin panel
*
* @return bool
* @throws \ErrorException
* @throws \OverflowException
*/
function is_admin()
{
// Exact URI or trailing slash after 'admin'.
return Uri::current() === 'admin' || strpos(Uri::current(), 'admin/') === 0;
}
/**
* Checks whether Anchor is installed
*
* @return bool
*/
function is_installed()
{
return Config::get('db') !== null or Config::get('database') !== null;
}
/**
* Creates a slug from a string
*
* @param string $string string to slugify
* @param string $separator separator character
*
* @return null|string|string[]
*/
function slug($string, $separator = '-')
{
$accents_regex = '~&([a-zA-Z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = [
'&' => 'and'
];
$string = mb_strtolower(trim($string), 'UTF-8');
$string = str_replace(array_keys($special_cases), array_values($special_cases), $string);
$string = preg_replace($accents_regex, '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
$string = preg_replace("/[^a-zA-Z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);
$string = trim($string, '-');
return $string;
}
/**
* parses a string, optionally using markdown
*
* @param string $str string to parse
* @param bool $markdown whether to use markdown
*
* @return mixed|string parsed string
*/
function parse($str, $markdown = true)
{
// process tags
$pattern = '/[\{\{]{1}([a-z]+)[\}\}]{1}/i';
if (preg_match_all($pattern, $str, $matches)) {
list($search, $replace) = $matches;
foreach ($replace as $index => $key) {
$replace[$index] = Config::meta($key);
}
$str = str_replace($search, $replace, $str);
}
// Parse Markdown as well?
if ($markdown === true) {
$md = new Parsedown();
$str = $md->text($str);
}
return $str;
}
/**
* Calculate a user-readable file size
*
* @param int $size original size in byte
*
* @return string user-readable file size
*/
function readable_size($size)
{
$unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
/**
* copies a filesystem tree recursively (aka. "cp -R")
*
* @param string $src source path
* @param string $dst destination path
*
* @return void
*/
function recurse_copy($src, $dst)
{
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . DS . $file)) {
recurse_copy($src . DS . $file, $dst . DS . $file);
} else {
copy($src . DS . $file, $dst . DS . $file);
}
}
}
closedir($dir);
}
/**
* deletes a filesystem tree recursively (aka. "rm -rf") by deleting all
* individual files and folders within it
*
* @param string $dir directory to remove
*
* @return bool whether the
*/
function delTree($dir)
{
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}