index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <html>
  2. <head>
  3. <title>pikachu</title>
  4. <style>
  5. * {font-size:20px;font-family:sans-serif;}
  6. body {width:40vw;margin-right:auto;margin-left:auto;}
  7. devices {display:block;}
  8. buttons {display:block;margin:40px;}
  9. button {background-color:#f1d6ae;border:2px solid #8a6d60;padding:10px;border-radius:10px;}
  10. device {display:block;}
  11. .on {}
  12. .off {color:#f7461f;}
  13. .showMore, #addNew {border:none;background-color:transparent;color:#8a6d60;}
  14. #addNew {margin-top:20px;}
  15. info, .addNew {margin:10px 30px;background-color:#ffe99f;padding:20px;}
  16. info {display:none;}
  17. #pikachu {position:absolute;z-index:-1;opacity:.3;width:40vw;}
  18. #title {font-size:40px;padding:40px 0px;font-weight:bold;}
  19. .result {
  20. position: fixed;
  21. width: 80vw;
  22. height: 80vh;
  23. background-color: yellow;
  24. top: 10vh;
  25. left: 10vw;
  26. font-size:60px;
  27. text-align:center;
  28. border-radius:30px;
  29. }
  30. .result div {font-size:60px;margin: 60px;}
  31. .result img {height:40vh;display:block;margin-right:auto;margin-left:auto;}
  32. .success {background-color:#6cdc32;}
  33. .failure {background-color:#dc7932;}
  34. </style>
  35. </head>
  36. <body>
  37. <img id="pikachu" src="pikachu.jpg" />
  38. <div id="title">pikachu: turn on and off ip addresses</div>
  39. <devices>
  40. <?php
  41. $knownIps = json_decode(file_get_contents("db.json"),TRUE);
  42. $count = 0;
  43. // loop through all of the known ip's and display them
  44. foreach($knownIps as $ip=>$info)
  45. {
  46. echo "<device class='{$info['status']}'>";
  47. echo "<input type='checkbox' id='device{$count}' value='{$ip}' /> ";
  48. echo "<label for='device{$count}'>".$info['name']."</label> ";
  49. echo "<button class='showMore'>[?]</button>";
  50. echo "<info>";
  51. echo "ip address: ".$ip."<br />";
  52. echo "schedule: ".($info['schedule']=="" ? "none specified" : $info['schedule']);
  53. echo "</info>";
  54. echo "</device>";
  55. $count++;
  56. }
  57. ?>
  58. <button id="addNew">[+] add new ip</button>
  59. </devices>
  60. <buttons>
  61. <button id="turnOn" class="on">turn on</button>
  62. <button id="turnOff" class="off">turn off</button>
  63. </buttons>
  64. <script type="text/javascript" src="jquery.js"></script>
  65. <script type="text/javascript">
  66. $(".showMore").click(function()
  67. {
  68. $(this).siblings("info").toggle();
  69. });
  70. $("#turnOff,#turnOn").click(function()
  71. {
  72. var count = 0;
  73. // figure out which boxes were checked
  74. $.each($("devices input[type='checkbox']:checked"),function()
  75. {
  76. console.log($(this).val());
  77. count++;
  78. });
  79. if(count>0 && $(this)[0].id==="turnOff")
  80. {
  81. showResult("turned off","success");
  82. }
  83. if(count>0 && $(this)[0].id==="turnOn")
  84. {
  85. showResult("turned on","success");
  86. }
  87. });
  88. $("#addNew").click(function()
  89. {
  90. // remove any existing add new boxes
  91. if($(".addNew").length>0) {$(".addNew").remove();}
  92. else
  93. {
  94. // create the html for the add new box
  95. var html = "<div class='addNew'><table>";
  96. html += "<tr><td>name</td><td><input type='text' id='newName' /></td></tr>";
  97. html += "<tr><td>ip</td><td><input type='text' id='newIp' /></td></tr>";
  98. html += "<tr><td>schedule</td><td><input type='text' id='newSchedule' /></td></tr>";
  99. html += "</table>";
  100. html += "<button onclick='addNewIp();'>add</button></div>";
  101. $(this).after(html);
  102. }
  103. });
  104. function addNewIp()
  105. {
  106. var data = {};
  107. data.type="addNewIp";
  108. data.ip = $("#newIp").val();
  109. data.name = $("#newName").val();
  110. data.schedule = $("#newSchedule").val();
  111. console.log(data);
  112. $.ajax(
  113. {
  114. url:"ajax.php",
  115. type:'post',
  116. data:data,
  117. success:function(data)
  118. {
  119. showResult("added ip","success");
  120. }
  121. });
  122. }
  123. function showResult(type,status)
  124. {
  125. if(status==="failure")
  126. {
  127. var inner = "FAILED";
  128. var image = "";
  129. }
  130. else
  131. {
  132. if(type==="turned on")
  133. {
  134. var inner = "DEVICE(S) TURNED ON!";
  135. var image = "pikachuattack2.jpg";
  136. }
  137. else if(type==="turned off")
  138. {
  139. var inner = "DEVICE(S) TURNED OFF!";
  140. var image = "pikachuattack.jpg";
  141. }
  142. else if(type==="added ip")
  143. {
  144. var inner = "IP ADDRESS ADDED!";
  145. var image="lightningbolt.png";
  146. }
  147. else
  148. {
  149. var inner = "SOMETHING SUCCEEDED!";
  150. var image="lightningbolt.png";
  151. }
  152. }
  153. var html = "<div class='result "+status+"'>";
  154. html += "<div>";
  155. html += inner;
  156. html += "<img style='margin-top:10px;' src='"+image+"' />";
  157. html += "</div>";
  158. html+= "</div>";
  159. $("html").append(html);
  160. setTimeout(function() {$(".result").remove()},1000);
  161. }
  162. </script>
  163. </body>
  164. </html>