Преглед изворни кода

remove oracles, sorry cynic

lolcat пре 4 недеља
родитељ
комит
6602df4fa9
6 измењених фајлова са 0 додато и 369 уклоњено
  1. 0 36
      oracles/base.php
  2. 0 165
      oracles/calc.php
  3. 0 40
      oracles/encoder.php
  4. 0 54
      oracles/numerics.php
  5. 0 45
      oracles/time.php
  6. 0 29
      web.php

+ 0 - 36
oracles/base.php

@@ -1,36 +0,0 @@
-<?php
-abstract class oracle {
-	// some info to spit out alongside the result, so the user knows
-	//  what exactly is giving out the answer. prevents confusion
-	//   about what oracle is answering them for ambiguous queries.
-	public $info = [
-		"name" => "some oracle"
-	];
-	// this function should take in a query string search from $_GET,
-	//  and return a bool determining whether or not it is a question
-	//   intended for the oracle.
-	public function check_query($q) {
-		return false;
-	}
-	// produce the correct answer for the query using the oracle.
-	//  note: if it becomes apparent /during generation/ that the
-	//   query is not in fact for the oracle, returning an empty
-	//    string will kill the oracle pane.
-	// answer format: ["ans1 title" => "ans1", ...]
-	public function generate_response($q) {
-		return "";
-	}
-}
-// backwards compatibility
-if (!function_exists('str_starts_with')) {
-	function str_starts_with($haystack, $needle) {
-		return strncmp($haystack, $needle, strlen($needle)) === 0;;
-	}
-}
-if (!function_exists('str_contains')) {
-	function str_contains($haystack, $needle) {
-		return strpos((string)$haystack, (string)$needle) !== false;
-	}
-}
-
-?>

+ 0 - 165
oracles/calc.php

