diff options
author | Minteck <contact@minteck.org> | 2021-12-12 15:17:06 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2021-12-12 15:17:06 +0100 |
commit | a985b2769f6c3dc907b57841134a293842c06fa6 (patch) | |
tree | 9a12a5b5d49f69357cf9ab05456caab3aa2a5343 | |
parent | dfdb8f100de53f3e0ca6b9143a6aab4c04554830 (diff) | |
download | wolfeye-php-a985b2769f6c3dc907b57841134a293842c06fa6.tar.gz wolfeye-php-a985b2769f6c3dc907b57841134a293842c06fa6.tar.bz2 wolfeye-php-a985b2769f6c3dc907b57841134a293842c06fa6.zip |
Add answers
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | .idea/sshConfigs.xml | 2 | ||||
-rw-r--r-- | .idea/webServers.xml | 2 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | assets/instant.js | 22 | ||||
-rw-r--r-- | ratelimited/answer.php | 99 |
6 files changed, 125 insertions, 6 deletions
@@ -1,4 +1,5 @@ private/ratelimiting/* private/cache/* private/db.json -private/dictionary.json
\ No newline at end of file +private/dictionary.json +private/keys/*
\ No newline at end of file diff --git a/.idea/sshConfigs.xml b/.idea/sshConfigs.xml index 26053e5..66ef29d 100644 --- a/.idea/sshConfigs.xml +++ b/.idea/sshConfigs.xml @@ -2,7 +2,7 @@ <project version="4"> <component name="SshConfigs"> <configs> - <sshConfig authType="OPEN_SSH" host="minteck.org" id="319e7a2c-5091-4381-a0bc-0aad15d58f41" port="22" nameFormat="DESCRIPTIVE" username="root" useOpenSSHConfig="true" /> + <sshConfig authType="OPEN_SSH" host="minteck.org" id="319e7a2c-5091-4381-a0bc-0aad15d58f41" port="22465" nameFormat="DESCRIPTIVE" username="root" useOpenSSHConfig="true" /> </configs> </component> </project>
\ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml index 933b983..e9b027b 100644 --- a/.idea/webServers.xml +++ b/.idea/webServers.xml @@ -3,7 +3,7 @@ <component name="WebServers"> <option name="servers"> <webServer id="4ddeb4db-477c-482b-8bc1-be8e25957b40" name="Familine"> - <fileTransfer accessType="SFTP" host="minteck.org" port="22" sshConfigId="319e7a2c-5091-4381-a0bc-0aad15d58f41" sshConfig="root@minteck.org:22 agent" authAgent="true"> + <fileTransfer accessType="SFTP" host="minteck.org" port="22465" sshConfigId="319e7a2c-5091-4381-a0bc-0aad15d58f41" sshConfig="root@minteck.org:22465 agent" authAgent="true"> <advancedOptions> <advancedOptions dataProtectionLevel="Private" keepAliveTimeout="0" passiveMode="true" shareSSLContext="true" /> </advancedOptions> diff --git a/README.md b/README.md new file mode 100644 index 0000000..492a3d9 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# WolfEye + +* Put your WolframAlpha AppID in the `/private/keys/wolfram` file.
\ No newline at end of file diff --git a/assets/instant.js b/assets/instant.js index 439daab..bc643ab 100644 --- a/assets/instant.js +++ b/assets/instant.js @@ -8,9 +8,25 @@ )); document.getElementById("details").innerHTML = "<small id='details-source'>from <a id='details-source-link' href='https://duckduckgo.com' target='_blank'>DuckDuckGo</a>:</small><p>" + instant.Abstract + "</p><p><i>— <a href='" + instant.AbstractURL + "' id='details-data-link' target='_blank'>" + instant.AbstractSource + "</a></i></p>"; - if (instant.Abstract.trim() !== "") document.getElementById("details").style.display = "block"; - if (instant.Abstract.trim() !== "") document.getElementById("results").classList.add("with-details"); - if (instant.Abstract.trim() !== "") try {document.getElementsByClassName("dym-section")[0].outerHTML = ""; } catch (e) {} + + if (instant.Abstract.trim() !== "") { + document.getElementById("details").style.display = "block"; + document.getElementById("results").classList.add("with-details"); + try {document.getElementsByClassName("dym-section")[0].outerHTML = ""; } catch (e) {} + } else { + answer = (await ( + await ( + await window.fetch("/ratelimited/answer.php?q=" + query) + ).blob() + ).text() + ) + if (answer.trim() !== "Wolfram|Alpha did not understand your input" && answer.trim() !== "No short answer available") { + document.getElementById("details").innerHTML = "<small id='details-source'>from <a id='details-source-link' href='https://www.wolframalpha.com/input/?i=" + encodeURI(query) + "' target='_blank'>Wolfram|Alpha</a>:</small><p>" + answer + "</p><p><details><summary style='cursor:pointer;'>Disclaimer</summary>This information is provided by Wolfram|Alpha. Results and information from this site are not a certified or definitive source of information that can be relied on for legal, financial, medical, life-safety or any other critical purposes.</details></p>"; + document.getElementById("details").style.display = "block"; + document.getElementById("results").classList.add("with-details"); + try {document.getElementsByClassName("dym-section")[0].outerHTML = ""; } catch (e) {} + } + } if (!instant._WolfEye_External) { try { document.getElementById("details-source").innerHTML = " " } catch (e) {} diff --git a/ratelimited/answer.php b/ratelimited/answer.php new file mode 100644 index 0000000..8bc9707 --- /dev/null +++ b/ratelimited/answer.php @@ -0,0 +1,99 @@ +<?php + +if (isset($_GET['q'])) { + $query = strtolower(substr($_GET['q'], 0, 200)); + $query = preg_replace("/[^A-Za-z0-9 ]/", '', preg_replace("/[\.]/", ' ', $query)); +} else { + header("Location: /"); + die(); +} + +$addr = sha1($_SERVER['REMOTE_ADDR']); +$cacheOnly = false; +$rl = [ + "local" => -1, + "global" => -1, + "blocked" => false +]; + +if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr)) { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr, "1|" . date('YmdHi')); +} else { + $requests = (int)explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr))[0]; + $date = explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr))[1]; + if ($date === date('YmdHi')) { + $requests++; + if ($requests > 3) { + $cacheOnly = true; + $rl["blocked"] = true; + } + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr, $requests . "|" . date('YmdHi')); + } else { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/" . $addr, "1|" . date('YmdHi')); + } + + $rl["local"] = $requests; +} + +if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global")) { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global", "1|" . date('YmdHi')); +} else { + $requests = (int)explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global"))[0]; + $date = explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global"))[1]; + if ($date === date('YmdHi')) { + $requests++; + if ($requests >= 20) { + $cacheOnly = true; + $rl["blocked"] = true; + } + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global", $requests . "|" . date('YmdHi')); + } else { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_global", "1|" . date('YmdHi')); + } + + $rl["global"] = $requests; +} + +if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month")) { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month", "1|" . date('Ym')); +} else { + $requests = (int)explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month"))[0]; + $date = explode("|", file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month"))[1]; + if ($date === date('Ym')) { + $requests++; + if ($requests >= 1990) { + $cacheOnly = true; + $rl["blocked"] = true; + } + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month", $requests . "|" . date('Ym')); + } else { + file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/private/ratelimiting/_month", "1|" . date('Ym')); + } + + $rl["monthly"] = $requests; +} + +header("Content-Type: text/plain"); +$text = urlencode($query); + +if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/private/answers/" . str_replace(" ", "_", $query))) { + $ds = "local"; + $data = file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/private/answers/" . str_replace(" ", "_", $query)); +} else if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/private/cache/answer_" . str_replace(" ", "_", $query))) { + $ds = "cache"; + $data = file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/private/cache/answer_" . str_replace(" ", "_", $query)); +} else { + if ($cacheOnly) { + die(); + } else { + $ds = "online"; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "https://api.wolframalpha.com/v1/result?appid=" . urlencode(trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/private/keys/wolfram"))) . "&i=" . urlencode($query)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + $data = curl_exec($ch); + curl_close($ch); + file_put_contents($_SERVER["DOCUMENT_ROOT"] . "/private/cache/answer_" . str_replace(" ", "_", $query), $data); + } +} + +die($data);
\ No newline at end of file |