Browse Source

first commit

Emma 2 years ago
commit
5f6b7a6121
9 changed files with 189 additions and 0 deletions
  1. 0 0
      README.md
  2. 14 0
      ajax.php
  3. 8 0
      backend.php
  4. 1 0
      db.json
  5. 166 0
      index.php
  6. BIN
      lightningbolt.png
  7. BIN
      pikachu.jpg
  8. BIN
      pikachuattack.jpg
  9. BIN
      pikachuattack2.jpg

+ 0 - 0
README.md


+ 14 - 0
ajax.php

@@ -0,0 +1,14 @@
+<?php
+if(isset($_POST['type']) && $_POST['type']==="addNewIp")
+	{
+	$prevJson = json_decode(file_get_contents("db.json"));
+	$newIp = new stdClass();
+	$newIp->name = $_POST['name'];
+	$newIp->schedule = $_POST['schedule'];
+	$newIp->status = "off";
+
+	$prevJson->{$_POST['ip']} = $newIp;
+	file_put_contents("db.json",json_encode($prevJson));
+	echo "did it";
+	}
+?>

+ 8 - 0
backend.php

@@ -0,0 +1,8 @@
+<?php
+// the backend
+
+$string = "hello";
+$hash = hash("sha256",$string,false);
+echo $hash;
+
+?>

+ 1 - 0
db.json

@@ -0,0 +1 @@
+{"asfdsaf":{"name":"asdafsdsfasdddd","schedule":"asddsaf","status":"off"},"asfdsafas":{"name":"asdafsdsfasdddd","schedule":"asddsaf","status":"off"},"192.168.2.254":{"name":"cat petting machine","schedule":"always off","status":"off"},"192.168.2.23":{"name":"coffee machine","schedule":"","status":"off"},"192.168.2.45":{"name":"super computer","schedule":"","status":"off"},"108.66.220.129":{"name":"cool computer","schedule":"always on","status":"off"},"192.168.2.54":{"name":"raspberry pi","schedule":"","status":"off"}}

+ 166 - 0
index.php

@@ -0,0 +1,166 @@
+<html>
+<head>
+<title>pikachu</title>
+<style>
+* {font-size:20px;font-family:sans-serif;}
+body {width:40vw;margin-right:auto;margin-left:auto;}
+devices {display:block;}
+buttons {display:block;margin:40px;}
+button {background-color:#f1d6ae;border:2px solid #8a6d60;padding:10px;border-radius:10px;}
+device {display:block;}
+.on {}
+.off {color:#f7461f;}
+.showMore, #addNew {border:none;background-color:transparent;color:#8a6d60;}
+#addNew {margin-top:20px;}
+info, .addNew {margin:10px 30px;background-color:#ffe99f;padding:20px;}
+info {display:none;}
+#pikachu {position:absolute;z-index:-1;opacity:.3;width:40vw;}
+#title {font-size:40px;padding:40px 0px;font-weight:bold;}
+.result {
+	position: fixed;
+    width: 80vw;
+    height: 80vh;
+    background-color: yellow;
+    top: 10vh;
+    left: 10vw;
+    font-size:60px;
+    text-align:center;
+    border-radius:30px;
+    }
+.result div {font-size:60px;margin: 60px;}
+.result img {height:40vh;display:block;margin-right:auto;margin-left:auto;}
+.success {background-color:#6cdc32;}
+.failure {background-color:#dc7932;}
+</style>
+</head>
+<body>
+<img id="pikachu" src="pikachu.jpg" />
+<div id="title">pikachu: turn on and off ip addresses</div>
+<devices>
+	<?php
+	$knownIps = json_decode(file_get_contents("db.json"),TRUE);
+	$count = 0;
+	// loop through all of the known ip's and display them
+	foreach($knownIps as $ip=>$info)
+		{
+		echo "<device class='{$info['status']}'>";
+		echo "<input type='checkbox' id='device{$count}' value='{$ip}' /> ";
+		echo "<label for='device{$count}'>".$info['name']."</label> ";
+		echo "<button class='showMore'>[?]</button>";
+		echo "<info>";
+		echo "ip address: ".$ip."<br />";
+		echo "schedule: ".($info['schedule']=="" ? "none specified" : $info['schedule']);
+		echo "</info>";
+		echo "</device>";
+		$count++;
+		}
+	?>
+<button id="addNew">[+] add new ip</button>
+</devices>
+<buttons>
+	<button id="turnOn" class="on">turn on</button>
+	<button id="turnOff" class="off">turn off</button>
+</buttons>
+<script type="text/javascript" src="jquery.js"></script>
+<script type="text/javascript">
+$(".showMore").click(function()
+	{
+	$(this).siblings("info").toggle();
+	});
+$("#turnOff,#turnOn").click(function()
+	{
+	var count = 0;
+	// figure out which boxes were checked
+	$.each($("devices input[type='checkbox']:checked"),function()
+		{
+		console.log($(this).val());
+		count++;
+		});
+	if(count>0 && $(this)[0].id==="turnOff")
+		{
+		showResult("turned off","success");
+		}
+	if(count>0 && $(this)[0].id==="turnOn")
+		{
+		showResult("turned on","success");
+		}
+	});
+$("#addNew").click(function()
+	{
+	// remove any existing add new boxes
+	if($(".addNew").length>0) {$(".addNew").remove();}
+	else
+		{
+		// create the html for the add new box
+		var html = "<div class='addNew'><table>";
+		html += "<tr><td>name</td><td><input type='text' id='newName' /></td></tr>";
+		html += "<tr><td>ip</td><td><input type='text' id='newIp' /></td></tr>";
+		html += "<tr><td>schedule</td><td><input type='text' id='newSchedule' /></td></tr>";
+		html += "</table>";
+		html += "<button onclick='addNewIp();'>add</button></div>";
+
+		$(this).after(html);
+		}
+	});
+function addNewIp()
+	{
+	var data = {};
+	data.type="addNewIp";
+	data.ip = $("#newIp").val();
+	data.name = $("#newName").val();
+	data.schedule = $("#newSchedule").val();
+	console.log(data);
+	$.ajax(
+		{
+		url:"ajax.php",
+		type:'post',
+		data:data,
+		success:function(data)
+			{
+			showResult("added ip","success");
+			}
+		});
+	}
+function showResult(type,status)
+	{
+	if(status==="failure")
+		{
+		var inner = "FAILED";
+		var image = "";
+		}
+	else
+		{
+		if(type==="turned on")
+			{
+			var inner = "DEVICE(S) TURNED ON!";
+			var image = "pikachuattack2.jpg";
+			}
+		else if(type==="turned off")
+			{
+			var inner = "DEVICE(S) TURNED OFF!";
+			var image = "pikachuattack.jpg";
+			}
+		else if(type==="added ip")
+			{
+			var inner = "IP ADDRESS ADDED!";
+			var image="lightningbolt.png";
+			}
+		else
+			{
+			var inner = "SOMETHING SUCCEEDED!";
+			var image="lightningbolt.png";
+			}
+		}
+	var html = "<div class='result "+status+"'>";
+	html += "<div>";
+	html += inner;
+	html += "<img style='margin-top:10px;' src='"+image+"' />";
+	html += "</div>";
+	html+= "</div>";
+	$("html").append(html);
+	setTimeout(function() {$(".result").remove()},1000);
+	}
+</script>
+
+</body>
+</html>

BIN
lightningbolt.png


BIN
pikachu.jpg


BIN
pikachuattack.jpg


BIN
pikachuattack2.jpg