index.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. $error_reporting = 1; // 1 = errors 0 = none
  3. if ($error_reporting == 1){
  4. ini_set('display_errors', 1);
  5. ini_set('display_startup_errors', 1);
  6. error_reporting(E_ALL);}
  7. //Make array $addr[] from url
  8. $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  9. $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  10. if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  11. $uri = '/' . trim($uri, '/');
  12. global $addr;
  13. $routes = array();
  14. $routes = explode('/', $uri);
  15. foreach($routes as $route)
  16. {
  17. if(trim($route) != '')
  18. array_push($routes, $route);
  19. if ($route != ""){
  20. $addr[] = $route;
  21. }
  22. }
  23. if ($addr[0]!="modify"){
  24. render($addr);}
  25. else{
  26. $db = json_decode(readDB(), true);
  27. $db["lol2"] = "test2";
  28. writeDB(json_encode($db));
  29. }
  30. function render($address){
  31. $db = readDB();
  32. echo $db;
  33. $web = json_decode($db, True);
  34. $route = implode(",", $address);
  35. if (array_key_exists($route, $web)){
  36. echo $web[$route];}
  37. else {
  38. echo "404";
  39. }
  40. }
  41. function readDB(){
  42. $myfile = fopen("database.db", "r");
  43. return fread($myfile,filesize("database.db"));
  44. fclose($myfile);
  45. }
  46. function writeDB($data){
  47. $myfile = fopen("database.db", "w");
  48. fwrite($myfile, $data);
  49. fclose($myfile);
  50. }
  51. ?>