|
| 1 | +<?php |
| 2 | +$mysql = new Mysql($db, "localhost", $dbun, $dbpw, 3306); |
| 3 | + |
| 4 | +function addUser($userData) |
| 5 | +{ |
| 6 | + global $mysql; |
| 7 | + $id = $userData["id"]; |
| 8 | + $givenName=$userData["givenName"]; |
| 9 | + $link=$userData["link"]; |
| 10 | + $email=$userData["email"]; |
| 11 | + $picture=$userData["picture"]; |
| 12 | + $mysql->insert_update("googleUsers",array("id"=>$id,"givenName"=>$givenName,"link"=>$link,"email"=>$email,"picture"=>$picture),array("id")); |
| 13 | + /*if($mysql->num_entries("SELECT id FROM googleUsers WHERE id='$id'") == 0) |
| 14 | + { |
| 15 | + $mysql->query("INSERT INTO googleUsers(id, givenName, link, picture, email) VALUES('$id','$givenName','$link','$picture','$email')"); |
| 16 | + } |
| 17 | + else |
| 18 | + { |
| 19 | + $mysql->query("UPDATE googleUsers SET givenName='$givenName', link='$link', email='$email', picture='$picture' WHERE id='$id'"); |
| 20 | + }*/ |
| 21 | +} |
| 22 | + |
| 23 | +function printVoteImage($button, $score) |
| 24 | +{ |
| 25 | + $image = "vote"; |
| 26 | + switch($button) |
| 27 | + { |
| 28 | + case -1: $image .= "Down"; break; |
| 29 | + case 0: $image .= "Null"; break; |
| 30 | + case 1: $image .= "Up"; break; |
| 31 | + } |
| 32 | + if($button != 0) |
| 33 | + { |
| 34 | + if($score == $button) |
| 35 | + $image .= "On"; |
| 36 | + else |
| 37 | + $image .= "Off"; |
| 38 | + } |
| 39 | + $image .= ".png"; |
| 40 | + echo "<input type='image' src='$image'>"; |
| 41 | +} |
| 42 | + |
| 43 | +function printVotes($schemaID,$schemaScore) |
| 44 | +{ |
| 45 | + global $mysql; |
| 46 | + global $userData; |
| 47 | + global $isLoggedIn; |
| 48 | + $score = 0; |
| 49 | + if($isLoggedIn) |
| 50 | + { |
| 51 | + $data = $mysql -> fetch_array("SELECT score FROM schematicVotes WHERE userID='".$userData["id"]."' AND schemaID='$schemaID'",MYSQLI_NUM,true); |
| 52 | + if(count($data) > 0) |
| 53 | + $score = $data[0]; |
| 54 | + } |
| 55 | + for($i = -1; $i <=1; $i++) |
| 56 | + { |
| 57 | + echo "<form method=GET>"; |
| 58 | + echo "<input type='hidden' name='v' value='$i'>"; |
| 59 | + echo "<input type='hidden' name='vs' value='$schemaID'>"; |
| 60 | + printVoteImage($i,$score); |
| 61 | + echo "</form>"; |
| 62 | + } |
| 63 | + if($schemaScore == null) |
| 64 | + $schemaScore = 0; |
| 65 | + echo "<br>Votes: " . $schemaScore; |
| 66 | +} |
| 67 | + |
| 68 | +function printSchemaBox($page) |
| 69 | +{ |
| 70 | + global $mysql; |
| 71 | + $schemaFolder = "/home/web/schema"; |
| 72 | + $schemaUrl = "http://foxpotato.com/schema"; |
| 73 | + $total = $mysql -> fetch_array("SELECT count(ID) FROM schematics",MYSQL_NUM,true)[0]; |
| 74 | + $numPerPage = 50; |
| 75 | + $index = $numPerPage * $page; |
| 76 | + $data = $mysql->fetch_array("SELECT * FROM schematics ORDER BY score DESC LIMIT $index,$numPerPage", MYSQLI_ASSOC, false); |
| 77 | + echo "<div class='schemaBox'>"; |
| 78 | + echo "<table>"; |
| 79 | + for($i = 0; $i<count($data); $i++) |
| 80 | + { |
| 81 | + $dataEntry = $data[$i]; |
| 82 | + $ownerEntry = getUserData($dataEntry["userID"]); |
| 83 | + echo "<tr><td class='schemaUserTD'><img class='smallUserIcon' src='". $ownerEntry["picture"] . "'/><br>".$ownerEntry["givenName"]."</td>"; |
| 84 | + echo "<th>Schematic Name:<td>" . str_replace(".schema","",$dataEntry["name"]); |
| 85 | + echo "<th>Blocks:<td>" . $dataEntry["blocks"]; |
| 86 | + echo "<th>Size:<td>" . $dataEntry["bounds"]; |
| 87 | + echo "<tr><td><th>Modified:<td>".date("F d Y H:i:s",filemtime($dataEntry["fileName"])); |
| 88 | + echo "<th colspan=3>"; |
| 89 | + printVotes($dataEntry["id"],$dataEntry["score"]); |
| 90 | + echo "<td><a href='".str_replace($schemaFolder,$schemaUrl,$dataEntry["fileName"])."'>Download here</a>"; |
| 91 | + echo "<tr><td><td colspan=6>".$dataEntry["description"]."</tr>"; |
| 92 | + } |
| 93 | + echo "</table>"; |
| 94 | + echo "</div>"; |
| 95 | + return $total > ($index + $numPerPage); |
| 96 | +} |
| 97 | + |
| 98 | +function handleUpload() |
| 99 | +{ |
| 100 | + global $userData; |
| 101 | + global $mysql; |
| 102 | + if(isset($_POST["submit"])) |
| 103 | + { |
| 104 | + $fileData = $_FILES['schemaToUpload']; |
| 105 | + switch($fileData['error']) |
| 106 | + { |
| 107 | + case UPLOAD_ERR_OK: break; |
| 108 | + default: echo "File upload failed<br>";return; |
| 109 | + } |
| 110 | + if($fileData['size'] >= 500000) |
| 111 | + { |
| 112 | + echo "File too large"; |
| 113 | + return; |
| 114 | + } |
| 115 | + $data = readNBTFile($fileData['tmp_name']); |
| 116 | + if(!isSchemaFile($data)) |
| 117 | + { |
| 118 | + echo "Invalid schema file!"; |
| 119 | + return; |
| 120 | + } |
| 121 | + $name = $fileData["name"]; |
| 122 | + $bounds = getBounds($data); |
| 123 | + $bounds = "X:".$bounds[0] . " Y:" .$bounds[1] . " Z:".$bounds[2]; |
| 124 | + $blocks = getSize($data); |
| 125 | + $desc = isset($_POST["desc"]) ? $_POST["desc"] : "No description"; |
| 126 | + $userID = $userData["id"]; |
| 127 | + //$id = $mysql->query_id("INSERT INTO schematics(name,bounds,userID,blocks,description) VALUES('$name','$bounds','$userID','$blocks','$desc')"); |
| 128 | + $id = $mysql->insert_update("schematics",array("name"=>$name,"bounds"=>$bounds,"userID"=>$userID,"blocks"=>$blocks,"description"=>$desc),array("userID","name")); |
| 129 | + $fileName = "/home/web/schema/" . $userID. "/".$id; |
| 130 | + if(!file_exists($fileName)) |
| 131 | + mkdir($fileName,0755,true); |
| 132 | + $fileName .= "/" . $name; |
| 133 | + $mysql->query("UPDATE schematics SET fileName='$fileName' WHERE id='" . $id."'"); |
| 134 | + if(!move_uploaded_file($fileData['tmp_name'],$fileName)) |
| 135 | + { |
| 136 | + $mysql->query("DELETE FROM schematics WHERE id='$id'"); |
| 137 | + echo "Upload failed"; |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + echo "Upload successful!<br>"; |
| 142 | + touch($fileName); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +function printFileUploadBox() |
| 150 | +{ |
| 151 | + global $isLoggedIn; |
| 152 | + echo "<div class='box uploadBox'>"; |
| 153 | + if($isLoggedIn) |
| 154 | + { |
| 155 | + handleUpload(); |
| 156 | + echo "<form method='POST' enctype='multipart/form-data'>"; |
| 157 | + echo "Upload a schematic<br>"; |
| 158 | + echo "<input type='file' name='schemaToUpload'><br>"; |
| 159 | + echo "<textarea name='desc' rows=5 cols=50 placeholder='Enter a description for the schematic'></textarea><br>"; |
| 160 | + echo "<input type='submit' value='Upload Schema' name='submit'>"; |
| 161 | + echo "</form>"; |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + echo "You must be logged in to upload schematics!"; |
| 166 | + } |
| 167 | + echo "</div>"; |
| 168 | +} |
| 169 | + |
| 170 | +$userDataArray = array(); |
| 171 | +function getUserData($id) |
| 172 | +{ |
| 173 | + global $mysql; |
| 174 | + global $userDataArray; |
| 175 | + if(array_key_exists($id,$userDataArray)) |
| 176 | + return $userDataArray[$id]; |
| 177 | + $data = $mysql->fetch_array("SELECT * FROM googleUsers WHERE id='$id'", MYSQLI_ASSOC); |
| 178 | + $userDataArray[$id] = $data; |
| 179 | + return $data; |
| 180 | +} |
| 181 | + |
| 182 | +function vote($schemaID,$score) |
| 183 | +{ |
| 184 | + global $isLoggedIn; |
| 185 | + global $userData; |
| 186 | + global $mysql; |
| 187 | + if($isLoggedIn) |
| 188 | + { |
| 189 | + $score = min(1,max(-1,$score)); |
| 190 | + if($score != 0) |
| 191 | + $mysql->insert_update("schematicVotes",array("userID"=>$userData["id"],"schemaID"=>$schemaID,"score"=>$score),array("userID","schemaID")); |
| 192 | + else |
| 193 | + $mysql->query("DELETE FROM schematicVotes WHERE userID='".$userData["id"]."' AND schemaID='$schemaID'"); |
| 194 | + $mysql->query("UPDATE schematics SET score=(SELECT sum(score) FROM schematicVotes WHERE schemaID='$schemaID') WHERE id='$schemaID'"); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function handleVotes() |
| 199 | +{ |
| 200 | + if(isset($_GET["v"]) && isset($_GET["vs"])) |
| 201 | + { |
| 202 | + vote($_GET["vs"],$_GET["v"]); |
| 203 | + } |
| 204 | +} |
| 205 | +?> |
0 commit comments