-
Notifications
You must be signed in to change notification settings - Fork 9
/
helper.php
207 lines (170 loc) · 4.87 KB
/
helper.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* A helper class designed for keeping track of helper functions
*
* @author Lauge Rud Knudsen <[email protected]>
* @version V2
*/
class Helper {
//properties
private static $instance;
/** @var array The array of settings. */
protected $settings;
//getters
//setters
//methods
function __construct() {
$settings = file(__DIR__ . '/settings.txt');
array_filter($settings, function($var) {
$shown = true;
if (strpos($var, '#') === 0) {
$shown = false;
} else if (empty($var)) {
$shown = false;
}
return $shown;
});
foreach ($settings as $setting) {
$array = explode('=', $setting);
$value = isset($array[1]) ? trim($array[1]) : "";
$arrayEnd = strrpos($value, ']');
if (strpos($value, '[') === 0 && $arrayEnd === (strlen($value) - 1)) {
$value = substr($value, 1, ($arrayEnd - 1));
$value = explode(',', $value);
}
$output[$array[0]] = $value;
}
$this->settings = $output;
}
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
private function __clone() { }
/**
* Get a settings value.
*
* @param string $key
* @return mixed Returns false if the key was not found
*/
public function setting($key) {
$output = false;
$key = strtoupper($key);
if (array_key_exists($key, $this->settings)) {
$output = $this->settings[$key];
}
return $output;
}
/**
* Returns the current environment.
*
* @return mixed Returns false if the env setting was not found
*/
public function env() {
return $this->setting('ENV');
}
/**
* Get the allowed file extensions for upload.
*
* @param string $delimiter The delimiter for seperation. Default value is ","
* @return string The file extensions seperated by $delimiter
*/
public function getFileExtensions($delimiter = ',') {
$extensions = [];
$extensions = array_merge($extensions, $this->setting('AVATAR_EXTENSIONS'));
$extensions = array_merge($extensions, $this->setting('SABER_EXTENSIONS'));
$extensions = array_merge($extensions, $this->setting('PLATFORM_EXTENSIONS'));
$extensions = array_merge($extensions, $this->setting('NOTE_EXTENSIONS'));
foreach ($extensions as $index => $extension) {
$extensions[$index] = ".$extension";
}
return implode($delimiter, $extensions);
}
public function convertFromBytes($bytes) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
$output['bytes'] = $bytes / pow(1024, $factor);
$output['type'] = @$sz[$factor];
return $output;
}
public function convertToBytes($amount, $byteType = 'MB') {
switch (strtoupper($byteType)) {
case 'KB':
$factor = 1;
break;
case 'MB':
$factor = 2;
break;
case 'GB':
$factor = 3;
break;
case 'TB':
$factor = 4;
break;
case 'PB':
$factor = 5;
break;
default:
$factor = 1;
break;
}
$output = $amount * pow(1024, $factor);
return $output;
}
public function isLocal() {
return ($this->env() == 'local');
}
public function isDevelopment() {
return ($this->env() == 'develop');
}
public function isProduction() {
return ($this->env() == 'production');
}
public function checkRemoteFile($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->setting('REMOTE_FILE_TIMEOUT'));
$output = curl_exec($ch) !== FALSE;
curl_close($ch);
return $output;
}
public function getQuickTags() {
return file(__DIR__ . '/quicktags.txt');
}
public function scriptFetcher() {
echo '<script id="fetcher" src="' . WEBROOT .'/resources/fetcher.js" data-webroot="' . WEBROOT . '"></script>';
}
public function getCheckmark() {
include ROOT . '/resources/components/checkmark.php';
}
public function getXmark() {
include ROOT . '/resources/components/xmark.php';
}
public function getMark($isCheckmark = true) {
if ($isCheckmark) {
$this->getCheckmark();
} else {
$this->getXmark();
}
}
public function echoChecked($value) {
return ($value) ? 'checked' : '';
}
public function deleteFiles($target) {
if (is_dir($target)) {
$files = glob($target . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
foreach($files as $file) {
$this->deleteFiles($file);
}
rmdir($target);
} elseif(is_file($target)) {
unlink($target);
}
}
}