-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphotoUpload.php
More file actions
262 lines (236 loc) · 8.97 KB
/
photoUpload.php
File metadata and controls
262 lines (236 loc) · 8.97 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scouting App</title>
<link rel="stylesheet" type="text/css" href="Style/scoutingStyle.css">
<body>
<h1><center>Bomb Botz Scouting App</center></h1>
</body>
<p></p>
<h2>
<center><a id="buttons" class="clickme danger" href="index.php">Home</a>
<a id="buttons" class="clickme danger" href="robotAttrList.php">Pit Scout</a></center>
</h2>
<?php
$serverName = getenv("ScoutAppDatabaseServerName");
$database = getenv("Database");
$userName = getenv("DatabaseUserName");
$password = getenv("DatabasePassword");
$connectionOptions = array(
"Database" => "$database",
"Uid" => "$userName",
"PWD" => "$password"
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
// Get Login info
$loginEmailAddress = $_SERVER['HTTP_X_MS_CLIENT_PRINCIPAL_NAME'] ?? getenv("DefaultLoginEmailAddress");
$tsql = "select s.scoutGUID
, s.isAdmin
, g.gameYear
from Scout s
inner join Team t
on t.id = s.teamId
inner join GameEvent ge
on ge.id = t.gameEventId
inner join Game g
on g.id = ge.gameId
where isActive = 'Y' and emailAddress = '$loginEmailAddress'";
$getResults = sqlsrv_query($conn, $tsql);
if ($getResults == FALSE)
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
$row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC);
$loginGUID = $row['scoutGUID'];
$gameYear = $row['gameYear'];
$isAdmin = $row['isAdmin'];
// Handle if logged in user is not active/configured in Scout table
if (empty($loginGUID)) {
$loginEmailAddress = getenv("DefaultLoginEmailAddress");
$tsql = "select s.scoutGUID
, s.isAdmin
, g.gameYear
from Scout s
inner join Team t
on t.id = s.teamId
inner join GameEvent ge
on ge.id = t.gameEventId
inner join Game g
on g.id = ge.gameId
where isActive = 'Y' and emailAddress = '$loginEmailAddress'";
$getResults = sqlsrv_query($conn, $tsql);
if ($getResults == FALSE)
if( ($errors = sqlsrv_errors() ) != null) {
foreach( $errors as $error ) {
echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
echo "code: ".$error[ 'code']."<br />";
echo "message: ".$error[ 'message']."<br />";
}
}
$loginGUID = $row['scoutGUID'];
$gameYear = $row['gameYear'];
$isAdmin = $row['isAdmin'];
}
// Non-Admin should not be on this page
if ($isAdmin != "Y") {
echo '<center>';
echo 'Email: ' . $loginEmailAddress . ' is not authorized on this page.';
echo '</center>';
sqlsrv_close($conn);
echo '</html>';
exit(0);
}
# Reference autoload (assuming you're using Composer)
require_once('vendor/autoload.php');
use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
// Get Query String Parameters
$teamId = $_POST['teamId'];
$teamNumber = $_POST['teamNumber'];
//echo "Team Id: $teamId, Number: $teamNumber.<br />";
$file = $_FILES["fileToUpload"]["name"];
$tmpFile = $_FILES["fileToUpload"]["tmp_name"];
$targetFile = basename($file);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION));
//echo "File: $file, Temp File: $tmpFile, Target File: $targetFile.<br />";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
$mime = $check["mime"];
if($check == false) {
echo "File is not an image.<br />";
$uploadOk = 0;
}
// Check file size < 15MB
$fileSize = $_FILES["fileToUpload"]["size"];
if ($fileSize > 15728640) {
echo "Sorry, your file is too large.<br />";
$uploadOk = 0;
}
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(!in_array($imageFileType, $allowTypes)){
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br />";
$uploadOk = 0;
}
// Compress file
if ($uploadOk !== 0) {
// Compress size and upload image
$destinationFile = "/tmp/" . $file;
$compressedImage = compressImage($tmpFile, $destinationFile, 75);
if (!$compressedImage) {
echo "Image compressed failed!<br />";
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.<br />";
}
// if everything is ok, try to upload file
else {
$storageAccountName = getenv("StorageAccountName");
$containerName = getenv("StorageContainer");
$accessKey = getenv("StorageAccessKey");
// echo "Account Name: $storageAccountName, Container Name: $containerName.<br />";
# Use functions to upload file
$fileNameOnStorage = $gameYear . "/" . $teamNumber . "/" . $teamNumber;
// echo "Calling Function storageAddFile.<br />";
storageAddFile($containerName, $compressedImage, $fileNameOnStorage, $mime, $storageAccountName, $accessKey, $teamNumber);
// echo "Remove the temporary file from the directory.<br />";
unlink($compressedImage);
}
}
# Function to add a file to storagev
function storageAddFile($containerName, $tmpFile, $fileNameOnStorage, $mime, $storageAccountName, $accessKey, $teamNumber) {
// echo "In Function storageAddFile.<br />";
# Setup Azure Storage connection
$connectionString = "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$accessKey";
// echo "Connection String: $connectionString.<br />";
try {
$blobClient = BlobRestProxy::createBlobService($connectionString);
}
catch (Exception $e) {
echo "Failed create Blob Service: " . $e . "<br />";
}
# Open the file
$handle = fopen($tmpFile, "r");
if ($handle) {
// echo "Opened file '" . $tmpFile . "' for upload to storage." . "<br />";
$options = new CreateBlockBlobOptions();
# Identify MIME type
try {
$options->setContentType($mime);
} catch (Exception $e) {
echo "Failed to read MIME from '" . $tmpFile . "': " . $e . "<br />";
}
# Upload the blob
try {
if ($mime) {
$cacheTime = getCacheTimeByMimeType($mime);
if ($cacheTime) {
$options->setCacheControl("public, max-age=" . $cacheTime);
}
}
$blobClient->createBlockBlob($containerName, $fileNameOnStorage, $handle, $options);
} catch (Exception $e) {
echo "Failed to upload file '" . $tmpFile . "' to storage: " . $e . "<br />";
}
#Get List of Blobs
$key = $gameYear . '/' . $teamNumber . '/';
// echo "Blob ".$key.")<br />";
$blobListOptions = new ListBlobsOptions();
$blobListOptions->setPrefix($key);
$blobList = $blobClient->listBlobs($containerName, $blobListOptions);
foreach($blobList->getBlobs() as $key => $blob) {
// echo "Blob ".$key.": \t".$blob->getName()."\t(".$blob->getUrl().")<br />";
echo '<img class="image'.$key.'" src="'.$blob->getUrl().'" style="max-width: 75%;"><br />';
}
return true;
} else {
echo "Failed to open file '" . $tmpFile . "' for upload to storage." . "<br />";
return false;
}
}
# Get cache time by MIME type
function getCacheTimeByMimeType($mime) {
$mime = strtolower($mime);
$types = array(
"application/json" => 604800, // 7 days
// Add more MIME types and cache times as needed
);
return $types[$mime] ?? null;
}
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
$image = imagecreatefromjpeg($source);
}
// Rotate Image to make it portrait
$rotate = imagerotate($image, 270, 0);
// Save image
imagejpeg($rotate, $destination, $quality);
//imagejpeg($image, $destination, $quality);
// Return compressed image
return $destination;
}
?>
</html>