|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
class ApiMethod { |
|
20
|
|
|
|
|
21
|
|
|
protected $apimethods = array(); |
|
22
|
|
|
protected $method = array(); |
|
23
|
|
|
|
|
24
|
|
|
public function __construct () { |
|
25
|
|
|
// |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function loadApiMethods () { |
|
29
|
|
|
|
|
30
|
|
|
if (Cache::contains("apimethods", "apimethods")) { |
|
31
|
|
|
$this->apimethods = Cache::get("apimethods", "apimethods"); |
|
32
|
|
|
} else { |
|
33
|
|
|
$rows = (Array) DataBase::getInstance()->listRows("SELECT * FROM `{praefix}api_methods` WHERE `activated` = '1'; "); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($rows as $row) { |
|
36
|
|
|
$row = (Array) $row; |
|
37
|
|
|
$this->apimethods[$row['api_method']] = $row; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
Cache::put("apimethods", "apimethods", $this->apimethods); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function loadMethod ($method) { |
|
46
|
|
|
if (isset($this->apimethods[$method])) { |
|
47
|
|
|
$this->method = $this->apimethods[$method]; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function executeApiMethod () { |
|
52
|
|
|
|
|
53
|
|
|
if (!$this->method) { |
|
|
|
|
|
|
54
|
|
|
exit; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->method['response_type'] != "specific") { |
|
58
|
|
|
header("Content-Type: " . $this->method['response_type']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$classname = $this->method['classname']; |
|
62
|
|
|
$method = $this->method['method']; |
|
63
|
|
|
|
|
64
|
|
|
$args = array(); |
|
65
|
|
|
Events::throwEvent("apimethods", array('method' => $this->method, 'args' => &$args)); |
|
66
|
|
|
|
|
67
|
|
|
$result = call_user_func(array($classname, $method), $args); |
|
68
|
|
|
|
|
69
|
|
|
echo $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function addMethod (string $api_method, string $class_name, string $method, string $owner = "system", string $response_type = "") { |
|
73
|
|
|
//add method to database |
|
74
|
|
|
Database::getInstance()->execute("INSERT INTO `{praefix}api_methods` ( |
|
75
|
|
|
`api_method`, `classname`, `method`, `response_type`, `owner`, `activated` |
|
76
|
|
|
) VALUES ( |
|
77
|
|
|
:api_method, :class_name, :method, :response_type, :owner, '1' |
|
78
|
|
|
) ON DUPLICATE KEY UPDATE `classname` = :class_name, `method` = :method, `response_type` = :response_type, `owner` = :owner, `activated` = '1'; ", array( |
|
79
|
|
|
'api_method' => $api_method, |
|
80
|
|
|
'class_name' => $class_name, |
|
81
|
|
|
'method' => $method, |
|
82
|
|
|
'response_type' => $response_type, |
|
83
|
|
|
'owner' => $owner |
|
84
|
|
|
)); |
|
85
|
|
|
|
|
86
|
|
|
//clear cache |
|
87
|
|
|
Cache::clear("apimethods"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public static function deleteMethod (string $api_method) { |
|
91
|
|
|
//delete from database |
|
92
|
|
|
Database::getInstance()->execute("DELETE FROM `{praefix}api_methods` WHERE `api_method` = :api_method; ", array( |
|
93
|
|
|
'api_method' => $api_method |
|
94
|
|
|
)); |
|
95
|
|
|
|
|
96
|
|
|
//clear cache |
|
97
|
|
|
Cache::clear("apimethods"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function deleteMethodsByOwner (string $owner) { |
|
101
|
|
|
//delete from database |
|
102
|
|
|
Database::getInstance()->execute("DELETE FROM `{praefix}api_methods` WHERE `owner` = :owner; ", array( |
|
103
|
|
|
'owner' => $owner |
|
104
|
|
|
)); |
|
105
|
|
|
|
|
106
|
|
|
//clear cache |
|
107
|
|
|
Cache::clear("apimethods"); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
?> |
|
|
|
|
|
|
113
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.