forked from Codiad/Codiad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.php
executable file
·244 lines (194 loc) · 9.28 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
<?php
/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), distributed
* as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
*/
Common::startSession();
//////////////////////////////////////////////////////////////////
// Common Class
//////////////////////////////////////////////////////////////////
class Common {
//////////////////////////////////////////////////////////////////
// PROPERTIES
//////////////////////////////////////////////////////////////////
public static $debugMessageStack = array();
//////////////////////////////////////////////////////////////////
// METHODS
//////////////////////////////////////////////////////////////////
// -----------------------------||----------------------------- //
//////////////////////////////////////////////////////////////////
// Construct
//////////////////////////////////////////////////////////////////
public static function construct(){
global $cookie_lifetime;
$path = str_replace("index.php", "", $_SERVER['SCRIPT_FILENAME']);
foreach (array("components","plugins") as $folder) {
if(strpos($_SERVER['SCRIPT_FILENAME'], $folder)) {
$path = substr($_SERVER['SCRIPT_FILENAME'],0, strpos($_SERVER['SCRIPT_FILENAME'], $folder));
break;
}
}
if(file_exists($path.'config.php')){ require_once($path.'config.php'); }
if(!defined('BASE_PATH')) {
define('BASE_PATH', rtrim(str_replace("index.php", "", $_SERVER['SCRIPT_FILENAME']),"/"));
}
if(!defined('COMPONENTS')) {
define('COMPONENTS', BASE_PATH . '/components');
}
if(!defined('PLUGINS')) {
define('PLUGINS', BASE_PATH . '/plugins');
}
if(!defined('DATA')) {
define('DATA', BASE_PATH . '/data');
}
if(!defined('THEMES')){
define("THEMES", BASE_PATH . "/themes");
}
if(!defined('THEME')){
define("THEME", "default");
}
global $lang;
if (isset($_SESSION['lang'])) {
include BASE_PATH."/languages/{$_SESSION['lang']}.php";
} else {
include BASE_PATH."/languages/en.php";
}
}
//////////////////////////////////////////////////////////////////
// SESSIONS
//////////////////////////////////////////////////////////////////
public static function startSession() {
Common::construct();
global $cookie_lifetime;
if(isset($cookie_lifetime) && $cookie_lifetime != "") {
ini_set("session.cookie_lifetime", $cookie_lifetime);
}
//Set a Session Name
session_name(md5(BASE_PATH));
session_start();
}
//////////////////////////////////////////////////////////////////
// Log debug message
// Messages will be displayed in the console when the response is
// made with the formatJSEND function.
//////////////////////////////////////////////////////////////////
public static function debug($message) {
Common::$debugMessageStack[] = $message;
}
//////////////////////////////////////////////////////////////////
// Localization
//////////////////////////////////////////////////////////////////
public static function i18n($key) {
echo Common::get_i18n($key);
}
public static function get_i18n($key) {
global $lang;
$key = ucwords(strtolower($key)); //Test, test TeSt and tESt are exacly the same
return isset($lang[$key]) ? $lang[$key] : $key;
}
//////////////////////////////////////////////////////////////////
// Check Session / Key
//////////////////////////////////////////////////////////////////
public static function checkSession(){
// Set any API keys
$api_keys = array();
// Check API Key or Session Authentication
$key = "";
if(isset($_GET['key'])){ $key = $_GET['key']; }
if(!isset($_SESSION['user']) && !in_array($key,$api_keys)){
exit('{"status":"error","message":"Authentication Error"}');
}
}
//////////////////////////////////////////////////////////////////
// Get JSON
//////////////////////////////////////////////////////////////////
public static function getJSON($file,$namespace=""){
$path = DATA . "/";
if($namespace != ""){
$path = $path . $namespace . "/";
$path = preg_replace('#/+#','/',$path);
}
$json = file_get_contents($path . $file);
$json = str_replace("|*/?>","",str_replace("<?php/*|","",$json));
$json = json_decode($json,true);
return $json;
}
//////////////////////////////////////////////////////////////////
// Save JSON
//////////////////////////////////////////////////////////////////
public static function saveJSON($file,$data,$namespace=""){
$path = DATA . "/";
if($namespace != ""){
$path = $path . $namespace . "/";
$path = preg_replace('#/+#','/',$path);
if(!is_dir($path)) mkdir($path);
}
$data = "<?php/*|" . json_encode($data) . "|*/?>";
$write = fopen($path . $file, 'w') or die("can't open file ".$path.$file);
fwrite($write, $data);
fclose($write);
}
//////////////////////////////////////////////////////////////////
// Format JSEND Response
//////////////////////////////////////////////////////////////////
public static function formatJSEND($status,$data=false){
/// Debug /////////////////////////////////////////////////
$debug = "";
if(count(Common::$debugMessageStack) > 0) {
$debug .= ',"debug":';
$debug .= json_encode(Common::$debugMessageStack);
}
// Success ///////////////////////////////////////////////
if($status=="success"){
if($data){
$jsend = '{"status":"success","data":'.json_encode($data).$debug.'}';
}else{
$jsend = '{"status":"success","data":null'.$debug.'}';
}
// Error /////////////////////////////////////////////////
}else{
$jsend = '{"status":"error","message":"'.$data.'"'.$debug.'}';
}
// Return ////////////////////////////////////////////////
return $jsend;
}
//////////////////////////////////////////////////////////////////
// Check Function Availability
//////////////////////////////////////////////////////////////////
public static function checkAccess() {
return !file_exists(DATA . "/" . $_SESSION['user'] . '_acl.php');
}
//////////////////////////////////////////////////////////////////
// Check Function Availability
//////////////////////////////////////////////////////////////////
public static function isAvailable($func) {
if (ini_get('safe_mode')) return false;
$disabled = ini_get('disable_functions');
if ($disabled) {
$disabled = explode(',', $disabled);
$disabled = array_map('trim', $disabled);
return !in_array($func, $disabled);
}
return true;
}
//////////////////////////////////////////////////////////////////
// Check If Path is absolute
//////////////////////////////////////////////////////////////////
public static function isAbsPath( $path ) {
return ($path[0] === '/')?true:false;
}
}
//////////////////////////////////////////////////////////////////
// Wrapper for old method names
//////////////////////////////////////////////////////////////////
function debug($message) { Common::debug($message); }
function i18n($key) { echo Common::i18n($key); }
function get_i18n($key) { return Common::get_i18n($key); }
function checkSession(){ Common::checkSession(); }
function getJSON($file,$namespace=""){ return Common::getJSON($file,$namespace); }
function saveJSON($file,$data,$namespace=""){ Common::saveJSON($file,$data,$namespace); }
function formatJSEND($status,$data=false){ return Common::formatJSEND($status,$data); }
function checkAccess() { return Common::checkAccess(); }
function isAvailable($func) { return Common::isAvailable($func); }
?>