@@ -1,165 +0,0 @@
-<?php
-include_once("oracles/base.php");
-class calculator extends oracle {
-	public $info = [
-		"name" => "calculator"
-	];
-	public function check_query($q) {
-		// straight numerics should go to that oracle
-		if (is_numeric($q)) {
-			return false;
-		}
-		// all chars should be number-y or operator-y
-		$char_whitelist = str_split("1234567890.+-/*^%() ");
-		foreach (str_split($q) as $char) {
-			if (!in_array($char, $char_whitelist)) {
-				return false;
-			}
-		}
-		return true;
-	}
-	// a custom parser and calculator because FUCK YUO, libraries are
-	//  gay.
-	public function generate_response($q)
-	{
-		$nums = str_split("1234567890.");
-		$ops = str_split("+-/*^%;");
-		$grouping = str_split("()");
-
-		$q = str_replace(" ", "", $q);
-
-		// backstop for the parser so it catches the last
-		//  numeric token
-		$q .= ";"; 
-
-		// the following comments refer to this example input:
-		//  21+9*(3+2^9)+1
-
-		// 2-length lists of the following patterns:
-		//  ["n" (umeric), <some number>]
-		//  ["o" (perator), "<some operator>"]
-		//  ["g" (roup explicit), <"(" or ")">]
-		// e.g. [["n", 21], ["o", "+"], ["n", 9], ["o", *],
-		//       ["g", "("], ["n", 3], ["o", "+"], ["n", 2],
-		//       ["o", "^"], ["n", 9], ["g", ")"], ["o", "+"],
-		//       ["n", "1"]]
-		$tokens = array();
-		$dragline = 0;
-		foreach(str_split($q) as $i=>$char) {
-			if (in_array($char, $nums)) {
-				continue;
-			}
-			elseif (in_array($char, $ops) || in_array($char, $grouping)) {
-				// hitting a non-numeric implies everything since the
-				//  last hit has been part of a number
-				$capture = substr($q, $dragline, $i - $dragline);
-				// prevent the int cast from creating imaginary
-				//  ["n", 0] tokens
-				if ($capture != "") {
-					if (substr_count($capture, ".") > 1) {
-						return "";
-					}
-					array_push($tokens, ["n", (float)$capture]);
-				}
-				// reset to one past the current (non-numeric) char
-				$dragline = $i + 1; 
-				// the `;' backstop is not a real token and this should
-				//  never be present in the token list
-				if ($char != ";") {
-					array_push($tokens, [
-						($char == "(" || $char == ")") ? "g" : "o",
-						$char
-					]);
-				}
-			}
-			else {
-				return "";
-			}
-		}
-
-		// two operators back to back should fail
-		for ($i = 1; $i < count($tokens); $i++) {
-			if ($tokens[$i][0] == "o" && $tokens[$i-1][0] == "o") {
-				return "";
-			}
-		}
-
-		// no implicit multiplication
-		for ($i = 0; $i < count($tokens) - 1; $i++) {
-			if ($tokens[$i][0] == "n" && $tokens[$i+1] == ["g", "("]) {
-				return "";
-			}
-		}
-
-		//strategy:
-		// traverse to group open (if there is one)
-		//  - return to start with the internals
-		// traverse to ^, attack token previous and after
-		// same but for *, then / then + then -
-		// poppers all teh way down
-		try {
-			return [
-				substr($q, 0, strlen($q)-1)." = " => $this->executeBlock($tokens)[0][1]
-			];
-		}   
-		catch (\Throwable $e) {
-			if (get_class($e) == "DivisionByZeroError") {
-				return [
-					$q." = " => "Division by Zero Error!!"
-				];
-			}
-			return "";
-		}
-	}
-	public function executeBlock($tokens) {
-		if (count($tokens) >= 2 && $tokens[0][0] == "o" && $tokens[0][1] == "-" && $tokens[1][0] == "n") {
-			array_splice($tokens, 0, 2, [["n", -1 * (float)$tokens[1][1]]]);
-		}
-		if (count($tokens) > 0 && $tokens[0][0] == "o" || $tokens[count($tokens)-1][0] == "o") {
-			throw new Exception("Error Processing Request", 1);
-		}
-		while (in_array(["g", "("], $tokens)) {
-			$first_open = array_search(["g", "("], $tokens);
-			$enclosedality = 1;
-			for ($i = $first_open+1; $i < count($tokens); $i++) {
-				if ($tokens[$i][0] == "g") {
-					$enclosedality += ($tokens[$i][1] == "(") ? 1 : -1;
-				}
-				if ($enclosedality == 0) {
-					array_splice($tokens, 
-						$first_open, 
-						$i+1 - $first_open, 
-						$this->executeBlock(
-							array_slice($tokens, $first_open+1, $i-1 - $first_open)
-						)
-					);
-					break;
-				}
-			}
-		}
-		$operators_in_pemdas_order = [
-			"^" => (fn($x, $y) => $x ** $y),
-			"*" => (fn($x, $y) => $x * $y),
-			"/" => (fn($x, $y) => $x / $y), 
-			"%" => (fn($x, $y) => $x % $y),
-			"+" => (fn($x, $y) => $x + $y), 
-			"-" => (fn($x, $y) => $x - $y)
-		];
-		foreach ($operators_in_pemdas_order as $op=>$func) {
-			while (in_array(["o", $op], $tokens)) {
-				for ($i = 0; $i < count($tokens); $i++) {
-					if ($tokens[$i] == ["o", $op]) {
-						array_splice(
-							$tokens,
-							$i-1,
-							3,
-							[["n", (string)($func((float)$tokens[$i-1][1], (float)$tokens[$i+1][1]))]]
-						);
-					}
-				}
-			}
-		}
-		return $tokens;
-	}
-}
-?>

+ 0 - 40
oracles/encoder.php

@@ -1,40 +0,0 @@
-<?php
-include_once("oracles/base.php");
-class encoder extends oracle {
-	public $info = [
-		"name" => "text encoder/hasher"
-	];
-	private $special_types = [
-		"rot13",
-		"base64"
-	];
-	public function check_query($q) {
-		$types = array_merge($this->special_types, hash_algos());
-		foreach ($types as $type) {
-			$type .= " ";
-			if (str_starts_with($q, $type)) {
-				return true;
-			}
-		}
-		return false;
-	}
-	public function generate_response($q)
-	{
-		$type = explode(" ", $q)[0];
-		$victim = substr($q, strlen($type)+1);
-		if (in_array($type, hash_algos())) {
-			return [$type." hash" => hash($type, $victim)];
-		}
-		switch ($type) {
-			case "rot13":
-				return ["rot13 encoded" => str_rot13($victim)];
-			case "base64":
-				return [
-					"base64 encoded" => base64_encode($victim),
-					"base64 decoded" => base64_decode($victim)
-				];
-		}
-		return "";
-	}
-}
-?>

+ 0 - 54
oracles/numerics.php

