forked from M1ke/easy-site-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
309 lines (283 loc) · 8.14 KB
/
common.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/* Constants
LOG - directory to store log files in
LOG_REDIRECTS - log redirects
*/
function count_true($arr){
$count = 0;
foreach ($arr as $item){
$count += $item ? 1 : 0;
}
return $count;
}
function csv_array($file, $check = false, $delimiter = ',', $start = 1){
$parsed = csv_file($file, $delimiter);
return csv_array_parse($parsed, $check, $start);
}
function csv_file($file, $delimiter = ',', $limit = 80000){
if (is_array($file)){
$file = $file['tmp_name'];
}
$parsed = [];
if (!is_readable($file)){
return $parsed;
}
$fh = fopen($file, 'r');
if (empty($fh)){
return $parsed;
}
// might want to improve this; maybe limit it based on number of empty rows?
$n = 0;
while (($parse = fgetcsv($fh, 0, $delimiter)) && $n<$limit){
$parsed[] = $parse;
$n++;
}
fclose($fh);
return $parsed;
}
function csv_array_parse($parsed, $check = false, $start = 1){
$arr = [];
if (empty($parsed)){
return $arr;
}
$title_line = $parsed[0];
foreach ($title_line as $n => $field){
$field = trim($field);
$field = @strtolower($field);
// $field=first_word($field); // its probably a bad idea to do this - is legacy from old import
if (!empty($field)){
$title_line[$n] = $field;
}
}
for ($n = $start; $n<count($parsed); $n++){
$item = $parsed[$n];
$i = 0;
foreach ($title_line as $field){
$item[$i] = trim($item[$i]);
$arr[$n][$field] = $check ? string_check($item[$i]) : $item[$i];
$i++;
}
}
return $arr;
}
function csv_array_string($string, $check = false, $delimiter = ',', $start = 1){
$string = preg_split("/\r\n|\n|\r/", $string);
foreach ($string as $line){
$parsed[] = str_getcsv($line, $delimiter);
}
return csv_array_parse($parsed, $check, $start);
}
// This code comes from somewhere online, find where and add a citation
function debug_code_error($num,$str,$file,$line,$context){
if (!(error_reporting() & $num)) return;
switch($num){
case E_WARNING:
case E_USER_WARNING:
case E_STRICT:
case E_NOTICE:
case E_USER_NOTICE:
$type='warning';
$fatal=false;
break;
default:
$type='fatal error';
$fatal=true;
}
$trace=array_reverse(debug_backtrace());
array_pop($trace);
if (php_sapi_name()=='cli'){
echo 'Backtrace from '.$type.' \''.$str.'\' at '.$file.' '.$line.':'."\n";
foreach ($trace as $item) echo ' '.(isset($item['file'])?$item['file']:'<unknown file>').' '.(isset($item['line'])?$item['line']:'<unknown line>').' calling '.$item['function'].'()'."\n";
}
else {
echo '<p class="error_backtrace">Backtrace from '.$type.' \''.$str.'\' at '.$file.' '.$line.':';
echo '<ol>';
foreach ($trace as $item) echo '<li>'.(isset($item['file'])?$item['file']:'<unknown file>').' '.(isset($item['line'])?$item['line']:'<unknown line>').' calling '.$item['function'].'()</li>';
echo '</ol></p>';
}
if (ini_get('log_errors')){
$items=array();
foreach($trace as $item) $items[]=(isset($item['file'])?$item['file']:'<unknown file>').' '.(isset($item['line'])?$item['line']:'<unknown line>').' calling '.$item['function'].'()';
$message='Backtrace from '.$type.' \''.$str.'\' at '.$file.' '.$line.': '.join(' | ', $items);
error_log($message);
}
if ($fatal) exit(1);
}
function is_on($define){
return defined($define) && constant($define)===true;
}
function is_off($define){
return !is_on($define);
}
function get_args_smart(Array $args,$n=0){
for (;$n<count($args);$n++){
if (is_array($args[$n])){
$array=$args[$n];
}
elseif (is_callable($args[$n])){
$callable=$args[$n];
}
else {
$string=$args[$n];
}
}
return ['array'=>$array,'callable'=>$callable,'string'=>$string];
}
function get_browse(){
$browser=array('OPERA','MSIE','NETSCAPE','FIREFOX','SAFARI','KONQUEROR','MOZILLA');
$info['browser']='OTHER';
foreach ($browser as $parent){
if (($s=strpos(strtoupper($_SERVER['HTTP_USER_AGENT']),$parent))!==false){
$f=$s+strlen($parent);
$version=substr($_SERVER['HTTP_USER_AGENT'],$f,5);
$version=preg_replace('/[^0-9,.]/','',$version);
$info['browser']=$parent;
$info['version']=$version;
break;
}
}
return $info;
}
function get_domain($url){
$url=str_replace('http://','',$url);
$dom=substr($url,0,strpos($url,'/'));
return $dom;
}
function is_binary($number){
return ($number==1 or $number==0);
}
function is_coord($lat,$lng){
return is_numeric($lat) and is_numeric($lng) and ($lat!=0 or $lng!=0);
}
function is_mobile($agent=null,$session=true,$make_mobile=false){
if ($make_mobile){
log_file('made mobile');
$_SESSION['S_mobile']=true;
$_SESSION['S_desktop']=false;
}
if (isset($_SESSION['S_mobile']) and $session){
return $_SESSION['S_mobile'];
}
if (empty($agent)){
$agent=$_SERVER['HTTP_USER_AGENT'];
}
switch (true){
case (preg_match('/mobile/i',$agent));
$mobile=true;
break;
case (preg_match('/iphone/i',$agent));
$mobile=true;
break;
// only for people on WiFi
case (preg_match('/ipod/i',$agent));
$mobile=true;
break;
case (preg_match('/blackberry/i',$agent));
$mobile=true;
break;
// can run on a number of phones
case (preg_match('/opera mini/i',$agent));
$mobile=true;
break;
}
if ($session){
$_SESSION['S_mobile']=$mobile;
}
else {
$_SESSION['S_mobile']=false;
unset($_SESSION['S_mobile']);
}
return $mobile;
}
function is_provided(&$val){
$val=trim($val);
return (strlen($val)>0);
}
function is_input($var){
return (!is_null($var) and $var!=='');
}
function local(){
return in_string(['127.0.0.1','localhost'],$_SERVER['HTTP_HOST']);
}
function log_file($log,$var=null,$file=null,$overwrite=false){
$file=log_file_location($file);
$log='----Logged on '.date('r').' ----'.PHP_EOL.PHP_EOL.'$'.$var.': '.((is_array($log) or is_object($log)) ? print_r($log,true) : $log).PHP_EOL.PHP_EOL;
file_save_($file,$log,$overwrite);
return true;
}
function log_file_location($file=null){
if (empty($file)){
$file='main.log';
$set_dir=true;
}
if (defined('LOG') and substr($file,0,1)!='/'){
$file=LOG.$file;
}
elseif ($set_dir){
$file=__DIR__.'/logs/'.$file;
}
return $file;
}
function json_error_msg(){
$int=json_last_error();
$arr=[
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
];
return $arr[$int];
}
function pause($seconds = 10){
if (!is_numeric($seconds) || $seconds<1){
die('You may not pause for less than 1 second.');
}
echo 'You have '.$seconds.' seconds before the script continues'.PHP_EOL;
return sleep($seconds);
}
function redirect_url($url=null,$debug=false){
if (empty($url)){
$url=$_SERVER['HTTP_REFERER'];
}
if (defined('LOG_REDIRECTS')){
log_file($url,'redirect');
}
if ($debug){
echo $url;
}
else {
header('location:'.$url);
}
die;
}
function shell(){
return !empty($_SERVER['shell']);
}
function success(){
$return=func_get_args();
if (is_array($return[0])){
$return=$return[0];
}
echo 'success'.(!empty($return) ? '|'.implode('|',$return) : '');
die;
}
function var_switch(&$var1,&$var2){
$copy=$var1;
$var1=$var2;
$var1=$copy;
}
function upload_array($input,$original,$n){
$_FILES[$input]=array(
'name'=>$_FILES[$original]['name'][$n],
'type'=>$_FILES[$original]['type'][$n],
'tmp_name'=>$_FILES[$original]['tmp_name'][$n],
'error'=>$_FILES[$original]['error'][$n],
'size'=>$_FILES[$original]['size'][$n]
);
return $_FILES[$input];
}