Repository navigation
Expand file tree
/
Copy pathprocess.php
More file actions
123 lines (121 loc) · 5.22 KB
/
Copy pathprocess.php
File metadata and controls
123 lines (121 loc) · 5.22 KB
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
<?php
include_once 'functions.php';
$edit_array = null;
if (isset($_POST['form']) && !isset($_POST['cancel'])) {
$db = db_connect();
switch ($_POST['form']) {
case 'add':
$new_values = array_slice($_POST, 1); /// this is pretty unsafe (SQL injection) but since this entry form is for internal use only; I won't bother sanitizing
if ($new_values['year_or_bau'] == 'bau') {
$new_values['rel_to_year'] = null;
}
if ($new_values['country_or_region'] == 'country') {
$new_values['region'] = null;
} else {
$new_values['iso3'] = null;
}
if ($new_values['quantity'] == 'target_Mt') {
$new_values['reduction_percent'] = null;
$new_values['rel_to'] = null;
$new_values['year_or_bau'] = null;
$new_values['rel_to_year'] = null;
} else {
$new_values['target_Mt'] = null;
}
// save empty textboxes as NULL not 0
foreach (array('reduction_percent', 'target_Mt', 'target_Mt_CO2', 'target_Mt_nonCO2', 'target_Mt_LULUCF') as $textbox) {
if (strlen($new_values[$textbox])==0) { $new_values[$textbox] = NULL; }
}
// construct the JSON data array for the caveat field
$json = "";
foreach ($new_values as $key=>$val) {
if (!(strpos($key, "caveat_")===false)) {
if (strlen($val)>0) {
$json .= (strlen($json)==0) ? "\n\n" . "{" : ", ";
$json .= '"' . str_replace("caveat_", "", $key) . '":';
$json .= '"' . str_replace("'","'",str_replace('"','"',trim($val))) . '"';
}
unset($new_values[$key]);
}
}
$json .= (strlen($json)>0) ? "}" : "";
$new_values['caveat'] = trim($new_values['caveat'] . $json);
// The following aren't fields in the database
unset($new_values['country_or_region']);
unset($new_values['db']);
if (isset($new_values['replace'])) {
$do_replace = true;
$edit_id = $new_values['edit_id'];
unset($new_values['replace']);
unset($new_values['edit_id']);
} else {
$do_replace = false;
}
// Check boxes are odd--they just don't appear if unchecked
foreach (array('conditional', 'public', 'include_nonco2', 'include_lulucf') as $checkbox) {
if (array_key_exists($checkbox, $new_values)) {
$new_values[$checkbox] = 1;
} else {
$new_values[$checkbox] = 0;
}
}
foreach ($new_values as $key=>$value) {
if ($value === null) {
$new_values[$key] = 'NULL';
} elseif (!is_numeric($value)) {
// Double up on single quotes if needed; replace any sequence of single quotes, to make
// sure that we don't just keep adding doubled single quotes
$new_values[$key] = "'" . preg_replace("/'+/", "''", $value) . "'";
}
}
if ($do_replace) {
$sql = "UPDATE pledge SET ";
$colvals = array();
foreach ($new_values as $key=>$value) {
$colvals[] = $key . '=' . $value . ' ';
}
$sql .= implode(",", $colvals);
$sql .= 'WHERE id=' .$edit_id . ';';
} else {
$sql = "INSERT INTO pledge (";
$sql .= implode(",", array_keys($new_values));
$sql .= ") VALUE (";
$sql .= implode(",", array_values($new_values));
$sql .= ")";
}
if (!mysql_query($sql, $db)) {
mysql_close($db);
die('Invalid query: ' . mysql_error() . ' from SQL: ' . $sql);
}
break;
case 'table':
foreach ($_POST as $key => $value) {
switch ($value) {
case 'Delete':
$sql = "DELETE FROM pledge WHERE id=" . $key;
mysql_query($sql, $db);
break;
case 'Publish':
$sql = "UPDATE pledge SET public = 1 WHERE id=" . $key;
mysql_query($sql, $db);
break;
case 'Hide':
$sql = "UPDATE pledge SET public = 0 WHERE id=" . $key;
mysql_query($sql, $db);
break;
case 'Edit':
$sql = "SELECT * FROM pledge WHERE id=" . $key;
$result = mysql_query($sql, $db);
$edit_array = mysql_fetch_array($result, MYSQL_ASSOC);
break;
default:
break;
}
}
break;
default:
break;
}
mysql_close($db);
}
?>