]> git.lizzy.rs Git - dragonblocks.git/blob - api.php
Implement per-map sky
[dragonblocks.git] / api.php
1 <?php
2         require("login-config.php");
3         error_reporting(E_ERROR | E_PARSE);
4
5         function get_files($path)
6         {
7                 $base = explode("\n", shell_exec("ls $path 2>/dev/null"));
8                 array_pop($base);
9                 echo json_encode($base);
10         }
11
12         function walk_directory($path, $func)
13         {
14                 $data = array();
15
16                 $files = scandir($path);
17
18                 foreach ($files as $filename) {
19                         if ($filename[0] == ".")
20                                 continue;
21
22                         $result = $func($filename, $path . "/" . $filename);
23
24                         $data[$filename] = $result;
25                 }
26
27                 echo json_encode($data);
28         }
29
30         function check_worldname($name)
31         {
32                 return preg_match("/^[a-zA-Z0-9]+$/", $name);
33         }
34
35         function world_exists($name)
36         {
37                 return check_worldname($name) && file_exists("worlds/" . $name);
38         }
39
40         function get_mods($path)
41         {
42                 walk_directory($path, function($modname, $modpath) {
43                         $dependencies = file_get_contents($modpath . "/dependencies.txt");
44
45                         return array(
46                                 "name" => $modname,
47                                 "description" => file_get_contents($modpath . "/description.txt"),
48                                 "dependencies" => $dependencies ? array_values(array_filter(explode("\n", $dependencies))) : array(),
49                                 "path" => $modpath,
50                         );
51                 });
52         }
53
54         switch($_POST["call"]) {
55                 case "getGamemods":
56                         get_mods("game");
57                         break;
58
59                 case "getMods":
60                         get_mods("mods");
61                         break;
62
63                 case "getWorlds":
64                         walk_directory("worlds", function($worldname, $path) {
65                                 return array(
66                                         "name" => $worldname,
67                                         "owned" => is_loggedin() && get_username() == file_get_contents($path . "/owner.txt"),
68                                 );
69                         });
70                         break;
71
72                 case "getTextures":
73                         get_files("textures/* game/*/textures/* mods/*/textures/*");
74                         break;
75
76                 case "getSounds":
77                         get_files("sounds/* game/*/sounds/* mods/*/sounds/*");
78                         break;
79
80                 case "isLoggedin":
81                         echo json_encode(is_loggedin());
82                         break;
83
84                 case "getUsername":
85                         echo get_username();
86                         break;
87
88                 case "saveWorld":
89                         if (! is_loggedin())
90                                 return;
91                         else if (! $_POST["name"])
92                                 return;
93                         else if (! check_worldname($_POST["name"]))
94                                 return;
95
96                         if (! world_exists($_POST["name"]))
97                                 mkdir("worlds/" . $_POST["name"]);
98                         else if (file_get_contents("worlds/" . $_POST["name"] . "/owner.txt") != get_username())
99                                 return;
100
101                         file_put_contents("worlds/" . $_POST["name"] . "/world.json", $_POST["world"]);
102                         file_put_contents("worlds/" . $_POST["name"] . "/owner.txt", get_username());
103                         break;
104
105                 case "commitID":
106                         echo shell_exec("git rev-parse --short HEAD");
107                         break;
108         }
109 ?>