-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_api.php
190 lines (173 loc) · 5.44 KB
/
admin_api.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
<?php
/***
* Handle admin-specific requests
***/
$print_login_state = false;
require_once("CONFIG.php");
require_once(dirname(__FILE__)."/core/core.php");
$db = new DBHelper($default_database,$default_sql_user,$default_sql_password,$default_sql_url,$default_table,$db_cols);
require_once(dirname(__FILE__)."/admin/async_login_handler.php");
$start_script_timer = microtime_float();
if(!function_exists('elapsed'))
{
function elapsed($start_time = null)
{
/***
* Return the duration since the start time in
* milliseconds.
* If no start time is provided, it'll try to use the global
* variable $start_script_timer
*
* @param float $start_time in unix epoch. See http://us1.php.net/microtime
***/
if(!is_numeric($start_time))
{
global $start_script_timer;
if(is_numeric($start_script_timer)) $start_time = $start_script_timer;
else return false;
}
return 1000*(microtime_float() - (float)$start_time);
}
}
$admin_req=isset($_REQUEST['perform']) ? strtolower($_REQUEST['perform']):null;
$login_status = getLoginState($get);
if($login_status["status"] !== true) {
$login_status["error"] = "Invalid user";
$login_status["human_error"] = "You're not logged in as a valid user to edit this. Please log in and try again.";
returnAjax($login_status);
}
switch($admin_req)
{
# Stuff
case "save":
returnAjax(saveEntry($_REQUEST));
break;
case "new":
returnAjax(newEntry($_REQUEST));
break;
case "delete":
returnAjax(deleteEntry($_REQUEST));
break;
default:
returnAjax(getLoginState($_REQUEST,true));
}
function saveEntry($get)
{
/***
* Save a new taxon entry
***/
$data64 = $get["data"];
$enc = strtr($data64, '-_', '+/');
$enc = chunk_split(preg_replace('!\015\012|\015|\012!','',$enc));
$enc = str_replace(' ','+',$enc);
$data_string = base64_decode($enc);
$data = json_decode($data_string,true);
if(!isset($data["id"]))
{
# The required attribute is missing
$details = array (
"original_data" => $data64,
"decoded_data" => $data_string,
"data_array" => $data
);
return array("status"=>false,"error"=>"POST data attribute \"id\" is missing","human_error"=>"The request to the server was malformed. Please try again.","details"=>$details);
}
# Add the perform key
global $db;
$ref = array();
$ref["id"] = $data["id"];
unset($data["id"]);
try
{
$result = $db->updateEntry($data,$ref);
# Now, we want to do image processing if an image was alerted
$imgDetails = false;
if(!empty($data["image"])) {
$img = $data["image"];
$imgDetails = array("has_provided_img"=>true);
# Process away!
$file = dirname(__FILE__)."/".$img;
$imgDetails["file_path"] = $file;
$imgDetails["relative_path"] = $img;
if(file_exists($file))
{
$imgDetails["img_present"] = true;
# Resize away
try
{
$i = new ImageFunctions($file);
$thumbArr = explode(".",$img);
$extension = array_pop($thumbArr);
$outputFile = dirname(__FILE__)."/".implode(".",$thumbArr)."-thumb.".$extension;
$imgDetails["resize_status"] = $i->resizeImage($outputFile,256,256);
}
catch(Exception $e)
{
$imgDetails["resize_status"] = false;
$imgDetails["resize_error"] = $e->getMessage();
}
}
else
{
$imgDetails["img_present"] = false;
}
}
}
catch(Exception $e)
{
return array("status"=>false,"error"=>$e->getMessage(),"humman_error"=>"Database error saving","data"=>$data,"ref"=>$ref,"perform"=>"save");
}
if($result !== true)
{
return array("status"=>false,"error"=>$result,"human_error"=>"Database error saving","data"=>$data,"ref"=>$ref,"perform"=>"save");
}
return array("status"=>true,"perform"=>"save","data"=>$data, "img_details"=>$imgDetails);
}
function newEntry($get)
{
/***
* Create a new taxon entry
*
*
* @param data a base 64-encoded JSON string of the data to insert
***/
$data64 = $get["data"];
$enc = strtr($data64, '-_', '+/');
$enc = chunk_split(preg_replace('!\015\012|\015|\012!','',$enc));
$enc = str_replace(' ','+',$enc);
$data_string = base64_decode($enc);
$data = json_decode($data_string,true);
# Add the perform key
global $db;
try
{
$result = $db->addItem($data);
}
catch(Exception $e)
{
return array("status"=>false,"error"=>$e->getMessage(),"humman_error"=>"Database error saving","data"=>$data,"ref"=>$result,"perform"=>"new");
}
if($result !== true)
{
return array("status"=>false,"error"=>$result,"human_error"=>"Database error saving","data"=>$data,"ref"=>$result,"perform"=>"new");
}
return array("status"=>true,"perform"=>"new","data"=>$data);
}
function deleteEntry($get)
{
/***
* Delete a taxon entry
* Delete an entry described by the ID parameter
*
* @param $get["id"] The DB id to delete
***/
global $db;
$id = $get["id"];
$result = $db->deleteRow($id,"id");
if ($result["status"] === false)
{
$result["human_error"] = "Failed to delete item '$id' from the database";
}
return $result;
}
?>