forked from ilausuch/ArrestDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_example.php
333 lines (253 loc) · 8.31 KB
/
config_example.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
---------------------------------------
METHODS
---------------------------------------
- GET: Get data from a table or execute a function
- GET_INTERNAL: Access to data in extends operation
- POST: Insert operation
- PUT: Modify operation
- DELETE: Delete operation
---------------------------------------
FILTERS (used in following operations)
---------------------------------------
Filters is an associative array that define when a auth, modifyQuery, postProcess and allow operations will execute
Options:
- method (string or array) : what method match [GET, GET_INTERNAL (in extends use), POST, PUT, DELETE
- no_method (string or array) : inverted method filter
- table (string or array) : what table must match
- no_table (string or array) : inverted table filter
- id_defined ('yes' or 'no') : if table identifier is defined or not in http query
- id (string or array) : if table identifier is in list
- no_id (string or array) : inverted table filter
For instance, execute when table is User or Category when use POST method
[
"table"=>["User","Category"],
"method"=>"POST"
]
---------------------------------------
QUERIES
---------------------------------------
TABLE
WHERE
VALUES
*/
/*
Configure DB access
Example MYSQL: mysql://user:pass@localhost/dbname/
SQLite: $dsn = 'sqlite://./path/to/database.sqlite';
MySQL: $dsn = 'mysql://[user[:pass]@]host[:port]/db/;
PostgreSQL: $dsn = 'pgsql://[user[:pass]@]host[:port]/db/;
With bad configuration any operation returns:
{
"error":
{
"code": 503,
"status": "Service Unavailable"
}
}
*/
$dsn = '';
/*
Allow only access from a list of clients (OPTIONAL)
*/
$clients = [];
/*
Define path where API is configured. (OPTIONAL, by default '')
For instance, if you have a 'api' folder in your website path you should access using this url: http://mydomain.com/api
so you must configure $prefix using $prefix = '/api';
*/
$prefix = '';
/*
Allows to connect from any origin. (OPTIONAL, by default true)
By default is true
*/
$allowAnyOrigin=true;
/*
Allows OPTIONS method (OPTIONAL, by default true)
*/
$enableOptionsRequest=true;
/*
ALIASES (OPTIONAL)
Define table aliases. An table alias can get diferent GET,POST,PUT and DELETE conditions and can be used in all following operations
ArrestDBConfig::alias($alias,$table);
*/
ArrestDBConfig::alias("CategoryVisible","Category");//This is an example, remove it
/*
RELATIONS (OPTIONAL)
Create relations for use extends in GET queries. This allow to get objects an related objects.
Examples
------------
- with objects: Each product has only one category
ArrestDBConfig::relation("Product","Category",ArrestDBConfig::prepareRelationObject("Category","Category_id"));
- with lists: Each category has a list of products.
ArrestDBConfig::relation("Category","Products",ArrestDBConfig::prepareRelationList("Product","Category_id"));
Details
------------
ArrestDBConfig::relation($table,$name,$config)
- $table: the table name (equal to table name) witch contains relation
- $name: the variable where relation is loaded
Configuration
Objects (one to one, * to one), prepareRelationObject($foreignTable,$key,$foreingKey="id")
- $foreignTable: Related table name (equal to table name)
- $key: Table identifier key of relation
- $foreingKey: Foreign table indentifier key of relation, by default "id"
List (one to *), prepareRelationList($foreignTable,$foreingKey,$key="id")
- $foreignTable: Related table name (equal to table name)
- $foreingKey: Foreign table indentifier key of relation
- $key: Table identifier key of relation, by default "id"
*/
ArrestDBConfig::relation("Category","Products",ArrestDBConfig::prepareRelationList("Product","Category_id")); //This is an example, remove it
/*
AUTH (OPTIONAL)
Check if authorization is required to access to a table or function. Return true if is authorized. By default all is authorized
ArrestDBConfig::auth($filter,$function)
- $filter: define the filter
- $function: define callback function(returns true or false), function($method,$table,$id){return true}
If you define a list of auth methods, when system match with one, all afther that are not checked
*/
//This is an example where authorization is requiered for all tables except for CategoryVisible that is always authorized
ArrestDBConfig::auth(
[
"table"=>"Category"
],
function($method,$table,$id){
return true;
});
ArrestDBConfig::auth(
[],
function($method,$table,$id){
global $user;
if (!isset($_SERVER['PHP_AUTH_USER'])||!isset($_SERVER['PHP_AUTH_PW'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Invalid Auth';
exit;
} else {
$user=$_SERVER['PHP_AUTH_USER'];
$pass=sha1($_SERVER['PHP_AUTH_PW']);
$query=ArrestDB::PrepareQueryGET([
"TABLE"=>"User",
"WHERE"=>["email='$user'","password='$pass'"]
]);
$result=ArrestDB::Query($query);
if (count($result)==0){
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Invalid Auth';
exit;
}
$user=$result[0];
return true;
}
});
/*
ALLOW (OPTIONAL)
It's similar to auth but it's used in other cases when is checked out if it's allowed to execute a method over a table or function. Return true if is allowed. By default all is allowed
ArrestDBConfig::allow($filter,$function)
- $filter: define the filter
- $function: define callback function(returns true or false), function($method,$table,$id){return true}
If you define a list of allow methods, when system match with one, all afther that are not checked
*/
//In this example is not allowed Access directly to UserInfo or do deletes
ArrestDBConfig::allow(
[
"table"=>"UserInfo",
"method"=>"GET"
],
function ($method,$table,$id){
return false;
});
ArrestDBConfig::allow(
[
"method"=>"DELETE"
],
function ($method,$table,$id){
return false;
});
/*
MODIFY QUERY (OPTIONAL)
Modify a query before execute this.
In GET, GET_INTERNAL methods you can modify
- SELECT atributes (string)
- WHERE conditions (array)
- TABLE name
- ORDER BY
- LIMIT
- OFFSET
In POST and PUT methods you can modify
- VALUES (array)
function ($method,$table,$id,$query)
- $method
- $table
- $id
- $query: query values
*/
//In this example only it's viewed non deleted tables
ArrestDBConfig::modifyQuery(
[
"method"=>["GET","GET_INTERNAL"]
],
function ($method,$table,$id,$query){
$query["WHERE"][]="deleted=0";
return $query;
});
//In this example when a new user is created password is ofuscated with MD5 and is added a new value
ArrestDBConfig::modifyQuery(
[
"table"=>"User",
"method"=>"POST"
],
function ($method,$table,$id,$query){
$query["VALUES"]["password"]=md5($query["VALUES"]["password"]);
$query["VALUES"]["createdByApi"]=1;
return $query;
});
/*
POST PROCESS (Optional)
It's called after an operation, and it allows to modify result or do any operation before send to caller
function($method,$table,$id,$data)
- $method
- $table
- $id: In POST case, $id is the id of created object.
- $data: Data to return. Data can be an array or object (as array). See the following example to understand how to act in each case.
*/
//In this case remove password on User table before return the data
ArrestDBConfig::postProcess(
[
"table"=>"User",
"method"=>["GET","GET_INTERNAL"],
],
function($method,$table,$id,$data){
if (isset($data[0]))
foreach ($data as $k=>$item)
unset($item["password"]);
else
unset($data["password"]);
return $data;
});
//In this case when a new user is created, it's inserted
ArrestDBConfig::postProcess(
[
"method"=>"POST",
"table"=>"User"
],
function($method,$table,$id,$data){
if (isset($_GET["Group_id"])){
$group_id=$_GET["Group_id"];
ArrestDB::query("INSERT INTO UserInGroup(Group_id,User_id) VALUES ({$group_id},{$id})");
}
return $data;
});
/**
CALL function (optional)
Allows to call a function to do complex operations. All functions use POST method. Remember this when you'll call it.
function ($func,$data)
- $func: function name
- $data: values in $_POST variable
*/
//In this case
ArrestDBConfig::fnc("sendMsg",
function ($func,$data){
return sendMsg($data);
});