forked from infinitesunrise/carsinbikelanes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission.php
188 lines (160 loc) · 6.4 KB
/
submission.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
<?php
function new_upload($image,
$plate,
$state,
$date_occurrence,
$date_added,
$gps_latitude,
$gps_longitude,
$street1,
$street2,
$description)
{
require 'admin/config_pointer.php';
date_default_timezone_set('UTC');
error_log('submission.php - $date_occurrence inbound: ' . $date_occurrence);
error_log('submission.php - $date_added inbound: ' . $date_added);
if (empty($image))
{ return array('error' => 'Submissions without an image attached are currently not accepted.'); }
if (!getimagesize($image['tmp_name']))
{ return array('error' => 'Image attachment must be an image file.'); }
if (empty($plate))
{ return array('error' => 'Plate field cannot be empty.'); }
if (!is_string($plate))
{ return array('error' => 'Plate field must be a string'); }
$plate = preg_replace("/[^a-zA-Z0-9]+/", "", $plate); //a-z, A-Z, 0-9 only
$plate = substr($plate, 0, 8); //Max plate length 8 characters
if (empty($state))
{ return array('error' => 'State field cannot be empty.'); }
if (!is_string($state))
{ return array('error' => 'State field must be a string'); }
$occurrence_check = new DateTime($date_occurrence);
if (!$occurrence_check){
return array('error' => 'Did not understand the date of occurrence value ' . $date_occurrence . '. Dates must be valid ISO8601 strings.');
}
$added_check = new DateTime($date_added);
if (!$added_check){
return array('error' => 'Did not understand the date added value ' . $date_added . '. Dates must be valid ISO8601 strings.');
}
//if (!is_timestamp($date_occurrence))
//{ $date_occurrence = strtotime($date_occurrence); }
//if (!$date_occurrence)
//{ return array('error' => 'Unacceptable date value.'); }
//$date_occurrence = date('Y-m-d H:i:s', $occurrence_check);
//$date_added = date('Y-m-d H:i:s', $added_check);
//error_log('submission.php - $date_occurrence conversion: ' . $date_occurrence);
//error_log('submission.php - $date_added conversion: ' . $date_added);
//error_log($date_occurrence);
if (empty($gps_latitude) || empty($gps_longitude))
{ return array('error' => 'Both latitude and longitude are required.'); }
if (!is_numeric($gps_latitude) || !is_numeric($gps_longitude))
{ return array('error' => 'GPS values must be numeric.'); }
//Optional fields
if ($street1){
if (!is_string($street1))
{ return array('error' => 'Street fields must be strings.'); }
}
if ($street2){
if (!is_string($street2))
{ return array('error' => 'Street fields must be strings.'); }
}
if ($description){
if (!is_string($description))
{ return array('error' => 'Description field must be a string.'); }
}
//Validate GPS coordinates against project area
if (!verify_coordinates($gps_latitude, $gps_longitude, $config)){
return array('error' => 'GPS coordinates are not within the project zone. Latitude must be between '
. $config['south_bounds'] . ' and ' . $config['north_bounds']
. ', longitude must be between ' . $config['west_bounds'] . ' and ' . $config['east_bounds']);
}
//Get the next entry ID number
$increment = get_increment($connection);
if (!$increment){
return array('error' => 'Server error, please alert the site administrator.');
}
//Create image directory, save out image and thumbnail
$url = save_images($image, $increment);
if (!$url){
return array('error' => 'Server error, please alert the site administrator.');
}
$stmt = $connection->prepare("INSERT INTO cibl_queue (
increment, url, plate, state, date_occurrence, date_added, gps_lat, gps_long, street1, street2, description)
VALUES (?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('isssssddsss', $increment, $url, $plate, $state, $date_occurrence, $date_added, $gps_latitude, $gps_longitude, $street1, $street2, $description);
$result = $stmt->execute();
$stmt->close();
if (!$result) {
error_log($connection->error);
return array('error' => 'Server error, please alert the site administrator.');
}
$submission_details = array(
'id' => $increment,
'plate' => $plate,
'state' => $state,
'date_occurrence' => $date_occurrence,
'date_added' => $date_added,
'lat' => $gps_latitude,
'lon' => $gps_longitude,
'street1' => $street1,
'street2' => $street2,
'description' => $description
);
$email_op = 'new_submission';
include 'email_notify.php';
return array('success' =>
'Thank you for contributing! All submissions require moderator approval before being added to the map. Expect yours to show up within 24 hours.');
}
function verify_coordinates($gps_latitude, $gps_longitude, $config){
if ( $gps_latitude > $config['north_bounds'] ||
$gps_latitude < $config['south_bounds'] ||
$gps_longitude > $config['east_bounds'] ||
$gps_longitude < $config['west_bounds'] ){
return false;
}
return true;
}
function get_increment($connection){
try{
$target_increment1 = mysqli_fetch_array(mysqli_query($connection, "SELECT MAX(increment) AS increment FROM cibl_data"))[0] + 1;
$target_increment2 = mysqli_fetch_array(mysqli_query($connection, "SELECT MAX(increment) AS increment FROM cibl_queue"))[0] + 1;
$target_increment = ($target_increment1 > $target_increment2) ? $target_increment1 : $target_increment2;
return $target_increment;
}
catch (Exception $e){
return false;
}
}
function save_images($image, $increment){
try{
$now = getdate();
//DETERMINE TARGET FILE NAME
$target_dir = $now['year'] . "/" . $now['mon'] . "/" . $now['mday'] . "/";
$target_extension = pathinfo(basename($image['name']), PATHINFO_EXTENSION);
$url = $target_dir . "queue_" . $increment . "." . $target_extension;
$target_image = __DIR__ . "/images/" . $url;
$target_thumb = __DIR__ . "/thumbs/" . $url;
//CREATE DIRECTORIES
$image_dir = __DIR__ . "/images/" . $target_dir;
$thumbs_dir = __DIR__ . "/thumbs/" . $target_dir;
if (!is_dir($image_dir)){ mkdir($image_dir, 0777, true); }
if (!is_dir($thumbs_dir)){ mkdir($thumbs_dir, 0777, true); }
//RESIZE AND MOVE RENAMED IMAGE INTO PLACE
$imagick = new Imagick($image['tmp_name']);
$imagick->writeImage($target_image);
$imagick->scaleImage(200, 200, true);
$imagick->writeImage($target_thumb);
}
catch (Exception $e){
return false;
}
return $url;
}
function is_timestamp($timestamp) //thanks, stackoverflow... http://stackoverflow.com/questions/4123541/var-is-valid-unix-timestamp
{
return ((string) (int) $timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX)
&& (!strtotime($timestamp));
}
?>