forked from Codiad/Codiad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
executable file
·85 lines (69 loc) · 2.87 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
<?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.
*/
//////////////////////////////////////////////////////////////////
// Check Session / Key
//////////////////////////////////////////////////////////////////
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
//////////////////////////////////////////////////////////////////
function getJSON($file){
$json = file_get_contents(BASE_PATH . "/data/" . $file);
$json = str_replace("|*/?>","",str_replace("<?php/*|","",$json));
$json = json_decode($json,true);
return $json;
}
//////////////////////////////////////////////////////////////////
// Save JSON
//////////////////////////////////////////////////////////////////
function saveJSON($file,$data){
$data = "<?php/*|" . json_encode($data) . "|*/?>";
$write = fopen(BASE_PATH . "/data/" . $file, 'w') or die("can't open file");
fwrite($write, $data);
fclose($write);
}
//////////////////////////////////////////////////////////////////
// Format JSEND Response
//////////////////////////////////////////////////////////////////
function formatJSEND($status,$data=false){
// Success ///////////////////////////////////////////////
if($status=="success"){
if($data){
$jsend = '{"status":"success","data":'.json_encode($data).'}';
}else{
$jsend = '{"status":"success","data":null}';
}
// Error /////////////////////////////////////////////////
}else{
$jsend = '{"status":"error","message":"'.$data.'"}';
}
// Return ////////////////////////////////////////////////
return $jsend;
}
//////////////////////////////////////////////////////////////////
// Check Function Availability
//////////////////////////////////////////////////////////////////
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;
}
?>