-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral.php
142 lines (101 loc) · 2.51 KB
/
general.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
<?php
/*
*
*/
function output($error, $stdout, $stderr) {
$json = array();
$json['err'] = $error;
$json['stdout'] = $stdout;
$json['stderr'] = $stderr;
//$json['data']['id'] = 1;
header('Content-type: application/json');
echo json_encode($json);
exit;
}
/*
* Read from file, checks if is readable and if the json is an array.
* @param {String} filename
* @return {json}
*/
function readFileContent($filename) {
if (!is_readable($filename)) {
output(true, null, 'File is not accessible. File:' . $filename);
}
$content = json_decode(file_get_contents($filename), true);
if (!is_array($content)) {
output(true, null, 'JSON in file is invalid');
return;
}
return $content;
}
/*
* Get the data from the version file
*/
function getVersionFile() {
$file = 'plugins/version.json';
$content = readFileContent($file);
return $content;
}
/*
*
*/
function setVersionFile($content) {
$file = 'plugins/version.json';
writeFileContent($file, $content);
}
/*
* Add a new plugin
*/
function addNewPlugin($data) {
$content = getVersionFile();
$id = getUniqueID($data);
$info['name'] = $data['plugin']['name'];
$info['description'] = $data['plugin']['description'];
$info['version'] = $data['plugin']['version'];
$info['developer'] = $data['developer']['name'];
$content['server'][$id] = $info;
$data['id'] = $id;
setVersionFile($content);
return $data;
}
/*
* Update version
*/
function setVersion($id, $version) {
$content = getVersionFile();
$curVersion = $content['server'][$id]['version'];
//Check if the uploaded version is to old
if ($curVersion > $version) {
output(true, null, 'There is already a newer version available (' . $curVersion. ')');
}
$content['server'][$id]['version'] = $version;
setVersionFile($content);
}
/*
* write to file, checks if is readable and if the json is an array.
* @param {String} filename
* @return {json}
*/
function writeFileContent($filename, $content) {
if (!is_writable($filename)) {
output(true, null, 'Can\'t write to file: ' . $filename);
}
file_put_contents($filename, json_encode($content));
return;
}
/*
* Check the permission of a file or item and try to fix it
*
* @param {string} path
* @param {permission} permission
* @param {boolean} fix: try to fix it? (default false)
*/
function checkPermission($path, $permission, $fix = false) {
$current = substr(sprintf('%o', fileperms($path)), -4);
//Check if equals
if ($permission !== $current) {
return false;
}
return true;
}
?>