forked from andresamayadiaz/FrontAccountingSimpleAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.php
executable file
·144 lines (131 loc) · 3.57 KB
/
util.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
<?php
/**********************************************
Author: Andres Amaya
Name: REST API Utils
Free software under GNU GPL
***********************************************/
function api_login()
{
$app = \Slim\Slim::getInstance('SASYS');
$app->hook('slim.before', function () use ($app) {
$req = $app->request();
$company = $req->headers('X-COMPANY');
$user = $req->headers('X-USER');
$password = $req->headers('X-PASSWORD');
// TESTING
/*$company = 0;
$user = 'admin';
$password = '123';*/
$succeed = $_SESSION["wa_current_user"]->login(
$company, $user, $password
);
if (!$succeed) {
$app->halt(403, 'Bad Login For Company: ' . $company . ' With User: ' . $user);
}
}, 1);
}
function api_response($code, $body)
{
$app = \Slim\Slim::getInstance('SASYS');
$app->response()->status($code);
if (is_array($body)) {
$body= json_encode($body);
}
$app->response()->body($body);
}
function api_success_response($body)
{
$app = \Slim\Slim::getInstance('SASYS');
api_response(200, $body);
//$app->response()->['Content-Type'] = $content_type;
}
function api_create_response($body)
{
$app = \Slim\Slim::getInstance('SASYS');
api_response(201, $body);
//$app->response()->['Content-Type'] = $content_type;
}
function api_error($code, $msg)
{
$app = \Slim\Slim::getInstance('SASYS');
$app->halt($code, json_encode(array('code' => $code, 'success' => 0, 'msg' => $msg)));
}
function api_ensureAssociativeArray($a)
{
if (!$a) {
$a = array();
}
foreach ($a as $key => $value) {
if (is_int($key)) {
unset($a[$key]);
}
}
return $a;
}
function api_validate_required($property, $model)
{
if (!isset($model[$property])) {
return false;
}
return true;
}
function api_validate_date($property, $model) {
if (!isset($model[$property])) {
return false;
}
$value = $model[$property];
$format = 'Y-m-d';
$dt = \DateTime::createFromFormat($format, $value);
if ($dt) {
$expected = $dt->format($format);
// if ($expected !== $value) {
// error_log(sprintf('Given %s expected %s', $value, $expected), 4);
// }
return $expected == $value;
}
// error_log(sprintf('DateTime::createFromFormat failed with %s for format %s', $value, $format), 4);
return false;
}
function api_validate_message($test, $property)
{
$messages = array(
"api_validate_required" => "Missing a required proprty '$property'",
"api_validate_date" => "Invalid ISO8601 date in '$property' (e.g. 2016-12-31)"
);
if (isset($messages[$test])) {
return $messages[$test];
}
return '';
}
function api_validate($property, $model, $code = 412, $test = 'api_validate_required', $msg = null)
{
if (!is_array($model)) {
$model = array($property => $model);
}
if ($test($property, $model)) {
return;
}
if (!$msg) {
$msg = \api_validate_message($test, $property);
if (!$msg) {
$msg = "Unknown error in '$property'";
}
}
\api_error($code, $msg);
}
/**
* @param string $property
* @param array $model
* @param string|array $default
*/
function api_check($property, &$model, $default = '')
{
if (!isset($model[$property])) {
if (is_array($default)) {
if (array_key_exists($property, $default)) {
$default = $default[$property];
}
}
$model[$property] = $default;
}
}