|
| 1 | +<?php |
| 2 | + |
| 3 | +$getParams = array( |
| 4 | + //If this is present, the rest is parsed. |
| 5 | + /* |
| 6 | + The higher in the array, the higher priority the parameter is.. |
| 7 | + This means you should put the stuff that requires multiple parameters at the top. |
| 8 | + */ |
| 9 | + |
| 10 | + "track" => // If this is present as a GET parameter, the rest are parsed |
| 11 | + array( |
| 12 | + "track" => "string", // We want to get the value of "tracked" as a string, |
| 13 | + "_js" => '$("#playerSelect").val("%s"); $("#playerSelect").change();' // the parameters above will be parsed into this string (see "trackAndZoom") |
| 14 | + ), |
| 15 | + |
| 16 | + /* |
| 17 | + Example query |
| 18 | +
|
| 19 | + ?trackAndZoom&id=ip:127.0.0.1&zoom=3 |
| 20 | + ?trackAndZoom&id=steam:11000010573D0E2&zoom=6 |
| 21 | + */ |
| 22 | + "trackAndZoom" => |
| 23 | + array( |
| 24 | + "id" => "string", |
| 25 | + "zoom" => "int", |
| 26 | + "_js" => '$("#playerSelect").val("%s"); $("#playerSelect").change(); map.setZoom(%d);' |
| 27 | + ), |
| 28 | + |
| 29 | + // Zoom won't be ran if "trackAndZoom" is ran because the "zoom" parameter is unset |
| 30 | + "zoom" => |
| 31 | + array( |
| 32 | + "zoom" => "int", |
| 33 | + "_js" => "map.setZoom(%d);" |
| 34 | + ), |
| 35 | + |
| 36 | +); |
| 37 | + |
| 38 | +$javascriptToPrint = array(); |
| 39 | + |
| 40 | +// Get any parameters and do stuff.. I guess |
| 41 | + |
| 42 | +foreach ($getParams as $key => $parameters) { |
| 43 | + |
| 44 | + if (isset($_GET[$key])){ |
| 45 | + echo "$key is set<br/>"; |
| 46 | + $gotParams = array(); |
| 47 | + $javascript = ""; |
| 48 | + |
| 49 | + foreach ($parameters as $paramName => $type) { |
| 50 | + if ($paramName == "_js"){ |
| 51 | + $javascript = $type; |
| 52 | + continue; |
| 53 | + } |
| 54 | + echo "Checking $paramName<br/>"; |
| 55 | + if(isset($_GET[$paramName])){ // Been set, check it and add it |
| 56 | + |
| 57 | + $val = $_GET[$paramName]; |
| 58 | + |
| 59 | + settype($val, $type); |
| 60 | + |
| 61 | + array_push($gotParams, $val); |
| 62 | + unset($_GET[$paramName]); |
| 63 | + } |
| 64 | + } |
| 65 | + // Now that we have each parameter, format the JS code and add it to the array |
| 66 | + $formattedString = vsprintf($javascript, $gotParams); |
| 67 | + |
| 68 | + array_push($javascriptToPrint, $formattedString); |
| 69 | + } |
| 70 | + |
| 71 | + if(isset($_GET[$key])){ |
| 72 | + unset($_GET[$key]); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Echos the JS for the params to work |
| 77 | +function printJsForParams(){ |
| 78 | + global $javascriptToPrint; |
| 79 | + |
| 80 | + $js = ""; |
| 81 | + foreach ($javascriptToPrint as $value) { |
| 82 | + $js .= "$value\n"; |
| 83 | + } |
| 84 | + |
| 85 | + echo "<script>$(document).ready(function(){ |
| 86 | + setTimeout(function(){ // Give the page some time to load all other JS code. |
| 87 | + // Inject the code gotten from the parameters |
| 88 | + $js |
| 89 | + }, 500); |
| 90 | + });</script>"; |
| 91 | +} |
| 92 | + |
| 93 | +?> |
0 commit comments