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
<?php
require_once(__DIR__."/../apl_config.php");
require_once(__DIR__."/../apl_ver.php");
require_once(__DIR__."/../apl_settings.php");
if (isset($_SERVER['REMOTE_ADDR'])) {$ip_address=$_SERVER['REMOTE_ADDR'];}
if (isset($_SERVER['HTTP_REFERER'])) {$refer=$_SERVER['HTTP_REFERER'];}
if (isset($_SERVER['REQUEST_URI'])) {$requested_page=$_SERVER['REQUEST_URI'];}
if (isset($_SERVER['SCRIPT_FILENAME'])) {$script_filename=basename($_SERVER['SCRIPT_FILENAME']);}
if (isset($_SERVER['HTTP_USER_AGENT'])) {$user_agent=$_SERVER['HTTP_USER_AGENT'];}
$action_success=0; //will be changed to 1 later only if everything OK
$error_detected=0; //will be changed to 1 later if error occurs
$error_details=null; //will be filled with errors (if any)
$added_records=0;
$updated_records=0;
$removed_records=0;
$api_action_success=0;
$api_error_detected=0;
$api_error_details=null;
$logged_admin_id=null; //used for compatibility with createReport function in the same file in /apl_admin directory. since admin is not logged in when API is called, $logged_admin_id must be null
if (!empty($_POST) && is_array($_POST) && array_walk($_POST, "sanitizeSubmittedData", array("script_filename"=>$script_filename, "html_fields"=>$FORM_FIELDS_WITH_TAGS))) //sanitize super variable with all POST values
{
extract($_POST, EXTR_SKIP); //extract sanitized data (don't overwrite existing variables)
}
if (empty($client_id) || !filter_var($client_id, FILTER_VALIDATE_INT) || empty($rows_array=fetchRow("SELECT * FROM apl_clients WHERE client_id=?", array($client_id), array("i")))) //invalid record
{
echo "Invalid client ID";
exit();
}
if (!empty($api_key_secret) && !empty($api_function) && $api_post_key==hash("sha256", $ROOT_URL) && $submit_ok=="Submit" && $refer=="$ROOT_URL/apl_api/api.php" && in_array($user_agent, $SUPPORTED_BROWSERS_ARRAY)) //prevent someone from posting to this file directly
{
if ($API_STATUS==1 && in_array($api_function, $SUPPORTED_API_FUNCTIONS_ARRAY))
{
$api_key_rows_array=fetchRow("SELECT * FROM apl_api_keys WHERE api_key_secret=? AND api_key_status=?", array($api_key_secret, 1), array("s", "i"));
if (empty($api_key_rows_array))
{
$api_error_detected=1;
$api_error_details.="Invalid or inactive API key.<br>";
}
else
{
foreach ($api_key_rows_array as $api_key_row)
{
extract($api_key_row);
}
if (!empty($api_key_ip))
{
$api_key_ip_array=explode(",", $api_key_ip);
if (!in_array($ip_address, $api_key_ip_array))
{
$api_error_detected=1;
$api_error_details.="Invalid IP address.<br>";
}
}
$api_permissions_name="api_key_".$api_function; //since each permission in database starts with api_key_ prefix, add this prefix to name of function submitted by user for quick permissions check
if ($$api_permissions_name!=1)
{
$api_error_detected=1;
$api_error_details.="Invalid API key permissions.<br>";
}
if ($api_error_detected!=1 && $$api_permissions_name==1)
{
$api_action_success=1;
}
}
}
else
{
$api_error_detected=1;
$api_error_details.="API not enabled or invalid API function.<br>";
}
if ($api_action_success==1) //everything OK
{
$optional_api_parameters_array=array("banned_host_comments"); //optional API parameters for this page
foreach ($optional_api_parameters_array as $optional_api_parameter) //in case some required parameter was not submitted, set its value to null to prevent "undefined variable" errors
{
if (!isset($$optional_api_parameter))
{
$$optional_api_parameter=null;
}
}
if (isset($submit_ok)) //code between {} tags is identical in files with the same name in /apl_admin and /apl_api directories, EXCEPT header("Location: $page_header_file_no_data"); LINE
{
if (!empty($delete_record) && $delete_record==1)
{
$removed_records=deleteRow("DELETE FROM apl_banned_hosts WHERE banned_host_id=?", array($banned_host_id), array("i"));
if ($removed_records>0)
{
$page_message="Deleted $removed_records banned host(s) from the database.";
createReport(strip_tags($page_message), $logged_admin_id, 1, $error_detected);
echo $page_message; //THIS LINE IS CUSTOM IN API. ORIGINAL CODE CONTAINS header("Location: $page_header_file_no_data"); LINE
exit();
}
else
{
$error_detected=1;
$error_details.="Invalid record or database error.<br>";
}
}
if (filter_var($banned_host_ip, FILTER_VALIDATE_IP))
{
if ($error_detected!=1)
{
$updated_records=updateRow("UPDATE apl_banned_hosts SET banned_host_ip=?, banned_host_comments=? WHERE banned_host_id=?", array($banned_host_ip, $banned_host_comments, $banned_host_id), array("s", "s", "i"));
if ($updated_records<1)
{
$error_detected=1;
$error_details.="Invalid record details or duplicated record (no new data).<br>";
}
else
{
$action_success=1;
}
}
}
else
{
$error_detected=1;
$error_details.="Invalid IP address.<br>";
}
if ($action_success==1) //everything OK
{
$page_message="Banned host $banned_host_ip updated.";
createReport(strip_tags($page_message), $logged_admin_id, 1, $error_detected);
$page_message_class="alert alert-success";
}
else //display error message
{
$page_message="The database could not be updated because of this error: <br><br>$error_details";
$page_message_class="alert alert-danger";
}
}
}
else //display error message
{
$page_message="The action could not be completed because of this error:<br><br>$api_error_details";
}
echo $page_message;
}