/
bootstrap.php
160 lines (155 loc) · 4.63 KB
/
bootstrap.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
<?php
/**
* This file is distributed under the MIT Open Source
* License. See README.MD for details.
* @author Ioan CHIRIAC
*/
defined('CAN_DEBUG') or define('CAN_DEBUG', false);
/**
* Handling forp profiler bootstrap
*/
if (
CAN_DEBUG && function_exists('forp_start')
) {
forp_start();
register_shutdown_function(
/**
* @ProfileAlias("Shutdown Function")
*/
function() {
if (defined('PROFILE_DISABLED')) return;
if ( PHP_SAPI !== 'cli' ) {
echo '<script src="'.ASSETS_URL.'/core/forp-ui/js/forp.min.js"></script>';
echo '<script>(new forp.Controller()).setStack(';
echo json_encode(forp_dump());
echo ').run();</script>';
} else {
forp_print();
}
}
);
}
/**
* Autoload handler
*/
spl_autoload_register(
/**
* @ProfileGroup("core")
* @ProfileCaption("#1")
* @ProfileAlias("Autoload")
*/
function($class) {
$location = explode('\\', $class, 2);
switch( $location[0] ) {
case APP_NAME:
include APP_PATH . '/' . strtr($location[1], '\\', '/') . '.php';
break;
case 'beaba':
include BEABA_PATH . '/' . strtr($location[1], '\\', '/') . '.php';
break;
default:
include BEABA_APP . '/' . $location[0] . '/' . strtr($location[1], '\\', '/') . '.php';
break;
}
return class_exists( $class, false );
}
);
// sets default defines
defined('BEABA_PATH') OR define('BEABA_PATH', __DIR__);
defined('BEABA_APP') OR define('BEABA_APP',
($path = getenv('BEABA_APP')) ?
$path : realpath(BEABA_PATH . '/../apps')
);
defined('APP_NAME') OR define('APP_NAME', 'default');
defined('APP_PATH') OR define('APP_PATH',
BEABA_APP . '/' . APP_NAME
);
defined('ASSETS_URL') OR define('ASSETS_URL',
($url = getenv('ASSETS_URL')) ?
$url : ''
);
// Gets the default language
defined('DEFAULT_LANG') OR define('DEFAULT_LANG',
($lang = getenv('DEFAULT_LANG')) ?
$lang : 'en'
);
// include the core build file
define('BEABA_BUILD_CORE', false);
// include the application build file
define('BEABA_BUILD_APP', false);
/**
* An array merging helper
* @ProfileGroup("core")
* @params array $original
* @params array $additionnal
* @params boolean $prepend
* @return array
*/
function merge_array( $original, $additionnal, $prepend = false )
{
if ( empty($additionnal) ) return $original;
if ( $prepend ) {
if ( empty($original) ) return $additionnal;
foreach($original as $key => $value) {
if ( is_numeric( $key ) ) {
$additionnal[] = $value;
} else {
if ( !empty($additionnal[$key]) ) {
$additionnal[$key] = (
is_array($additionnal[$key])
&& is_array($value) ?
merge_array($value, $additionnal[$key], true) :
$additionnal[$key]
);
} else {
$additionnal[$key] = $value;
}
}
}
return $additionnal;
} else {
foreach($additionnal as $key => $value) {
if ( is_numeric( $key ) ) {
$original[] = $value;
} else {
$original[$key] = (
!empty($original[$key])
&& is_array($value)
&& is_array($original[$key]) ?
merge_array($original[$key], $value, false) : $value
);
}
}
return $original;
}
}
/**
* Function: sanitize
* Returns a sanitized string, typically for URLs.
*
* Parameters:
* $string - The string to sanitize.
* $lowercase - Force the string to lowercase?
* $alnum - If set to *true*, will remove all non-alphanumeric characters.
*/
function sanitize($string, $lowercase = true, $alnum = false) {
$string = trim(
strtr(
strip_tags(
str_replace(
array('`', '^', '\''), null,
iconv(
'UTF-8',
'US-ASCII//TRANSLIT//IGNORE',
strtr($string, '\'', ' ')
)
)
),
'~`!@?#$%§^&*()_=+[]{}\\/|;:,"\'<>.',
' '
)
);
if ($alnum) $string = preg_replace('/[^a-zA-Z0-9]/', ' ', $string);
if ($lowercase) $string = strtolower($string);
return preg_replace('/\s+/', '-', $string);
}