diff --git a/.gitignore b/.gitignore index 7d18118..45b1e23 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ error/ stats/ vendor/ settings.php -*.log \ No newline at end of file +*.log +database.mwb.bak \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 03eb613..a5b2c80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ -FROM php:5-apache -MAINTAINER Skylar Ittner -RUN apt-get update && apt-get upgrade -y -RUN apt-get install git openssh-server -# nuke the webroot -WORKDIR /var/www -RUN rm -rf html && mkdir html -WORKDIR /var/www/html -# install the server crap (private repo for now, thx GitHub Student) -RUN git clone https://skylarmt-script-account:scriptb0t@github.com/skylarmt/TerranQuestServer.git . -# 0wn3d +FROM php:5-apache +MAINTAINER Skylar Ittner +RUN apt-get update && apt-get upgrade -y +RUN apt-get install git openssh-server +# nuke the webroot +WORKDIR /var/www +RUN rm -rf html && mkdir html +WORKDIR /var/www/html +# install the server crap (private repo for now, thx GitHub Student) +RUN git clone https://skylarmt-script-account:scriptb0t@github.com/skylarmt/TerranQuestServer.git . +# 0wn3d RUN cd .. && chown -R www-data:www-data html \ No newline at end of file diff --git a/database.mwb.bak b/database.mwb.bak deleted file mode 100644 index 5d07e5f..0000000 Binary files a/database.mwb.bak and /dev/null differ diff --git a/getweather.php b/getweather.php index 29069f5..a50ac8a 100644 --- a/getweather.php +++ b/getweather.php @@ -2,36 +2,9 @@ require 'required.php'; require 'onlyloggedin.php'; +require 'weather_inc.php'; -// Validate input -if (is_empty($VARS['lat']) || is_empty($VARS['long'])) { - sendError("Missing information.", true); -} -if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['lat'])) { - sendError("Latitude (lat) is in the wrong format.", true); -} -if (!preg_match('/-?[0-9]{1,3}\.[0-9]{2,}/', $VARS['long'])) { - sendError("Longitude (long) is in the wrong format.", true); -} - -// Round to 2 digits (approx. 1.1km) -$lat = number_format((float) $VARS['lat'], 2, '.', ''); -$long = number_format((float) $VARS['long'], 2, '.', ''); - -// Delete old records -$database->delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-1 hour'))]); - -// If we don't get a cache hit, request from the API -if (!$database->has('weathercache', ["AND" => ["latitude" => $lat, "longitude" => $long]])) { - $weather = json_decode(file_get_contents("https://api.darksky.net/forecast/" . DARKSKY_APIKEY . "/$lat,$long"), TRUE); - $currentjson = json_encode($weather['currently']); - $database->insert('weathercache', ["latitude" => $lat, "longitude" => $long, "#date" => "NOW()", "currentjson" => $currentjson]); -} - -// Build output array -$output = ["status" => "OK", "currently" => []]; -// Get the cached record and put it in the output array -$output['currently'] = json_decode($database->select('weathercache', 'currentjson', ["AND" => ["latitude" => $lat, "longitude" => $long]])[0], TRUE); +$output['currently'] = $currently; // Re-encode the data to JSON and send to client echo json_encode($output); \ No newline at end of file diff --git a/nbproject/project.properties b/nbproject/project.properties index d37ef95..8b7302c 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,7 +1,7 @@ -include.path=${php.global.include.path} -php.version=PHP_54 -source.encoding=UTF-8 -src.dir=. -tags.asp=false -tags.short=false -web.root=. +include.path=${php.global.include.path} +php.version=PHP_54 +source.encoding=UTF-8 +src.dir=. +tags.asp=false +tags.short=false +web.root=. diff --git a/nbproject/project.xml b/nbproject/project.xml index 7951c6b..00ef7d0 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -1,9 +1,9 @@ - - - org.netbeans.modules.php.project - - - TerranQuest Server - - - + + + org.netbeans.modules.php.project + + + TerranQuest Server + + + diff --git a/weather_inc.php b/weather_inc.php new file mode 100644 index 0000000..8ff878c --- /dev/null +++ b/weather_inc.php @@ -0,0 +1,35 @@ +delete('weathercache', ["date[<]" => date('Y-m-d H:i:s', strtotime('-1 hour'))]); + +// If we don't get a cache hit, request from the API +if (!$database->has('weathercache', ["AND" => ["latitude" => $lat, "longitude" => $long]])) { + $weather = json_decode(file_get_contents("https://api.darksky.net/forecast/" . DARKSKY_APIKEY . "/$lat,$long"), TRUE); + $currentjson = json_encode($weather['currently']); + $database->insert('weathercache', ["latitude" => $lat, "longitude" => $long, "#date" => "NOW()", "currentjson" => $currentjson]); +} + +// Get the cached record and put it in a variable +$currently = json_decode($database->select('weathercache', 'currentjson', ["AND" => ["latitude" => $lat, "longitude" => $long]])[0], TRUE);