@@ -1,54 +0,0 @@
-<?php
-include_once("oracles/base.php");
-class numerics extends oracle {
-	public $info = [
-		"name" => "numeric base conversion"
-	];
-	public function check_query($q) {
-		if (str_contains($q, " ")) {
-			return false;
-		}
-
-		$q = strtolower($q);
-
-		$profiles = [
-			["0x", str_split("0123456789abcdef")],
-			["", str_split("1234567890")],
-			["b", str_split("10")]
-		];
-
-		foreach ($profiles as $profile) {
-			$good = true;
-			$good &= str_starts_with($q, $profile[0]);
-			$nq = substr($q, strlen($profile[0]));
-			foreach (str_split($nq) as $c) {
-				$good &= in_array($c, $profile[1]);
-			}
-			if ($good) {
-				return true;
-			}
-		}
-		return false;
-	}
-	public function generate_response($q) {
-		$n = 0;
-		if (str_starts_with($q, "0x")) {
-			$nq = substr($q, strlen("0x"));
-			$n = hexdec($nq);
-		}
-		elseif (str_starts_with($q, "b")) {
-			$nq = substr($q, strlen("b"));
-			$n = bindec($nq);
-		}
-		else {
-			$n = (int)$q;
-		}
-		return [
-			"decimal (base 10)" => (string)$n,
-			"hexadecimal (base 16)" => "0x".(string)dechex($n),
-			"binary (base 2)" => "b".(string)decbin($n),
-			"" => "binary inputs should be prefixed with 'b', hex with '0x'."
-		];
-	}
-}
-?>

+ 0 - 45
oracles/time.php

@@ -1,45 +0,0 @@
-<?php
-include_once("oracles/base.php");
-class time extends oracle {
-	public $info = [
-		"name" => "what time is it?"
-	];
-	public function check_query($q) {
-		$prompts = [
-			"what", "time", "is", "it",
-			"right", "now", "the", "current",
-			"get", "date"
-		];
-		$q = str_replace(",", "", $q);
-		$q = str_replace("?", "", $q);
-		$q = str_replace("what's", "what is", $q);
-		$oq = $q;
-		$q = explode(" ", $q);
-		$count = 0;
-		foreach ($q as $word) {
-			if (in_array($word, $prompts)) {
-				$count++;
-			}
-		}
-		// remove one from total count if a timezone is specified
-		return ($count/(count($q) + (str_contains($oq, "tz:") ? -1 : 0))) > 3/4;
-	}
-	public function generate_response($q) {
-		$timezone = timezone_name_from_abbr("UTC");
-		foreach (explode(" ", $q) as $word) {
-			if (str_starts_with($word, "tz:")) {
-				$decltz = timezone_name_from_abbr(substr($word, 3, 3));
-				if ($decltz) {
-					$timezone = $decltz;
-				}
-			}
-		}
-		date_default_timezone_set($timezone);
-		return [
-			"The time in ".$timezone => date("H:i:s"),
-			" " => date("l, F jS"),
-			"" => "include the string \"tz:XXX\" to use timezone XXX"
-		];
-	}
-}
-?>

+ 0 - 29
web.php

@@ -37,35 +37,6 @@ try{
 	$frontend->drawscrapererror($error->getMessage(), $get, "web", $payload["timetaken"]);
 }
 
-/*
-	Prepend Oracle output, if applicable
-*/
-include("oracles/encoder.php");
-include("oracles/calc.php");
-include("oracles/time.php");
-include("oracles/numerics.php");
-$oracles = [new calculator(), new encoder(), new time(), new numerics()];
-$fortune = "";
-foreach ($oracles as $oracle) {
-	if ($oracle->check_query($_GET["s"])) {
-		$resp = $oracle->generate_response($_GET["s"]);
-		if ($resp != "") {
-			$fortune .= "<div class=\"infobox\">";
-			foreach ($resp as $title => $r) {
-				if ($title) {
-					$fortune .= "<h3>".htmlspecialchars($title)."</h3><div class=\"code\">".htmlspecialchars($r)."</div>";
-				}
-				else {
-					$fortune .= "<i>".$r."</i><br>";
-				}
-			}
-			$fortune .= "<small>Answer provided by oracle: ".$oracle->info["name"]."</small></div>";
-		}
-		break;
-	}
-}
-$payload["left"] = $fortune;
-
 $answerlen = 0;
 
 /*